comparison loadtools/ltdispatch.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 0d199c6a6ea4
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * This module implements the command dispatch for fc-loadtool
3 */
4
5 #include <ctype.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stdlib.h>
10
11 extern int cmd_baud();
12 extern int cmd_crc32();
13 extern int cmd_dieid();
14 extern int cmd_dump2bin();
15 extern int cmd_dump2srec();
16 extern int cmd_exec();
17 extern int cmd_exit();
18 extern int cmd_flash();
19 extern int cmd_help();
20 extern int loadtool_cmd_passthru();
21
22 static struct cmdtab {
23 char *cmd;
24 int minargs;
25 int maxargs;
26 int (*func)();
27 } cmdtab[] = {
28 {"abbr", 2, 2, loadtool_cmd_passthru},
29 {"abbw", 3, 3, loadtool_cmd_passthru},
30 {"baud", 0, 1, cmd_baud},
31 {"crc32", 2, 2, cmd_crc32},
32 {"dieid", 0, 1, cmd_dieid},
33 {"dump", 2, 2, loadtool_cmd_passthru},
34 {"dump2bin", 3, 3, cmd_dump2bin},
35 {"dump2srec", 3, 3, cmd_dump2srec},
36 {"exec", 1, 1, cmd_exec},
37 {"exit", 0, 1, cmd_exit},
38 {"flash", 1, 5, cmd_flash},
39 {"flash2", 1, 5, cmd_flash},
40 {"help", 0, 2, cmd_help},
41 {"quit", 0, 1, cmd_exit},
42 {"r8", 1, 1, loadtool_cmd_passthru},
43 {"r16", 1, 1, loadtool_cmd_passthru},
44 {"r32", 1, 1, loadtool_cmd_passthru},
45 {"w8", 2, 2, loadtool_cmd_passthru},
46 {"w16", 2, 2, loadtool_cmd_passthru},
47 {"w32", 2, 2, loadtool_cmd_passthru},
48 {0, 0, 0, 0}
49 };
50
51 loadtool_dispatch_cmd(cmd, is_script)
52 char *cmd;
53 {
54 char *argv[10];
55 char *cp, **ap;
56 struct cmdtab *tp;
57
58 for (cp = cmd; isspace(*cp); cp++)
59 ;
60 if (!*cp || *cp == '#')
61 return(0);
62 if (is_script)
63 printf("Script command: %s\n", cp);
64 argv[0] = cp;
65 while (*cp && !isspace(*cp))
66 cp++;
67 if (*cp)
68 *cp++ = '\0';
69 for (tp = cmdtab; tp->cmd; tp++)
70 if (!strcmp(tp->cmd, argv[0]))
71 break;
72 if (!tp->func) {
73 fprintf(stderr, "error: no such command\n");
74 return(-1);
75 }
76 for (ap = argv + 1; ; ) {
77 while (isspace(*cp))
78 cp++;
79 if (!*cp || *cp == '#')
80 break;
81 if (ap - argv - 1 >= tp->maxargs) {
82 fprintf(stderr, "error: too many arguments\n");
83 return(-1);
84 }
85 *ap++ = cp;
86 while (*cp && !isspace(*cp))
87 cp++;
88 if (*cp)
89 *cp++ = '\0';
90 }
91 if (ap - argv - 1 < tp->minargs) {
92 fprintf(stderr, "error: too few arguments\n");
93 return(-1);
94 }
95 *ap = 0;
96 return tp->func(ap - argv, argv);
97 }