X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ftepp.c;h=5f414f358bb868df23815d875d0579ad9067ac41;hp=b07899a327e936ba45771ccb46c9f01b3940cc0b;hb=46fa12cb265706673927319d07651096e10c5b80;hpb=dc8523c6503e014a463df42520b48f597c7655be diff --git a/ftepp.c b/ftepp.c old mode 100755 new mode 100644 index b07899a..5f414f3 --- a/ftepp.c +++ b/ftepp.c @@ -22,6 +22,10 @@ * SOFTWARE. */ #include +#include +#include +#include + #include "gmqcc.h" #include "lexer.h" @@ -37,7 +41,7 @@ typedef struct { char *value; /* a copy from the lexer */ union { - vector v; + vec3_t v; int i; double f; int t; /* type */ @@ -45,7 +49,7 @@ typedef struct { } pptoken; typedef struct { - lex_ctx ctx; + lex_ctx_t ctx; char *name; char **params; @@ -64,8 +68,7 @@ typedef struct ftepp_s { bool output_on; ppcondition *conditions; /*ppmacro **macros;*/ - ht macros; /* hashtable */ - + ht macros; /* hashtable */ char *output_string; char *itemname; @@ -81,7 +84,7 @@ static uint32_t ftepp_predef_countval = 0; static uint32_t ftepp_predef_randval = 0; /* __DATE__ */ -char *ftepp_predef_date(lex_file *context) { +static char *ftepp_predef_date(lex_file *context) { struct tm *itime = NULL; time_t rtime; char *value = (char*)mem_a(82); @@ -104,7 +107,7 @@ char *ftepp_predef_date(lex_file *context) { } /* __TIME__ */ -char *ftepp_predef_time(lex_file *context) { +static char *ftepp_predef_time(lex_file *context) { struct tm *itime = NULL; time_t rtime; char *value = (char*)mem_a(82); @@ -127,13 +130,13 @@ char *ftepp_predef_time(lex_file *context) { } /* __LINE__ */ -char *ftepp_predef_line(lex_file *context) { +static char *ftepp_predef_line(lex_file *context) { char *value; util_asprintf(&value, "%d", (int)context->line); return value; } /* __FILE__ */ -char *ftepp_predef_file(lex_file *context) { +static char *ftepp_predef_file(lex_file *context) { size_t length = strlen(context->name) + 3; /* two quotes and a terminator */ char *value = (char*)mem_a(length); util_snprintf(value, length, "\"%s\"", context->name); @@ -141,7 +144,7 @@ char *ftepp_predef_file(lex_file *context) { return value; } /* __COUNTER_LAST__ */ -char *ftepp_predef_counterlast(lex_file *context) { +static char *ftepp_predef_counterlast(lex_file *context) { char *value; util_asprintf(&value, "%u", ftepp_predef_countval); @@ -149,7 +152,7 @@ char *ftepp_predef_counterlast(lex_file *context) { return value; } /* __COUNTER__ */ -char *ftepp_predef_counter(lex_file *context) { +static char *ftepp_predef_counter(lex_file *context) { char *value; ftepp_predef_countval ++; util_asprintf(&value, "%u", ftepp_predef_countval); @@ -158,7 +161,7 @@ char *ftepp_predef_counter(lex_file *context) { return value; } /* __RANDOM__ */ -char *ftepp_predef_random(lex_file *context) { +static char *ftepp_predef_random(lex_file *context) { char *value; ftepp_predef_randval = (util_rand() % 0xFF) + 1; util_asprintf(&value, "%u", ftepp_predef_randval); @@ -167,15 +170,52 @@ char *ftepp_predef_random(lex_file *context) { return value; } /* __RANDOM_LAST__ */ -char *ftepp_predef_randomlast(lex_file *context) { +static char *ftepp_predef_randomlast(lex_file *context) { char *value; util_asprintf(&value, "%u", ftepp_predef_randval); (void)context; return value; } +/* __TIMESTAMP__ */ +static char *ftepp_predef_timestamp(lex_file *context) { + struct stat finfo; + char *find; + char *value; + size_t size; +#ifdef _MSC_VER + char buffer[64]; +#endif + if (stat(context->name, &finfo)) + return util_strdup("\"\""); + + /* + * ctime and its fucking annoying newline char, no worries, we're + * professionals here. + */ + +#ifndef _MSC_VER + find = ctime(&finfo.st_mtime); +#else + ctime_s(buffer, sizeof(buffer), &finfo.st_mtime); + find = buffer; +#endif -const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = { + value = (char*)mem_a(strlen(find) + 1); + memcpy(&value[1], find, (size = strlen(find)) - 1); + + value[0] = '"'; + value[size] = '"'; + + return value; +} + +typedef struct { + const char *name; + char *(*func)(lex_file *); +} ftepp_predef_t; + +static const ftepp_predef_t ftepp_predefs[] = { { "__LINE__", &ftepp_predef_line }, { "__FILE__", &ftepp_predef_file }, { "__COUNTER__", &ftepp_predef_counter }, @@ -183,20 +223,41 @@ const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = { { "__RANDOM__", &ftepp_predef_random }, { "__RANDOM_LAST__", &ftepp_predef_randomlast }, { "__DATE__", &ftepp_predef_date }, - { "__TIME__", &ftepp_predef_time } + { "__TIME__", &ftepp_predef_time }, + { "__TIME_STAMP__", &ftepp_predef_timestamp } }; +static GMQCC_INLINE size_t ftepp_predef_index(const char *name) { + /* no hashtable here, we simply check for one to exist the naive way */ + size_t i; + for(i = 1; i < GMQCC_ARRAY_COUNT(ftepp_predefs) + 1; i++) + if (!strcmp(ftepp_predefs[i-1].name, name)) + return i; + return 0; +} + +bool ftepp_predef_exists(const char *name); +bool ftepp_predef_exists(const char *name) { + return ftepp_predef_index(name) != 0; +} + +/* singleton because we're allowed */ +static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) { + size_t i = ftepp_predef_index(name); + return (i != 0) ? ftepp_predefs[i-1].func : NULL; +} + #define ftepp_tokval(f) ((f)->lex->tok.value) #define ftepp_ctx(f) ((f)->lex->tok.ctx) -static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...) +static void ftepp_errorat(ftepp_t *ftepp, lex_ctx_t ctx, const char *fmt, ...) { va_list ap; ftepp->errors++; va_start(ap, fmt); - con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap); + con_cvprintmsg(ctx, LVL_ERROR, "error", fmt, ap); va_end(ap); } @@ -207,7 +268,7 @@ static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...) ftepp->errors++; va_start(ap, fmt); - con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap); + con_cvprintmsg(ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap); va_end(ap); } @@ -243,7 +304,7 @@ static GMQCC_INLINE void pptoken_delete(pptoken *self) mem_d(self); } -static ppmacro *ppmacro_new(lex_ctx ctx, const char *name) +static ppmacro *ppmacro_new(lex_ctx_t ctx, const char *name) { ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro)); @@ -266,7 +327,7 @@ static void ppmacro_delete(ppmacro *self) mem_d(self); } -static ftepp_t* ftepp_new() +static ftepp_t* ftepp_new(void) { ftepp_t *ftepp; @@ -327,7 +388,7 @@ static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name) static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name) { - util_htrm(ftepp->macros, name, NULL); + util_htrm(ftepp->macros, name, (void (*)(void*))&ppmacro_delete); } static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp) @@ -517,10 +578,6 @@ static bool ftepp_define(ftepp_t *ftepp) return false; } -#if 0 - if (ftepp->output_on) - vec_push(ftepp->macros, macro); -#endif if (ftepp->output_on) util_htset(ftepp->macros, macro->name, (void*)macro); else { @@ -578,7 +635,7 @@ static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params) ptok = pptoken_make(ftepp); vec_push(mp.tokens, ptok); if (ftepp_next(ftepp) >= TOKEN_EOF) { - ftepp_error(ftepp, "unexpected EOF in macro call"); + ftepp_error(ftepp, "unexpected end of file in macro call"); goto on_error; } } @@ -591,16 +648,10 @@ static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params) goto on_error; } if (ftepp_next(ftepp) >= TOKEN_EOF) { - ftepp_error(ftepp, "unexpected EOF in macro call"); + ftepp_error(ftepp, "unexpected end of file in macro call"); goto on_error; } } - /* need to leave that up - if (ftepp_next(ftepp) >= TOKEN_EOF) { - ftepp_error(ftepp, "unexpected EOF in macro call"); - goto on_error; - } - */ *out_params = params; return true; @@ -1300,10 +1351,10 @@ static bool ftepp_directive_warning(ftepp_t *ftepp) { /* handle the odd non string constant case so it works like C */ if (ftepp->token != TOKEN_STRINGCONST) { bool store = false; - vec_upload(message, "#warning", 8); + vec_append(message, 8, "#warning"); ftepp_next(ftepp); while (ftepp->token != TOKEN_EOL) { - vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp))); + vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp)); ftepp_next(ftepp); } vec_push(message, '\0'); @@ -1330,10 +1381,10 @@ static void ftepp_directive_error(ftepp_t *ftepp) { /* handle the odd non string constant case so it works like C */ if (ftepp->token != TOKEN_STRINGCONST) { - vec_upload(message, "#error", 6); + vec_append(message, 6, "#error"); ftepp_next(ftepp); while (ftepp->token != TOKEN_EOL) { - vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp))); + vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp)); ftepp_next(ftepp); } vec_push(message, '\0'); @@ -1358,15 +1409,15 @@ static void ftepp_directive_message(ftepp_t *ftepp) { /* handle the odd non string constant case so it works like C */ if (ftepp->token != TOKEN_STRINGCONST) { - vec_upload(message, "#message", 8); + vec_append(message, 8, "#message"); ftepp_next(ftepp); while (ftepp->token != TOKEN_EOL) { - vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp))); + vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp)); ftepp_next(ftepp); } vec_push(message, '\0'); if (ftepp->output_on) - con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message); + con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", message); vec_free(message); return; } @@ -1375,7 +1426,7 @@ static void ftepp_directive_message(ftepp_t *ftepp) { return; unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp)); - con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp)); + con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp)); } /** @@ -1387,7 +1438,7 @@ static bool ftepp_include(ftepp_t *ftepp) { lex_file *old_lexer = ftepp->lex; lex_file *inlex; - lex_ctx ctx; + lex_ctx_t ctx; char lineno[128]; char *filename; char *old_includename; @@ -1477,7 +1528,7 @@ static bool ftepp_hash(ftepp_t *ftepp) ppcondition cond; ppcondition *pc; - lex_ctx ctx = ftepp_ctx(ftepp); + lex_ctx_t ctx = ftepp_ctx(ftepp); if (!ftepp_skipspace(ftepp)) return false; @@ -1628,7 +1679,6 @@ static bool ftepp_preprocess(ftepp_t *ftepp) /* predef stuff */ char *expand = NULL; - size_t i; ftepp->lex->flags.preprocessing = true; ftepp->lex->flags.mergelines = false; @@ -1649,15 +1699,14 @@ static bool ftepp_preprocess(ftepp_t *ftepp) case TOKEN_TYPENAME: /* is it a predef? */ if (OPTS_FLAG(FTEPP_PREDEFS)) { - for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) { - if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) { - expand = ftepp_predefs[i].func(ftepp->lex); - ftepp_out(ftepp, expand, false); - ftepp_next(ftepp); /* skip */ - - mem_d(expand); /* free memory */ - break; - } + char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp)); + if (predef) { + expand = predef(ftepp->lex); + ftepp_out (ftepp, expand, false); + ftepp_next(ftepp); + + mem_d(expand); + break; } } @@ -1770,10 +1819,10 @@ void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) { return; } - vec_upload(create, "#define ", 8); - vec_upload(create, name, strlen(name)); + vec_append(create, 8, "#define "); + vec_append(create, strlen(name), name); vec_push (create, ' '); - vec_upload(create, value, strlen(value)); + vec_append(create, strlen(value), value); vec_push (create, 0); ftepp_preprocess_string(ftepp, "__builtin__", create); @@ -1828,13 +1877,19 @@ ftepp_t *ftepp_create() ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor); ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major); + /* + * We're going to just make __NULL__ nil, which works for 60% of the + * cases of __NULL_ for fteqcc. + */ + ftepp_add_macro(ftepp, "__NULL__", "nil"); + return ftepp; } void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name) { ppmacro *macro; - lex_ctx ctx = { "__builtin__", 0 }; + lex_ctx_t ctx = { "__builtin__", 0, 0 }; ctx.file = source; macro = ppmacro_new(ctx, name); /*vec_push(ftepp->macros, macro);*/