void printStandard(Header *h) { char *name, *prefix; name = (char*)alloca(sizeof(char) * (NAME_LEN + 1)); prefix = (char*)alloca(sizeof(char) * (PREFIX_LEN + 1)); strcap(name, h->name, NAME_LEN); strcap(prefix, h->prefix, PREFIX_LEN); if (strlen(prefix)) printf("%s/", prefix); printf("%s\n", name); }
//-------------------------------------------------------- // Assemble macro // pre: macroFP contains file pointer to macro // for (each line of macro) { // if macro label, define // perform parameter substitution // assemble this line of macro // } int asmMacro(int size, char *label, char *arg, int *errorPtr) { char capLine[256], macLine[MAC_SIZE], labelNumA[16]; char *capL, *macL; char arguments[MAX_ARGS][ARG_SIZE+1]; const int MAXT = 128; // maximum number of tokens const int MAX_SIZE = 512; // maximun size of input line char *token[MAXT]; // pointers to tokens char tokens[MAX_SIZE]; // place tokens here char *tokenEnd[MAXT]; // where tokens end in source line int error, argN, i, n; int tempFP; // temporary File Pointer int value; bool backRef; bool textArg; // true for 'text' argument bool endmFlag; // set true by ENDM instruction // clear arguments[] array for (argN=0; argN < MAX_ARGS; argN++) // for all of arguments[] array arguments[argN][0] = '\0'; // *ck 12-6-2005 added following to support size extensions on macro calls. // Argument \0 is reserved for size and defaults to .W switch (size) { case BYTE_SIZE: strcpy(arguments[0],"B"); break; case WORD_SIZE: strcpy(arguments[0],"W"); break; case LONG_SIZE: strcpy(arguments[0],"L"); break; default: strcpy(arguments[0],"W"); } argN = 0; macroNestLevel++; // count nested macro calls if (macroNestLevel > MACRO_NEST_LIMIT) { // if nested too deep NEWERROR(*errorPtr, MACRO_NEST); // error, probably in recursive macro call macroNestLevel--; // count nested macro calls return NORMAL; } // build macro "mmm" identifier for listing for (i=0; i < macroNestLevel; i++) lineIdent[i] = 'm'; lineIdent[i] = '\0'; // Define the label attached to this macro call, if any if (*label) define(label, loc, pass2, true, errorPtr); // parse macro call and put arguments into array strcap(capLine, arg); capL = capLine; if (*capL) argN = 1; while (*capL) { // loop until out of arguments i = 0; textArg = false; while ( *capL && ( (*capL != ',' && !(isspace(*capL)) ) || textArg )) { if (*capL == '<') { // if < *ck 12-7-2005 if (!textArg) // if not text mode textArg = true; // set text mode flag else arguments[argN][i++] = *capL; // copy < to argument } else if (*capL == '>') { if (textArg) // if text mode textArg = false; // turn off text mode else arguments[argN][i++] = *capL; // copy > to argument } else if (!textArg && (*capL == '\'' && *(capL+1) == '\'')) // if '' capL++; // skip ' (NULL Argument) else if (i < ARG_SIZE && *capL != '\n') arguments[argN][i++] = *capL; // put argument in arguments[] capL++; } arguments[argN][i] = '\0'; // terminate argument if (*capL == ',') { // if more arguments remain capL++; // skip ',' if (*capL == '\n') { // if ',' was last char on line // macro parameters continue on next line if (pass2) { if (listFlag) { printError(listFile, *errorPtr, lineNum); if (!skipList) listLine(line, lineIdent); } else { printError(NULL, *errorPtr, lineNum); } } if (fgets(line, 256, inFile) == NULL) { // get next line NEWERROR(*errorPtr, INVALID_ARG); macroNestLevel--; // count nested macro calls return NORMAL; } strcap(capLine, line); capL = capLine; error = OK; continuation = false; if (pass2 && listFlag) listLoc(); if (*capL != '&') { // continuation line must start with '&' NEWERROR(*errorPtr, INVALID_ARG); macroNestLevel--; // count nested macro calls return NORMAL; } *capL++; } capL = skipSpace(capL); // skip spaces if (!(*capL)) { // if syntax error NEWERROR(*errorPtr, SYNTAX); macroNestLevel--; // count nested macro calls return NORMAL; } argN++; // next spot in arguments[] if (argN >= MAX_ARGS) { // if too many arguments NEWERROR(*errorPtr, TOO_MANY_ARGS); macroNestLevel--; // count nested macro calls return NORMAL; } } else break; } if (pass2 && listFlag && !skipList) { // if macro call should be listed listLoc(); listLine(line, lineIdent); } // position tempFile to macro definition fseek(tmpFile, macroFP, SEEK_SET); // move to macro definition // send each line of macro to assembler labelNum++; // increment macro label number itoa(labelNum, labelNumA, 10); // convert labelNum to string endmFlag = false; while(!endmFlag && fgets(line, 256, tmpFile)) { strcap(capLine, line); tokenize(capLine, ", \t\n", token, tokens); // tokenize line error = OK; skipList = false; printCond = false; macL = macLine; capL = capLine; while(*capL && isspace(*capL)) // copy spaces *macL++ = *capL++; if (*capL == '*') { // if comment line while(*capL) // copy rest of line *macL++ = *capL++; } else { // else not comment line if (skipCond) // if code conditionally skipped while(*capL) *macL++ = *capL++; // just copy line to check for ENDC else { // else, include code // do macro parameter substitution and label generation while (*capL) { // while not empty while (*capL && !(isspace(*capL))) { // while not empty and not space if (*capL == '\\') { // if macro label or parameter capL++; if (*capL == '@') { // if \@ macro label capL++; *macL++ = '_'; // add '_' i = 0; // add labelNum to macro label while(labelNumA[i]) { *macL++ = labelNumA[i++]; } } else if (isalnum(*capL)) { // if alpha numeric n = -1; if (isdigit(*capL)) // if parameter \0 - \9 n = *capL++ - '0'; // convert parameter to integer else if (*capL >= 'A' && *capL <= 'Z') // if parameter \A - \Z n = *capL++ - 'A' + 10; // convert parameter to integer else // invalid argument NEWERROR(error, INVALID_ARG); if (n >= 0 && n < MAX_ARGS) { // if valid argument number i = 0; while(arguments[n][i]) { *macL++ = arguments[n][i++]; } } else NEWERROR(error, INVALID_ARG); } else NEWERROR(error, SYNTAX); // if NARG opcode } else if( !(stricmp(token[1], "NARG")) ) { sprintf(macL,"%d",argN); if (argN > 9) macL++; macL++; capL += 4; } else { *macL++ = *capL++; // copy macro line } } while(*capL && isspace(*capL)) // copy spaces *macL++ = *capL++; } } // end else, include code } // end if comment, else not comment *macL = '\0'; tempFP = ftell(tmpFile); // save current file position to support nested macros *ck 12-1-2005 continuation = false; strcpy(line,macLine); // replace original source line with macro line if (!MEXflag) skipList = true; skipCreateCode = false; // pre process macro commands // ----- ENDM and MEXIT ----- if( !(stricmp(token[1], "ENDM")) || // if ENDM opcode or (!(stricmp(token[1],"MEXIT"))) && !skipCond) { // MEXIT if (token[0] != empty) // if label present NEWERROR(*errorPtr, LABEL_ERROR); endmFlag = true; skipCreateCode = true; // ----- IFARG ----- } else if(!(stricmp(token[1], "IFARG"))) { // if IFARG opcode if (token[0] != empty) // if label present NEWERROR(*errorPtr, LABEL_ERROR); if (token[2] == empty) { // if IFARG argument missing NEWERROR(*errorPtr, INVALID_ARG); } else { eval(token[2], &value, &backRef, &error); //value--; if (error < ERRORN && value > 0 && value < MAX_ARGS) { // if valid arg number if (arguments[value][0] == '\0') { // if argument does not exist skipCond = true; // skip lines in macro nestLevel++; // nest level of skip } } else { // else, invalid arg number NEWERROR(*errorPtr, INVALID_ARG); } } printCond = true; skipCreateCode = true; } assemble(line,&error); // this supports structured statements in macros macroFP = tempFP; // restore macroFP fseek(tmpFile, macroFP, SEEK_SET); // position tmpFile to previous spot *ck 12-1-2005 } // end while more lines of macro remain skipCreateCode = false; macroNestLevel--; // count nested macro calls // build macro "mmm" identifier for listing for (i=0; i < macroNestLevel; i++) lineIdent[i] = 'm'; lineIdent[i] = '\0'; skipList = true; // don't display ENDM twice return NORMAL; }
void initiate() { //Prompt Start printf("\n:> "); fgets(input,sizeof(input),stdin); //---------------------------------------------------------------------------------------- //EXIT COMMAND if((stricmp(input,"exit\n")==0)) //To exit command interpreter { main_exit(); } //---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- //CLEAR SCREEN COMMAND else if(stricmp(input,"clr\n")==0) //To clear screen { system("cls"); initiate(); } //---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- //TIME COMMANDS else if((stricmp(input,"time\n")==0)) //To show time (default format-24hr HH/MM/SS.MS) { sys_time_def(); initiate(); } else if((stricmp(input,"time-24\n")==0)) //To show time in 24hrformat (HH:MM) { sys_time_24hr(); initiate(); } else if((stricmp(input,"time-12\n")==0)) //To show time in 12hr format (HH:MM AM/PM) { sys_time_12hr(); initiate(); } else if((stricmp(input,"time-e\n")==0)) //To show time and provide 'editing' option { system("time"); initiate(); } //---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- //DATE COMMANDS else if((stricmp(input,"date\n")==0)) //To show date (default format:MM/DD/YYYY) { sys_date_def(); initiate(); } else if((stricmp(input,"date-e\n")==0)) //To show date and provide 'editing' option { system("date"); initiate(); } //---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- //DAY COMMANDS else if((stricmp(input,"day\n")==0)) //To show day/date of month { sys_day(); initiate(); } else if((stricmp(input,"day-i\n")==0)) //To show complete day info { sys_day_info(); initiate(); } //---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- // STRING MANIPULATION COMMANDS else if((stricmp(input,"strln\n")==0)) // Returns length of String { strln(); initiate(); } else if((stricmp(input,"strwht\n")==0)) // Returns no. of whitespaces in the String { strwht(); initiate(); } else if((stricmp(input,"strslice\n")==0)) // Returns a SubString from the given String using mentioned indices { strslice(); initiate(); } else if((stricmp(input,"strcap\n")==0)) // Capitalises the entered String { strcap(); initiate(); } else if((stricmp(input,"strtolow\n")==0)) // Converts uppercase String to lowercase { strtolow(); initiate(); } //---------------------------------------------------------------------------------------- else { printf("\aCommand not found.\n"); initiate(); } }
int incbin(int size, char *label, char *fileName, int *errorPtr) { char capLine[256]; char *src, *dst; FILE *incFile; unsigned char dataByte; if (size) { // if .size code specified NEWERROR(*errorPtr, INV_SIZE_CODE); // error, invalid size code return NORMAL; } if (!*fileName) { NEWERROR(*errorPtr, FILE_ERROR); // error, invalid syntax return NORMAL; } // Define the label attached to this include directive, if any if (*label) define(label, loc, pass2, true, errorPtr); strcap(capLine, fileName); // strip quotes from filename src = capLine; dst = capLine; while (*src) { if (*src != '\'' && *src != '\"') *dst++ = *src; src++; } // strip whitespace from end of filename dst--; while (dst > capLine && isspace(*dst)) dst--; dst++; *dst = '\0'; if (pass2 && listFlag) { // if incbin directive should be listed listLine(line); } try { incFile = fopen(capLine, "rb"); // attempt to open incbin binary file if (!incFile) { // if ERROR opening file NEWERROR(*errorPtr, FILE_ERROR); // error, invalid syntax return SEVERE; } // loop through every byte of incbin file // while(!feof(incFile)) { // fread(&dataByte, 1, 1, incFile); // read 1 byte of data from file while(fread(&dataByte, 1, 1, incFile) && !feof(incFile)) { // On pass 2, output the block of values directly // to the object and binary files (without putting them in the listing) if (pass2) { if (objFlag) outputObj(loc, dataByte, BYTE_SIZE); } loc++; // increment location counter once for each byte in file } fclose(incFile); if (pass2 && listFlag) { skipList = true; // don't list INCBIN statement again } } catch( ... ) { sprintf(buffer, "ERROR: An exception occurred in routine 'incbin'. \n"); printError(NULL, EXCEPTION, 0); return NULL; } return NORMAL; }
//-------------------------------------------------------- // INCLUDE directive (ck) // Inserts specified source file into current file and assembles // for (each line of new file) { // assemble this line // } int include(int size, char *label, char *fileName, int *errorPtr) { char incLine[256], capLine[256]; char *src, *dst; int error, i; int tempFP; // temporary File Pointer bool comment; // true when line is comment int value; bool backRef; FILE *incFile; FILE *tmpInFile; // save current file char quote; int lineNumInc; // line number for include file int lineNumSave; char fileNameSave[256]; if (size) { // if .size code specified NEWERROR(*errorPtr, INV_SIZE_CODE); // error, invalid size code return NORMAL; } if (!*fileName) { NEWERROR(*errorPtr, FILE_ERROR); // error, invalid syntax return NORMAL; } // Define the label attached to this include directive, if any if (*label) define(label, loc, pass2, true, errorPtr); strcap(capLine, fileName); // strip quotes from filename src = capLine; dst = capLine; src = skipSpace(src); // skip leading spaces in filename if (*src == '\"' || *src == '\'') { // *ck 12-9-2005 quote = *src; src++; } else quote = '\0'; while (*src && (*src != ' ' || quote) ) { if (*src == quote) break; else *dst++ = *src++; } // strip whitespace from end of filename dst--; while (dst > capLine && isspace(*dst)) dst--; dst++; *dst = '\0'; if (pass2 && listFlag) { // if include directive should be listed listLine(line); } try { incFile = fopen(capLine, "r"); // attempt to open include file if (!incFile) { // if error opening file NEWERROR(*errorPtr, FILE_ERROR); // error, invalid syntax return SEVERE; } tmpInFile = inFile; // save current input file inFile = incFile; // make include file input file strcpy(fileNameSave,includeFile); // save current include file strcpy(includeFile,capLine); // save new include file lineNumSave = lineNum; // save current line number // check to see if the included file is already open AnsiString includeFileStr(includeFile); for(int i = Main->MDIChildCount-1; i >= 0; i--) { TextStuff = (TTextStuff*)Main->MDIChildren[i]; //if(TextStuff->Project.CurrentFile == includeFileStr) { // if file already open if(ExpandFileName(TextStuff->Project.CurrentFile).AnsiCompareIC(ExpandFileName(includeFileStr)) == 0) { TextStuff->Messages->Height = 7; TextStuff->Messages->Items->Clear(); TextStuff->Messages->Enabled = false; } } // assemble each line of the include file // until END directive or EOF includeNestLevel++; // count nest level of include directive lineNum = 1; while(!endFlag && fgets(line, 256, inFile)) { error = OK; skipList = false; continuation = false; printCond = false; // true to print condition on listing line if(line[0] != '*' && line[1] != '~') // don't assemble font info assemble(line, &error); lineNum++; } fclose(inFile); inFile = tmpInFile; // restore previous input file strcpy(includeFile,fileNameSave); // restore previous include file lineNum = lineNumSave; // restore line number includeNestLevel--; if (pass2 && listFlag) { skipList = true; // don't list INCLUDE statement again listLine("-------------------- end include --------------------\n"); } } catch( ... ) { sprintf(buffer, "ERROR: An exception occurred in routine 'include'. \n"); printError(NULL, EXCEPTION, 0); return NULL; } return NORMAL; }
int processFile(char *szFile) { char capLine[256]; int error; char pass; MDL mdl; FILE *pfilInput; // Allocate temporary buffers used for code, data, and resources. gpbCode = (unsigned char *)malloc(kcbCodeMax); gpbData = (unsigned char *)malloc(kcbDataMax); gpbResource = (unsigned char *)malloc(kcbResMax); if (gpbCode == NULL || gpbData == NULL || gpbResource == NULL) { printf("Failed to allocate necessary assembly buffers!\n"); exit(0); } gfPass2 = FALSE; for (pass = 0; pass < 2; pass++) { gulOutLoc = gulCodeLoc = gulDataLoc = gulResLoc = 0; gbt = kbtCode; // block is code unless otherwise specified gpbOutput = gpbCode; pfilInput = PushSourceFile(szFile, NULL); if (pfilInput == NULL || pfilInput == (FILE *)-1) { fputs("Input file not found\n", stdout); exit(0); } //gpsseCur->iLineNum = 1; endFlag = FALSE; errorCount = warningCount = 0; do { while ((mdl = MdlGetLine(line, 256, gpsseCur->pfil)) != mdlEOF) { strcap(capLine, line); error = OK; continuation = FALSE; if (gfPass2 && gfListOn) { listLoc(); } if (mdl != mdlIgnoreLine) { assemble(capLine, &error); } if (gfPass2) { if (error > MINOR) { errorCount++; } else if (error > WARNING) { warningCount++; } if (gfListOn) { listLine(); } if (error != OK) { printf(line); printError(stdout, error, gpsseCur->iLineNum); if (gfListing) { printError(gpfilList, error, -1); } } } if (mdl != mdlPseudoLine) { gpsseCur->iLineNum++; } } } while (PopSourceFile()); if (!gfPass2) { gfPass2 = TRUE; } } return NORMAL; }