struct ec_completed_node *compnode = NULL;
struct ec_completed_match *match = NULL;
int ret = -ENOMEM;
- size_t n;
/* find the compnode entry corresponding to this node */
TAILQ_FOREACH(compnode, &completed->nodes, next) {
if (match == NULL)
goto fail;
- if (match->add != NULL) {
- if (completed->smallest_start == NULL) {
- completed->smallest_start = ec_strdup(match->add);
- if (completed->smallest_start == NULL)
- goto fail;
- } else {
- n = strcmp_count(match->add,
- completed->smallest_start);
- completed->smallest_start[n] = '\0';
- }
+ if (match->add != NULL)
completed->count_match++;
- }
TAILQ_INSERT_TAIL(&compnode->matches, match, next);
completed->count++;
}
ec_free(compnode);
}
- ec_free(completed->smallest_start);
ec_free(completed);
}
return;
}
- fprintf(out, "completion: count=%u match=%u smallest_start=<%s>\n",
- completed->count, completed->count_match,
- completed->smallest_start);
+ fprintf(out, "completion: count=%u match=%u\n",
+ completed->count, completed->count_match);
TAILQ_FOREACH(compnode, &completed->nodes, next) {
fprintf(out, "node=%p, node_type=%s\n",
}
}
-const char *ec_completed_smallest_start(
- const struct ec_completed *completed)
+char *ec_completed_smallest_start(const struct ec_completed *completed)
{
- if (completed == NULL || completed->smallest_start == NULL)
- return "";
+ struct ec_completed_node *compnode;
+ struct ec_completed_match *item;
+ char *smallest_start = NULL;
+ size_t n;
+
+ if (completed == NULL)
+ goto fail;
- return completed->smallest_start;
+ TAILQ_FOREACH(compnode, &completed->nodes, next) {
+ TAILQ_FOREACH(item, &compnode->matches, next) {
+ if (item->add == NULL)
+ continue;
+ if (smallest_start == NULL) {
+ smallest_start = ec_strdup(item->add);
+ if (smallest_start == NULL)
+ goto fail;
+ } else {
+ n = strcmp_count(item->add, smallest_start);
+ smallest_start[n] = '\0';
+ }
+ }
+ }
+
+ return smallest_start;
+
+fail:
+ ec_free(smallest_start);
+ return NULL;
}
unsigned int ec_completed_count(
struct ec_completed {
unsigned count;
unsigned count_match;
- char *smallest_start;
struct ec_completed_node_list nodes;
};
struct ec_parsed *state,
const struct ec_strvec *strvec);
-/* cannot return NULL */
-const char *ec_completed_smallest_start(
- const struct ec_completed *completed);
+/* return the smallest string start, or NULL on error */
+char *ec_completed_smallest_start(const struct ec_completed *completed);
unsigned int ec_completed_count(
const struct ec_completed *completed,
{
struct ec_completed *c = NULL;
struct ec_strvec *vec = NULL;
- const char *s, *expected;
+ const char *expected, *s;
+ char *smallest_start = NULL;
int ret = 0;
unsigned int count = 0;
va_list ap;
/* check the expected smallest start */
expected = va_arg(ap, const char *);
- s = ec_completed_smallest_start(c);
- if (strcmp(s, expected)) {
+ smallest_start = ec_completed_smallest_start(c);
+ if (smallest_start == NULL)
+ goto out;
+ if (strcmp(smallest_start, expected)) {
ret = -1;
ec_log(EC_LOG_ERR,
"should complete with <%s> but completes with <%s>\n",
out:
ec_strvec_free(vec);
ec_completed_free(c);
+ ec_free(smallest_start);
va_end(ap);
return ret;
}