aboutsummaryrefslogtreecommitdiffstats
path: root/cppawk-quote.1
blob: 29af8e6423baf01ddd25822b18b147b02ade0188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
CPPAWK-QUOTE(1)                           Shell Quoting                           CPPAWK-QUOTE(1)

NAME
       q - Function for shell quoting.

SYNOPSIS
         #include <quote.h>

         q(str)        // return str, shell-escaped

DESCRIPTION

       The  q  function shell-escapes its argument str.  This means that if the argument contains
       special characters, it is transformed such that the resulting string  can  safely  be  in-
       serted  into a shell command as an argument. That argument, when interpreted by the shell,
       will be interpreted as the original text which was input to q.

       If str contains any of the following special characters, it will be single-quoted  (turned
       into  a  string which begins and ends with the single quote, also known as the ASCII apos-
       trophe character).

         \ * ? $ ` ~

       Otherwise, if it contains single quotes it will be  double-quoted  (surrounded  in  double
       quotes).

       Otherwise, if it contains double quotes, it will be single-quoted.

       Otherwise, if it contains spaces, tabs or newlines, it will be double-quoted.

       If none of the above cases apply, str is returned unmodified.

       If  single  quoting  is  applied to a string which contains single quote characters, those
       characters are replaced by the sequence '\''.

       If double quoting is applied to a string which contains double  quotes,  those  characters
       are replaced by \".

       Examples:

         // safely execute mv

         function mv(from, to)
         {
           system("mv -- " q(from) " " q(to))
         }

         // code           // output
         print q("abc")    abc
         print q("ab cd")  "ab cd"
         print q("ab\nc")  "ab
                           c"
         print q("ab'cd")  "ab'cd"
         print q("ab\"cd") 'ab"cd'
         print q("abc*")   'abc*'

SEE ALSO
       cppawk(1)

BUGS
AUTHOR
       Kaz Kylheku <kaz@kylheku.com>

COPYRIGHT
       Copyright 2023, BSD2 License.

cppawk Libraries                           26 June 2023                           CPPAWK-QUOTE(1)