ffescape.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2012 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #include "config.h"
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #if HAVE_UNISTD_H
  27. #include <unistd.h> /* getopt */
  28. #endif
  29. #include "libavutil/log.h"
  30. #include "libavutil/bprint.h"
  31. #include "libavutil/mem.h"
  32. #if !HAVE_GETOPT
  33. #include "compat/getopt.c"
  34. #endif
  35. /**
  36. * @file
  37. * escaping utility
  38. */
  39. static void usage(void)
  40. {
  41. printf("Escape an input string, adopting the av_get_token() escaping logic\n");
  42. printf("usage: ffescape [OPTIONS]\n");
  43. printf("\n"
  44. "Options:\n"
  45. "-e echo each input line on output\n"
  46. "-f flag select an escape flag, can assume the values 'whitespace' and 'strict'\n"
  47. "-h print this help\n"
  48. "-i INFILE set INFILE as input file, stdin if omitted\n"
  49. "-l LEVEL set the number of escaping levels, 1 if omitted\n"
  50. "-m ESCAPE_MODE select escape mode between 'auto', 'backslash', 'quote'\n"
  51. "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
  52. "-p PROMPT set output prompt, is '=> ' by default\n"
  53. "-s SPECIAL_CHARS set the list of special characters\n");
  54. }
  55. int main(int argc, char **argv)
  56. {
  57. AVBPrint src;
  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;
  63. int escape_flags = 0;
  64. int level = 1;
  65. int echo = 0;
  66. char *special_chars = NULL;
  67. int c;
  68. while ((c = getopt(argc, argv, "ef:hi:l:o:m:p:s:")) != -1) {
  69. switch (c) {
  70. case 'e':
  71. echo = 1;
  72. break;
  73. case 'h':
  74. usage();
  75. return 0;
  76. case 'i':
  77. infilename = optarg;
  78. break;
  79. case 'f':
  80. if (!strcmp(optarg, "whitespace")) escape_flags |= AV_ESCAPE_FLAG_WHITESPACE;
  81. else if (!strcmp(optarg, "strict")) escape_flags |= AV_ESCAPE_FLAG_STRICT;
  82. else if (!strcmp(optarg, "xml_single_quotes")) escape_flags |= AV_ESCAPE_FLAG_XML_SINGLE_QUOTES;
  83. else if (!strcmp(optarg, "xml_double_quotes")) escape_flags |= AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES;
  84. else {
  85. av_log(NULL, AV_LOG_ERROR,
  86. "Invalid value '%s' for option -f, "
  87. "valid arguments are 'whitespace', and 'strict'\n", optarg);
  88. return 1;
  89. }
  90. break;
  91. case 'l':
  92. {
  93. char *tail;
  94. long int li = strtol(optarg, &tail, 10);
  95. if (*tail || li > INT_MAX || li < 0) {
  96. av_log(NULL, AV_LOG_ERROR,
  97. "Invalid value '%s' for option -l, argument must be a non negative integer\n",
  98. optarg);
  99. return 1;
  100. }
  101. level = li;
  102. break;
  103. }
  104. case 'm':
  105. if (!strcmp(optarg, "auto")) escape_mode = AV_ESCAPE_MODE_AUTO;
  106. else if (!strcmp(optarg, "backslash")) escape_mode = AV_ESCAPE_MODE_BACKSLASH;
  107. else if (!strcmp(optarg, "quote")) escape_mode = AV_ESCAPE_MODE_QUOTE;
  108. else if (!strcmp(optarg, "xml")) escape_mode = AV_ESCAPE_MODE_XML;
  109. else {
  110. av_log(NULL, AV_LOG_ERROR,
  111. "Invalid value '%s' for option -m, "
  112. "valid arguments are 'backslash', and 'quote'\n", optarg);
  113. return 1;
  114. }
  115. break;
  116. case 'o':
  117. outfilename = optarg;
  118. break;
  119. case 'p':
  120. prompt = optarg;
  121. break;
  122. case 's':
  123. special_chars = optarg;
  124. break;
  125. case '?':
  126. return 1;
  127. }
  128. }
  129. if (!infilename || !strcmp(infilename, "-")) {
  130. infilename = "stdin";
  131. infile = stdin;
  132. } else {
  133. infile = fopen(infilename, "r");
  134. }
  135. if (!infile) {
  136. av_log(NULL, AV_LOG_ERROR, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
  137. return 1;
  138. }
  139. if (!outfilename || !strcmp(outfilename, "-")) {
  140. outfilename = "stdout";
  141. outfile = stdout;
  142. } else {
  143. outfile = fopen(outfilename, "w");
  144. }
  145. if (!outfile) {
  146. av_log(NULL, AV_LOG_ERROR, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
  147. return 1;
  148. }
  149. /* grab the input and store it in src */
  150. av_bprint_init(&src, 1, AV_BPRINT_SIZE_UNLIMITED);
  151. while ((c = fgetc(infile)) != EOF)
  152. av_bprint_chars(&src, c, 1);
  153. av_bprint_chars(&src, 0, 1);
  154. if (!av_bprint_is_complete(&src)) {
  155. av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the source string\n");
  156. av_bprint_finalize(&src, NULL);
  157. return 1;
  158. }
  159. av_bprint_finalize(&src, &src_buf);
  160. if (echo)
  161. fprintf(outfile, "%s", src_buf);
  162. /* escape */
  163. dst_buf = src_buf;
  164. while (level--) {
  165. if (av_escape(&dst_buf, src_buf, special_chars, escape_mode, escape_flags) < 0) {
  166. av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");
  167. return 1;
  168. }
  169. av_free(src_buf);
  170. src_buf = dst_buf;
  171. }
  172. fprintf(outfile, "%s%s", prompt, dst_buf);
  173. av_free(dst_buf);
  174. return 0;
  175. }