{"thread":{"id":"26634","subject":"[PATCH/RFC 0/2] no-op shell script i18n infrastructure","startedAt":"2011-03-01T22:49:35Z","lastAt":"2011-04-03T15:25:13Z","messageCount":4,"participants":["Ævar Arnfjörð Bjarmason"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"162598","messageId":"1299019777-19033-1-git-send-email-avarab@gmail.com","threadId":"26634","inReplyTo":null,"subject":"[PATCH/RFC 0/2] no-op shell script i18n infrastructure","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2011-03-01T22:49:35Z","receivedAt":"2011-03-01T22:49:35Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"While the no-op C patch series is cooking I thought I'd send an RFC\nfor the no-op shellscript infrastructure. So here (as threatened in\n<AANLkTinUtqJJHNyS9CxrC=VnS87v=GH=pOw9yr4r=pii@mail.gmail.com>[1]) is\nthe infrastructure needed to gettextize the shell script programs.\n\nTo quote my previous E-Mail[1] here are the things that are DONE and\nstill need to be done.\n    \n    Open issues:\n    \n       * Write documentation for git-sh-i18n.sh and git-sh-i18n--envsubst\n         like we have for git-sh-setup (already in WIP form).\n\nIt now has documentation. So this isn't a problem anymore.\n    \n       * git-sh-i18n--envsubst is still too fat:\n    \n            $ ldd -r git-sh-i18n--envsubst\n                linux-vdso.so.1 =>  (0x00007fffc60fd000)\n                libz.so.1 => /usr/lib/libz.so.1 (0x00007f25cff9e000)\n                libcrypto.so.0.9.8 => /usr/lib/libcrypto.so.0.9.8\n    (0x00007f25cfbfd000)\n                libpthread.so.0 => /lib/libpthread.so.0 (0x00007f25cf9e0000)\n                libc.so.6 => /lib/libc.so.6 (0x00007f25cf67f000)\n                libdl.so.2 => /lib/libdl.so.2 (0x00007f25cf47b000)\n                /lib64/ld-linux-x86-64.so.2 (0x00007f25d01c0000)\n    \n         It only needs to link to libc, but I didn't find out when I last\n         checked how to convince the Makefile to only link against\n         that. Help welcome :)\n\nThis is still an issue. It needs to link to libgit for xmalloc() and\nfriends, but it doesn't need libz, libcrypto etc.\n    \n       * Deal with the changes in 92c62a3f4f93432c0c82e3031a9e64e03ba290f7:\n    \n            $ git --no-pager  grep -A1 abomination *.sh\n            git-pull.sh:            # XXX: This is an abomination\n            git-pull.sh-            require_clean_work_tree \"pull with\n    rebase\" \"Please commit or stash them.\"\n    \n         The changes Ramkumar Ramachandra made in 92c62a3f4f, while good,\n         are hard to square with i18n.\n    \n         I think I'll just leave those bits untranslated for now and deal\n         with them later, since I'm trying to keep this minimal.\n\nThis is not part of this RFC series, but I still haven't dealt with it\nin my branch.\n    \n    And then there's the issue that unlike the C patches these will not be\n    a no-op that'll be optimized away by the compiler. We'll be calling an\n    external program for displaying messages. While this is a trivial cost\n    on Unix (especially in the context we're using it, i.e. not in tight\n    loops) it's more expensive on Windows.\n    \n    I don't see any way to deal with that short of implementing some\n    pre-processor, but I think the cost is worth it, but others might\n    disagree of course.\n\nThis is still a problem I think. Although there's been some work on\nthis on the MinGW front from what I can gleam from the mailing list.\n    \n1. http://www.spinics.net/lists/git/msg151971.html\n\nÆvar Arnfjörð Bjarmason (2):\n  git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()\n  git-sh-i18n.sh: add no-op gettext() and eval_gettext() wrappers\n\n .gitignore                              |    3 +\n Documentation/git-sh-i18n--envsubst.txt |   36 +++\n Documentation/git-sh-i18n.txt           |   57 ++++\n Makefile                                |    2 +\n git-sh-i18n.sh                          |   17 ++\n sh-i18n--envsubst.c                     |  444 +++++++++++++++++++++++++++++++\n 6 files changed, 559 insertions(+), 0 deletions(-)\n create mode 100644 Documentation/git-sh-i18n--envsubst.txt\n create mode 100644 Documentation/git-sh-i18n.txt\n create mode 100644 git-sh-i18n.sh\n create mode 100644 sh-i18n--envsubst.c\n\n-- \n1.7.4.1\n"},{"id":"162599","messageId":"1299019777-19033-2-git-send-email-avarab@gmail.com","threadId":"26634","inReplyTo":"1299019777-19033-1-git-send-email-avarab@gmail.com","subject":"[PATCH/RFC 1/2] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2011-03-01T22:49:36Z","receivedAt":"2011-03-01T22:49:36Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Add a git-sh-i18n--envsubst program which is a stripped-down version\nof the GNU envsubst(1) program that comes with GNU gettext for use in\nthe eval_gettext() fallback, instead of using a clever (but broken)\nprintf + eval + printf trick.\n\nIn a previous incarnation of the gettext series I implemented the\neval_gettext() fallback like this:\n\n    eval_gettext() {\n        gettext_out=$(gettext \"$1\")\n        gettext_eval=\"printf '%s' \\\"$gettext_out\\\"\"\n        printf \"%s\" \"`eval \\\"$gettext_eval\\\"`\"\n    }\n\nThis was clever, but would incorrectly handle cases where the variable\nbeing interpolated contained spaces. E.g.:\n\n    cmd=\"git foo\"; eval_gettext \"command: \\$cmd\"\n\nWould emit \"command: gitfoo\", instead of the correct \"command: git\nfoo\". This happened with a message in git-am.sh that used the $cmdline\nvariable.\n\nTo work around this, and to improve our variable expansion behavior\n(eval has security issues) I've imported a stripped-down version of\ngettext's envsubst(1) program. With it eval_gettext() is implemented\nlike this:\n\n    eval_gettext() {\n        printf \"%s\" \"$1\" | (\n            export PATH $(git sh-i18n--envsubst --variables \"$1\");\n            git sh-i18n--envsubst \"$1\"\n        )\n    }\n\nThese are the modifications I made to envsubst.c as I turned it into\nsh-i18n--envsubst.c:\n\n * Added our git-compat-util.h header for xrealloc() and friends.\n\n * Removed inclusion of gettext-specific headers.\n\n * Removed most of main() and replaced it with my own. The modified\n   version only does option parsing for --variables. That's all it\n   needs.\n\n * Modified error() invocations to use our error() instead of\n   error(3).\n\n * Replaced the gettext XNMALLOC(n, size) macro with just\n   xmalloc(n). Since XNMALLOC() only allocated char's.\n\n * Removed the string_list_destroy function. It's redundant (also in\n   the upstream code).\n\n * Replaced the use of stdbool.h (a C99 header) by doing the following\n   replacements on the code:\n\n    * s/bool/unsigned short int/g\n    * s/true/1/g\n    * s/false/0/g\n\nReported-by: Johannes Sixt <j.sixt@viscovery.net>\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n .gitignore                              |    2 +\n Documentation/git-sh-i18n--envsubst.txt |   36 +++\n Makefile                                |    1 +\n sh-i18n--envsubst.c                     |  444 +++++++++++++++++++++++++++++++\n 4 files changed, 483 insertions(+), 0 deletions(-)\n create mode 100644 Documentation/git-sh-i18n--envsubst.txt\n create mode 100644 sh-i18n--envsubst.c\n\ndiff --git a/.gitignore b/.gitignore\nindex c460c66..dad653d 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -127,6 +127,8 @@\n /git-rm\n /git-send-email\n /git-send-pack\n+/git-sh-i18n\n+/git-sh-i18n--envsubst\n /git-sh-setup\n /git-shell\n /git-shortlog\ndiff --git a/Documentation/git-sh-i18n--envsubst.txt b/Documentation/git-sh-i18n--envsubst.txt\nnew file mode 100644\nindex 0000000..e146a2c\n--- /dev/null\n+++ b/Documentation/git-sh-i18n--envsubst.txt\n@@ -0,0 +1,36 @@\n+git-sh-i18n--envsubst(1)\n+========================\n+\n+NAME\n+----\n+git-sh-i18n--envsubst - Git's own envsubst(1) for i18n fallbacks\n+\n+DESCRIPTION\n+-----------\n+\n+This is not a command the end user would want to run.  Ever.\n+This documentation is meant for people who are studying the\n+plumbing scripts and/or are writing new ones.\n+\n+git-sh-i18n--envsubst is Git's stripped-down copy of the GNU\n+`envsubst(1)` program that comes with the GNU gettext\n+package. It's used internally by linkgit:git-sh-i18n[1] to\n+provide the `eval_gettext` fallback on Solaris and systems\n+that don't have a gettext implementation.\n+\n+No promises are made about the interface, or that this\n+program won't disappear without warning in the next version\n+of Git. Don't use it.\n+\n+Author\n+------\n+Written by Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n+\n+Documentation\n+--------------\n+Documentation by Ævar Arnfjörð Bjarmason and the git-list\n+<git@vger.kernel.org>.\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\ndiff --git a/Makefile b/Makefile\nindex 7497835..c002c08 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -419,6 +419,7 @@ PROGRAM_OBJS += shell.o\n PROGRAM_OBJS += show-index.o\n PROGRAM_OBJS += upload-pack.o\n PROGRAM_OBJS += http-backend.o\n+PROGRAM_OBJS += sh-i18n--envsubst.o\n \n PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))\n \ndiff --git a/sh-i18n--envsubst.c b/sh-i18n--envsubst.c\nnew file mode 100644\nindex 0000000..8db71b1\n--- /dev/null\n+++ b/sh-i18n--envsubst.c\n@@ -0,0 +1,444 @@\n+/*\n+ * sh-i18n--envsubst.c - a stripped-down version of gettext's envsubst(1)\n+ *\n+ * Copyright (C) 2010 Ævar Arnfjörð Bjarmason\n+ *\n+ * This is a modified version of\n+ * 67d0871a8c:gettext-runtime/src/envsubst.c from the gettext.git\n+ * repository. It has been stripped down to only implement the\n+ * envsubst(1) features that we need in the git-sh-i18n fallbacks.\n+ *\n+ * The \"Close standard error\" part in main() is from\n+ * 8dac033df0:gnulib-local/lib/closeout.c. The copyright notices for\n+ * both files are reproduced immediately below.\n+ */\n+\n+#include \"git-compat-util.h\"\n+\n+/* Substitution of environment variables in shell format strings.\n+   Copyright (C) 2003-2007 Free Software Foundation, Inc.\n+   Written by Bruno Haible <bruno@clisp.org>, 2003.\n+\n+   This program is free software; you can redistribute it and/or modify\n+   it under the terms of the GNU General Public License as published by\n+   the Free Software Foundation; either version 2, or (at your option)\n+   any later version.\n+\n+   This program is distributed in the hope that it will be useful,\n+   but WITHOUT ANY WARRANTY; without even the implied warranty of\n+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+   GNU General Public License for more details.\n+\n+   You should have received a copy of the GNU General Public License\n+   along with this program; if not, write to the Free Software Foundation,\n+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */\n+\n+/* closeout.c - close standard output and standard error\n+   Copyright (C) 1998-2007 Free Software Foundation, Inc.\n+\n+   This program is free software; you can redistribute it and/or modify\n+   it under the terms of the GNU General Public License as published by\n+   the Free Software Foundation; either version 2, or (at your option)\n+   any later version.\n+\n+   This program is distributed in the hope that it will be useful,\n+   but WITHOUT ANY WARRANTY; without even the implied warranty of\n+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+   GNU General Public License for more details.\n+\n+   You should have received a copy of the GNU General Public License\n+   along with this program; if not, write to the Free Software Foundation,\n+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */\n+\n+#include <errno.h>\n+#include <getopt.h>\n+#include <stdio.h>\n+#include <stdlib.h>\n+#include <string.h>\n+\n+/* If true, substitution shall be performed on all variables.  */\n+static unsigned short int all_variables;\n+\n+/* Forward declaration of local functions.  */\n+static void print_variables (const char *string);\n+static void note_variables (const char *string);\n+static void subst_from_stdin (void);\n+\n+int\n+main (int argc, char *argv[])\n+{\n+  /* Default values for command line options.  */\n+  unsigned short int show_variables = 0;\n+\n+  switch (argc)\n+\t{\n+\tcase 1:\n+\t  error (\"we won't substitute all variables on stdin for you\");\n+\t  /*\n+\t  all_variables = 1;\n+      subst_from_stdin ();\n+\t  */\n+\tcase 2:\n+\t  /* echo '$foo and $bar' | git sh-i18n--envsubst --variables '$foo and $bar' */\n+\t  all_variables = 0;\n+\t  note_variables (argv[1]);\n+      subst_from_stdin ();\n+\t  break;\n+\tcase 3:\n+\t  /* git sh-i18n--envsubst --variables '$foo and $bar' */\n+\t  if (strcmp(argv[1], \"--variables\"))\n+\t\terror (\"first argument must be --variables when two are given\");\n+\t  show_variables = 1;\n+      print_variables (argv[2]);\n+\t  break;\n+\tdefault:\n+\t  error (\"too many arguments\");\n+\t  break;\n+\t}\n+\n+  /* Close standard error.  This is simpler than fwriteerror_no_ebadf, because\n+     upon failure we don't need an errno - all we can do at this point is to\n+     set an exit status.  */\n+  errno = 0;\n+  if (ferror (stderr) || fflush (stderr))\n+    { \n+      fclose (stderr);\n+      exit (EXIT_FAILURE);\n+    }\n+  if (fclose (stderr) && errno != EBADF)\n+    exit (EXIT_FAILURE);\n+\n+  exit (EXIT_SUCCESS);\n+}\n+\n+/* Parse the string and invoke the callback each time a $VARIABLE or\n+   ${VARIABLE} construct is seen, where VARIABLE is a nonempty sequence\n+   of ASCII alphanumeric/underscore characters, starting with an ASCII\n+   alphabetic/underscore character.\n+   We allow only ASCII characters, to avoid dependencies w.r.t. the current\n+   encoding: While \"${\\xe0}\" looks like a variable access in ISO-8859-1\n+   encoding, it doesn't look like one in the BIG5, BIG5-HKSCS, GBK, GB18030,\n+   SHIFT_JIS, JOHAB encodings, because \\xe0\\x7d is a single character in these\n+   encodings.  */\n+static void\n+find_variables (const char *string,\n+\t\tvoid (*callback) (const char *var_ptr, size_t var_len))\n+{\n+  for (; *string != '\\0';)\n+    if (*string++ == '$')\n+      {\n+\tconst char *variable_start;\n+\tconst char *variable_end;\n+\tunsigned short int valid;\n+\tchar c;\n+\n+\tif (*string == '{')\n+\t  string++;\n+\n+\tvariable_start = string;\n+\tc = *string;\n+\tif ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')\n+\t  {\n+\t    do\n+\t      c = *++string;\n+\t    while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')\n+\t\t   || (c >= '0' && c <= '9') || c == '_');\n+\t    variable_end = string;\n+\n+\t    if (variable_start[-1] == '{')\n+\t      {\n+\t\tif (*string == '}')\n+\t\t  {\n+\t\t    string++;\n+\t\t    valid = 1;\n+\t\t  }\n+\t\telse\n+\t\t  valid = 0;\n+\t      }\n+\t    else\n+\t      valid = 1;\n+\n+\t    if (valid)\n+\t      callback (variable_start, variable_end - variable_start);\n+\t  }\n+      }\n+}\n+\n+\n+/* Print a variable to stdout, followed by a newline.  */\n+static void\n+print_variable (const char *var_ptr, size_t var_len)\n+{\n+  fwrite (var_ptr, var_len, 1, stdout);\n+  putchar ('\\n');\n+}\n+\n+/* Print the variables contained in STRING to stdout, each one followed by a\n+   newline.  */\n+static void\n+print_variables (const char *string)\n+{\n+  find_variables (string, &print_variable);\n+}\n+\n+\n+/* Type describing list of immutable strings,\n+   implemented using a dynamic array.  */\n+typedef struct string_list_ty string_list_ty;\n+struct string_list_ty\n+{\n+  const char **item;\n+  size_t nitems;\n+  size_t nitems_max;\n+};\n+\n+/* Initialize an empty list of strings.  */\n+static inline void\n+string_list_init (string_list_ty *slp)\n+{\n+  slp->item = NULL;\n+  slp->nitems = 0;\n+  slp->nitems_max = 0;\n+}\n+\n+/* Append a single string to the end of a list of strings.  */\n+static inline void\n+string_list_append (string_list_ty *slp, const char *s)\n+{\n+  /* Grow the list.  */\n+  if (slp->nitems >= slp->nitems_max)\n+    {\n+      size_t nbytes;\n+\n+      slp->nitems_max = slp->nitems_max * 2 + 4;\n+      nbytes = slp->nitems_max * sizeof (slp->item[0]);\n+      slp->item = (const char **) xrealloc (slp->item, nbytes);\n+    }\n+\n+  /* Add the string to the end of the list.  */\n+  slp->item[slp->nitems++] = s;\n+}\n+\n+/* Compare two strings given by reference.  */\n+static int\n+cmp_string (const void *pstr1, const void *pstr2)\n+{\n+  const char *str1 = *(const char **)pstr1;\n+  const char *str2 = *(const char **)pstr2;\n+\n+  return strcmp (str1, str2);\n+}\n+\n+/* Sort a list of strings.  */\n+static inline void\n+string_list_sort (string_list_ty *slp)\n+{\n+  if (slp->nitems > 0)\n+    qsort (slp->item, slp->nitems, sizeof (slp->item[0]), cmp_string);\n+}\n+\n+/* Test whether a string list contains a given string.  */\n+static inline int\n+string_list_member (const string_list_ty *slp, const char *s)\n+{\n+  size_t j;\n+\n+  for (j = 0; j < slp->nitems; ++j)\n+    if (strcmp (slp->item[j], s) == 0)\n+      return 1;\n+  return 0;\n+}\n+\n+/* Test whether a sorted string list contains a given string.  */\n+static int\n+sorted_string_list_member (const string_list_ty *slp, const char *s)\n+{\n+  size_t j1, j2;\n+\n+  j1 = 0;\n+  j2 = slp->nitems;\n+  if (j2 > 0)\n+    {\n+      /* Binary search.  */\n+      while (j2 - j1 > 1)\n+\t{\n+\t  /* Here we know that if s is in the list, it is at an index j\n+\t     with j1 <= j < j2.  */\n+\t  size_t j = (j1 + j2) >> 1;\n+\t  int result = strcmp (slp->item[j], s);\n+\n+\t  if (result > 0)\n+\t    j2 = j;\n+\t  else if (result == 0)\n+\t    return 1;\n+\t  else\n+\t    j1 = j + 1;\n+\t}\n+      if (j2 > j1)\n+\tif (strcmp (slp->item[j1], s) == 0)\n+\t  return 1;\n+    }\n+  return 0;\n+}\n+\n+\n+/* Set of variables on which to perform substitution.\n+   Used only if !all_variables.  */\n+static string_list_ty variables_set;\n+\n+/* Adds a variable to variables_set.  */\n+static void\n+note_variable (const char *var_ptr, size_t var_len)\n+{\n+  char *string = xmalloc (var_len + 1);\n+  memcpy (string, var_ptr, var_len);\n+  string[var_len] = '\\0';\n+\n+  string_list_append (&variables_set, string);\n+}\n+\n+/* Stores the variables occurring in the string in variables_set.  */\n+static void\n+note_variables (const char *string)\n+{\n+  string_list_init (&variables_set);\n+  find_variables (string, &note_variable);\n+  string_list_sort (&variables_set);\n+}\n+\n+\n+static int\n+do_getc ()\n+{\n+  int c = getc (stdin);\n+\n+  if (c == EOF)\n+    {\n+      if (ferror (stdin))\n+\terror (\"error while reading standard input\");\n+    }\n+\n+  return c;\n+}\n+\n+static inline void\n+do_ungetc (int c)\n+{\n+  if (c != EOF)\n+    ungetc (c, stdin);\n+}\n+\n+/* Copies stdin to stdout, performing substitutions.  */\n+static void\n+subst_from_stdin ()\n+{\n+  static char *buffer;\n+  static size_t bufmax;\n+  static size_t buflen;\n+  int c;\n+\n+  for (;;)\n+    {\n+      c = do_getc ();\n+      if (c == EOF)\n+\tbreak;\n+      /* Look for $VARIABLE or ${VARIABLE}.  */\n+      if (c == '$')\n+\t{\n+\t  unsigned short int opening_brace = 0;\n+\t  unsigned short int closing_brace = 0;\n+\n+\t  c = do_getc ();\n+\t  if (c == '{')\n+\t    {\n+\t      opening_brace = 1;\n+\t      c = do_getc ();\n+\t    }\n+\t  if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')\n+\t    {\n+\t      unsigned short int valid;\n+\n+\t      /* Accumulate the VARIABLE in buffer.  */\n+\t      buflen = 0;\n+\t      do\n+\t\t{\n+\t\t  if (buflen >= bufmax)\n+\t\t    {\n+\t\t      bufmax = 2 * bufmax + 10;\n+\t\t      buffer = xrealloc (buffer, bufmax);\n+\t\t    }\n+\t\t  buffer[buflen++] = c;\n+\n+\t\t  c = do_getc ();\n+\t\t}\n+\t      while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')\n+\t\t     || (c >= '0' && c <= '9') || c == '_');\n+\n+\t      if (opening_brace)\n+\t\t{\n+\t\t  if (c == '}')\n+\t\t    {\n+\t\t      closing_brace = 1;\n+\t\t      valid = 1;\n+\t\t    }\n+\t\t  else\n+\t\t    {\n+\t\t      valid = 0;\n+\t\t      do_ungetc (c);\n+\t\t    }\n+\t\t}\n+\t      else\n+\t\t{\n+\t\t  valid = 1;\n+\t\t  do_ungetc (c);\n+\t\t}\n+\n+\t      if (valid)\n+\t\t{\n+\t\t  /* Terminate the variable in the buffer.  */\n+\t\t  if (buflen >= bufmax)\n+\t\t    {\n+\t\t      bufmax = 2 * bufmax + 10;\n+\t\t      buffer = xrealloc (buffer, bufmax);\n+\t\t    }\n+\t\t  buffer[buflen] = '\\0';\n+\n+\t\t  /* Test whether the variable shall be substituted.  */\n+\t\t  if (!all_variables\n+\t\t      && !sorted_string_list_member (&variables_set, buffer))\n+\t\t    valid = 0;\n+\t\t}\n+\n+\t      if (valid)\n+\t\t{\n+\t\t  /* Substitute the variable's value from the environment.  */\n+\t\t  const char *env_value = getenv (buffer);\n+\n+\t\t  if (env_value != NULL)\n+\t\t    fputs (env_value, stdout);\n+\t\t}\n+\t      else\n+\t\t{\n+\t\t  /* Perform no substitution at all.  Since the buffered input\n+\t\t     contains no other '$' than at the start, we can just\n+\t\t     output all the buffered contents.  */\n+\t\t  putchar ('$');\n+\t\t  if (opening_brace)\n+\t\t    putchar ('{');\n+\t\t  fwrite (buffer, buflen, 1, stdout);\n+\t\t  if (closing_brace)\n+\t\t    putchar ('}');\n+\t\t}\n+\t    }\n+\t  else\n+\t    {\n+\t      do_ungetc (c);\n+\t      putchar ('$');\n+\t      if (opening_brace)\n+\t\tputchar ('{');\n+\t    }\n+\t}\n+      else\n+\tputchar (c);\n+    }\n+}\n-- \n1.7.4.1\n"},{"id":"162600","messageId":"1299019777-19033-3-git-send-email-avarab@gmail.com","threadId":"26634","inReplyTo":"1299019777-19033-1-git-send-email-avarab@gmail.com","subject":"[PATCH/RFC 2/2] git-sh-i18n.sh: add no-op gettext() and eval_gettext() wrappers","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2011-03-01T22:49:37Z","receivedAt":"2011-03-01T22:49:37Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Add a no-op wrapper library for Git's shell scripts. To split up the\ngettext series I'm first submitting patches to gettextize the source\ntree before I add any of the Makefile and Shell library changes needed\nto actually use them.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n\nThis documentation doesn't accurately reflect what it does *now*, but\nwhat it'll do eventually later in the series.\n\n .gitignore                    |    1 +\n Documentation/git-sh-i18n.txt |   57 +++++++++++++++++++++++++++++++++++++++++\n Makefile                      |    1 +\n git-sh-i18n.sh                |   17 ++++++++++++\n 4 files changed, 76 insertions(+), 0 deletions(-)\n create mode 100644 Documentation/git-sh-i18n.txt\n create mode 100644 git-sh-i18n.sh\n\ndiff --git a/.gitignore b/.gitignore\nindex dad653d..e9ab0af 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -130,6 +130,7 @@\n /git-sh-i18n\n /git-sh-i18n--envsubst\n /git-sh-setup\n+/git-sh-i18n\n /git-shell\n /git-shortlog\n /git-show\ndiff --git a/Documentation/git-sh-i18n.txt b/Documentation/git-sh-i18n.txt\nnew file mode 100644\nindex 0000000..b41b3af\n--- /dev/null\n+++ b/Documentation/git-sh-i18n.txt\n@@ -0,0 +1,57 @@\n+git-sh-i18n(1)\n+==============\n+\n+NAME\n+----\n+git-sh-i18n - Git's i18n setup code for shell scripts\n+\n+SYNOPSIS\n+--------\n+'. \"$(git --exec-path)/git-sh-i18n\"'\n+\n+DESCRIPTION\n+-----------\n+\n+This is not a command the end user would want to run.  Ever.\n+This documentation is meant for people who are studying the\n+Porcelain-ish scripts and/or are writing new ones.\n+\n+The 'git sh-i18n scriptlet is designed to be sourced (using\n+`.`) by Git's porcelain programs implemented in shell\n+script. It provides wrappers for the GNU `gettext` and\n+`eval_gettext` functions accessible through the `gettext.sh`\n+script, and provides pass-through fallbacks on systems\n+without GNU gettext.\n+\n+FUNCTIONS\n+---------\n+\n+gettext::\n+\tOn GNU systems this will be the `gettext` function from\n+\t`gettext.sh`, on Solaris it`ll be the `gettext(1)`\n+\tcommand.\n+\n+\tIf neither of those implementations are available a\n+\tdummy fall-through function is provided.\n+    \n+eval_gettext::\n+\tOn GNU systems this will be the `eval_gettext` function\n+\tfrom `gettext.sh`, on Solaris we provide an\n+\t`eval_gettext` using the\n+\tlinkgit:git-sh-i18n--envsubst[1] helper.\n+\n+\tIf neither of those implementations are available a\n+\tdummy fall-through function is provided.\n+\n+Author\n+------\n+Written by Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n+\n+Documentation\n+--------------\n+Documentation by Ævar Arnfjörð Bjarmason and the git-list\n+<git@vger.kernel.org>.\n+\n+GIT\n+---\n+Part of the linkgit:git[1] suite\ndiff --git a/Makefile b/Makefile\nindex c002c08..f5fa8a6 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -386,6 +386,7 @@ SCRIPT_SH += git-web--browse.sh\n SCRIPT_LIB += git-mergetool--lib\n SCRIPT_LIB += git-parse-remote\n SCRIPT_LIB += git-sh-setup\n+SCRIPT_LIB += git-sh-i18n\n \n SCRIPT_PERL += git-add--interactive.perl\n SCRIPT_PERL += git-difftool.perl\ndiff --git a/git-sh-i18n.sh b/git-sh-i18n.sh\nnew file mode 100644\nindex 0000000..ea05e16\n--- /dev/null\n+++ b/git-sh-i18n.sh\n@@ -0,0 +1,17 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason\n+#\n+# This is a skeleton no-op implementation of gettext for Git. It'll be\n+# replaced by something that uses gettext.sh in a future patch series.\n+\n+gettext () {\n+\tprintf \"%s\" \"$1\"\n+}\n+\n+eval_gettext () {\n+\tprintf \"%s\" \"$1\" | (\n+\t\texport PATH $(git sh-i18n--envsubst --variables \"$1\");\n+\t\tgit sh-i18n--envsubst \"$1\"\n+\t)\n+}\n-- \n1.7.4.1\n"},{"id":"164999","messageId":"BANLkTikf0s=tyGSnmgf1zmRAe3WnzB4JUA@mail.gmail.com","threadId":"26634","inReplyTo":"7vpqq99i11.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH/RFC 1/2] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2011-04-03T15:25:13Z","receivedAt":"2011-04-03T15:25:13Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"On Thu, Mar 3, 2011 at 01:14, Junio C Hamano <gitster@pobox.com> wrote:\n> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:\n>\n>> In a previous incarnation of the gettext series I implemented the\n>> eval_gettext() fallback like this:\n>>\n>>     eval_gettext() {\n>>         gettext_out=$(gettext \"$1\")\n>>         gettext_eval=\"printf '%s' \\\"$gettext_out\\\"\"\n>>         printf \"%s\" \"`eval \\\"$gettext_eval\\\"`\"\n>>     }\n>>\n>> This was clever, but would incorrectly handle cases where the variable\n>> being interpolated contained spaces. E.g.:\n>>\n>>     cmd=\"git foo\"; eval_gettext \"command: \\$cmd\"\n>>\n>> Would emit \"command: gitfoo\", instead of the correct \"command: git\n>> foo\".\n>\n> Hmm, are you sure you got your quoting right?\n>\n>    $ cat >1.sh <<\\EOF\n>    #!/bin/sh\n>    gettext () {\n>            case \"$1\" in\n>            'command: $cmd') echo 'dcomman: $cmd' ;;\n>            *) echo \"GETTEXT POISON\" ;;\n>            esac\n>    }\n>\n>    eval_gettext() {\n>            gettext_out=$(gettext \"$1\") &&\n>            gettext_eval=\"printf '%s' \\\"$gettext_out\\\"\" &&\n>            gettext_cmd=$(eval \"$gettext_eval\") &&\n>            printf \"%s\" \"$gettext_cmd\"\n>    }\n>\n>    cmd=\"git foo\"\n>    eval_gettext \"command: \\$cmd\"\n>    EOF\n>    $ sh -x 1.sh\n>    + cmd='git foo'\n>    + eval_gettext 'command: $cmd'\n>    ++ gettext 'command: $cmd'\n>    ++ case \"$1\" in\n>    ++ echo 'dcomman: $cmd'\n>    + gettext_out='dcomman: $cmd'\n>    + gettext_eval='printf '\\''%s'\\'' \"dcomman: $cmd\"'\n>    ++ eval 'printf '\\''%s'\\'' \"dcomman: $cmd\"'\n>    +++ printf %s 'dcomman: git foo'\n>    + gettext_cmd='dcomman: git foo'\n>    + printf %s 'dcomman: git foo'\n>    dcomman: git foo\n>\n> Am I grossly missing something from what you are trying to do here?\n\n(CC-ing the list again, so there's a record of this in the mail\narchive).\n\nYour eval_gettext() is better, but it still fails in cases where the\nstring contains \"'s. E.g.:\n\n    test_expect_success C_LOCALE_OUTPUT 'eval_gettext: our\neval_gettext() fallback can interpolate variables with spaces in them'\n'\n        cmdline=\"git am\" &&\n        export cmdline;\n        printf \"When you have resolved this problem run \\\"git am\n--resolved\\\".\" >expect &&\n        eval_gettext \"When you have resolved this problem run\n\\\"\\$cmdline --resolved\\\".\" >actual\n        test_cmp expect actual\n    '\n\nIf this were just:\n\n    test_expect_success C_LOCALE_OUTPUT 'eval_gettext: our\neval_gettext() fallback can interpolate variables with spaces in them'\n'\n        cmdline=\"git am\" &&\n        export cmdline;\n        printf \"When you have resolved this problem run git am\n--resolved.\" >expect &&\n        eval_gettext \"When you have resolved this problem run\n\\$cmdline --resolved.\" >actual\n        test_cmp expect actual\n    '\n\nYour version would work.\n\nMaybe there's some portable version of doing this that works with\nvariables with spaces mixed with quotes. But unless someone can point\nit out I'll submit a series with git-sh-i18n--envsubst and tests\ndemonstrating why it's needed soon.q\n"}]}