History

lavu: add escape API

The escape API will be useful to perform escaping programmatically, which is required when crafting argument strings, and will be used for context printing as well. This is based on the ffescape tool code, with a few extensions and fixes.

Stefano Sabatini committed 13 years ago

Commit 9767ec61 parent 38d40ac7 files · +147 −75Browse files

Unified · Split

7 files
  1. doc/APIchanges +3 −0
  2. libavutil/avstring.c +18 −0
  3. libavutil/avstring.h +42 −0
  4. libavutil/bprint.c +45 −0
  5. libavutil/bprint.h +17 −0
  6. libavutil/version.h +2 −2
  7. tools/ffescape.c +20 −73
doc/APIchanges+3−0
@@ -15,6 +15,9 @@ libavutil: 2012-10-22 · 14 unmodified lines
15 15
16API changes, most recent first:16API changes, most recent first:
17 17
182013-03-07 - xxxxxx - lavu 52.18.100 - avstring.h,bprint.h
19 Add av_escape() and av_bprint_escape() API.
20
182013-02-24 - xxxxxx - lavfi 3.41.100 - buffersink.h212013-02-24 - xxxxxx - lavfi 3.41.100 - buffersink.h
19 Add sample_rates field to AVABufferSinkParams.22 Add sample_rates field to AVABufferSinkParams.
20 23
libavutil/avstring.c+18−0
@@ -28,6 +28,7 @@ · 27 unmodified lines
28#include "common.h"28#include "common.h"
29#include "mem.h"29#include "mem.h"
30#include "avstring.h"30#include "avstring.h"
31#include "bprint.h"
31 32
32int av_strstart(const char *str, const char *pfx, const char **ptr)33int av_strstart(const char *str, const char *pfx, const char **ptr)
33{34{
@@ -267,6 +268,23 @@ const char *av_dirname(char *path) · 233 unmodified lines
267 return path;268 return path;
268}269}
269 270
271int av_escape(char **dst, const char *src, const char *special_chars,
272 enum AVEscapeMode mode, int flags)
273{
274 AVBPrint dstbuf;
275
276 av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
277 av_bprint_escape(&dstbuf, src, special_chars, mode, flags);
278
279 if (!av_bprint_is_complete(&dstbuf)) {
280 av_bprint_finalize(&dstbuf, NULL);
281 return AVERROR(ENOMEM);
282 } else {
283 av_bprint_finalize(&dstbuf, dst);
284 return dstbuf.len;
285 }
286}
287
270#ifdef TEST288#ifdef TEST
271 289
272int main(void)290int main(void)
libavutil/avstring.h+42−0
@@ -266,6 +266,48 @@ const char *av_basename(const char *path); · 265 unmodified lines
266 */266 */
267const char *av_dirname(char *path);267const char *av_dirname(char *path);
268 268
269enum AVEscapeMode {
270 AV_ESCAPE_MODE_AUTO, ///< Use auto-selected escaping mode.
271 AV_ESCAPE_MODE_BACKSLASH, ///< Use backslash escaping.
272 AV_ESCAPE_MODE_QUOTE, ///< Use single-quote escaping.
273};
274
275/**
276 * Consider spaces special and escape them even in the middle of the
277 * string.
278 *
279 * This is equivalent to adding the whitespace characters to the special
280 * characters lists, except it is guaranteed to use the exact same list
281 * of whitespace characters as the rest of libavutil.
282 */
283#define AV_ESCAPE_FLAG_WHITESPACE 0x01
284
285/**
286 * Escape only specified special characters.
287 * Without this flag, escape also any characters that may be considered
288 * special by av_get_token(), such as the single quote.
289 */
290#define AV_ESCAPE_FLAG_STRICT 0x02
291
292/**
293 * Escape string in src, and put the escaped string in an allocated
294 * string in *dst, which must be freed with av_free().
295 *
296 * @param dst pointer where an allocated string is put
297 * @param src string to escape, must be non-NULL
298 * @param special_chars string containing the special characters which
299 * need to be escaped, can be NULL
300 * @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.
301 * Any unknown value for mode will be considered equivalent to
302 * AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
303 * notice.
304 * @param flags flags which control how to escape, see AV_ESCAPE_FLAG_ macros
305 * @return the length of the allocated string, or a negative error code in case of error
306 * @see av_bprint_escape()
307 */
308int av_escape(char **dst, const char *src, const char *special_chars,
309 enum AVEscapeMode mode, int flags);
310
269/**311/**
270 * @}312 * @}
271 */313 */
libavutil/bprint.c+45−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
23#include <string.h>23#include <string.h>
24#include <time.h>24#include <time.h>
25#include "avassert.h"25#include "avassert.h"
26#include "avstring.h"
26#include "bprint.h"27#include "bprint.h"
27#include "common.h"28#include "common.h"
28#include "error.h"29#include "error.h"
@@ -217,6 +218,50 @@ int av_bprint_finalize(AVBPrint *buf, char **ret_str) · 188 unmodified lines
217 return ret;218 return ret;
218}219}
219 220
221#define WHITESPACES " \n\t"
222
223void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
224 enum AVEscapeMode mode, int flags)
225{
226 const char *src0 = src;
227
228 if (mode == AV_ESCAPE_MODE_AUTO)
229 mode = AV_ESCAPE_MODE_BACKSLASH; /* TODO: implement a heuristic */
230
231 switch (mode) {
232 case AV_ESCAPE_MODE_QUOTE:
233 /* enclose the string between '' */
234 av_bprint_chars(dstbuf, '\'', 1);
235 for (; *src; src++) {
236 if (*src == '\'')
237 av_bprintf(dstbuf, "'\\''");
238 else
239 av_bprint_chars(dstbuf, *src, 1);
240 }
241 av_bprint_chars(dstbuf, '\'', 1);
242 break;
243
244 /* case AV_ESCAPE_MODE_BACKSLASH or unknown mode */
245 default:
246 /* \-escape characters */
247 for (; *src; src++) {
248 int is_first_last = src == src0 || !*(src+1);
249 int is_ws = !!strchr(WHITESPACES, *src);
250 int is_strictly_special = special_chars && strchr(special_chars, *src);
251 int is_special =
252 is_strictly_special || strchr("'\\", *src) ||
253 (is_ws && (flags & AV_ESCAPE_FLAG_WHITESPACE));
254
255 if (is_strictly_special ||
256 (!(flags & AV_ESCAPE_FLAG_STRICT) &&
257 (is_special || (is_ws && is_first_last))))
258 av_bprint_chars(dstbuf, '\\', 1);
259 av_bprint_chars(dstbuf, *src, 1);
260 }
261 break;
262 }
263}
264
220#ifdef TEST265#ifdef TEST
221 266
222#undef printf267#undef printf
libavutil/bprint.h+17−0
@@ -22,6 +22,7 @@ · 21 unmodified lines
22#define AVUTIL_BPRINT_H22#define AVUTIL_BPRINT_H
23 23
24#include "attributes.h"24#include "attributes.h"
25#include "avstring.h"
25 26
26/**27/**
27 * Define a structure with extra padding to a fixed size28 * Define a structure with extra padding to a fixed size
@@ -180,4 +181,20 @@ static inline int av_bprint_is_complete(AVBPrint *buf) · 152 unmodified lines
180 */181 */
181int av_bprint_finalize(AVBPrint *buf, char **ret_str);182int av_bprint_finalize(AVBPrint *buf, char **ret_str);
182 183
184/**
185 * Escape the content in src and append it to dstbuf.
186 *
187 * @param dstbuf already inited destination bprint buffer
188 * @param src string containing the text to escape
189 * @param special_chars string containing the special characters which
190 * need to be escaped, can be NULL
191 * @param mode escape mode to employ, see AV_ESCAPE_MODE_* macros.
192 * Any unknown value for mode will be considered equivalent to
193 * AV_ESCAPE_MODE_BACKSLASH, but this behaviour can change without
194 * notice.
195 * @param flags flags which control how to escape, see AV_ESCAPE_FLAG_* macros
196 */
197void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
198 enum AVEscapeMode mode, int flags);
199
183#endif /* AVUTIL_BPRINT_H */200#endif /* AVUTIL_BPRINT_H */
libavutil/version.h+2−2
@@ -75,8 +75,8 @@ · 74 unmodified lines
75 */75 */
76 76
77#define LIBAVUTIL_VERSION_MAJOR 5277#define LIBAVUTIL_VERSION_MAJOR 52
78#define LIBAVUTIL_VERSION_MINOR 1778#define LIBAVUTIL_VERSION_MINOR 18
79#define LIBAVUTIL_VERSION_MICRO 10379#define LIBAVUTIL_VERSION_MICRO 100
80 80
81#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \81#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
82 LIBAVUTIL_VERSION_MINOR, \82 LIBAVUTIL_VERSION_MINOR, \
tools/ffescape.c+20−73
@@ -42,80 +42,16 @@ static void usage(void) · 41 unmodified lines
42 printf("\n"42 printf("\n"
43 "Options:\n"43 "Options:\n"
44 "-e echo each input line on output\n"44 "-e echo each input line on output\n"
45 "-f flag select an escape flag, can assume the values 'whitespace' and 'strict'\n"
45 "-h print this help\n"46 "-h print this help\n"
46 "-i INFILE set INFILE as input file, stdin if omitted\n"47 "-i INFILE set INFILE as input file, stdin if omitted\n"
47 "-l LEVEL set the number of escaping levels, 1 if omitted\n"48 "-l LEVEL set the number of escaping levels, 1 if omitted\n"
48 "-m ESCAPE_MODE select escape mode between 'full', 'lazy', 'quote', default is 'lazy'\n"49 "-m ESCAPE_MODE select escape mode between 'auto', 'backslash', 'quote'\n"
49 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"50 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
50 "-p PROMPT set output prompt, is '=> ' by default\n"51 "-p PROMPT set output prompt, is '=> ' by default\n"
51 "-s SPECIAL_CHARS set the list of special characters\n");52 "-s SPECIAL_CHARS set the list of special characters\n");
52}53}
53 54
54#define WHITESPACES " \n\t"
55
56enum EscapeMode {
57 ESCAPE_MODE_FULL,
58 ESCAPE_MODE_LAZY,
59 ESCAPE_MODE_QUOTE,
60};
61
62static int escape(char **dst, const char *src, const char *special_chars,
63 enum EscapeMode mode)
64{
65 AVBPrint dstbuf;
66
67 av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
68
69 switch (mode) {
70 case ESCAPE_MODE_FULL:
71 case ESCAPE_MODE_LAZY:
72 /* \-escape characters */
73
74 if (mode == ESCAPE_MODE_LAZY && strchr(WHITESPACES, *src))
75 av_bprintf(&dstbuf, "\\%c", *src++);
76
77 for (; *src; src++) {
78 if ((special_chars && strchr(special_chars, *src)) ||
79 strchr("'\\", *src) ||
80 (mode == ESCAPE_MODE_FULL && strchr(WHITESPACES, *src)))
81 av_bprintf(&dstbuf, "\\%c", *src);
82 else
83 av_bprint_chars(&dstbuf, *src, 1);
84 }
85
86 if (mode == ESCAPE_MODE_LAZY && strchr(WHITESPACES, dstbuf.str[dstbuf.len-1])) {
87 char c = dstbuf.str[dstbuf.len-1];
88 dstbuf.str[dstbuf.len-1] = '\\';
89 av_bprint_chars(&dstbuf, c, 1);
90 }
91 break;
92
93 case ESCAPE_MODE_QUOTE:
94 /* enclose between '' the string */
95 av_bprint_chars(&dstbuf, '\'', 1);
96 for (; *src; src++) {
97 if (*src == '\'')
98 av_bprintf(&dstbuf, "'\\''");
99 else
100 av_bprint_chars(&dstbuf, *src, 1);
101 }
102 av_bprint_chars(&dstbuf, '\'', 1);
103 break;
104
105 default:
106 /* unknown escape mode */
107 return AVERROR(EINVAL);
108 }
109
110 if (!av_bprint_is_complete(&dstbuf)) {
111 av_bprint_finalize(&dstbuf, NULL);
112 return AVERROR(ENOMEM);
113 } else {
114 av_bprint_finalize(&dstbuf, dst);
115 return 0;
116 }
117}
118
119int main(int argc, char **argv)55int main(int argc, char **argv)
120{56{
121 AVBPrint src;57 AVBPrint src;
@@ -123,13 +59,14 @@ int main(int argc, char **argv) · 1 unmodified line
123 const char *outfilename = NULL, *infilename = NULL;59 const char *outfilename = NULL, *infilename = NULL;
124 FILE *outfile = NULL, *infile = NULL;60 FILE *outfile = NULL, *infile = NULL;
125 const char *prompt = "=> ";61 const char *prompt = "=> ";
126 enum EscapeMode escape_mode = ESCAPE_MODE_LAZY;62 enum AVEscapeMode escape_mode = AV_ESCAPE_MODE_AUTO;
63 int escape_flags = 0;
127 int level = 1;64 int level = 1;
128 int echo = 0;65 int echo = 0;
129 char *special_chars = NULL;66 char *special_chars = NULL;
130 int c;67 int c;
131 68
132 while ((c = getopt(argc, argv, "ehi:l:o:m:p:s:")) != -1) {69 while ((c = getopt(argc, argv, "ef:hi:l:o:m:p:s:")) != -1) {
133 switch (c) {70 switch (c) {
134 case 'e':71 case 'e':
135 echo = 1;72 echo = 1;
@@ -140,6 +77,16 @@ int main(int argc, char **argv) · 4 unmodified lines
140 case 'i':77 case 'i':
141 infilename = optarg;78 infilename = optarg;
142 break;79 break;
80 case 'f':
81 if (!strcmp(optarg, "whitespace")) escape_flags |= AV_ESCAPE_FLAG_WHITESPACE;
82 else if (!strcmp(optarg, "strict")) escape_flags |= AV_ESCAPE_FLAG_STRICT;
83 else {
84 av_log(NULL, AV_LOG_ERROR,
85 "Invalid value '%s' for option -f, "
86 "valid arguments are 'whitespace', and 'strict'\n", optarg);
87 return 1;
88 }
89 break;
143 case 'l':90 case 'l':
144 {91 {
145 char *tail;92 char *tail;
@@ -154,13 +101,13 @@ int main(int argc, char **argv) · 8 unmodified lines
154 break;101 break;
155 }102 }
156 case 'm':103 case 'm':
157 if (!strcmp(optarg, "full")) escape_mode = ESCAPE_MODE_FULL;104 if (!strcmp(optarg, "auto")) escape_mode = AV_ESCAPE_MODE_AUTO;
158 else if (!strcmp(optarg, "lazy")) escape_mode = ESCAPE_MODE_LAZY;105 else if (!strcmp(optarg, "backslash")) escape_mode = AV_ESCAPE_MODE_BACKSLASH;
159 else if (!strcmp(optarg, "quote")) escape_mode = ESCAPE_MODE_QUOTE;106 else if (!strcmp(optarg, "quote")) escape_mode = AV_ESCAPE_MODE_QUOTE;
160 else {107 else {
161 av_log(NULL, AV_LOG_ERROR,108 av_log(NULL, AV_LOG_ERROR,
162 "Invalid value '%s' for option -m, "109 "Invalid value '%s' for option -m, "
163 "valid arguments are 'full', 'lazy', 'quote'\n", optarg);110 "valid arguments are 'backslash', and 'quote'\n", optarg);
164 return 1;111 return 1;
165 }112 }
166 break;113 break;
@@ -219,7 +166,7 @@ int main(int argc, char **argv) · 52 unmodified lines
219 /* escape */166 /* escape */
220 dst_buf = src_buf;167 dst_buf = src_buf;
221 while (level--) {168 while (level--) {
222 if (escape(&dst_buf, src_buf, special_chars, escape_mode) < 0) {169 if (av_escape(&dst_buf, src_buf, special_chars, escape_mode, escape_flags) < 0) {
223 av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");170 av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");
224 return 1;171 return 1;
225 }172 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t