xref: /dflybsd-src/contrib/cvs-1.12/lib/quotearg.h (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /* quotearg.h - quote arguments for output
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004 Free Software
4*86d7f5d3SJohn Marino    Foundation, Inc.
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify
7*86d7f5d3SJohn Marino    it under the terms of the GNU General Public License as published by
8*86d7f5d3SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
9*86d7f5d3SJohn Marino    any later version.
10*86d7f5d3SJohn Marino 
11*86d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
12*86d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*86d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*86d7f5d3SJohn Marino    GNU General Public License for more details.
15*86d7f5d3SJohn Marino 
16*86d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
17*86d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
18*86d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19*86d7f5d3SJohn Marino 
20*86d7f5d3SJohn Marino /* Written by Paul Eggert <eggert@twinsun.com> */
21*86d7f5d3SJohn Marino 
22*86d7f5d3SJohn Marino #ifndef QUOTEARG_H_
23*86d7f5d3SJohn Marino # define QUOTEARG_H_ 1
24*86d7f5d3SJohn Marino 
25*86d7f5d3SJohn Marino # include <stddef.h>
26*86d7f5d3SJohn Marino 
27*86d7f5d3SJohn Marino /* Basic quoting styles.  */
28*86d7f5d3SJohn Marino enum quoting_style
29*86d7f5d3SJohn Marino   {
30*86d7f5d3SJohn Marino     /* Output names as-is (ls --quoting-style=literal).  */
31*86d7f5d3SJohn Marino     literal_quoting_style,
32*86d7f5d3SJohn Marino 
33*86d7f5d3SJohn Marino     /* Quote names for the shell if they contain shell metacharacters
34*86d7f5d3SJohn Marino        or would cause ambiguous output (ls --quoting-style=shell).  */
35*86d7f5d3SJohn Marino     shell_quoting_style,
36*86d7f5d3SJohn Marino 
37*86d7f5d3SJohn Marino     /* Quote names for the shell, even if they would normally not
38*86d7f5d3SJohn Marino        require quoting (ls --quoting-style=shell-always).  */
39*86d7f5d3SJohn Marino     shell_always_quoting_style,
40*86d7f5d3SJohn Marino 
41*86d7f5d3SJohn Marino     /* Quote names as for a C language string (ls --quoting-style=c).  */
42*86d7f5d3SJohn Marino     c_quoting_style,
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino     /* Like c_quoting_style except omit the surrounding double-quote
45*86d7f5d3SJohn Marino        characters (ls --quoting-style=escape).  */
46*86d7f5d3SJohn Marino     escape_quoting_style,
47*86d7f5d3SJohn Marino 
48*86d7f5d3SJohn Marino     /* Like clocale_quoting_style, but quote `like this' instead of
49*86d7f5d3SJohn Marino        "like this" in the default C locale (ls --quoting-style=locale).  */
50*86d7f5d3SJohn Marino     locale_quoting_style,
51*86d7f5d3SJohn Marino 
52*86d7f5d3SJohn Marino     /* Like c_quoting_style except use quotation marks appropriate for
53*86d7f5d3SJohn Marino        the locale (ls --quoting-style=clocale).  */
54*86d7f5d3SJohn Marino     clocale_quoting_style
55*86d7f5d3SJohn Marino   };
56*86d7f5d3SJohn Marino 
57*86d7f5d3SJohn Marino /* For now, --quoting-style=literal is the default, but this may change.  */
58*86d7f5d3SJohn Marino # ifndef DEFAULT_QUOTING_STYLE
59*86d7f5d3SJohn Marino #  define DEFAULT_QUOTING_STYLE literal_quoting_style
60*86d7f5d3SJohn Marino # endif
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino /* Names of quoting styles and their corresponding values.  */
63*86d7f5d3SJohn Marino extern char const *const quoting_style_args[];
64*86d7f5d3SJohn Marino extern enum quoting_style const quoting_style_vals[];
65*86d7f5d3SJohn Marino 
66*86d7f5d3SJohn Marino struct quoting_options;
67*86d7f5d3SJohn Marino 
68*86d7f5d3SJohn Marino /* The functions listed below set and use a hidden variable
69*86d7f5d3SJohn Marino    that contains the default quoting style options.  */
70*86d7f5d3SJohn Marino 
71*86d7f5d3SJohn Marino /* Allocate a new set of quoting options, with contents initially identical
72*86d7f5d3SJohn Marino    to O if O is not null, or to the default if O is null.
73*86d7f5d3SJohn Marino    It is the caller's responsibility to free the result.  */
74*86d7f5d3SJohn Marino struct quoting_options *clone_quoting_options (struct quoting_options *o);
75*86d7f5d3SJohn Marino 
76*86d7f5d3SJohn Marino /* Get the value of O's quoting style.  If O is null, use the default.  */
77*86d7f5d3SJohn Marino enum quoting_style get_quoting_style (struct quoting_options *o);
78*86d7f5d3SJohn Marino 
79*86d7f5d3SJohn Marino /* In O (or in the default if O is null),
80*86d7f5d3SJohn Marino    set the value of the quoting style to S.  */
81*86d7f5d3SJohn Marino void set_quoting_style (struct quoting_options *o, enum quoting_style s);
82*86d7f5d3SJohn Marino 
83*86d7f5d3SJohn Marino /* In O (or in the default if O is null),
84*86d7f5d3SJohn Marino    set the value of the quoting options for character C to I.
85*86d7f5d3SJohn Marino    Return the old value.  Currently, the only values defined for I are
86*86d7f5d3SJohn Marino    0 (the default) and 1 (which means to quote the character even if
87*86d7f5d3SJohn Marino    it would not otherwise be quoted).  */
88*86d7f5d3SJohn Marino int set_char_quoting (struct quoting_options *o, char c, int i);
89*86d7f5d3SJohn Marino 
90*86d7f5d3SJohn Marino /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
91*86d7f5d3SJohn Marino    argument ARG (of size ARGSIZE), using O to control quoting.
92*86d7f5d3SJohn Marino    If O is null, use the default.
93*86d7f5d3SJohn Marino    Terminate the output with a null character, and return the written
94*86d7f5d3SJohn Marino    size of the output, not counting the terminating null.
95*86d7f5d3SJohn Marino    If BUFFERSIZE is too small to store the output string, return the
96*86d7f5d3SJohn Marino    value that would have been returned had BUFFERSIZE been large enough.
97*86d7f5d3SJohn Marino    If ARGSIZE is -1, use the string length of the argument for ARGSIZE.  */
98*86d7f5d3SJohn Marino size_t quotearg_buffer (char *buffer, size_t buffersize,
99*86d7f5d3SJohn Marino 			char const *arg, size_t argsize,
100*86d7f5d3SJohn Marino 			struct quoting_options const *o);
101*86d7f5d3SJohn Marino 
102*86d7f5d3SJohn Marino /* Like quotearg_buffer, except return the result in a newly allocated
103*86d7f5d3SJohn Marino    buffer.  It is the caller's responsibility to free the result.  */
104*86d7f5d3SJohn Marino char *quotearg_alloc (char const *arg, size_t argsize,
105*86d7f5d3SJohn Marino 		      struct quoting_options const *o);
106*86d7f5d3SJohn Marino 
107*86d7f5d3SJohn Marino /* Use storage slot N to return a quoted version of the string ARG.
108*86d7f5d3SJohn Marino    Use the default quoting options.
109*86d7f5d3SJohn Marino    The returned value points to static storage that can be
110*86d7f5d3SJohn Marino    reused by the next call to this function with the same value of N.
111*86d7f5d3SJohn Marino    N must be nonnegative.  */
112*86d7f5d3SJohn Marino char *quotearg_n (int n, char const *arg);
113*86d7f5d3SJohn Marino 
114*86d7f5d3SJohn Marino /* Equivalent to quotearg_n (0, ARG).  */
115*86d7f5d3SJohn Marino char *quotearg (char const *arg);
116*86d7f5d3SJohn Marino 
117*86d7f5d3SJohn Marino /* Use style S and storage slot N to return a quoted version of the string ARG.
118*86d7f5d3SJohn Marino    This is like quotearg_n (N, ARG), except that it uses S with no other
119*86d7f5d3SJohn Marino    options to specify the quoting method.  */
120*86d7f5d3SJohn Marino char *quotearg_n_style (int n, enum quoting_style s, char const *arg);
121*86d7f5d3SJohn Marino 
122*86d7f5d3SJohn Marino /* Use style S and storage slot N to return a quoted version of the
123*86d7f5d3SJohn Marino    argument ARG of size ARGSIZE.  This is like quotearg_n_style
124*86d7f5d3SJohn Marino    (N, S, ARG), except it can quote null bytes.  */
125*86d7f5d3SJohn Marino char *quotearg_n_style_mem (int n, enum quoting_style s,
126*86d7f5d3SJohn Marino 			    char const *arg, size_t argsize);
127*86d7f5d3SJohn Marino 
128*86d7f5d3SJohn Marino /* Equivalent to quotearg_n_style (0, S, ARG).  */
129*86d7f5d3SJohn Marino char *quotearg_style (enum quoting_style s, char const *arg);
130*86d7f5d3SJohn Marino 
131*86d7f5d3SJohn Marino /* Like quotearg (ARG), except also quote any instances of CH.  */
132*86d7f5d3SJohn Marino char *quotearg_char (char const *arg, char ch);
133*86d7f5d3SJohn Marino 
134*86d7f5d3SJohn Marino /* Equivalent to quotearg_char (ARG, ':').  */
135*86d7f5d3SJohn Marino char *quotearg_colon (char const *arg);
136*86d7f5d3SJohn Marino 
137*86d7f5d3SJohn Marino #endif /* !QUOTEARG_H_ */
138