ffescape.c 6.0 KB

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