From cd6797499f8377d26018c27540dd1b8338ce0a3f Mon Sep 17 00:00:00 2001 From: nemerle Date: Thu, 19 May 2016 19:50:47 +0200 Subject: [PATCH] Add parsehdr to the build As requested in #22 (cherry picked from commit d5985b4b97cb06a89050c34674878c6430432fe1) --- tools/CMakeLists.txt | 1 + tools/parsehdr/CMakeLists.txt | 4 + tools/parsehdr/locident.h | 131 +- tools/parsehdr/parsehdr.cpp | 2208 +++++++++++++++------------------ tools/parsehdr/parsehdr.h | 16 +- 5 files changed, 1049 insertions(+), 1311 deletions(-) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 8d27447..e59dd48 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(makedsig) +add_subdirectory(parsehdr) diff --git a/tools/parsehdr/CMakeLists.txt b/tools/parsehdr/CMakeLists.txt index e69de29..83ca7f6 100644 --- a/tools/parsehdr/CMakeLists.txt +++ b/tools/parsehdr/CMakeLists.txt @@ -0,0 +1,4 @@ +add_executable(parselib parsehdr.cpp) + +target_link_libraries(parselib dcc_hash) +qt5_use_modules(parselib Core) diff --git a/tools/parsehdr/locident.h b/tools/parsehdr/locident.h index a57a86a..4a5b7ad 100644 --- a/tools/parsehdr/locident.h +++ b/tools/parsehdr/locident.h @@ -1,23 +1,24 @@ +#pragma once /*$Log: locident.h,v $ * Revision 1.6 94/02/22 15:20:23 cifuente * Code generation is done. - * + * * Revision 1.5 93/12/10 09:38:20 cifuente * New high-level types - * + * * Revision 1.4 93/11/10 17:30:51 cifuente * Procedure header, locals - * + * * Revision 1.3 93/11/08 12:06:35 cifuente * du1 analysis finished. Instantiates procedure arguments for user * declared procedures. - * + * * Revision 1.2 93/10/25 11:01:00 cifuente * New SYNTHETIC instructions for d/u analysis - * + * * Revision 1.1 93/10/11 11:47:39 cifuente * Initial revision - * + * * File: locIdent.h * Purpose: High-level local identifier definitions * Date: October 1993 @@ -26,92 +27,92 @@ /* Type definition */ typedef struct { - Int csym; /* # symbols used */ - Int alloc; /* # symbols allocated */ - Int *idx; /* Array of integer indexes */ + Int csym; /* # symbols used */ + Int alloc; /* # symbols allocated */ + Int *idx; /* Array of integer indexes */ } IDX_ARRAY; /* Type definitions used in the decompiled program */ typedef enum { TYPE_UNKNOWN = 0, /* unknown so far */ TYPE_BYTE_SIGN, /* signed byte (8 bits) */ - TYPE_BYTE_UNSIGN, /* unsigned byte */ + TYPE_BYTE_UNSIGN, /* unsigned byte */ TYPE_WORD_SIGN, /* signed word (16 bits) */ - TYPE_WORD_UNSIGN, /* unsigned word (16 bits) */ - TYPE_LONG_SIGN, /* signed long (32 bits) */ - TYPE_LONG_UNSIGN, /* unsigned long (32 bits) */ - TYPE_RECORD, /* record structure */ + TYPE_WORD_UNSIGN, /* unsigned word (16 bits) */ + TYPE_LONG_SIGN, /* signed long (32 bits) */ + TYPE_LONG_UNSIGN, /* unsigned long (32 bits) */ + TYPE_RECORD, /* record structure */ TYPE_PTR, /* pointer (32 bit ptr) */ TYPE_STR, /* string */ - TYPE_CONST, /* constant (any type) */ - TYPE_FLOAT, /* floating point */ - TYPE_DOUBLE, /* double precision float */ + TYPE_CONST, /* constant (any type) */ + TYPE_FLOAT, /* floating point */ + TYPE_DOUBLE, /* double precision float */ } hlType; -static char *hlTypes[13] = {"", "char", "unsigned char", "int", "unsigned int", - "long", "unsigned long", "record", "int *", "char *", - "", "float", "double"}; +static char *hlTypes[13] = {"", "char", "unsigned char", "int", "unsigned int", + "long", "unsigned long", "record", "int *", "char *", + "", "float", "double"}; typedef enum { - STK_FRAME, /* For stack vars */ - REG_FRAME, /* For register variables */ - GLB_FRAME, /* For globals */ + STK_FRAME, /* For stack vars */ + REG_FRAME, /* For register variables */ + GLB_FRAME, /* For globals */ } frameType; /* Enumeration to determine whether pIcode points to the high or low part * of a long number */ typedef enum { - HIGH_FIRST, /* High value is first */ - LOW_FIRST, /* Low value is first */ + HIGH_FIRST, /* High value is first */ + LOW_FIRST, /* Low value is first */ } hlFirst; /* LOCAL_ID */ typedef struct { - hlType type; /* Probable type */ - boolT illegal;/* Boolean: not a valid field any more */ - IDX_ARRAY idx; /* Index into icode array (REG_FRAME only) */ - frameType loc; /* Frame location */ - boolT hasMacro;/* Identifier requires a macro */ - char macro[10];/* Macro for this identifier */ - char name[20];/* Identifier's name */ - union { /* Different types of identifiers */ - byte regi; /* For TYPE_BYTE(WORD)_(UN)SIGN registers */ - struct { /* For TYPE_BYTE(WORD)_(UN)SIGN on the stack */ - byte regOff; /* register offset (if any) */ - Int off; /* offset from BP */ - } bwId; - struct _bwGlb { /* For TYPE_BYTE(WORD)_(UN)SIGN globals */ - int16 seg; /* segment value */ - int16 off; /* offset */ - byte regi; /* optional indexed register */ - } bwGlb; - struct _longId{ /* For TYPE_LONG_(UN)SIGN registers */ - byte h; /* high register */ - byte l; /* low register */ - } longId; - struct _longStkId { /* For TYPE_LONG_(UN)SIGN on the stack */ - Int offH; /* high offset from BP */ - Int offL; /* low offset from BP */ - } longStkId; - struct { /* For TYPE_LONG_(UN)SIGN globals */ - int16 seg; /* segment value */ - int16 offH; /* offset high */ - int16 offL; /* offset low */ - byte regi; /* optional indexed register */ - } longGlb; - struct { /* For TYPE_LONG_(UN)SIGN constants */ - dword h; /* high word */ - dword l; /* low word */ - } longKte; - } id; + hlType type; /* Probable type */ + boolT illegal;/* Boolean: not a valid field any more */ + IDX_ARRAY idx; /* Index into icode array (REG_FRAME only) */ + frameType loc; /* Frame location */ + boolT hasMacro;/* Identifier requires a macro */ + char macro[10];/* Macro for this identifier */ + char name[20];/* Identifier's name */ + union { /* Different types of identifiers */ + byte regi; /* For TYPE_BYTE(WORD)_(UN)SIGN registers */ + struct { /* For TYPE_BYTE(WORD)_(UN)SIGN on the stack */ + byte regOff; /* register offset (if any) */ + Int off; /* offset from BP */ + } bwId; + struct _bwGlb { /* For TYPE_BYTE(WORD)_(UN)SIGN globals */ + int16 seg; /* segment value */ + int16 off; /* offset */ + byte regi; /* optional indexed register */ + } bwGlb; + struct _longId{ /* For TYPE_LONG_(UN)SIGN registers */ + byte h; /* high register */ + byte l; /* low register */ + } longId; + struct _longStkId { /* For TYPE_LONG_(UN)SIGN on the stack */ + Int offH; /* high offset from BP */ + Int offL; /* low offset from BP */ + } longStkId; + struct { /* For TYPE_LONG_(UN)SIGN globals */ + int16 seg; /* segment value */ + int16 offH; /* offset high */ + int16 offL; /* offset low */ + byte regi; /* optional indexed register */ + } longGlb; + struct { /* For TYPE_LONG_(UN)SIGN constants */ + dword h; /* high word */ + dword l; /* low word */ + } longKte; + } id; } ID; typedef struct { - Int csym; /* No. of symbols in the table */ - Int alloc; /* No. of symbols allocated */ - ID *id; /* Identifier */ + Int csym; /* No. of symbols in the table */ + Int alloc; /* No. of symbols allocated */ + ID *id; /* Identifier */ } LOCAL_ID; diff --git a/tools/parsehdr/parsehdr.cpp b/tools/parsehdr/parsehdr.cpp index f2a35c7..e6a34dd 100644 --- a/tools/parsehdr/parsehdr.cpp +++ b/tools/parsehdr/parsehdr.cpp @@ -4,618 +4,480 @@ /* Code to parse a header (.h) file */ /* Descended from xansi; thanks Geoff! thanks Glenn! */ +#include "parsehdr.h" +#include /* For debugging */ #include #include #include -#include -#include /* For debugging */ -#include "parsehdr.h" +#include -dword userval; +dword userval; /* the IGNORE byte */ -byte slosh; -byte last_slosh; -byte quote1; -byte quote2; -byte comment; -byte hash; -byte ignore1; /* Special: ignore egactly 1 char */ -byte double_slash; -byte spare; +byte slosh; +byte last_slosh; +byte quote1; +byte quote2; +byte comment; +byte hash; +byte ignore1; /* Special: ignore egactly 1 char */ +byte double_slash; +byte spare; -int buff_idx; -char buffer[BUFF_SIZE]; +int buff_idx; +char buffer[BUFF_SIZE]; -byte start; /* Started recording to the buffer */ -byte func; /* Function header detected */ -byte hash_ext; -int curly; /* Level inside curly brackets */ -int xtern; /* Level inside a extern "C" {} situation */ -int round; /* Level inside () */ -int line,col; -dword chars; -char lastch; +byte start; /* Started recording to the buffer */ +byte func; /* Function header detected */ +byte hash_ext; +int curly; /* Level inside curly brackets */ +int xtern; /* Level inside a extern "C" {} situation */ +int round; /* Level inside () */ +int line, col; +dword chars; +char lastch; -#define NIL -1 /* Used as an illegal index */ +#define NIL -1 /* Used as an illegal index */ -FILE *datFile; /* Stream of the data (output) file */ -char fileName[81]; /* Name of current header file */ +FILE *datFile; /* Stream of the data (output) file */ +char fileName[81]; /* Name of current header file */ -PH_FUNC_STRUCT *pFunc; /* Pointer to the functions array */ -int numFunc; /* How many elements saved so far */ -int allocFunc; /* How many elements allocated so far */ -int headFunc; /* Head of the function name linked list */ - -PH_ARG_STRUCT *pArg; /* Pointer to the arguements array */ -int numArg; /* How many elements saved so far */ -int allocArg; /* How many elements allocated so far */ -int headArg; /* Head of the arguements linked list */ +PH_FUNC_STRUCT *pFunc; /* Pointer to the functions array */ +int numFunc; /* How many elements saved so far */ +int allocFunc; /* How many elements allocated so far */ +int headFunc; /* Head of the function name linked list */ +PH_ARG_STRUCT *pArg; /* Pointer to the arguements array */ +int numArg; /* How many elements saved so far */ +int allocArg; /* How many elements allocated so far */ +int headArg; /* Head of the arguements linked list */ // DO Callback -boolT -phDoCB(int id, char *data) -{ -/* return callback(hDCX, id, data, userval);*/ - return TRUE; +boolT phDoCB(int id, char *data) { + /* return callback(hDCX, id, data, userval);*/ + return true; } +void phError(char *errmsg) { + char msg[200]; -void -phError(char *errmsg) -{ - char msg[200]; - - sprintf(msg,"PH *ERROR*\nFile: %s L=%d C=%d O=%lu\n%s", fileName, line, col, - chars, errmsg); - printf(msg); + sprintf(msg, "PH *ERROR*\nFile: %s L=%d C=%d O=%lu\n%s", fileName, line, col, + chars, errmsg); + printf(msg); } +void phWarning(char *errmsg) { + char msg[200]; -void -phWarning(char *errmsg) -{ - char msg[200]; - - sprintf(msg,"PH -warning-\nFile: %s L=%d C=%d O=%lu\n%s\n", fileName, line, - col, chars, errmsg); - printf(msg); + sprintf(msg, "PH -warning-\nFile: %s L=%d C=%d O=%lu\n%s\n", fileName, line, + col, chars, errmsg); + printf(msg); } - - - -int -IsIgnore() -{ - return( comment || - quote1 || - quote2 || - slosh || - hash || - ignore1 || - double_slash - ); +int IsIgnore() { + return (comment || quote1 || quote2 || slosh || hash || ignore1 || + double_slash); } -boolT -isAlphaNum(char ch) -{ - return(((ch >= 'A') && (ch <= 'Z')) || - ((ch >= 'a') && (ch <= 'z')) || - ((ch >= '0') && (ch <= '9')) || - (ch == '_') - ); +boolT isAlphaNum(char ch) { + return (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')) || + ((ch >= '0') && (ch <= '9')) || (ch == '_')); } - -boolT -AddToBuffer(char ch) -{ - if (buff_idx >= BUFF_SIZE) - { - ERR("function buffer overflow (function unterminated?)\n"); - return FALSE; - } - else - { - buffer[buff_idx++] = ch; - return TRUE; - } +boolT AddToBuffer(char ch) { + if (buff_idx >= BUFF_SIZE) { + ERR("function buffer overflow (function unterminated?)\n"); + return false; + } else { + buffer[buff_idx++] = ch; + return true; + } } -boolT -remFromBuffer() -{ - if (buff_idx == 0) - { - ERR("function buffer underflow (internal error?)\n"); - return FALSE; - } - else - { - buff_idx--; - return TRUE; - } +boolT remFromBuffer() { + if (buff_idx == 0) { + ERR("function buffer underflow (internal error?)\n"); + return false; + } else { + buff_idx--; + return true; + } } - - - - /*----------------------------------------------*/ -/* This is a function declaration, typedef, etc.* +/* This is a function declaration, typedef, etc.*/ /* Do something with it. */ /*----------------------------------------------*/ -void -ProcessBuffer(int id) -{ - if (buff_idx > 0) - { - buffer[buff_idx] = '\0'; +void ProcessBuffer(int id) { + if (buff_idx > 0) { + buffer[buff_idx] = '\0'; - // CALL CALL BACK FUNTION WITH APPRORIATE CODE! + // CALL CALL BACK FUNTION WITH APPRORIATE CODE! - switch (id) - { - case PH_FUNCTION: - // eek, but... - case PH_PROTO: - // sort out into params etc - phBuffToFunc(buffer); - break; + switch (id) { + case PH_FUNCTION: + // eek, but... + case PH_PROTO: + // sort out into params etc + phBuffToFunc(buffer); + break; - case PH_TYPEDEF: - case PH_DEFINE: - // sort out into defs - phBuffToDef(buffer); - break; + case PH_TYPEDEF: + case PH_DEFINE: + // sort out into defs + phBuffToDef(buffer); + break; - case PH_MPROTO: - // eek! + case PH_MPROTO: + // eek! - case PH_VAR: - // eek! + case PH_VAR: + // eek! - case PH_JUNK: - default: - phDoCB(id, buffer); - break; - } - start = FALSE; - func = FALSE; - buff_idx = 0; - } + case PH_JUNK: + default: + phDoCB(id, buffer); + break; + } + start = false; + func = false; + buff_idx = 0; + } } - -void -phInit(char *filename) // filename is for reference only!!! +void phInit(char *filename) // filename is for reference only!!! { - slosh = - last_slosh = - start = - func = - comment = - double_slash = - hash = - ignore1 = - quote1 = - quote2 = - hash_ext = - FALSE; + slosh = last_slosh = start = func = comment = double_slash = hash = ignore1 = + quote1 = quote2 = hash_ext = false; - buff_idx = - curly = - xtern = - col = - round = 0; + buff_idx = curly = xtern = col = round = 0; - line = 1; + line = 1; - chars = 0L; - - lastch = 0; + chars = 0L; + lastch = 0; } - - -boolT -phFree(void) -{ - // remove atoms etc, free buffers - return TRUE; +boolT phFree(void) { + // remove atoms etc, free buffers + return true; } +void phChar(char ch) { + col++; + chars++; + // printf("%d%% done\r", chars*100/filelen); + if (slosh != last_slosh) { + DBG("[SLOSH OFF]"); + } + switch (ch) { + case ',': + if (!IsIgnore() && (curly == xtern) && (start) && (func)) + /* must be multi proto */ + { + if (lastch == ')') /* eg int foo(), bar(); */ + { + ProcessBuffer(PH_MPROTO); + DBG("[END OF MULTIPROTOTYPE]") + } + } + break; + case ';': + if (!IsIgnore() && (curly == xtern) && (start)) { + if (func) { + if (lastch == ')') { + ProcessBuffer(PH_PROTO); + DBG("[END OF PROTOTYPE]") + } + } else { + ProcessBuffer(PH_VAR); + DBG("[END OF VARIABLE]") + } + } + break; + case 10: /* end of line */ + line++; + /* chars++; */ /* must have been a CR before it + methinks */ + col = 0; + if (double_slash) { + double_slash = false; + DBG("[DOUBLE_SLASH_COMMENT OFF]") + } else if (hash) { + if (hash_ext) { + hash_ext = false; + } else { + hash = false; + DBG("[HASH OFF]") + } + } + if (xtern && (strncmp(buffer, "extern", 6) == 0)) { + start = false; /* Not the start of anything */ + buff_idx = 0; /* Kill the buffer */ + } + break; -void -phChar(char ch) -{ - col++; - chars++; -/* printf("%d%% done\r", chars*100/filelen); /**/ + case '#': /* start of # something at beginning of line */ + if ((!IsIgnore()) && (curly == xtern)) { + hash = true; + DBG("[HASH ON]") + } + break; + case '{': + if (!IsIgnore()) { + char st[80]; - if (slosh != last_slosh) - { - DBG("[SLOSH OFF]"); - } + if ((curly == xtern) && (start) && (func)) { + ProcessBuffer(PH_FUNCTION); + DBG("[FUNCTION DECLARED]") + } - switch(ch) - { - case ',': - if (!IsIgnore() && (curly == xtern) && (start) && (func)) - /* must be multi proto */ - { - if (lastch == ')') /* eg int foo(), bar(); */ - { - ProcessBuffer(PH_MPROTO); - DBG("[END OF MULTIPROTOTYPE]") - } - } - break; + curly++; + sprintf(st, "[CURLY++ %d]", curly); + DBG(st) + } + break; - case ';': - if (!IsIgnore() && (curly == xtern) && (start)) - { - if (func) - { - if (lastch == ')') - { - ProcessBuffer(PH_PROTO); - DBG("[END OF PROTOTYPE]") - } - } - else - { - ProcessBuffer(PH_VAR); - DBG("[END OF VARIABLE]") - } - } - break; + case '}': + if (!IsIgnore()) { + char st[80]; - case 10: /* end of line */ - line++; -/* chars++; */ /* must have been a CR before it methinks */ - col = 0; - if (double_slash) - { - double_slash = FALSE; - DBG("[DOUBLE_SLASH_COMMENT OFF]") - } - else if (hash) - { - if (hash_ext) - { - hash_ext = FALSE; - } - else - { - hash = FALSE; - DBG("[HASH OFF]") - } - } - if (xtern && (strncmp(buffer, "extern", 6) == 0)) - { - start = FALSE; /* Not the start of anything */ - buff_idx = 0; /* Kill the buffer */ - } - break; + if (curly > 0) { + if (xtern && (xtern == curly)) { + xtern = 0; + DBG("[EXTERN OFF]"); + } + curly--; + sprintf(st, "[CURLY-- %d]", curly); + DBG(st) + } else { + /* match the {s */ + ERR("too many \"}\"\n"); + } + } + break; - case '#': /* start of # something at beginning of line */ - if ((!IsIgnore()) && (curly == xtern)) - { - hash = TRUE; - DBG("[HASH ON]") - } - break; + case '(': + if (!IsIgnore()) { + char st[80]; - case '{': - if (!IsIgnore()) - { - char st[80]; + if ((curly == xtern) && (round == 0) && (start)) { + func = true; + DBG("[FUNCTION]") + } + round++; + sprintf(st, "[ROUND++ %d]", round); + DBG(st) + } + break; - if ((curly == xtern) && (start) && (func)) - { - ProcessBuffer(PH_FUNCTION); - DBG("[FUNCTION DECLARED]") - } + case ')': + if (!IsIgnore()) { + char st[80]; - curly++; - sprintf(st, "[CURLY++ %d]", curly); - DBG(st) - } - break; + if (round > 0) { + round--; + sprintf(st, "[ROUND-- %d]", round); + DBG(st) + } else { + ERR("too many \")\"\n"); + } + } + break; - case '}': - if (!IsIgnore()) - { - char st[80]; + case '\\': + if (!slosh && (quote1 || quote2)) { + last_slosh = true; + DBG("[SLOSH ON]") + } else if (hash) { + hash_ext = true; + } + break; - if (curly > 0) - { - if (xtern && (xtern == curly)) - { - xtern = 0; - DBG("[EXTERN OFF]"); - } - curly--; - sprintf(st,"[CURLY-- %d]", curly); - DBG(st) - } - else - { - /* match the {s */ - ERR("too many \"}\"\n"); - } - } - break; + case '*': + if (lastch == '/') /* allow nested comments ! */ + { + char st[80]; - case '(': - if (!IsIgnore()) - { - char st[80]; + comment++; - if ((curly == xtern) && (round == 0) && (start)) - { - func = TRUE; - DBG("[FUNCTION]") - } - round++; - sprintf(st,"[ROUND++ %d]", round); - DBG(st) - } - break; + if (start) { + remFromBuffer(); + } - case ')': - if (!IsIgnore()) - { - char st[80]; + sprintf(st, "[COMMENT++ %d]", comment); + DBG(st) + } + break; - if (round > 0) - { - round--; - sprintf(st,"[ROUND-- %d]",round); - DBG(st) - } - else - { - ERR("too many \")\"\n"); - } - } - break; + case '/': + if ((lastch == '*') && (!quote1) && (!quote2)) { + if (comment > 0) { + char st[80]; - case '\\': - if (!slosh && (quote1 || quote2)) - { - last_slosh = TRUE; - DBG("[SLOSH ON]") - } - else - if (hash) - { - hash_ext = TRUE; - } - break; + comment--; - case '*': - if (lastch == '/') /* allow nested comments ! */ - { - char st[80]; + /* Don't want the closing slash in the buffer */ + ignore1 = true; - comment++; + sprintf(st, "[COMMENT-- %d]", comment); + DBG(st) + } else { + ERR("too many \"*/\"\n"); + } + } else if (lastch == '/') { + /* Double slash to end of line is a comment. */ + double_slash = true; - if (start) - { - remFromBuffer(); - } + if (start) { + remFromBuffer(); + } - sprintf(st, "[COMMENT++ %d]",comment); - DBG(st) + DBG("[DOUBLE_SLASH_COMMENT ON]") + } + break; + case '\"': + if ((!comment) && (!quote1) && (!slosh)) { + quote2 = (byte)(!quote2); + if (quote2) + DBG("[QUOTE2ON]") + if (!quote2) + DBG("[QUOTE2OFF]") - } - break; + /* We want to catch the extern "C" {} thing... */ + if (!quote2 && start && (lastch == 'C')) { + if (strcmp(buffer, "extern ") == 0) { + char st[80]; - case '/': - if ((lastch == '*') && (!quote1) && (!quote2) ) - { - if (comment > 0) - { - char st[80]; + xtern = curly + 1; /* The level inside the extern {} */ + sprintf(st, "[EXTERN ON %d]", xtern); + DBG(st) + } + } + } + break; - comment--; + case '\'': + if ((!comment) && (!quote2) && (!slosh)) { + { + quote1 = (byte)(!quote1); + if (quote1) + DBG("[QUOTE1ON]") + if (!quote1) + DBG("[QUOTE1OFF]") + } + } + break; - /* Don't want the closing slash in the buffer */ - ignore1 = TRUE; + case '\t': + ch = ' '; + break; - sprintf(st, "[COMMENT-- %d]",comment); - DBG(st) - } - else - { - ERR("too many \"*/\"\n"); - } - } - else - if (lastch == '/') - { - /* Double slash to end of line is a comment. */ - double_slash = TRUE; + default: + if ((ch != -1) && !IsIgnore() && (curly == xtern) && (!start) && + (ch != ' ')) { + start = true; + DBG("[START OF SOMETHING]") + } + break; + } - if (start) - { - remFromBuffer(); - } + if (ch != -1) { + if (start && !IsIgnore()) { + AddToBuffer(ch); + } + } - DBG("[DOUBLE_SLASH_COMMENT ON]") - } - break; - - case '\"': - if ((!comment) && (!quote1) && (!slosh)) - { - quote2 = (byte) (!quote2); - if (quote2) DBG("[QUOTE2ON]") - if (!quote2) DBG("[QUOTE2OFF]") - - /* We want to catch the extern "C" {} thing... */ - if (!quote2 && start && (lastch == 'C')) - { - if (strcmp(buffer, "extern ") == 0) - { - char st[80]; - - xtern = curly+1; /* The level inside the extern {} */ - sprintf(st, "[EXTERN ON %d]", xtern); - DBG(st) - } - } - } - break; - - case '\'': - if ((!comment) && (!quote2) && (!slosh)) - { - { - quote1 = (byte) (!quote1); - if (quote1) DBG("[QUOTE1ON]") - if (!quote1) DBG("[QUOTE1OFF]") - } - } - break; - - case '\t': - ch = ' '; - break; - - - default: - if ((ch != -1) && !IsIgnore() && (curly == xtern) && (!start) && - (ch != ' ')) - { - start = TRUE; - DBG("[START OF SOMETHING]") - } - break; - - - } - - if (ch != -1) - { - if (start && !IsIgnore()) - { - AddToBuffer(ch); - } - } - - lastch = ch; - slosh = last_slosh; - last_slosh = 0; - ignore1 = FALSE; - -} /* of phChar */ + lastch = ch; + slosh = last_slosh; + last_slosh = 0; + ignore1 = false; +} /* of phChar */ /* Take a lump of data from a header file, and churn the state machine through each char */ -boolT -phData(char *buff, int ndata) -{ - int i, j; +boolT phData(char *buff, int ndata) { + int i, j; #ifdef DEBUG - char cLine[81]; - char cfLine[90]; + char cLine[81]; + char cfLine[90]; #endif - if (ndata < 1) - { - ndata = strlen(buff); - } - j = 0; - - for (i=0; i < ndata; i++) - { - phChar(buff[i]); + if (ndata < 1) { + ndata = strlen(buff); + } + j = 0; + + for (i = 0; i < ndata; i++) { + phChar(buff[i]); #ifdef DEBUG - if (j < 80) cLine[j++] = buff[i]; - if (buff[i] == '\n') - { - cLine[j] = '\0'; - sprintf(cfLine, "\n***%03d %s\n", line, cLine); - DBG(cfLine); - j = 0; - } + if (j < 80) + cLine[j++] = buff[i]; + if (buff[i] == '\n') { + cLine[j] = '\0'; + sprintf(cfLine, "\n***%03d %s\n", line, cLine); + DBG(cfLine); + j = 0; + } #endif - } + } - return TRUE; + return true; } +boolT phPost(void) { + boolT err = true; + char msg[80]; -boolT -phPost(void) -{ - boolT err=TRUE; - char msg[80]; + if (quote1) { + WARN("EOF: \' not closed"); + err = false; + } - if (quote1) - { - WARN("EOF: \' not closed"); - err = FALSE; - } + if (quote2) { + WARN("EOF: \" not closed"); + err = false; + } - if (quote2) - { - WARN("EOF: \" not closed"); - err = FALSE; - } + if (comment) { + WARN("EOF: comment not closed"); + err = false; + } - if (comment) - { - WARN("EOF: comment not closed"); - err = FALSE; - } + if (slosh) { + WARN("EOF: internal slosh set error"); + err = false; + } - if (slosh) - { - WARN("EOF: internal slosh set error"); - err = FALSE; - } + if (curly > 0) { + sprintf(msg, "EOF: { level = %d", curly); + WARN(msg); + err = false; + } - if (curly > 0) - { - sprintf(msg,"EOF: { level = %d", curly); - WARN(msg); - err = FALSE; - } + if (round > 0) { + sprintf(msg, "EOF: ( level = %d", round); + WARN(msg); + err = false; + } - if (round > 0) - { - sprintf(msg,"EOF: ( level = %d", round); - WARN(msg); - err = FALSE; - } + if (hash) { + WARN("warning hash is set on last line ???"); + err = false; + } - if (hash) - { - WARN("warning hash is set on last line ???"); - err = FALSE; - } - - return err; + return err; } - - #if 0 enum FUNC_E { NORM , LEFT , RIGHT, NEXT }; @@ -624,25 +486,25 @@ NamesToLabel(LPPH_FUNC lpF) { int i, j; - for (i=0; i < lpF->num_params; i++) - { - while (isspace(types[i][0])) - { - lstrcdel(types[i], 0); - } + for (i=0; i < lpF->num_params; i++) + { + while (isspace(types[i][0])) + { + lstrcdel(types[i], 0); + } - j = 0; - while(names[i][j] != '\0') - { - if ((names[i][j] != '*') && - (names[i][j] != '[') && - (names[i][j] != ']')) - { - lstrccat(label[i], names[i][j]); - } - j++ ; - } - } + j = 0; + while(names[i][j] != '\0') + { + if ((names[i][j] != '*') && + (names[i][j] != '[') && + (names[i][j] != ']')) + { + lstrccat(label[i], names[i][j]); + } + j++ ; + } + } } boolT @@ -651,82 +513,82 @@ MoveLastWord(char *src, char *dest) /* take arg name from type */ int i, l; LPSTR s2; - if (s2 = strchr(src, '*')) /* if there is a * use it as starting point */ - { - lstrcpy(dest, s2); - *s2 = '\0'; /* terminate */ - } - else - { - l = strlen(src); - i = l-1; - while ((i > 0) && ((isspace(src[i])) || (src[i] == '[') || (src[i] == ']'))) - /* find last non space or non [] */ - { - if (isspace(src[i])) - { - lstrcdel(src, i); - } /* remove trailing spaces */ - i--; - } - while ((i > 0) && (!isspace(src[i]))) /* find the previous space */ - { - i--; - } - if (i) - { - i++; - lstrcpy(dest, &src[i]); - src[i] = '\0'; /* terminate */ - } - else - { - /* no type !!! */ + if (s2 = strchr(src, '*')) /* if there is a * use it as starting point */ + { + lstrcpy(dest, s2); + *s2 = '\0'; /* terminate */ + } + else + { + l = strlen(src); + i = l-1; + while ((i > 0) && ((isspace(src[i])) || (src[i] == '[') || (src[i] == ']'))) + /* find last non space or non [] */ + { + if (isspace(src[i])) + { + lstrcdel(src, i); + } /* remove trailing spaces */ + i--; + } + while ((i > 0) && (!isspace(src[i]))) /* find the previous space */ + { + i--; + } + if (i) + { + i++; + lstrcpy(dest, &src[i]); + src[i] = '\0'; /* terminate */ + } + else + { + /* no type !!! */ // if ((mode == TOANSI) || // ((mode == FROMANSI) && (strstr(src, "...") == NULL)) // ) - if (strstr(src, "...") == NULL) // not a var arg perhaps? - { - char msg[80]; + if (strstr(src, "...") == NULL) // not a var arg perhaps? + { + char msg[80]; // sprintf(msg,"no type for arg # %d, \"%s\"\n",lpF->num_params, (LPSTR) src); // ERR(msg); - return FALSE; - } - else // d'oh! - { - WARN("looks like a vararg to me!"); - /* maybe it is a ... !!!!!! */ - } - } - } + return false; + } + else // d'oh! + { + WARN("looks like a vararg to me!"); + /* maybe it is a ... !!!!!! */ + } + } + } - i = strlen(src) - 1; + i = strlen(src) - 1; - while ((isspace(src[i])) && (i >= 0)) - { - src[i--] = '\0'; - } + while ((isspace(src[i])) && (i >= 0)) + { + src[i--] = '\0'; + } - i = 0; - while (dest[i] != '\0') - { - if (isspace(dest[i])) - { - lstrcdel(dest, i); - } - i++ ; - } + i = 0; + while (dest[i] != '\0') + { + if (isspace(dest[i])) + { + lstrcdel(dest, i); + } + i++ ; + } - while ((isspace(src[0])) && (src[0] != '\0')) - { - lstrcdel(src, 0); - } + while ((isspace(src[0])) && (src[0] != '\0')) + { + lstrcdel(src, 0); + } - return TRUE; + return true; } @@ -737,44 +599,44 @@ Lookup(char *aname) /* lookup var name in labels and return arg number */ { int i, p; char tname[NAMES_L]; -int bstate = FALSE; +int bstate = false; /* eg: lookup *fred[] for match to fred */ - tname[0] = '\0'; - p = -1; /* default return for no match */ + tname[0] = '\0'; + p = -1; /* default return for no match */ - /* tname is aname without puncs etc */ - i = 0; - while(aname[i] != '\0') - { - if (aname[i] == '[') - { - bstate = TRUE; - } - else - if (aname[i] == ']') - { - bstate = FALSE; - } + /* tname is aname without puncs etc */ + i = 0; + while(aname[i] != '\0') + { + if (aname[i] == '[') + { + bstate = true; + } + else + if (aname[i] == ']') + { + bstate = false; + } - if ((isalnum(aname[i]) || aname[i] == '-' || aname[i] == '_') && (!bstate)) - { - lstrccat(tname, aname[i]); - } - i++; - } + if ((isalnum(aname[i]) || aname[i] == '-' || aname[i] == '_') && (!bstate)) + { + lstrccat(tname, aname[i]); + } + i++; + } - /* lookup tname in the labels and find out which arg it is */ - for (i=0; i < num_params; i++) - { - if (lstrcmp(tname, label[i]) == 0) - { - p = i; /* this one ! */ - break; - } - } - return p; + /* lookup tname in the labels and find out which arg it is */ + for (i=0; i < num_params; i++) + { + if (lstrcmp(tname, label[i]) == 0) + { + p = i; /* this one ! */ + break; + } + } + return p; } @@ -787,505 +649,415 @@ Plop(char *atype, char *aname, int FAR *num_params, int FAR *num_found) char msg[80]; int t; - if (num_found >= num_params) - { - sprintf(msg,"extra argument \"%s\" in func \"%s\"\n", (LPSTR) aname, (LPSTR) func_name); - ERR(msg); - return FALSE; - } + if (num_found >= num_params) + { + sprintf(msg,"extra argument \"%s\" in func \"%s\"\n", (LPSTR) aname, (LPSTR) func_name); + ERR(msg); + return false; + } - t = Lookup(aname); /* arg number */ + t = Lookup(aname); /* arg number */ - if (t == -1) /* couldn't find it */ - { - sprintf(msg,"bad argument \"%s\" in func \"%s\"\n", (LPSTR) aname, (LPSTR) func_name); - ERR(msg); - return FALSE; - } + if (t == -1) /* couldn't find it */ + { + sprintf(msg,"bad argument \"%s\" in func \"%s\"\n", (LPSTR) aname, (LPSTR) func_name); + ERR(msg); + return false; + } - if ((strlen(types[t]) > 0) || (strlen(names[t]) > 0)) /* in use ? */ - { - - sprintf(msg,"argument \"%s\" already used in \"%s\"\n", (LPSTR) aname, (LPSTR) func_name); - ERR(msg); - return FALSE; - } + if ((strlen(types[t]) > 0) || (strlen(names[t]) > 0)) /* in use ? */ + { - lstrcpy(types[t], atype); - lstrcpy(names[t], aname); + sprintf(msg,"argument \"%s\" already used in \"%s\"\n", (LPSTR) aname, (LPSTR) func_name); + ERR(msg); + return false; + } - num_found++; /* got another! */ + lstrcpy(types[t], atype); + lstrcpy(names[t], aname); - return TRUE; + num_found++; /* got another! */ + + return true; } -#define IGN ((*chp == '(') || (*chp == ')') || (*chp == ',') || \ - (*chp == ';') || (isspace(*chp))) +#define IGN \ + ((*chp == '(') || (*chp == ')') || (*chp == ',') || (*chp == ';') || \ + (isspace(*chp))) -#define IGNP ((*chp == '(') || (*chp == ')') || (*chp == ',') || \ - (*chp == ';') || (*chp == '\n')) +#define IGNP \ + ((*chp == '(') || (*chp == ')') || (*chp == ',') || (*chp == ';') || \ + (*chp == '\n')) #endif +char token[40]; /* Strings that might be types, nodifiers or idents */ +char ident[40]; /* Names of functions or protos go here */ +char lastChar; +char *p; +int indirect; +boolT isLong, isShort, isUnsigned; +int lastTokPos; /* For "^" in error messages */ +char *buffP; +int tok; /* Current token */ +baseType bt; /* Type of current param (or return type) */ +int argNum; /* Arg number (in case no name: arg1, arg2...) */ - - - - - -char token[40]; /* Strings that might be types, nodifiers or idents */ -char ident[40]; /* Names of functions or protos go here */ -char lastChar; -char *p; -int indirect; -boolT isLong, isShort, isUnsigned; -int lastTokPos; /* For "^" in error messages */ -char *buffP; -int tok; /* Current token */ -baseType bt; /* Type of current param (or return type) */ -int argNum; /* Arg number (in case no name: arg1, arg2...) */ - - -void -initType(void) -{ - indirect = 0; - isLong = isShort = isUnsigned = FALSE; - bt = BT_INT; +void initType(void) { + indirect = 0; + isLong = isShort = isUnsigned = false; + bt = BT_INT; } -void -errorParse(char *msg) -{ - printf("%s: got ", msg); - if (tok == TOK_NAME) printf("<%s>", token); - else if (tok == TOK_DOTS) printf("..."); - else printf("%c (%X)", tok, tok); - printf("\n%s\n", buffP); - printf("%*c\n", lastTokPos+1, '^'); +void errorParse(char *msg) { + printf("%s: got ", msg); + if (tok == TOK_NAME) + printf("<%s>", token); + else if (tok == TOK_DOTS) + printf("..."); + else + printf("%c (%X)", tok, tok); + printf("\n%s\n", buffP); + printf("%*c\n", lastTokPos + 1, '^'); } - /* Get a token from pointer p */ -int -getToken(void) -{ - char ch; +int getToken(void) { + char ch; - memset(token, 0, sizeof(token)); - while (*p && ((*p == ' ') || (*p == '\n'))) p++; - lastTokPos = p - buffP; /* For error messages */ - if (lastChar) - { - ch = lastChar; - lastChar = '\0'; + memset(token, 0, sizeof(token)); + while (*p && ((*p == ' ') || (*p == '\n'))) + p++; + lastTokPos = p - buffP; /* For error messages */ + if (lastChar) { + ch = lastChar; + lastChar = '\0'; + return ch; + } + + while (ch = *p++) { + switch (ch) { + case '*': + case '[': + case ']': + case '(': + case ')': + case ',': + case ';': + case ' ': + case '\n': + if (strlen(token)) { + if ((ch != ' ') && (ch != '\n')) + lastChar = ch; + return TOK_NAME; + } else if ((ch == ' ') || (ch == '\n')) + break; + else return ch; + + case '.': + if ((*p == '.') && (p[1] == '.')) { + p += 2; + return TOK_DOTS; + } + + default: + token[strlen(token)] = ch; } - - while (ch = *p++) - { - switch (ch) - { - case '*': - case '[': - case ']': - case '(': - case ')': - case ',': - case ';': - case ' ': - case '\n': - if (strlen(token)) - { - if ((ch != ' ') && (ch != '\n')) lastChar = ch; - return TOK_NAME; - } - else if ((ch == ' ') || (ch == '\n')) break; - else return ch; - - case '.': - if ((*p == '.') && (p[1] == '.')) - { - p += 2; - return TOK_DOTS; - } - - - default: - token[strlen(token)] = ch; - } - } - return TOK_EOL; + } + return TOK_EOL; } -boolT -isBaseType(void) -{ - if (tok != TOK_NAME) return FALSE; +boolT isBaseType(void) { + if (tok != TOK_NAME) + return false; - if (strcmp(token, "int") == 0) - { - bt = BT_INT; - } - else if (strcmp(token, "char") == 0) - { - bt = BT_CHAR; - } - else if (strcmp(token, "void") == 0) - { - bt = BT_VOID; - } - else if (strcmp(token, "float") == 0) - { - bt = BT_FLOAT; - } - else if (strcmp(token, "double") == 0) - { - bt = BT_DOUBLE; - } - else if (strcmp(token, "struct") == 0) - { - bt = BT_STRUCT; - tok = getToken(); /* The name of the struct */ - /* Do something with the struct name */ - } - else if (strcmp(token, "union") == 0) - { - bt = BT_STRUCT; /* Well its still a struct */ - tok = getToken(); /* The name of the union */ - /* Do something with the union name */ - } - else if (strcmp(token, "FILE") == 0) - { - bt = BT_STRUCT; - } - else if (strcmp(token, "size_t") == 0) - { - bt = BT_INT; - isUnsigned = TRUE; - } - else if (strcmp(token, "va_list") == 0) - { - bt = BT_VOID; - indirect = 1; /* va_list is a void* */ - } - else return FALSE; - return TRUE; + if (strcmp(token, "int") == 0) { + bt = BT_INT; + } else if (strcmp(token, "char") == 0) { + bt = BT_CHAR; + } else if (strcmp(token, "void") == 0) { + bt = BT_VOID; + } else if (strcmp(token, "float") == 0) { + bt = BT_FLOAT; + } else if (strcmp(token, "double") == 0) { + bt = BT_DOUBLE; + } else if (strcmp(token, "struct") == 0) { + bt = BT_STRUCT; + tok = getToken(); /* The name of the struct */ + /* Do something with the struct name */ + } else if (strcmp(token, "union") == 0) { + bt = BT_STRUCT; /* Well its still a struct */ + tok = getToken(); /* The name of the union */ + /* Do something with the union name */ + } else if (strcmp(token, "FILE") == 0) { + bt = BT_STRUCT; + } else if (strcmp(token, "size_t") == 0) { + bt = BT_INT; + isUnsigned = true; + } else if (strcmp(token, "va_list") == 0) { + bt = BT_VOID; + indirect = 1; /* va_list is a void* */ + } else + return false; + return true; } -boolT -isModifier(void) -{ - if (tok != TOK_NAME) return FALSE; - if (strcmp(token, "long") == 0) - { - isLong = TRUE; - } - else if (strcmp(token, "unsigned") == 0) - { - isUnsigned = TRUE; - } - else if (strcmp(token, "short") == 0) - { - isShort = TRUE; - } - else if (strcmp(token, "const") == 0) - { +boolT isModifier(void) { + if (tok != TOK_NAME) + return false; + if (strcmp(token, "long") == 0) { + isLong = true; + } else if (strcmp(token, "unsigned") == 0) { + isUnsigned = true; + } else if (strcmp(token, "short") == 0) { + isShort = true; + } else if (strcmp(token, "const") == 0) { - } - else if (strcmp(token, "_far") == 0) - { + } else if (strcmp(token, "_far") == 0) { - } - else return FALSE; - return TRUE; + } else + return false; + return true; } -boolT -isAttrib(void) -{ +boolT isAttrib(void) { - if (tok != TOK_NAME) return FALSE; - if (strcmp(token, "far") == 0) - { - /* Not implemented yet */ - } - else if (strcmp(token, "__far") == 0) - { - /* Not implemented yet */ - } - else if (strcmp(token, "__interrupt") == 0) - { - /* Not implemented yet */ - } - else return FALSE; - return TRUE; + if (tok != TOK_NAME) + return false; + if (strcmp(token, "far") == 0) { + /* Not implemented yet */ + } else if (strcmp(token, "__far") == 0) { + /* Not implemented yet */ + } else if (strcmp(token, "__interrupt") == 0) { + /* Not implemented yet */ + } else + return false; + return true; } - -boolT -isCdecl(void) -{ - return ( - (strcmp(token, "__cdecl") == 0) || - (strcmp(token, "_Cdecl") == 0) || - (strcmp(token, "cdecl") == 0)); +boolT isCdecl(void) { + return ((strcmp(token, "__cdecl") == 0) || (strcmp(token, "_Cdecl") == 0) || + (strcmp(token, "cdecl") == 0)); } -void -getTypeAndIdent(void) -{ - /* Get a type and ident pair. Complicated by the fact that types are - actually optional modifiers followed by types, and the identifier - is also optional. For example: - myfunc(unsigned footype *foovar); - declares an arg named foovar, of type unsigned pointer to footype. - But we don't exand typedefs and #defines, so the footype may not - be recognised as a type. Then it is not possible to know whether - footype is an identifier or an unknown type until the next token is - read (in this case, the star). If it is a comma or paren, then footype - is actually an identifier. (This function gets called for the function - return type and name as well, so "(" is possible as well as ")" and - ","). - The identifier is copied to ident. - */ +void getTypeAndIdent(void) { + /* Get a type and ident pair. Complicated by the fact that types are + actually optional modifiers followed by types, and the identifier + is also optional. For example: + myfunc(unsigned footype *foovar); + declares an arg named foovar, of type unsigned pointer to footype. + But we don't exand typedefs and #defines, so the footype may not + be recognised as a type. Then it is not possible to know whether + footype is an identifier or an unknown type until the next token is + read (in this case, the star). If it is a comma or paren, then footype + is actually an identifier. (This function gets called for the function + return type and name as well, so "(" is possible as well as ")" and + ","). + The identifier is copied to ident. + */ - boolT im = FALSE, ib; + boolT im = false, ib; - while (isModifier()) - { - tok = getToken(); - im = TRUE; - } + while (isModifier()) { + tok = getToken(); + im = true; + } - if (!im && (tok != TOK_NAME)) - { - errorParse("Expected type"); - } + if (!im && (tok != TOK_NAME)) { + errorParse("Expected type"); + } - ib = isBaseType(); - if (ib) tok = getToken(); + ib = isBaseType(); + if (ib) + tok = getToken(); - /* Could be modifiers like "far", "interrupt" etc */ - while (isAttrib()) - { - tok = getToken(); - } + /* Could be modifiers like "far", "interrupt" etc */ + while (isAttrib()) { + tok = getToken(); + } - while (tok == '*') - { - indirect++; - tok = getToken(); + while (tok == '*') { + indirect++; + tok = getToken(); + } + + /* Ignore the cdecl's */ + while (isCdecl()) + tok = getToken(); + + if (tok == TOK_NAME) { + /* This could be an ident or an unknown type */ + strcpy(ident, token); + tok = getToken(); + } + + if (!ib && (tok != ',') && (tok != '(') && (tok != ')')) { + /* That was (probably) not an ident! Assume it was an unknown type */ + printf("Unknown type %s\n", ident); + ident[0] = '\0'; + bt = BT_UNKWN; + + while (tok == '*') { + indirect++; + tok = getToken(); } /* Ignore the cdecl's */ - while (isCdecl()) tok = getToken(); + while (isCdecl()) + tok = getToken(); + } - if (tok == TOK_NAME) - { - /* This could be an ident or an unknown type */ - strcpy(ident, token); - tok = getToken(); - } - - if (!ib && (tok != ',') && (tok != '(') && (tok != ')')) - { - /* That was (probably) not an ident! Assume it was an unknown type */ -printf("Unknown type %s\n", ident); - ident[0] = '\0'; - bt = BT_UNKWN; - - while (tok == '*') - { - indirect++; - tok = getToken(); - } - - /* Ignore the cdecl's */ - while (isCdecl()) tok = getToken(); - } - - if (tok == TOK_NAME) - { - /* This has to be the ident */ - strcpy(ident, token); - tok = getToken(); - } - - while (tok == '[') - { - indirect++; /* Treat x[] like *x */ - do - { - tok = getToken(); /* Ignore stuff between the '[' and ']' */ - } - while (tok != ']'); - tok = getToken(); - } + if (tok == TOK_NAME) { + /* This has to be the ident */ + strcpy(ident, token); + tok = getToken(); + } + while (tok == '[') { + indirect++; /* Treat x[] like *x */ + do { + tok = getToken(); /* Ignore stuff between the '[' and ']' */ + } while (tok != ']'); + tok = getToken(); + } } +hlType convType(void) { + /* Convert from base type and signed/unsigned flags, etc, to a htType + as Cristina currently uses */ -hlType -convType(void) -{ - /* Convert from base type and signed/unsigned flags, etc, to a htType - as Cristina currently uses */ - - if (indirect >= 1) - { - if (bt == BT_CHAR) return TYPE_STR; /* Assume char* is ptr */ - /* Pointer to anything else (even unknown) is type pointer */ - else return TYPE_PTR; + if (indirect >= 1) { + if (bt == BT_CHAR) + return TYPE_STR; /* Assume char* is ptr */ + /* Pointer to anything else (even unknown) is type pointer */ + else + return TYPE_PTR; + } + switch (bt) { + case BT_INT: + if (isLong) { + if (isUnsigned) + return TYPE_LONG_UNSIGN; + else + return TYPE_LONG_SIGN; + } else { + if (isUnsigned) + return TYPE_WORD_UNSIGN; + else + return TYPE_WORD_SIGN; } - switch (bt) - { - case BT_INT: - if (isLong) - { - if (isUnsigned) return TYPE_LONG_UNSIGN; - else return TYPE_LONG_SIGN; - } - else - { - if (isUnsigned) return TYPE_WORD_UNSIGN; - else return TYPE_WORD_SIGN; - } - case BT_CHAR: - if (isUnsigned) return TYPE_BYTE_UNSIGN; - else return TYPE_BYTE_SIGN; + case BT_CHAR: + if (isUnsigned) + return TYPE_BYTE_UNSIGN; + else + return TYPE_BYTE_SIGN; - case BT_FLOAT: - return TYPE_FLOAT; - case BT_DOUBLE: - return TYPE_DOUBLE; + case BT_FLOAT: + return TYPE_FLOAT; + case BT_DOUBLE: + return TYPE_DOUBLE; - case BT_STRUCT: - return TYPE_RECORD; + case BT_STRUCT: + return TYPE_RECORD; - case BT_VOID: - default: - return TYPE_UNKNOWN; - } + case BT_VOID: + default: + return TYPE_UNKNOWN; + } } - /* Add a new function to the array of function name and return types. The array is logically sorted by a linked list. Note that numArg is filled in later */ -boolT -addNewFunc(char *name, hlType typ) -{ - int i, prev, res; +boolT addNewFunc(char *name, hlType typ) { + int i, prev, res; - /* First see if the name already exists */ - prev = NIL; - for (i=headFunc; i != NIL; i = pFunc[i].next) - { - res = strcmp(pFunc[i].name, name); - if (res > 0) - { - break; /* Exit this loop when just past insert point */ - } - if (res == 0) - { - /* Already have this function name */ - return TRUE; - } - prev = i; + /* First see if the name already exists */ + prev = NIL; + for (i = headFunc; i != NIL; i = pFunc[i].next) { + res = strcmp(pFunc[i].name, name); + if (res > 0) { + break; /* Exit this loop when just past insert point */ } + if (res == 0) { + /* Already have this function name */ + return true; + } + prev = i; + } - if (numFunc >= allocFunc) - { - allocFunc += DELTA_FUNC; - pFunc = realloc(pFunc, allocFunc * sizeof(PH_FUNC_STRUCT)); - if (pFunc == NULL) - { - fprintf(stderr, "Could not allocate %ud bytes for function array\n", - allocFunc * sizeof(PH_FUNC_STRUCT)); - exit(1); - } - memset(&pFunc[allocFunc - DELTA_FUNC], 0, - DELTA_FUNC * sizeof(PH_FUNC_STRUCT)); + if (numFunc >= allocFunc) { + allocFunc += DELTA_FUNC; + pFunc = (PH_FUNC_STRUCT *)realloc(pFunc, allocFunc * sizeof(PH_FUNC_STRUCT)); + if (pFunc == NULL) { + fprintf(stderr, "Could not allocate %ud bytes for function array\n", + allocFunc * sizeof(PH_FUNC_STRUCT)); + exit(1); } + memset(&pFunc[allocFunc - DELTA_FUNC], 0, + DELTA_FUNC * sizeof(PH_FUNC_STRUCT)); + } - name[SYMLEN-1] = '\0'; - strcpy(pFunc[numFunc].name, name); - pFunc[numFunc].typ = typ; - pFunc[numFunc].firstArg = numArg; - if (prev == NIL) - { - pFunc[numFunc].next = headFunc; - headFunc = numFunc; - } - else - { - pFunc[numFunc].next = pFunc[prev].next; - pFunc[prev].next = numFunc; - } - numFunc++; + name[SYMLEN - 1] = '\0'; + strcpy(pFunc[numFunc].name, name); + pFunc[numFunc].typ = typ; + pFunc[numFunc].firstArg = numArg; + if (prev == NIL) { + pFunc[numFunc].next = headFunc; + headFunc = numFunc; + } else { + pFunc[numFunc].next = pFunc[prev].next; + pFunc[prev].next = numFunc; + } + numFunc++; - return FALSE; + return false; } /* Add a new arguement to the array of arguement name and types. The array is logically sorted by a linked list */ -void -addNewArg(char *name, hlType typ) -{ - if (numArg >= allocArg) - { - allocArg += DELTA_FUNC; - pArg = realloc(pArg, allocArg * sizeof(PH_ARG_STRUCT)); - if (pArg == NULL) - { - fprintf(stderr, "Could not allocate %ud bytes for arguement array\n", - allocArg * sizeof(PH_ARG_STRUCT)); - exit(1); - } - memset(&pArg[allocArg - DELTA_FUNC], 0, - DELTA_FUNC * sizeof(PH_ARG_STRUCT)); +void addNewArg(char *name, hlType typ) { + if (numArg >= allocArg) { + allocArg += DELTA_FUNC; + pArg = (PH_ARG_STRUCT *)realloc(pArg, allocArg * sizeof(PH_ARG_STRUCT)); + if (pArg == NULL) { + fprintf(stderr, "Could not allocate %ud bytes for arguement array\n", + allocArg * sizeof(PH_ARG_STRUCT)); + exit(1); } - name[SYMLEN-1] = '\0'; - strcpy(pArg[numArg].name, name); - pArg[numArg].typ = typ; - numArg++; - + memset(&pArg[allocArg - DELTA_FUNC], 0, DELTA_FUNC * sizeof(PH_ARG_STRUCT)); + } + name[SYMLEN - 1] = '\0'; + strcpy(pArg[numArg].name, name); + pArg[numArg].typ = typ; + numArg++; } -void -parseParam(void) -{ - initType(); - if (tok == TOK_DOTS) - { - tok = getToken(); - pFunc[numFunc-1].bVararg = TRUE; - return; - } +void parseParam(void) { + initType(); + if (tok == TOK_DOTS) { + tok = getToken(); + pFunc[numFunc - 1].bVararg = true; + return; + } - getTypeAndIdent(); - - if ((bt == BT_VOID) && (indirect == 0)) - { - /* Just a void arg list. Ignore and pFunc[].numArgs will be set to zero */ - return; - } - argNum++; - if (ident[0]) - { - addNewArg(ident, convType()); - } - else - { - sprintf(ident, "arg%d", argNum); - addNewArg(ident, convType()); - } + getTypeAndIdent(); + if ((bt == BT_VOID) && (indirect == 0)) { + /* Just a void arg list. Ignore and pFunc[].numArgs will be set to zero */ + return; + } + argNum++; + if (ident[0]) { + addNewArg(ident, convType()); + } else { + sprintf(ident, "arg%d", argNum); + addNewArg(ident, convType()); + } } - /* Parse the prototype as follows: [] ["*"]... "(" [","]...")" where is @@ -1295,244 +1067,208 @@ and where paramdef is Note that the closing semicolon is not seen. */ +void phBuffToFunc(char *buff) { -void -phBuffToFunc(char *buff) -{ + initType(); + p = buffP = buff; + tok = getToken(); - initType(); - p = buffP = buff; - tok = getToken(); + /* Ignore typedefs, for now */ + if ((tok == TOK_NAME) && (strcmp(token, "typedef") == 0)) + return; - /* Ignore typedefs, for now */ - if ((tok == TOK_NAME) && (strcmp(token, "typedef") == 0)) return; + getTypeAndIdent(); - getTypeAndIdent(); + if (ident[0] == '\0') { + errorParse("Expected function name"); + return; + } - if (ident[0] == '\0') - { - errorParse("Expected function name"); - return; - } + if (addNewFunc(ident, convType())) { + /* Already have this prototype, so ignore it */ + return; + } - if (addNewFunc(ident, convType())) - { - /* Already have this prototype, so ignore it */ - return; - } + if (tok != '(') { + errorParse("Expected '('"); + return; + } + tok = getToken(); - if (tok != '(') - { - errorParse("Expected '('"); - return; + argNum = 0; + while (tok != TOK_EOL) { + parseParam(); + if ((tok != ',') && (tok != ')')) { + errorParse("Expected ',' between parameter defs"); + return; } tok = getToken(); - - argNum = 0; - while (tok != TOK_EOL) - { - parseParam(); - if ((tok != ',') && (tok != ')')) - { - errorParse("Expected ',' between parameter defs"); - return; - } - tok = getToken(); - } - pFunc[numFunc-1].numArg = argNum; /* Number of args this func */ + } + pFunc[numFunc - 1].numArg = argNum; /* Number of args this func */ } +void phBuffToDef(char *buff) {} -void -phBuffToDef(char *buff) -{ - +void writeFile(char *buffer, int len) { + if ((int)fwrite(buffer, 1, len, datFile) != len) { + printf("Could not write to file\n"); + exit(1); + } } +void writeFileShort(word w) { + byte b; -void -writeFile(char *buffer, int len) -{ - if ((int)fwrite(buffer, 1, len, datFile) != len) - { - printf("Could not write to file\n"); - exit(1); - } + b = (byte)(w & 0xFF); + writeFile((char *)&b, 1); /* Write a short little endian */ + b = (byte)(w >> 8); + writeFile((char *)&b, 1); } +void saveFile(void) { + int i; -void -writeFileShort(word w) -{ - byte b; + fprintf(datFile, "dccp"); /* Signature */ + fprintf(datFile, "FN"); /* Function name tag */ + writeFileShort(numFunc); /* Number of func name records */ + for (i = headFunc; i != NIL; i = pFunc[i].next) { + writeFile(pFunc[i].name, SYMLEN); + writeFileShort((word)pFunc[i].typ); + writeFileShort((word)pFunc[i].numArg); + writeFileShort((word)pFunc[i].firstArg); + writeFile((char *)&pFunc[i].bVararg, 1); + } - b = (byte)(w & 0xFF); - writeFile(&b, 1); /* Write a short little endian */ - b = (byte)(w>>8); - writeFile(&b, 1); + fprintf(datFile, "PM"); /* Parameter Name tag */ + writeFileShort(numArg); /* Number of args */ + for (i = 0; i < numArg; i++) { + // writeFile(pArg[i].name, SYMLEN); // Don't want names yet + writeFileShort((word)pArg[i].typ); + } } +int main(int argc, char *argv[]) { + char *buf; + long fSize; + int ndata; + FILE *f, *fl; + int i; + char *p; -void -saveFile(void) -{ - int i; + if (argc != 2) { + printf("Usage: parsehdr \n" + "where is a file of header file names to parse.\n" + "The file dcclibs.dat will be written\n"); + exit(1); + } - fprintf(datFile, "dccp"); /* Signature */ - fprintf(datFile, "FN"); /* Function name tag */ - writeFileShort(numFunc); /* Number of func name records */ - for (i=headFunc; i != NIL; i = pFunc[i].next) - { - writeFile(pFunc[i].name, SYMLEN); - writeFileShort((word)pFunc[i].typ); - writeFileShort((word)pFunc[i].numArg); - writeFileShort((word)pFunc[i].firstArg); - writeFile((char *)&pFunc[i].bVararg, 1); - } + fl = fopen(argv[1], "rt"); + if (fl == NULL) { + printf("Could not open file list file %s\n", argv[1]); + exit(1); + } - fprintf(datFile, "PM"); /* Parameter Name tag */ - writeFileShort(numArg); /* Number of args */ - for (i=0; i < numArg; i++) - { -/* writeFile(pArg[i].name, SYMLEN); /* Don't want names yet */ - writeFileShort((word)pArg[i].typ); - } + datFile = fopen("dcclibs.dat", "wb"); + if (datFile == NULL) { + printf("Could not open output file dcclibs.dat\n"); + exit(2); + } -} - -void -main(int argc, char *argv[]) -{ - char *buf; - long fSize; - int ndata; - FILE *f, *fl; - int i; - char *p; - - if (argc != 2) - { - printf("Usage: parsehdr \n" - "where is a file of header file names to parse.\n" - "The file dcclibs.dat will be written\n"); - exit(1); - } - - fl = fopen(argv[1], "rt"); - if (fl == NULL) - { - printf("Could not open file list file %s\n", argv[1]); - exit(1); - } - - datFile = fopen("dcclibs.dat", "wb"); - if (datFile == NULL) - { - printf("Could not open output file dcclibs.dat\n"); - exit(2); - } - - /* Allocate the arrys for function and proto names and types */ - pFunc = malloc(DELTA_FUNC * sizeof(PH_FUNC_STRUCT)); - if (pFunc == 0) - { - fprintf(stderr, "Could not malloc %ud bytes for function name array\n", + /* Allocate the arrys for function and proto names and types */ + pFunc = (PH_FUNC_STRUCT *)malloc(DELTA_FUNC * sizeof(PH_FUNC_STRUCT)); + if (pFunc == 0) { + fprintf(stderr, "Could not malloc %ud bytes for function name array\n", DELTA_FUNC * sizeof(PH_FUNC_STRUCT)); - exit(1); - } - memset(pFunc, 0, DELTA_FUNC * sizeof(PH_FUNC_STRUCT)); - allocFunc = DELTA_FUNC; - numFunc = 0; + exit(1); + } + memset(pFunc, 0, DELTA_FUNC * sizeof(PH_FUNC_STRUCT)); + allocFunc = DELTA_FUNC; + numFunc = 0; - pArg = malloc(DELTA_FUNC * sizeof(PH_ARG_STRUCT)); - if (pArg == 0) - { - fprintf(stderr, "Could not malloc %ud bytes for arguement array\n", + pArg = (PH_ARG_STRUCT *)malloc(DELTA_FUNC * sizeof(PH_ARG_STRUCT)); + if (pArg == 0) { + fprintf(stderr, "Could not malloc %ud bytes for arguement array\n", DELTA_FUNC * sizeof(PH_ARG_STRUCT)); - exit(1); + exit(1); + } + memset(pArg, 0, DELTA_FUNC * sizeof(PH_ARG_STRUCT)); + allocArg = DELTA_FUNC; + numArg = 0; + + headFunc = headArg = NIL; + + buf = NULL; + while (!feof(fl)) { + /* Get another filename from the file list */ + p = fgets(fileName, 80, fl); + if (p == NULL) + break; /* Otherwise read last filename twice */ + i = strlen(fileName) - 1; + if (fileName[i] == '\n') + fileName[i] = '\0'; + f = fopen(fileName, "rt"); + if (f == NULL) { + printf("Could not open header file %s\n", fileName); + exit(1); } - memset(pArg, 0, DELTA_FUNC * sizeof(PH_ARG_STRUCT)); - allocArg = DELTA_FUNC; - numArg = 0; - headFunc = headArg = NIL; + printf("Processing %s...\n", fileName); - buf = NULL; - while (!feof(fl)) - { - /* Get another filename from the file list */ - p = fgets(fileName, 80, fl); - if (p == NULL) break; /* Otherwise read last filename twice */ - i = strlen(fileName)-1; - if (fileName[i] == '\n') fileName[i] = '\0'; - f = fopen(fileName, "rt"); - if (f == NULL) - { - printf("Could not open header file %s\n", fileName); - exit(1); - } - - printf("Processing %s...\n", fileName); - - fSize = _lseek(_fileno(f), 0, SEEK_END); - fseek(f, 0, SEEK_SET); - ndata = (int) min(fSize, FBUF_SIZE); - if (buf) free(buf); - buf = (char *)malloc(ndata); - if (buf == 0) - { - printf("Could not malloc input file buffer of %d bytes\n", ndata); - exit(1); - } - - while (!feof(f)) - { - ndata = fread(buf, 1, ndata, f); - phData(buf, ndata); - } - phPost(); - fclose(f); + fseek(f, 0, SEEK_END); + fSize = ftell(f); + fseek(f, 0, SEEK_SET); + ndata = std::min(fSize, FBUF_SIZE); + if (buf) + free(buf); + buf = (char *)malloc(ndata); + if (buf == 0) { + printf("Could not malloc input file buffer of %d bytes\n", ndata); + exit(1); } - saveFile(); - fclose(datFile); - fclose(fl); - free(buf); - free(pFunc); - free(pArg); + while (!feof(f)) { + ndata = fread(buf, 1, ndata, f); + phData(buf, ndata); + } + phPost(); + fclose(f); + } + saveFile(); + fclose(datFile); + fclose(fl); + + free(buf); + free(pFunc); + free(pArg); } - #if CHECK_HEAP -void -checkHeap(char *msg) +void checkHeap(char *msg) /* HEAPCHK.C: This program checks the heap for * consistency and prints an appropriate message. */ { - int heapstatus; + int heapstatus; - printf("%s\n", msg); + printf("%s\n", msg); - /* Check heap status */ - heapstatus = _heapchk(); - switch( heapstatus ) - { - case _HEAPOK: - printf(" OK - heap is fine\n" ); - break; - case _HEAPEMPTY: - printf(" OK - heap is empty\n" ); - break; - case _HEAPBADBEGIN: - printf( "ERROR - bad start of heap\n" ); - break; - case _HEAPBADNODE: - printf( "ERROR - bad node in heap\n" ); - break; - } + /* Check heap status */ + heapstatus = _heapchk(); + switch (heapstatus) { + case _HEAPOK: + printf(" OK - heap is fine\n"); + break; + case _HEAPEMPTY: + printf(" OK - heap is empty\n"); + break; + case _HEAPBADBEGIN: + printf("ERROR - bad start of heap\n"); + break; + case _HEAPBADNODE: + printf("ERROR - bad node in heap\n"); + break; + } } #endif - diff --git a/tools/parsehdr/parsehdr.h b/tools/parsehdr/parsehdr.h index 4baf048..e25de9b 100644 --- a/tools/parsehdr/parsehdr.h +++ b/tools/parsehdr/parsehdr.h @@ -1,18 +1,17 @@ +#pragma once +#include "Enums.h" /* *$Log: parsehdr.h,v $ */ /* Header file for parsehdr.c */ typedef unsigned long dword; /* 32 bits */ -typedef unsigned char byte; /* 8 bits */ +typedef unsigned char byte; /* 8 bits */ typedef unsigned short word; /* 16 bits */ typedef unsigned char boolT; /* 8 bits */ -#define TRUE 1 -#define FALSE 0 - #define BUFF_SIZE 8192 /* Holds a declaration */ -#define FBUF_SIZE 32700 /* Holds part of a header file */ +#define FBUF_SIZE 32700 /* Holds part of a header file */ #define NARGS 15 #define NAMES_L 160 @@ -38,11 +37,8 @@ void phWarning(char *errmsg); #define SYMLEN 16 /* Including the null */ #define Int long /* For locident.h */ -#define int16 short int /* For locident.h */ -#include "locident.h" /* For the hlType enum */ -#define bool unsigned char /* For internal use */ -#define TRUE 1 -#define FALSE 0 +//#include "locident.h" /* For the hlType enum */ +//#define bool unsigned char /* For internal use */ typedef struct ph_func_tag