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);
98 sprintf(tmpName,"/tmp/as31-XXXXXX.asm");
99 fd = mkstemps(tmpName, 4);
101 mesg_f("Cannot create temp file\n");
102 if (outfile) free(outfile);
103 if (lstfile) free(lstfile);
106 fin = fdopen(fd, "w");
108 mesg_f("Cannot open temp file: %s\n",tmpName);
110 if (outfile) free(outfile);
111 if (lstfile) free(lstfile);
115 while (!feof(finPre)) {
116 if (getline(&lineBuffer,&sizeBuf,finPre) == -1)
118 if ((includePtr=strstr(lineBuffer,INC_CMD))) {
119 includePtr=includePtr+strlen(INC_CMD);
120 while ((*includePtr==' ')|| //move includePtr to filename
122 (*includePtr=='\"')||
128 while ((includePtr[i]!=0) &&
129 (includePtr[i]!=10) &&
130 (includePtr[i]!=13) &&
131 (includePtr[i]!='\"') &&
132 (includePtr[i]!='\'')) {
136 includeFile=fopen(includePtr,"r");
137 mesg_f("including file: %s\n",includePtr);
139 mesg_f("Cannot open include file: %s\n",includePtr);
141 while (!feof(includeFile)) {
142 if (getline(&incLineBuffer,&incSizeBuf,includeFile) == -1)
144 fprintf(fin,"%s",incLineBuffer);
145 if (strlen(incLineBuffer)) {
155 fprintf(fin,"%s",lineBuffer);
157 } //.inc -files are now inserted
162 fin = freopen(tmpName, "r", stdin);
165 listing = fopen(lstfile,"w");
166 if( listing == NULL ) {
167 mesg_f("Cannot open file: %s for writing.\n",
171 if (outfile) free(outfile);
172 if (lstfile) free(lstfile);
177 /* what happens if this doesn't work */
178 emitopen(outfile, fmt, arg);
181 clear_location_counter();
182 fatal = abort_asap = 0;
191 if (!use_stdout) mesg_f("Begin Pass #1\n");
194 mesg_f("Errors in pass1, assembly aborted\n");
206 if (!use_stdout) mesg_f("Begin Pass #2\n");
210 mesg_f("Errors in pass2, assembly aborted\n");
215 unlink(tmpName); //delete tmpName
216 if (dashl) fclose(listing);
218 if (outfile) free(outfile);
219 if (lstfile) free(lstfile);
221 if (fatal) return -1;
226 /* the parser, lexer and other stuff that actually do the */
227 /* assembly will call to these two functions to report any */
228 /* errors or warning. error() calls exit() in the command */
229 /* line version, but the abort_asap flag was added, and the */
230 /* parser check it at the end of every line */
232 void error(const char *fmt, ...)
242 // fixme: negative line no for includes...
243 len = snprintf(buf, sizeof(buf), "Error, line %d, ", lineno-incLineCount);
244 len += vsnprintf(buf + len, sizeof(buf) - len, fmt, args);
245 snprintf(buf + len, sizeof(buf) - len, ".\n");
250 void warn(const char *fmt, ...)
259 printf("incLineCount=%d\n",incLineCount);
260 len = snprintf(buf, sizeof(buf), "Warning, line %d, ", lineno-incLineCount);
261 len += vsnprintf(buf + len, sizeof(buf) - len, fmt, args);
262 snprintf(buf + len, sizeof(buf) - len, ".\n");
267 void mesg_f(const char *fmt, ...)
274 vsnprintf(buf, sizeof(buf), fmt, args);