From 2c463e63babad15fe81049bdf15a35de32bc45ec Mon Sep 17 00:00:00 2001 From: "S. Gilles" Date: Thu, 6 Dec 2018 16:07:37 -0500 Subject: [PATCH] Add "renamename" to clav-cli I get sick of parsing the output of "print" when I'm scripting mutations for which changing the name actually matters. --- ui-cli.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/ui-cli.c b/ui-cli.c index 3989643..7cdcb70 100644 --- a/ui-cli.c +++ b/ui-cli.c @@ -116,6 +116,7 @@ print_help(void) printf( "edge

/ Add an edge from to , weight

/\n"); printf("rename Rename vertex to \n"); + printf("renamename Rename vertex to \n"); printf( "incfat Increase fatness of vertex by 1\n"); printf( @@ -422,6 +423,70 @@ check_edge(char *buf, struct ui_event *e, uint_fast8_t *partial_match) return 0; } +/* Check for `renamename' */ +static int +check_renamename(char *buf, struct ui_event *e, uint_fast8_t *partial_match) +{ + size_t i = 0; + size_t j = 0; + uint_fast8_t seen = 0; + char *s = 0; + char *t = 0; + char dummy = 0; + int ret = 0; + + if (!(s = malloc(1024))) { + perror(L("malloc")); + goto done_err; + } + + if (!(t = malloc(1024))) { + perror(L("malloc")); + goto done_err; + } + + if (strncmp(buf, "renamename", 10)) { + goto done_err; + } + + *partial_match = 1; + + if (sscanf(buf, "renamename %1023s %1023s %c", s, t, &dummy) != 2) { + printf("Type `help' to see how to use `renamename'\n"); + goto done_err; + } + + for (j = 0; j < q->v_num; ++j) { + if (!(strcmp(q->v[j].name, s))) { + i = j; + seen++; + } + } + + if (seen < 1) { + printf("Cannot rename %s: not in quiver\n", s); + goto done_err; + } + + if (seen > 1) { + printf("Cannot rename %s: ambiguous\n", s); + goto done_err; + } + + ret = 0; + *e = (struct ui_event) { .type = ET_RENAME, .idx_1 = i, .str = t }; + free(s); + free_this = t; +done: + + return ret; +done_err: + ret = 1; + free(s); + free(t); + goto done; +} + /* Check for `rename' */ static int check_rename(char *buf, struct ui_event *e, uint_fast8_t *partial_match) @@ -670,6 +735,9 @@ ui_get_event(struct ui_event *e, uint_fast8_t *more) !check_edge(buf, e, &partial_match)) { goto have_something; } else if (!partial_match && + !check_renamename(buf, e, &partial_match)) { + goto have_something; + } else if (!partial_match && !check_rename(buf, e, &partial_match)) { goto have_something; } else if (!partial_match && -- 2.11.4.GIT