+static void
+resp_begin_http(int code, const char *msg)
+{
+
+ if (200 != code)
+ printf("Status: %d %s\n", code, msg);
+
+ puts("Content-Type: text/html; charset=utf-8\n"
+ "Cache-Control: no-cache\n"
+ "Pragma: no-cache\n"
+ "");
+
+ fflush(stdout);
+}
+
+static void
+resp_begin_html(int code, const char *msg)
+{
+
+ resp_begin_http(code, msg);
+
+ printf("<!DOCTYPE HTML PUBLIC "
+ " \"-//W3C//DTD HTML 4.01//EN\""
+ " \"http://www.w3.org/TR/html4/strict.dtd\">\n"
+ "<HTML>\n"
+ "<HEAD>\n"
+ "<META HTTP-EQUIV=\"Content-Type\""
+ " CONTENT=\"text/html; charset=utf-8\">\n"
+ "<LINK REL=\"stylesheet\" HREF=\"%s/man-cgi.css\""
+ " TYPE=\"text/css\" media=\"all\">\n"
+ "<LINK REL=\"stylesheet\" HREF=\"%s/man.css\""
+ " TYPE=\"text/css\" media=\"all\">\n"
+ "<TITLE>System Manpage Reference</TITLE>\n"
+ "</HEAD>\n"
+ "<BODY>\n"
+ "<!-- Begin page content. //-->\n",
+ cssdir, cssdir);
+}
+
+static void
+resp_end_html(void)
+{
+
+ puts("</BODY>\n"
+ "</HTML>");
+}
+
+static void
+resp_searchform(const struct req *req)
+{
+ int i;
+
+ puts("<!-- Begin search form. //-->");
+ printf("<DIV ID=\"mancgi\">\n"
+ "<FORM ACTION=\"%s/search\" METHOD=\"get\">\n"
+ "<FIELDSET>\n"
+ "<LEGEND>Search Parameters</LEGEND>\n"
+ "<INPUT TYPE=\"submit\" "
+ " VALUE=\"Search\"> for manuals matching \n"
+ "<INPUT TYPE=\"text\" NAME=\"expr\" VALUE=\"",
+ scriptname);
+ html_print(req->q.expr ? req->q.expr : "");
+ printf("\">, section "
+ "<INPUT TYPE=\"text\""
+ " SIZE=\"4\" NAME=\"sec\" VALUE=\"");
+ html_print(req->q.sec ? req->q.sec : "");
+ printf("\">, arch "
+ "<INPUT TYPE=\"text\""
+ " SIZE=\"8\" NAME=\"arch\" VALUE=\"");
+ html_print(req->q.arch ? req->q.arch : "");
+ printf("\">");
+ if (req->psz > 1) {
+ puts(", in <SELECT NAME=\"manpath\">");
+ for (i = 0; i < (int)req->psz; i++) {
+ printf("<OPTION ");
+ if (NULL == req->q.manpath ? 0 == i :
+ 0 == strcmp(req->q.manpath, req->p[i]))
+ printf("SELECTED=\"selected\" ");
+ printf("VALUE=\"");
+ html_print(req->p[i]);
+ printf("\">");
+ html_print(req->p[i]);
+ puts("</OPTION>");
+ }
+ puts("</SELECT>");
+ }
+ puts("—\n"
+ "<INPUT TYPE=\"reset\" VALUE=\"Reset\">\n"
+ "</FIELDSET>\n"
+ "</FORM>\n"
+ "</DIV>");
+ puts("<!-- End search form. //-->");
+}
+
+static void
+resp_index(const struct req *req)
+{
+
+ resp_begin_html(200, NULL);
+ puts("<H1>\n"
+ "Online manuals with "
+ "<A HREF=\"http://mdocml.bsd.lv/\">mandoc</A>\n"
+ "</H1>");
+ resp_searchform(req);
+ puts("<P>\n"
+ "This web interface is documented in the "
+ "<A HREF=\"search?expr=Nm~^man\\.cgi$&sec=8\">"
+ "man.cgi</A> manual, and the "
+ "<A HREF=\"search?expr=Nm~^apropos$&sec=1\">"
+ "apropos</A> manual explains the query syntax.\n"
+ "</P>");
+ resp_end_html();
+}
+
+static void
+resp_noresult(const struct req *req, const char *msg)
+{
+ resp_begin_html(200, NULL);
+ resp_searchform(req);
+ puts("<P>");
+ puts(msg);
+ puts("</P>");
+ resp_end_html();
+}
+
+static void
+resp_error_badrequest(const char *msg)
+{
+
+ resp_begin_html(400, "Bad Request");
+ puts("<H1>Bad Request</H1>\n"
+ "<P>\n");
+ puts(msg);
+ printf("Try again from the\n"
+ "<A HREF=\"%s\">main page</A>.\n"
+ "</P>", scriptname);
+ resp_end_html();
+}
+
+static void
+resp_error_notfound(const char *page)
+{
+
+ resp_begin_html(404, "Not Found");
+ puts("<H1>Page Not Found</H1>\n"
+ "<P>\n"
+ "The page you're looking for, ");
+ printf("<B>");
+ html_print(page);
+ printf("</B>,\n"
+ "could not be found.\n"
+ "Try searching from the\n"
+ "<A HREF=\"%s\">main page</A>.\n"
+ "</P>", scriptname);
+ resp_end_html();
+}
+
+static void
+resp_error_internal(void)
+{
+ resp_begin_html(500, "Internal Server Error");
+ puts("<P>Internal Server Error</P>");
+ resp_end_html();
+}
+
+static void
+resp_search(const struct req *req, struct manpage *r, size_t sz)
+{
+ size_t i;
+
+ if (1 == sz) {
+ /*
+ * If we have just one result, then jump there now
+ * without any delay.
+ */
+ puts("Status: 303 See Other");
+ 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");
+ return;
+ }
+
+ qsort(r, sz, sizeof(struct manpage), cmp);
+
+ resp_begin_html(200, NULL);
+ resp_searchform(req);
+ puts("<DIV CLASS=\"results\">");
+ puts("<TABLE>");
+
+ for (i = 0; i < sz; i++) {
+ printf("<TR>\n"
+ "<TD CLASS=\"title\">\n"
+ "<A HREF=\"%s/show/%s/%s?",
+ scriptname, req->q.manpath, r[i].file);
+ html_printquery(req);
+ printf("\">");
+ html_print(r[i].names);
+ printf("</A>\n"
+ "</TD>\n"
+ "<TD CLASS=\"desc\">");
+ html_print(r[i].output);
+ puts("</TD>\n"
+ "</TR>");
+ }
+
+ puts("</TABLE>\n"
+ "</DIV>");
+ resp_end_html();
+}