Introduce old redir program
[lcapit-junk-code.git] / pet-projects / DLW-1e / das / das.c
blobb2d09769bd30416225cb279a183b179e6c708291
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
5 #include "pass1.h"
6 #include "pass2.h"
7 #include "file.h"
8 #include "error.h"
10 /* usage(): print das's usage information */
11 static void usage(void)
13 printf(
14 "usage: das [ options ] < -o file > < file.asm >\n"
15 " options:\n"
16 "-h this text\n"
17 "-1 performs only first pass\n"
18 "-d debug mode\n"
19 "-D do not remove first pass temp file\n"
20 "-o < file > output (object) file\n"
24 int main(int argc, char *argv[])
26 char *output, *input;
27 FILE *asmfp, *interfp, *binfp;
28 char pass1file[] = ".pass1.XXXXXX";
29 int err, ret, del_p1file, p1_only, debug;
31 err = 0;
32 del_p1file = 1;
33 p1_only = debug = 0;
34 output = input = NULL;
36 while ((ret = getopt(argc, argv, "1dDho:")) != -1) {
37 switch (ret) {
38 case '1':
39 p1_only = 1;
40 break;
41 case 'd':
42 debug = 1;
43 break;
44 case 'D':
45 del_p1file = 0;
46 break;
47 case 'h':
48 usage();
49 exit(0);
50 case 'o':
51 output = optarg;
52 break;
53 default:
54 usage();
55 exit(1);
59 if (optind >= argc)
60 fatal("no input file given");
62 input = argv[optind];
64 if (!output)
65 fatal("no output file given");
67 error_setup(input);
70 * First pass
73 asmfp = file_open(input, "r");
75 if (p1_only)
76 interfp = file_open(output, "w");
77 else
78 interfp = file_open_tmp(pass1file);
80 ret = das_pass1(asmfp, interfp, debug);
81 if (ret)
82 err = 1;
84 fclose(asmfp);
86 if (p1_only) {
87 /* done */
88 fclose(interfp);
89 exit(0);
93 * Second pass
96 binfp = file_open(output, "w");
98 /* XXX: Why do I need freopen() to rewind() ? */
99 interfp = freopen(pass1file, "r", interfp);
100 rewind(interfp);
102 ret = das_pass2(interfp, binfp, debug);
103 if (ret)
104 err = 1;
106 fclose(interfp);
107 fclose(binfp);
109 if (del_p1file)
110 remove(pass1file);
112 /* we remove the binary file on error because
113 * it's probably botched */
114 if (err)
115 remove(output);
117 return err;