annotate loadtools/initscript.c @ 88:ccde45a06737

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