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