X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=blobdiff_plain;f=ftepp.c;h=d68eed45285baa72e2fed256faa9314596a0311a;hp=9d0fed9c0e931373779e638a66457c78a2b733bc;hb=8db9724c5d4189ec55b0c827b9633e9257243582;hpb=685fa54daf0b3fdd92d9424e7fcab1ded9fb3c29 diff --git a/ftepp.c b/ftepp.c index 9d0fed9..d68eed4 100644 --- a/ftepp.c +++ b/ftepp.c @@ -22,6 +22,10 @@ * SOFTWARE. */ #include +#include +#include +#include + #include "gmqcc.h" #include "lexer.h" @@ -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,8 +84,8 @@ static uint32_t ftepp_predef_countval = 0; static uint32_t ftepp_predef_randval = 0; /* __DATE__ */ -char *ftepp_predef_date(lex_file *context) { - struct tm *itime; +static char *ftepp_predef_date(lex_file *context) { + struct tm *itime = NULL; time_t rtime; char *value = (char*)mem_a(82); /* 82 is enough for strftime but we also have " " in our string */ @@ -91,7 +94,12 @@ char *ftepp_predef_date(lex_file *context) { /* get time */ time (&rtime); + +#ifdef _MSC_VER + localtime_s(itime, &rtime); +#else itime = localtime(&rtime); +#endif strftime(value, 82, "\"%b %d %Y\"", itime); @@ -99,8 +107,8 @@ char *ftepp_predef_date(lex_file *context) { } /* __TIME__ */ -char *ftepp_predef_time(lex_file *context) { - struct tm *itime; +static char *ftepp_predef_time(lex_file *context) { + struct tm *itime = NULL; time_t rtime; char *value = (char*)mem_a(82); /* 82 is enough for strftime but we also have " " in our string */ @@ -109,7 +117,12 @@ char *ftepp_predef_time(lex_file *context) { /* get time */ time (&rtime); + +#ifdef _MSC_VER + localtime_s(itime, &rtime); +#else itime = localtime(&rtime); +#endif strftime(value, 82, "\"%X\"", itime); @@ -117,21 +130,21 @@ 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); - snprintf(value, length, "\"%s\"", context->name); + util_snprintf(value, length, "\"%s\"", context->name); 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); @@ -139,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); @@ -148,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); @@ -157,15 +170,42 @@ 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; + if (stat(context->name, &finfo)) + return util_strdup("\"\""); + + /* + * ctime and its fucking annoying newline char, no worries, we're + * professionals here. + */ + find = ctime(&finfo.st_mtime); + 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; -const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = { +static const ftepp_predef_t ftepp_predefs[] = { { "__LINE__", &ftepp_predef_line }, { "__FILE__", &ftepp_predef_file }, { "__COUNTER__", &ftepp_predef_counter }, @@ -173,9 +213,29 @@ 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 int ftepp_predef_index(const char *name) { + /* no hashtable here, we simply check for one to exist the naive way */ + int i; + for(i = 0; i < (int)(sizeof(ftepp_predefs)/sizeof(*ftepp_predefs)); i++) + if (!strcmp(ftepp_predefs[i].name, name)) + return i; + return -1; +} + +bool ftepp_predef_exists(const char *name) { + return ftepp_predef_index(name) != -1; +} + +/* singleton because we're allowed */ +static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) { + int i = ftepp_predef_index(name); + return (i != -1) ? ftepp_predefs[i].func : NULL; +} + #define ftepp_tokval(f) ((f)->lex->tok.value) #define ftepp_ctx(f) ((f)->lex->tok.ctx) @@ -256,7 +316,7 @@ static void ppmacro_delete(ppmacro *self) mem_d(self); } -static ftepp_t* ftepp_new() +static ftepp_t* ftepp_new(void) { ftepp_t *ftepp; @@ -312,12 +372,12 @@ static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp) static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name) { - return util_htget(ftepp->macros, name); + return (ppmacro*)util_htget(ftepp->macros, 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) @@ -507,10 +567,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 { @@ -568,7 +624,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; } } @@ -581,16 +637,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; @@ -832,7 +882,7 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param if (resetline && !ftepp->in_macro) { char lineno[128]; - snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline)); + util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline)); ftepp_out(ftepp, lineno, false); } @@ -1432,7 +1482,7 @@ static bool ftepp_include(ftepp_t *ftepp) ftepp_out(ftepp, "\n#pragma file(", false); ftepp_out(ftepp, ctx.file, false); - snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1)); + util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1)); ftepp_out(ftepp, lineno, false); /* skip the line */ @@ -1618,7 +1668,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; @@ -1639,15 +1688,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; } } @@ -1797,12 +1845,12 @@ ftepp_t *ftepp_create() minor[2] = '"'; } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) { ftepp_add_define(ftepp, NULL, "__STD_GMQCC__"); - snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR); - snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR); + util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR); + util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR); } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) { ftepp_add_define(ftepp, NULL, "__STD_QCCX__"); - snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR); - snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR); + util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR); + util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR); } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) { ftepp_add_define(ftepp, NULL, "__STD_QCC__"); /* 1.0 */ @@ -1818,13 +1866,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 ctx = { "__builtin__", 0, 0 }; ctx.file = source; macro = ppmacro_new(ctx, name); /*vec_push(ftepp->macros, macro);*/