1/* 2 * sh-i18n--envsubst.c - a stripped-down version of gettext's envsubst(1) 3 * 4 * Copyright (C) 2010 Ævar Arnfjörð Bjarmason 5 * 6 * This is a modified version of 7 * 67d0871a8c:gettext-runtime/src/envsubst.c from the gettext.git 8 * repository. It has been stripped down to only implement the 9 * envsubst(1) features that we need in the git-sh-i18n fallbacks. 10 * 11 * The "Close standard error" part in main() is from 12 * 8dac033df0:gnulib-local/lib/closeout.c. The copyright notices for 13 * both files are reproduced immediately below. 14 */ 15 16#include"git-compat-util.h" 17 18/* Substitution of environment variables in shell format strings. 19 Copyright (C) 2003-2007 Free Software Foundation, Inc. 20 Written by Bruno Haible <bruno@clisp.org>, 2003. 21 22 This program is free software; you can redistribute it and/or modify 23 it under the terms of the GNU General Public License as published by 24 the Free Software Foundation; either version 2, or (at your option) 25 any later version. 26 27 This program is distributed in the hope that it will be useful, 28 but WITHOUT ANY WARRANTY; without even the implied warranty of 29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 30 GNU General Public License for more details. 31 32 You should have received a copy of the GNU General Public License 33 along with this program; if not, write to the Free Software Foundation, 34 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 35 36/* closeout.c - close standard output and standard error 37 Copyright (C) 1998-2007 Free Software Foundation, Inc. 38 39 This program is free software; you can redistribute it and/or modify 40 it under the terms of the GNU General Public License as published by 41 the Free Software Foundation; either version 2, or (at your option) 42 any later version. 43 44 This program is distributed in the hope that it will be useful, 45 but WITHOUT ANY WARRANTY; without even the implied warranty of 46 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 47 GNU General Public License for more details. 48 49 You should have received a copy of the GNU General Public License 50 along with this program; if not, write to the Free Software Foundation, 51 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 52 53#include <errno.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <string.h> 57 58/* If true, substitution shall be performed on all variables. */ 59static unsigned short int all_variables; 60 61/* Forward declaration of local functions. */ 62static voidprint_variables(const char*string); 63static voidnote_variables(const char*string); 64static voidsubst_from_stdin(void); 65 66int 67main(int argc,char*argv[]) 68{ 69/* Default values for command line options. */ 70/* unsigned short int show_variables = 0; */ 71 72switch(argc) 73{ 74case1: 75error("we won't substitute all variables on stdin for you"); 76break; 77/* 78 all_variables = 1; 79 subst_from_stdin (); 80 */ 81case2: 82/* echo '$foo and $bar' | git sh-i18n--envsubst --variables '$foo and $bar' */ 83 all_variables =0; 84note_variables(argv[1]); 85subst_from_stdin(); 86break; 87case3: 88/* git sh-i18n--envsubst --variables '$foo and $bar' */ 89if(strcmp(argv[1],"--variables")) 90error("first argument must be --variables when two are given"); 91/* show_variables = 1; */ 92print_variables(argv[2]); 93break; 94default: 95error("too many arguments"); 96break; 97} 98 99/* Close standard error. This is simpler than fwriteerror_no_ebadf, because 100 upon failure we don't need an errno - all we can do at this point is to 101 set an exit status. */ 102 errno =0; 103if(ferror(stderr) ||fflush(stderr)) 104{ 105fclose(stderr); 106exit(EXIT_FAILURE); 107} 108if(fclose(stderr) && errno != EBADF) 109exit(EXIT_FAILURE); 110 111exit(EXIT_SUCCESS); 112} 113 114/* Parse the string and invoke the callback each time a $VARIABLE or 115 ${VARIABLE} construct is seen, where VARIABLE is a nonempty sequence 116 of ASCII alphanumeric/underscore characters, starting with an ASCII 117 alphabetic/underscore character. 118 We allow only ASCII characters, to avoid dependencies w.r.t. the current 119 encoding: While "${\xe0}" looks like a variable access in ISO-8859-1 120 encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030, 121 SHIFT_JIS, JOHAB encodings, because \xe0\x7d is a single character in these 122 encodings. */ 123static void 124find_variables(const char*string, 125void(*callback) (const char*var_ptr,size_t var_len)) 126{ 127for(; *string !='\0';) 128if(*string++ =='$') 129{ 130const char*variable_start; 131const char*variable_end; 132unsigned short int valid; 133char c; 134 135if(*string =='{') 136 string++; 137 138 variable_start = string; 139 c = *string; 140if((c >='A'&& c <='Z') || (c >='a'&& c <='z') || c =='_') 141{ 142do 143 c = *++string; 144while((c >='A'&& c <='Z') || (c >='a'&& c <='z') 145|| (c >='0'&& c <='9') || c =='_'); 146 variable_end = string; 147 148if(variable_start[-1] =='{') 149{ 150if(*string =='}') 151{ 152 string++; 153 valid =1; 154} 155else 156 valid =0; 157} 158else 159 valid =1; 160 161if(valid) 162callback(variable_start, variable_end - variable_start); 163} 164} 165} 166 167 168/* Print a variable to stdout, followed by a newline. */ 169static void 170print_variable(const char*var_ptr,size_t var_len) 171{ 172fwrite(var_ptr, var_len,1, stdout); 173putchar('\n'); 174} 175 176/* Print the variables contained in STRING to stdout, each one followed by a 177 newline. */ 178static void 179print_variables(const char*string) 180{ 181find_variables(string, &print_variable); 182} 183 184 185/* Type describing list of immutable strings, 186 implemented using a dynamic array. */ 187typedefstruct string_list_ty string_list_ty; 188struct string_list_ty 189{ 190const char**item; 191size_t nitems; 192size_t nitems_max; 193}; 194 195/* Initialize an empty list of strings. */ 196staticinlinevoid 197string_list_init(string_list_ty *slp) 198{ 199 slp->item = NULL; 200 slp->nitems =0; 201 slp->nitems_max =0; 202} 203 204/* Append a single string to the end of a list of strings. */ 205staticinlinevoid 206string_list_append(string_list_ty *slp,const char*s) 207{ 208/* Grow the list. */ 209if(slp->nitems >= slp->nitems_max) 210{ 211size_t nbytes; 212 213 slp->nitems_max = slp->nitems_max *2+4; 214 nbytes = slp->nitems_max *sizeof(slp->item[0]); 215 slp->item = (const char**)xrealloc(slp->item, nbytes); 216} 217 218/* Add the string to the end of the list. */ 219 slp->item[slp->nitems++] = s; 220} 221 222/* Compare two strings given by reference. */ 223static int 224cmp_string(const void*pstr1,const void*pstr2) 225{ 226const char*str1 = *(const char**)pstr1; 227const char*str2 = *(const char**)pstr2; 228 229returnstrcmp(str1, str2); 230} 231 232/* Sort a list of strings. */ 233staticinlinevoid 234string_list_sort(string_list_ty *slp) 235{ 236if(slp->nitems >0) 237qsort(slp->item, slp->nitems,sizeof(slp->item[0]), cmp_string); 238} 239 240/* Test whether a sorted string list contains a given string. */ 241static int 242sorted_string_list_member(const string_list_ty *slp,const char*s) 243{ 244size_t j1, j2; 245 246 j1 =0; 247 j2 = slp->nitems; 248if(j2 >0) 249{ 250/* Binary search. */ 251while(j2 - j1 >1) 252{ 253/* Here we know that if s is in the list, it is at an index j 254 with j1 <= j < j2. */ 255size_t j = (j1 + j2) >>1; 256int result =strcmp(slp->item[j], s); 257 258if(result >0) 259 j2 = j; 260else if(result ==0) 261return1; 262else 263 j1 = j +1; 264} 265if(j2 > j1) 266if(strcmp(slp->item[j1], s) ==0) 267return1; 268} 269return0; 270} 271 272 273/* Set of variables on which to perform substitution. 274 Used only if !all_variables. */ 275static string_list_ty variables_set; 276 277/* Adds a variable to variables_set. */ 278static void 279note_variable(const char*var_ptr,size_t var_len) 280{ 281char*string =xmalloc(var_len +1); 282memcpy(string, var_ptr, var_len); 283 string[var_len] ='\0'; 284 285string_list_append(&variables_set, string); 286} 287 288/* Stores the variables occurring in the string in variables_set. */ 289static void 290note_variables(const char*string) 291{ 292string_list_init(&variables_set); 293find_variables(string, ¬e_variable); 294string_list_sort(&variables_set); 295} 296 297 298static int 299do_getc(void) 300{ 301int c =getc(stdin); 302 303if(c == EOF) 304{ 305if(ferror(stdin)) 306error("error while reading standard input"); 307} 308 309return c; 310} 311 312staticinlinevoid 313do_ungetc(int c) 314{ 315if(c != EOF) 316ungetc(c, stdin); 317} 318 319/* Copies stdin to stdout, performing substitutions. */ 320static void 321subst_from_stdin(void) 322{ 323static char*buffer; 324static size_t bufmax; 325static size_t buflen; 326int c; 327 328for(;;) 329{ 330 c =do_getc(); 331if(c == EOF) 332break; 333/* Look for $VARIABLE or ${VARIABLE}. */ 334if(c =='$') 335{ 336unsigned short int opening_brace =0; 337unsigned short int closing_brace =0; 338 339 c =do_getc(); 340if(c =='{') 341{ 342 opening_brace =1; 343 c =do_getc(); 344} 345if((c >='A'&& c <='Z') || (c >='a'&& c <='z') || c =='_') 346{ 347unsigned short int valid; 348 349/* Accumulate the VARIABLE in buffer. */ 350 buflen =0; 351do 352{ 353if(buflen >= bufmax) 354{ 355 bufmax =2* bufmax +10; 356 buffer =xrealloc(buffer, bufmax); 357} 358 buffer[buflen++] = c; 359 360 c =do_getc(); 361} 362while((c >='A'&& c <='Z') || (c >='a'&& c <='z') 363|| (c >='0'&& c <='9') || c =='_'); 364 365if(opening_brace) 366{ 367if(c =='}') 368{ 369 closing_brace =1; 370 valid =1; 371} 372else 373{ 374 valid =0; 375do_ungetc(c); 376} 377} 378else 379{ 380 valid =1; 381do_ungetc(c); 382} 383 384if(valid) 385{ 386/* Terminate the variable in the buffer. */ 387if(buflen >= bufmax) 388{ 389 bufmax =2* bufmax +10; 390 buffer =xrealloc(buffer, bufmax); 391} 392 buffer[buflen] ='\0'; 393 394/* Test whether the variable shall be substituted. */ 395if(!all_variables 396&& !sorted_string_list_member(&variables_set, buffer)) 397 valid =0; 398} 399 400if(valid) 401{ 402/* Substitute the variable's value from the environment. */ 403const char*env_value =getenv(buffer); 404 405if(env_value != NULL) 406fputs(env_value, stdout); 407} 408else 409{ 410/* Perform no substitution at all. Since the buffered input 411 contains no other '$' than at the start, we can just 412 output all the buffered contents. */ 413putchar('$'); 414if(opening_brace) 415putchar('{'); 416fwrite(buffer, buflen,1, stdout); 417if(closing_brace) 418putchar('}'); 419} 420} 421else 422{ 423do_ungetc(c); 424putchar('$'); 425if(opening_brace) 426putchar('{'); 427} 428} 429else 430putchar(c); 431} 432}