/* $Id: w3mbookmark.c,v 1.5 2001/11/24 02:01:26 ukai Exp $ */ #ifdef __EMX__ #include #endif #include #include "config.h" #include "Str.h" #include "indep.h" #include "textlist.h" #include "parsetag.h" #include "gcmain.c" #if LANG == JA static char *bkmark_src1 = "Bookmark Registration\n\

ブックマークの登録

\n\n" "
\n\n" "\n\ \n\ \n"; static char *bkmark_src2 = "\n\ \n\ \n\ \n\
New Section:
URL:
Title:
\n\ \
\ \n"; static char *default_section = "未分類"; #else /* LANG != JA */ static char *bkmark_src1 = "Bookmark Registration\n\

Register to my bookmark

\n\n" "
\n\n" "\n\ \n\ \n"; static char *bkmark_src2 = "\n\ \n\ \n\ \n\
New Section:
URL:
Title:
\n\ \
\ \n"; static char *default_section = "Miscellaneous"; #endif /* LANG != JA */ #define FALSE 0 #define T 1 static char end_section[] = "\n"; char *Local_cookie; void print_bookmark_panel(char *bmark, char *url, char *title) { Str tmp, tmp2; FILE *f; char *p; printf("Content-Type: text/html\n\n"); printf(bkmark_src1, w3m_lib_dir(), bmark); if ((f = fopen(bmark, "r")) != NULL) { printf("Section:\n"); } printf(bkmark_src2, html_quote(url), html_quote(title), Local_cookie); } /* create new bookmark */ static int create_new_bookmark(char *bmark, char *section, char *title, char *url, char *mode) { FILE *f; f = fopen(bmark, mode); if (f == NULL) { printf("\nCan't open bookmark %s\n", bmark); return FALSE; } else { fprintf(f, "Bookmarks\n"); fprintf(f, "\n

Bookmarks

\n"); fprintf(f, "

%s

\n\n\n\n"); fclose(f); } return TRUE; } int insert_bookmark(char *bmark, struct parsed_tagarg *data) { char *url, *title, *section; FILE *f; TextList *tl = newTextList(); int section_found = 0; int bmark_added = 0; Str tmp, section_tmp; url = tag_get_value(data, "url"); title = tag_get_value(data, "title"); section = tag_get_value(data, "newsection"); if (section == NULL || *section == '\0') section = tag_get_value(data, "section"); if (section == NULL || *section == '\0') section = default_section; if (url == NULL || *url == '\0' || title == NULL || *title == '\0') { /* Bookmark not added */ return FALSE; } url = html_quote(url); title = html_quote(title); section = html_quote(section); f = fopen(bmark, "r"); if (f == NULL) return create_new_bookmark(bmark, section, title, url, "w"); section_tmp = Sprintf("

%s

\n", section); for (;;) { tmp = Strfgets(f); if (tmp->length == 0) break; if (Strcasecmp(tmp, section_tmp) == 0) section_found = 1; if (section_found && !bmark_added) { Strremovefirstspaces(tmp); if (Strcmp_charp(tmp, end_section) == 0) { pushText(tl, Sprintf("
  • %s\n", url, title)->ptr); bmark_added = 1; } } if (!bmark_added && Strcasecmp_charp(tmp, "\n") == 0) { pushText(tl, Sprintf("

    %s

    \n
      \n", section)->ptr); pushText(tl, Sprintf("
    • %s\n", url, title)->ptr); pushText(tl, end_section); pushText(tl, "
    \n"); bmark_added = 1; } pushText(tl, tmp->ptr); } fclose(f); if (!bmark_added) { /* Bookmark not added; perhaps the bookmark file is ill-formed */ /* In this case, a new bookmark is appeneded after the bookmark file */ return create_new_bookmark(bmark, section, title, url, "a"); } f = fopen(bmark, "w"); while (tl->nitem) { fputs(popText(tl), f); } fclose(f); return TRUE; } int MAIN(int argc, char *argv[], char **envp) { extern char *getenv(); char *qs; struct parsed_tagarg *cgiarg; char *mode; char *bmark; char *url; char *title; char *sent_cookie; if ((qs = getenv("QUERY_STRING")) == NULL) { printf("Content-Type: text/plain\n\n"); printf("Incomplete Request: no QUERY_STRING\n"); exit(1); } cgiarg = cgistr2tagarg(qs); mode = tag_get_value(cgiarg, "mode"); bmark = expandPath(tag_get_value(cgiarg, "bmark")); url = tag_get_value(cgiarg, "url"); title = tag_get_value(cgiarg, "title"); if (bmark == NULL || url == NULL) { /* incomplete request */ printf("Content-Type: text/plain\n\n"); printf("Incomplete Request: QUERY_STRING=%s\n", qs); exit(1); } Local_cookie = getenv("LOCAL_COOKIE"); sent_cookie = tag_get_value(cgiarg, "cookie"); if (Local_cookie == NULL) { /* Local cookie not provided: maybe illegal invocation */ Local_cookie = ""; } if (mode && !strcmp(mode, "panel")) { if (title == NULL) title = ""; print_bookmark_panel(bmark, url, title); } else if (mode && !strcmp(mode, "register")) { printf("Content-Type: text/plain\n"); if (sent_cookie == NULL || Local_cookie[0] == '\0' || strcmp(sent_cookie, Local_cookie) != 0) { /* local cookie doesn't match: It may be an illegal invocation */ printf("\nBookmark not added: local cookie doesn't match\n"); } else if (insert_bookmark(bmark, cgiarg)) { printf("w3m-control: BACK\n"); printf("w3m-control: BACK\n\n"); } } return 0; }