Changeset 439 for EcnlProtoTool/trunk/prototool/src/cmdmisc.c
- Timestamp:
- Jul 9, 2020, 8:51:43 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/prototool/src/cmdmisc.c
r321 r439 4 4 */ 5 5 6 #include <ctype.h> 6 7 #include <stdlib.h> 7 8 #include <string.h> 8 9 9 #include "mruby/compile.h" 10 #include "mrdb.h" 11 12 typedef struct help_msg 13 { 14 const char *cmd1; 15 const char *cmd2; 16 const char *short_msg; 17 const char *long_msg; 10 #include "apilist.h" 11 #include <mruby/compile.h> 12 13 typedef struct help_msg { 14 const char *cmd1; 15 const char *cmd2; 16 const char *short_msg; 17 const char *long_msg; 18 18 } help_msg; 19 19 20 20 static help_msg help_msg_list[] = { 21 21 { 22 "b[reak]", NULL, "Set breakpoint", 23 "Usage: break [file:]line\n" 24 " break [class:]method\n" 25 "\n" 26 "Set breakpoint at specified line or method.\n" 27 "If \'[file:]line\' is specified, break at start of code for that line (in a file).\n" 28 "If \'[class:]method\' is specified, break at start of code for that method (of the class).\n" 29 }, 30 { 31 "c[ontinue]", NULL, "Continue program being debugged", 32 "Usage: continue [N]\n" 33 "\n" 34 "Continue program stopped by a breakpoint.\n" 35 "If N, which is non negative value, is passed,\n" 36 "proceed program until the N-th breakpoint is coming.\n" 37 "If N is not passed, N is assumed 1.\n" 38 }, 39 { 40 "d[elete]", NULL, "Delete some breakpoints", 41 "Usage: delete [bpno1 [bpno2 [... [bpnoN]]]]\n" 42 "\n" 43 "Delete some breakpoints.\n" 44 "Arguments are breakpoint numbers with spaces in between.\n" 45 "To delete all breakpoints, give no argument.\n" 46 }, 47 { 48 "dis[able]", NULL, "Disable some breakpoints", 49 "Usage: disable [bpno1 [bpno2 [... [bpnoN]]]]\n" 50 "\n" 51 "Disable some breakpoints.\n" 52 "Arguments are breakpoint numbers with spaces in between.\n" 53 "To disable all breakpoints, give no argument.\n" 54 }, 55 { 56 "en[able]", NULL, "Enable some breakpoints", 57 "Usage: enable [bpno1 [bpno2 [... [bpnoN]]]]\n" 58 "\n" 59 "Enable some breakpoints.\n" 60 "Arguments are breakpoint numbers with spaces in between.\n" 61 "To enable all breakpoints, give no argument.\n" 62 }, 63 { 64 "ev[al]", NULL, "Evaluate expression", 65 "Usage: eval expr\n" 66 "\n" 67 "It evaluates and prints the value of the mruby expression.\n" 68 "This is equivalent to the \'print\' command.\n" 69 }, 70 { 71 "h[elp]", NULL, "Print this help", 72 "Usage: help [command]\n" 73 "\n" 74 "With no arguments, help displays a short list of commands.\n" 75 "With a command name as help argument, help displays how to use that command.\n" 76 }, 77 { 78 "i[nfo]", "b[reakpoints]", "Status of breakpoints", 79 "Usage: info breakpoints [bpno1 [bpno2 [... [bpnoN]]]]\n" 80 "\n" 81 "Status of specified breakpoints (all user-settable breakpoints if no argument).\n" 82 "Arguments are breakpoint numbers with spaces in between.\n" 83 }, 84 { 85 "l[ist]", NULL, "List specified line", 86 "Usage: list\n" 87 " list first[,last]\n" 88 " list filename:first[,last]\n" 89 "\n" 90 "Print lines from a source file.\n" 91 "\n" 92 "With first and last, list prints lines from first to last.\n" 93 "When last is empty, it stands for ten lines away from first.\n" 94 "With filename, list prints lines in the specified source file.\n" 95 }, 96 { 97 "p[rint]", NULL, "Print value of expression", 98 "Usage: print expr\n" 99 "\n" 100 "It evaluates and prints the value of the mruby expression.\n" 101 "This is equivalent to the \'eval\' command.\n" 102 }, 103 { 104 "q[uit]", NULL, "Exit mrdb", 105 "Usage: quit\n" 106 "\n" 107 "Exit mrdb.\n" 108 }, 109 { 110 "r[un]", NULL, "Start debugged program", 111 "Usage: run\n" 112 "\n" 113 "Start debugged program.\n" 114 }, 115 { 116 "s[tep]", NULL, "Step program until it reaches a different source line", 117 "Usage: step\n" 118 "\n" 119 "Step program until it reaches a different source line.\n" 22 "b[reak]", NULL, "Set breakpoint", 23 "Usage: break [file:]line\n" 24 " break [class:]method\n" 25 "\n" 26 "Set breakpoint at specified line or method.\n" 27 "If \'[file:]line\' is specified, break at start of code for that line (in a file).\n" 28 "If \'[class:]method\' is specified, break at start of code for that method (of the class).\n" 29 }, 30 { 31 "c[ontinue]", NULL, "Continue program being debugged", 32 "Usage: continue [N]\n" 33 "\n" 34 "Continue program stopped by a breakpoint.\n" 35 "If N, which is non negative value, is passed,\n" 36 "proceed program until the N-th breakpoint is coming.\n" 37 "If N is not passed, N is assumed 1.\n" 38 }, 39 { 40 "d[elete]", NULL, "Delete some breakpoints", 41 "Usage: delete [bpno1 [bpno2 [... [bpnoN]]]]\n" 42 "\n" 43 "Delete some breakpoints.\n" 44 "Arguments are breakpoint numbers with spaces in between.\n" 45 "To delete all breakpoints, give no argument.\n" 46 }, 47 { 48 "dis[able]", NULL, "Disable some breakpoints", 49 "Usage: disable [bpno1 [bpno2 [... [bpnoN]]]]\n" 50 "\n" 51 "Disable some breakpoints.\n" 52 "Arguments are breakpoint numbers with spaces in between.\n" 53 "To disable all breakpoints, give no argument.\n" 54 }, 55 { 56 "en[able]", NULL, "Enable some breakpoints", 57 "Usage: enable [bpno1 [bpno2 [... [bpnoN]]]]\n" 58 "\n" 59 "Enable some breakpoints.\n" 60 "Arguments are breakpoint numbers with spaces in between.\n" 61 "To enable all breakpoints, give no argument.\n" 62 }, 63 { 64 "ev[al]", NULL, "Evaluate expression", 65 "Usage: eval expr\n" 66 "\n" 67 "It evaluates and prints the value of the mruby expression.\n" 68 "This is equivalent to the \'print\' command.\n" 69 }, 70 { 71 "h[elp]", NULL, "Print this help", 72 "Usage: help [command]\n" 73 "\n" 74 "With no arguments, help displays a short list of commands.\n" 75 "With a command name as help argument, help displays how to use that command.\n" 76 }, 77 { 78 "i[nfo]", "b[reakpoints]", "Status of breakpoints", 79 "Usage: info breakpoints [bpno1 [bpno2 [... [bpnoN]]]]\n" 80 "\n" 81 "Status of specified breakpoints (all user-settable breakpoints if no argument).\n" 82 "Arguments are breakpoint numbers with spaces in between.\n" 83 }, 84 { 85 "i[nfo]", "l[ocals]", "Print name of local variables", 86 "Usage: info locals\n" 87 "\n" 88 "Print name of local variables.\n" 89 }, 90 { 91 "l[ist]", NULL, "List specified line", 92 "Usage: list\n" 93 " list first[,last]\n" 94 " list filename:first[,last]\n" 95 "\n" 96 "Print lines from a source file.\n" 97 "\n" 98 "With first and last, list prints lines from first to last.\n" 99 "When last is empty, it stands for ten lines away from first.\n" 100 "With filename, list prints lines in the specified source file.\n" 101 }, 102 { 103 "p[rint]", NULL, "Print value of expression", 104 "Usage: print expr\n" 105 "\n" 106 "It evaluates and prints the value of the mruby expression.\n" 107 "This is equivalent to the \'eval\' command.\n" 108 }, 109 { 110 "q[uit]", NULL, "Exit mrdb", 111 "Usage: quit\n" 112 "\n" 113 "Exit mrdb.\n" 114 }, 115 { 116 "r[un]", NULL, "Start debugged program", 117 "Usage: run\n" 118 "\n" 119 "Start debugged program.\n" 120 }, 121 { 122 "s[tep]", NULL, "Step program until it reaches a different source line", 123 "Usage: step\n" 124 "\n" 125 "Step program until it reaches a different source line.\n" 120 126 }, 121 127 { NULL, NULL, NULL, NULL } 122 128 }; 123 129 124 typedef struct listcmd_parser_state 125 { 126 mrb_bool parse_error; 127 mrb_bool has_line_min; 128 mrb_bool has_line_max; 129 char *filename; 130 uint16_t line_min; 131 uint16_t line_max; 130 typedef struct listcmd_parser_state { 131 mrb_bool parse_error; 132 mrb_bool has_line_min; 133 mrb_bool has_line_max; 134 char *filename; 135 uint16_t line_min; 136 uint16_t line_max; 132 137 } listcmd_parser_state; 133 138 … … 135 140 listcmd_parser_state_new(mrb_state *mrb) 136 141 { 137 listcmd_parser_state *st = (listcmd_parser_state*)mrb_malloc(mrb, sizeof(listcmd_parser_state));138 139 142 listcmd_parser_state *st = (listcmd_parser_state*)mrb_malloc(mrb, sizeof(listcmd_parser_state)); 143 memset(st, 0, sizeof(listcmd_parser_state)); 144 return st; 140 145 } 141 146 … … 143 148 listcmd_parser_state_free(mrb_state *mrb, listcmd_parser_state *st) 144 149 { 145 146 147 148 149 150 150 if (st != NULL) { 151 if (st->filename != NULL) { 152 mrb_free(mrb, st->filename); 153 } 154 mrb_free(mrb, st); 155 } 151 156 } 152 157 … … 154 159 parse_uint(char **sp, uint16_t *n) 155 160 { 156 157 158 159 160 161 162 163 for (p = *sp; *p != '\0' && ISDIGIT(*p); p++);164 165 166 167 168 169 170 161 char *p; 162 int i; 163 164 if (*sp == NULL || **sp == '\0') { 165 return FALSE; 166 } 167 168 for (p = *sp; *p != '\0' && ISDIGIT(*p); p++) ; 169 170 if (p != *sp && (i = atoi(*sp)) >= 0) { 171 *n = (uint16_t)i; 172 *sp = p; 173 return TRUE; 174 } 175 return FALSE; 171 176 } 172 177 … … 174 179 skip_char(char **sp, char c) 175 180 { 176 177 178 179 180 181 if (*sp != NULL && **sp == c) { 182 ++*sp; 183 return TRUE; 184 } 185 return FALSE; 181 186 } 182 187 … … 184 189 parse_lineno(mrb_state *mrb, char **sp, listcmd_parser_state *st) 185 190 { 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 191 if (*sp == NULL || **sp == '\0') { 192 return FALSE; 193 } 194 195 st->has_line_min = FALSE; 196 st->has_line_max = FALSE; 197 198 if (parse_uint(sp, &st->line_min)) { 199 st->has_line_min = TRUE; 200 } 201 else { 202 return FALSE; 203 } 204 205 if (skip_char(sp, ',')) { 206 if (parse_uint(sp, &st->line_max)) { 207 st->has_line_max = TRUE; 208 } 209 else { 210 st->parse_error = TRUE; 211 return FALSE; 212 } 213 } 214 return TRUE; 210 215 } 211 216 … … 213 218 parse_filename(mrb_state *mrb, char **sp, listcmd_parser_state *st) 214 219 { 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 st->filename =mrb_malloc(mrb, len + 1);232 strlcpy(st->filename, *sp, len + 1);233 234 235 236 237 238 239 220 char *p; 221 int len; 222 223 if (st->filename != NULL) { 224 mrb_free(mrb, st->filename); 225 st->filename = NULL; 226 } 227 228 if ((p = strchr(*sp, ':')) != NULL) { 229 len = p - *sp; 230 } 231 else { 232 len = strlen(*sp); 233 } 234 235 if (len > 0) { 236 st->filename = (char*)mrb_malloc(mrb, len + 1); 237 strncpy(st->filename, *sp, len); 238 st->filename[len] = '\0'; 239 *sp += len; 240 return TRUE; 241 } 242 else { 243 return FALSE; 244 } 240 245 } 241 246 … … 243 248 replace_ext(mrb_state *mrb, const char *filename, const char *ext) 244 249 { 245 size_t len, len2; 246 const char *p; 247 char *s; 248 249 if (filename == NULL) { 250 return NULL; 251 } 252 253 if ((p = strrchr(filename, '.')) != NULL && strchr(p, '/') == NULL) { 254 len = p - filename; 255 } 256 else { 257 len = strlen(filename); 258 } 259 260 len2 = len + strlen(ext) + 1; 261 s = mrb_malloc(mrb, len2); 262 memset(s, '\0', len2); 263 strlcpy(s, filename, len); 264 strlcat(s, ext, len2); 265 266 return s; 250 size_t len; 251 const char *p; 252 char *s; 253 254 if (filename == NULL) { 255 return NULL; 256 } 257 258 if ((p = strrchr(filename, '.')) != NULL && strchr(p, '/') == NULL) { 259 len = p - filename; 260 } 261 else { 262 len = strlen(filename); 263 } 264 265 s = (char*)mrb_malloc(mrb, len + strlen(ext) + 1); 266 memset(s, '\0', len + strlen(ext) + 1); 267 strncpy(s, filename, len); 268 strcat(s, ext); 269 270 return s; 267 271 } 268 272 … … 270 274 parse_listcmd_args(mrb_state *mrb, mrdb_state *mrdb, listcmd_parser_state *st) 271 275 { 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 276 char *p; 277 278 switch (mrdb->wcnt) { 279 case 2: 280 p = mrdb->words[1]; 281 282 /* mrdb->words[1] ::= <lineno> | <filename> ':' <lineno> | <filename> */ 283 if (!parse_lineno(mrb, &p, st)) { 284 if (parse_filename(mrb, &p, st)) { 285 if (skip_char(&p, ':')) { 286 if (!parse_lineno(mrb, &p, st)) { 287 st->parse_error = TRUE; 288 } 289 } 290 } 291 else { 292 st->parse_error = TRUE; 293 } 294 } 295 if (*p != '\0') { 296 st->parse_error = TRUE; 297 } 298 break; 299 case 1: 300 case 0: 301 /* do nothing */ 302 break; 303 default: 304 st->parse_error = TRUE; 305 printf("too many arguments\n"); 306 break; 307 } 308 309 if (!st->parse_error) { 310 if (!st->has_line_min) { 311 st->line_min = (!st->filename && mrdb->dbg->prvline > 0) ? mrdb->dbg->prvline : 1; 312 } 313 314 if (!st->has_line_max) { 315 st->line_max = st->line_min + 9; 316 } 317 318 if (st->filename == NULL) { 319 if (mrdb->dbg->prvfile && strcmp(mrdb->dbg->prvfile, "-")) { 320 st->filename = replace_ext(mrb, mrdb->dbg->prvfile, ".rb"); 321 } 322 } 323 } 324 325 if (st->parse_error || st->filename == NULL) { 326 return FALSE; 327 } 328 329 return TRUE; 326 330 } 327 331 … … 329 333 check_cmd_pattern(const char *pattern, const char *cmd) 330 334 { 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 for (; p < rbracket && *q != '\0'; p++, q++) {353 354 355 356 357 335 const char *lbracket, *rbracket, *p, *q; 336 337 if (pattern == NULL && cmd == NULL) { 338 return TRUE; 339 } 340 if (pattern == NULL || cmd == NULL) { 341 return FALSE; 342 } 343 if ((lbracket = strchr(pattern, '[')) == NULL) { 344 return !strcmp(pattern, cmd); 345 } 346 if ((rbracket = strchr(pattern, ']')) == NULL) { 347 return FALSE; 348 } 349 if (strncmp(pattern, cmd, lbracket - pattern)) { 350 return FALSE; 351 } 352 353 p = lbracket + 1; 354 q = (char *)cmd + (lbracket - pattern); 355 356 for ( ; p < rbracket && *q != '\0'; p++, q++) { 357 if (*p != *q) { 358 break; 359 } 360 } 361 return *q == '\0'; 358 362 } 359 363 … … 361 365 get_help_msg(char *cmd1, char *cmd2) 362 366 { 363 364 365 366 367 368 369 370 371 372 373 367 help_msg *p; 368 369 if (cmd1 == NULL) { 370 return NULL; 371 } 372 for (p = help_msg_list; p->cmd1 != NULL; p++) { 373 if (check_cmd_pattern(p->cmd1, cmd1) && check_cmd_pattern(p->cmd2, cmd2)) { 374 return p; 375 } 376 } 377 return NULL; 374 378 } 375 379 … … 377 381 show_short_help(void) 378 382 { 379 380 381 382 383 384 385 386 387 388 389 390 391 383 help_msg *p; 384 385 printf("Commands\n"); 386 387 for (p = help_msg_list; p->cmd1 != NULL; p++) { 388 if (p->cmd2 == NULL) { 389 printf(" %s -- %s\n", p->cmd1, p->short_msg); 390 } 391 else { 392 printf(" %s %s -- %s\n", p->cmd1, p->cmd2, p->short_msg); 393 } 394 } 395 return TRUE; 392 396 } 393 397 … … 395 399 show_long_help(char *cmd1, char *cmd2) 396 400 { 397 help_msg *help; 398 399 if ((help = get_help_msg(cmd1, cmd2)) == NULL) { 400 return FALSE; 401 } 402 printf("%s", help->long_msg); 403 return TRUE; 401 help_msg *help; 402 403 if ((help = get_help_msg(cmd1, cmd2)) == NULL) { 404 return FALSE; 405 } 406 printf("%s", help->long_msg); 407 return TRUE; 408 } 409 410 dbgcmd_state 411 dbgcmd_list(mrb_state *mrb, mrdb_state *mrdb) 412 { 413 char *filename; 414 listcmd_parser_state *st = listcmd_parser_state_new(mrb); 415 416 if (parse_listcmd_args(mrb, mrdb, st)) { 417 if ((filename = mrb_debug_get_source(mrb, mrdb, mrdb->srcpath, st->filename)) == NULL) { 418 filename = st->filename; 419 } 420 mrb_debug_list(mrb, mrdb->dbg, filename, st->line_min, st->line_max); 421 422 if (filename != NULL && filename != st->filename) { 423 mrb_free(mrb, filename); 424 } 425 listcmd_parser_state_free(mrb, st); 426 } 427 428 return DBGST_PROMPT; 404 429 } 405 430 … … 407 432 dbgcmd_help(mrb_state *mrb, mrdb_state *mrdb) 408 433 { 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 434 mrb_bool is_valid; 435 int i; 436 437 switch (mrdb->wcnt) { 438 case 0: 439 case 1: 440 is_valid = show_short_help(); 441 break; 442 case 2: 443 is_valid = show_long_help(mrdb->words[1], NULL); 444 break; 445 case 3: 446 is_valid = show_long_help(mrdb->words[1], mrdb->words[2]); 447 break; 448 default: 449 is_valid = FALSE; 450 break; 451 } 452 453 if (!is_valid) { 454 printf("Invalid command \""); 455 for (i = 1; i < mrdb->wcnt; i++) { 456 printf("%s%s", i == 1 ? "" : " ", mrdb->words[i]); 457 } 458 printf("\". Try \"help\".\n"); 459 } 460 461 return DBGST_PROMPT; 437 462 } 438 463 … … 440 465 dbgcmd_quit(mrb_state *mrb, mrdb_state *mrdb) 441 466 { 442 443 444 445 446 447 448 449 450 451 452 453 454 InterlockedExchange(&mrdb->dbg->xm, DBG_QUIT);455 456 457 458 while (buf != '\n' && (buf = getchar()) != EOF);459 460 461 InterlockedExchange(&mrdb->dbg->xm, DBG_QUIT);462 463 464 465 466 467 468 469 470 471 472 473 InterlockedExchange(&mrdb->dbg->xm, DBG_QUIT);474 475 476 477 478 479 exc = mrb_define_class(mrb, "DebuggerExit", mrb_class_get(mrb, "Exception"));480 481 482 483 } 467 switch (mrdb->dbg->xm) { 468 case DBG_RUN: 469 case DBG_STEP: 470 case DBG_NEXT: 471 while (1) { 472 char c; 473 int buf; 474 475 printf("The program is running. Exit anyway? (y or n) "); 476 fflush(stdout); 477 478 if ((buf = getchar()) == EOF) { 479 mrdb->dbg->xm = DBG_QUIT; 480 break; 481 } 482 c = buf; 483 while (buf != '\n' && (buf = getchar()) != EOF) ; 484 485 if (c == 'y' || c == 'Y') { 486 mrdb->dbg->xm = DBG_QUIT; 487 break; 488 } 489 else if (c == 'n' || c == 'N') { 490 break; 491 } 492 else { 493 printf("Please answer y or n.\n"); 494 } 495 } 496 break; 497 default: 498 mrdb->dbg->xm = DBG_QUIT; 499 break; 500 } 501 502 if (mrdb->dbg->xm == DBG_QUIT) { 503 struct RClass *exc; 504 exc = mrb_define_class(mrb, "DebuggerExit", mrb->eException_class); 505 mrb_raise(mrb, exc, "Exit mrdb."); 506 } 507 return DBGST_PROMPT; 508 }
Note:
See TracChangeset
for help on using the changeset viewer.