Обновление "хешегенератора"

Dec 15, 2022 09:30

Хоть true inline - и хорошо, но больно уж неудобно пользоваться и есть шанс, что сгенерировав код, затрешь свои изменения. Поэтому я решил функции делать "слабыми синонимами": __attribute__ ((weak, alias ("__f1"))) на функцию __f1, которая просто возвращает 1. Функцию parsecmd упростил (теперь она не выкидывает пробелы, чтобы разработчик мог сложные команды типа "set ip" или "set param" отправить, вычленять команду из строки ввода - задача разработчика).
Заодно добавил проверку на совпадение имен функций для разных команд (скажем, "sim-key", "sim_key", "sim key" и "sim'key" дадут одну и ту же функцию fn_sim_key). В таком случае утилита выдаст матюк:

Have two similar function names for 'sim key' and 'sim-key': 'fn_sim_key'
Have two similar function names for 'sim-key' and 'sim'key': 'fn_sim_key'
Have two similar function names for 'sim'key' and 'sim_key': 'fn_sim_key'
Can't generate code when names of some functions matches
И благополучно отвалится.

Код на гитхабе. Исходник хешегенератора:

/*
* Copyright 2022 Edward V. Emelianov .
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/

#include
#include
#include
#include

#define ALLOCSZ (5000)

typedef struct{
char *dict;
char *headerfile;
char *sourcefile;
int genfunc;
} glob_pars;

static glob_pars G = {.headerfile = "hash.h", .sourcefile = "hash.c"};
static int help = 0;
static myoption 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"},
end_option
};
static void parse_args(int argc, char **argv){
parseargs(&argc, &argv, cmdlnopts);
if(help) 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);
}
}

#define HASHFNO (3)
// djb2 & sdbm: http://www.cse.yorku.ca/~oz/hash.html
static uint32_t djb2(const char *str){
uint32_t hash = 5381;
uint32_t c;
while((c = (uint32_t)*str++))
hash = ((hash << 7) + hash) + c;
//hash = hash * 31 + c;
//hash = hash * 33 + c;
return hash;
}
static uint32_t sdbm(const char *str){
uint32_t hash = 5381;
uint32_t c;
while((c = (uint32_t)*str++))
hash = c + (hash << 6) + (hash << 16) - hash;
return hash;
}
// jenkins: https://en.wikipedia.org/wiki/Jenkins_hash_function
static uint32_t jenkins(const char *str){
uint32_t hash = 0, c;
while((c = (uint32_t)*str++)){
hash += c;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}

static const char *hashsources[HASHFNO] = {
"static uint32_t hashf(const char *str){\n\
uint32_t hash = 5381;\n\
uint32_t c;\n\
while((c = (uint32_t)*str++))\n\
hash = ((hash << 7) + hash) + c;\n\
return hash;\n\
}\n",
"static uint32_t hashf(const char *str){\n\
uint32_t hash = 5381;\n\
uint32_t c;\n\
while((c = (uint32_t)*str++))\n\
hash = c + (hash << 6) + (hash << 16) - hash;\n\
return hash;\n\
}\n",
"static uint32_t hashf(const char *str){\n\
uint32_t hash = 0, c;\n\
while((c = (uint32_t)*str++)){\n\
hash += c;\n\
hash += (hash << 10);\n\
hash ^= (hash >> 6);\n\
}\n\
hash += (hash << 3);\n\
hash ^= (hash >> 11);\n\
hash += (hash << 15);\n\
return hash;\n\
}\n"
};

static uint32_t (*hash[HASHFNO])(const char *str) = {djb2, sdbm, jenkins};
static const char *hashnames[HASHFNO] = {"DJB2", "SDBM", "Jenkins"};

typedef struct{
char str[32]; // string command
char fname[32]; // function namee
uint32_t hash; // command hash
} strhash;

static int sorthashesH(const void *a, const void *b){ // sort by hash
register uint32_t h1 = ((strhash*)a)->hash, h2 = ((strhash*)b)->hash;
if(h1 > h2) return 1;
else if(h1 < h2) return -1;
return 0;
}
static int sorthashesS(const void *a, const void *b){ // sort by string
char *s1 = ((strhash*)a)->str, *s2 = ((strhash*)b)->str;
return strcmp(s1, s2);
}
static int sorthashesF(const void *a, const void *b){ // sort by fname
char *s1 = ((strhash*)a)->fname, *s2 = ((strhash*)b)->fname;
return strcmp(s1, s2);
}

static FILE *openoutp(const char *name){
FILE *f = fopen(name, "w");
if(!f) ERR("Can't open file %s", name);
return f;
}

static char *macroname(const char *cmd){
static char macro[32];
int i = 0;
while(i < 31 && *cmd){
char c = *cmd++;
if(!isalnum(c)) c = '_';
if(islower(c)) c = toupper(c);
macro[i++] = c;
}
macro[i] = 0;
return macro;
}

static char *fnname(const char *cmd){
static char fn[32];
int i = 0;
while(i < 31 && *cmd){
char c = *cmd++;
if(!isalpha(c)) c = '_';
if(isupper(c)) c = tolower(c);
fn[i++] = c;
}
fn[i] = 0;
return fn;
}

static const char *fhdr =
"int parsecmd(char *cmd, char *args){\n\
if(!cmd || !args) return 0;\n\
uint32_t h = hashf(cmd);\n\
switch(h){\n"
;
static const char *ffooter =
" default: return 0;\n\
}\n\
return 0;\n\
}\n\n"
;
static const char *fns =
"int fn_%s(_U_ uint32_t hash, _U_ char *args) WAL; // \"%s\" (%u)\n\n"
;
static const char *fproto = "int parsecmd(char *cmdwargs, char *args);\n\n";
static const char *sw =
" case CMD_%s:\n\
return fn_%s(h, args);\n\
break;\n";
static const char *srchdr =
"#include \n\
#include \n\
#include \"%s\"\n\n\
#ifndef _U_\n\
#define _U_ __attribute__((__unused__))\n\
#endif\n\n\
#ifndef WAL\n\
#define WAL __attribute__ ((weak, alias (\"__f1\")))\n\
#endif\n\nint __f1(_U_ uint32_t h, _U_ char *a){return 1;}\n\n"
;

static void build(strhash *H, int hno, int hlen){
green("Generate files for hash function '%s'\n", hashnames[hno]);
int lmax = 1;
for(int i = 0; i < hlen; ++i){
int l = strlen(H[i].str);
if(l > lmax){
lmax = l;
}
}
lmax = (lmax + 3)/4;
lmax *= 4;
// resort H by strings
qsort(H, hlen, sizeof(strhash), sorthashesS);
FILE *source = openoutp(G.sourcefile), *header = openoutp(G.headerfile);
fprintf(source, srchdr, G.headerfile);
if(G.genfunc){
for(int i = 0; i < hlen; ++i){
fprintf(source, fns, H[i].fname, H[i].str, H[i].hash);
}
}
fprintf(header, "%s", fproto);
fprintf(source, "%s\n", hashsources[hno]);
fprintf(source, "%s", fhdr);
for(int i = 0; i < hlen; ++i){
char *m = macroname(H[i].str);
fprintf(source, sw, m, H[i].fname);
fprintf(header, "#define CMD_%-*s (%u)\n", lmax, m, H[i].hash);
}
fprintf(source, "%s", ffooter);
fclose(source);
fclose(header);
}

int main(int argc, char **argv){
initial_setup();
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);
if(!b) ERRX("Can't open %s", G.dict);
char *word = b->data;
strhash *H = MALLOC(strhash, ALLOCSZ);
int l = ALLOCSZ, idx = 0;
while(*word){
if(idx >= l){
l += ALLOCSZ;
H = realloc(H, sizeof(strhash) * l);
if(!H) ERR("realloc()");
}
while(*word && *word < 33) ++word;
if(!*word) break;
char *nxt = strchr(word, '\n');
if(nxt){
int len = nxt - word;
if(len > 31) len = 31;
strncpy(H[idx].str, word, len);
H[idx].str[len] = 0;
}else{
snprintf(H[idx].str, 31, "%s", word);
}
snprintf(H[idx].fname, 31, "%s", fnname(H[idx].str));
++idx;
if(!nxt) break;
word = nxt + 1;
}
// test fname matches
qsort(H, idx, sizeof(strhash), sorthashesF);
int mflag = 0;
int imax1 = idx - 1, hno = 0;
for(int i = 0; i < imax1; ++i){ // test hash matches
if(0 == strcmp(H[i].fname, H[i+1].fname)){
mflag = 1;
WARNX("Have two similar function names for '%s' and '%s': 'fn_%s'",
H[i].str, H[i+1].str, H[i].fname);
}
}
if(mflag) ERRX("Can't generate code when names of some functions matches");
for(; hno < HASHFNO; ++hno){
for(int i = 0; i < idx; ++i)
H[i].hash = hash[hno](H[i].str);
qsort(H, idx, sizeof(strhash), sorthashesH);
strhash *p = H;
int nmatches = 0;
for(int i = 0; i < imax1; ++i, ++p){ // test hash matches
if(p->hash == p[1].hash) ++nmatches;
}
if(nmatches == 0){
build(H, hno, idx);
break;
}
WARNX("Function '%s' have %d matches", hashnames[hno], nmatches);
}
if(hno == HASHFNO) WARNX("Can't find proper hash function");
FREE(H);
My_munmap(b);
return 0;
}

Собираем и запускаем с тестовым "словарем":

hello
world
what
put
sim key
change ip
change param
set
clear
reset
get
out
in
Теперь допускаются команды из нескольких слов. Если же первыми символами в строке являются недопустимые, они опускаются.
Получаем заголовочный файл:

int parsecmd(char *cmdwargs, char *args);

#define CMD_CHANGE_IP (1920374500)
#define CMD_CHANGE_PARAM (1673545756)
#define CMD_CLEAR (2063720716)
#define CMD_GET (2963126085)
#define CMD_HELLO (3433426201)
#define CMD_IN (89558876)
#define CMD_OUT (2963261277)
#define CMD_PUT (2963277918)
#define CMD_RESET (1907803304)
#define CMD_SET (2963325777)
#define CMD_SIM_KEY (2773741623)
#define CMD_WHAT (25570233)
#define CMD_WORLD (3313868845)

и исходник:

#include
#include
#include "hash.h"

#ifndef _U_
#define _U_ __attribute__((__unused__))
#endif

#ifndef WAL
#define WAL __attribute__ ((weak, alias ("__f1")))
#endif

static int __f1(_U_ uint32_t h, _U_ char *a){return 1;}

int fn_change_ip(_U_ uint32_t hash, _U_ char *args) WAL; // "change ip" (1920374500)

int fn_change_param(_U_ uint32_t hash, _U_ char *args) WAL; // "change param" (1673545756)

int fn_clear(_U_ uint32_t hash, _U_ char *args) WAL; // "clear" (2063720716)

int fn_get(_U_ uint32_t hash, _U_ char *args) WAL; // "get" (2963126085)

int fn_hello(_U_ uint32_t hash, _U_ char *args) WAL; // "hello" (3433426201)

int fn_in(_U_ uint32_t hash, _U_ char *args) WAL; // "in" (89558876)

int fn_out(_U_ uint32_t hash, _U_ char *args) WAL; // "out" (2963261277)

int fn_put(_U_ uint32_t hash, _U_ char *args) WAL; // "put" (2963277918)

int fn_reset(_U_ uint32_t hash, _U_ char *args) WAL; // "reset" (1907803304)

int fn_set(_U_ uint32_t hash, _U_ char *args) WAL; // "set" (2963325777)

int fn_sim_key(_U_ uint32_t hash, _U_ char *args) WAL; // "sim key" (2773741623)

int fn_what(_U_ uint32_t hash, _U_ char *args) WAL; // "what" (25570233)

int fn_world(_U_ uint32_t hash, _U_ char *args) WAL; // "world" (3313868845)

static uint32_t hashf(const char *str){
uint32_t hash = 5381;
uint32_t c;
while((c = (uint32_t)*str++))
hash = ((hash << 7) + hash) + c;
return hash;
}

int parsecmd(char *cmd, char *args){
if(!cmd || !args) return 0;
uint32_t h = hashf(cmd);
switch(h){
case CMD_CHANGE_IP:
return fn_change_ip(h, args);
break;
case CMD_CHANGE_PARAM:
return fn_change_param(h, args);
break;
case CMD_CLEAR:
return fn_clear(h, args);
break;
case CMD_GET:
return fn_get(h, args);
break;
case CMD_HELLO:
return fn_hello(h, args);
break;
case CMD_IN:
return fn_in(h, args);
break;
case CMD_OUT:
return fn_out(h, args);
break;
case CMD_PUT:
return fn_put(h, args);
break;
case CMD_RESET:
return fn_reset(h, args);
break;
case CMD_SET:
return fn_set(h, args);
break;
case CMD_SIM_KEY:
return fn_sim_key(h, args);
break;
case CMD_WHAT:
return fn_what(h, args);
break;
case CMD_WORLD:
return fn_world(h, args);
break;
default: return 0;
}
return 0;
}

Теперь уже где-то в другом месте все эти функции мы можем спокойно переопределить, скажем, так:

#include
#include
#include

#include "hash.h"

int fn_hello(uint32_t hash, char *args){
printf("HELLO! Hash=%u, param=%s\n", hash, args);
return 1;
}
int fn_world(uint32_t hash, char *args){
printf("WORLD: %u - %s\n", hash, args);
return 1;
}

int main(int argc, char **argv){
if(argc < 2) return 1;
char *args = "";
if(argc > 2) args = argv[2];
if(!parsecmd(argv[1], args)) printf("%s not found\n", argv[1]);
else printf("All OK\n");
return 0;
}

И проверить:

./test hello param1
HELLO! Hash=3433426201, param=param1
All OK

./test world
WORLD: 3313868845 -
All OK

./test "change ip"
All OK

./test change ip
change not found

c, snippets

Previous post Next post
Up