From 632023acc2feed519659926bf320d303562a5713 Mon Sep 17 00:00:00 2001 From: dana Date: Tue, 12 Mar 2019 19:01:18 -0500 Subject: 44100: zparseopts: Add -F option, completion, tests; improve documentation * Enable zparseopts to perform basic usage validation (aborting on an unrecognised option-like parameter) * Officially document the resolution of ambiguous option specs --- Src/Modules/zutil.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'Src/Modules/zutil.c') diff --git a/Src/Modules/zutil.c b/Src/Modules/zutil.c index 19a8306b5..c4fe4a15e 100644 --- a/Src/Modules/zutil.c +++ b/Src/Modules/zutil.c @@ -1644,7 +1644,7 @@ static int bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) { char *o, *p, *n, **pp, **aval, **ap, *assoc = NULL, **cp, **np; - int del = 0, flags = 0, extract = 0, keep = 0; + int del = 0, flags = 0, extract = 0, fail = 0, keep = 0; Zoptdesc sopts[256], d; Zoptarr a, defarr = NULL; Zoptval v; @@ -1681,6 +1681,14 @@ bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) } extract = 1; break; + case 'F': + if (o[2]) { + args--; + o = NULL; + break; + } + fail = 1; + break; case 'K': if (o[2]) { args--; @@ -1843,6 +1851,10 @@ bin_zparseopts(char *nam, char **args, UNUSED(Options ops), UNUSED(int func)) if (!(d = lookup_opt(o + 1))) { while (*++o) { if (!(d = sopts[STOUC(*o)])) { + if (fail) { + zwarnnam(nam, "bad option: %c", *o); + return 1; + } o = NULL; break; } -- cgit v1.2.3 From 2acbae3badc5c9afe40e2a393472d2d303c60ae6 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 1 Dec 2019 03:59:39 +0000 Subject: unposted: zstyle: Add a unit test and some comments. --- ChangeLog | 3 +++ Src/Modules/zutil.c | 19 ++++++++++++++++--- Test/V05styles.ztst | 10 ++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) (limited to 'Src/Modules/zutil.c') diff --git a/ChangeLog b/ChangeLog index c9bca3071..83e082e7a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2019-12-01 Daniel Shahaf + * unposted: Src/Modules/zutil.c, Test/V05styles.ztst: zstyle: + Add a unit test and some comments. + * unposted: Completion/Unix/Command/_sqlite: Fix syntax error 2019-11-30 Daniel Shahaf diff --git a/Src/Modules/zutil.c b/Src/Modules/zutil.c index c4fe4a15e..24659cb16 100644 --- a/Src/Modules/zutil.c +++ b/Src/Modules/zutil.c @@ -88,7 +88,8 @@ typedef struct style *Style; struct style { struct hashnode node; - Stypat pats; /* patterns */ + Stypat pats; /* patterns, sorted by weight descending, then + by order of definition, newest first. */ }; struct stypat { @@ -337,7 +338,19 @@ setstypat(Style s, char *pat, Patprog prog, char **vals, int eval) p->eval = eprog; p->next = NULL; - /* Calculate the weight. */ + /* Calculate the weight. + * + * The weight of a pattern is scored as follows: + * + * - The pattern is split to colon-separated components. + * - A component equal to '*' (with nothing else) scores 0 points. + * - A component that's a pattern, otherwise, scores 1 point. + * - A component that's a literal string scores 2 points. + * - The score of a pattern is the sum of the score of its components. + * + * This corresponds to the notion of 'more specific' in the zshmodules(1) + * documentation of zstyle. + */ for (weight = 0, tmp = 2, first = 1, str = pat; *str; str++) { if (first && *str == '*' && (!str[1] || str[1] == ':')) { @@ -362,9 +375,9 @@ setstypat(Style s, char *pat, Patprog prog, char **vals, int eval) } p->weight = (weight += tmp); + /* Insert 'q' to 's->pats', using 'qq' as a temporary. */ for (qq = NULL, q = s->pats; q && q->weight >= weight; qq = q, q = q->next); - p->next = q; if (qq) qq->next = p; diff --git a/Test/V05styles.ztst b/Test/V05styles.ztst index ca95b6348..c221d9db8 100644 --- a/Test/V05styles.ztst +++ b/Test/V05styles.ztst @@ -141,3 +141,13 @@ >scalar-style > :ztst:context:* second-scalar-value + (zstyle 'ctx?' foo one + zstyle 'ctx*' foo two + zstyle -s 'ctx1' foo bar && print $bar) + (zstyle 'ctx*' foo two + zstyle 'ctx?' foo one + zstyle -s 'ctx1' foo bar && print $bar) +0:patterns of equal weight are used in order of definition +>one +>two + -- cgit v1.2.3