diff --git a/include/tripl/tripl.h b/include/tripl/tripl.h index deac13d2584234c4f7bc359e889416b3c62dc521..dd6c9993be8d2970a5542d2bee7382bc7a842be1 100644 --- a/include/tripl/tripl.h +++ b/include/tripl/tripl.h @@ -130,13 +130,14 @@ extern char *linebuffer_save; #define arg_init(a) (a)->arg_s = NULL; (a)->arg_v = 0; \ (a)->next = (a); (a)->prev = (a); -struct arg_entry { +typedef struct arg_entry { char *arg_s; long arg_v; struct arg_entry *next, *prev; -}; +} arg_entry; +#define get_last_arg(args, dest) do {arg_entry begin;for (begin = *(dest) = (args);(dest)->next != &begin;dest = dest->next) {}} while (0) struct attr_table { char *attrname; diff --git a/tripl.c b/tripl.c index 7e8ff1e73de7d7f220620335d41779be4e8b6ab3..57871c3f3ca978189fa5012802ce1ac14ef08afb 100644 --- a/tripl.c +++ b/tripl.c @@ -234,8 +234,8 @@ static int getnumber(char *p) { /* NEVER MODIFY THIS! MAGIC! * THIS IS REALLY THE UGLY CODE, MOST UGLY CODE ALL OVER THE WORLD. * - * BUT IT WORKS NOW, SO WE DO NOT TO CHANGE THIS AND WE WILL CHANGE - * A LOT BECAUSE ALL OF THE TRIPL REQUIRES THIS FUNCTION. + * BUT IT WORKS NOW, SO WE SHOULE NOT CHANGE THIS AND WE WILL CHANGE + * A LOT BECAUSE THE WHOLE TRIPL REQUIRES THIS FUNCTION. * */ #define isfirstchr() (!i) @@ -387,7 +387,7 @@ int getattrmask(char *p, const struct attr_table *tab) { attrname = strndup(p, out); if (!(t = find_attr(attrname, tab))) - exit(generic_error("unknow attribute", linebuffer_save, lno)); + exit(generic_error("unknown attribute", linebuffer_save, lno)); mask |= t; @@ -414,4 +414,90 @@ static int parse_line(void) { return arch_do_keyword(p, arch_fn); } +/* I don't know how to do it, so I only make a function that can parse a function to arg_entry and funcname */ +void parse_func(const char *str, size_t max_length) { + arg_entry args; + arg_init(&args); + char *funcname; + size_t past_i, i, begin_pos, end_pos; + + /* find the position of the first '(' */ + for (i = 0;str[i] != '(' && i <= max_length;++i); + + /* didn't find '(' */ + if (i > max_length) { + fprintf(stderr, "cannot find the bracket"); + exit(1); + } + + begin_pos = i; + funcname = (char *)malloc(i + 1); /* remember to free it */ + memcpy(funcname, str, i); + funcname[i] = 0; /* make it a complete string */ + + /* find the position of the first ')' after the first '(' */ + for (;str[i] != ')' && i <= max_length;++i); + + /* didn't find ')' */ + if (i > max_length) { + fprintf(stderr, "cannot find the bracket"); + exit(1); + } + end_pos = i; + past_i = i = begin_pos; + + /* find commas */ + while (i <= end_pos) { + int is_digit = 1; + size_t a; + + /* find comma */ + for (++i;str[i] != ',' && i <= end_pos;++i); + + /* get if it is integer */ + for (a = 0;a <= i;++a,is_digit &= isdigit(str[past_i + a])); + is_digit &= i - past_i > 0; + if (is_digit) { /* when it is integer */ + /* get the last arg */ + arg_entry *last_arg; + get_last_arg(args, last_arg); + + /* parse the string to integer then copy it to the last arg */ + char *temp_str = malloc(i - past_i); + memcpy(temp_str, str + past_i, i - past_i - 1); + temp_str[i - past_i - 1] = 0; + last_arg->arg_v = atol(temp_str); + + /* create new arg and init it */ + last_arg->next = malloc(sizeof(arg_entry)); /* remember to free it too */ + arg_init(last_arg->next); + last_arg->next->prev = last_arg; + last_arg->next->next = &args; + free(temp_str); + + } else if ((str[i - 1] == '\'' || str[i - 1] == '"') && (str[past_i + 1] == '\'' || str[past_i + 1] == '"')) { + /* when it is string */ + arg_entry *last_arg; + get_last_arg(args, last_arg); + + char *temp_str = malloc(i - past_i - 2); /* remember to free it too */ + memcpy(temp_str, str + past_i + 1, i - past_i - 3); + temp_str[i - past_i - 3] = 0; + last_arg->arg_s = temp_str; + + /* create new arg and init it */ + last_arg->next = malloc(sizeof(arg_entry)); /* remember to free it too */ + arg_init(last_arg->next); + last_arg->next->prev = last_arg; + last_arg->next->next = &args; + } else { + /* when it is varaible */ + } + + past_i = i; + + } + arch_fn->arch_funcall(funcname, &args); + +}