From 6c316619e722c007a65c5ee51dc53df6b33d3e11 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Sat, 14 Aug 2010 13:00:13 +0000 Subject: 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 --- common/expr.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'common/expr.c') 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 #include +#include #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 - /* 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) { -- cgit v1.2.3