cmdutils.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Various utilities for command line tools
  3. * Copyright (c) 2000-2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include "avformat.h"
  25. #include "cmdutils.h"
  26. #include "avstring.h"
  27. #include "version.h"
  28. #include "config.h"
  29. #undef exit
  30. void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
  31. {
  32. const OptionDef *po;
  33. int first;
  34. first = 1;
  35. for(po = options; po->name != NULL; po++) {
  36. char buf[64];
  37. if ((po->flags & mask) == value) {
  38. if (first) {
  39. printf("%s", msg);
  40. first = 0;
  41. }
  42. av_strlcpy(buf, po->name, sizeof(buf));
  43. if (po->flags & HAS_ARG) {
  44. av_strlcat(buf, " ", sizeof(buf));
  45. av_strlcat(buf, po->argname, sizeof(buf));
  46. }
  47. printf("-%-17s %s\n", buf, po->help);
  48. }
  49. }
  50. }
  51. static const OptionDef* find_option(const OptionDef *po, const char *name){
  52. while (po->name != NULL) {
  53. if (!strcmp(name, po->name))
  54. break;
  55. po++;
  56. }
  57. return po;
  58. }
  59. void parse_options(int argc, char **argv, const OptionDef *options,
  60. void (* parse_arg_function)(const char*))
  61. {
  62. const char *opt, *arg;
  63. int optindex, handleoptions=1;
  64. const OptionDef *po;
  65. /* parse options */
  66. optindex = 1;
  67. while (optindex < argc) {
  68. opt = argv[optindex++];
  69. if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
  70. if (opt[1] == '-' && opt[2] == '\0') {
  71. handleoptions = 0;
  72. continue;
  73. }
  74. po= find_option(options, opt + 1);
  75. if (!po->name)
  76. po= find_option(options, "default");
  77. if (!po->name) {
  78. unknown_opt:
  79. fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
  80. exit(1);
  81. }
  82. arg = NULL;
  83. if (po->flags & HAS_ARG) {
  84. arg = argv[optindex++];
  85. if (!arg) {
  86. fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
  87. exit(1);
  88. }
  89. }
  90. if (po->flags & OPT_STRING) {
  91. char *str;
  92. str = av_strdup(arg);
  93. *po->u.str_arg = str;
  94. } else if (po->flags & OPT_BOOL) {
  95. *po->u.int_arg = 1;
  96. } else if (po->flags & OPT_INT) {
  97. *po->u.int_arg = atoi(arg);
  98. } else if (po->flags & OPT_INT64) {
  99. *po->u.int64_arg = strtoll(arg, (char **)NULL, 10);
  100. } else if (po->flags & OPT_FLOAT) {
  101. *po->u.float_arg = atof(arg);
  102. } else if (po->flags & OPT_FUNC2) {
  103. if(po->u.func2_arg(opt+1, arg)<0)
  104. goto unknown_opt;
  105. } else {
  106. po->u.func_arg(arg);
  107. }
  108. } else {
  109. if (parse_arg_function)
  110. parse_arg_function(opt);
  111. }
  112. }
  113. }
  114. void print_error(const char *filename, int err)
  115. {
  116. switch(err) {
  117. case AVERROR_NUMEXPECTED:
  118. fprintf(stderr, "%s: Incorrect image filename syntax.\n"
  119. "Use '%%d' to specify the image number:\n"
  120. " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
  121. " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
  122. filename);
  123. break;
  124. case AVERROR_INVALIDDATA:
  125. fprintf(stderr, "%s: Error while parsing header\n", filename);
  126. break;
  127. case AVERROR_NOFMT:
  128. fprintf(stderr, "%s: Unknown format\n", filename);
  129. break;
  130. case AVERROR(EIO):
  131. fprintf(stderr, "%s: I/O error occured\n"
  132. "Usually that means that input file is truncated and/or corrupted.\n",
  133. filename);
  134. break;
  135. case AVERROR(ENOMEM):
  136. fprintf(stderr, "%s: memory allocation error occured\n", filename);
  137. break;
  138. case AVERROR(ENOENT):
  139. fprintf(stderr, "%s: no such file or directory\n", filename);
  140. break;
  141. default:
  142. fprintf(stderr, "%s: Error while opening file\n", filename);
  143. break;
  144. }
  145. }
  146. void show_banner(const char *program_name, int program_birth_year)
  147. {
  148. fprintf(stderr, "%s version " FFMPEG_VERSION ", Copyright (c) %d-2008 Fabrice Bellard, et al.\n",
  149. program_name, program_birth_year);
  150. fprintf(stderr, " configuration: " FFMPEG_CONFIGURATION "\n");
  151. fprintf(stderr, " libavutil version: " AV_STRINGIFY(LIBAVUTIL_VERSION) "\n");
  152. fprintf(stderr, " libavcodec version: " AV_STRINGIFY(LIBAVCODEC_VERSION) "\n");
  153. fprintf(stderr, " libavformat version: " AV_STRINGIFY(LIBAVFORMAT_VERSION) "\n");
  154. fprintf(stderr, " built on " __DATE__ " " __TIME__);
  155. #ifdef __GNUC__
  156. fprintf(stderr, ", gcc: " __VERSION__ "\n");
  157. #else
  158. fprintf(stderr, ", using a non-gcc compiler\n");
  159. #endif
  160. }
  161. void show_version(const char *program_name) {
  162. /* TODO: add function interface to avutil and avformat */
  163. printf("%s " FFMPEG_VERSION "\n", program_name);
  164. printf("libavutil %d\n"
  165. "libavcodec %d\n"
  166. "libavformat %d\n",
  167. LIBAVUTIL_BUILD, avcodec_build(), LIBAVFORMAT_BUILD);
  168. }
  169. void show_license(void)
  170. {
  171. #ifdef CONFIG_GPL
  172. printf(
  173. "FFmpeg is free software; you can redistribute it and/or modify\n"
  174. "it under the terms of the GNU General Public License as published by\n"
  175. "the Free Software Foundation; either version 2 of the License, or\n"
  176. "(at your option) any later version.\n"
  177. "\n"
  178. "FFmpeg is distributed in the hope that it will be useful,\n"
  179. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  180. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  181. "GNU General Public License for more details.\n"
  182. "\n"
  183. "You should have received a copy of the GNU General Public License\n"
  184. "along with FFmpeg; if not, write to the Free Software\n"
  185. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
  186. );
  187. #else
  188. printf(
  189. "FFmpeg is free software; you can redistribute it and/or\n"
  190. "modify it under the terms of the GNU Lesser General Public\n"
  191. "License as published by the Free Software Foundation; either\n"
  192. "version 2.1 of the License, or (at your option) any later version.\n"
  193. "\n"
  194. "FFmpeg is distributed in the hope that it will be useful,\n"
  195. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  196. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
  197. "Lesser General Public License for more details.\n"
  198. "\n"
  199. "You should have received a copy of the GNU Lesser General Public\n"
  200. "License along with FFmpeg; if not, write to the Free Software\n"
  201. "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n"
  202. );
  203. #endif
  204. }