=================================================================== RCS file: /cvs/mandoc/cgi.c,v retrieving revision 1.59 retrieving revision 1.65 diff -u -p -r1.59 -r1.65 --- mandoc/cgi.c 2014/07/09 16:23:01 1.59 +++ mandoc/cgi.c 2014/07/12 16:14:35 1.65 @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.59 2014/07/09 16:23:01 schwarze Exp $ */ +/* $Id: cgi.c,v 1.65 2014/07/12 16:14:35 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014 Ingo Schwarze @@ -49,7 +49,7 @@ struct query { const char *arch; /* architecture */ const char *sec; /* manual section */ const char *expr; /* unparsed expression string */ - int legacy; /* whether legacy mode */ + int equal; /* match whole names, not substrings */ }; struct req { @@ -142,9 +142,11 @@ http_printquery(const struct req *req) http_print(req->q.arch); } if (NULL != req->q.expr) { - printf("&expr="); - http_print(req->q.expr ? req->q.expr : ""); + printf("&query="); + http_print(req->q.expr); } + if (0 == req->q.equal) + printf("&apropos=1"); } static void @@ -164,9 +166,11 @@ html_printquery(const struct req *req) html_print(req->q.arch); } if (NULL != req->q.expr) { - printf("&expr="); - html_print(req->q.expr ? req->q.expr : ""); + printf("&query="); + html_print(req->q.expr); } + if (0 == req->q.equal) + printf("&apropos=1"); } static void @@ -202,12 +206,11 @@ static void http_parse(struct req *req, char *p) { char *key, *val; - int legacy; memset(&req->q, 0, sizeof(struct query)); req->q.manpath = req->p[0]; + req->q.equal = 1; - legacy = -1; while ('\0' != *p) { key = p; val = NULL; @@ -228,37 +231,21 @@ http_parse(struct req *req, char *p) if (NULL != val && ! http_decode(val)) break; - if (0 == strcmp(key, "expr")) + if (0 == strcmp(key, "query")) req->q.expr = val; - else if (0 == strcmp(key, "query")) - req->q.expr = val; - else if (0 == strcmp(key, "sec")) - req->q.sec = val; - else if (0 == strcmp(key, "sektion")) - req->q.sec = val; - else if (0 == strcmp(key, "arch")) - req->q.arch = val; else if (0 == strcmp(key, "manpath")) req->q.manpath = val; else if (0 == strcmp(key, "apropos")) - legacy = 0 == strcmp(val, "0"); + req->q.equal = !strcmp(val, "0"); + else if (0 == strcmp(key, "sec") || + 0 == strcmp(key, "sektion")) { + if (strcmp(val, "0")) + req->q.sec = val; + } else if (0 == strcmp(key, "arch")) { + if (strcmp(val, "default")) + req->q.arch = val; + } } - - /* Test for old man.cgi compatibility mode. */ - - req->q.legacy = legacy > 0; - - /* - * Section "0" means no section when in legacy mode. - * For some man.cgi scripts, "default" arch is none. - */ - - if (req->q.legacy && NULL != req->q.sec) - if (0 == strcmp(req->q.sec, "0")) - req->q.sec = NULL; - if (req->q.legacy && NULL != req->q.arch) - if (0 == strcmp(req->q.arch, "default")) - req->q.arch = NULL; } static void @@ -284,11 +271,13 @@ static int http_decode(char *p) { char hex[3]; + char *q; int c; hex[2] = '\0'; - for ( ; '\0' != *p; p++) { + q = p; + for ( ; '\0' != *p; p++, q++) { if ('%' == *p) { if ('\0' == (hex[0] = *(p + 1))) return(0); @@ -299,13 +288,13 @@ http_decode(char *p) if ('\0' == c) return(0); - *p = (char)c; - memmove(p + 1, p + 3, strlen(p + 3) + 1); + *q = (char)c; + p += 2; } else - *p = '+' == *p ? ' ' : *p; + *q = '+' == *p ? ' ' : *p; } - *p = '\0'; + *q = '\0'; return(1); } @@ -314,12 +303,12 @@ resp_begin_http(int code, const char *msg) { if (200 != code) - printf("Status: %d %s\n", code, msg); + printf("Status: %d %s\r\n", code, msg); - puts("Content-Type: text/html; charset=utf-8\n" - "Cache-Control: no-cache\n" - "Pragma: no-cache\n" - ""); + printf("Content-Type: text/html; charset=utf-8\r\n" + "Cache-Control: no-cache\r\n" + "Pragma: no-cache\r\n" + "\r\n"); fflush(stdout); } @@ -366,10 +355,20 @@ resp_searchform(const struct req *req) "
\n" "
\n" "Search Parameters\n" - " for manuals satisfying \n" - " " + "for manuals \n", scriptname); + printf("\n" + "q.expr ? req->q.expr : ""); printf("\">, section " "q.arch ? req->q.arch : ""); printf("\">"); if (req->psz > 1) { - puts(", "); for (i = 0; i < (int)req->psz; i++) { printf("
\n" "
\n" @@ -408,7 +407,19 @@ resp_index(const struct req *req) { resp_begin_html(200, NULL); + puts("

\n" + "Online manuals with " + "mandoc\n" + "

"); resp_searchform(req); + printf("

\n" + "This web interface is documented in the " + "" + "man.cgi manual, and the " + "" + "apropos manual explains the query syntax.\n" + "

\n", + scriptname, scriptname); resp_end_html(); } @@ -473,12 +484,13 @@ resp_search(const struct req *req, struct manpage *r, * If we have just one result, then jump there now * without any delay. */ - puts("Status: 303 See Other"); + printf("Status: 303 See Other\r\n"); printf("Location: http://%s%s/show/%s/%s?", httphost, scriptname, req->q.manpath, r[0].file); http_printquery(req); - puts("\n" - "Content-Type: text/html; charset=utf-8\n"); + printf("\r\n" + "Content-Type: text/html; charset=utf-8\r\n" + "\r\n"); return; } @@ -769,8 +781,8 @@ pg_search(const struct req *req, char *path) search.arch = req->q.arch; search.sec = req->q.sec; - search.deftype = TYPE_Nm | TYPE_Nd; - search.flags = 0; + search.deftype = req->q.equal ? TYPE_Nm : (TYPE_Nm | TYPE_Nd); + search.flags = req->q.equal ? MANSEARCH_MAN : 0; paths.sz = 1; paths.paths = mandoc_malloc(sizeof(char *));