-
Notifications
You must be signed in to change notification settings - Fork 3
feat: introduce a new replace template syntax ${*} #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ extern void replace_templates(array_t *pts, array_t *rtdb) { | |
| symbol_t *sym; | ||
| size_t ptslen, ptlen, rtslen; | ||
| char *rt; | ||
| const char *sectname; | ||
| int v; | ||
|
|
||
| pre_user = -1; | ||
|
|
@@ -18,26 +19,37 @@ extern void replace_templates(array_t *pts, array_t *rtdb) { | |
| for (size_t symidx = 0; symidx < ptlen; symidx++) { | ||
| sym = array_get(pt, symidx); | ||
|
|
||
| if (sym->kind == SymkindText) | ||
| continue; | ||
| if (sym->kind != SymkindRepl) | ||
| continue; /* only handle replace templates */ | ||
|
|
||
| rts = parse_replace_string(sym->content); | ||
| rtslen = array_size(rts); | ||
|
|
||
| if (rtslen == 0) | ||
| if (!rtslen) | ||
| synerr_empty(); | ||
|
|
||
| v = rand_range(0, rtslen - 1); | ||
| rt = *((char **) array_get(rts, v)); | ||
|
|
||
| sectarr = rtdbquery(rtdb, rt); | ||
| if (!sectarr) | ||
| synerr_invalid(rt); | ||
| if (STREQL(rt, SECTNAME_WILDCARD)) { /* wildcard template? */ | ||
| v = rand_range(0, array_size(rtdb) - 1); | ||
| sectarr = rtdbquerybyidx(rtdb, v, §name); | ||
|
Comment on lines
+34
to
+36
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| if (STREQL(rt, "user")) | ||
| rthandle_user(sym, sectarr); | ||
| else | ||
| rthandle_else(sym, sectarr); | ||
| if (STREQL(sectname, SECTNAME_USER)) | ||
| rthandle_user(sym, sectarr); | ||
| else | ||
| rthandle_else(sym, sectarr); | ||
| } | ||
| else { | ||
| sectarr = rtdbquery(rtdb, rt); | ||
| if (!sectarr) | ||
| synerr_invalid(rt); | ||
|
|
||
| if (STREQL(rt, SECTNAME_USER)) | ||
| rthandle_user(sym, sectarr); | ||
| else | ||
| rthandle_else(sym, sectarr); | ||
| } | ||
|
|
||
| /* cleanup rts */ | ||
| for (size_t i = 0; i < rtslen; i++) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,7 @@ extern array_t *rtdbparse(array_t *linestr) { | |
| if (!setjmp(env)) { | ||
| /* backward compatibility for usr.dat */ | ||
| if (!seek_section()) { | ||
| sectarr = add_mapper(table, "user"); | ||
| sectarr = add_mapper(table, SECTNAME_USER); | ||
| while (parse_value(sectarr)); | ||
| } | ||
|
|
||
|
|
@@ -42,6 +42,12 @@ extern array_t *rtdbquery(array_t *table, const char *sectname) { | |
| return NULL; | ||
| } | ||
|
|
||
| extern array_t *rtdbquerybyidx(array_t *table, size_t idx, const char **sectname) { | ||
| mapper_t *mapper = array_get(table, idx); | ||
| if (sectname) *sectname = mapper->sect; | ||
|
Comment on lines
+45
to
+47
|
||
| return mapper->addr; | ||
| } | ||
|
|
||
| extern void rtdbcheck(array_t *db) { | ||
| mapper_t *m; | ||
| array_t *sectarr; | ||
|
|
@@ -57,7 +63,7 @@ extern void rtdbcheck(array_t *db) { | |
| sectname = m->sect; | ||
| sectarr_siz = array_size(sectarr); | ||
|
|
||
| if (STREQL(sectname, "user")) { | ||
| if (STREQL(sectname, SECTNAME_USER)) { | ||
| if (sectarr_siz >= threshold) | ||
| continue; | ||
| VERR("at least %zu user entries required but only %zu", | ||
|
|
@@ -116,6 +122,8 @@ static int seek_section(void) { | |
| } | ||
|
|
||
| static char *parse_section(void) { | ||
| char *ret; | ||
|
|
||
| find_rearpos(); | ||
|
|
||
| if (l->run[rearpos] != ']') { | ||
|
|
@@ -128,8 +136,15 @@ static char *parse_section(void) { | |
| synerr(); | ||
| } | ||
|
|
||
| ret = l->run + frontpos; | ||
| l->run[rearpos] = '\0'; | ||
| return l->run + frontpos; | ||
|
|
||
| if (STREQL(ret, SECTNAME_WILDCARD)) { | ||
| reason = "section name can't be *"; | ||
| synerr(); | ||
|
Comment on lines
140
to
+144
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. intended for convenience |
||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| static int parse_value(array_t *sectarr) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace templates other than
${user}is currently a feature only supported by the C implementation.