Blame gnulib-tests/test-quotearg-simple.c

Packit 33f14e
/* Test of quotearg family of functions.
Packit 33f14e
   Copyright (C) 2008-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This program is free software; you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation; either version 3, or (at your option)
Packit 33f14e
   any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
/* Written by Eric Blake <ebb9@byu.net>, 2008.  */
Packit 33f14e
Packit 33f14e
#include <config.h>
Packit 33f14e
Packit 33f14e
#include "quotearg.h"
Packit 33f14e
Packit 33f14e
#include <ctype.h>
Packit 33f14e
#include <stdbool.h>
Packit 33f14e
#include <stdint.h>
Packit 33f14e
#include <stdlib.h>
Packit 33f14e
#include <string.h>
Packit 33f14e
Packit 33f14e
#include "localcharset.h"
Packit 33f14e
#include "macros.h"
Packit 33f14e
#include "zerosize-ptr.h"
Packit 33f14e
Packit 33f14e
#include "test-quotearg.h"
Packit 33f14e
Packit 33f14e
static struct result_groups results_g[] = {
Packit 33f14e
  /* literal_quoting_style */
Packit 33f14e
  { { "", "\0""1\0", 3, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
Packit 33f14e
      "a' b", LQ RQ, LQ RQ },
Packit 33f14e
    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
Packit 33f14e
      "a' b", LQ RQ, LQ RQ },
Packit 33f14e
    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b",
Packit 33f14e
      "a' b", LQ RQ, LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* shell_quoting_style */
Packit 33f14e
  { { "''", "\0""1\0", 3, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
Packit 33f14e
      "'a\\b'", "\"a' b\"", LQ RQ, LQ RQ },
Packit 33f14e
    { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "a:b",
Packit 33f14e
      "'a\\b'", "\"a' b\"", LQ RQ, LQ RQ },
Packit 33f14e
    { "''", "1", 1, "simple", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", LQ RQ, LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* shell_always_quoting_style */
Packit 33f14e
  { { "''", "'\0""1\0'", 5, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "'" LQ RQ "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "'" LQ RQ "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "'1'", 3, "'simple'", "' \t\n'\\''\"\033?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "'" LQ RQ "'", "'" LQ RQ "'" } },
Packit 33f14e
Packit 33f14e
  /* shell_escape_quoting_style */
Packit 33f14e
  { { "''", "''$'\\0''1'$'\\0'", 15, "simple",
Packit 33f14e
      "' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "a:b",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", LQ RQ },
Packit 33f14e
    { "''", "''$'\\0''1'$'\\0'", 15, "simple",
Packit 33f14e
      "' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "a:b",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", LQ RQ },
Packit 33f14e
    { "''", "''$'\\0''1'$'\\0'", 15, "simple",
Packit 33f14e
      "' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* shell_escape_always_quoting_style */
Packit 33f14e
  { { "''", "''$'\\0''1'$'\\0'", 15, "'simple'",
Packit 33f14e
      "' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "''$'\\0''1'$'\\0'", 15, "'simple'",
Packit 33f14e
      "' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "''$'\\0''1'$'\\0'", 15, "'simple'",
Packit 33f14e
      "' '$'\\t\\n'\\''\"'$'\\033''?""?/\\'", "'a:b'",
Packit 33f14e
      "'a\\b'", "\"a' b\"", "''$'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" } },
Packit 33f14e
Packit 33f14e
  /* c_quoting_style */
Packit 33f14e
  { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
Packit 33f14e
    { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
Packit 33f14e
    { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" } },
Packit 33f14e
Packit 33f14e
  /* c_maybe_quoting_style */
Packit 33f14e
  { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
Packit 33f14e
      "a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
Packit 33f14e
    { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
Packit 33f14e
      "a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
Packit 33f14e
    { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
Packit 33f14e
      "\"a:b\"", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* escape_quoting_style */
Packit 33f14e
  { { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
Packit 33f14e
      "a\\\\b", "a' b", LQ_ENC RQ_ENC, LQ RQ },
Packit 33f14e
    { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a:b",
Packit 33f14e
      "a\\\\b", "a' b", LQ_ENC RQ_ENC, LQ RQ },
Packit 33f14e
    { "", "\\0001\\0", 7, "simple", " \\t\\n'\"\\033?""?/\\\\", "a\\:b",
Packit 33f14e
      "a\\\\b", "a' b", LQ_ENC RQ_ENC, LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* locale_quoting_style */
Packit 33f14e
  { { "''", "'\\0001\\0'", 9, "'simple'", "' \\t\\n\\'\"\\033?""?/\\\\'",
Packit 33f14e
      "'a:b'", "'a\\\\b'", "'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "'\\0001\\0'", 9, "'simple'", "' \\t\\n\\'\"\\033?""?/\\\\'",
Packit 33f14e
      "'a:b'", "'a\\\\b'", "'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "'\\0001\\0'", 9, "'simple'", "' \\t\\n\\'\"\\033?""?/\\\\'",
Packit 33f14e
      "'a\\:b'", "'a\\\\b'", "'a\\' b'",
Packit 33f14e
      "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" } },
Packit 33f14e
Packit 33f14e
  /* clocale_quoting_style */
Packit 33f14e
  { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
Packit 33f14e
    { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
Packit 33f14e
    { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?""?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" } }
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
static struct result_groups flag_results[] = {
Packit 33f14e
  /* literal_quoting_style and QA_ELIDE_NULL_BYTES */
Packit 33f14e
  { { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", "a' b",
Packit 33f14e
      LQ RQ, LQ RQ },
Packit 33f14e
    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", "a' b",
Packit 33f14e
      LQ RQ, LQ RQ },
Packit 33f14e
    { "", "1", 1, "simple", " \t\n'\"\033?""?/\\", "a:b", "a\\b", "a' b",
Packit 33f14e
      LQ RQ, LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* c_quoting_style and QA_ELIDE_OUTER_QUOTES */
Packit 33f14e
  { { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
Packit 33f14e
      "a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
Packit 33f14e
    { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
Packit 33f14e
      "a:b", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ },
Packit 33f14e
    { "", "\"\\0001\\0\"", 9, "simple", "\" \\t\\n'\\\"\\033?""?/\\\\\"",
Packit 33f14e
      "\"a:b\"", "a\\b", "a' b", "\"" LQ_ENC RQ_ENC "\"", LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* c_quoting_style and QA_SPLIT_TRIGRAPHS */
Packit 33f14e
  { { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
Packit 33f14e
    { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" },
Packit 33f14e
    { "\"\"", "\"\\0001\\0\"", 9, "\"simple\"",
Packit 33f14e
      "\" \\t\\n'\\\"\\033?\"\"?/\\\\\"", "\"a\\:b\"", "\"a\\\\b\"",
Packit 33f14e
      "\"a' b\"", "\"" LQ_ENC RQ_ENC "\"", "\"" LQ RQ "\"" } }
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
static char const *custom_quotes[][2] = {
Packit 33f14e
  { "", ""  },
Packit 33f14e
  { "'", "'"  },
Packit 33f14e
  { "(", ")"  },
Packit 33f14e
  { ":", " "  },
Packit 33f14e
  { " ", ":"  },
Packit 33f14e
  { "# ", "\n" },
Packit 33f14e
  { "\"'", "'\"" }
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
static struct result_groups custom_results[] = {
Packit 33f14e
  /* left_quote = right_quote = "" */
Packit 33f14e
  { { "", "\\0001\\0", 7, "simple",
Packit 33f14e
      " \\t\\n'\"\\033?""?/\\\\", "a:b", "a\\\\b",
Packit 33f14e
      "a' b", LQ_ENC RQ_ENC, LQ RQ },
Packit 33f14e
    { "", "\\0001\\0", 7, "simple",
Packit 33f14e
      " \\t\\n'\"\\033?""?/\\\\", "a:b", "a\\\\b",
Packit 33f14e
      "a' b", LQ_ENC RQ_ENC, LQ RQ },
Packit 33f14e
    { "", "\\0001\\0", 7, "simple",
Packit 33f14e
      " \\t\\n'\"\\033?""?/\\\\", "a\\:b", "a\\\\b",
Packit 33f14e
      "a' b", LQ_ENC RQ_ENC, LQ RQ } },
Packit 33f14e
Packit 33f14e
  /* left_quote = right_quote = "'" */
Packit 33f14e
  { { "''", "'\\0001\\0'", 9, "'simple'",
Packit 33f14e
      "' \\t\\n\\'\"\\033?""?/\\\\'", "'a:b'", "'a\\\\b'",
Packit 33f14e
      "'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "'\\0001\\0'", 9, "'simple'",
Packit 33f14e
      "' \\t\\n\\'\"\\033?""?/\\\\'", "'a:b'", "'a\\\\b'",
Packit 33f14e
      "'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" },
Packit 33f14e
    { "''", "'\\0001\\0'", 9, "'simple'",
Packit 33f14e
      "' \\t\\n\\'\"\\033?""?/\\\\'", "'a\\:b'", "'a\\\\b'",
Packit 33f14e
      "'a\\' b'", "'" LQ_ENC RQ_ENC "'", "'" LQ RQ "'" } },
Packit 33f14e
Packit 33f14e
  /* left_quote = "(" and right_quote = ")" */
Packit 33f14e
  { { "()", "(\\0001\\0)", 9, "(simple)",
Packit 33f14e
      "( \\t\\n'\"\\033?""?/\\\\)", "(a:b)", "(a\\\\b)",
Packit 33f14e
      "(a' b)", "(" LQ_ENC RQ_ENC ")", "(" LQ RQ ")" },
Packit 33f14e
    { "()", "(\\0001\\0)", 9, "(simple)",
Packit 33f14e
      "( \\t\\n'\"\\033?""?/\\\\)", "(a:b)", "(a\\\\b)",
Packit 33f14e
      "(a' b)", "(" LQ_ENC RQ_ENC ")", "(" LQ RQ ")" },
Packit 33f14e
    { "()", "(\\0001\\0)", 9, "(simple)",
Packit 33f14e
      "( \\t\\n'\"\\033?""?/\\\\)", "(a\\:b)", "(a\\\\b)",
Packit 33f14e
      "(a' b)", "(" LQ_ENC RQ_ENC ")", "(" LQ RQ ")" } },
Packit 33f14e
Packit 33f14e
  /* left_quote = ":" and right_quote = " " */
Packit 33f14e
  { { ": ", ":\\0001\\0 ", 9, ":simple ",
Packit 33f14e
      ":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a:b ", ":a\\\\b ",
Packit 33f14e
      ":a'\\ b ", ":" LQ_ENC RQ_ENC " ", ":" LQ RQ " " },
Packit 33f14e
    { ": ", ":\\0001\\0 ", 9, ":simple ",
Packit 33f14e
      ":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a:b ", ":a\\\\b ",
Packit 33f14e
      ":a'\\ b ", ":" LQ_ENC RQ_ENC " ", ":" LQ RQ " " },
Packit 33f14e
    { ": ", ":\\0001\\0 ", 9, ":simple ",
Packit 33f14e
      ":\\ \\t\\n'\"\\033?""?/\\\\ ", ":a\\:b ", ":a\\\\b ",
Packit 33f14e
      ":a'\\ b ", ":" LQ_ENC RQ_ENC " ", ":" LQ RQ " " } },
Packit 33f14e
Packit 33f14e
  /* left_quote = " " and right_quote = ":" */
Packit 33f14e
  { { " :", " \\0001\\0:", 9, " simple:",
Packit 33f14e
      "  \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
Packit 33f14e
      " a' b:", " " LQ_ENC RQ_ENC ":", " " LQ RQ ":" },
Packit 33f14e
    { " :", " \\0001\\0:", 9, " simple:",
Packit 33f14e
      "  \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
Packit 33f14e
      " a' b:", " " LQ_ENC RQ_ENC ":", " " LQ RQ ":" },
Packit 33f14e
    { " :", " \\0001\\0:", 9, " simple:",
Packit 33f14e
      "  \\t\\n'\"\\033?""?/\\\\:", " a\\:b:", " a\\\\b:",
Packit 33f14e
      " a' b:", " " LQ_ENC RQ_ENC ":", " " LQ RQ ":" } },
Packit 33f14e
Packit 33f14e
  /* left_quote = "# " and right_quote = "\n" */
Packit 33f14e
  { { "# \n", "# \\0001\\0\n", 10, "# simple\n",
Packit 33f14e
      "#  \\t\\n'\"\\033?""?/\\\\\n", "# a:b\n", "# a\\\\b\n",
Packit 33f14e
      "# a' b\n", "# " LQ_ENC RQ_ENC "\n", "# " LQ RQ "\n" },
Packit 33f14e
    { "# \n", "# \\0001\\0\n", 10, "# simple\n",
Packit 33f14e
      "#  \\t\\n'\"\\033?""?/\\\\\n", "# a:b\n", "# a\\\\b\n",
Packit 33f14e
      "# a' b\n", "# " LQ_ENC RQ_ENC "\n", "# " LQ RQ "\n" },
Packit 33f14e
    { "# \n", "# \\0001\\0\n", 10, "# simple\n",
Packit 33f14e
      "#  \\t\\n'\"\\033?""?/\\\\\n", "# a\\:b\n", "# a\\\\b\n",
Packit 33f14e
      "# a' b\n", "# " LQ_ENC RQ_ENC "\n", "# " LQ RQ "\n" } },
Packit 33f14e
Packit 33f14e
  /* left_quote = "\"'" and right_quote = "'\"" */
Packit 33f14e
  { { "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
Packit 33f14e
      "\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a:b'\"", "\"'a\\\\b'\"",
Packit 33f14e
      "\"'a' b'\"", "\"'" LQ_ENC RQ_ENC "'\"", "\"'" LQ RQ "'\"" },
Packit 33f14e
    { "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
Packit 33f14e
      "\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a:b'\"", "\"'a\\\\b'\"",
Packit 33f14e
      "\"'a' b'\"", "\"'" LQ_ENC RQ_ENC "'\"", "\"'" LQ RQ "'\"" },
Packit 33f14e
    { "\"''\"", "\"'\\0001\\0'\"", 11, "\"'simple'\"",
Packit 33f14e
      "\"' \\t\\n\\'\"\\033?""?/\\\\'\"", "\"'a\\:b'\"", "\"'a\\\\b'\"",
Packit 33f14e
      "\"'a' b'\"", "\"'" LQ_ENC RQ_ENC "'\"", "\"'" LQ RQ "'\"" } }
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
static char *
Packit 33f14e
use_quote_double_quotes (const char *str, size_t *len)
Packit 33f14e
{
Packit 33f14e
  char *p = *len == SIZE_MAX ? quotearg_char (str, '"')
Packit 33f14e
                               : quotearg_char_mem (str, *len, '"');
Packit 33f14e
  *len = strlen (p);
Packit 33f14e
  return p;
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
int
Packit 33f14e
main (int argc _GL_UNUSED, char *argv[])
Packit 33f14e
{
Packit 33f14e
  int i;
Packit 33f14e
  bool ascii_only = MB_CUR_MAX == 1 && !isprint ((unsigned char) LQ[0]);
Packit 33f14e
Packit 33f14e
  /* This part of the program is hard-wired to the C locale since it
Packit 33f14e
     does not call setlocale.  However, according to POSIX, the use of
Packit 33f14e
     8-bit bytes in a character context in the C locale gives
Packit 33f14e
     unspecified results (that is, the C locale charset is allowed to
Packit 33f14e
     be unibyte with 8-bit bytes rejected [ASCII], unibyte with 8-bit
Packit 33f14e
     bytes being characters [often ISO-8859-1], or multibyte [often
Packit 33f14e
     UTF-8]).  We assume that the latter two cases will be
Packit 33f14e
     indistinguishable in this test - that is, the LQ and RQ sequences
Packit 33f14e
     will pass through unchanged in either type of charset.  So when
Packit 33f14e
     testing for quoting of str7, use the ascii_only flag to decide
Packit 33f14e
     what to expect for the 8-bit data being quoted.  */
Packit 33f14e
  ASSERT (!isprint ('\033'));
Packit 33f14e
  for (i = literal_quoting_style; i <= clocale_quoting_style; i++)
Packit 33f14e
    {
Packit 33f14e
      set_quoting_style (NULL, (enum quoting_style) i);
Packit 33f14e
      if (!(i == locale_quoting_style || i == clocale_quoting_style)
Packit 33f14e
          || (strcmp (locale_charset (), "ASCII") == 0
Packit 33f14e
              || strcmp (locale_charset (), "ANSI_X3.4-1968") == 0))
Packit 33f14e
        {
Packit 33f14e
          compare_strings (use_quotearg_buffer, &results_g[i].group1,
Packit 33f14e
                           ascii_only);
Packit 33f14e
          compare_strings (use_quotearg, &results_g[i].group2,
Packit 33f14e
                           ascii_only);
Packit 33f14e
          if (i == c_quoting_style)
Packit 33f14e
            compare_strings (use_quote_double_quotes, &results_g[i].group2,
Packit 33f14e
                             ascii_only);
Packit 33f14e
          compare_strings (use_quotearg_colon, &results_g[i].group3,
Packit 33f14e
                           ascii_only);
Packit 33f14e
        }
Packit 33f14e
    }
Packit 33f14e
Packit 33f14e
  set_quoting_style (NULL, literal_quoting_style);
Packit 33f14e
  ASSERT (set_quoting_flags (NULL, QA_ELIDE_NULL_BYTES) == 0);
Packit 33f14e
  compare_strings (use_quotearg_buffer, &flag_results[0].group1, ascii_only);
Packit 33f14e
  compare_strings (use_quotearg, &flag_results[0].group2, ascii_only);
Packit 33f14e
  compare_strings (use_quotearg_colon, &flag_results[0].group3, ascii_only);
Packit 33f14e
Packit 33f14e
  set_quoting_style (NULL, c_quoting_style);
Packit 33f14e
  ASSERT (set_quoting_flags (NULL, QA_ELIDE_OUTER_QUOTES)
Packit 33f14e
          == QA_ELIDE_NULL_BYTES);
Packit 33f14e
  compare_strings (use_quotearg_buffer, &flag_results[1].group1, ascii_only);
Packit 33f14e
  compare_strings (use_quotearg, &flag_results[1].group2, ascii_only);
Packit 33f14e
  compare_strings (use_quote_double_quotes, &flag_results[1].group2,
Packit 33f14e
                   ascii_only);
Packit 33f14e
  compare_strings (use_quotearg_colon, &flag_results[1].group3, ascii_only);
Packit 33f14e
Packit 33f14e
  ASSERT (set_quoting_flags (NULL, QA_SPLIT_TRIGRAPHS)
Packit 33f14e
          == QA_ELIDE_OUTER_QUOTES);
Packit 33f14e
  compare_strings (use_quotearg_buffer, &flag_results[2].group1, ascii_only);
Packit 33f14e
  compare_strings (use_quotearg, &flag_results[2].group2, ascii_only);
Packit 33f14e
  compare_strings (use_quote_double_quotes, &flag_results[2].group2,
Packit 33f14e
                   ascii_only);
Packit 33f14e
  compare_strings (use_quotearg_colon, &flag_results[2].group3, ascii_only);
Packit 33f14e
Packit 33f14e
  ASSERT (set_quoting_flags (NULL, 0) == QA_SPLIT_TRIGRAPHS);
Packit 33f14e
Packit 33f14e
  for (i = 0; i < sizeof custom_quotes / sizeof *custom_quotes; ++i)
Packit 33f14e
    {
Packit 33f14e
      set_custom_quoting (NULL,
Packit 33f14e
                          custom_quotes[i][0], custom_quotes[i][1]);
Packit 33f14e
      compare_strings (use_quotearg_buffer, &custom_results[i].group1,
Packit 33f14e
                       ascii_only);
Packit 33f14e
      compare_strings (use_quotearg, &custom_results[i].group2, ascii_only);
Packit 33f14e
      compare_strings (use_quotearg_colon, &custom_results[i].group3,
Packit 33f14e
                       ascii_only);
Packit 33f14e
    }
Packit 33f14e
Packit 33f14e
  {
Packit 33f14e
    /* Trigger the bug whereby quotearg_buffer would read beyond the NUL
Packit 33f14e
       that defines the end of the string being quoted.  Use an input
Packit 33f14e
       string whose NUL is the last byte before an unreadable page.  */
Packit 33f14e
    char *z = zerosize_ptr ();
Packit 33f14e
Packit 33f14e
    if (z)
Packit 33f14e
      {
Packit 33f14e
        size_t q_len = 1024;
Packit 33f14e
        char *q = malloc (q_len + 1);
Packit 33f14e
        char buf[10];
Packit 33f14e
        memset (q, 'Q', q_len);
Packit 33f14e
        q[q_len] = 0;
Packit 33f14e
Packit 33f14e
        /* Z points to the boundary between a readable/writable page
Packit 33f14e
           and one that is neither readable nor writable.  Position
Packit 33f14e
           our string so its NUL is at the end of the writable one.  */
Packit 33f14e
        char const *str = "____";
Packit 33f14e
        size_t s_len = strlen (str);
Packit 33f14e
        z -= s_len + 1;
Packit 33f14e
        memcpy (z, str, s_len + 1);
Packit 33f14e
Packit 33f14e
        set_custom_quoting (NULL, q, q);
Packit 33f14e
        /* Whether this actually triggers a SEGV depends on the
Packit 33f14e
           implementation of memcmp: whether it compares only byte-at-
Packit 33f14e
           a-time, and from left to right (no SEGV) or some other way.  */
Packit 33f14e
        size_t n = quotearg_buffer (buf, sizeof buf, z, SIZE_MAX, NULL);
Packit 33f14e
        ASSERT (n == s_len + 2 * q_len);
Packit 33f14e
        ASSERT (memcmp (buf, q, sizeof buf) == 0);
Packit 33f14e
        free (q);
Packit 33f14e
      }
Packit 33f14e
  }
Packit 33f14e
Packit 33f14e
  quotearg_free ();
Packit 33f14e
Packit 33f14e
  return 0;
Packit 33f14e
}