FreeCalypso > hg > freecalypso-tools
comparison rvinterf/etmsync/dispatch.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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e7502631a0f9 |
---|---|
1 /* | |
2 * This module implements the command dispatch for fc-fsio | |
3 * and possibly other similar utilities in the future. | |
4 */ | |
5 | |
6 #include <ctype.h> | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <stdlib.h> | |
11 #include "cmdtab.h" | |
12 #include "exitcodes.h" | |
13 | |
14 extern struct cmdtab cmdtab[]; | |
15 | |
16 parse_and_dispatch_cmd(cmd, is_script) | |
17 char *cmd; | |
18 { | |
19 char *argv[MAX_CMD_ARGS+2]; | |
20 char *cp, **ap; | |
21 struct cmdtab *tp; | |
22 | |
23 for (cp = cmd; isspace(*cp); cp++) | |
24 ; | |
25 if (!*cp || *cp == '#') | |
26 return(0); | |
27 if (is_script) | |
28 printf("Script command: %s\n", cp); | |
29 argv[0] = cp; | |
30 while (*cp && !isspace(*cp)) | |
31 cp++; | |
32 if (*cp) | |
33 *cp++ = '\0'; | |
34 for (tp = cmdtab; tp->cmd; tp++) | |
35 if (!strcmp(tp->cmd, argv[0])) | |
36 break; | |
37 if (!tp->func) { | |
38 fprintf(stderr, "error: no such command\n"); | |
39 return(ERROR_USAGE); | |
40 } | |
41 for (ap = argv + 1; ; ) { | |
42 while (isspace(*cp)) | |
43 cp++; | |
44 if (!*cp || *cp == '#') | |
45 break; | |
46 if (ap - argv - 1 >= tp->maxargs) { | |
47 fprintf(stderr, "error: too many arguments\n"); | |
48 return(ERROR_USAGE); | |
49 } | |
50 if (*cp == '"') { | |
51 *ap++ = ++cp; | |
52 while (*cp && *cp != '"') | |
53 cp++; | |
54 if (*cp != '"') { | |
55 fprintf(stderr, | |
56 "error: unterminated quoted string\n"); | |
57 return(ERROR_USAGE); | |
58 } | |
59 *cp++ = '\0'; | |
60 } else { | |
61 *ap++ = cp; | |
62 while (*cp && !isspace(*cp)) | |
63 cp++; | |
64 if (*cp) | |
65 *cp++ = '\0'; | |
66 } | |
67 } | |
68 if (ap - argv - 1 < tp->minargs) { | |
69 fprintf(stderr, "error: too few arguments\n"); | |
70 return(ERROR_USAGE); | |
71 } | |
72 *ap = 0; | |
73 return tp->func(ap - argv, argv); | |
74 } | |
75 | |
76 dispatch_ready_argv(argc, argv) | |
77 char **argv; | |
78 { | |
79 struct cmdtab *tp; | |
80 | |
81 for (tp = cmdtab; tp->cmd; tp++) | |
82 if (!strcmp(tp->cmd, argv[0])) | |
83 break; | |
84 if (!tp->func) { | |
85 fprintf(stderr, "error: no such command\n"); | |
86 return(ERROR_USAGE); | |
87 } | |
88 if (argc - 1 > tp->maxargs) { | |
89 fprintf(stderr, "error: too many arguments\n"); | |
90 return(ERROR_USAGE); | |
91 } | |
92 if (argc - 1 < tp->minargs) { | |
93 fprintf(stderr, "error: too few arguments\n"); | |
94 return(ERROR_USAGE); | |
95 } | |
96 return tp->func(argc, argv); | |
97 } | |
98 | |
99 cmd_exec(argc, argv) | |
100 char **argv; | |
101 { | |
102 FILE *f; | |
103 char linebuf[512], *cp; | |
104 int lineno, retval = 0; | |
105 | |
106 f = fopen(argv[1], "r"); | |
107 if (!f) { | |
108 perror(argv[1]); | |
109 return(ERROR_USAGE); | |
110 } | |
111 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { | |
112 cp = index(linebuf, '\n'); | |
113 if (!cp) { | |
114 fprintf(stderr, "%s line %d: missing newline\n", | |
115 argv[1], lineno); | |
116 fclose(f); | |
117 return(ERROR_USAGE); | |
118 } | |
119 *cp = '\0'; | |
120 retval = parse_and_dispatch_cmd(linebuf, 1); | |
121 if (retval) | |
122 break; | |
123 } | |
124 fclose(f); | |
125 return(retval); | |
126 } | |
127 | |
128 cmd_exit() | |
129 { | |
130 exit(0); | |
131 } |