annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module implements the command dispatch for fc-loadtool
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <ctype.h>
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <stdio.h>
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <string.h>
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <strings.h>
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <stdlib.h>
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 extern char loadtool_command[];
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 extern int loadtool_cmd_passthru();
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 static int
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 exitcmd()
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 exit(0);
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 }
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 static struct cmdtab {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 char *cmd;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 int minargs;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 int maxargs;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 int (*func)();
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 } cmdtab[] = {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 {"dump", 2, 2, loadtool_cmd_passthru},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 {"exit", 0, 0, &exitcmd},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 {"quit", 0, 0, &exitcmd},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 {"r8", 1, 1, loadtool_cmd_passthru},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 {"r16", 1, 1, loadtool_cmd_passthru},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 {"r32", 1, 1, loadtool_cmd_passthru},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 {"w8", 2, 2, loadtool_cmd_passthru},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 {"w16", 2, 2, loadtool_cmd_passthru},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 {"w32", 2, 2, loadtool_cmd_passthru},
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 {0, 0, 0, 0}
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 };
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 loadtool_dispatch_cmd()
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 char *argv[10];
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 char *cp, **ap;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 struct cmdtab *tp;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 for (cp = loadtool_command; isspace(*cp); cp++)
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 ;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 if (!*cp || *cp == '#')
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 return(0);
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 argv[0] = cp;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 while (*cp && !isspace(*cp))
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 cp++;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 if (*cp)
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 *cp++ = '\0';
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 for (tp = cmdtab; tp->cmd; tp++)
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 if (!strcmp(tp->cmd, argv[0]))
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 break;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 if (!tp->func) {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 fprintf(stderr, "error: no such command\n");
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 return(-1);
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 }
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 for (ap = argv + 1; ; ) {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 while (isspace(*cp))
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 cp++;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 if (!*cp || *cp == '#')
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 break;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 if (ap - argv - 1 > tp->maxargs) {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 fprintf(stderr, "error: too many arguments\n");
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 return(-1);
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 }
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 *ap++ = cp;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 while (*cp && !isspace(*cp))
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 cp++;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 if (*cp)
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 *cp++ = '\0';
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 }
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 if (ap - argv - 1 < tp->minargs) {
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 fprintf(stderr, "error: too few arguments\n");
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 return(-1);
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 }
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 *ap = 0;
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 return tp->func(ap - argv, argv);
e658a84b37df loadtool coming along
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 }