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, see <http://www.gnu.org/licenses/>. */
35 /* closeout.c - close standard output and standard error
36 Copyright (C) 1998-2007 Free Software Foundation, Inc.
38 This program is free software; you can redistribute it and/or modify
39 it under the terms of the GNU General Public License as published by
40 the Free Software Foundation; either version 2, or (at your option)
43 This program is distributed in the hope that it will be useful,
44 but WITHOUT ANY WARRANTY; without even the implied warranty of
45 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 GNU General Public License for more details.
48 You should have received a copy of the GNU General Public License
49 along with this program; if not, see <http://www.gnu.org/licenses/>. */
56 /* If true, substitution shall be performed on all variables. */
57 static unsigned short int all_variables
;
59 /* Forward declaration of local functions. */
60 static void print_variables (const char *string
);
61 static void note_variables (const char *string
);
62 static void subst_from_stdin (void);
65 cmd_main (int argc
, const char *argv
[])
67 /* Default values for command line options. */
68 /* unsigned short int show_variables = 0; */
73 error ("we won't substitute all variables on stdin for you");
80 /* echo '$foo and $bar' | git sh-i18n--envsubst --variables '$foo and $bar' */
82 note_variables (argv
[1]);
86 /* git sh-i18n--envsubst --variables '$foo and $bar' */
87 if (strcmp(argv
[1], "--variables"))
88 error ("first argument must be --variables when two are given");
89 /* show_variables = 1; */
90 print_variables (argv
[2]);
93 error ("too many arguments");
97 /* Close standard error. This is simpler than fwriteerror_no_ebadf, because
98 upon failure we don't need an errno - all we can do at this point is to
99 set an exit status. */
101 if (ferror (stderr
) || fflush (stderr
))
106 if (fclose (stderr
) && errno
!= EBADF
)
112 /* Parse the string and invoke the callback each time a $VARIABLE or
113 ${VARIABLE} construct is seen, where VARIABLE is a nonempty sequence
114 of ASCII alphanumeric/underscore characters, starting with an ASCII
115 alphabetic/underscore character.
116 We allow only ASCII characters, to avoid dependencies w.r.t. the current
117 encoding: While "${\xe0}" looks like a variable access in ISO-8859-1
118 encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030,
119 SHIFT_JIS, JOHAB encodings, because \xe0\x7d is a single character in these
122 find_variables (const char *string
,
123 void (*callback
) (const char *var_ptr
, size_t var_len
))
125 for (; *string
!= '\0';)
126 if (*string
++ == '$')
128 const char *variable_start
;
129 const char *variable_end
;
130 unsigned short int valid
;
136 variable_start
= string
;
138 if ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z') || c
== '_')
142 while ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z')
143 || (c
>= '0' && c
<= '9') || c
== '_');
144 variable_end
= string
;
146 if (variable_start
[-1] == '{')
160 callback (variable_start
, variable_end
- variable_start
);
166 /* Print a variable to stdout, followed by a newline. */
168 print_variable (const char *var_ptr
, size_t var_len
)
170 fwrite (var_ptr
, var_len
, 1, stdout
);
174 /* Print the variables contained in STRING to stdout, each one followed by a
177 print_variables (const char *string
)
179 find_variables (string
, &print_variable
);
183 /* Type describing list of immutable strings,
184 implemented using a dynamic array. */
185 typedef struct string_list_ty string_list_ty
;
186 struct string_list_ty
193 /* Initialize an empty list of strings. */
195 string_list_init (string_list_ty
*slp
)
202 /* Append a single string to the end of a list of strings. */
204 string_list_append (string_list_ty
*slp
, const char *s
)
207 if (slp
->nitems
>= slp
->nitems_max
)
209 slp
->nitems_max
= slp
->nitems_max
* 2 + 4;
210 REALLOC_ARRAY(slp
->item
, slp
->nitems_max
);
213 /* Add the string to the end of the list. */
214 slp
->item
[slp
->nitems
++] = s
;
217 /* Compare two strings given by reference. */
219 cmp_string (const void *pstr1
, const void *pstr2
)
221 const char *str1
= *(const char **)pstr1
;
222 const char *str2
= *(const char **)pstr2
;
224 return strcmp (str1
, str2
);
227 /* Sort a list of strings. */
229 string_list_sort (string_list_ty
*slp
)
231 QSORT(slp
->item
, slp
->nitems
, cmp_string
);
234 /* Test whether a sorted string list contains a given string. */
236 sorted_string_list_member (const string_list_ty
*slp
, const char *s
)
247 /* Here we know that if s is in the list, it is at an index j
248 with j1 <= j < j2. */
249 size_t j
= (j1
+ j2
) >> 1;
250 int result
= strcmp (slp
->item
[j
], s
);
254 else if (result
== 0)
260 if (strcmp (slp
->item
[j1
], s
) == 0)
267 /* Set of variables on which to perform substitution.
268 Used only if !all_variables. */
269 static string_list_ty variables_set
;
271 /* Adds a variable to variables_set. */
273 note_variable (const char *var_ptr
, size_t var_len
)
275 char *string
= xmemdupz (var_ptr
, var_len
);
277 string_list_append (&variables_set
, string
);
280 /* Stores the variables occurring in the string in variables_set. */
282 note_variables (const char *string
)
284 string_list_init (&variables_set
);
285 find_variables (string
, ¬e_variable
);
286 string_list_sort (&variables_set
);
293 int c
= getc (stdin
);
298 error ("error while reading standard input");
311 /* Copies stdin to stdout, performing substitutions. */
313 subst_from_stdin (void)
316 static size_t bufmax
;
317 static size_t buflen
;
325 /* Look for $VARIABLE or ${VARIABLE}. */
328 unsigned short int opening_brace
= 0;
329 unsigned short int closing_brace
= 0;
337 if ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z') || c
== '_')
339 unsigned short int valid
;
341 /* Accumulate the VARIABLE in buffer. */
345 if (buflen
>= bufmax
)
347 bufmax
= 2 * bufmax
+ 10;
348 buffer
= xrealloc (buffer
, bufmax
);
350 buffer
[buflen
++] = c
;
354 while ((c
>= 'A' && c
<= 'Z') || (c
>= 'a' && c
<= 'z')
355 || (c
>= '0' && c
<= '9') || c
== '_');
378 /* Terminate the variable in the buffer. */
379 if (buflen
>= bufmax
)
381 bufmax
= 2 * bufmax
+ 10;
382 buffer
= xrealloc (buffer
, bufmax
);
384 buffer
[buflen
] = '\0';
386 /* Test whether the variable shall be substituted. */
388 && !sorted_string_list_member (&variables_set
, buffer
))
394 /* Substitute the variable's value from the environment. */
395 const char *env_value
= getenv (buffer
);
397 if (env_value
!= NULL
)
398 fputs (env_value
, stdout
);
402 /* Perform no substitution at all. Since the buffered input
403 contains no other '$' than at the start, we can just
404 output all the buffered contents. */
408 fwrite (buffer
, buflen
, 1, stdout
);