1 /* Copyright (C) 1999 by Marc Olzheim */
2
3 /*
4 * # with description do commands with \[\]
5 * # p.e. with \# \?food\<400 do mov f 1,1 50 \[\]
6 * # to move 50 food from 1,1 to all sectors with less than 400 food.
7 * alias with "runfeed /usr/local/lib/eif/${0}"
8 * alias lwith "runfeed /usr/local/lib/eif/${0}"
9 * alias nwith "runfeed /usr/local/lib/eif/${0}"
10 * alias pwith "runfeed /usr/local/lib/eif/${0}"
11 * alias swith "runfeed /usr/local/lib/eif/${0}"
12 */
13
14 /* Yeah, I know.... It sux. But it works, kind of... */
15
16 /* with <sectors> do <command with []> */
17
18 #include "../config.h"
19
20 #include <ctype.h>
21 #include <err.h>
22 #include <stdio.h>
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif /* HAVE_STDLIB_H */
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif /* HAVE_STRING_H */
29 #ifdef HAVE_STRINGS_H
30 #include <strings.h>
31 #endif /* HAVE_STRINGS_H */
32 #include <unistd.h>
33
34 char *progname = NULL;
35
usage(void)36 static void usage(void)
37 {
38 fprintf(stderr, "Usage: %s <sectors> do <command with `[]' in it>\n",
39 progname);
40 fprintf(stderr, " %s -f <dump_file>\n", progname);
41 exit(1);
42 }
43
44 int
main(int argc,char * argv[])45 main(int argc, char *argv[])
46 {
47 char **argw, *walk0, *walk1;
48 char buffer[4096], buffer2[4096], filename[BUFSIZ];
49 char type = 0;
50 FILE *fpr;
51 unsigned int i, has_params = 0;
52
53 buffer[0] = ' ';
54 progname = argv[0];
55
56 if (((i = strlen(progname)) > 4) && (progname[i - 5] != '/'))
57 type = progname[i - 5];
58
59 (void) snprintf(filename, sizeof(filename), "tmp/%i.dump", getpid());
60
61 if ((argc < 5) && (argc != 3))
62 usage();
63
64 if (argc == 3)
65 {
66 if (strcmp(argv[1], "-f"))
67 usage();
68 if (!(fpr = fopen(argv[2], "r")))
69 err(1, "Could not open dumpfile `%s' for reading",
70 filename);
71
72 if (!fgets(buffer + 1, sizeof(buffer) - 1, fpr))
73 err(1, "File empty ?!");
74
75 walk0 = buffer + strlen(buffer);
76 while (walk0 >= buffer)
77 {
78 if (isspace(*walk0))
79 {
80 if (strcmp(walk0 + 1, "[]"))
81 sprintf(buffer2 + strlen(buffer2),
82 "%s ", walk0 + 1);
83 else
84 {
85 if (!type)
86 sprintf(buffer2 +
87 strlen(buffer2),
88 "%%s,%%s ");
89 else
90 sprintf(buffer2 +
91 strlen(buffer2),
92 "%%s ");
93 has_params++;
94 }
95 *walk0-- = 0;
96 }
97 walk0--;
98 }
99 buffer2[strlen(buffer2) + 1] = 0;
100 buffer2[strlen(buffer2)] = '\n';
101
102 while (fgets(buffer + 1, sizeof(buffer) - 1, fpr))
103 {
104 if (!isdigit(buffer[1]) && (buffer[1] != '-'))
105 continue;
106
107 walk0 = walk1 = buffer + 1;
108 while (*walk1 && !isspace(*walk1))
109 walk1++;
110
111
112 while (isspace(*walk1))
113 *walk1++ = 0;
114
115 if (!isdigit(*walk1) && (*walk1 != '-'))
116 continue;
117
118 walk0 = walk1;
119
120 while (*walk1 && !isspace(*walk1))
121 walk1++;
122 if (*walk1)
123 *walk1++ = 0;
124
125 switch(has_params)
126 {
127 case 0:
128 printf(buffer2);
129 break;
130 case 1:
131 if (type)
132 printf(buffer2, buffer + 1);
133 else
134 printf(buffer2, buffer + 1, walk0);
135 break;
136 case 2:
137 if (type)
138 printf(buffer2, buffer + 1, buffer + 1);
139 else
140 printf(buffer2, buffer + 1, walk0,
141 buffer + 1, walk0);
142 break;
143 case 3:
144 if (type)
145 printf(buffer2, buffer + 1, buffer + 1,
146 buffer + 1);
147 else
148 printf(buffer2, buffer + 1, walk0,
149 buffer + 1, walk0,
150 buffer + 1, walk0);
151 break;
152 case 4:
153 if (type)
154 printf(buffer2, buffer + 1, buffer + 1,
155 buffer + 1, buffer + 1);
156 else
157 printf(buffer2, buffer + 1, walk0,
158 buffer + 1, walk0,
159 buffer + 1, walk0,
160 buffer + 1, walk0);
161 break;
162 default:
163 printf("echo Too many variable occurences\n");
164 }
165 }
166
167 fclose(fpr);
168 } else
169 {
170 printf("echo ");
171 /* Print command first */
172 /* Skip 'do' */
173 argw = argv + argc - 1;
174
175 i = 0;
176
177 while ((strcmp(*argw, "do")) && (argw >= argv))
178 {
179 printf("%s%s", (i++) ? " " : "", *argw);
180 argw--;
181 }
182
183 if (argw == argv)
184 usage();
185
186 printf(" >! %s\n", filename);
187
188 /* Skip 'with' */
189 argw = argv + 1;
190
191 if (type)
192 (void) putchar(type);
193 if (**(argw + 1) == '?')
194 printf("dump %s %s %s >> %s\n", *argw, *(argw + 1),
195 (type) ? "x" : "civ", filename);
196 else /* *(argw + 1) == "do" */
197 printf("dump %s %s >> %s\n",
198 *argw, (type) ? "x" : "civ", filename);
199
200 if (type)
201 (void) putchar(type);
202 printf("with -f %s\n", filename);
203 }
204
205 return(0);
206 }
207
208 /* vim:ts=8:ai:syntax=c
209 */
210