annotate rvinterf/etmsync/dispatch.c @ 285:bb28ba9e82c5

fc-fsio: file read primitive and hd command implemented
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 28 Feb 2014 06:16:02 +0000
parents 082d12a1651e
children 69e8ae2b5ba2
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 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 *ap++ = cp;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 while (*cp && !isspace(*cp))
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 cp++;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 if (*cp)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 *cp++ = '\0';
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 if (ap - argv - 1 < tp->minargs) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 fprintf(stderr, "error: too few arguments\n");
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 *ap = 0;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 return tp->func(ap - argv, argv);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63
280
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
64 dispatch_ready_argv(argc, argv)
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
65 char **argv;
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
66 {
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
67 struct cmdtab *tp;
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
68
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
69 for (tp = cmdtab; tp->cmd; tp++)
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
70 if (!strcmp(tp->cmd, argv[0]))
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
71 break;
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
72 if (!tp->func) {
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
73 fprintf(stderr, "error: no such command\n");
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
74 return(ERROR_USAGE);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
75 }
281
082d12a1651e fc-fsio: single command invokation works
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 280
diff changeset
76 if (argc - 1 > tp->maxargs) {
280
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
77 fprintf(stderr, "error: too many arguments\n");
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
78 return(ERROR_USAGE);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
79 }
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
80 if (argc - 1 < tp->minargs) {
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
81 fprintf(stderr, "error: too few arguments\n");
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
82 return(ERROR_USAGE);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
83 }
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
84 return tp->func(argc, argv);
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
85 }
f304f9bcde3b fc-fsio: single command invokation implemented
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents: 276
diff changeset
86
276
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 cmd_exec(argc, argv)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 char **argv;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 FILE *f;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 char linebuf[512], *cp;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 int lineno, retval = 0;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 f = fopen(argv[1], "r");
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 if (!f) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 perror(argv[1]);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100 cp = index(linebuf, '\n');
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 if (!cp) {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 fprintf(stderr, "%s line %d: missing newline\n",
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 argv[1], lineno);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 fclose(f);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 return(ERROR_USAGE);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 *cp = '\0';
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 retval = parse_and_dispatch_cmd(linebuf, 1);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 if (retval)
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110 break;
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 fclose(f);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113 return(retval);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 }
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 cmd_exit()
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 {
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 exit(0);
909f00c15f27 more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119 }