00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "config.h"
00022 #if HAVE_UNISTD_H
00023 #include <unistd.h>
00024 #endif
00025
00026 #include "libavutil/eval.h"
00027
00028 #if !HAVE_GETOPT
00029 #include "compat/getopt.c"
00030 #endif
00031
00037 static void usage(void)
00038 {
00039 printf("Simple expression evalutor, please *don't* turn me to a feature-complete language interpreter\n");
00040 printf("usage: ffeval [OPTIONS]\n");
00041 printf("\n"
00042 "Options:\n"
00043 "-e echo each input line on output\n"
00044 "-h print this help\n"
00045 "-i INFILE set INFILE as input file, stdin if omitted\n"
00046 "-o OUTFILE set OUTFILE as output file, stdout if omitted\n"
00047 "-p PROMPT set output prompt\n");
00048 }
00049
00050 #define MAX_BLOCK_SIZE SIZE_MAX
00051
00052 int main(int argc, char **argv)
00053 {
00054 size_t buf_size = 256;
00055 char *buf = av_malloc(buf_size);
00056 const char *outfilename = NULL, *infilename = NULL;
00057 FILE *outfile = NULL, *infile = NULL;
00058 const char *prompt = "=> ";
00059 int count = 0, echo = 0;
00060 int c;
00061
00062 av_max_alloc(MAX_BLOCK_SIZE);
00063
00064 while ((c = getopt(argc, argv, "ehi:o:p:")) != -1) {
00065 switch (c) {
00066 case 'e':
00067 echo = 1;
00068 break;
00069 case 'h':
00070 usage();
00071 return 0;
00072 case 'i':
00073 infilename = optarg;
00074 break;
00075 case 'o':
00076 outfilename = optarg;
00077 break;
00078 case 'p':
00079 prompt = optarg;
00080 break;
00081 case '?':
00082 return 1;
00083 }
00084 }
00085
00086 if (!infilename || !strcmp(infilename, "-")) {
00087 infilename = "stdin";
00088 infile = stdin;
00089 } else {
00090 infile = fopen(infilename, "r");
00091 }
00092 if (!infile) {
00093 fprintf(stderr, "Impossible to open input file '%s': %s\n", infilename, strerror(errno));
00094 return 1;
00095 }
00096
00097 if (!outfilename || !strcmp(outfilename, "-")) {
00098 outfilename = "stdout";
00099 outfile = stdout;
00100 } else {
00101 outfile = fopen(outfilename, "w");
00102 }
00103 if (!outfile) {
00104 fprintf(stderr, "Impossible to open output file '%s': %s\n", outfilename, strerror(errno));
00105 return 1;
00106 }
00107
00108 while ((c = fgetc(infile)) != EOF) {
00109 if (c == '\n') {
00110 double d;
00111
00112 buf[count] = 0;
00113 if (buf[0] != '#') {
00114 av_expr_parse_and_eval(&d, buf,
00115 NULL, NULL,
00116 NULL, NULL, NULL, NULL, NULL, 0, NULL);
00117 if (echo)
00118 fprintf(outfile, "%s ", buf);
00119 fprintf(outfile, "%s%f\n", prompt, d);
00120 }
00121 count = 0;
00122 } else {
00123 if (count >= buf_size-1) {
00124 if (buf_size == MAX_BLOCK_SIZE) {
00125 av_log(NULL, AV_LOG_ERROR, "Memory allocation problem, "
00126 "max block size '%zd' reached\n", MAX_BLOCK_SIZE);
00127 return 1;
00128 }
00129 buf_size = FFMIN(buf_size, MAX_BLOCK_SIZE / 2) * 2;
00130 buf = av_realloc_f((void *)buf, buf_size, 1);
00131 if (!buf) {
00132 av_log(NULL, AV_LOG_ERROR, "Memory allocation problem occurred\n");
00133 return 1;
00134 }
00135 }
00136 buf[count++] = c;
00137 }
00138 }
00139
00140 av_free(buf);
00141 return 0;
00142 }