static PyObject * CLIgen_completion(CLIgen *self) { return PyLong_FromLong(cligen_completion(self->handle->ch_cligen)); }
/* * match_complete * Try to complete a string as far as possible using the syntax. * * Parameters * IN * h cligen handle * string Input string to match * pt Vector of commands (array of cligen object pointers (cg_obj) * pt_max Length of the pt array * maxlen Max length of string * OUT * cvec cligen variable vector containing vars/values pair for completion * RETURNS: * -1 - Error * 0 - No matches, no completions made * 1 - We have completed by adding characters at the end of "string" */ int match_complete(cligen_handle h, char *string, parse_tree pt, int maxlen, cvec *cvec) { int level; int slen; int equal; int i, j, minmatch; cg_obj *co, *co1 = NULL; char *s; char *ss; pt_vec pt1; int nr; int matchlen = 0; int *matchv = NULL; int mv; int append = 0; /* Has appended characters */ int retval = -1; /* ignore any leading whitespace */ s = string; while ((strlen(s) > 0) && isblank(*s)) s++; again: matchlen = 0; if ((nr = match_pattern(h, s, pt, 0, 1, &pt1, &matchv, &matchlen, cvec, NULL)) < 0) goto done; if (nr==0){ retval = 0; goto done; /* No matches */ } if ((level = command_levels(s)) < 0) goto done; extract_substring(s, level, &ss); slen = ss?strlen(ss):0; if (ss) free(ss); minmatch = slen; equal = 1; for (i=0; i<matchlen; i++){ assert((mv = matchv[i])!=-1); co = pt1[mv]; if (co == NULL){ retval = 0; goto done; } if (co->co_type != CO_COMMAND) continue; if (co1 == NULL){ minmatch = strlen(co->co_command); co1 = co; } else{ if (strcmp(co1->co_command, co->co_command)==0) ; /* equal */ else{ equal = 0; for (j=0; j<MIN(strlen(co1->co_command), strlen(co->co_command)); j++) if (co1->co_command[j] != co->co_command[j]) break; minmatch = MIN(minmatch, j); } } } if (co1 == NULL){ retval = 0; goto done; } assert(strlen(string) + minmatch - slen < maxlen); strncat(string, &co1->co_command[slen], minmatch-slen); append = append || minmatch-slen; if (equal){ /* add space */ string[strlen(string)+1] = '\0'; string[strlen(string)] = co1?co1->co_delimiter:' '; level++; slen = 0; co1 = NULL; if (cligen_completion(h) == 1){ if (matchv) free(matchv); matchv = NULL; goto again; } } retval = append?1:0; done: if (matchv) free(matchv); return retval; }