Debian Codegraph @

src/icont/tunix.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * tunix.c - user interface for Unix.
3 */
4
5 #include "../h/gsupport.h"
6 #include "../h/version.h"
7 #include "tproto.h"
8 #include "tglobals.h"
9
10 static void execute (char *ofile, char *efile, char *args[]);
11 static void usage (void);
12 static char *libpath (char *prog, char *envname);
13
14 static void txrun(char *(*func)(FILE*, char*), char *source, char *args[]);
15 static char *copyfile(FILE *f, char *srcfile);
16 static char *savefile(FILE *f, char *srcprog);
17 static void cleanup(void);
18
19 static char **rfiles; /* list of files removed by cleanup() */
20
21 /*
22 * for old method of hardwiring iconx path; not normally used.
23 */
24 static char patchpath[MaxPath+18] = "%PatchStringHere->";
25
26 /*
27 * getopt() variables
28 */
29 extern int optind; /* index into parent argv vector */
30 extern int optopt; /* character checked for validity */
31 extern char *optarg; /* argument associated with option */
32
33 /*
34 * main program
35 */
36 int main(int argc, char *argv[]) {
37 int nolink = 0; /* suppress linking? */
38 int errors = 0; /* translator and linker errors */
39 char **tfiles, **tptr; /* list of files to translate */
40 char **lfiles, **lptr; /* list of files to link */
41 char **rptr; /* list of files to remove */
42 char *efile = NULL; /* stderr file */
43 char buf[MaxPath]; /* file name construction buffer */
44 char *ppath; /* ICONPROFILE value */
45 int c, n;
46 char ch;
47 struct fileparts *fp;
48
49 /*
50 * Initialize globals.
51 */
52 initglob(); /* general global initialization */
53 ipath = libpath(argv[0], "IPATH"); /* set library search paths */
54 lpath = libpath(argv[0], "LPATH");
55 if (strlen(patchpath) > 18)
56 iconxloc = patchpath + 18; /* use stated iconx path if patched */
57 else
58 iconxloc = relfile(argv[0], "/../iconx"); /* otherwise infer it */
59 ppath = getenv("ICONPROFILE");
60 if (ppath != NULL && ppath[0] != '\0')
61 profile = 1; /* ICONPROFILE implies -p */
62
63 /*
64 * Process options.
65 * IMPORTANT: When making changes here,
66 * also update usage() function and man page.
67 */
68 while ((c = getopt(argc, argv, "+ce:f:o:pstuv:ENP:VX:")) != EOF)
69 switch (c) {
70 case 'c': /* -c: compile only (no linking) */
71 nolink = 1;
72 break;
73 case 'e': /* -e file: [undoc] redirect stderr */
74 efile = optarg;
75 break;
76 case 'f': /* -f features: enable features */
77 if (strchr(optarg, 's') || strchr(optarg, 'a'))
78 strinv = 1; /* this is the only icont feature */
79 break;
80 case 'o': /* -o file: name output file */
81 ofile = optarg;
82 break;
83 case 'p': /* -p: enable icode profiling */
84 profile = 1;
85 break;
86 case 's': /* -s: suppress informative messages */
87 silent = 1;
88 verbose = 0;
89 break;
90 case 't': /* -t: turn on procedure tracing */
91 trace = -1;
92 break;
93 case 'u': /* -u: warn about undeclared ids */
94 uwarn = 1;
95 break;
96 case 'v': /* -v n: set verbosity level */
97 if (sscanf(optarg, "%d%c", &verbose, &ch) != 1)
98 quitf("bad operand to -v option: %s", optarg);
99 if (verbose == 0)
100 silent = 1;
101 break;
102 case 'E': /* -E: preprocess only */
103 pponly = 1;
104 nolink = 1;
105 break;
106 case 'P': /* -P program: execute from argument */
107 txrun(savefile, optarg, &argv[optind]);
108 break; /*NOTREACHED*/
109 case 'N': /* -N: don't embed iconx path */
110 iconxloc = "";
111 break;
112 case 'V': /* -V: print version information */
113 fprintf(stderr, "%s (%s %d/%d, %s)\n",
114 Version, Config, IntBits, WordBits, __DATE__);
115 if (optind == argc)
116 exit(0);
117 break;
118 case 'X': /* -X srcfile: execute single srcfile */
119 txrun(copyfile, optarg, &argv[optind]);
120 break; /*NOTREACHED*/
121 default:
122 case 'x': /* -x illegal until after file list */
123 usage();
124 }
125
126 /*
127 * If argv[0] ends in "icon" (instead of "icont" or anything else),
128 * process as "icon [options] sourcefile [arguments]" scripting shortcut.
129 */
130 n = strlen(argv[0]);
131 if (n >= 4 && strcmp(argv[0]+n-4, "icon") == 0) {
132 if (optind < argc)
133 txrun(copyfile, argv[optind], &argv[optind+1]);
134 else
135 usage();
136 }
137
138 /*
139 * Allocate space for lists of file names.
140 */
141 n = argc - optind + 1;
142 tptr = tfiles = alloc(n * sizeof(char *));
143 lptr = lfiles = alloc(n * sizeof(char *));
144 rptr = rfiles = alloc(2 * n * sizeof(char *));
145
146 /*
147 * Scan file name arguments.
148 */
149 while (optind < argc) {
150 if (strcmp(argv[optind], "-x") == 0) /* stop at -x */
151 break;
152 else if (strcmp(argv[optind], "-") == 0) {
153 *tptr++ = "-"; /* "-" means standard input */
154 *lptr++ = *rptr++ = "stdin.u1";
155 *rptr++ = "stdin.u2";
156 }
157 else {
158 fp = fparse(argv[optind]); /* parse file name */
159 if (*fp->ext == '\0' || smatch(fp->ext, SourceSuffix)) {
160 makename(buf, SourceDir, argv[optind], SourceSuffix);
161 *tptr++ = salloc(buf); /* translate the .icn file */
162 makename(buf, TargetDir, argv[optind], U1Suffix);
163 *lptr++ = *rptr++ = salloc(buf); /* link & remove .u1 */
164 makename(buf, TargetDir, argv[optind], U2Suffix);
165 *rptr++ = salloc(buf); /* also remove .u2 */
166 }
167 else if (smatch(fp->ext, U1Suffix) || smatch(fp->ext, U2Suffix)
168 || smatch(fp->ext, USuffix)) {
169 makename(buf, TargetDir, argv[optind], U1Suffix);
170 *lptr++ = salloc(buf);
171 }
172 else
173 quitf("bad argument %s", argv[optind]);
174 }
175 optind++;
176 }
177
178 *tptr = *lptr = *rptr = NULL; /* terminate filename lists */
179 if (lptr == lfiles)
180 usage(); /* error -- no files named */
181
182 /*
183 * Translate .icn files to make .u1 and .u2 files.
184 */
185 if (tptr > tfiles) {
186 if (!pponly)
187 report("Translating");
188 errors = trans(tfiles, TargetDir);
189 if (errors > 0) /* exit if errors seen */
190 exit(EXIT_FAILURE);
191 }
192
193 /*
194 * Link .u1 and .u2 files to make an executable.
195 */
196 if (nolink) /* exit if no linking wanted */
197 exit(EXIT_SUCCESS);
198
199 if (ofile == NULL) { /* if no -o file, synthesize a name */
200 ofile = salloc(makename(buf, TargetDir, lfiles[0], IcodeSuffix));
201 }
202 else { /* add extension in necessary */
203 fp = fparse(ofile);
204 if (*fp->ext == '\0' && *IcodeSuffix != '\0') /* if no ext given */
205 ofile = salloc(makename(buf, NULL, ofile, IcodeSuffix));
206 }
207
208 report("Linking");
209 errors = ilink(lfiles, ofile); /* link .u files to make icode file */
210
211 /*
212 * Finish by removing intermediate files.
213 * Execute the linked program if so requested and if there were no errors.
214 */
215 cleanup(); /* delete intermediate files */
216 if (errors > 0) { /* exit if linker errors seen */
217 remove(ofile);
218 exit(EXIT_FAILURE);
219 }
220 if (optind < argc) {
221 report("Executing");
222 execute (ofile, efile, argv + optind + 1);
223 }
224 exit(EXIT_SUCCESS);
225 return 0;
226 }
227
228 /*
229 * execute - execute iconx to run the icon program
230 */
231 static void execute(char *ofile, char *efile, char *args[]) {
232 int n;
233 char **argv, **p;
234 char buf[MaxPath+10];
235
236 /*
237 * Build argument vector.
238 */
239 for (n = 0; args[n] != NULL; n++) /* count arguments */
240 ;
241 p = argv = alloc((n + 5) * sizeof(char *));
242
243 *p++ = ofile; /* pass icode file name */
244 while ((*p++ = *args++) != 0) /* copy args into argument vector */
245 ;
246 *p = NULL;
247
248 /*
249 * Redirect stderr if requested.
250 */
251 if (efile != NULL) {
252 close(fileno(stderr));
253 if (strcmp(efile, "-") == 0) {
254 if (dup(fileno(stdout)) < 0)
255 quit("could not merge standard output with standard error\n");
256 }
257 else if (freopen(efile, "w", stderr) == NULL)
258 quitf("could not redirect stderr to %s\n", efile);
259 }
260
261 /*
262 * Export $ICONX to specify the path to iconx.
263 */
264 sprintf(buf, "ICONX=%s", iconxloc);
265 putenv(buf);
266
267 /*
268 * Execute the generated program.
269 */
270 execv(ofile, argv);
271 quitf("could not execute %s", ofile);
272 }
273
274 void report(char *s) {
275 char *c = (strchr(s, '\n') ? "" : ":\n") ;
276 if (!silent)
277 fprintf(stderr, "%s%s", s, c);
278 }
279
280 /*
281 * Print a usage message and abort the run.
282 */
283 static void usage(void) {
284 fprintf(stderr, "usage: icon sourcefile [args]\n");
285 fprintf(stderr, " icon -P 'program' [args]\n");
286 fprintf(stderr, " icont %s\n",
287 "[-cpstuENV] [-f s] [-o ofile] [-v i] file ... [-x args]");
288 exit(EXIT_FAILURE);
289 }
290
291 /*
292 * Return path after appending lib directory.
293 */
294 static char *libpath(char *prog, char *envname) {
295 char buf[1000], *s;
296
297 s = getenv(envname);
298 if (s != NULL)
299 strcpy(buf, s);
300 else
301 strcpy(buf, ".");
302 strcat(buf, ":");
303 strcat(buf, relfile(prog, "/../../lib"));
304 return salloc(buf);
305 }
306
307 /*
308 * Translate, link, and execute a source file.
309 * Does not return under any circumstances.
310 */
311 static void txrun(char *(*func)(FILE*, char*), char *source, char *args[]) {
312 int omask;
313 char c1, c2;
314 char *flist[2], *progname;
315 char srcfile[MaxPath], u1[MaxPath], u2[MaxPath];
316 char icode[MaxPath], buf[MaxPath + 20];
317 static char abet[] = "abcdefghijklmnopqrstuvwxyz";
318 FILE *f;
319
320 silent = 1; /* don't issue commentary while translating */
321 uwarn = 1; /* do diagnose undeclared identifiers */
322 omask = umask(0077); /* remember umask; keep /tmp files private */
323
324 /*
325 * Create a temporary file named /tmp/innnnnxx.icn.
326 */
327 srand(time(NULL));
328 c1 = abet[rand() % (sizeof(abet) - 1)];
329 c2 = abet[rand() % (sizeof(abet) - 1)];
330 sprintf(srcfile, "/tmp/i%d%c%c.icn", getpid(), c1, c2);
331 f = fopen(srcfile, "w");
332 if (f == NULL)
333 quitf("cannot open for writing: %s", srcfile);
334
335 /*
336 * Derive other names and arrange for cleanup on exit.
337 */
338 rfiles = alloc(5 * sizeof(char *));
339 rfiles[0] = srcfile;
340 makename(rfiles[1] = u1, NULL, srcfile, U1Suffix);
341 makename(rfiles[2] = u2, NULL, srcfile, U2Suffix);
342 makename(rfiles[3] = icode, NULL, srcfile, IcodeSuffix);
343 rfiles[4] = NULL;
344 atexit(cleanup);
345
346 /*
347 * If source is from a file, prepend its directory to LPATH.
348 */
349 if (func == copyfile) {
350 sprintf(buf, "%s/..", source);
351 canonize(buf);
352 strcat(buf, ":");
353 strcat(buf, lpath);
354 free(lpath);
355 lpath = salloc(buf);
356 }
357
358 /*
359 * Copy the source file, then translate to produce .u1 and .u2 files.
360 */
361 progname = func(f, source);
362 fclose(f);
363 flist[0] = srcfile;
364 flist[1] = NULL;
365 if (trans(flist, SourceDir) > 0)
366 exit(EXIT_FAILURE);
367
368 /*
369 * Link to make an icode file.
370 */
371 flist[0] = u1;
372 if (ilink(flist, icode) > 0)
373 exit(EXIT_FAILURE);
374
375 /*
376 * Execute the icode file.
377 */
378 rfiles[3] = NULL; /* don't delete icode yet */
379 cleanup(); /* but delete the others */
380 sprintf(buf, "ICODE_TEMP=%s:%s", icode, progname);
381 putenv(buf); /* tell iconx to delete icode */
382 umask(omask); /* reset original umask */
383 execute(icode, NULL, args); /* execute the program */
384 quitf("could not execute %s", icode);
385 }
386
387 /*
388 * Dump a string to a file, prefixed by $line 0 "[inline]".
389 */
390 static char *savefile(FILE *f, char *srcprog) {
391 static char *progname = "[inline]";
392 fprintf(f, "$line 0 \"%s\"\n", progname);
393 fwrite(srcprog, 1, strlen(srcprog), f);
394 return progname;
395 }
396
397 /*
398 * Copy a source file for later translation, adding $line 0 "filename".
399 */
400 static char *copyfile(FILE *f, char *srcfile) {
401 int c;
402 FILE *e;
403
404 if (strcmp(srcfile, "-") == 0) {
405 e = stdin;
406 srcfile = "stdin";
407 }
408 else {
409 if ((e = fopen(srcfile, "r")) == NULL)
410 quitf("cannot open: %s", srcfile);
411 }
412 fprintf(f, "$line 0 \"%s\"\n", srcfile);
413 while ((c = getc(e)) != EOF)
414 putc(c, f);
415 fclose(e);
416 return srcfile;
417 }
418
419 /*
420 * Deletes the files listed in rfiles[].
421 */
422 static void cleanup(void) {
423 char **p;
424
425 for (p = rfiles; *p; p++)
426 remove(*p);
427 }