* SOFTWARE.
*/
#include <time.h>
+#include <sys/stat.h>
#include "gmqcc.h"
#include "lexer.h"
bool output_on;
ppcondition *conditions;
/*ppmacro **macros;*/
- ht macros; /* hashtable<string, ppmacro*> */
-
+ ht macros; /* hashtable<string, ppmacro*> */
char *output_string;
char *itemname;
(void)context;
return value;
}
+/* __TIMESTAMP__ */
+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("\"<failed to determine timestamp>\"");
+
+ /*
+ * 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;
+}
-const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = {
+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 },
{ "__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)
/* predef stuff */
char *expand = NULL;
- size_t i;
ftepp->lex->flags.preprocessing = true;
ftepp->lex->flags.mergelines = false;
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;
}
}
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;
}