mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-03-20 00:30:59 +03:00
fixed for new libusefull_macros
This commit is contained in:
@@ -5,3 +5,16 @@ another files used for test: use script ./run for it
|
|||||||
|
|
||||||
Compile: gcc -lusefull_macro file.c -o file
|
Compile: gcc -lusefull_macro file.c -o file
|
||||||
gcc -lusefull_macro test.c hash.c -o test
|
gcc -lusefull_macro test.c hash.c -o test
|
||||||
|
|
||||||
|
|
||||||
|
file helpcmds.in should be included into proto.c as help list:
|
||||||
|
|
||||||
|
const char *helpstring =
|
||||||
|
"https://github.com/eddyem/stm32samples/tree/master/F3:F303/Multistepper build#" BUILD_NUMBER " @ " BUILD_DATE "\n"
|
||||||
|
"Format: cmd [N] - getter, cmd [N] = val - setter; N - optional index\n"
|
||||||
|
"* - command not supported yet, G - getter, S - setter\n\n"
|
||||||
|
#include "hashgen/helpcmds.in"
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
helpcmds.in format: each string should be in quotes with '\n' @ its end. First word - command, next text - help.
|
||||||
|
|||||||
@@ -31,23 +31,23 @@ typedef struct{
|
|||||||
|
|
||||||
static glob_pars G = {.headerfile = "hash.h", .sourcefile = "hash.c"};
|
static glob_pars G = {.headerfile = "hash.h", .sourcefile = "hash.c"};
|
||||||
static int help = 0;
|
static int help = 0;
|
||||||
static myoption cmdlnopts[] = {
|
static sl_option_t cmdlnopts[] = {
|
||||||
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), "show this help"},
|
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), "show this help"},
|
||||||
{"dict", NEED_ARG, NULL, 'd', arg_string, APTR(&G.dict), "dictionary file"},
|
{"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"},
|
{"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"},
|
{"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
|
end_option
|
||||||
};
|
};
|
||||||
static void parse_args(int argc, char **argv){
|
static void parse_args(int argc, char **argv){
|
||||||
parseargs(&argc, &argv, cmdlnopts);
|
sl_parseargs(&argc, &argv, cmdlnopts);
|
||||||
if(help) showhelp(-1, cmdlnopts);
|
if(help) sl_showhelp(-1, cmdlnopts);
|
||||||
if(argc > 0){
|
if(argc > 0){
|
||||||
red("Unused arguments:\n");
|
red("Unused arguments:\n");
|
||||||
for(int i = 0; i < argc; ++i)
|
for(int i = 0; i < argc; ++i)
|
||||||
printf("%s ", argv[i]);
|
printf("%s ", argv[i]);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
showhelp(-1, cmdlnopts);
|
sl_showhelp(-1, cmdlnopts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,16 +170,13 @@ static char *fnname(const char *cmd){
|
|||||||
|
|
||||||
static const char *fhdr =
|
static const char *fhdr =
|
||||||
"int parsecmd(const char *str){\n\
|
"int parsecmd(const char *str){\n\
|
||||||
char cmd[CMD_MAXLEN + 1];\n\
|
|
||||||
if(!str || !*str) return RET_CMDNOTFOUND;\n\
|
if(!str || !*str) return RET_CMDNOTFOUND;\n\
|
||||||
int i = 0;\n\
|
int i = 0;\n\
|
||||||
while(*str > '@' && i < CMD_MAXLEN){ cmd[i++] = *str++; }\n\
|
while(*str > '@' && i < CMD_MAXLEN){ lastcmd[i++] = *str++; }\n\
|
||||||
cmd[i] = 0;\n\
|
lastcmd[i] = 0;\n\
|
||||||
if(*str){\n\
|
while(*str && *str <= ' ') ++str;\n\
|
||||||
while(*str <= ' ') ++str;\n\
|
|
||||||
}\n\
|
|
||||||
char *args = (char*) str;\n\
|
char *args = (char*) str;\n\
|
||||||
uint32_t h = hashf(cmd);\n\
|
uint32_t h = hashf(lastcmd);\n\
|
||||||
switch(h){\n\n"
|
switch(h){\n\n"
|
||||||
;
|
;
|
||||||
static const char *ffooter =
|
static const char *ffooter =
|
||||||
@@ -189,30 +186,40 @@ static const char *ffooter =
|
|||||||
}\n\n"
|
}\n\n"
|
||||||
;
|
;
|
||||||
static const char *fns =
|
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\
|
#define _U_ __attribute__((__unused__))\n\
|
||||||
#endif\n\n\
|
#endif\n\n\
|
||||||
#define CMD_MAXLEN (32)\n\n\
|
#define CMD_MAXLEN (32)\n\n\
|
||||||
enum{\n\
|
enum{\n\
|
||||||
|
RET_HELP = -3,\n\
|
||||||
RET_CMDNOTFOUND = -2,\n\
|
RET_CMDNOTFOUND = -2,\n\
|
||||||
RET_WRONGCMD = -1,\n\
|
RET_WRONGCMD = -1,\n\
|
||||||
RET_BAD = 0,\n\
|
RET_GOOD = 0,\n\
|
||||||
RET_GOOD = 1\n\
|
RET_BAD = 1\n\
|
||||||
};\n\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 =
|
static const char *sw =
|
||||||
" case CMD_%s:\n\
|
" case CMD_%s:\n\
|
||||||
return fn_%s(h, args);\n\
|
return fn_%s(h, args);\n\
|
||||||
break;\n";
|
break;\n";
|
||||||
static const char *srchdr =
|
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 <stddef.h>\n\
|
||||||
#include \"%s\"\n\n\
|
#include \"%s\"\n\n\
|
||||||
#ifndef WAL\n\
|
#ifndef WAL\n\
|
||||||
#define WAL __attribute__ ((weak, alias (\"__f1\")))\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){
|
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);
|
fprintf(source, "%s", ffooter);
|
||||||
fclose(source);
|
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);
|
fclose(header);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv){
|
int main(int argc, char **argv){
|
||||||
initial_setup();
|
sl_init();
|
||||||
parse_args(argc, argv);
|
parse_args(argc, argv);
|
||||||
if(!G.dict) ERRX("point dictionary file");
|
if(!G.dict) ERRX("point dictionary file");
|
||||||
if(!G.headerfile) ERRX("point header source file");
|
if(!G.headerfile) ERRX("point header source file");
|
||||||
if(!G.sourcefile) ERRX("point c 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);
|
if(!b) ERRX("Can't open %s", G.dict);
|
||||||
char *word = b->data;
|
char *word = b->data;
|
||||||
strhash *H = MALLOC(strhash, ALLOCSZ);
|
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");
|
if(hno == HASHFNO) WARNX("Can't find proper hash function");
|
||||||
FREE(H);
|
FREE(H);
|
||||||
My_munmap(b);
|
sl_munmap(b);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user