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