mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-03-20 08:41:02 +03:00
fixed for new libusefull_macros
This commit is contained in:
@@ -31,23 +31,23 @@ typedef struct{
|
||||
|
||||
static glob_pars G = {.headerfile = "hash.h", .sourcefile = "hash.c"};
|
||||
static int help = 0;
|
||||
static myoption cmdlnopts[] = {
|
||||
static sl_option_t cmdlnopts[] = {
|
||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), "show this help"},
|
||||
{"dict", NEED_ARG, NULL, 'd', arg_string, APTR(&G.dict), "dictionary file"},
|
||||
{"header", NEED_ARG, NULL, 'H', arg_string, APTR(&G.headerfile),"output header filename"},
|
||||
{"source", NEED_ARG, NULL, 'S', arg_string, APTR(&G.sourcefile),"output source filename"},
|
||||
{"genfunc", NO_ARGS, NULL, 'F', arg_int, APTR(&G.genfunc), "generate function bodys"},
|
||||
{"genfunc", NO_ARGS, NULL, 'F', arg_int, APTR(&G.genfunc), "generate function bodies"},
|
||||
end_option
|
||||
};
|
||||
static void parse_args(int argc, char **argv){
|
||||
parseargs(&argc, &argv, cmdlnopts);
|
||||
if(help) showhelp(-1, cmdlnopts);
|
||||
sl_parseargs(&argc, &argv, cmdlnopts);
|
||||
if(help) sl_showhelp(-1, cmdlnopts);
|
||||
if(argc > 0){
|
||||
red("Unused arguments:\n");
|
||||
for(int i = 0; i < argc; ++i)
|
||||
printf("%s ", argv[i]);
|
||||
printf("\n");
|
||||
showhelp(-1, cmdlnopts);
|
||||
sl_showhelp(-1, cmdlnopts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,16 +170,13 @@ static char *fnname(const char *cmd){
|
||||
|
||||
static const char *fhdr =
|
||||
"int parsecmd(const char *str){\n\
|
||||
char cmd[CMD_MAXLEN + 1];\n\
|
||||
if(!str || !*str) return RET_CMDNOTFOUND;\n\
|
||||
int i = 0;\n\
|
||||
while(*str > '@' && i < CMD_MAXLEN){ cmd[i++] = *str++; }\n\
|
||||
cmd[i] = 0;\n\
|
||||
if(*str){\n\
|
||||
while(*str <= ' ') ++str;\n\
|
||||
}\n\
|
||||
while(*str > '@' && i < CMD_MAXLEN){ lastcmd[i++] = *str++; }\n\
|
||||
lastcmd[i] = 0;\n\
|
||||
while(*str && *str <= ' ') ++str;\n\
|
||||
char *args = (char*) str;\n\
|
||||
uint32_t h = hashf(cmd);\n\
|
||||
uint32_t h = hashf(lastcmd);\n\
|
||||
switch(h){\n\n"
|
||||
;
|
||||
static const char *ffooter =
|
||||
@@ -189,30 +186,40 @@ static const char *ffooter =
|
||||
}\n\n"
|
||||
;
|
||||
static const char *fns =
|
||||
"int fn_%s(_U_ uint32_t hash, _U_ char *args) WAL; // \"%s\" (%u)\n\n"
|
||||
"int fn_%s(uint32_t _U_ hash, char _U_ *args) WAL; // \"%s\" (%u)\n\n"
|
||||
;
|
||||
static const char *headercontent = "#ifndef _U_\n\
|
||||
static const char *headercontent =
|
||||
"// Generated by HASHGEN (https://github.com/eddyem/eddys_snippets/tree/master/stringHash4MCU_)\n\
|
||||
// Licensed by GPLv3\n\
|
||||
#pragma once\n\
|
||||
#ifndef _U_\n\
|
||||
#define _U_ __attribute__((__unused__))\n\
|
||||
#endif\n\n\
|
||||
#define CMD_MAXLEN (32)\n\n\
|
||||
enum{\n\
|
||||
RET_HELP = -3,\n\
|
||||
RET_CMDNOTFOUND = -2,\n\
|
||||
RET_WRONGCMD = -1,\n\
|
||||
RET_BAD = 0,\n\
|
||||
RET_GOOD = 1\n\
|
||||
RET_GOOD = 0,\n\
|
||||
RET_BAD = 1\n\
|
||||
};\n\n\
|
||||
int parsecmd(const char *cmdwargs);\n\n";
|
||||
int parsecmd(const char *cmdwargs);\n\n\
|
||||
extern char lastcmd[];\n\n";
|
||||
|
||||
static const char *sw =
|
||||
" case CMD_%s:\n\
|
||||
return fn_%s(h, args);\n\
|
||||
break;\n";
|
||||
static const char *srchdr =
|
||||
"#include <stdint.h>\n\
|
||||
"// Generated by HASHGEN (https://github.com/eddyem/eddys_snippets/tree/master/stringHash4MCU_)\n\
|
||||
// Licensed by GPLv3\n\
|
||||
#include <stdint.h>\n\
|
||||
#include <stddef.h>\n\
|
||||
#include \"%s\"\n\n\
|
||||
#ifndef WAL\n\
|
||||
#define WAL __attribute__ ((weak, alias (\"__f1\")))\n\
|
||||
#endif\n\nstatic int __f1(_U_ uint32_t h, _U_ char *a){return 1;}\n\n"
|
||||
#endif\n\nstatic int __f1(uint32_t _U_ h, char _U_ *a){return 1;}\n\n\
|
||||
char lastcmd[CMD_MAXLEN + 1];\n\n"
|
||||
;
|
||||
|
||||
static void build(strhash *H, int hno, int hlen){
|
||||
@@ -245,16 +252,22 @@ static void build(strhash *H, int hno, int hlen){
|
||||
}
|
||||
fprintf(source, "%s", ffooter);
|
||||
fclose(source);
|
||||
fprintf(header, "\n\n");
|
||||
for(int i = 0; i < hlen; ++i){
|
||||
char *m = macroname(H[i].str);
|
||||
fprintf(header, "#define STR_%-*s \"%s\"\n", lmax, m, H[i].str);
|
||||
}
|
||||
|
||||
fclose(header);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
initial_setup();
|
||||
sl_init();
|
||||
parse_args(argc, argv);
|
||||
if(!G.dict) ERRX("point dictionary file");
|
||||
if(!G.headerfile) ERRX("point header source file");
|
||||
if(!G.sourcefile) ERRX("point c source file");
|
||||
mmapbuf *b = My_mmap(G.dict);
|
||||
sl_mmapbuf_t *b = sl_mmap(G.dict);
|
||||
if(!b) ERRX("Can't open %s", G.dict);
|
||||
char *word = b->data;
|
||||
strhash *H = MALLOC(strhash, ALLOCSZ);
|
||||
@@ -310,6 +323,6 @@ int main(int argc, char **argv){
|
||||
}
|
||||
if(hno == HASHFNO) WARNX("Can't find proper hash function");
|
||||
FREE(H);
|
||||
My_munmap(b);
|
||||
sl_munmap(b);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user