cgmnlm

Unnamed repository; edit this file 'description' to name the repository.
git clone git://code.clttr.info/cgmnlm.git
Log | Files | Refs | README | LICENSE

commit dde8799e758b1e6c3985492a8205c189d4d47b9c
parent 9ef33fb102426d0bf56e93ceebcd81eb24171a9e
Author: René Wagner <rwagner@rw-net.de>
Date:   Sun, 10 Jan 2021 16:54:10 +0100

implement s command to remove bookmark for current page

It will remove all lines from the bookmark file that
match the URL of the current page viewed in the browser.

Diffstat:
Msrc/gmnlm.c | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 58 insertions(+), 13 deletions(-)

diff --git a/src/gmnlm.c b/src/gmnlm.c @@ -88,6 +88,7 @@ const char *help_msg = "f\t\tForward (in the page history)\n" "H\t\tView all page history\n" "a\t\tSave bookmark\n" + "s\tRemove bookmark for current URL\n" "B\t\tBrowse bookmarks\n" "r\t\tReload the page\n" "/<text>\t\tsearch for text (POSIX regular expression)\n" @@ -169,18 +170,16 @@ save_bookmark(struct browser *browser) size_t n; n = snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); + free(path_fmt); assert(n < sizeof(path)); - strncpy(dname, dirname(path), sizeof(dname)-1); + strncpy(dname, path, sizeof(dname)-1); + dirname(dname); if (mkdirs(dname, 0755) != 0) { - snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); - free(path_fmt); fprintf(stderr, "Error creating directory %s: %s\n", - dirname(path), strerror(errno)); + dname, strerror(errno)); return; } - snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); - free(path_fmt); FILE *f = fopen(path, "a"); if (!f) { fprintf(stderr, "Error opening %s for writing: %s\n", @@ -202,6 +201,49 @@ save_bookmark(struct browser *browser) } static void +remove_bookmark(struct browser *browser) +{ + char *path_fmt = get_data_pathfmt(); + static char path[PATH_MAX+1]; + snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); + free(path_fmt); + + static char tempfile[PATH_MAX+2]; + snprintf(tempfile, sizeof(tempfile), "%s2", path); + FILE *fi = fopen(path, "r"); + FILE *fo = fopen(tempfile, "w"); + if(fi == NULL) { + fprintf(stderr, "Bookmark file not available!\n"); + return; + } + if(fo == NULL) { + fprintf(stderr, "tempfile not available!\n"); + return; + } + + char *line = NULL; + size_t len = 0; + size_t n = 0; + + static char url[1024]; + n = snprintf(url, sizeof(url), "=> %s ", browser->plain_url); + while(getline(&line, &len, fi) != -1) { + if (strncmp(line, url, n)==0) { + fprintf(browser->tty, "Bookmark removed!\n"); + } else { + fprintf(fo, line); + } + } + + fclose(fi); + fclose(fo); + free(line); + if ( rename(tempfile, path) != 0) { + fprintf(browser->tty, "Failed to udpate bookmarks: %s\n", strerror(errno)); + } +} + +static void open_bookmarks(struct browser *browser) { char *path_fmt = get_data_pathfmt(); @@ -210,19 +252,17 @@ open_bookmarks(struct browser *browser) size_t n; n = snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); + free(path_fmt); + assert(n < sizeof(path)); - strncpy(dname, dirname(path), sizeof(dname)-1); + strncpy(dname, path, sizeof(dname)-1); + dirname(dname); if (mkdirs(dname, 0755) != 0) { - snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); - free(path_fmt); fprintf(stderr, "Error creating directory %s: %s\n", - dirname(path), strerror(errno)); + dname, strerror(errno)); return; } - snprintf(path, sizeof(path), path_fmt, "bookmarks.gmi"); - free(path_fmt); - struct stat buf; if (stat(path, &buf) == -1 && errno == ENOENT) { // TOCTOU, but we almost certainly don't care @@ -568,6 +608,11 @@ do_prompts(const char *prompt, struct browser *browser) save_bookmark(browser); result = PROMPT_AGAIN; goto exit; + case 's': + if (in[1]) break; + remove_bookmark(browser); + result = PROMPT_AGAIN; + goto exit; case 'B': if (in[1]) break; open_bookmarks(browser);