2 * Copyright (c) 2012 Stefano Sabatini
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <unistd.h> /* getopt */
26 #include "libavutil/log.h"
27 #include "libavutil/bprint.h"
30 #include "compat/getopt.c"
38 static void usage(void)
40 printf("Escape an input string, adopting the av_get_token() escaping logic\n");
41 printf("usage: ffescape [OPTIONS]\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"
46 "-h print this help\n"
47 "-i INFILE set INFILE as input file, stdin if omitted\n"
48 "-l LEVEL set the number of escaping levels, 1 if omitted\n"
49 "-m ESCAPE_MODE select escape mode between 'auto', 'backslash', 'quote'\n"
50 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
51 "-p PROMPT set output prompt, is '=> ' by default\n"
52 "-s SPECIAL_CHARS set the list of special characters\n");
55 int main(int argc, char **argv)
58 char *src_buf, *dst_buf;
59 const char *outfilename = NULL, *infilename = NULL;
60 FILE *outfile = NULL, *infile = NULL;
61 const char *prompt = "=> ";
62 enum AVEscapeMode escape_mode = AV_ESCAPE_MODE_AUTO;
66 char *special_chars = NULL;
69 while ((c = getopt(argc, argv, "ef:hi:l:o:m:p:s:")) != -1) {
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 if (!strcmp(optarg, "xml_single_quotes")) escape_flags |= AV_ESCAPE_FLAG_XML_SINGLE_QUOTES;
84 else if (!strcmp(optarg, "xml_double_quotes")) escape_flags |= AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES;
86 av_log(NULL, AV_LOG_ERROR,
87 "Invalid value '%s' for option -f, "
88 "valid arguments are 'whitespace', and 'strict'\n", optarg);
95 long int li = strtol(optarg, &tail, 10);
96 if (*tail || li > INT_MAX || li < 0) {
97 av_log(NULL, AV_LOG_ERROR,
98 "Invalid value '%s' for option -l, argument must be a non negative integer\n",
106 if (!strcmp(optarg, "auto")) escape_mode = AV_ESCAPE_MODE_AUTO;
107 else if (!strcmp(optarg, "backslash")) escape_mode = AV_ESCAPE_MODE_BACKSLASH;
108 else if (!strcmp(optarg, "quote")) escape_mode = AV_ESCAPE_MODE_QUOTE;
109 else if (!strcmp(optarg, "xml")) escape_mode = AV_ESCAPE_MODE_XML;
111 av_log(NULL, AV_LOG_ERROR,
112 "Invalid value '%s' for option -m, "
113 "valid arguments are 'backslash', and 'quote'\n", optarg);
118 outfilename = optarg;
124 special_chars = optarg;
131 if (!infilename || !strcmp(infilename, "-")) {
132 infilename = "stdin";
135 infile = fopen(infilename, "r");
138 av_log(NULL, AV_LOG_ERROR, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
142 if (!outfilename || !strcmp(outfilename, "-")) {
143 outfilename = "stdout";
146 outfile = fopen(outfilename, "w");
149 av_log(NULL, AV_LOG_ERROR, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
153 /* grab the input and store it in src */
154 av_bprint_init(&src, 1, AV_BPRINT_SIZE_UNLIMITED);
155 while ((c = fgetc(infile)) != EOF)
156 av_bprint_chars(&src, c, 1);
157 av_bprint_chars(&src, 0, 1);
159 if (!av_bprint_is_complete(&src)) {
160 av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the source string\n");
161 av_bprint_finalize(&src, NULL);
164 av_bprint_finalize(&src, &src_buf);
167 fprintf(outfile, "%s", src_buf);
172 if (av_escape(&dst_buf, src_buf, special_chars, escape_mode, escape_flags) < 0) {
173 av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");
180 fprintf(outfile, "%s%s", prompt, dst_buf);