9 #include <unistd.h> //for user stuff
14 // added ".inc" command to include header files. B.Porr
16 /* global variables */
19 int fatal=0, abort_asap=0;
22 FILE *listing=NULL, *fin=NULL;
27 int run_as31(const char *infile, int lst, int use_stdout,
28 const char *fmt, const char *arg, const char *customoutfile)
30 char *outfile=NULL, *lstfile=NULL;
31 const char *extension;
33 int i,len, baselen, extlen;
36 char *lineBuffer=NULL;
38 char *includePtr=NULL;
39 char *incLineBuffer=NULL;
41 FILE* includeFile=NULL;
44 /* first, figure out all the file names */
47 extension = emit_extension(fmt);
48 extlen = strlen(extension);
50 len = baselen = strlen(infile);
51 if (len >= 4 && strcasecmp(infile + len - 4, ".asm") == 0) {
56 asmfile = (char *)malloc(baselen + 5);
57 strcpy(asmfile, infile);
60 lstfile = (char *)malloc(baselen + 5);
61 strncpy(lstfile, infile, baselen);
62 strcpy(lstfile + baselen, ".lst");
68 if (customoutfile==NULL) {
69 outfile = (char *)malloc(baselen + extlen + 2);
70 strncpy(outfile, infile, baselen);
71 *(outfile + baselen) = '.';
72 strcpy(outfile + baselen + 1, extension);
74 len = strlen(customoutfile);
75 outfile = (char *)malloc(len + 1);
76 strncpy(outfile, customoutfile, len);
80 /* now open the files */
83 finPre = freopen(asmfile, "r", stdin);
86 strcpy(asmfile + baselen, ".asm");
87 finPre = freopen(asmfile, "r", stdin);
90 mesg_f("Cannot open input file: %s\n", asmfile);
92 if (outfile) free(outfile);
93 if (lstfile) free(lstfile);
99 sprintf(tmpName,"/tmp/as31-%i-%ld.asm",uid,random());
100 fin = fopen(tmpName, "w");
102 mesg_f("Cannot open temp file: %s\n",tmpName);
103 if (outfile) free(outfile);
104 if (lstfile) free(lstfile);
108 while (!feof(finPre)) {
109 getline(&lineBuffer,&sizeBuf,finPre);
110 if ((includePtr=strstr(lineBuffer,INC_CMD))) {
111 includePtr=includePtr+strlen(INC_CMD);
112 while ((*includePtr==' ')|| //move includePtr to filename
114 (*includePtr=='\"')||
120 while ((includePtr[i]!=0) &&
121 (includePtr[i]!=10) &&
122 (includePtr[i]!=13) &&
123 (includePtr[i]!='\"') &&
124 (includePtr[i]!='\'')) {
128 includeFile=fopen(includePtr,"r");
129 mesg_f("including file: %s\n",includePtr);
131 mesg_f("Cannot open include file: %s\n",includePtr);
133 while (!feof(includeFile)) {
134 getline(&incLineBuffer,&incSizeBuf,includeFile);
135 fprintf(fin,"%s",incLineBuffer);
136 if (strlen(incLineBuffer)) {
146 fprintf(fin,"%s",lineBuffer);
148 } //.inc -files are now inserted
153 fin = freopen(tmpName, "r", stdin);
156 listing = fopen(lstfile,"w");
157 if( listing == NULL ) {
158 mesg_f("Cannot open file: %s for writing.\n",
162 if (outfile) free(outfile);
163 if (lstfile) free(lstfile);
168 /* what happens if this doesn't work */
169 emitopen(outfile, fmt, arg);
172 clear_location_counter();
173 fatal = abort_asap = 0;
182 if (!use_stdout) mesg_f("Begin Pass #1\n");
185 mesg_f("Errors in pass1, assembly aborted\n");
197 if (!use_stdout) mesg_f("Begin Pass #2\n");
201 mesg_f("Errors in pass2, assembly aborted\n");
206 unlink(tmpName); //delete tmpName
207 if (dashl) fclose(listing);
209 if (outfile) free(outfile);
210 if (lstfile) free(lstfile);
212 if (fatal) return -1;
217 /* the parser, lexer and other stuff that actually do the */
218 /* assembly will call to these two functions to report any */
219 /* errors or warning. error() calls exit() in the command */
220 /* line version, but the abort_asap flag was added, and the */
221 /* parser check it at the end of every line */
223 void error(const char *fmt, ...)
233 // fixme: negative line no for includes...
234 len = snprintf(buf, sizeof(buf), "Error, line %d, ", lineno-incLineCount);
235 len += vsnprintf(buf + len, sizeof(buf) - len, fmt, args);
236 snprintf(buf + len, sizeof(buf) - len, ".\n");
241 void warn(const char *fmt, ...)
250 printf("incLineCount=%d\n",incLineCount);
251 len = snprintf(buf, sizeof(buf), "Warning, line %d, ", lineno-incLineCount);
252 len += vsnprintf(buf + len, sizeof(buf) - len, fmt, args);
253 snprintf(buf + len, sizeof(buf) - len, ".\n");
258 void mesg_f(const char *fmt, ...)
265 vsnprintf(buf, sizeof(buf), fmt, args);