void assoc_dir(const char *name, const char *dir) { union { char *s; tree_val_t l; }u = { .s = strdup(name), }; ensure_dirs_tree_exists(); if(tree_set_data(dirs, dir, u.l) != 0) free(u.s); }
void assoc_dir(const char *name, const char *dir) { union { char *s; unsigned long long l; }u = { .s = strdup(name), }; if(dirs == NULL) dirs = tree_create(1, 1); if(tree_set_data(dirs, dir, u.l) != 0) free(u.s); }
/* copy elements of exp buffer into a tree */ Expr *parse(char *exp) { Tree *root; int state = STATE_BEGIN; int layer = 0; char *ptr = exp; char buf[MAX_WORD]; int i = 0; /* check if malloc succeeded */ if ((root = tree_new(NULL)) == NULL) return NULL; /* initialize buffer */ memset(buf, 0, MAX_WORD); state = STATE_BEGIN; do { if (state == STATE_BEGIN) { /* go to first open paren */ if (*ptr == '(') { layer++; state = STATE_OPEN_PAREN; } } else if (state == STATE_OPEN_PAREN) { if (*ptr == ')') { layer--; root = down(root); state = STATE_CLOSE_PAREN; } else if (*ptr == '(') { layer++; root = up(root); state = STATE_OPEN_PAREN; } else if (!is_whitespace(*ptr)) { buf[i++] = *ptr; if (*ptr == '\"') state = STATE_QUOTE; else state = STATE_PROC; } } else if (state == STATE_CLOSE_PAREN) { if (layer < 0) { state = STATE_ERROR; printf("error: too many close parens\n"); } else if (*ptr == ')') { /* go to next buf */ layer--; root = down(root); state = STATE_CLOSE_PAREN; } else if (*ptr == '(') { layer++; root = up(root); state = STATE_OPEN_PAREN; } else if (!is_whitespace(*ptr)) { buf[i++] = *ptr; state = STATE_ARG; } } else if (state == STATE_PROC || state == STATE_ARG) { if (buf[0] != '\0' && (is_whitespace(*ptr) || *ptr == ')')) { /* terminate buf */ buf[i] = '\0'; if (state == STATE_PROC) { /* malloc() failed somewhere */ if (expr_insert_word(root, strdup(buf), TYPE_PROC) < 0) { state = STATE_ERROR; printf("error: memory error\n"); } } else { if (expr_insert_word(root, strdup(buf), TYPE_ARG) < 0) { state = STATE_ERROR; printf("error: memory error\n"); } } /* reset buf */ buf[0] = '\0'; i = 0; /* read next buf or close paren */ state = STATE_ARG; } if (*ptr == ')') { layer--; root = down(root); state = STATE_CLOSE_PAREN; } else if (*ptr == '(') { layer++; root = up(root); state = STATE_OPEN_PAREN; } else if (!is_whitespace(*ptr)) { buf[i++] = *ptr; if (*ptr == '\"') state = STATE_QUOTE; } else if (*ptr == '\"') { state = STATE_QUOTE; } } else if (state == STATE_QUOTE) { if (*ptr == '\"') state = STATE_ARG; else buf[i++] = *ptr; } ptr++; } while (*ptr != '\0' && state != STATE_ERROR); /* not a function call, return value instead */ if (state != STATE_ERROR && state == STATE_BEGIN) { tree_set_data(root, strdup(exp)); } else if (state != STATE_ERROR && layer > 0) { printf("error: too many open parens\n"); state = STATE_ERROR; } if (state == STATE_ERROR) { expr_free(root); root = NULL; } return root; }