Minimal changes to get m6502 to compile (won't work)
[Apout.git] / main.c
1 /* Startup for apout. Parse arguments, load the binary, and run it.
2  *
3  * $Revision: 1.23 $
4  * $Date: Mon 31 Oct 2016 03:46:15 AM CET $
5  */
6 #include <assert.h>
7 #include "defines.h"
8
9 /* The following array holds the FILE pointers that correspond to open file
10  * descriptors. Only fds which are not ttys have FILE * pointers
11  */
12 FILE *stream[NFILE];
13 char *streammode[NFILE];        /* Mode for each file - used for dup */
14
15 /* The following two buffers are used as */
16 /* part of the translation from virtal */
17 /* absolute filenames to native ones. We */
18 /* only have 2 buffers, so if you call */
19 /* xlate_filename() 3 times, the 1st return */
20 /* value will be destroyed. */
21 static char realfilename[2][2 * MAXPATHLEN];
22 static char *rfn[2];
23 static int whichrfn = 0;
24 char *apout_root = NULL;        /* Root dir for simulated a.out */
25
26 #ifdef DEBUG
27 /* Debugging flags */
28 int inst_debug = 0,             /* Print a line before each instruction */
29     trap_debug = 0,             /* Print details of each trap */
30     jsr_debug = 0,              /* Print out each jsr */
31     fp_debug = 0;               /* Print out each floating-point instruction */
32 FILE *dbg_file = NULL;          /* Debugging output file */
33 char *progname = NULL;          /* The program's name - used in debugging */
34 #endif
35
36 void usage()
37 {
38     fprintf(stderr, "Usage: apout");
39 #ifdef DEBUG
40     fprintf(stderr, " [-inst] [-trap] [-jsr] [-fp]");
41 #endif
42     fprintf(stderr, " pdp11_binary\n");
43     exit(1);
44 }
45
46 int main(int argc, char **argv)
47 {
48     int i;
49
50     /* Ensure, before we start, that certain types are right */
51     assert(sizeof(int8_t) == 1);
52     assert(sizeof(u_int8_t) == 1);
53     assert(sizeof(int16_t) == 2);
54     assert(sizeof(u_int16_t) == 2);
55     assert(sizeof(int32_t) == 4);
56     assert(sizeof(u_int32_t) == 4);
57
58     if (argc < 2)
59         usage();
60     if (!strcmp(argv[1], "-help"))
61         usage();
62     if (!strcmp(argv[1], "--help"))
63         usage();
64
65 #ifdef DEBUG
66     while (1) {
67         if (!strcmp(argv[1], "-inst")) {
68             inst_debug = 1;
69             argc--;
70             argv++;
71             continue;
72         }
73         if (!strcmp(argv[1], "-trap")) {
74             trap_debug = 1;
75             argc--;
76             argv++;
77             continue;
78         }
79         if (!strcmp(argv[1], "-jsr")) {
80             jsr_debug = 1;
81             argc--;
82             argv++;
83             continue;
84         }
85         if (!strcmp(argv[1], "-fp")) {
86             fp_debug = 1;
87             argc--;
88             argv++;
89             continue;
90         }
91         break;
92     }
93     if (inst_debug | trap_debug | jsr_debug | fp_debug)
94         dbg_file = fopen("apout.dbg", "w");
95 #endif
96
97     /* Prepare arg list for emulated environment */
98     argc--;
99     argv++;
100     Argc = argc;
101     Envp[0] = NULL;
102     for (i = 0; i < argc; i++)
103         Argv[i] = argv[i];
104
105     /* Initialise the stream pointers */
106     for (i = 3; i < NFILE; i++) {
107         stream[i] = NULL;
108         streammode[i] = NULL;
109     }
110     stream[0] = stdin;
111     streammode[0] = "r";
112     stream[1] = stdout;
113     streammode[1] = "w";
114     stream[2] = stderr;
115     streammode[2] = "w";
116
117     /* Set the translation to a fictitious */
118     /* root filesystem */
119     if ((apout_root = getenv("APOUT_ROOT"))) {
120         set_apout_root(apout_root);
121     } else {
122 #ifdef APOUT_DONT_ASSUME_ROOT
123         fprintf(stderr,
124                 "APOUT_ROOT env variable not set before running apout\n");
125         exit(1);
126 #else
127         set_apout_root("/");
128 #endif
129     }
130
131     /* Try to load the binary as an a.out */
132     if (load_a_out(argv[0], NULL, 1) == -1) {
133         fprintf(stderr, "Apout - couldn't load %s\n", argv[0]);
134         exit(1);
135     }
136
137     /* Other emulated systems (RT-11) can go here */
138
139     run();                      /* Now run the binary */
140     exit(0);
141 }
142
143 /* Translate from a filename to one which is possibly rooted in $APOUT_ROOT.
144  * Note we return a pointer to one of two buffers. The caller does not
145  * have to free the returned pointer, but successive calls will destroy
146  * calls from >2 calls earlier.
147  */
148 char *xlate_filename(char *name)
149 {
150     int i = whichrfn;
151
152     if (name == NULL)
153         return (NULL);
154     if (name[0] == '\0')
155         return (".");           /* Undocumented, but used in V7 */
156     if (name[0] != '/')
157         return (name);          /* Relative, keep it relative */
158     strcpy(rfn[i], name);       /* Copy name into buffer */
159     whichrfn = 1 - whichrfn;    /* Switch to other buffer next time */
160     return (realfilename[i]);
161 }
162
163 void set_apout_root(char *dirname)
164 {
165     strcpy(realfilename[0], dirname);
166     strcpy(realfilename[1], dirname);
167     rfn[0] = realfilename[0];
168     rfn[0] += strlen(realfilename[0]);
169     rfn[1] = realfilename[1];
170     rfn[1] += strlen(realfilename[1]);
171 }