[FFmpeg-devel] [PATCH] tools: add ffescape utility
Stefano Sabatini
stefasab at gmail.com
Mon Oct 15 12:24:41 CEST 2012
---
libavutil/Makefile | 2 +-
tools/ffescape.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 176 insertions(+), 1 deletions(-)
create mode 100644 tools/ffescape.c
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 841eec5..8d9996d 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -129,6 +129,6 @@ TESTPROGS = adler32 \
TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo
-TOOLS = ffeval
+TOOLS = ffeval ffescape
$(SUBDIR)lzo-test$(EXESUF): ELIBS = -llzo2
diff --git a/tools/ffescape.c b/tools/ffescape.c
new file mode 100644
index 0000000..42e3370
--- /dev/null
+++ b/tools/ffescape.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright (c) 2012 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "config.h"
+#if HAVE_UNISTD_H
+#include <unistd.h> /* getopt */
+#endif
+
+#include "libavutil/log.h"
+#include "libavutil/bprint.h"
+
+#if !HAVE_GETOPT
+#include "compat/getopt.c"
+#endif
+
+/**
+ * @file
+ * escaping utility
+ */
+
+static void usage(void)
+{
+ printf("Escape an input string, adopting the av_get_token() escaping logic\n");
+ printf("usage: ffescape [OPTIONS]\n");
+ printf("\n"
+ "Options:\n"
+ "-e echo each input line on output\n"
+ "-h print this help\n"
+ "-i INFILE set INFILE as input file, stdin if omitted\n"
+ "-l LEVEL set the number of escaping levels, 1 if omitted\n"
+ "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
+ "-p PROMPT set output prompt, is '=> ' by default\n"
+ "-s SPECIAL_CHARS set the list of special characters\n");
+}
+
+#define MAX_BLOCK_SIZE SIZE_MAX
+
+#define WHITESPACES " \n\t"
+
+static char *escape(const char *src, const char *special_chars)
+{
+ AVBPrint dst;
+ char *ret;
+
+ av_bprint_init(&dst, 1, AV_BPRINT_SIZE_UNLIMITED);
+ for (; *src; src++) {
+ if ((special_chars && strchr(special_chars, *src)) ||
+ strchr("'\\" WHITESPACES, *src))
+ av_bprintf(&dst, "\\%c", *src);
+ else
+ av_bprint_chars(&dst, *src, 1);
+ }
+ av_bprint_finalize(&dst, &ret);
+ return ret;
+}
+
+int main(int argc, char **argv)
+{
+ size_t src_buf_size = 256;
+ char *src_buf = av_malloc(src_buf_size), *dst_buf = NULL;
+ const char *outfilename = NULL, *infilename = NULL;
+ FILE *outfile = NULL, *infile = NULL;
+ const char *prompt = "=> ";
+ int level = 1;
+ int count = 0, echo = 0;
+ char *special_chars = NULL;
+ char c;
+
+ av_max_alloc(MAX_BLOCK_SIZE);
+
+ while ((c = getopt(argc, argv, "ehi:l:o:p:s:")) != -1) {
+ switch (c) {
+ case 'e':
+ echo = 1;
+ break;
+ case 'h':
+ usage();
+ return 0;
+ case 'i':
+ infilename = optarg;
+ break;
+ case 'l':
+ {
+ char *tail;
+ long int li = strtol(optarg, &tail, 10);
+ if (*tail || li > INT_MAX || li < 0) {
+ av_log(NULL, AV_LOG_ERROR,
+ "Invalid value '%s' for option -l, argument must be a non negative integer\n",
+ optarg);
+ return 1;
+ }
+ level = li;
+ break;
+ }
+ case 'o':
+ outfilename = optarg;
+ break;
+ case 'p':
+ prompt = optarg;
+ break;
+ case 's':
+ special_chars = optarg;
+ break;
+ case '?':
+ return 1;
+ }
+ }
+
+ if (!infilename || !strcmp(infilename, "-"))
+ infilename = "/dev/stdin";
+ infile = fopen(infilename, "r");
+ if (!infile) {
+ fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
+ return 1;
+ }
+
+ if (!outfilename || !strcmp(outfilename, "-"))
+ outfilename = "/dev/stdout";
+ outfile = fopen(outfilename, "w");
+ if (!outfile) {
+ fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
+ return 1;
+ }
+
+ /* grab the input and store it in buf */
+ while ((c = fgetc(infile)) != EOF) {
+ if (count >= src_buf_size-1) {
+ if (src_buf_size == MAX_BLOCK_SIZE) {
+ av_log(NULL, AV_LOG_ERROR, "Memory allocation problem, "
+ "max block size '%zd' reached\n", MAX_BLOCK_SIZE);
+ return 1;
+ }
+ src_buf_size = FFMIN(src_buf_size, MAX_BLOCK_SIZE / 2) * 2;
+ src_buf = av_realloc_f((void *)src_buf, src_buf_size, 1);
+ if (!src_buf) {
+ av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");
+ return 1;
+ }
+ }
+ src_buf[count++] = c;
+ }
+ src_buf[count] = 0;
+
+ if (echo)
+ fprintf(outfile, "%s ", src_buf);
+
+ /* escape */
+ dst_buf = src_buf;
+ while (level--) {
+ dst_buf = escape(src_buf, special_chars);
+ av_free(src_buf);
+ src_buf = dst_buf;
+ }
+
+ fprintf(outfile, "%s%s", prompt, dst_buf);
+ av_free(dst_buf);
+ return 0;
+}
--
1.7.5.4
More information about the ffmpeg-devel
mailing list