You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
6.8KB

  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. "-h print this help\n"
  41. "-i INFILE set INFILE as input file, stdin if omitted\n"
  42. "-l LEVEL set the number of escaping levels, 1 if omitted\n"
  43. "-m ESCAPE_MODE select escape mode between 'full', 'lazy', 'quote', default is 'lazy'\n"
  44. "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
  45. "-p PROMPT set output prompt, is '=> ' by default\n"
  46. "-s SPECIAL_CHARS set the list of special characters\n");
  47. }
  48. #define WHITESPACES " \n\t"
  49. enum EscapeMode {
  50. ESCAPE_MODE_FULL,
  51. ESCAPE_MODE_LAZY,
  52. ESCAPE_MODE_QUOTE,
  53. };
  54. static int escape(char **dst, const char *src, const char *special_chars,
  55. enum EscapeMode mode)
  56. {
  57. AVBPrint dstbuf;
  58. av_bprint_init(&dstbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
  59. switch (mode) {
  60. case ESCAPE_MODE_FULL:
  61. case ESCAPE_MODE_LAZY:
  62. /* \-escape characters */
  63. if (mode == ESCAPE_MODE_LAZY && strchr(WHITESPACES, *src))
  64. av_bprintf(&dstbuf, "\\%c", *src++);
  65. for (; *src; src++) {
  66. if ((special_chars && strchr(special_chars, *src)) ||
  67. strchr("'\\", *src) ||
  68. (mode == ESCAPE_MODE_FULL && strchr(WHITESPACES, *src)))
  69. av_bprintf(&dstbuf, "\\%c", *src);
  70. else
  71. av_bprint_chars(&dstbuf, *src, 1);
  72. }
  73. if (mode == ESCAPE_MODE_LAZY && strchr(WHITESPACES, dstbuf.str[dstbuf.len-1])) {
  74. char c = dstbuf.str[dstbuf.len-1];
  75. dstbuf.str[dstbuf.len-1] = '\\';
  76. av_bprint_chars(&dstbuf, c, 1);
  77. }
  78. break;
  79. case ESCAPE_MODE_QUOTE:
  80. /* enclose between '' the string */
  81. av_bprint_chars(&dstbuf, '\'', 1);
  82. for (; *src; src++) {
  83. if (*src == '\'')
  84. av_bprintf(&dstbuf, "'\\''");
  85. else
  86. av_bprint_chars(&dstbuf, *src, 1);
  87. }
  88. av_bprint_chars(&dstbuf, '\'', 1);
  89. break;
  90. default:
  91. /* unknown escape mode */
  92. return AVERROR(EINVAL);
  93. }
  94. if (!av_bprint_is_complete(&dstbuf)) {
  95. av_bprint_finalize(&dstbuf, NULL);
  96. return AVERROR(ENOMEM);
  97. } else {
  98. av_bprint_finalize(&dstbuf, dst);
  99. return 0;
  100. }
  101. }
  102. int main(int argc, char **argv)
  103. {
  104. AVBPrint src;
  105. char *src_buf, *dst_buf;
  106. const char *outfilename = NULL, *infilename = NULL;
  107. FILE *outfile = NULL, *infile = NULL;
  108. const char *prompt = "=> ";
  109. enum EscapeMode escape_mode = ESCAPE_MODE_LAZY;
  110. int level = 1;
  111. int echo = 0;
  112. char *special_chars = NULL;
  113. int c;
  114. while ((c = getopt(argc, argv, "ehi:l:o:m:p:s:")) != -1) {
  115. switch (c) {
  116. case 'e':
  117. echo = 1;
  118. break;
  119. case 'h':
  120. usage();
  121. return 0;
  122. case 'i':
  123. infilename = optarg;
  124. break;
  125. case 'l':
  126. {
  127. char *tail;
  128. long int li = strtol(optarg, &tail, 10);
  129. if (*tail || li > INT_MAX || li < 0) {
  130. av_log(NULL, AV_LOG_ERROR,
  131. "Invalid value '%s' for option -l, argument must be a non negative integer\n",
  132. optarg);
  133. return 1;
  134. }
  135. level = li;
  136. break;
  137. }
  138. case 'm':
  139. if (!strcmp(optarg, "full")) escape_mode = ESCAPE_MODE_FULL;
  140. else if (!strcmp(optarg, "lazy")) escape_mode = ESCAPE_MODE_LAZY;
  141. else if (!strcmp(optarg, "quote")) escape_mode = ESCAPE_MODE_QUOTE;
  142. else {
  143. av_log(NULL, AV_LOG_ERROR,
  144. "Invalid value '%s' for option -m, "
  145. "valid arguments are 'full', 'lazy', 'quote'\n", optarg);
  146. return 1;
  147. }
  148. break;
  149. case 'o':
  150. outfilename = optarg;
  151. break;
  152. case 'p':
  153. prompt = optarg;
  154. break;
  155. case 's':
  156. special_chars = optarg;
  157. break;
  158. case '?':
  159. return 1;
  160. }
  161. }
  162. if (!infilename || !strcmp(infilename, "-")) {
  163. infilename = "stdin";
  164. infile = stdin;
  165. } else {
  166. infile = fopen(infilename, "r");
  167. }
  168. if (!infile) {
  169. av_log(NULL, AV_LOG_ERROR, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
  170. return 1;
  171. }
  172. if (!outfilename || !strcmp(outfilename, "-")) {
  173. outfilename = "stdout";
  174. outfile = stdout;
  175. } else {
  176. outfile = fopen(outfilename, "w");
  177. }
  178. if (!outfile) {
  179. av_log(NULL, AV_LOG_ERROR, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
  180. return 1;
  181. }
  182. /* grab the input and store it in src */
  183. av_bprint_init(&src, 1, AV_BPRINT_SIZE_UNLIMITED);
  184. while ((c = fgetc(infile)) != EOF)
  185. av_bprint_chars(&src, c, 1);
  186. av_bprint_chars(&src, 0, 1);
  187. if (!av_bprint_is_complete(&src)) {
  188. av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the source string\n");
  189. av_bprint_finalize(&src, NULL);
  190. return 1;
  191. }
  192. av_bprint_finalize(&src, &src_buf);
  193. if (echo)
  194. fprintf(outfile, "%s", src_buf);
  195. /* escape */
  196. dst_buf = src_buf;
  197. while (level--) {
  198. if (escape(&dst_buf, src_buf, special_chars, escape_mode) < 0) {
  199. av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");
  200. return 1;
  201. }
  202. av_free(src_buf);
  203. src_buf = dst_buf;
  204. }
  205. fprintf(outfile, "%s%s", prompt, dst_buf);
  206. av_free(dst_buf);
  207. return 0;
  208. }