2004-10-13 11:07:56 -07:00
|
|
|
/*
|
2004-10-19 09:50:27 -07:00
|
|
|
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
|
2004-10-13 11:07:56 -07:00
|
|
|
*/
|
|
|
|
|
2003-06-11 06:20:48 -07:00
|
|
|
#include "edje_cc.h"
|
|
|
|
|
|
|
|
static void new_object(void);
|
|
|
|
static void new_statement(void);
|
|
|
|
static int isdelim(char c);
|
|
|
|
static char *next_token(char *p, char *end, char **new_p, int *delim);
|
|
|
|
static char *stack_id(void);
|
|
|
|
static void stack_chop_top(void);
|
|
|
|
static void parse(char *data, off_t size);
|
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
/* simple expression parsing protos */
|
|
|
|
static int my_atoi(const char * s);
|
|
|
|
static char * _alphai(char *s, int * val);
|
|
|
|
static char * _betai(char *s, int * val);
|
|
|
|
static char * _gammai(char *s, int * val);
|
|
|
|
static char * _deltai(char *s, int * val);
|
|
|
|
static char * _get_numi(char *s, int * val);
|
|
|
|
static int _is_numi(char c);
|
|
|
|
static int _is_op1i(char c);
|
|
|
|
static int _is_op2i(char c);
|
|
|
|
static int _calci(char op, int a, int b);
|
|
|
|
|
|
|
|
static double my_atof(const char * s);
|
|
|
|
static char * _alphaf(char *s, double * val);
|
|
|
|
static char * _betaf(char *s, double * val);
|
|
|
|
static char * _gammaf(char *s, double * val);
|
|
|
|
static char * _deltaf(char *s, double * val);
|
|
|
|
static char * _get_numf(char *s, double * val);
|
|
|
|
static int _is_numf(char c);
|
|
|
|
static int _is_op1f(char c);
|
|
|
|
static int _is_op2f(char c);
|
|
|
|
static double _calcf(char op, double a, double b);
|
2004-10-13 11:07:56 -07:00
|
|
|
static int strstrip(const char *in, char *out, size_t size);
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
|
2003-06-11 06:20:48 -07:00
|
|
|
int line = 0;
|
|
|
|
Evas_List *stack = NULL;
|
|
|
|
Evas_List *params = NULL;
|
|
|
|
|
2004-03-27 21:26:17 -08:00
|
|
|
static char file_buf[4096];
|
|
|
|
static int verbatim = 0;
|
2004-04-24 21:20:58 -07:00
|
|
|
static int verbatim_line1 = 0;
|
|
|
|
static int verbatim_line2 = 0;
|
2004-03-27 21:26:17 -08:00
|
|
|
static char *verbatim_str = NULL;
|
2003-08-29 01:34:53 -07:00
|
|
|
|
2003-06-11 06:20:48 -07:00
|
|
|
static void
|
|
|
|
new_object(void)
|
|
|
|
{
|
|
|
|
char *id;
|
|
|
|
int i;
|
2003-06-11 07:14:23 -07:00
|
|
|
int handled = 0;
|
2003-06-11 06:20:48 -07:00
|
|
|
|
|
|
|
id = stack_id();
|
|
|
|
for (i = 0; i < object_handler_num(); i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(object_handlers[i].type, id))
|
|
|
|
{
|
2003-06-11 07:14:23 -07:00
|
|
|
handled = 1;
|
|
|
|
if (object_handlers[i].func)
|
|
|
|
{
|
|
|
|
object_handlers[i].func();
|
|
|
|
}
|
|
|
|
break;
|
2003-06-11 06:20:48 -07:00
|
|
|
}
|
|
|
|
}
|
2004-10-05 22:25:03 -07:00
|
|
|
if (!handled)
|
|
|
|
{
|
|
|
|
for (i = 0; i < statement_handler_num(); i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(statement_handlers[i].type, id))
|
|
|
|
{
|
2004-10-19 10:39:35 -07:00
|
|
|
free(id);
|
2004-10-05 22:25:03 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-11 07:14:23 -07:00
|
|
|
if (!handled)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i unhandled keyword %s\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1,
|
2003-10-18 19:11:06 -07:00
|
|
|
(char *)evas_list_data(evas_list_last(stack)));
|
2003-06-11 07:14:23 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
free(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
new_statement(void)
|
|
|
|
{
|
|
|
|
char *id;
|
|
|
|
int i;
|
2003-06-11 07:14:23 -07:00
|
|
|
int handled = 0;
|
2003-06-11 06:20:48 -07:00
|
|
|
|
|
|
|
id = stack_id();
|
|
|
|
for (i = 0; i < statement_handler_num(); i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(statement_handlers[i].type, id))
|
|
|
|
{
|
2003-06-11 07:14:23 -07:00
|
|
|
handled = 1;
|
|
|
|
if (statement_handlers[i].func)
|
|
|
|
{
|
|
|
|
statement_handlers[i].func();
|
|
|
|
}
|
|
|
|
break;
|
2003-06-11 06:20:48 -07:00
|
|
|
}
|
2003-06-11 07:14:23 -07:00
|
|
|
}
|
|
|
|
if (!handled)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i unhandled keyword %s\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1,
|
2003-10-18 19:11:06 -07:00
|
|
|
(char *)evas_list_data(evas_list_last(stack)));
|
2003-06-11 07:14:23 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
free(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
isdelim(char c)
|
|
|
|
{
|
2004-01-15 16:51:43 -08:00
|
|
|
const char *delims = "{},;:";
|
2003-06-11 06:20:48 -07:00
|
|
|
char *d;
|
|
|
|
|
|
|
|
d = (char *)delims;
|
|
|
|
while (*d)
|
|
|
|
{
|
|
|
|
if (c == *d) return 1;
|
|
|
|
d++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
next_token(char *p, char *end, char **new_p, int *delim)
|
|
|
|
{
|
|
|
|
char *tok_start = NULL, *tok_end = NULL, *tok = NULL, *sa_start = NULL;
|
|
|
|
int in_tok = 0;
|
|
|
|
int in_quote = 0;
|
2003-08-29 01:34:53 -07:00
|
|
|
int in_comment_ss = 0;
|
|
|
|
int in_comment_cpp = 0;
|
|
|
|
int in_comment_sa = 0;
|
2003-06-11 06:20:48 -07:00
|
|
|
int had_quote = 0;
|
2003-08-29 01:34:53 -07:00
|
|
|
char *cpp_token_line = NULL;
|
|
|
|
char *cpp_token_file = NULL;
|
2003-06-11 06:20:48 -07:00
|
|
|
|
|
|
|
*delim = 0;
|
|
|
|
if (p >= end) return NULL;
|
|
|
|
while (p < end)
|
|
|
|
{
|
|
|
|
if (*p == '\n')
|
|
|
|
{
|
|
|
|
in_comment_ss = 0;
|
2003-08-29 01:34:53 -07:00
|
|
|
in_comment_cpp = 0;
|
|
|
|
cpp_token_line = NULL;
|
|
|
|
cpp_token_file = NULL;
|
2003-06-11 06:20:48 -07:00
|
|
|
line++;
|
|
|
|
}
|
|
|
|
if ((!in_comment_ss) && (!in_comment_sa))
|
|
|
|
{
|
|
|
|
if ((!in_quote) && (*p == '/') && (p < (end - 1)) && (*(p + 1) == '/'))
|
|
|
|
in_comment_ss = 1;
|
2003-07-29 19:58:22 -07:00
|
|
|
if ((!in_quote) && (*p == '#'))
|
2003-08-29 01:34:53 -07:00
|
|
|
in_comment_cpp = 1;
|
2003-06-11 06:20:48 -07:00
|
|
|
if ((!in_quote) && (*p == '/') && (p < (end - 1)) && (*(p + 1) == '*'))
|
|
|
|
{
|
|
|
|
in_comment_sa = 1;
|
|
|
|
sa_start = p;
|
|
|
|
}
|
|
|
|
}
|
2003-08-29 01:34:53 -07:00
|
|
|
if ((in_comment_cpp) && (*p == '#'))
|
|
|
|
{
|
|
|
|
char *pp, fl[4096];
|
|
|
|
char *tmpstr = NULL;
|
|
|
|
int l, nm;
|
|
|
|
|
|
|
|
/* handle cpp comments */
|
|
|
|
/* their line format is
|
|
|
|
* # <line no. of next line> <filename from next line on> [??]
|
|
|
|
*/
|
|
|
|
cpp_token_line = NULL;
|
|
|
|
cpp_token_file = NULL;
|
|
|
|
|
|
|
|
pp = p;
|
|
|
|
while ((pp < end) && (*pp != '\n'))
|
|
|
|
{
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
l = pp - p;
|
|
|
|
tmpstr = malloc(l + 1);
|
|
|
|
if (!tmpstr)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i malloc %i bytes failed\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, l + 1);
|
2003-08-29 01:34:53 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
strncpy(tmpstr, p, l);
|
|
|
|
tmpstr[l] = 0;
|
|
|
|
l = sscanf(tmpstr, "%*s %i \"%[^\"]\"", &nm, fl);
|
|
|
|
if (l == 2)
|
|
|
|
{
|
|
|
|
strcpy(file_buf, fl);
|
|
|
|
line = nm;
|
|
|
|
file_in = file_buf;
|
|
|
|
}
|
|
|
|
free(tmpstr);
|
|
|
|
}
|
|
|
|
else if ((!in_comment_ss) && (!in_comment_sa) && (!in_comment_cpp))
|
2003-06-11 06:20:48 -07:00
|
|
|
{
|
|
|
|
if (!in_tok)
|
|
|
|
{
|
|
|
|
if (!in_quote)
|
|
|
|
{
|
|
|
|
if (!isspace(*p))
|
|
|
|
{
|
|
|
|
if (*p == '"')
|
2003-06-19 16:56:27 -07:00
|
|
|
{
|
|
|
|
in_quote = 1;
|
|
|
|
had_quote = 1;
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
in_tok = 1;
|
|
|
|
tok_start = p;
|
|
|
|
if (isdelim(*p)) *delim = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (in_quote)
|
|
|
|
{
|
|
|
|
if (((*p) == '"') && (*(p - 1) != '\\'))
|
2003-06-19 16:56:27 -07:00
|
|
|
{
|
|
|
|
in_quote = 0;
|
|
|
|
had_quote = 1;
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*p == '"')
|
|
|
|
{
|
|
|
|
in_quote = 1;
|
|
|
|
had_quote = 1;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(isspace(*p)) ||
|
|
|
|
((*delim) && (!isdelim(*p))) ||
|
|
|
|
(isdelim(*p))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
in_tok = 0;
|
|
|
|
tok_end = p - 1;
|
2003-06-11 07:14:23 -07:00
|
|
|
if (*p == '\n') line--;
|
2003-06-11 06:20:48 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (in_comment_sa)
|
|
|
|
{
|
|
|
|
if ((*p == '/') && (*(p - 1) == '*') && ((p - sa_start) > 2))
|
|
|
|
in_comment_sa = 0;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (!in_tok) return NULL;
|
|
|
|
tok_end = p - 1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
*new_p = p;
|
|
|
|
|
2003-06-12 15:34:51 -07:00
|
|
|
tok = mem_alloc(tok_end - tok_start + 2);
|
2003-06-11 06:20:48 -07:00
|
|
|
strncpy(tok, tok_start, tok_end - tok_start + 1);
|
|
|
|
tok[tok_end - tok_start + 1] = 0;
|
|
|
|
|
|
|
|
if (had_quote)
|
|
|
|
{
|
|
|
|
p = tok;
|
|
|
|
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
if (*p == '"')
|
2004-01-17 04:39:01 -08:00
|
|
|
memmove(p, p + 1, strlen(p));
|
2003-06-11 06:20:48 -07:00
|
|
|
else if ((*p == '\\') && (*(p + 1) == '"'))
|
2004-01-17 04:39:01 -08:00
|
|
|
memmove(p, p + 1, strlen(p));
|
2003-06-11 06:20:48 -07:00
|
|
|
else if ((*p == '\\') && (*(p + 1) == '\\'))
|
2004-01-17 04:39:01 -08:00
|
|
|
memmove(p, p + 1, strlen(p));
|
2003-06-19 16:56:27 -07:00
|
|
|
else
|
|
|
|
p++;
|
2003-06-11 06:20:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
stack_id(void)
|
|
|
|
{
|
|
|
|
char *id;
|
|
|
|
int len;
|
|
|
|
Evas_List *l;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
for (l = stack; l; l = l->next)
|
|
|
|
len += strlen(l->data) + 1;
|
2003-06-12 15:34:51 -07:00
|
|
|
id = mem_alloc(len);
|
2003-06-11 06:20:48 -07:00
|
|
|
id[0] = 0;
|
|
|
|
for (l = stack; l; l = l->next)
|
|
|
|
{
|
|
|
|
strcat(id, l->data);
|
|
|
|
if (l->next) strcat(id, ".");
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stack_chop_top(void)
|
|
|
|
{
|
|
|
|
char *top;
|
|
|
|
|
|
|
|
/* remove top from stack */
|
|
|
|
top = evas_list_data(evas_list_last(stack));
|
|
|
|
if (top)
|
|
|
|
{
|
|
|
|
free(top);
|
|
|
|
stack = evas_list_remove(stack, top);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. parse error %s:%i. } marker without matching { marker\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2003-06-11 06:20:48 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse(char *data, off_t size)
|
|
|
|
{
|
|
|
|
char *p, *end, *token;
|
|
|
|
int delim = 0;
|
|
|
|
int do_params = 0;
|
2003-06-16 06:55:13 -07:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("%s: Parsing input file\n",
|
|
|
|
progname);
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
p = data;
|
|
|
|
end = data + size;
|
|
|
|
line = 1;
|
|
|
|
while ((token = next_token(p, end, &p, &delim)) != NULL)
|
|
|
|
{
|
2004-10-23 18:10:04 -07:00
|
|
|
/* if we are in param mode, the only delimiter
|
|
|
|
* we'll accept is the semicolon
|
|
|
|
*/
|
|
|
|
if (do_params && delim && *token != ';')
|
2003-06-11 06:20:48 -07:00
|
|
|
{
|
2004-10-23 18:10:04 -07:00
|
|
|
fprintf(stderr, "%s: Error. parse error %s:%i. %c marker before ; marker\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, *token);
|
2004-10-23 18:10:04 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
else if (delim)
|
|
|
|
{
|
|
|
|
if (*token == ',' || *token == ':') do_params = 1;
|
|
|
|
else if (*token == '}')
|
2003-06-11 06:20:48 -07:00
|
|
|
{
|
|
|
|
if (do_params)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. parse error %s:%i. } marker before ; marker\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2003-06-11 06:20:48 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stack_chop_top();
|
|
|
|
}
|
2004-10-23 18:10:04 -07:00
|
|
|
else if (*token == ';')
|
2003-06-11 06:20:48 -07:00
|
|
|
{
|
|
|
|
if (do_params)
|
|
|
|
{
|
|
|
|
do_params = 0;
|
|
|
|
new_statement();
|
|
|
|
/* clear out params */
|
|
|
|
while (params)
|
|
|
|
{
|
|
|
|
free(params->data);
|
|
|
|
params = evas_list_remove(params, params->data);
|
|
|
|
}
|
|
|
|
/* remove top from stack */
|
|
|
|
stack_chop_top();
|
|
|
|
}
|
|
|
|
}
|
2004-10-23 18:10:04 -07:00
|
|
|
else if (*token == '{')
|
2003-06-11 06:20:48 -07:00
|
|
|
{
|
|
|
|
if (do_params)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. parse error %s:%i. { marker before ; marker\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2003-06-11 06:20:48 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(token);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (do_params)
|
|
|
|
params = evas_list_append(params, token);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack = evas_list_append(stack, token);
|
|
|
|
new_object();
|
2004-03-27 21:26:17 -08:00
|
|
|
if ((verbatim == 1) && (p < (end - 2)))
|
|
|
|
{
|
|
|
|
int escaped = 0;
|
|
|
|
int inquotes = 0;
|
|
|
|
int insquotes = 0;
|
|
|
|
int squigglie = 1;
|
2004-04-24 21:20:58 -07:00
|
|
|
int l1 = 0, l2 = 0;
|
2004-03-27 21:26:17 -08:00
|
|
|
char *verbatim_1;
|
|
|
|
char *verbatim_2;
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
l1 = line;
|
|
|
|
while ((p[0] != '{') && (p < end))
|
|
|
|
{
|
|
|
|
if (*p == '\n') line++;
|
|
|
|
p++;
|
|
|
|
}
|
2004-03-27 21:26:17 -08:00
|
|
|
p++;
|
|
|
|
verbatim_1 = p;
|
|
|
|
verbatim_2 = NULL;
|
|
|
|
for (; p < end; p++)
|
|
|
|
{
|
2004-04-24 21:20:58 -07:00
|
|
|
if (*p == '\n') line++;
|
2004-03-27 21:26:17 -08:00
|
|
|
if (escaped) escaped = 0;
|
|
|
|
if (!escaped)
|
|
|
|
{
|
|
|
|
if (p[0] == '\\') escaped = 1;
|
|
|
|
else if (p[0] == '\"')
|
|
|
|
{
|
|
|
|
if (!insquotes)
|
|
|
|
{
|
|
|
|
if (inquotes) inquotes = 0;
|
|
|
|
else inquotes = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p[0] == '\'')
|
|
|
|
{
|
|
|
|
if (!inquotes)
|
|
|
|
{
|
|
|
|
if (insquotes) insquotes = 0;
|
|
|
|
else insquotes = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((!inquotes) && (!insquotes))
|
|
|
|
{
|
|
|
|
if (p[0] == '{') squigglie++;
|
|
|
|
else if (p[0] == '}') squigglie--;
|
|
|
|
if (squigglie == 0)
|
|
|
|
{
|
|
|
|
verbatim_2 = p - 1;
|
2004-04-24 21:20:58 -07:00
|
|
|
l2 = line;
|
2004-03-27 21:26:17 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (verbatim_2 > verbatim_1)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
char *v;
|
|
|
|
|
|
|
|
l = verbatim_2 - verbatim_1 + 1;
|
|
|
|
v = malloc(l + 1);
|
|
|
|
strncpy(v, verbatim_1, l);
|
|
|
|
v[l] = 0;
|
2004-04-24 21:20:58 -07:00
|
|
|
set_verbatim(v, l1, l2);
|
2004-03-27 21:26:17 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. parse error %s:%i. { marker does not have matching } marker\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2004-03-27 21:26:17 -08:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
new_object();
|
|
|
|
verbatim = 0;
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-16 06:55:13 -07:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("%s: Parsing done\n",
|
|
|
|
progname);
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
}
|
|
|
|
|
2003-08-29 01:34:53 -07:00
|
|
|
static char *clean_file = NULL;
|
|
|
|
static void
|
|
|
|
clean_tmp_file(void)
|
|
|
|
{
|
|
|
|
if (clean_file) unlink(clean_file);
|
|
|
|
}
|
|
|
|
|
2004-03-27 21:26:17 -08:00
|
|
|
int
|
|
|
|
is_verbatim(void)
|
|
|
|
{
|
|
|
|
return verbatim;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
track_verbatim(int on)
|
|
|
|
{
|
|
|
|
verbatim = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-04-24 21:20:58 -07:00
|
|
|
set_verbatim(char *s, int l1, int l2)
|
2004-03-27 21:26:17 -08:00
|
|
|
{
|
2004-04-24 21:20:58 -07:00
|
|
|
verbatim_line1 = l1;
|
|
|
|
verbatim_line2 = l2;
|
2004-03-27 21:26:17 -08:00
|
|
|
verbatim_str = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
get_verbatim(void)
|
|
|
|
{
|
|
|
|
return verbatim_str;
|
|
|
|
}
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
int
|
|
|
|
get_verbatim_line1(void)
|
|
|
|
{
|
|
|
|
return verbatim_line1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
get_verbatim_line2(void)
|
|
|
|
{
|
|
|
|
return verbatim_line2;
|
|
|
|
}
|
|
|
|
|
2003-06-11 06:20:48 -07:00
|
|
|
void
|
|
|
|
compile(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
off_t size;
|
|
|
|
char *data;
|
2003-08-29 01:34:53 -07:00
|
|
|
char buf[4096];
|
|
|
|
static char tmpn[4096];
|
2003-06-11 06:20:48 -07:00
|
|
|
|
2003-08-29 01:34:53 -07:00
|
|
|
strcpy(tmpn, "/tmp/edje_cc.edc-tmp-XXXXXX");
|
|
|
|
fd = mkstemp(tmpn);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
int ret;
|
2004-02-18 03:35:08 -08:00
|
|
|
|
2003-08-29 01:34:53 -07:00
|
|
|
clean_file = tmpn;
|
|
|
|
close(fd);
|
|
|
|
atexit(clean_tmp_file);
|
2004-04-19 20:14:46 -07:00
|
|
|
snprintf(buf, sizeof(buf), "cat %s | cpp -E -o %s", file_in, tmpn);
|
2003-08-29 01:34:53 -07:00
|
|
|
ret = system(buf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2004-02-18 03:35:08 -08:00
|
|
|
snprintf(buf, sizeof(buf), "gcc -E -o %s %s", tmpn, file_in);
|
2003-08-29 01:34:53 -07:00
|
|
|
ret = system(buf);
|
|
|
|
}
|
|
|
|
if (ret >= 0) file_in = tmpn;
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
fd = open(file_in, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2003-06-16 06:55:13 -07:00
|
|
|
fprintf(stderr, "%s: Error. cannot open file \"%s\" for input. %s\n",
|
2003-06-11 06:20:48 -07:00
|
|
|
progname, file_in, strerror(errno));
|
|
|
|
exit(-1);
|
|
|
|
}
|
2003-06-16 06:55:13 -07:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
printf("%s: Opening \"%s\" for input\n",
|
|
|
|
progname, file_in);
|
|
|
|
}
|
2003-06-11 06:20:48 -07:00
|
|
|
|
|
|
|
size = lseek(fd, 0, SEEK_END);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
2004-10-20 09:48:58 -07:00
|
|
|
data = malloc(size);
|
|
|
|
if (data && read(fd, data, size) == size)
|
2003-06-11 06:20:48 -07:00
|
|
|
parse(data, size);
|
|
|
|
else
|
|
|
|
{
|
2004-10-20 09:48:58 -07:00
|
|
|
fprintf(stderr, "%s: Error. cannot read file \"%s\". %s\n",
|
2003-06-11 06:20:48 -07:00
|
|
|
progname, file_in, strerror(errno));
|
|
|
|
exit(-1);
|
|
|
|
}
|
2004-10-20 09:48:58 -07:00
|
|
|
free(data);
|
2003-06-11 06:20:48 -07:00
|
|
|
close(fd);
|
|
|
|
}
|
2003-06-12 06:02:28 -07:00
|
|
|
|
2003-07-22 22:19:50 -07:00
|
|
|
int
|
|
|
|
is_param(int n)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (str) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
is_num(int n)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
long int ret;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2003-07-22 22:19:50 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (str[0] == 0) return 0;
|
|
|
|
end = str;
|
|
|
|
ret = strtol(str, &end, 0);
|
|
|
|
if ((end != str) && (end[0] == 0)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-12 06:02:28 -07:00
|
|
|
char *
|
|
|
|
parse_str(int n)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
2003-06-12 15:34:51 -07:00
|
|
|
s = mem_strdup(str);
|
2003-06-12 06:02:28 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
parse_enum(int n, ...)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
va_start(va, n);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int v;
|
|
|
|
|
|
|
|
s = va_arg(va, char *);
|
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i token %s not one of:",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, str);
|
2003-06-12 06:02:28 -07:00
|
|
|
va_start(va, n);
|
|
|
|
s = va_arg(va, char *);
|
|
|
|
while (s)
|
|
|
|
{
|
|
|
|
v = va_arg(va, int);
|
|
|
|
fprintf(stderr, " %s", s);
|
|
|
|
s = va_arg(va, char *);
|
|
|
|
if (!s) break;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(va);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
v = va_arg(va, int);
|
|
|
|
if (!strcmp(s, str))
|
|
|
|
{
|
|
|
|
va_end(va);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
parse_int(int n)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
i = my_atoi(str);
|
2003-06-12 06:02:28 -07:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
parse_int_range(int n, int f, int t)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
i = my_atoi(str);
|
2003-06-12 06:02:28 -07:00
|
|
|
if ((i < f) || (i > t))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i integer %i out of range of %i to %i inclusive\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, i, f, t);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2004-10-13 11:07:56 -07:00
|
|
|
int
|
|
|
|
parse_bool(int n)
|
|
|
|
{
|
|
|
|
char *str, buf[4096];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2004-10-13 11:07:56 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strstrip(str, buf, sizeof (buf)))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i expression is too long\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2004-10-13 11:07:56 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(buf, "false") || !strcasecmp(buf, "off"))
|
|
|
|
return 0;
|
|
|
|
if (!strcasecmp(buf, "true") || !strcasecmp(buf, "on"))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
i = my_atoi(str);
|
|
|
|
if ((i < 0) || (i > 1))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i integer %i out of range of 0 to 1 inclusive\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, i);
|
2004-10-13 11:07:56 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2003-06-12 06:02:28 -07:00
|
|
|
double
|
|
|
|
parse_float(int n)
|
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
double i;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
i = my_atof(str);
|
2003-06-12 06:02:28 -07:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2003-07-07 00:55:18 -07:00
|
|
|
parse_float_range(int n, double f, double t)
|
2003-06-12 06:02:28 -07:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
double i;
|
|
|
|
|
|
|
|
str = evas_list_nth(params, n);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, n + 1);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
i = my_atof(str);
|
2003-06-12 06:02:28 -07:00
|
|
|
if ((i < f) || (i > t))
|
|
|
|
{
|
2003-07-07 00:55:18 -07:00
|
|
|
fprintf(stderr, "%s: Error. %s:%i float %3.3f out of range of %3.3f to %3.3f inclusive\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, i, f, t);
|
2003-06-12 06:02:28 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
|
2004-10-23 13:12:21 -07:00
|
|
|
void
|
|
|
|
check_arg_count(int required_args)
|
|
|
|
{
|
|
|
|
int num_args = evas_list_count (params);
|
|
|
|
|
|
|
|
if (num_args != required_args)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i got %i arguments, but expected %i\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, num_args, required_args);
|
2004-10-23 13:12:21 -07:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
/* simple expression parsing stuff */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* alpha ::= beta + beta || beta
|
|
|
|
* beta ::= gamma + gamma || gamma
|
|
|
|
* gamma ::= num || delta
|
|
|
|
* delta ::= '(' alpha ')'
|
|
|
|
*
|
2004-04-24 21:20:58 -07:00
|
|
|
*/
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
/* int set of function */
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static int
|
2004-04-24 21:20:58 -07:00
|
|
|
my_atoi(const char * s)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
char buf[4096];
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!s)
|
2004-04-24 21:20:58 -07:00
|
|
|
return 0;
|
|
|
|
|
2004-10-13 11:07:56 -07:00
|
|
|
if (!strstrip(s, buf, sizeof (buf)))
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i expression is too long\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2004-04-24 21:20:58 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-13 11:07:56 -07:00
|
|
|
_alphai(buf, &res);
|
2004-03-10 19:21:06 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_deltai(char *s, int * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
2004-08-24 03:06:33 -07:00
|
|
|
if (!val) return NULL;
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
if ('(' != s[0])
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i unexpected character at %s\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, s);
|
2004-04-24 21:20:58 -07:00
|
|
|
return s;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
else
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
s++;
|
|
|
|
s = _alphai(s, val);
|
|
|
|
s++;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_gammai(char *s, int * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
2004-08-24 03:06:33 -07:00
|
|
|
if (!val) return NULL;
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
if (_is_numi(s[0]))
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
s = _get_numi(s, val);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else if ('(' == s[0])
|
|
|
|
{
|
|
|
|
s = _deltai(s, val);
|
|
|
|
return s;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
else
|
2004-04-24 21:20:58 -07:00
|
|
|
fprintf(stderr, "%s: Error. %s:%i unexpected character at %s\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, s);
|
2004-03-10 19:21:06 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_betai(char *s, int * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
int a1, a2;
|
|
|
|
char op;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!val)
|
2004-08-24 03:06:33 -07:00
|
|
|
return NULL;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
s = _gammai(s, &a1);
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
while (_is_op1i(s[0]))
|
|
|
|
{
|
|
|
|
op = s[0];
|
|
|
|
s++;
|
|
|
|
s = _gammai(s, &a2);
|
|
|
|
a1 = _calci(op, a1, a2);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
(*val) = a1;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_alphai(char *s, int * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
int a1, a2;
|
|
|
|
char op;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!val)
|
2004-08-24 03:06:33 -07:00
|
|
|
return NULL;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
s = _betai(s, &a1);
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
while (_is_op2i(s[0]))
|
|
|
|
{
|
|
|
|
op = s[0];
|
|
|
|
s++;
|
|
|
|
s = _betai(s, &a2);
|
|
|
|
a1 = _calci(op, a1, a2);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
(*val) = a1;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
char *
|
|
|
|
_get_numi(char *s, int * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
int pos = 0;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!val)
|
2004-04-24 21:20:58 -07:00
|
|
|
return s;
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
while (
|
2004-04-24 21:20:58 -07:00
|
|
|
(('0' <= s[pos]) && ('9' >= s[pos])) ||
|
|
|
|
((0 == pos) && ('-' == s[pos]))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
buf[pos] = s[pos];
|
|
|
|
pos++;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
buf[pos] = '\0';
|
|
|
|
(*val) = atoi(buf);
|
|
|
|
return (s+pos);
|
|
|
|
}
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
int
|
|
|
|
_is_numi(char c)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
if (((c >= '0') && (c <= '9')) || ('-' == c) || ('+' == c))
|
2004-04-24 21:20:58 -07:00
|
|
|
return 1;
|
2004-03-10 19:21:06 -08:00
|
|
|
else
|
2004-04-24 21:20:58 -07:00
|
|
|
return 0;
|
2004-03-10 19:21:06 -08:00
|
|
|
}
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
int
|
|
|
|
_is_op1i(char c)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
switch(c)
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
case '*':;
|
|
|
|
case '/': return 1;
|
|
|
|
default: return 0;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
int
|
|
|
|
_is_op2i(char c)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
switch(c)
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
case '+':;
|
|
|
|
case '-': return 1;
|
|
|
|
default: return 0;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
int
|
|
|
|
_calci(char op, int a, int b)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
switch(op)
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
a += b;
|
|
|
|
return a;
|
|
|
|
case '-':
|
|
|
|
a -= b;
|
|
|
|
return a;
|
|
|
|
case '/':
|
|
|
|
if(0 != b)
|
|
|
|
a /= b;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i divide by zero\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2004-04-24 21:20:58 -07:00
|
|
|
return a;
|
|
|
|
case '*':
|
|
|
|
a *= b;
|
|
|
|
return a;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i unexpected character '%c'\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, op);
|
2004-04-24 21:20:58 -07:00
|
|
|
return a;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* float set of functoins */
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
double
|
|
|
|
my_atof(const char * s)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
double res = 0;
|
|
|
|
char buf[4096];
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!s)
|
2004-04-24 21:20:58 -07:00
|
|
|
return 0;
|
|
|
|
|
2004-10-13 11:07:56 -07:00
|
|
|
if (!strstrip(s, buf, sizeof (buf)))
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i expression is too long\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2004-04-24 21:20:58 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-13 11:07:56 -07:00
|
|
|
_alphaf(buf, &res);
|
2004-03-10 19:21:06 -08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_deltaf(char *s, double * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
2004-08-24 03:06:33 -07:00
|
|
|
if (!val) return NULL;
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
if ('(' != s[0])
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i unexpected character at %s\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, s);
|
2004-04-24 21:20:58 -07:00
|
|
|
return s;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
else
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
s++;
|
|
|
|
s = _alphaf(s, val);
|
|
|
|
s++;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_gammaf(char *s, double * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
2004-08-24 03:06:33 -07:00
|
|
|
if (!val) return NULL;
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
if (_is_numf(s[0]))
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
s = _get_numf(s, val);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else if ('(' == s[0])
|
|
|
|
{
|
|
|
|
s = _deltaf(s, val);
|
|
|
|
return s;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
else
|
2004-04-24 21:20:58 -07:00
|
|
|
fprintf(stderr, "%s: Error. %s:%i unexpected character at %s\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, s);
|
2004-03-10 19:21:06 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_betaf(char *s, double * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
double a1=0, a2=0;
|
|
|
|
char op;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!val)
|
2004-08-24 03:06:33 -07:00
|
|
|
return NULL;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
s = _gammaf(s, &a1);
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
while (_is_op1f(s[0]))
|
|
|
|
{
|
|
|
|
op = s[0];
|
|
|
|
s++;
|
|
|
|
s = _gammaf(s, &a2);
|
|
|
|
a1 = _calcf(op, a1, a2);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
(*val) = a1;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_alphaf(char *s, double * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
double a1=0, a2=0;
|
|
|
|
char op;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!val)
|
2004-08-24 03:06:33 -07:00
|
|
|
return NULL;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
s = _betaf(s, &a1);
|
|
|
|
|
2004-04-24 21:20:58 -07:00
|
|
|
while (_is_op2f(s[0]))
|
|
|
|
{
|
|
|
|
op = s[0];
|
|
|
|
s++;
|
|
|
|
s = _betaf(s, &a2);
|
|
|
|
a1 = _calcf(op, a1, a2);
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
(*val) = a1;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static char *
|
2004-04-24 21:20:58 -07:00
|
|
|
_get_numf(char *s, double * val)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
char buf[4096];
|
|
|
|
int pos = 0;
|
2004-04-24 21:20:58 -07:00
|
|
|
|
2004-03-10 19:21:06 -08:00
|
|
|
if (!val)
|
2004-04-24 21:20:58 -07:00
|
|
|
return s;
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
while (
|
2004-04-24 21:20:58 -07:00
|
|
|
(('0' <= s[pos]) && ('9' >= s[pos])) ||
|
|
|
|
('.' == s[pos]) ||
|
|
|
|
((0 == pos) && ('-' == s[pos]))
|
|
|
|
)
|
|
|
|
{
|
|
|
|
buf[pos] = s[pos];
|
|
|
|
pos++;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
|
|
|
|
buf[pos] = '\0';
|
|
|
|
(*val) = atof(buf);
|
|
|
|
return (s+pos);
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static int
|
2004-04-24 21:20:58 -07:00
|
|
|
_is_numf(char c)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
if (((c >= '0') && (c <= '9'))
|
2004-04-24 21:20:58 -07:00
|
|
|
|| ('-' == c)
|
|
|
|
|| ('.' == c)
|
|
|
|
|| ('+' == c))
|
|
|
|
return 1;
|
2004-03-10 19:21:06 -08:00
|
|
|
else
|
2004-04-24 21:20:58 -07:00
|
|
|
return 0;
|
2004-03-10 19:21:06 -08:00
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static int
|
2004-04-24 21:20:58 -07:00
|
|
|
_is_op1f(char c)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
switch(c)
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
case '*':;
|
|
|
|
case '/': return 1;
|
|
|
|
default: return 0;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static int
|
2004-04-24 21:20:58 -07:00
|
|
|
_is_op2f(char c)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
switch(c)
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
case '+':;
|
|
|
|
case '-': return 1;
|
|
|
|
default: return 0;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-24 03:06:33 -07:00
|
|
|
static double
|
2004-04-24 21:20:58 -07:00
|
|
|
_calcf(char op, double a, double b)
|
2004-03-10 19:21:06 -08:00
|
|
|
{
|
|
|
|
switch(op)
|
2004-04-24 21:20:58 -07:00
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
a += b;
|
|
|
|
return a;
|
|
|
|
case '-':
|
|
|
|
a -= b;
|
|
|
|
return a;
|
|
|
|
case '/':
|
|
|
|
if (b != 0) a /= b;
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i divide by zero\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2004-04-24 21:20:58 -07:00
|
|
|
return a;
|
|
|
|
case '*':
|
|
|
|
a *= b;
|
|
|
|
return a;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i unexpected character '%c'\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1, op);
|
2004-04-24 21:20:58 -07:00
|
|
|
return a;
|
|
|
|
}
|
2004-03-10 19:21:06 -08:00
|
|
|
}
|
2004-10-13 11:07:56 -07:00
|
|
|
|
|
|
|
static int
|
|
|
|
strstrip(const char *in, char *out, size_t size)
|
|
|
|
{
|
|
|
|
if ((size -1 ) < strlen(in))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: Error. %s:%i expression is too long\n",
|
2004-10-23 23:43:45 -07:00
|
|
|
progname, file_in, line - 1);
|
2004-10-13 11:07:56 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove spaces and tabs */
|
|
|
|
while (*in)
|
|
|
|
{
|
|
|
|
if ((0x20 != *in) && (0x09 != *in))
|
|
|
|
{
|
|
|
|
*out = *in;
|
|
|
|
out++;
|
|
|
|
}
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = '\0';
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|