From cf2ff5f763dd9ebdf38f76761bcc41a4990fc156 Mon Sep 17 00:00:00 2001 From: Erik K Date: Sat, 14 May 2022 20:49:17 +0000 Subject: added unset command --- sm.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'sm.c') diff --git a/sm.c b/sm.c index d4064c7..6358086 100644 --- a/sm.c +++ b/sm.c @@ -37,6 +37,7 @@ static int pladddoc(const char *, Document *); static int add(const char *); static int edit(const char *); static void list(const char *); +static int unset(const char *, const char *); static Document **docdb; static int ndocdb; @@ -93,6 +94,7 @@ dbdel(const char *filename) plugins[i]->usedby = xrealloc(plugins[i]->usedby, --plugins[i]->nusedby * sizeof(*plugins[i]->usedby)); + break; } } @@ -365,6 +367,35 @@ list(const char *filename) } } +static int +unset(const char *filename, const char *plname) +{ + Document *d; + int i, j; + + if ((d = dbfind(filename)) == NULL) { + fprintf(stderr, "%s: no such file\n", filename); + return 0; + } + for (i = 0; i < NPLUGINS; i++) { + if (strcmp(plugins[i]->name, plname) != 0) + continue; + for (j = 0; j < plugins[i]->nusedby; j++) { + if (plugins[i]->usedby[j] != d) + continue; + if (j + 1 < plugins[i]->nusedby) { + memmove(&plugins[i]->usedby[j], &plugins[i]->usedby[j + + 1], (plugins[i]->nusedby - j - 1) * + sizeof(*plugins[i]->usedby)); + } + plugins[i]->usedby = xrealloc(plugins[i]->usedby, + --plugins[i]->nusedby * + sizeof(*plugins[i]->usedby)); + return 1; + } + } + return 0; +} int @@ -435,6 +466,9 @@ editcmd: } if (pladddoc(cmd.argv[2], d)) dbwrite(kdbloc); + } else if (!strcmp(cmd.argv[0], "unset")) { + if (unset(cmd.argv[1], cmd.argv[2])) + dbwrite(kdbloc); } } fputc('\n', stdout); -- cgit v1.2.3