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
21 #include <unistd.h> /* getopt */
22 #include "libavutil/eval.h"
26 * simple arithmetic expression evaluator
29 static void usage(void)
31 printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
32 printf("usage: ffeval [OPTIONS]\n");
35 "-e echo each input line on output\n"
36 "-h print this help\n"
37 "-i INFILE set INFILE as input file, stdin if omitted\n"
38 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
39 "-p PROMPT set output prompt\n");
42 #define MAX_BLOCK_SIZE SIZE_MAX
44 int main(int argc, char **argv)
46 size_t buf_size = 256;
47 char *buf = av_malloc(buf_size);
48 const char *outfilename = NULL, *infilename = NULL;
49 FILE *outfile = NULL, *infile = NULL;
50 const char *prompt = "=> ";
51 int count = 0, echo = 0;
54 av_max_alloc(MAX_BLOCK_SIZE);
56 while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
78 if (!infilename || !strcmp(infilename, "-"))
79 infilename = "/dev/stdin";
80 infile = fopen(infilename, "r");
82 fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
86 if (!outfilename || !strcmp(outfilename, "-"))
87 outfilename = "/dev/stdout";
88 outfile = fopen(outfilename, "w");
90 fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
94 while ((c = fgetc(infile)) != EOF) {
100 av_expr_parse_and_eval(&d, buf,
102 NULL, NULL, NULL, NULL, NULL, 0, NULL);
104 fprintf(outfile, "%s ", buf);
105 fprintf(outfile, "%s%f\n", prompt, d);
109 if (count >= buf_size-1) {
110 if (buf_size == MAX_BLOCK_SIZE) {
111 av_log(NULL, AV_LOG_ERROR, "Memory allocation problem, "
112 "max block size '%zd' reached\n", MAX_BLOCK_SIZE);
115 buf_size = FFMIN(buf_size, MAX_BLOCK_SIZE / 2) * 2;
116 buf = av_realloc_f((void *)buf, buf_size, 1);
118 av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");