comparison 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
comparison
equal deleted inserted replaced
275:cedf09b6b5ac 276:909f00c15f27
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 *ap++ = cp;
51 while (*cp && !isspace(*cp))
52 cp++;
53 if (*cp)
54 *cp++ = '\0';
55 }
56 if (ap - argv - 1 < tp->minargs) {
57 fprintf(stderr, "error: too few arguments\n");
58 return(ERROR_USAGE);
59 }
60 *ap = 0;
61 return tp->func(ap - argv, argv);
62 }
63
64 cmd_exec(argc, argv)
65 char **argv;
66 {
67 FILE *f;
68 char linebuf[512], *cp;
69 int lineno, retval = 0;
70
71 f = fopen(argv[1], "r");
72 if (!f) {
73 perror(argv[1]);
74 return(ERROR_USAGE);
75 }
76 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
77 cp = index(linebuf, '\n');
78 if (!cp) {
79 fprintf(stderr, "%s line %d: missing newline\n",
80 argv[1], lineno);
81 fclose(f);
82 return(ERROR_USAGE);
83 }
84 *cp = '\0';
85 retval = parse_and_dispatch_cmd(linebuf, 1);
86 if (retval)
87 break;
88 }
89 fclose(f);
90 return(retval);
91 }
92
93 cmd_exit()
94 {
95 exit(0);
96 }