/* * sh-i18n--envsubst.c - a stripped-down version of gettext's envsubst(1) * * Copyright (C) 2010 Ævar Arnfjörð Bjarmason * * This is a modified version of * 67d0871a8c:gettext-runtime/src/envsubst.c from the gettext.git * repository. It has been stripped down to only implement the * envsubst(1) features that we need in the git-sh-i18n fallbacks. * * The "Close standard error" part in main() is from * 8dac033df0:gnulib-local/lib/closeout.c. The copyright notices for * both files are reproduced immediately below. */ #include "git-compat-util.h" #include "trace2.h" /* Substitution of environment variables in shell format strings. Copyright (C) 2003-2007 Free Software Foundation, Inc. Written by Bruno Haible <[email protected]>, 2003. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see <https://www.gnu.org/licenses/>. */ /* closeout.c - close standard output and standard error Copyright (C) 1998-2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see <https://www.gnu.org/licenses/>. */ #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* If true, substitution shall be performed on all variables. */ static unsigned short int all_variables; /* Forward declaration of local functions. */ static void print_variables (const char *string); static void note_variables (const char *string); static void subst_from_stdin (void); int cmd_main (int argc, const char *argv[]) { … } /* Parse the string and invoke the callback each time a $VARIABLE or ${VARIABLE} construct is seen, where VARIABLE is a nonempty sequence of ASCII alphanumeric/underscore characters, starting with an ASCII alphabetic/underscore character. We allow only ASCII characters, to avoid dependencies w.r.t. the current encoding: While "${\xe0}" looks like a variable access in ISO-8859-1 encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030, SHIFT_JIS, JOHAB encodings, because \xe0\x7d is a single character in these encodings. */ static void find_variables (const char *string, void (*callback) (const char *var_ptr, size_t var_len)) { … } /* Print a variable to stdout, followed by a newline. */ static void print_variable (const char *var_ptr, size_t var_len) { … } /* Print the variables contained in STRING to stdout, each one followed by a newline. */ static void print_variables (const char *string) { … } /* Type describing list of immutable strings, implemented using a dynamic array. */ string_list_ty; struct string_list_ty { … }; /* Initialize an empty list of strings. */ static inline void string_list_init (string_list_ty *slp) { … } /* Append a single string to the end of a list of strings. */ static inline void string_list_append (string_list_ty *slp, const char *s) { … } /* Compare two strings given by reference. */ static int cmp_string (const void *pstr1, const void *pstr2) { … } /* Sort a list of strings. */ static inline void string_list_sort (string_list_ty *slp) { … } /* Test whether a sorted string list contains a given string. */ static int sorted_string_list_member (const string_list_ty *slp, const char *s) { … } /* Set of variables on which to perform substitution. Used only if !all_variables. */ static string_list_ty variables_set; /* Adds a variable to variables_set. */ static void note_variable (const char *var_ptr, size_t var_len) { … } /* Stores the variables occurring in the string in variables_set. */ static void note_variables (const char *string) { … } static int do_getc (void) { … } static inline void do_ungetc (int c) { … } /* Copies stdin to stdout, performing substitutions. */ static void subst_from_stdin (void) { … }