Quotearg part 1: more wrappers, restore quotearg_char state.
[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).  */
30     literal_quoting_style,
31
32     /* Quote names for the shell if they contain shell metacharacters
33        or would cause ambiguous output (ls --quoting-style=shell).  */
34     shell_quoting_style,
35
36     /* Quote names for the shell, even if they would normally not
37        require quoting (ls --quoting-style=shell-always).  */
38     shell_always_quoting_style,
39
40     /* Quote names as for a C language string (ls --quoting-style=c).  */
41     c_quoting_style,
42
43     /* Like c_quoting_style except omit the surrounding double-quote
44        characters (ls --quoting-style=escape).  */
45     escape_quoting_style,
46
47     /* Like clocale_quoting_style, but quote `like this' instead of
48        "like this" in the default C locale (ls --quoting-style=locale).  */
49     locale_quoting_style,
50
51     /* Like c_quoting_style except use quotation marks appropriate for
52        the locale (ls --quoting-style=clocale).  */
53     clocale_quoting_style
54   };
55
56 /* For now, --quoting-style=literal is the default, but this may change.  */
57 # ifndef DEFAULT_QUOTING_STYLE
58 #  define DEFAULT_QUOTING_STYLE literal_quoting_style
59 # endif
60
61 /* Names of quoting styles and their corresponding values.  */
62 extern char const *const quoting_style_args[];
63 extern enum quoting_style const quoting_style_vals[];
64
65 struct quoting_options;
66
67 /* The functions listed below set and use a hidden variable
68    that contains the default quoting style options.  */
69
70 /* Allocate a new set of quoting options, with contents initially identical
71    to O if O is not null, or to the default if O is null.
72    It is the caller's responsibility to free the result.  */
73 struct quoting_options *clone_quoting_options (struct quoting_options *o);
74
75 /* Get the value of O's quoting style.  If O is null, use the default.  */
76 enum quoting_style get_quoting_style (struct quoting_options *o);
77
78 /* In O (or in the default if O is null),
79    set the value of the quoting style to S.  */
80 void set_quoting_style (struct quoting_options *o, enum quoting_style s);
81
82 /* In O (or in the default if O is null),
83    set the value of the quoting options for character C to I.
84    Return the old value.  Currently, the only values defined for I are
85    0 (the default) and 1 (which means to quote the character even if
86    it would not otherwise be quoted).  */
87 int set_char_quoting (struct quoting_options *o, char c, int i);
88
89 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
90    argument ARG (of size ARGSIZE), using O to control quoting.
91    If O is null, use the default.
92    Terminate the output with a null character, and return the written
93    size of the output, not counting the terminating null.
94    If BUFFERSIZE is too small to store the output string, return the
95    value that would have been returned had BUFFERSIZE been large enough.
96    If ARGSIZE is -1, use the string length of the argument for ARGSIZE.  */
97 size_t quotearg_buffer (char *buffer, size_t buffersize,
98                         char const *arg, size_t argsize,
99                         struct quoting_options const *o);
100
101 /* Like quotearg_buffer, except return the result in a newly allocated
102    buffer.  It is the caller's responsibility to free the result.
103    Either ARGSIZE should be -1, or O should not permit embedded null
104    bytes in the output.  */
105 char *quotearg_alloc (char const *arg, size_t argsize,
106                       struct quoting_options const *o);
107
108 /* Like quotearg_alloc, except that the length of the result,
109    excluding the terminating null byte, is stored into SIZE if it is
110    non-NULL.  Thus, this can be safe to use even when O specifies
111    embedded null bytes.  */
112 char *quotearg_alloc_mem (char const *arg, size_t argsize,
113                           size_t *size, struct quoting_options const *o);
114
115 /* Use storage slot N to return a quoted version of the string ARG.
116    Use the default quoting options.
117    The returned value points to static storage that can be
118    reused by the next call to this function with the same value of N.
119    N must be nonnegative.  */
120 char *quotearg_n (int n, char const *arg);
121
122 /* Equivalent to quotearg_n (0, ARG).  */
123 char *quotearg (char const *arg);
124
125 /* Use storage slot N to return a quoted version of the argument ARG
126    of size ARGSIZE.  This is like quotearg_n (N, ARG), except it can
127    quote null bytes.  */
128 char *quotearg_n_mem (int n, char const *arg, size_t argsize);
129
130 /* Equivalent to quotearg_n_mem (0, ARG, ARGSIZE).  */
131 char *quotearg_mem (char const *arg, size_t argsize);
132
133 /* Use style S and storage slot N to return a quoted version of the string ARG.
134    This is like quotearg_n (N, ARG), except that it uses S with no other
135    options to specify the quoting method.  */
136 char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
137
138 /* Use style S and storage slot N to return a quoted version of the
139    argument ARG of size ARGSIZE.  This is like quotearg_n_style
140    (N, S, ARG), except it can quote null bytes.  */
141 char *quotearg_n_style_mem (int n, enum quoting_style s,
142                             char const *arg, size_t argsize);
143
144 /* Equivalent to quotearg_n_style (0, S, ARG).  */
145 char *quotearg_style (enum quoting_style s, char const *arg);
146
147 /* Equivalent to quotearg_n_style_mem (0, S, ARG, ARGSIZE).  */
148 char *quotearg_style_mem (enum quoting_style s,
149                           char const *arg, size_t argsize);
150
151 /* Like quotearg (ARG), except also quote any instances of CH.  */
152 char *quotearg_char (char const *arg, char ch);
153
154 /* Like quotearg_char (ARG, CH), except it can quote null bytes.  */
155 char *quotearg_char_mem (char const *arg, size_t argsize, char ch);
156
157 /* Equivalent to quotearg_char (ARG, ':').  */
158 char *quotearg_colon (char const *arg);
159
160 /* Like quotearg_colon (ARG), except it can quote null bytes.  */
161 char *quotearg_colon_mem (char const *arg, size_t argsize);
162
163 /* Free any dynamically allocated memory.  */
164 void quotearg_free (void);
165
166 #endif /* !QUOTEARG_H_ */