Quotearg part 3: add flag to control outer quote elision.
[gnulib.git] / lib / quotearg.h
1 /* quotearg.h - quote arguments for output
2
3    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2006, 2008 Free
4    Software Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* Written by Paul Eggert <eggert@twinsun.com> */
20
21 #ifndef QUOTEARG_H_
22 # define QUOTEARG_H_ 1
23
24 # include <stddef.h>
25
26 /* Basic quoting styles.  */
27 enum quoting_style
28   {
29     /* Output names as-is (ls --quoting-style=literal).  Can result in
30        embedded null bytes if QA_ELIDE_NULL_BYTES is not in
31        effect.  */
32     literal_quoting_style,
33
34     /* Quote names for the shell if they contain shell metacharacters
35        or would cause ambiguous output (ls --quoting-style=shell).
36        Can result in embedded null bytes if QA_ELIDE_NULL_BYTES is not
37        in effect.  */
38     shell_quoting_style,
39
40     /* Quote names for the shell, even if they would normally not
41        require quoting (ls --quoting-style=shell-always).  Can result
42        in embedded null bytes if QA_ELIDE_NULL_BYTES is not in effect.
43        Behaves like shell_quoting_style if QA_ELIDE_OUTER_QUOTES is in
44        effect.  */
45     shell_always_quoting_style,
46
47     /* Quote names as for a C language string (ls --quoting-style=c).
48        Behaves like c_maybe_quoting_style if QA_ELIDE_OUTER_QUOTES is
49        in effect.  */
50     c_quoting_style,
51
52     /* Like c_quoting_style except omit the surrounding double-quote
53        characters if no quoted characters are encountered.  */
54     c_maybe_quoting_style,
55
56     /* Like c_quoting_style except always omit the surrounding
57        double-quote characters (ls --quoting-style=escape).  */
58     escape_quoting_style,
59
60     /* Like clocale_quoting_style, but quote `like this' instead of
61        "like this" in the default C locale (ls --quoting-style=locale).  */
62     locale_quoting_style,
63
64     /* Like c_quoting_style except use quotation marks appropriate for
65        the locale (ls --quoting-style=clocale).  */
66     clocale_quoting_style
67   };
68
69 /* Flags for use in set_quoting_flags.  */
70 enum quoting_flags
71   {
72     /* Always elide null bytes from styles that do not quote them,
73        even when the length of the result is available to the
74        caller.  */
75     QA_ELIDE_NULL_BYTES = 0x01,
76
77     /* Omit the surrounding quote characters if no escaped characters
78        are encountered.  */
79     QA_ELIDE_OUTER_QUOTES = 0x02
80   };
81
82 /* For now, --quoting-style=literal is the default, but this may change.  */
83 # ifndef DEFAULT_QUOTING_STYLE
84 #  define DEFAULT_QUOTING_STYLE literal_quoting_style
85 # endif
86
87 /* Names of quoting styles and their corresponding values.  */
88 extern char const *const quoting_style_args[];
89 extern enum quoting_style const quoting_style_vals[];
90
91 struct quoting_options;
92
93 /* The functions listed below set and use a hidden variable
94    that contains the default quoting style options.  */
95
96 /* Allocate a new set of quoting options, with contents initially identical
97    to O if O is not null, or to the default if O is null.
98    It is the caller's responsibility to free the result.  */
99 struct quoting_options *clone_quoting_options (struct quoting_options *o);
100
101 /* Get the value of O's quoting style.  If O is null, use the default.  */
102 enum quoting_style get_quoting_style (struct quoting_options *o);
103
104 /* In O (or in the default if O is null),
105    set the value of the quoting style to S.  */
106 void set_quoting_style (struct quoting_options *o, enum quoting_style s);
107
108 /* In O (or in the default if O is null),
109    set the value of the quoting options for character C to I.
110    Return the old value.  Currently, the only values defined for I are
111    0 (the default) and 1 (which means to quote the character even if
112    it would not otherwise be quoted).  */
113 int set_char_quoting (struct quoting_options *o, char c, int i);
114
115 /* In O (or in the default if O is null),
116    set the value of the quoting options flag to I, which can be a
117    bitwise combination of enum quoting_flags, or 0 for default
118    behavior.  Return the old value.  */
119 int set_quoting_flags (struct quoting_options *o, int i);
120
121 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
122    argument ARG (of size ARGSIZE), using O to control quoting.
123    If O is null, use the default.
124    Terminate the output with a null character, and return the written
125    size of the output, not counting the terminating null.
126    If BUFFERSIZE is too small to store the output string, return the
127    value that would have been returned had BUFFERSIZE been large enough.
128    If ARGSIZE is -1, use the string length of the argument for ARGSIZE.
129    On output, BUFFER might contain embedded null bytes if ARGSIZE was
130    not -1, the style of O does not use backslash escapes, and the
131    flags of O do not request elision of null bytes.*/
132 size_t quotearg_buffer (char *buffer, size_t buffersize,
133                         char const *arg, size_t argsize,
134                         struct quoting_options const *o);
135
136 /* Like quotearg_buffer, except return the result in a newly allocated
137    buffer.  It is the caller's responsibility to free the result.  The
138    result will not contain embedded null bytes.  */
139 char *quotearg_alloc (char const *arg, size_t argsize,
140                       struct quoting_options const *o);
141
142 /* Like quotearg_alloc, except that the length of the result,
143    excluding the terminating null byte, is stored into SIZE if it is
144    non-NULL.  The result might contain embedded null bytes if ARGSIZE
145    was not -1, SIZE was not NULL, the style of O does not use
146    backslash escapes, and the flags of O do not request elision of
147    null bytes.*/
148 char *quotearg_alloc_mem (char const *arg, size_t argsize,
149                           size_t *size, struct quoting_options const *o);
150
151 /* Use storage slot N to return a quoted version of the string ARG.
152    Use the default quoting options.
153    The returned value points to static storage that can be
154    reused by the next call to this function with the same value of N.
155    N must be nonnegative.  The output of all functions in the
156    quotearg_n family are guaranteed to not contain embedded null
157    bytes.*/
158 char *quotearg_n (int n, char const *arg);
159
160 /* Equivalent to quotearg_n (0, ARG).  */
161 char *quotearg (char const *arg);
162
163 /* Use storage slot N to return a quoted version of the argument ARG
164    of size ARGSIZE.  This is like quotearg_n (N, ARG), except it can
165    quote null bytes.  */
166 char *quotearg_n_mem (int n, char const *arg, size_t argsize);
167
168 /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE).  */
169 char *quotearg_mem (char const *arg, size_t argsize);
170
171 /* Use style S and storage slot N to return a quoted version of the string ARG.
172    This is like quotearg_n (N, ARG), except that it uses S with no other
173    options to specify the quoting method.  */
174 char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
175
176 /* Use style S and storage slot N to return a quoted version of the
177    argument ARG of size ARGSIZE.  This is like quotearg_n_style
178    (N, S, ARG), except it can quote null bytes.  */
179 char *quotearg_n_style_mem (int n, enum quoting_style s,
180                             char const *arg, size_t argsize);
181
182 /* Equivalent to quotearg_n_style (0, S, ARG).  */
183 char *quotearg_style (enum quoting_style s, char const *arg);
184
185 /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE).  */
186 char *quotearg_style_mem (enum quoting_style s,
187                           char const *arg, size_t argsize);
188
189 /* Like quotearg (ARG), except also quote any instances of CH.  */
190 char *quotearg_char (char const *arg, char ch);
191
192 /* Like quotearg_char (ARG, CH), except it can quote null bytes.  */
193 char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
194
195 /* Equivalent to quotearg_char (ARG, ':').  */
196 char *quotearg_colon (char const *arg);
197
198 /* Like quotearg_colon (ARG), except it can quote null bytes.  */
199 char *quotearg_colon_mem (char const *arg, size_t argsize);
200
201 /* Free any dynamically allocated memory.  */
202 void quotearg_free (void);
203
204 #endif /* !QUOTEARG_H_ */