ffescape.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 {
  77. av_log(NULL, AV_LOG_ERROR,
  78. "Invalid value '%s' for option -f, "
  79. "valid arguments are 'whitespace', and 'strict'\n", optarg);
  80. return 1;
  81. }
  82. break;
  83. case 'l':
  84. {
  85. char *tail;
  86. long int li = strtol(optarg, &tail, 10);
  87. if (*tail || li > INT_MAX || li < 0) {
  88. av_log(NULL, AV_LOG_ERROR,
  89. "Invalid value '%s' for option -l, argument must be a non negative integer\n",
  90. optarg);
  91. return 1;
  92. }
  93. level = li;
  94. break;
  95. }
  96. case 'm':
  97. if (!strcmp(optarg, "auto")) escape_mode = AV_ESCAPE_MODE_AUTO;
  98. else if (!strcmp(optarg, "backslash")) escape_mode = AV_ESCAPE_MODE_BACKSLASH;
  99. else if (!strcmp(optarg, "quote")) escape_mode = AV_ESCAPE_MODE_QUOTE;
  100. else {
  101. av_log(NULL, AV_LOG_ERROR,
  102. "Invalid value '%s' for option -m, "
  103. "valid arguments are 'backslash', and 'quote'\n", optarg);
  104. return 1;
  105. }
  106. break;
  107. case 'o':
  108. outfilename = optarg;
  109. break;
  110. case 'p':
  111. prompt = optarg;
  112. break;
  113. case 's':
  114. special_chars = optarg;
  115. break;
  116. case '?':
  117. return 1;
  118. }
  119. }
  120. if (!infilename || !strcmp(infilename, "-")) {
  121. infilename = "stdin";
  122. infile = stdin;
  123. } else {
  124. infile = fopen(infilename, "r");
  125. }
  126. if (!infile) {
  127. av_log(NULL, AV_LOG_ERROR, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
  128. return 1;
  129. }
  130. if (!outfilename || !strcmp(outfilename, "-")) {
  131. outfilename = "stdout";
  132. outfile = stdout;
  133. } else {
  134. outfile = fopen(outfilename, "w");
  135. }
  136. if (!outfile) {
  137. av_log(NULL, AV_LOG_ERROR, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
  138. return 1;
  139. }
  140. /* grab the input and store it in src */
  141. av_bprint_init(&src, 1, AV_BPRINT_SIZE_UNLIMITED);
  142. while ((c = fgetc(infile)) != EOF)
  143. av_bprint_chars(&src, c, 1);
  144. av_bprint_chars(&src, 0, 1);
  145. if (!av_bprint_is_complete(&src)) {
  146. av_log(NULL, AV_LOG_ERROR, "Could not allocate a buffer for the source string\n");
  147. av_bprint_finalize(&src, NULL);
  148. return 1;
  149. }
  150. av_bprint_finalize(&src, &src_buf);
  151. if (echo)
  152. fprintf(outfile, "%s", src_buf);
  153. /* escape */
  154. dst_buf = src_buf;
  155. while (level--) {
  156. if (av_escape(&dst_buf, src_buf, special_chars, escape_mode, escape_flags) < 0) {
  157. av_log(NULL, AV_LOG_ERROR, "Could not escape string\n");
  158. return 1;
  159. }
  160. av_free(src_buf);
  161. src_buf = dst_buf;
  162. }
  163. fprintf(outfile, "%s%s", prompt, dst_buf);
  164. av_free(dst_buf);
  165. return 0;
  166. }