1 /************************************************************************ 2 Copyright (c) 2015, The Linux Foundation. All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions are 6 met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above 10 copyright notice, this list of conditions and the following 11 disclaimer in the documentation and/or other materials provided 12 with the distribution. 13 * Neither the name of The Linux Foundation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 ************************************************************************/ 29 30 /** 31 * @file datatop_opt.c 32 * @brief Adds getopt functionality for CLI commands. 33 * 34 * Contains method for getopt functionality used for parsing 35 * the CLI arguments into executable commands. Handles 36 * errors which arise when parsing. 37 */ 38 39 #include <stdio.h> 40 #include <string.h> 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <ctype.h> 44 #include <getopt.h> 45 #include "datatop_opt.h" 46 #include "datatop_interface.h" 47 #include "datatop_linked_list.h" 48 #include "datatop_fileops.h" 49 50 /** 51 * @brief Populate the comand line options with sane defaults 52 * 53 * @param clopts Struct used to hold data regarding CLI arguments. 54 */ 55 void dtop_load_default_options(struct cli_opts *clopts) 56 { 57 memset(clopts, 0, sizeof(struct cli_opts)); 58 59 clopts->priority = DEFAULT_NICE; 60 } 61 62 /** 63 * @brief Parses all CLI commands for main() to execute. 64 * 65 * @param clopts Struct used to hold data regarding CLI arguments. 66 * @param argc Parameter used to read CLI commands from. 67 * @param argv Parameter used to read CLI arguments from. 68 * @return PARSE_SUCCESS - CLI arguments read successfully, 69 * @return PARSE_FAILURE - CLI arguments and/or input not valid. 70 * @return PARSE_FORCE_EXIT - Exit immediately, print help options. 71 */ 72 int dtop_parse_cli_opts(struct cli_opts *clopts, int argc, char **argv) 73 { 74 int option; 75 76 if (!clopts || !*argv) { 77 printf("Internal Error: Null Pointer\n"); 78 goto error; 79 } 80 81 while ((option = getopt(argc, argv, "phi:t:w:s:n:")) != -1) { 82 switch (option) { 83 case 'p': 84 clopts->print_cl = OPT_CHOSE; 85 break; 86 87 case 'h': 88 dtop_print_help_opts(); 89 return PARSE_FORCE_EXIT; 90 break; 91 92 case 'n': 93 clopts->priority = strtol(optarg, 0, 10); 94 if (clopts->priority > 19 || clopts->priority < -20) { 95 printf("Argument for -n is not valid. "); 96 printf("Must be between -20 and 19.\n"); 97 goto error; 98 } 99 break; 100 101 case 'i': 102 clopts->poll_per = strtol(optarg, 0, 10); 103 if (clopts->poll_per <= 0) { 104 printf("Argument for -i is not valid. "); 105 printf("Must be positive integer.\n"); 106 goto error; 107 } 108 break; 109 110 case 't': 111 clopts->poll_time = strtol(optarg, 0, 10); 112 clopts->poll_time_selected = POLL_TIME_SELECTED; 113 if (clopts->poll_time <= 0) { 114 printf("Argument for -t is not valid. "); 115 printf("Must be positive integer.\n"); 116 goto error; 117 } 118 break; 119 120 case 'w': 121 if (dtop_check_writefile_access(optarg) == VALID) { 122 clopts->file_name = optarg; 123 clopts->print_csv = OPT_CHOSE; 124 } else { 125 goto error; 126 } 127 break; 128 129 case 's': 130 if (dtop_check_writefile_access(optarg) == VALID) 131 clopts->snapshot_file = optarg; 132 else 133 goto error; 134 break; 135 136 case '?': 137 default: 138 goto error; 139 } 140 } 141 142 if (clopts->poll_time == 0) { 143 if (clopts->print_csv == 1) 144 clopts->poll_time = POLL_NOT_SPECIFIED; 145 else 146 clopts->poll_time = POLL_TIME_DEFAULT; 147 } 148 if (clopts->poll_per == 0) 149 clopts->poll_per = DEFAULT_POLL_INTERVAL; 150 151 return PARSE_SUCCESS; 152 153 error: 154 printf("See datatop -h for help\n"); 155 return PARSE_FAILURE; 156 } 157 158 /** 159 * @brief Prints the options the user has for the program to terminal. 160 */ 161 void dtop_print_help_opts(void) 162 { 163 printf("The following datatop commands are:\n"); 164 printf("\t-p\t\t\tPrint output to terminal\n"); 165 printf("\t-i , seconds\t\tSpecify polling period\n"); 166 printf("\t-t , seconds\t\tSpecify polling duration\n"); 167 printf("\t-w , file name (.csv)\tWrite output to a file\n"); 168 printf("\t-s , file name\t\tPrint system snapshot to a file\n"); 169 printf("\t-n , nice value\t\tSet niceness (default 19)\n"); 170 printf("\t-h\t\t\tGet help\n"); 171 } 172 173 174 /** 175 * @brief Prints the interactive options the user can enter during runtime. 176 */ 177 void dtop_print_interactive_opts(void) 178 { 179 printf("The following interactive commands are:\n"); 180 printf("\tq | quit\tTerminate program at any time\n"); 181 printf("\ti\t\tPrint dp differences, reset initial dp values\n"); 182 printf("\tl\t\tPrint dp differences since last reset\n"); 183 printf("\n"); 184 } 185