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
1515
1616 API changes, most recent first:
1717
18+2013-03-07 - xxxxxx - lavu 52.18.100 - avstring.h,bprint.h
19+ Add av_escape() and av_bprint_escape() API.
20+
1821 2013-02-24 - xxxxxx - lavfi 3.41.100 - buffersink.h
1922 Add sample_rates field to AVABufferSinkParams.
2023
libavutil/avstring.c+18−0
@@ -28,6 +28,7 @@ · 27 unmodified lines
2828 #include "common.h"
2929 #include "mem.h"
3030 #include "avstring.h"
31+#include "bprint.h"
3132
3233 int av_strstart(const char *str, const char *pfx, const char **ptr)
3334 {
@@ -267,6 +268,23 @@ const char *av_dirname(char *path) · 233 unmodified lines
267268 return path;
268269 }
269270
271+int 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+
270288 #ifdef TEST
271289
272290 int main(void)
libavutil/avstring.h+42−0
@@ -266,6 +266,48 @@ const char *av_basename(const char *path); · 265 unmodified lines
266266 */
267267 const char *av_dirname(char *path);
268268
269+enum 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+ */
308+int av_escape(char **dst, const char *src, const char *special_chars,
309+ enum AVEscapeMode mode, int flags);
310+
269311 /**
270312 * @}
271313 */
libavutil/bprint.c+45−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
2323 #include <string.h>
2424 #include <time.h>
2525 #include "avassert.h"
26+#include "avstring.h"
2627 #include "bprint.h"
2728 #include "common.h"
2829 #include "error.h"
@@ -217,6 +218,50 @@ int av_bprint_finalize(AVBPrint *buf, char **ret_str) · 188 unmodified lines
217218 return ret;
218219 }
219220
221+#define WHITESPACES " \n\t"
222+
223+void 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+
220265 #ifdef TEST
221266
222267 #undef printf
libavutil/bprint.h+17−0
@@ -22,6 +22,7 @@ · 21 unmodified lines
2222 #define AVUTIL_BPRINT_H
2323
2424 #include "attributes.h"
25+#include "avstring.h"
2526
2627 /**
2728 * 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
180181 */
181182 int av_bprint_finalize(AVBPrint *buf, char **ret_str);
182183
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+ */
197+void av_bprint_escape(AVBPrint *dstbuf, const char *src, const char *special_chars,
198+ enum AVEscapeMode mode, int flags);
199+
183200 #endif /* AVUTIL_BPRINT_H */
libavutil/version.h+2−2
@@ -75,8 +75,8 @@ · 74 unmodified lines
7575 */
7676
7777 #define LIBAVUTIL_VERSION_MAJOR 52
78−#define LIBAVUTIL_VERSION_MINOR 17
79−#define LIBAVUTIL_VERSION_MICRO 103
78+#define LIBAVUTIL_VERSION_MINOR 18
79+#define LIBAVUTIL_VERSION_MICRO 100
8080
8181 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
8282 LIBAVUTIL_VERSION_MINOR, \
tools/ffescape.c+20−73
@@ -42,80 +42,16 @@ static void usage(void) · 41 unmodified lines
4242 printf("\n"
4343 "Options:\n"
4444 "-e echo each input line on output\n"
45+ "-f flag select an escape flag, can assume the values 'whitespace' and 'strict'\n"
4546 "-h print this help\n"
4647 "-i INFILE set INFILE as input file, stdin if omitted\n"
4748 "-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"
4950 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
5051 "-p PROMPT set output prompt, is '=> ' by default\n"
5152 "-s SPECIAL_CHARS set the list of special characters\n");
5253 }
5354
54−#define WHITESPACES " \n\t"
55−
56−enum EscapeMode {
57− ESCAPE_MODE_FULL,
58− ESCAPE_MODE_LAZY,
59− ESCAPE_MODE_QUOTE,
60−};
61−
62−static 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−
11955 int main(int argc, char **argv)
12056 {
12157 AVBPrint src;
@@ -123,13 +59,14 @@ int main(int argc, char **argv) · 1 unmodified line
12359 const char *outfilename = NULL, *infilename = NULL;
12460 FILE *outfile = NULL, *infile = NULL;
12561 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;
12764 int level = 1;
12865 int echo = 0;
12966 char *special_chars = NULL;
13067 int c;
13168
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) {
13370 switch (c) {
13471 case 'e':
13572 echo = 1;
@@ -140,6 +77,16 @@ int main(int argc, char **argv) · 4 unmodified lines
14077 case 'i':
14178 infilename = optarg;
14279 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;
14390 case 'l':
14491 {
14592 char *tail;
@@ -154,13 +101,13 @@ int main(int argc, char **argv) · 8 unmodified lines
154101 break;
155102 }
156103 case 'm':
157− if (!strcmp(optarg, "full")) escape_mode = ESCAPE_MODE_FULL;
158− else if (!strcmp(optarg, "lazy")) escape_mode = ESCAPE_MODE_LAZY;
159− else if (!strcmp(optarg, "quote")) escape_mode = ESCAPE_MODE_QUOTE;
104+ if (!strcmp(optarg, "auto")) escape_mode = AV_ESCAPE_MODE_AUTO;
105+ else if (!strcmp(optarg, "backslash")) escape_mode = AV_ESCAPE_MODE_BACKSLASH;
106+ else if (!strcmp(optarg, "quote")) escape_mode = AV_ESCAPE_MODE_QUOTE;
160107 else {
161108 av_log(NULL, AV_LOG_ERROR,
162109 "Invalid value '%s' for option -m, "
163− "valid arguments are 'full', 'lazy', 'quote'\n", optarg);
110+ "valid arguments are 'backslash', and 'quote'\n", optarg);
164111 return 1;
165112 }
166113 break;
@@ -219,7 +166,7 @@ int main(int argc, char **argv) · 52 unmodified lines
219166 /* escape */
220167 dst_buf = src_buf;
221168 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) {
223170 av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");
224171 return 1;
225172 }

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