| 1 |
/* Test of quotearg family of functions. |
| 2 |
Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3, or (at your option) |
| 7 |
any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Eric Blake <[email protected]>, 2008. */ |
| 18 |
|
| 19 |
struct result_strings { |
| 20 |
char const *str1; /* Translation of "". */ |
| 21 |
char const *str2; /* Translation of "\0""1\0". */ |
| 22 |
size_t len2; /* Length of str2. */ |
| 23 |
char const *str3; /* Translation of "simple". */ |
| 24 |
char const *str4q; /* Translation of "\t'\t". */ |
| 25 |
char const *str4; /* Translation of " \t\n'\"\033?""?/\\". */ |
| 26 |
char const *str5; /* Translation of "a:b". */ |
| 27 |
char const *str6; /* Translation of "a\\b". */ |
| 28 |
char const *str7; /* Translation of "a' b". */ |
| 29 |
char const *str8a; /* Translation of LQ RQ, in ASCII charset. */ |
| 30 |
char const *str8b; /* Translation of LQ RQ, in Latin1 or UTF-8 charset. */ |
| 31 |
}; |
| 32 |
|
| 33 |
struct result_groups { |
| 34 |
struct result_strings group1; /* Via quotearg_buffer. */ |
| 35 |
struct result_strings group2; /* Via quotearg{,_mem}. */ |
| 36 |
struct result_strings group3; /* Via quotearg_colon{,_mem}. */ |
| 37 |
}; |
| 38 |
|
| 39 |
/* These quotes are borrowed from a pt_PT.utf8 translation. */ |
| 40 |
# define LQ "\302\253" |
| 41 |
# define RQ "\302\273" |
| 42 |
# define LQ_ENC "\\302\\253" |
| 43 |
# define RQ_ENC "\\302\\273" |
| 44 |
# define RQ_ESC "\\\302\273" |
| 45 |
|
| 46 |
static struct result_strings inputs = { |
| 47 |
"", "\0001\0", 3, "simple", "\t'\t", " \t\n'\"\033?""?/\\", "a:b", "a\\b", |
| 48 |
"a' b", LQ RQ, NULL |
| 49 |
}; |
| 50 |
|
| 51 |
static void |
| 52 |
compare (char const *a, size_t la, char const *b, size_t lb) |
| 53 |
{ |
| 54 |
ASSERT (la == lb); |
| 55 |
ASSERT (memcmp (a, b, la) == 0); |
| 56 |
ASSERT (b[lb] == '\0'); |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
compare_strings (char *(func) (char const *, size_t *), |
| 61 |
struct result_strings *results, bool ascii_only) |
| 62 |
{ |
| 63 |
size_t len; |
| 64 |
char *p; |
| 65 |
|
| 66 |
len = 0; |
| 67 |
p = func (inputs.str1, &len); |
| 68 |
compare (results->str1, strlen (results->str1), p, len); |
| 69 |
|
| 70 |
len = inputs.len2; |
| 71 |
p = func (inputs.str2, &len); |
| 72 |
compare (results->str2, results->len2, p, len); |
| 73 |
|
| 74 |
len = SIZE_MAX; |
| 75 |
p = func (inputs.str3, &len); |
| 76 |
compare (results->str3, strlen (results->str3), p, len); |
| 77 |
|
| 78 |
len = strlen (inputs.str4q); |
| 79 |
p = func (inputs.str4q, &len); |
| 80 |
compare (results->str4q, strlen (results->str4q), p, len); |
| 81 |
|
| 82 |
len = strlen (inputs.str4); |
| 83 |
p = func (inputs.str4, &len); |
| 84 |
compare (results->str4, strlen (results->str4), p, len); |
| 85 |
|
| 86 |
len = SIZE_MAX; |
| 87 |
p = func (inputs.str5, &len); |
| 88 |
compare (results->str5, strlen (results->str5), p, len); |
| 89 |
|
| 90 |
len = strlen (inputs.str6); |
| 91 |
p = func (inputs.str6, &len); |
| 92 |
compare (results->str6, strlen (results->str6), p, len); |
| 93 |
|
| 94 |
len = strlen (inputs.str7); |
| 95 |
p = func (inputs.str7, &len); |
| 96 |
compare (results->str7, strlen (results->str7), p, len); |
| 97 |
|
| 98 |
len = strlen (inputs.str8a); |
| 99 |
p = func (inputs.str8a, &len); |
| 100 |
if (ascii_only) |
| 101 |
compare (results->str8a, strlen (results->str8a), p, len); |
| 102 |
else |
| 103 |
compare (results->str8b, strlen (results->str8b), p, len); |
| 104 |
} |
| 105 |
|
| 106 |
static char * |
| 107 |
use_quotearg_buffer (const char *str, size_t *len) |
| 108 |
{ |
| 109 |
static char buf[100]; |
| 110 |
size_t size; |
| 111 |
memset (buf, 0xa5, 100); |
| 112 |
size = quotearg_buffer (buf, 100, str, *len, NULL); |
| 113 |
*len = size; |
| 114 |
ASSERT ((unsigned char) buf[size + 1] == 0xa5); |
| 115 |
return buf; |
| 116 |
} |
| 117 |
|
| 118 |
static char * |
| 119 |
use_quotearg (const char *str, size_t *len) |
| 120 |
{ |
| 121 |
char *p = *len == SIZE_MAX ? quotearg (str) : quotearg_mem (str, *len); |
| 122 |
*len = strlen (p); |
| 123 |
return p; |
| 124 |
} |
| 125 |
|
| 126 |
static char * |
| 127 |
use_quotearg_colon (const char *str, size_t *len) |
| 128 |
{ |
| 129 |
char *p = (*len == SIZE_MAX ? quotearg_colon (str) |
| 130 |
: quotearg_colon_mem (str, *len)); |
| 131 |
*len = strlen (p); |
| 132 |
return p; |
| 133 |
} |