quote 2.9 KB

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