]> git.sesse.net Git - ffmpeg/blob - tools/ffeval.c
Merge commit 'f1d8763a02b5fce9a7d9789e049d74a45b15e1e8'
[ffmpeg] / tools / ffeval.c
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
21 #include "config.h"
22 #if HAVE_UNISTD_H
23 #include <unistd.h>             /* getopt */
24 #endif
25
26 #include "libavutil/eval.h"
27
28 #if !HAVE_GETOPT
29 #include "compat/getopt.c"
30 #endif
31
32 /**
33  * @file
34  * simple arithmetic expression evaluator
35  */
36
37 static void usage(void)
38 {
39     printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
40     printf("usage: ffeval [OPTIONS]\n");
41     printf("\n"
42            "Options:\n"
43            "-e                echo each input line on output\n"
44            "-h                print this help\n"
45            "-i INFILE         set INFILE as input file, stdin if omitted\n"
46            "-o OUTFILE        set OUTFILE as output file, stdout if omitted\n"
47            "-p PROMPT         set output prompt\n");
48 }
49
50 #define MAX_BLOCK_SIZE SIZE_MAX
51
52 int main(int argc, char **argv)
53 {
54     size_t buf_size = 256;
55     char *buf = av_malloc(buf_size);
56     const char *outfilename = NULL, *infilename = NULL;
57     FILE *outfile = NULL, *infile = NULL;
58     const char *prompt = "=> ";
59     int count = 0, echo = 0;
60     int c;
61
62     av_max_alloc(MAX_BLOCK_SIZE);
63
64     while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
65         switch (c) {
66         case 'e':
67             echo = 1;
68             break;
69         case 'h':
70             usage();
71             return 0;
72         case 'i':
73             infilename = optarg;
74             break;
75         case 'o':
76             outfilename = optarg;
77             break;
78         case 'p':
79             prompt = optarg;
80             break;
81         case '?':
82             return 1;
83         }
84     }
85
86     if (!infilename || !strcmp(infilename, "-")) {
87         infilename = "stdin";
88         infile = stdin;
89     } else {
90         infile = fopen(infilename, "r");
91     }
92     if (!infile) {
93         fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
94         return 1;
95     }
96
97     if (!outfilename || !strcmp(outfilename, "-")) {
98         outfilename = "stdout";
99         outfile = stdout;
100     } else {
101         outfile = fopen(outfilename, "w");
102     }
103     if (!outfile) {
104         fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
105         return 1;
106     }
107
108     while ((c = fgetc(infile)) != EOF) {
109         if (c == '\n') {
110             double d;
111
112             buf[count] = 0;
113             if (buf[0] != '#') {
114                 av_expr_parse_and_eval(&d, buf,
115                                        NULL, NULL,
116                                        NULL, NULL, NULL, NULL, NULL, 0, NULL);
117                 if (echo)
118                     fprintf(outfile, "%s ", buf);
119                 fprintf(outfile, "%s%f\n", prompt, d);
120             }
121             count = 0;
122         } else {
123             if (count >= buf_size-1) {
124                 if (buf_size == MAX_BLOCK_SIZE) {
125                     av_log(NULL, AV_LOG_ERROR, "Memory allocation problem, "
126                            "max block size '%zd' reached\n", MAX_BLOCK_SIZE);
127                     return 1;
128                 }
129                 buf_size = FFMIN(buf_size, MAX_BLOCK_SIZE / 2) * 2;
130                 buf = av_realloc_f((void *)buf, buf_size, 1);
131                 if (!buf) {
132                     av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");
133                     return 1;
134                 }
135             }
136             buf[count++] = c;
137         }
138     }
139
140     av_free(buf);
141     return 0;
142 }