annotate rvinterf/etmsync/dispatch.c @ 571:8d6062f4e7e4

L1: l1_small.c reworked for FreeCalypso
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 10 Aug 2014 19:51:26 +0000
parents 69e8ae2b5ba2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
276
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module implements the command dispatch for fc-fsio
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * and possibly other similar utilities in the future.
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 */
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <ctype.h>
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <stdio.h>
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <string.h>
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <strings.h>
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdlib.h>
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include "cmdtab.h"
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include "exitcodes.h"
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 extern struct cmdtab cmdtab[];
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 parse_and_dispatch_cmd(cmd, is_script)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 char *cmd;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 char *argv[MAX_CMD_ARGS+2];
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 char *cp, **ap;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 struct cmdtab *tp;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 for (cp = cmd; isspace(*cp); cp++)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 ;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 if (!*cp || *cp == '#')
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 return(0);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 if (is_script)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 printf("Script command: %s\n", cp);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 argv[0] = cp;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 while (*cp && !isspace(*cp))
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 cp++;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 if (*cp)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 *cp++ = '\0';
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 for (tp = cmdtab; tp->cmd; tp++)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 if (!strcmp(tp->cmd, argv[0]))
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 break;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 if (!tp->func) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 fprintf(stderr, "error: no such command\n");
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 for (ap = argv + 1; ; ) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 while (isspace(*cp))
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 cp++;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 if (!*cp || *cp == '#')
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 break;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 if (ap - argv - 1 >= tp->maxargs) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 fprintf(stderr, "error: too many arguments\n");
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 }
291
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
50 if (*cp == '"') {
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
51 *ap++ = ++cp;
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
52 while (*cp && *cp != '"')
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
53 cp++;
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
54 if (*cp != '"') {
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
55 fprintf(stderr,
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
56 "error: unterminated quoted string\n");
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
57 return(ERROR_USAGE);
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
58 }
276
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 *cp++ = '\0';
291
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
60 } else {
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
61 *ap++ = cp;
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
62 while (*cp && !isspace(*cp))
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
63 cp++;
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
64 if (*cp)
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
65 *cp++ = '\0';
69e8ae2b5ba2 fc-fsio: fwrite implementation started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 281
diff changeset
66 }
276
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 if (ap - argv - 1 < tp->minargs) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 fprintf(stderr, "error: too few arguments\n");
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 *ap = 0;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 return tp->func(ap - argv, argv);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75
280
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
76 dispatch_ready_argv(argc, argv)
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
77 char **argv;
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
78 {
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
79 struct cmdtab *tp;
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
80
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
81 for (tp = cmdtab; tp->cmd; tp++)
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
82 if (!strcmp(tp->cmd, argv[0]))
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
83 break;
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
84 if (!tp->func) {
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
85 fprintf(stderr, "error: no such command\n");
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
86 return(ERROR_USAGE);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
87 }
281
082d12a1651e fc-fsio: single command invokation works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 280
diff changeset
88 if (argc - 1 > tp->maxargs) {
280
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
89 fprintf(stderr, "error: too many arguments\n");
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
90 return(ERROR_USAGE);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
91 }
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
92 if (argc - 1 < tp->minargs) {
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
93 fprintf(stderr, "error: too few arguments\n");
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
94 return(ERROR_USAGE);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
95 }
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
96 return tp->func(argc, argv);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
97 }
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
98
276
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 cmd_exec(argc, argv)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100 char **argv;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 FILE *f;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 char linebuf[512], *cp;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 int lineno, retval = 0;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 f = fopen(argv[1], "r");
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 if (!f) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 perror(argv[1]);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 cp = index(linebuf, '\n');
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113 if (!cp) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 fprintf(stderr, "%s line %d: missing newline\n",
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115 argv[1], lineno);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 fclose(f);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119 *cp = '\0';
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
120 retval = parse_and_dispatch_cmd(linebuf, 1);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
121 if (retval)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
122 break;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
123 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
124 fclose(f);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
125 return(retval);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
126 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
127
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
128 cmd_exit()
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
129 {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
130 exit(0);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
131 }