Merge branch 'js/get-short-oid-drop-cache'
[git/raj.git] / sh-i18n--envsubst.c
blobcecfdd36c7e696dce6188aea3398fbe8857d798b
1 /*
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"
17 #include "trace2.h"
19 /* Substitution of environment variables in shell format strings.
20 Copyright (C) 2003-2007 Free Software Foundation, Inc.
21 Written by Bruno Haible <bruno@clisp.org>, 2003.
23 This program is free software; you can redistribute it and/or modify
24 it under the terms of the GNU General Public License as published by
25 the Free Software Foundation; either version 2, or (at your option)
26 any later version.
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, see <http://www.gnu.org/licenses/>. */
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)
42 any later version.
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, see <http://www.gnu.org/licenses/>. */
52 #include <errno.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
57 /* If true, substitution shall be performed on all variables. */
58 static unsigned short int all_variables;
60 /* Forward declaration of local functions. */
61 static void print_variables (const char *string);
62 static void note_variables (const char *string);
63 static void subst_from_stdin (void);
65 int
66 cmd_main (int argc, const char *argv[])
68 /* Default values for command line options. */
69 /* unsigned short int show_variables = 0; */
71 trace2_cmd_name("sh-i18n--envsubst");
73 switch (argc)
75 case 1:
76 error ("we won't substitute all variables on stdin for you");
77 break;
79 all_variables = 1;
80 subst_from_stdin ();
82 case 2:
83 /* echo '$foo and $bar' | git sh-i18n--envsubst --variables '$foo and $bar' */
84 all_variables = 0;
85 note_variables (argv[1]);
86 subst_from_stdin ();
87 break;
88 case 3:
89 /* git sh-i18n--envsubst --variables '$foo and $bar' */
90 if (strcmp(argv[1], "--variables"))
91 error ("first argument must be --variables when two are given");
92 /* show_variables = 1; */
93 print_variables (argv[2]);
94 break;
95 default:
96 error ("too many arguments");
97 break;
100 /* Close standard error. This is simpler than fwriteerror_no_ebadf, because
101 upon failure we don't need an errno - all we can do at this point is to
102 set an exit status. */
103 errno = 0;
104 if (ferror (stderr) || fflush (stderr))
106 fclose (stderr);
107 exit (EXIT_FAILURE);
109 if (fclose (stderr) && errno != EBADF)
110 exit (EXIT_FAILURE);
112 exit (EXIT_SUCCESS);
115 /* Parse the string and invoke the callback each time a $VARIABLE or
116 ${VARIABLE} construct is seen, where VARIABLE is a nonempty sequence
117 of ASCII alphanumeric/underscore characters, starting with an ASCII
118 alphabetic/underscore character.
119 We allow only ASCII characters, to avoid dependencies w.r.t. the current
120 encoding: While "${\xe0}" looks like a variable access in ISO-8859-1
121 encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030,
122 SHIFT_JIS, JOHAB encodings, because \xe0\x7d is a single character in these
123 encodings. */
124 static void
125 find_variables (const char *string,
126 void (*callback) (const char *var_ptr, size_t var_len))
128 for (; *string != '\0';)
129 if (*string++ == '$')
131 const char *variable_start;
132 const char *variable_end;
133 unsigned short int valid;
134 char c;
136 if (*string == '{')
137 string++;
139 variable_start = string;
140 c = *string;
141 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
144 c = *++string;
145 while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
146 || (c >= '0' && c <= '9') || c == '_');
147 variable_end = string;
149 if (variable_start[-1] == '{')
151 if (*string == '}')
153 string++;
154 valid = 1;
156 else
157 valid = 0;
159 else
160 valid = 1;
162 if (valid)
163 callback (variable_start, variable_end - variable_start);
169 /* Print a variable to stdout, followed by a newline. */
170 static void
171 print_variable (const char *var_ptr, size_t var_len)
173 fwrite (var_ptr, var_len, 1, stdout);
174 putchar ('\n');
177 /* Print the variables contained in STRING to stdout, each one followed by a
178 newline. */
179 static void
180 print_variables (const char *string)
182 find_variables (string, &print_variable);
186 /* Type describing list of immutable strings,
187 implemented using a dynamic array. */
188 typedef struct string_list_ty string_list_ty;
189 struct string_list_ty
191 const char **item;
192 size_t nitems;
193 size_t nitems_max;
196 /* Initialize an empty list of strings. */
197 static inline void
198 string_list_init (string_list_ty *slp)
200 slp->item = NULL;
201 slp->nitems = 0;
202 slp->nitems_max = 0;
205 /* Append a single string to the end of a list of strings. */
206 static inline void
207 string_list_append (string_list_ty *slp, const char *s)
209 /* Grow the list. */
210 if (slp->nitems >= slp->nitems_max)
212 slp->nitems_max = slp->nitems_max * 2 + 4;
213 REALLOC_ARRAY(slp->item, slp->nitems_max);
216 /* Add the string to the end of the list. */
217 slp->item[slp->nitems++] = s;
220 /* Compare two strings given by reference. */
221 static int
222 cmp_string (const void *pstr1, const void *pstr2)
224 const char *str1 = *(const char **)pstr1;
225 const char *str2 = *(const char **)pstr2;
227 return strcmp (str1, str2);
230 /* Sort a list of strings. */
231 static inline void
232 string_list_sort (string_list_ty *slp)
234 QSORT(slp->item, slp->nitems, cmp_string);
237 /* Test whether a sorted string list contains a given string. */
238 static int
239 sorted_string_list_member (const string_list_ty *slp, const char *s)
241 size_t j1, j2;
243 j1 = 0;
244 j2 = slp->nitems;
245 if (j2 > 0)
247 /* Binary search. */
248 while (j2 - j1 > 1)
250 /* Here we know that if s is in the list, it is at an index j
251 with j1 <= j < j2. */
252 size_t j = (j1 + j2) >> 1;
253 int result = strcmp (slp->item[j], s);
255 if (result > 0)
256 j2 = j;
257 else if (result == 0)
258 return 1;
259 else
260 j1 = j + 1;
262 if (j2 > j1)
263 if (strcmp (slp->item[j1], s) == 0)
264 return 1;
266 return 0;
270 /* Set of variables on which to perform substitution.
271 Used only if !all_variables. */
272 static string_list_ty variables_set;
274 /* Adds a variable to variables_set. */
275 static void
276 note_variable (const char *var_ptr, size_t var_len)
278 char *string = xmemdupz (var_ptr, var_len);
280 string_list_append (&variables_set, string);
283 /* Stores the variables occurring in the string in variables_set. */
284 static void
285 note_variables (const char *string)
287 string_list_init (&variables_set);
288 find_variables (string, &note_variable);
289 string_list_sort (&variables_set);
293 static int
294 do_getc (void)
296 int c = getc (stdin);
298 if (c == EOF)
300 if (ferror (stdin))
301 error ("error while reading standard input");
304 return c;
307 static inline void
308 do_ungetc (int c)
310 if (c != EOF)
311 ungetc (c, stdin);
314 /* Copies stdin to stdout, performing substitutions. */
315 static void
316 subst_from_stdin (void)
318 static char *buffer;
319 static size_t bufmax;
320 static size_t buflen;
321 int c;
323 for (;;)
325 c = do_getc ();
326 if (c == EOF)
327 break;
328 /* Look for $VARIABLE or ${VARIABLE}. */
329 if (c == '$')
331 unsigned short int opening_brace = 0;
332 unsigned short int closing_brace = 0;
334 c = do_getc ();
335 if (c == '{')
337 opening_brace = 1;
338 c = do_getc ();
340 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
342 unsigned short int valid;
344 /* Accumulate the VARIABLE in buffer. */
345 buflen = 0;
348 if (buflen >= bufmax)
350 bufmax = 2 * bufmax + 10;
351 buffer = xrealloc (buffer, bufmax);
353 buffer[buflen++] = c;
355 c = do_getc ();
357 while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
358 || (c >= '0' && c <= '9') || c == '_');
360 if (opening_brace)
362 if (c == '}')
364 closing_brace = 1;
365 valid = 1;
367 else
369 valid = 0;
370 do_ungetc (c);
373 else
375 valid = 1;
376 do_ungetc (c);
379 if (valid)
381 /* Terminate the variable in the buffer. */
382 if (buflen >= bufmax)
384 bufmax = 2 * bufmax + 10;
385 buffer = xrealloc (buffer, bufmax);
387 buffer[buflen] = '\0';
389 /* Test whether the variable shall be substituted. */
390 if (!all_variables
391 && !sorted_string_list_member (&variables_set, buffer))
392 valid = 0;
395 if (valid)
397 /* Substitute the variable's value from the environment. */
398 const char *env_value = getenv (buffer);
400 if (env_value != NULL)
401 fputs (env_value, stdout);
403 else
405 /* Perform no substitution at all. Since the buffered input
406 contains no other '$' than at the start, we can just
407 output all the buffered contents. */
408 putchar ('$');
409 if (opening_brace)
410 putchar ('{');
411 fwrite (buffer, buflen, 1, stdout);
412 if (closing_brace)
413 putchar ('}');
416 else
418 do_ungetc (c);
419 putchar ('$');
420 if (opening_brace)
421 putchar ('{');
424 else
425 putchar (c);