]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - cmdutils.c
various security fixes and precautionary checks
[frescor/ffmpeg.git] / cmdutils.c
1 /*
2  * Various utilities for command line tools
3  * Copyright (c) 2000-2003 Fabrice Bellard
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #define HAVE_AV_CONFIG_H
20 #include "avformat.h"
21 #include "common.h"
22
23 #include "cmdutils.h"
24
25 void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
26 {
27     const OptionDef *po;
28     int first;
29
30     first = 1;
31     for(po = options; po->name != NULL; po++) {
32         char buf[64];
33         if ((po->flags & mask) == value) {
34             if (first) {
35                 printf("%s", msg);
36                 first = 0;
37             }
38             pstrcpy(buf, sizeof(buf), po->name);
39             if (po->flags & HAS_ARG) {
40                 pstrcat(buf, sizeof(buf), " ");
41                 pstrcat(buf, sizeof(buf), po->argname);
42             }
43             printf("-%-17s  %s\n", buf, po->help);
44         }
45     }
46 }
47
48 void parse_options(int argc, char **argv, const OptionDef *options)
49 {
50     const char *opt, *arg;
51     int optindex;
52     const OptionDef *po;
53
54     /* parse options */
55     optindex = 1;
56     while (optindex < argc) {
57         opt = argv[optindex++];
58         
59         if (opt[0] == '-' && opt[1] != '\0') {
60             po = options;
61             while (po->name != NULL) {
62                 if (!strcmp(opt + 1, po->name))
63                     break;
64                 po++;
65             }
66             if (!po->name) {
67                 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
68                 exit(1);
69             }
70             arg = NULL;
71             if (po->flags & HAS_ARG) {
72                 arg = argv[optindex++];
73                 if (!arg) {
74                     fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
75                     exit(1);
76                 }
77             }
78             if (po->flags & OPT_STRING) {
79                 char *str;
80                 str = av_strdup(arg);
81                 *po->u.str_arg = str;
82             } else if (po->flags & OPT_BOOL) {
83                 *po->u.int_arg = 1;
84             } else if (po->flags & OPT_INT) {
85                 *po->u.int_arg = atoi(arg);
86             } else if (po->flags & OPT_FLOAT) {
87                 *po->u.float_arg = atof(arg);
88             } else {
89                 po->u.func_arg(arg);
90             }
91         } else {
92             parse_arg_file(opt);
93         }
94     }
95 }
96
97 void print_error(const char *filename, int err)
98 {
99     switch(err) {
100     case AVERROR_NUMEXPECTED:
101         fprintf(stderr, "%s: Incorrect image filename syntax.\n"
102                 "Use '%%d' to specify the image number:\n"
103                 "  for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
104                 "  for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n", 
105                 filename);
106         break;
107     case AVERROR_INVALIDDATA:
108         fprintf(stderr, "%s: Error while parsing header\n", filename);
109         break;
110     case AVERROR_NOFMT:
111         fprintf(stderr, "%s: Unknown format\n", filename);
112         break;
113     default:
114         fprintf(stderr, "%s: Error while opening file\n", filename);
115         break;
116     }
117 }