1 /* 2 * ReactOS log2lines 3 * Written by Jan Roeloffzen 4 * 5 * - Option init and parsing 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <getopt.h> 12 13 #include "util.h" 14 #include "compat.h" 15 #include "config.h" 16 #include "help.h" 17 #include "log2lines.h" 18 #include "options.h" 19 20 char *optchars = "bcd:fFhl:L:mMP:rsS:tTuUvz:"; 21 int opt_buffered = 0; // -b 22 int opt_help = 0; // -h 23 int opt_force = 0; // -f 24 int opt_exit = 0; // -e 25 int opt_verbose = 0; // -v 26 int opt_console = 0; // -c 27 int opt_mark = 0; // -m 28 int opt_Mark = 0; // -M 29 char *opt_Pipe = NULL; // -P 30 int opt_quit = 0; // -q (cli only) 31 int opt_cli = 0; // (cli internal) 32 int opt_raw = 0; // -r 33 int opt_stats = 0; // -s 34 int opt_Source = 0; // -S <opt_Source>[+<opt_SrcPlus>][,<sources_path>] 35 int opt_SrcPlus = 0; // -S <opt_Source>[+<opt_SrcPlus>][,<sources_path>] 36 int opt_twice = 0; // -t 37 int opt_Twice = 0; // -T 38 int opt_undo = 0; // -u 39 int opt_redo = 0; // -U 40 char opt_dir[PATH_MAX]; // -d <opt_dir> 41 char opt_logFile[PATH_MAX]; // -l|L <opt_logFile> 42 char *opt_mod = NULL; // -mod for opt_logFile 43 char opt_7z[PATH_MAX]; // -z <opt_7z> 44 char opt_scanned[LINESIZE]; // all scanned options 45 char opt_SourcesPath[LINESIZE]; //sources path 46 47 /* optionInit returns 0 for normal operation, and -1 in case just "loglines.exe" was written. 48 In such case, the help is shown */ 49 50 int optionInit(int argc, const char **argv) 51 { 52 int i; 53 char *s; 54 55 opt_mod = "a"; 56 strcpy(opt_dir, ""); 57 strcpy(opt_logFile, ""); 58 strcpy(opt_7z, CMD_7Z); 59 strcpy(opt_SourcesPath, ""); 60 if ((s = getenv(SOURCES_ENV))) 61 strcpy(opt_SourcesPath, s); 62 63 strcpy(opt_scanned, ""); 64 65 //The user introduced "log2lines.exe" or "log2lines.exe /?" 66 //Let's help the user 67 if ((argc == 1) || 68 ((argc == 2) && (argv[1][0] == '/') && (argv[1][1] == '?'))) 69 { 70 opt_help++; 71 usage(1); 72 return -1; 73 } 74 75 for (i = 1; i < argc; i++) 76 { 77 78 if ((argv[i][0] == '-') && (i+1 < argc)) 79 { 80 //Because these arguments can contain spaces we cant use getopt(), a known bug: 81 switch (argv[i][1]) 82 { 83 case 'd': 84 strcpy(opt_dir, argv[i+1]); 85 break; 86 case 'L': 87 opt_mod = "w"; 88 //fall through 89 case 'l': 90 strcpy(opt_logFile, argv[i+1]); 91 break; 92 case 'P': 93 if (!opt_Pipe) 94 opt_Pipe = malloc(LINESIZE); 95 strcpy(opt_Pipe, argv[i+1]); 96 break; 97 case 'z': 98 strcpy(opt_7z, argv[i+1]); 99 break; 100 } 101 } 102 103 strcat(opt_scanned, argv[i]); 104 strcat(opt_scanned, " "); 105 } 106 107 l2l_dbg(4,"opt_scanned=[%s]\n",opt_scanned); 108 109 return 0; 110 } 111 112 int optionParse(int argc, const char **argv) 113 { 114 int i; 115 int optCount = 0; 116 int opt; 117 118 while (-1 != (opt = getopt(argc, (char **const)argv, optchars))) 119 { 120 switch (opt) 121 { 122 case 'b': 123 opt_buffered++; 124 break; 125 case 'c': 126 opt_console++; 127 break; 128 case 'd': 129 optCount++; 130 //just count, see optionInit() 131 break; 132 case 'f': 133 opt_force++; 134 break; 135 case 'h': 136 opt_help++; 137 usage(1); 138 return -1; 139 break; 140 case 'F': 141 opt_exit++; 142 opt_force++; 143 break; 144 case 'l': 145 optCount++; 146 //just count, see optionInit() 147 break; 148 case 'm': 149 opt_mark++; 150 break; 151 case 'M': 152 opt_Mark++; 153 break; 154 case 'r': 155 opt_raw++; 156 break; 157 case 'P': 158 optCount++; 159 //just count, see optionInit() 160 break; 161 case 's': 162 opt_stats++; 163 break; 164 case 'S': 165 optCount++; 166 i = sscanf(optarg, "%d+%d,%s", &opt_Source, &opt_SrcPlus, opt_SourcesPath); 167 if (i == 1) 168 sscanf(optarg, "%*d,%s", opt_SourcesPath); 169 l2l_dbg(3, "Sources option parse result: %d+%d,\"%s\"\n", opt_Source, opt_SrcPlus, opt_SourcesPath); 170 if (opt_Source) 171 { 172 /* need to retranslate for source info: */ 173 opt_undo++; 174 opt_redo++; 175 } 176 break; 177 case 't': 178 opt_twice++; 179 break; 180 case 'T': 181 opt_twice++; 182 opt_Twice++; 183 break; 184 case 'u': 185 opt_undo++; 186 break; 187 case 'U': 188 opt_undo++; 189 opt_redo++; 190 break; 191 case 'v': 192 opt_verbose++; 193 break; 194 case 'z': 195 optCount++; 196 strcpy(opt_7z, optarg); 197 break; 198 default: 199 usage(0); 200 return -2; 201 break; 202 } 203 optCount++; 204 } 205 if(opt_console) 206 { 207 l2l_dbg(2, "Note: use 's' command in console mode. Statistics option disabled\n"); 208 opt_stats = 0; 209 } 210 if (opt_SourcesPath[0]) 211 { 212 strcat(opt_SourcesPath, PATH_STR); 213 } 214 if (!opt_dir[0]) 215 { 216 strcpy(opt_dir, opt_SourcesPath); 217 strcat(opt_dir, DEF_OPT_DIR); 218 } 219 220 return optCount; 221 } 222 223 /* EOF */ 224