This patch makes it possible to use a negative offset to count from the end of a variable. Signed-off-by: Johannes Berg <johannes@sipsolutions.net> --- dovecot-1.0.rc2/doc/variables.txt 2006-04-12 09:39:31.000000000 +0200 +++ dovecot-1.0.rc2.mod/doc/variables.txt 2006-07-19 19:27:15.611041184 +0200 @@ -35,7 +35,10 @@ You can take a substring of the variable by giving optional offset followed by '.' and width after the '%' character. For example %2u gives first two characters of the username. %2.1u gives third character of the username. If -offset points outside the value, empty string is returned. +the offset is negative, it counts from the end, for example %-2.02i gives +the UID mod 100 (last two characters of the UID printed in a string). If a +positive offset points outside the value, empty string is returned, if a +negative offset does then the string is taken from the start. For login_log_format_elements there are also these variables: --- dovecot-1.0.rc2/src/lib/var-expand.c 2006-04-12 09:39:32.000000000 +0200 +++ dovecot-1.0.rc2.mod/src/lib/var-expand.c 2006-07-19 19:35:25.821041184 +0200 @@ -11,7 +11,8 @@ #include <stdlib.h> struct var_expand_context { - unsigned int offset, width; + int offset; + unsigned int width; }; struct var_expand_modifier { @@ -109,6 +110,7 @@ const char *(*modifier[MAX_MODIFIER_COUNT]) (const char *, struct var_expand_context *); unsigned int i, modifier_count; + int sign = 1; bool zero_padding = FALSE; memset(&ctx, 0, sizeof(ctx)); @@ -120,12 +122,16 @@ /* [<offset>.]<width>[<modifiers>]<variable> */ ctx.width = 0; + if (*str == '-') { + sign = -1; + str++; + } if (*str == '0') { zero_padding = TRUE; str++; } while (*str >= '0' && *str <= '9') { - ctx.width = ctx.width*10 + (*str - '0'); + ctx.width = ctx.width*10 + sign * (*str - '0'); str++; } @@ -178,9 +184,21 @@ if (var != NULL) { for (i = 0; i < modifier_count; i++) var = modifier[i](var, &ctx); - while (*var != '\0' && ctx.offset > 0) { - ctx.offset--; - var++; + if (ctx.offset < 0) { + /* if offset is < 0 then we want to + * start at the end */ + size_t len = strlen(var); + var += len; /* point to trailing NUL byte */ + while (ctx.offset < 0 && len > 0) { + ctx.offset++; + var--; + len--; + } + } else { + while (*var != '\0' && ctx.offset > 0) { + ctx.offset--; + var++; + } } if (ctx.width == 0) str_append(dest, var); @@ -205,7 +223,7 @@ const struct var_expand_modifier *m; /* [<offset>.]<width>[<modifiers>]<variable> */ - while (*str >= '0' && *str <= '9') + while ((*str >= '0' && *str <= '9') || *str == '-') str++; if (*str == '.') {
On Wed, 2006-07-19 at 19:38 +0200, Johannes Berg wrote:
This patch makes it possible to use a negative offset to count from the end of a variable.
Committed, although with a few changes. Could you test if it still works? ;)
On Fri, 11 Aug 2006, Johannes Berg wrote:
Timo Sirainen wrote:
Committed, although with a few changes. Could you test if it still works? ;)
I can't right now, David? Are you actually using this now?
My brief tests of your (Johannes's) patch on an rc2 base seemed good.
I am glad that Timo has committed this functionality to CVS since then.
I would be happy to test a later (post-commit) version.
But I notice (1) Timo says "with a few changes" (2) list discussion suggests that "rc6" seems a little unstable.
Would it be OK if I waited until "rc7" is out? (Another list thread seems to suggest that this should be quite soon.)
I'm a newcomer to dovecot so would rather avoid known instabilities diverting and distracting me. But if there did happen to be any problems in this new functionality(*), I would be reasonably confident in helping diagnose and attempting to fix them on a reasonably stable baseline.
(*) For the sake of mailing-list readership: the subject under discussion is the ability for "default_mail_env" to be able to specify: INBOX=/var/spool/mail/%-2.02i/%u (our example: large site, this area split into subdirectories of uid%100 i.e. last two decimal digits of users' uid).
--
: David Lee I.T. Service : : Senior Systems Programmer Computer Centre : : Durham University : : http://www.dur.ac.uk/t.d.lee/ South Road : : Durham DH1 3LE : : Phone: +44 191 334 2752 U.K. :
* On 11/08/06 09:59 +0100, David Lee wrote: | On Fri, 11 Aug 2006, Johannes Berg wrote: | | > Timo Sirainen wrote: | > > Committed, although with a few changes. Could you test if it still | > > works? ;) | > > | > I can't right now, David? Are you actually using this now? | | 1. My brief tests of your (Johannes's) patch on an rc2 base seemed good. | | 2. I am glad that Timo has committed this functionality to CVS since then. | | 3. I would be happy to test a later (post-commit) version. | | But I notice (1) Timo says "with a few changes" (2) list discussion | suggests that "rc6" seems a little unstable. | | Would it be OK if I waited until "rc7" is out? (Another list thread | seems to suggest that this should be quite soon.) You could use the nightly snapshot as of yesterday and test these features, then report on what happens, rather than wait until rc7 and realize something got broken with Timo's "with a few changes". | I'm a newcomer to dovecot so would rather avoid known instabilities | diverting and distracting me. But if there did happen to be any problems | in this new functionality(*), I would be reasonably confident in helping | diagnose and attempting to fix them on a reasonably stable baseline. That decision is good. Please help diagnose. -Wash http://www.netmeister.org/news/learn2quote.html DISCLAIMER: See http://www.wananchi.com/bms/terms.php -- +======================================================================+ |\ _,,,---,,_ | Odhiambo Washington <wash@wananchi.com> Zzz /,`.-'`' -. ;-;;,_ | Wananchi Online Ltd. www.wananchi.com |,4- ) )-,_. ,\ ( `'-'| Tel: +254 20 313985-9 +254 20 313922 '---''(_/--' `-'\_) | GSM: +254 722 743223 +254 733 744121 +======================================================================+ I don't believe there really IS a GAS SHORTAGE.. I think it's all just a BIG HOAX on the part of the plastic sign salesmen -- to sell more numbers!!
participants (4)
-
David Lee
-
Johannes Berg
-
Odhiambo WASHINGTON
-
Timo Sirainen