diff options
author | Arthur de Jong <arthur@arthurdejong.org> | 2010-08-14 15:00:13 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2010-08-14 15:00:13 +0200 |
commit | 6c316619e722c007a65c5ee51dc53df6b33d3e11 (patch) | |
tree | 12ff63b86d4219bdb4bb9e3a07c7cb2b3b1678a1 /common/expr.c | |
parent | 2ea91ea150376c63f39b0b10f6b95306ecd8a04b (diff) |
do not expand variables in rest of ${var:-rest} expressions if var is not blank or empty
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1168 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'common/expr.c')
-rw-r--r-- | common/expr.c | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/common/expr.c b/common/expr.c index 679d8b7..4f957ee 100644 --- a/common/expr.c +++ b/common/expr.c @@ -2,7 +2,7 @@ expr.c - limited shell-like expression parsing functions This file is part of the nss-pam-ldapd library. - Copyright (C) 2009 Arthur de Jong + Copyright (C) 2009, 2010 Arthur de Jong This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -24,8 +24,10 @@ #include <stdlib.h> #include <string.h> +#include <stdio.h> #include "expr.h" +#include "compat/attrs.h" /* the maximum length of a variable name */ #define MAXVARLENGTH 30 @@ -40,8 +42,6 @@ static inline int my_isalphanum(const char c) return my_isalpha(c)||((c>='0')&&(c<='9')); } -#include <stdio.h> - /* return the part of the string that is a valid name */ MUST_USE static const char *parse_name(const char *str,int *ptr,char *buffer,size_t buflen) { @@ -64,6 +64,12 @@ MUST_USE static const char *parse_name(const char *str,int *ptr,char *buffer,siz return buffer; } +/* dummy expander function to always return an empty string */ +static const char *empty_expander(const char UNUSED(*name),void UNUSED(*expander_arg)) +{ + return ""; +} + /* definition of the parse functions (they call eachother) */ MUST_USE static const char *parse_dollar_expression( const char *str,int *ptr,char *buffer,size_t buflen, @@ -98,14 +104,21 @@ MUST_USE static const char *parse_dollar_expression( { /* if variable is not set or empty, substitute remainder */ (*ptr)+=2; - if (parse_expression(str,ptr,'}',buffer,buflen,expander,expander_arg)==NULL) - return NULL; if ((varvalue!=NULL)&&(*varvalue!='\0')) { + /* value is set, skip rest of expression and use value */ + if (parse_expression(str,ptr,'}',buffer,buflen,empty_expander,NULL)==NULL) + return NULL; if (strlen(varvalue)>=buflen) return NULL; strcpy(buffer,varvalue); } + else + { + /* value is not set, evaluate rest of expression */ + if (parse_expression(str,ptr,'}',buffer,buflen,expander,expander_arg)==NULL) + return NULL; + } } else if (strncmp(str+*ptr,":+",2)==0) { |