FreeCalypso > hg > freecalypso-sw
comparison loadtools/initscript.c @ 45:16315ed6401a
init-script logic implemented in fc-xram
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Mon, 17 Jun 2013 08:15:30 +0000 |
parents | |
children | 50b652bc3a4f |
comparison
equal
deleted
inserted
replaced
44:5ca0ad4003a0 | 45:16315ed6401a |
---|---|
1 /* | |
2 * This module has been copied from ltscript.c, ltdispatch.c and ltpassthru.c: | |
3 * here we implement the init-script functionality for fc-xram. | |
4 */ | |
5 | |
6 #include <sys/param.h> | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <stdlib.h> | |
11 | |
12 extern char default_helpers_dir[]; | |
13 | |
14 static | |
15 loadagent_cmd(argc, argv) | |
16 char **argv; | |
17 { | |
18 if (tpinterf_make_cmd(argv) < 0) { | |
19 fprintf(stderr, "error: unable to form target command\n"); | |
20 return(-1); | |
21 } | |
22 if (tpinterf_send_cmd() < 0) | |
23 return(-1); | |
24 return tpinterf_pass_output(1); | |
25 } | |
26 | |
27 static struct cmdtab { | |
28 char *cmd; | |
29 int minargs; | |
30 int maxargs; | |
31 int (*func)(); | |
32 } cmdtab[] = { | |
33 {"w8", 2, 2, loadagent_cmd}, | |
34 {"w16", 2, 2, loadagent_cmd}, | |
35 {"w32", 2, 2, loadagent_cmd}, | |
36 {0, 0, 0, 0} | |
37 }; | |
38 | |
39 static | |
40 dispatch_cmd(cmd) | |
41 char *cmd; | |
42 { | |
43 char *argv[10]; | |
44 char *cp, **ap; | |
45 struct cmdtab *tp; | |
46 | |
47 for (cp = cmd; isspace(*cp); cp++) | |
48 ; | |
49 if (!*cp || *cp == '#') | |
50 return(0); | |
51 printf("init-script command: %s\n", cp); | |
52 argv[0] = cp; | |
53 while (*cp && !isspace(*cp)) | |
54 cp++; | |
55 if (*cp) | |
56 *cp++ = '\0'; | |
57 for (tp = cmdtab; tp->cmd; tp++) | |
58 if (!strcmp(tp->cmd, argv[0])) | |
59 break; | |
60 if (!tp->func) { | |
61 fprintf(stderr, "error: no such command\n"); | |
62 return(-1); | |
63 } | |
64 for (ap = argv + 1; ; ) { | |
65 while (isspace(*cp)) | |
66 cp++; | |
67 if (!*cp || *cp == '#') | |
68 break; | |
69 if (ap - argv - 1 > tp->maxargs) { | |
70 fprintf(stderr, "error: too many arguments\n"); | |
71 return(-1); | |
72 } | |
73 *ap++ = cp; | |
74 while (*cp && !isspace(*cp)) | |
75 cp++; | |
76 if (*cp) | |
77 *cp++ = '\0'; | |
78 } | |
79 if (ap - argv - 1 < tp->minargs) { | |
80 fprintf(stderr, "error: too few arguments\n"); | |
81 return(-1); | |
82 } | |
83 *ap = 0; | |
84 return tp->func(ap - argv, argv); | |
85 } | |
86 | |
87 exec_init_script(script_name) | |
88 char *script_name; | |
89 { | |
90 char pathbuf[MAXPATHLEN], *openfname; | |
91 FILE *f; | |
92 char linebuf[512], *cp; | |
93 int lineno, retval = 0; | |
94 | |
95 if (index(script_name, '/')) | |
96 openfname = script_name; | |
97 else { | |
98 sprintf(pathbuf, "%s/%s", default_helpers_dir, script_name); | |
99 openfname = pathbuf; | |
100 } | |
101 f = fopen(openfname, "r"); | |
102 if (!f) { | |
103 perror(openfname); | |
104 return(-1); | |
105 } | |
106 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { | |
107 cp = index(linebuf, '\n'); | |
108 if (!cp) { | |
109 fprintf(stderr, "%s line %d: missing newline\n", | |
110 openfname, lineno); | |
111 fclose(f); | |
112 return(-1); | |
113 } | |
114 *cp = '\0'; | |
115 retval = dispatch_cmd(linebuf); | |
116 if (retval) | |
117 break; | |
118 } | |
119 fclose(f); | |
120 return(retval); | |
121 } |