agssim: implement argv passing to program
[rofl0r-agsutils.git] / StringEscape.c
blob92b76f7082962136cc78feb1440c074b9b2b5f1c
1 #include <stddef.h>
2 #include <assert.h>
3 #include <stdlib.h>
5 //FIXME out gets silently truncated if outsize is too small
7 size_t escape(char* in, char* out, size_t outsize) {
8 size_t l = 0;
9 while(*in && l + 3 < outsize) {
10 switch(*in) {
11 case '\a': /* 0x07 */
12 case '\b': /* 0x08 */
13 case '\t': /* 0x09 */
14 case '\n': /* 0x0a */
15 case '\v': /* 0x0b */
16 case '\f': /* 0x0c */
17 case '\r': /* 0x0d */
18 case '\"': /* 0x22 */
19 case '\'': /* 0x27 */
20 case '\?': /* 0x3f */
21 case '\\': /* 0x5c */
22 *out++ = '\\';
23 l++;
24 switch(*in) {
25 case '\a': /* 0x07 */
26 *out = 'a'; break;
27 case '\b': /* 0x08 */
28 *out = 'b'; break;
29 case '\t': /* 0x09 */
30 *out = 't'; break;
31 case '\n': /* 0x0a */
32 *out = 'n'; break;
33 case '\v': /* 0x0b */
34 *out = 'v'; break;
35 case '\f': /* 0x0c */
36 *out = 'f'; break;
37 case '\r': /* 0x0d */
38 *out = 'r'; break;
39 case '\"': /* 0x22 */
40 *out = '\"'; break;
41 case '\'': /* 0x27 */
42 *out = '\''; break;
43 case '\?': /* 0x3f */
44 *out = '\?'; break;
45 case '\\': /* 0x5c */
46 *out = '\\'; break;
48 break;
49 default:
50 *out = *in;
52 in++;
53 out++;
54 l++;
56 *out = 0;
57 return l;
60 size_t unescape(char* in, char *out, size_t outsize) {
61 size_t l = 0;
62 while(*in && l + 1 < outsize) {
63 switch (*in) {
64 case '\\':
65 ++in;
66 assert(*in);
67 switch(*in) {
68 case 'a':
69 *out = '\a';
70 break;
71 case 'b':
72 *out = '\b';
73 break;
74 case 't':
75 *out='\t';
76 break;
77 case 'n':
78 *out='\n';
79 break;
80 case 'v':
81 *out='\v';
82 break;
83 case 'f':
84 *out = '\f';
85 break;
86 case 'r':
87 *out='\r';
88 break;
89 case '"':
90 *out='"';
91 break;
92 case '\'':
93 *out = '\'';
94 break;
95 case '?':
96 *out = '\?';
97 break;
98 case '\\':
99 *out='\\';
100 break;
101 // FIXME add handling of hex and octal
102 default:
103 abort();
105 break;
106 default:
107 *out=*in;
109 in++;
110 out++;
111 l++;
113 *out = 0;
114 return l;