1 /* $Id: w3mbookmark.c,v 1.12 2007/05/31 01:19:50 inu Exp $ */
11 /* FIXME: gettextize here */
12 #define BKMARK_TITLE "¥Ö¥Ã¥¯¥Þ¡¼¥¯¤ÎÅÐÏ¿"
13 #define BKMARK_ADD "ÅÐÏ¿"
14 #define DEFAULT_SECTION "̤ʬÎà"
16 #define BKMARK_TITLE "Register to my bookmark"
17 #define BKMARK_ADD "ADD"
18 #define DEFAULT_SECTION "Miscellaneous"
21 static char *bkmark_src1 =
24 <title>" BKMARK_TITLE "</title>\n\
27 <h1>" BKMARK_TITLE "</h1>\n\
28 <form method=post action=\"file:///$LIB/" W3MBOOKMARK_CMDNAME "\">\n\
29 <input type=hidden name=mode value=register>\n\
30 <input type=hidden name=bmark value=\"%s\">\n\
31 <input type=hidden name=cookie value=\"%s\">\n\
32 <table cellpadding=0>\n";
34 static char *bkmark_src2 =
35 "<tr><td>New Section:<td><input type=text name=newsection size=60>\n\
36 <tr><td>URL:<td><input type=text name=url value=\"%s\" size=60>\n\
37 <tr><td>Title:<td><input type=text name=title value=\"%s\" size=60>\n\
38 <tr><td><input type=submit value=\"" BKMARK_ADD "\">\n\
49 static char end_section[] =
50 "<!--End of section (do not delete this comment)-->\n";
52 static char *Local_cookie = NULL;
55 print_bookmark_panel(char *bmark, char *url, char *title, char *charset)
61 if (charset == NULL) {
62 printf("Content-Type: text/html\n\n");
65 printf("Content-Type: text/html; charset=%s\n\n", charset);
67 printf(bkmark_src1, html_quote(bmark), html_quote(Local_cookie));
68 if ((f = fopen(bmark, "r")) != NULL) {
69 printf("<tr><td>Section:<td><select name=\"section\">\n");
70 while (tmp = Strfgets(f), tmp->length > 0) {
71 Strremovefirstspaces(tmp);
72 if (Strncasecmp_charp(tmp, "<h2>", 4) == 0) {
75 while (*p && *p != '<')
76 Strcat_char(tmp2, *p++);
77 printf("<option value=\"%s\">%s\n", tmp2->ptr,
81 printf("</select>\n");
83 printf(bkmark_src2, html_quote(url), html_quote(title));
86 /* create new bookmark */
88 create_new_bookmark(char *bmark, char *section, char *title, char *url,
92 f = fopen(bmark, mode);
94 printf("\nCan't open bookmark %s\n", bmark);
98 fprintf(f, "<html><head><title>Bookmarks</title></head>\n");
99 fprintf(f, "<body>\n<h1>Bookmarks</h1>\n");
100 fprintf(f, "<h2>%s</h2>\n<ul>\n", section);
101 fprintf(f, "<li><a href=\"%s\">%s</a>\n", url, title);
102 fprintf(f, end_section);
103 fprintf(f, "</ul>\n</body>\n</html>\n");
110 insert_bookmark(char *bmark, struct parsed_tagarg *data)
112 char *url, *title, *section;
114 TextList *tl = newTextList();
115 int section_found = 0;
117 Str tmp, section_tmp;
119 url = tag_get_value(data, "url");
120 title = tag_get_value(data, "title");
121 section = tag_get_value(data, "newsection");
122 if (section == NULL || *section == '\0')
123 section = tag_get_value(data, "section");
124 if (section == NULL || *section == '\0')
125 section = DEFAULT_SECTION;
127 if (url == NULL || *url == '\0' || title == NULL || *title == '\0') {
128 /* Bookmark not added */
131 url = html_quote(url);
132 title = html_quote(title);
133 section = html_quote(section);
135 f = fopen(bmark, "r");
137 return create_new_bookmark(bmark, section, title, url, "w");
139 section_tmp = Sprintf("<h2>%s</h2>\n", section);
142 if (tmp->length == 0)
144 if (Strcasecmp(tmp, section_tmp) == 0)
146 if (section_found && !bmark_added) {
147 Strremovefirstspaces(tmp);
148 if (Strcmp_charp(tmp, end_section) == 0) {
150 Sprintf("<li><a href=\"%s\">%s</a>\n", url,
155 if (!bmark_added && Strcasecmp_charp(tmp, "</body>\n") == 0) {
156 pushText(tl, Sprintf("<h2>%s</h2>\n<ul>\n", section)->ptr);
158 Sprintf("<li><a href=\"%s\">%s</a>\n", url, title)->ptr);
159 pushText(tl, end_section);
160 pushText(tl, "</ul>\n");
163 pushText(tl, tmp->ptr);
167 /* Bookmark not added; perhaps the bookmark file is ill-formed */
168 /* In this case, a new bookmark is appeneded after the bookmark file */
169 return create_new_bookmark(bmark, section, title, url, "a");
171 f = fopen(bmark, "w");
173 fputs(popText(tl), f);
180 main(int argc, char *argv[], char **envp)
182 extern char *getenv();
186 struct parsed_tagarg *cgiarg;
195 p = getenv("REQUEST_METHOD");
196 if (p == NULL || strcasecmp(p, "post"))
198 p = getenv("CONTENT_LENGTH");
199 if (p == NULL || (length = atoi(p)) <= 0)
202 qs = Strfgets(stdin);
204 if (qs->length != length)
206 cgiarg = cgistr2tagarg(qs->ptr);
208 p = getenv("LOCAL_COOKIE_FILE");
210 FILE *f = fopen(p, "r");
212 Local_cookie = Strfgets(f)->ptr;
216 sent_cookie = tag_get_value(cgiarg, "cookie");
217 if (sent_cookie == NULL || Local_cookie == NULL ||
218 strcmp(sent_cookie, Local_cookie) != 0) {
219 /* local cookie doesn't match: It may be an illegal invocation */
220 printf("Content-Type: text/plain\n\n");
221 printf("Local cookie doesn't match: It may be an illegal invocation\n");
225 mode = tag_get_value(cgiarg, "mode");
226 bmark = expandPath(tag_get_value(cgiarg, "bmark"));
227 url = tag_get_value(cgiarg, "url");
228 title = tag_get_value(cgiarg, "title");
229 charset = tag_get_value(cgiarg, "charset");
230 if (bmark == NULL || url == NULL)
232 if (mode && !strcmp(mode, "panel")) {
235 print_bookmark_panel(bmark, url, title, charset);
237 else if (mode && !strcmp(mode, "register")) {
238 printf("Content-Type: text/plain\n");
239 if (insert_bookmark(bmark, cgiarg)) {
240 printf("w3m-control: BACK\n");
241 printf("w3m-control: BACK\n");
248 printf("Content-Type: text/plain\n\n");
249 printf("Incomplete Request: %s\n", qs ? qs->ptr : "(null)");