2 * Copyright (c) 2012 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 #include <unistd.h> /* getopt */
26 #include "libavutil/eval.h"
27 #include "libavutil/mem.h"
30 #include "compat/getopt.c"
35 * simple arithmetic expression evaluator
38 static void usage(void)
40 printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
41 printf("usage: ffeval [OPTIONS]\n");
44 "-e echo each input line on output\n"
45 "-h print this help\n"
46 "-i INFILE set INFILE as input file, stdin if omitted\n"
47 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
48 "-p PROMPT set output prompt\n");
51 int main(int argc
, char **argv
)
55 const char *outfilename
= NULL
, *infilename
= NULL
;
56 FILE *outfile
= NULL
, *infile
= NULL
;
57 const char *prompt
= "=> ";
58 int count
= 0, echo
= 0;
61 #define GROW_ARRAY() \
63 if (!av_dynarray2_add((void **)&buf, &buf_size, 1, NULL)) { \
64 av_log(NULL, AV_LOG_ERROR, \
65 "Memory allocation problem occurred\n"); \
71 while ((c
= getopt(argc
, argv
, "ehi:o:p:")) != -1) {
93 if (!infilename
|| !strcmp(infilename
, "-")) {
97 infile
= fopen(infilename
, "r");
100 fprintf(stderr
, "Impossible to open input file '%s': %s\n", infilename
, strerror(errno
));
104 if (!outfilename
|| !strcmp(outfilename
, "-")) {
105 outfilename
= "stdout";
108 outfile
= fopen(outfilename
, "w");
111 fprintf(stderr
, "Impossible to open output file '%s': %s\n", outfilename
, strerror(errno
));
115 while ((c
= fgetc(infile
)) != EOF
) {
121 int ret
= av_expr_parse_and_eval(&d
, buf
,
123 NULL
, NULL
, NULL
, NULL
, NULL
, 0, NULL
);
125 fprintf(outfile
, "%s ", buf
);
126 if (ret
>= 0) fprintf(outfile
, "%s%f\n", prompt
, d
);
127 else fprintf(outfile
, "%s%f (%s)\n", prompt
, d
, av_err2str(ret
));
131 if (count
>= buf_size
-1)