72877dcb82226f52da4fa37022c6e829a9b1fa23
[reactos.git] / reactos / tools / log2lines / cmd.c
1 /*
2 * ReactOS log2lines
3 * Written by Jan Roeloffzen
4 *
5 * - Cli for escape commands
6 */
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10
11 #include "util.h"
12 #include "cmd.h"
13 #include "options.h"
14 #include "log2lines.h"
15 #include "help.h"
16
17 /* When you edit the cmd line and/or use the history instead of just typing,
18 * a bunch of editing BS and space characters
19 * is inserted, so the string looks right on the console but still
20 * starts with the original string:
21 */
22 static char
23 *backSpaceEdit(char *s)
24 {
25 char c;
26 char *edit = s;
27 char *text = s;
28
29 while (( c = *edit++ ))
30 {
31 switch (c)
32 {
33 case KDBG_BS_CHAR:
34 if (text > s)
35 text --;
36 break;
37 default:
38 *text++ = c;
39 }
40 }
41 *text = '\0';
42
43 return s;
44 }
45
46 static int
47 handle_switch(FILE *outFile, int *sw, char *arg, char *desc)
48 {
49 int changed =0;
50 int x = 0;
51
52 if (arg && (strcmp(arg,"") != 0))
53 {
54 x = atoi(arg);
55 if (x != *sw)
56 {
57 *sw = x;
58 changed = 1;
59 }
60 }
61 if (desc)
62 {
63 esclog(outFile, "%s is %d (%s)\n", desc, *sw, changed ? "changed":"unchanged");
64 if (!arg)
65 esclog(outFile, "(readonly)\n");
66 }
67
68 return changed;
69 }
70
71 static int
72 handle_switch_str(FILE *outFile, char *sw, char *arg, char *desc)
73 {
74 int changed =0;
75
76 if (arg)
77 {
78 if (strcmp(arg,"") != 0)
79 {
80 if (strcmp(arg,KDBG_ESC_OFF) == 0)
81 {
82 if (*sw)
83 changed = 1;
84 *sw = '\0';
85 }
86 else if (strcmp(arg, sw) != 0)
87 {
88 strcpy(sw, arg);
89 changed = 1;
90 }
91 }
92 }
93 if (desc)
94 {
95 esclog(outFile, "%s is \"%s\" (%s)\n", desc, sw, changed ? "changed":"unchanged");
96 if (!arg)
97 esclog(outFile, "(readonly)\n");
98 }
99
100 return changed;
101 }
102
103 static int
104 handle_switch_pstr(FILE *outFile, char **psw, char *arg, char *desc)
105 {
106 int changed =0;
107
108 if (arg)
109 {
110 if (strcmp(arg,"") != 0)
111 {
112 if (strcmp(arg,KDBG_ESC_OFF) == 0)
113 {
114 if (*psw)
115 changed = 1;
116 free(*psw);
117 *psw = NULL;
118 }
119 else
120 {
121 if (!*psw)
122 {
123 *psw = malloc(LINESIZE);
124 **psw = '\0';
125 }
126
127 if (strcmp(arg, *psw) != 0)
128 {
129 strcpy(*psw, arg);
130 changed = 1;
131 }
132 }
133 }
134 }
135 if (desc)
136 {
137 esclog(outFile, "%s is \"%s\" (%s)\n", desc, *psw, changed ? "changed":"unchanged");
138 if (!arg)
139 esclog(outFile, "(readonly)\n");
140 }
141
142 return changed;
143 }
144
145 char
146 handle_escape_cmd(FILE *outFile, char *Line, char *path, char *LineOut)
147 {
148 char cmd;
149 char sep = '\n';
150 char *arg;
151 char *l = Line;
152 int res = 1;
153 int cnt = 0;
154 int changed = 0;
155
156 l = backSpaceEdit(l);
157 if (l[1] != KDBG_ESC_CHAR)
158 return l[1]; //for reprocessing as not escaped
159
160 log(outFile, "\n");
161
162 l += 2; //skip space+escape character
163 if ( (cnt=sscanf(l,"%c%c",&cmd,&sep)) < 1)
164 {
165 esclog(outFile, "Command expected\n");
166 res = 0;
167 }
168
169 if (res && cnt==2 && sep != ' ')
170 {
171 esclog(outFile, "' ' expected\n");
172 res = 0;
173 }
174 l++; //skip cmd
175 while ( *l == ' ')l++; //skip more spaces
176 arg = l;
177 opt_cli = 1;
178 switch (cmd)
179 {
180 case 'h':
181 usage(1);
182 break;
183 case 'b':
184 if (handle_switch(outFile, &opt_buffered, arg, "-b Logfile buffering"))
185 set_LogFile(logFile); //re-open same logfile
186 break;
187 case 'c':
188 handle_switch(outFile, &opt_console, NULL, "-c Console option");
189 break;
190 case 'd':
191 handle_switch_str(outFile, opt_dir, NULL, "-d Directory option");
192 break;
193 case 'l':
194 if (handle_switch_str(outFile, opt_logFile, arg, "-l logfile"))
195 set_LogFile(logFile); //open new logfile
196 break;
197 case 'm':
198 handle_switch(outFile, &opt_Mark, arg, "-m mark (*)");
199 break;
200 case 'M':
201 handle_switch(outFile, &opt_Mark, arg, "-M Mark (?)");
202 break;
203 case 'P':
204 handle_switch_str(outFile, opt_Pipe, NULL, "-P Pipeline option");
205 break;
206 case 'q':
207 opt_quit = 1;
208 esclog(outFile, "Bye!\n");
209 break;
210 case 'r':
211 handle_switch(outFile, &opt_raw, arg, "-r Raw");
212 break;
213 case 'R':
214 changed = handle_switch_pstr(outFile, &opt_Revision, arg, NULL);
215 if (opt_Revision)
216 {
217 if (strstr(opt_Revision, "check") == opt_Revision)
218 {
219 esclog(outFile, "-R is \"%s\" (%s)\n", opt_Revision, changed ? "changed":"unchanged");
220 }
221 else if (strstr(opt_Revision, "regscan") == opt_Revision)
222 {
223 char *s = strchr(opt_Revision, ',');
224
225 revinfo.range = DEF_RANGE;
226 if (s)
227 {
228 *s++ = '\0';
229 revinfo.range = atoi(s);
230 }
231 regscan(outFile);
232 }
233 else if (strstr(opt_Revision, "regclear") == opt_Revision)
234 {
235 list_clear(&sources);
236 summ.regfound = 0;
237 esclog(outFile, "cleared regression scan results\n");
238 }
239 }
240 break;
241 case 's':
242 if (strcmp(arg,"clear") == 0)
243 {
244 memset(&summ, 0, sizeof(SUMM));
245 esclog(outFile, "Statistics cleared\n");
246 }
247 else
248 stat_print(outFile, &summ);
249 break;
250 case 'S':
251 cnt = sscanf(arg, "%d+%d", &opt_Source, &opt_SrcPlus);
252 if (opt_Source)
253 {
254 handle_switch(outFile, &opt_undo, "1", "-u Undo");
255 handle_switch(outFile, &opt_redo, "1", "-U Undo and reprocess");
256 }
257 esclog(outFile, "-S Sources option is %d+%d,\"%s\"\n", opt_Source, opt_SrcPlus, opt_SourcesPath);
258 esclog(outFile, "(Setting source tree not implemented)\n");
259 break;
260 case 't':
261 handle_switch(outFile, &opt_twice, arg, "-t Translate twice");
262 break;
263 case 'T':
264 handle_switch(outFile, &opt_twice, arg, NULL);
265 handle_switch(outFile, &opt_Twice, arg, "-T Translate for (address-1)");
266 break;
267 case 'u':
268 handle_switch(outFile, &opt_undo, arg, "-u undo");
269 break;
270 case 'U':
271 handle_switch(outFile, &opt_undo, arg, NULL);
272 handle_switch(outFile, &opt_redo, arg, "-U Undo and reprocess");
273 break;
274 case 'v':
275 handle_switch(outFile, &opt_verbose, arg, "-v Verbosity");
276 break;
277 case 'z':
278 handle_switch_str(outFile, opt_7z, NULL, "-z 7z path");
279 break;
280 default:
281 if (strchr(optchars, cmd))
282 esclog(outFile, "Command not implemented in cli: %c %s\n",cmd, arg)
283 else
284 esclog(outFile, "Unknown command: %c %s\n",cmd, arg);
285 }
286 opt_cli = 0;
287
288 memset(Line, '\0', LINESIZE); // flushed
289
290 return KDBG_ESC_CHAR; //handled escaped command
291 }