FreeCalypso > hg > freecalypso-sw
annotate rvinterf/etmsync/dispatch.c @ 276:909f00c15f27
more fc-fsio foundation
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Sun, 23 Feb 2014 21:31:30 +0000 |
parents | |
children | f304f9bcde3b |
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 |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 cmd_exec(argc, argv) |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 char **argv; |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 { |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 FILE *f; |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 char linebuf[512], *cp; |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 int lineno, retval = 0; |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 f = fopen(argv[1], "r"); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 if (!f) { |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 perror(argv[1]); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 return(ERROR_USAGE); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 } |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 cp = index(linebuf, '\n'); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 if (!cp) { |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 fprintf(stderr, "%s line %d: missing newline\n", |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 argv[1], lineno); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 fclose(f); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 return(ERROR_USAGE); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 } |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 *cp = '\0'; |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 retval = parse_and_dispatch_cmd(linebuf, 1); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 if (retval) |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 break; |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 } |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 fclose(f); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 return(retval); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 } |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 cmd_exit() |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 { |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 exit(0); |
909f00c15f27
more fc-fsio foundation
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 } |