| 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 | #include "libavutil/mem.h" |
| 28 | |
| 29 | #if !HAVE_GETOPT |
| 30 | #include "compat/getopt.c" |
| 31 | #endif |
| 32 | |
| 33 | /** |
| 34 | * @file |
| 35 | * simple arithmetic expression evaluator |
| 36 | */ |
| 37 | |
| 38 | static void usage(void) |
| 39 | { |
| 40 | printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n"); |
| 41 | printf("usage: ffeval [OPTIONS]\n"); |
| 42 | printf("\n" |
| 43 | "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"); |
| 49 | } |
| 50 | |
| 51 | int main(int argc, char **argv) |
| 52 | { |
| 53 | int buf_size = 0; |
| 54 | char *buf = NULL; |
| 55 | const char *outfilename = NULL, *infilename = NULL; |
| 56 | FILE *outfile = NULL, *infile = NULL; |
| 57 | const char *prompt = "=> "; |
| 58 | int count = 0, echo = 0; |
| 59 | int c; |
| 60 | |
| 61 | #define GROW_ARRAY() \ |
| 62 | do { \ |
| 63 | if (!av_dynarray2_add((void **)&buf, &buf_size, 1, NULL)) { \ |
| 64 | av_log(NULL, AV_LOG_ERROR, \ |
| 65 | "Memory allocation problem occurred\n"); \ |
| 66 | return 1; \ |
| 67 | } \ |
| 68 | } while (0) |
| 69 | |
| 70 | GROW_ARRAY(); |
| 71 | while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) { |
| 72 | switch (c) { |
| 73 | case 'e': |
| 74 | echo = 1; |
| 75 | break; |
| 76 | case 'h': |
| 77 | usage(); |
| 78 | return 0; |
| 79 | case 'i': |
| 80 | infilename = optarg; |
| 81 | break; |
| 82 | case 'o': |
| 83 | outfilename = optarg; |
| 84 | break; |
| 85 | case 'p': |
| 86 | prompt = optarg; |
| 87 | break; |
| 88 | case '?': |
| 89 | return 1; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (!infilename || !strcmp(infilename, "-")) { |
| 94 | infilename = "stdin"; |
| 95 | infile = stdin; |
| 96 | } else { |
| 97 | infile = fopen(infilename, "r"); |
| 98 | } |
| 99 | if (!infile) { |
| 100 | fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno)); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | if (!outfilename || !strcmp(outfilename, "-")) { |
| 105 | outfilename = "stdout"; |
| 106 | outfile = stdout; |
| 107 | } else { |
| 108 | outfile = fopen(outfilename, "w"); |
| 109 | } |
| 110 | if (!outfile) { |
| 111 | fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno)); |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | while ((c = fgetc(infile)) != EOF) { |
| 116 | if (c == '\n') { |
| 117 | double d; |
| 118 | |
| 119 | buf[count] = 0; |
| 120 | if (buf[0] != '#') { |
| 121 | int ret = av_expr_parse_and_eval(&d, buf, |
| 122 | NULL, NULL, |
| 123 | NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 124 | if (echo) |
| 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)); |
| 128 | } |
| 129 | count = 0; |
| 130 | } else { |
| 131 | if (count >= buf_size-1) |
| 132 | GROW_ARRAY(); |
| 133 | buf[count++] = c; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | av_free(buf); |
| 138 | return 0; |
| 139 | } |