]> granicus.if.org Git - postgresql/blobdiff - src/fe_utils/string_utils.c
Phase 2 of pgindent updates.
[postgresql] / src / fe_utils / string_utils.c
index d1a9ddc4c6cd94543243775c5ae8d98acd264b12..c7e42ddec9fd304ca4acce0835f8cbc104d1feb5 100644 (file)
@@ -425,13 +425,30 @@ appendByteaLiteral(PQExpBuffer buf, const unsigned char *str, size_t length,
  * arguments containing LF or CR characters.  A future major release should
  * reject those characters in CREATE ROLE and CREATE DATABASE, because use
  * there eventually leads to errors here.
+ *
+ * appendShellString() simply prints an error and dies if LF or CR appears.
+ * appendShellStringNoError() omits those characters from the result, and
+ * returns false if there were any.
  */
 void
 appendShellString(PQExpBuffer buf, const char *str)
+{
+       if (!appendShellStringNoError(buf, str))
+       {
+               fprintf(stderr,
+                               _("shell command argument contains a newline or carriage return: \"%s\"\n"),
+                               str);
+               exit(EXIT_FAILURE);
+       }
+}
+
+bool
+appendShellStringNoError(PQExpBuffer buf, const char *str)
 {
 #ifdef WIN32
        int                     backslash_run_length = 0;
 #endif
+       bool            ok = true;
        const char *p;
 
        /*
@@ -442,7 +459,7 @@ appendShellString(PQExpBuffer buf, const char *str)
                strspn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_./:") == strlen(str))
        {
                appendPQExpBufferStr(buf, str);
-               return;
+               return ok;
        }
 
 #ifndef WIN32
@@ -451,10 +468,8 @@ appendShellString(PQExpBuffer buf, const char *str)
        {
                if (*p == '\n' || *p == '\r')
                {
-                       fprintf(stderr,
-                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
-                                       str);
-                       exit(EXIT_FAILURE);
+                       ok = false;
+                       continue;
                }
 
                if (*p == '\'')
@@ -481,10 +496,8 @@ appendShellString(PQExpBuffer buf, const char *str)
        {
                if (*p == '\n' || *p == '\r')
                {
-                       fprintf(stderr,
-                                       _("shell command argument contains a newline or carriage return: \"%s\"\n"),
-                                       str);
-                       exit(EXIT_FAILURE);
+                       ok = false;
+                       continue;
                }
 
                /* Change N backslashes before a double quote to 2N+1 backslashes. */
@@ -523,7 +536,9 @@ appendShellString(PQExpBuffer buf, const char *str)
                backslash_run_length--;
        }
        appendPQExpBufferStr(buf, "^\"");
-#endif   /* WIN32 */
+#endif                                                 /* WIN32 */
+
+       return ok;
 }
 
 
@@ -581,7 +596,7 @@ void
 appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
 {
        const char *s;
-       bool            complex;
+       bool complex;
 
        /*
         * If the name is plain ASCII characters, emit a trivial "\connect "foo"".
@@ -589,6 +604,7 @@ appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
         * general case.  No database has a zero-length name.
         */
        complex = false;
+
        for (s = dbname; *s; s++)
        {
                if (*s == '\n' || *s == '\r')
@@ -697,9 +713,9 @@ parsePGArray(const char *atext, char ***itemarray, int *nitems)
                                        {
                                                atext++;
                                                if (*atext == '\0')
-                                                       return false;           /* premature end of string */
+                                                       return false;   /* premature end of string */
                                        }
-                                       *strings++ = *atext++;          /* copy quoted data */
+                                       *strings++ = *atext++;  /* copy quoted data */
                                }
                                atext++;
                        }