3 * Written by Jan Roeloffzen
5 * - Cli for escape commands
15 #include "log2lines.h"
18 /* When you edit the cmd line and/or use the history instead of just typing,
19 * a bunch of editing BS and space characters
20 * is inserted, so the string looks right on the console but still
21 * contains the original string, plus other garbage:
24 *backSpaceEdit(char *s
)
30 while (( c
= *edit
++ ))
48 handle_switch(FILE *outFile
, int *sw
, char *arg
, char *desc
)
53 if (arg
&& (strcmp(arg
,"") != 0))
64 esclog(outFile
, "%s is %d (%s)\n", desc
, *sw
, changed
? "changed":"unchanged");
66 esclog(outFile
, "(readonly)\n");
73 handle_switch_str(FILE *outFile
, char *sw
, char *arg
, char *desc
)
79 if (strcmp(arg
,"") != 0)
81 if (strcmp(arg
,KDBG_ESC_OFF
) == 0)
87 else if (strcmp(arg
, sw
) != 0)
96 esclog(outFile
, "%s is \"%s\" (%s)\n", desc
, sw
, changed
? "changed":"unchanged");
98 esclog(outFile
, "(readonly)\n");
105 handle_switch_pstr(FILE *outFile
, char **psw
, char *arg
, char *desc
)
111 if (strcmp(arg
,"") != 0)
113 if (strcmp(arg
,KDBG_ESC_OFF
) == 0)
124 *psw
= malloc(LINESIZE
);
128 if (strcmp(arg
, *psw
) != 0)
138 esclog(outFile
, "%s is \"%s\" (%s)\n", desc
, *psw
, changed
? "changed":"unchanged");
140 esclog(outFile
, "(readonly)\n");
147 handle_address_cmd(FILE *outFile
, char *arg
)
150 char Image
[NAMESIZE
];
155 if(( s
= strchr(arg
, ':') ))
158 if ( (cnt
= sscanf(arg
,"%20s %x", Image
, &Offset
)) == 2)
160 if (( plm
= entry_lookup(&cache
, Image
) ))
162 if (plm
->RelBase
!= INVALID_BASE
)
163 esclog(outFile
, "Address: 0x%lx\n", plm
->RelBase
+ Offset
)
165 esclog(outFile
, "Relocated base missing for '%s' ('mod' will update)\n", Image
);
168 esclog(outFile
, "Image '%s' not found\n", Image
);
171 esclog(outFile
, "usage: `a <Image>:<offset>\n");
174 esclog(outFile
, "':' expected\n");
180 handle_escape_cmd(FILE *outFile
, char *Line
, char *path
, char *LineOut
)
190 l
= backSpaceEdit(l
);
191 if (l
[1] != KDBG_ESC_CHAR
)
192 return l
[1]; //for reprocessing as not escaped
196 l
+= 2; //skip space+escape character
197 if ( (cnt
=sscanf(l
,"%c%c",&cmd
,&sep
)) < 1)
199 esclog(outFile
, "Command expected\n");
203 if (res
&& cnt
==2 && sep
!= ' ')
205 esclog(outFile
, "' ' expected\n");
209 while ( *l
== ' ')l
++; //skip more spaces
215 handle_address_cmd(outFile
, arg
);
221 if (handle_switch(outFile
, &opt_buffered
, arg
, "-b Logfile buffering"))
222 set_LogFile(&logFile
); //re-open same logfile
225 handle_switch(outFile
, &opt_console
, NULL
, "-c Console option");
228 handle_switch_str(outFile
, opt_dir
, NULL
, "-d Directory option");
231 if (handle_switch_str(outFile
, opt_logFile
, arg
, "-l logfile") || (strcmp(opt_mod
,"a")!=0))
234 set_LogFile(&logFile
); //open new logfile
238 if (handle_switch_str(outFile
, opt_logFile
, arg
, "-L logfile") || (strcmp(opt_mod
,"w")!=0))
241 set_LogFile(&logFile
); //open new logfile
245 handle_switch(outFile
, &opt_Mark
, arg
, "-m mark (*)");
248 handle_switch(outFile
, &opt_Mark
, arg
, "-M Mark (?)");
251 handle_switch_str(outFile
, opt_Pipe
, NULL
, "-P Pipeline option");
255 esclog(outFile
, "Bye!\n");
258 handle_switch(outFile
, &opt_raw
, arg
, "-r Raw");
261 changed
= handle_switch_pstr(outFile
, &opt_Revision
, arg
, NULL
);
262 opt_Revision_check
= 0;
265 opt_Revision_check
= 1;
266 if (strstr(opt_Revision
, "check") == opt_Revision
)
268 esclog(outFile
, "-R is \"%s\" (%s)\n", opt_Revision
, changed
? "changed":"unchanged");
270 else if (strstr(opt_Revision
, "regscan") == opt_Revision
)
272 char *s
= strchr(opt_Revision
, ',');
274 revinfo
.range
= DEF_RANGE
;
278 revinfo
.range
= atoi(s
);
282 else if (strstr(opt_Revision
, "regclear") == opt_Revision
)
284 list_clear(&sources
);
286 esclog(outFile
, "cleared regression scan results\n");
291 if (strcmp(arg
,"clear") == 0)
293 memset(&summ
, 0, sizeof(SUMM
));
294 esclog(outFile
, "Statistics cleared\n");
297 stat_print(outFile
, &summ
);
300 cnt
= sscanf(arg
, "%d+%d", &opt_Source
, &opt_SrcPlus
);
303 handle_switch(outFile
, &opt_undo
, "1", "-u Undo");
304 handle_switch(outFile
, &opt_redo
, "1", "-U Undo and reprocess");
305 opt_Revision_check
= 1;
307 esclog(outFile
, "-S Sources option is %d+%d,\"%s\"\n", opt_Source
, opt_SrcPlus
, opt_SourcesPath
);
308 esclog(outFile
, "(Setting source tree not implemented)\n");
311 handle_switch(outFile
, &opt_twice
, arg
, "-t Translate twice");
314 handle_switch(outFile
, &opt_twice
, arg
, NULL
);
315 handle_switch(outFile
, &opt_Twice
, arg
, "-T Translate for (address-1)");
318 handle_switch(outFile
, &opt_undo
, arg
, "-u undo");
321 handle_switch(outFile
, &opt_undo
, arg
, NULL
);
322 handle_switch(outFile
, &opt_redo
, arg
, "-U Undo and reprocess");
325 handle_switch(outFile
, &opt_verbose
, arg
, "-v Verbosity");
328 handle_switch_str(outFile
, opt_7z
, NULL
, "-z 7z path");
331 if (strchr(optchars
, cmd
))
332 esclog(outFile
, "Command not implemented in cli: %c %s\n",cmd
, arg
)
334 esclog(outFile
, "Unknown command: %c %s\n",cmd
, arg
);
338 memset(Line
, '\0', LINESIZE
); // flushed
340 return KDBG_ESC_CHAR
; //handled escaped command