comparison loadtools/ltdispatch.c @ 22:e658a84b37df

loadtool coming along
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 04 May 2013 04:36:29 +0000
parents
children ae6294b8a015
comparison
equal deleted inserted replaced
21:67a39d8914a8 22:e658a84b37df
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 char loadtool_command[];
12
13 extern int loadtool_cmd_passthru();
14
15 static int
16 exitcmd()
17 {
18 exit(0);
19 }
20
21 static struct cmdtab {
22 char *cmd;
23 int minargs;
24 int maxargs;
25 int (*func)();
26 } cmdtab[] = {
27 {"dump", 2, 2, loadtool_cmd_passthru},
28 {"exit", 0, 0, &exitcmd},
29 {"quit", 0, 0, &exitcmd},
30 {"r8", 1, 1, loadtool_cmd_passthru},
31 {"r16", 1, 1, loadtool_cmd_passthru},
32 {"r32", 1, 1, loadtool_cmd_passthru},
33 {"w8", 2, 2, loadtool_cmd_passthru},
34 {"w16", 2, 2, loadtool_cmd_passthru},
35 {"w32", 2, 2, loadtool_cmd_passthru},
36 {0, 0, 0, 0}
37 };
38
39 loadtool_dispatch_cmd()
40 {
41 char *argv[10];
42 char *cp, **ap;
43 struct cmdtab *tp;
44
45 for (cp = loadtool_command; isspace(*cp); cp++)
46 ;
47 if (!*cp || *cp == '#')
48 return(0);
49 argv[0] = cp;
50 while (*cp && !isspace(*cp))
51 cp++;
52 if (*cp)
53 *cp++ = '\0';
54 for (tp = cmdtab; tp->cmd; tp++)
55 if (!strcmp(tp->cmd, argv[0]))
56 break;
57 if (!tp->func) {
58 fprintf(stderr, "error: no such command\n");
59 return(-1);
60 }
61 for (ap = argv + 1; ; ) {
62 while (isspace(*cp))
63 cp++;
64 if (!*cp || *cp == '#')
65 break;
66 if (ap - argv - 1 > tp->maxargs) {
67 fprintf(stderr, "error: too many arguments\n");
68 return(-1);
69 }
70 *ap++ = cp;
71 while (*cp && !isspace(*cp))
72 cp++;
73 if (*cp)
74 *cp++ = '\0';
75 }
76 if (ap - argv - 1 < tp->minargs) {
77 fprintf(stderr, "error: too few arguments\n");
78 return(-1);
79 }
80 *ap = 0;
81 return tp->func(ap - argv, argv);
82 }