2 * sh-i18n--envsubst.c - a stripped-down version of gettext's envsubst(1)
4 * Copyright (C) 2010 Ævar Arnfjörð Bjarmason
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.
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.
16 #include "git-compat-util.h"
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.
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)
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.
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. */
36 /* closeout.c - close standard output and standard error
37 Copyright (C) 1998-2007 Free Software Foundation, Inc.
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)
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.
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. */
58 /* If true, substitution shall be performed on all variables. */
59 static unsigned short int all_variables;
61 /* Forward declaration of local functions. */
62 static void print_variables (const char *string);
63 static void note_variables (const char *string);
64 static void subst_from_stdin (void);
67 main (int argc, char *argv[])
69 /* Default values for command line options. */
70 /* unsigned short int show_variables = 0; */
75 error ("we won't substitute all variables on stdin for you");
81 /* echo '$foo and $bar' | git sh-i18n--envsubst --variables '$foo and $bar' */
83 note_variables (argv[1]);
87 /* git sh-i18n--envsubst --variables '$foo and $bar' */
88 if (strcmp(argv[1], "--variables"))
89 error ("first argument must be --variables when two are given");
90 /* show_variables = 1; */
91 print_variables (argv[2]);
94 error ("too many arguments");
98 /* Close standard error. This is simpler than fwriteerror_no_ebadf, because
99 upon failure we don't need an errno - all we can do at this point is to
100 set an exit status. */
102 if (ferror (stderr) || fflush (stderr))
107 if (fclose (stderr) && errno != EBADF)
113 /* Parse the string and invoke the callback each time a $VARIABLE or
114 ${VARIABLE} construct is seen, where VARIABLE is a nonempty sequence
115 of ASCII alphanumeric/underscore characters, starting with an ASCII
116 alphabetic/underscore character.
117 We allow only ASCII characters, to avoid dependencies w.r.t. the current
118 encoding: While "${\xe0}" looks like a variable access in ISO-8859-1
119 encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030,
120 SHIFT_JIS, JOHAB encodings, because \xe0\x7d is a single character in these
123 find_variables (const char *string,
124 void (*callback) (const char *var_ptr, size_t var_len))
126 for (; *string != '\0';)
127 if (*string++ == '$')
129 const char *variable_start;
130 const char *variable_end;
131 unsigned short int valid;
137 variable_start = string;
139 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
143 while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
144 || (c >= '0' && c <= '9') || c == '_');
145 variable_end = string;
147 if (variable_start[-1] == '{')
161 callback (variable_start, variable_end - variable_start);
167 /* Print a variable to stdout, followed by a newline. */
169 print_variable (const char *var_ptr, size_t var_len)
171 fwrite (var_ptr, var_len, 1, stdout);
175 /* Print the variables contained in STRING to stdout, each one followed by a
178 print_variables (const char *string)
180 find_variables (string, &print_variable);
184 /* Type describing list of immutable strings,
185 implemented using a dynamic array. */
186 typedef struct string_list_ty string_list_ty;
187 struct string_list_ty
194 /* Initialize an empty list of strings. */
196 string_list_init (string_list_ty *slp)
203 /* Append a single string to the end of a list of strings. */
205 string_list_append (string_list_ty *slp, const char *s)
208 if (slp->nitems >= slp->nitems_max)
212 slp->nitems_max = slp->nitems_max * 2 + 4;
213 nbytes = slp->nitems_max * sizeof (slp->item[0]);
214 slp->item = (const char **) xrealloc (slp->item, nbytes);
217 /* Add the string to the end of the list. */
218 slp->item[slp->nitems++] = s;
221 /* Compare two strings given by reference. */
223 cmp_string (const void *pstr1, const void *pstr2)
225 const char *str1 = *(const char **)pstr1;
226 const char *str2 = *(const char **)pstr2;
228 return strcmp (str1, str2);
231 /* Sort a list of strings. */
233 string_list_sort (string_list_ty *slp)
236 qsort (slp->item, slp->nitems, sizeof (slp->item[0]), cmp_string);
239 /* Test whether a string list contains a given string. */
241 string_list_member (const string_list_ty *slp, const char *s)
245 for (j = 0; j < slp->nitems; ++j)
246 if (strcmp (slp->item[j], s) == 0)
251 /* Test whether a sorted string list contains a given string. */
253 sorted_string_list_member (const string_list_ty *slp, const char *s)
264 /* Here we know that if s is in the list, it is at an index j
265 with j1 <= j < j2. */
266 size_t j = (j1 + j2) >> 1;
267 int result = strcmp (slp->item[j], s);
271 else if (result == 0)
277 if (strcmp (slp->item[j1], s) == 0)
284 /* Set of variables on which to perform substitution.
285 Used only if !all_variables. */
286 static string_list_ty variables_set;
288 /* Adds a variable to variables_set. */
290 note_variable (const char *var_ptr, size_t var_len)
292 char *string = xmalloc (var_len + 1);
293 memcpy (string, var_ptr, var_len);
294 string[var_len] = '\0';
296 string_list_append (&variables_set, string);
299 /* Stores the variables occurring in the string in variables_set. */
301 note_variables (const char *string)
303 string_list_init (&variables_set);
304 find_variables (string, ¬e_variable);
305 string_list_sort (&variables_set);
312 int c = getc (stdin);
317 error ("error while reading standard input");
330 /* Copies stdin to stdout, performing substitutions. */
332 subst_from_stdin (void)
335 static size_t bufmax;
336 static size_t buflen;
344 /* Look for $VARIABLE or ${VARIABLE}. */
347 unsigned short int opening_brace = 0;
348 unsigned short int closing_brace = 0;
356 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
358 unsigned short int valid;
360 /* Accumulate the VARIABLE in buffer. */
364 if (buflen >= bufmax)
366 bufmax = 2 * bufmax + 10;
367 buffer = xrealloc (buffer, bufmax);
369 buffer[buflen++] = c;
373 while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
374 || (c >= '0' && c <= '9') || c == '_');
397 /* Terminate the variable in the buffer. */
398 if (buflen >= bufmax)
400 bufmax = 2 * bufmax + 10;
401 buffer = xrealloc (buffer, bufmax);
403 buffer[buflen] = '\0';
405 /* Test whether the variable shall be substituted. */
407 && !sorted_string_list_member (&variables_set, buffer))
413 /* Substitute the variable's value from the environment. */
414 const char *env_value = getenv (buffer);
416 if (env_value != NULL)
417 fputs (env_value, stdout);
421 /* Perform no substitution at all. Since the buffered input
422 contains no other '$' than at the start, we can just
423 output all the buffered contents. */
427 fwrite (buffer, buflen, 1, stdout);