/* * paren_squish -- * replaces "parenthesized" plans in our search plan with "expr" nodes. */ PLAN * paren_squish(PLAN *plan) { PLAN *expr; /* pointer to next expression */ PLAN *tail; /* pointer to tail of result plan */ PLAN *result; /* pointer to head of result plan */ result = tail = NULL; /* * the basic idea is to have yankexpr do all our work and just * collect its results together. */ while ((expr = yankexpr(&plan)) != NULL) { /* * if we find an unclaimed ')' it means there is a missing * '(' someplace. */ if (expr->execute == f_closeparen) errx(1, "): no beginning '('"); /* add the expression to our result plan */ if (result == NULL) tail = result = expr; else { tail->next = expr; tail = expr; } tail->next = NULL; } return (result); }
/* * yankexpr -- * Removes one expression from the plan. This is used mainly by * paren_squish. In comments below, an expression is either a * simple node or a N_EXPR node containing a list of simple nodes. */ static PLAN * yankexpr(PLAN **planp) /* pointer to top of plan (modified) */ { PLAN *next; /* temp node holding subexpression results */ PLAN *node; /* pointer to returned node or expression */ PLAN *tail; /* pointer to tail of subplan */ PLAN *subplan; /* pointer to head of ( ) expression */ /* first pull the top node from the plan */ if ((node = yanknode(planp)) == NULL) return (NULL); /* * If the node is an '(' then we recursively slurp up expressions * until we find its associated ')'. If it's a closing paren we * just return it and unwind our recursion; all other nodes are * complete expressions, so just return them. */ if (node->type == N_OPENPAREN) for (tail = subplan = NULL;;) { if ((next = yankexpr(planp)) == NULL) err(1, "(: missing closing ')'"); /* * If we find a closing ')' we store the collected * subplan in our '(' node and convert the node to * a N_EXPR. The ')' we found is ignored. Otherwise, * we just continue to add whatever we get to our * subplan. */ if (next->type == N_CLOSEPAREN) { if (subplan == NULL) errx(1, "(): empty inner expression"); node->p_data[0] = subplan; node->type = N_EXPR; node->eval = f_expr; break; } else { if (subplan == NULL) tail = subplan = next; else { tail->next = next; tail = next; } tail->next = NULL; } } return (node); }