/* $Header: jetasm.c,v 3.17 96/05/21 10:21:17 barry Exp $ */ /************************************************************************** * * (c) Copyright Hewlett-Packard Company, 1994-1996. All rights are * reserved. Copying or other reproduction of this program except * for archival purposes is prohibited without the prior written * consent of Hewlett-Packard Company. * * RESTRICTED RIGHTS LEGEND * * Use, duplication, or disclosure by the Government is subject to * restrictions as set forth in paragraph (b) (3) (B) of the Rights * in Technical Data and Computer Software clause in DAR 7-104.9(a). * * HEWLETT-PACKARD COMPANY * Boise, Idaho, USA ***************************************************************************/ /* ******************************* NOTICE *************************************** HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE SOFTWARE OR TECHNICAL INFORMATION. HEWLETT-PACKARD COMPANY DOES NOT WARRANT, GUARANTEE OR MAKE ANY REPRESENTATIONS REGARDING THE USE OR THE RESULTS OF THE USE OF THE SOFTWARE OR TECHNICAL INFORMATION IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY, CURRENTNESS, OR OTHERWISE. THE ENTIRE RISK AS TO THE RESULTS AND PERFORMANCE OF THE SOFTWARE OR TECHNICAL INFORMATION IS ASSUMED BY YOU. The exclusion of implied warranties is not permitted by some jurisdictions. The above exclusion may not apply to you. IN NO EVENT WILL HEWLETT-PACKARD COMPANY BE LIABLE TO YOU FOR ANY CONSEQUENTIAL, INCIDENTAL OR INDIRECT DAMAGES (INCLUDING DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION AND THE LIKE) ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE OR TECHNICAL INFORMATION EVEN IF HEWLETT-PACKARD HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Because some jurisdictions do not allow the exclusion or limitation of liability for consequential or incidental damages, the above limitations may not apply to you. Hewlett-Packard liability to you for actual damages from any cause whatsoever, and regardless of the form of the action (whether in contract, tort including negligence, product liability or otherwise), will be limited to US $50. Copyright (c) 1994-1996 Hewlett-Packard Company. All rights reserved. ****************************************************************************** */ #include #include #include #include #include #include #include "jetlib.h" /* PCL XL Stream library Header and basic data types */ #include "jetasmf.h" /* PCL XL Stream Assembler Header */ #define TRUE_JetAsm 1 #define FALSE_JetAsm 0 #define HP_MEMALLOC(size) malloc(size) #define HP_MEMDEALLOC(ptr) free(ptr) #define HP_MEMREALLOC(ptr, size) realloc(ptr, size) static char* JetAsm_version = "PCL XL Stream Assembler/Disassembler. $Revision: 3.17 $"; static char* JetAsm_copyright = "(c) Copyright Hewlett-Packard Company, 1994-1996. All rights are reserved."; static int errorLogged = 0; FILE *outputFile = 0; /* the assembler output file */ FILE *errOutputFile = 0; FILE *msgOut; int TagOperations = 0; PathList localPathList; /* pointer to the $include path file list built by this program: */ PathList *pPathList = &localPathList; SInt16FuncReturnType HP_ReadInputString( HP_pUByte returnString, HP_UInt16 returnStringLen ) { int returnValue = 1; if (fgets(returnString, returnStringLen, stdin)) { returnValue = 0; } return(returnValue); } SInt16FuncReturnType HP_EscapeMacro(HP_StreamHandleType pStream, HP_pUByte escapeName, HP_pUByte macroBody, HP_pUByte returnString, HP_UInt16 returnStringMax) { HP_SInt16 returnValue = 0; if (strcmp(escapeName, "test")==0) { fprintf(errOutputFile, "Escape Name = \"%s\". Macro Body = \"%s\".\n", escapeName, macroBody); if (strlen(macroBody) <= returnStringMax) { strcpy(returnString, macroBody); } } else { fprintf(errOutputFile, "Escape Name = \"%s\". Macro Body = \"%s\".\n", escapeName, macroBody); returnValue = 1; /* non-zero means macro failed or not recognized */ } return(returnValue); } StdFuncReturnType HP_LogMsg(HP_pUByte outBuff) { fputs(outBuff, msgOut); } StdFuncReturnType HP_LogError(HP_pUByte outBuff) { fputs(outBuff, errOutputFile); errorLogged = 1; } HP_StdFuncPrefix HP_FlushOutBuffer(HP_StreamHandleType pStream, unsigned long cookie, HP_pUByte pOutBuffer, HP_SInt32 libBuffLen) { HP_SInt32 index; index = 0; while ((!ferror(outputFile)) && (index != libBuffLen)) { fputc((* pOutBuffer++), outputFile); index++; } if (ferror(outputFile)) { HP_SetErrorCode(pStream, HPERR_FlushBufferFailed); } pStream->HP_CurrentBufferLen = 0; /* set outbuffer to empty */ } void manualDump(char *outFile) { if (!outFile) msgOut = stdout; else { msgOut = fopen(outFile, "wt"); if (!msgOut) { fprintf(errOutputFile, "Could Not Open Manual Output File: %s\n", outFile); fprintf(errOutputFile, "MANUAL DISPLAY ABORTED.\n"); return; } } printDoc(); } int HexDumpFile (char *inFile, char *outFile) { FILE *inputFile = 0; /* the assembler output file */ int getCount, x; long offset=0; unsigned char buffer[16]; unsigned char lineBuffer[256]; unsigned char asciiBuffer[32]; if (!outFile) outputFile = stdout; else { outputFile = fopen(outFile, "wt"); if (!outputFile) return 1; if (!strcmp(inFile, outFile)) { fprintf(errOutputFile, "Could Not Open Hex Dump Output File: %s\n", outFile); exitDump: fprintf(errOutputFile, "HEX DUMP ABORTED.\n"); return 1; } } inputFile = fopen(inFile, "rb"); if (!inputFile) { fprintf(errOutputFile, "Unable to open source file: %s\n", inFile); goto exitDump; } fprintf(outputFile, "HEX Dump of file: %s\n\n", inFile); do { getCount = fread(buffer, 1, 16, inputFile); sprintf(lineBuffer, "%08X - %02X %02X %02X %02X %02X %02X %02X %02X " " %02X %02X %02X %02X %02X %02X %02X %02X -", offset, buffer[ 0], buffer[ 1], buffer[ 2], buffer[ 3], buffer[ 4], buffer[ 5], buffer[ 6], buffer[ 7], buffer[ 8], buffer[ 9], buffer[10], buffer[11], buffer[12], buffer[13], buffer[14], buffer[15]); for (x=0;x 127) buffer[ x] = '^'; } sprintf(asciiBuffer," %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n", buffer[ 0], buffer[ 1], buffer[ 2], buffer[ 3], buffer[ 4], buffer[ 5], buffer[ 6], buffer[ 7], buffer[ 8], buffer[ 9], buffer[10], buffer[11], buffer[12], buffer[13], buffer[14], buffer[15]); fprintf(outputFile, "%s%s", lineBuffer, asciiBuffer); offset += getCount; } while (getCount); fprintf(errOutputFile, "HEX DUMP COMPLETE.\n"); return 0; } main (int argc, HP_pChar argv[]) { #define MAX_OUT_BUFFER_SIZE 4000 int argIndex; int binaryDump=0; int printUsage = FALSE_JetAsm; int doParse = 1; int doManual= 0; int disassemblyRequested = 0; HP_StreamHandleType pStream; HP_pChar inFileName = "stdin"; HP_pChar outFileName = 0; HP_pChar errFileName = 0; /* default disassembler directive: */ DisAsmEnumType disAsmEnum = eDisAsmOnly; msgOut = stdout; INIT_PATH_LIST(pPathList); /* initialize the input file path list */ /* replace unwanted RCS revision $'s in revision string with spaces */ { int index; int length = strlen(JetAsm_version); for (index = 0; index < length; index++) { if (JetAsm_version[index] == '$') { JetAsm_version[index] = ' '; } } } if (argc < 2) { inFileName = "stdin"; } else { int gotFileName = FALSE_JetAsm; for (argIndex = 1; (argIndex < argc) && doParse && (!printUsage);) { if (argv[argIndex][0] != '-') { gotFileName = TRUE_JetAsm; inFileName = argv[argIndex]; argIndex++; } else if (argv[argIndex][0] == '-') { if (strlen(argv[argIndex]) == 2) { switch (argv[argIndex][1]) { case 'I': /* directory for include file path */ if (pPathList->numFilePaths < MAX_PATHS) { pPathList->pathArray[(pPathList->numFilePaths)++] = argv[argIndex + 1]; argIndex = argIndex + 2; } else { fprintf(errOutputFile, "\nERROR...\n"); fprintf(errOutputFile, "TOO MANY FILE PATHS DEFINED (\"-I\").\n"); fprintf(errOutputFile, "MAXIMUM PATHS = %d\n", MAX_PATHS); printUsage = TRUE_JetAsm; } break; case 'h': case '?': doParse = 0; printUsage = TRUE_JetAsm; argIndex = argIndex + 1; break; case 'd': disassemblyRequested = 1; disAsmEnum = eDisAsmOnly; argIndex = argIndex + 1; break; case 's': disassemblyRequested = 1; disAsmEnum = eDisAsmStatOnly; argIndex = argIndex + 1; break; case 'o': outFileName = argv[argIndex + 1]; argIndex = argIndex + 2; break; case 'e': errFileName = argv[argIndex + 1]; argIndex = argIndex + 2; break; default : doParse = 0; printUsage = TRUE_JetAsm; argIndex = argc; break; } } else if (strcmp(argv[argIndex], "-ds") == 0) { disassemblyRequested = 1; disAsmEnum = eDisAsmAndStat; argIndex = argIndex + 1; } else if (strcmp(argv[argIndex], "-dt") == 0) { disassemblyRequested = 1; TagOperations = 1; argIndex = argIndex + 1; } else if (strcmp(argv[argIndex], "-cover") == 0) { disassemblyRequested = 1; disAsmEnum = eDisAsmCoverage; argIndex = argIndex + 1; } else if (strcmp(argv[argIndex], "-man") == 0) { doManual=1;/* did call printDoc(); */ argIndex = argIndex + 1; } else if (strcmp(argv[argIndex], "-dump") == 0) { binaryDump = 1; argIndex = argIndex + 1; } else { /* parameter leading with '-' (i.e. "-I") had * more than two characters */ printUsage = TRUE_JetAsm; } } } /* end of for loop */ if (doParse & (!gotFileName)) { inFileName = "stdin"; } } /* Open error output file for -e parameter */ if (!errFileName) errOutputFile = stderr; else errOutputFile = fopen(errFileName, "w+"); fprintf(errOutputFile, "%s (%s)\n", JetAsm_version, __DATE__); fprintf(errOutputFile, "%s\n", JetAsm_copyright); if (doManual) { manualDump(outFileName); return 0; } if (binaryDump) { fprintf(errOutputFile, "Hex Dump of File: %s...\n", inFileName); return HexDumpFile(inFileName, outFileName); } if (printUsage) { fprintf(errOutputFile, "usage (assembly):\n"); fprintf(errOutputFile, " jetasm [inFile] [-I dir1...-I dirn] [-o ] [-man | -h | -?]\n"); fprintf(errOutputFile, "\n"); fprintf(errOutputFile, "usage (disassembly):\n"); fprintf(errOutputFile, " jetasm [inFile] [-d | -ds | -s | -cover] [-o ] [-man | -h | -?]\n\n"); doParse = 0; } if (doParse) { if (disassemblyRequested) { fprintf(errOutputFile, "Disassembling File: %s...\n", inFileName); } else { fprintf(errOutputFile, "Assembling File: %s...\n", inFileName); } } if (doParse) { pStream = HP_NewStream(MAX_OUT_BUFFER_SIZE, HP_FlushOutBuffer, 0 /* cookie */); if ((pStream) && HP_NoErrors(pStream)) { if (!disassemblyRequested) { /* Default function is Assembly: */ if (!outFileName) { outFileName = "jetasm.out"; } if ((strcmp(inFileName, outFileName) != 0) && (outputFile = fopen(outFileName, "wb"))) { ParseAssembly(pStream, inFileName, pPathList); if (HP_GetErrorCount(pStream) == 0) { fprintf(errOutputFile, "Output File: %s\n", outFileName); HP_FlushStreamBuffer(pStream); fclose(outputFile); fprintf(errOutputFile, "ASSEMBLY SUCCESSFUL.\n"); } } else { fprintf(errOutputFile, "Could Not Open Assembler Output File: %s\n", outFileName); fprintf(errOutputFile, "ASSEMBLY ABORTED.\n"); } } else { /* Disassembly Selected */ int doParseStream = 0; if (!outFileName) { outFileName = "stdout"; outputFile = stdout; doParseStream = 1; } else { if ((strcmp(inFileName, outFileName) != 0) && (outputFile = fopen(outFileName, "w"))) { doParseStream = 1; } else { fprintf(errOutputFile, "Could Not Open Disassembler Output File: %s\n", outFileName); fprintf(errOutputFile, "DISASSEMBLY ABORTED.\n"); } } if (doParseStream) { ParseStream(pStream, inFileName, pPathList, disAsmEnum); if (HP_GetErrorCount(pStream) == 0) { fprintf(errOutputFile, "Output File: %s\n", outFileName); HP_FlushStreamBuffer(pStream); } fclose(outputFile); } } } } return(errorLogged); }