blob: ae6c6c28f89070e934c39923dd8dff0551d7bd69 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH QUOTE 3
2.SH NAME
3quotestrdup, quoterunestrdup, unquotestrdup, unquoterunestrdup, quotestrfmt, quoterunestrfmt, quotefmtinstall, doquote, needsrcquote \- quoted character strings
4.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.PP
9.B
10char *quotestrdup(char *s)
11.PP
12.B
13Rune *quoterunestrdup(Rune *s)
14.PP
15.B
16char *unquotestrdup(char *s)
17.PP
18.B
19Rune *unquoterunestrdup(Rune *s)
20.PP
21.B
22int quotestrfmt(Fmt*)
23.PP
24.B
25int quoterunestrfmt(Fmt*)
26.PP
27.B
28void quotefmtinstall(void)
29.PP
30.B
31int (*doquote)(int c)
32.PP
33.B
34int needsrcquote(int c)
35.PP
36.SH DESCRIPTION
37These routines manipulate character strings, either adding or removing
38quotes as necessary.
39In the quoted form, the strings are in the style of
40.IR rc (1) ,
41with single quotes surrounding the string.
42Embedded single quotes are indicated by a doubled single quote.
43For instance,
44.IP
45.EX
46Don't worry!
47.EE
48.PP
49when quoted becomes
50.IP
51.EX
52\&'Don''t worry!'
53.EE
54.PP
55The empty string is represented by two quotes,
56.BR '' .
57.PP
58The first four functions act as variants of
59.B strdup
60(see
rscbf8a59f2004-04-11 03:42:27 +000061.IR strcat (3)).
rsccfa37a72004-04-10 18:53:55 +000062Each returns a
63freshly allocated copy of the string, created using
rscbf8a59f2004-04-11 03:42:27 +000064.IR malloc (3).
rsccfa37a72004-04-10 18:53:55 +000065.I Quotestrdup
66returns a quoted copy of
67.IR s ,
68while
69.I unquotestrdup
70returns a copy of
71.IR s
72with the quotes evaluated.
73The
74.I rune
75versions of these functions do the same for
76.CW Rune
77strings (see
rscbf8a59f2004-04-11 03:42:27 +000078.IR runestrcat (3)).
rsccfa37a72004-04-10 18:53:55 +000079.PP
80The string returned by
81.I quotestrdup
82or
83.I quoterunestrdup
84has the following properties:
85.TP
861.
87If the original string
88.IR s
89is empty, the returned string is
90.BR '' .
91.TP
922.
93If
94.I s
95contains no quotes, blanks, or control characters,
96the returned string is identical to
97.IR s .
98.TP
993.
100If
101.I s
102needs quotes to be added, the first character of the returned
103string will be a quote.
104For example,
105.B hello\ world
106becomes
107.B \&'hello\ world'
108not
109.BR hello'\ 'world .
110.PP
111The function pointer
112.I doquote
113is
114.B nil
115by default.
116If it is non-nil, characters are passed to that function to see if they should
117be quoted.
118This mechanism allows programs to specify that
119characters other than blanks, control characters, or quotes be quoted.
120Regardless of the return value of
121.IR *doquote ,
122blanks, control characters, and quotes are always quoted.
123.I Needsrcquote
124is provided as a
125.I doquote
126function that flags any character special to
127.IR rc (1).
128.PP
129.I Quotestrfmt
130and
131.I quoterunestrfmt
132are
rscbf8a59f2004-04-11 03:42:27 +0000133.IR print (3)
rsccfa37a72004-04-10 18:53:55 +0000134formatting routines that produce quoted strings as output.
135They may be installed by hand, but
136.I quotefmtinstall
137installs them under the standard format characters
138.B q
139and
140.BR Q .
141(They are not installed automatically.)
142If the format string includes the alternate format character
143.BR # ,
144for example
145.BR %#q ,
146the printed string will always be quoted; otherwise quotes will only be provided if necessary
147to avoid ambiguity.
148In
149.B <libc.h>
150there are
151.B #pragma
152statements so the compiler can type-check uses of
153.B %q
154and
155.B %Q
156in
rscbf8a59f2004-04-11 03:42:27 +0000157.IR print (3)
rsccfa37a72004-04-10 18:53:55 +0000158format strings.
159.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000160.B \*9/src/lib9/quote.c
rsccfa37a72004-04-10 18:53:55 +0000161.br
rscc3674de2005-01-11 17:37:33 +0000162.B \*9/src/lib9/fmt/fmtquote.c
rsccfa37a72004-04-10 18:53:55 +0000163.SH "SEE ALSO
164.IR rc (1),
rscbf8a59f2004-04-11 03:42:27 +0000165.IR malloc (3),
166.IR print (3),
167.IR strcat (3)
rscc8b63422005-01-13 04:49:19 +0000168.SH BUGS
169Because it is provided by the format library,
170.I doquote
171is a preprocessor macro defined as
172.IR fmtdoquote ;
173see
174.IR intro (3).