+/*
+ * If PATH_INFO is not a file name, translate it to a query.
+ */
+static void
+path_parse(struct req *req, const char *path)
+{
+ int dir_done;
+
+ req->q.equal = 1;
+ req->q.manpath = mandoc_strdup(path);
+
+ /* Mandatory manual page name. */
+ if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) {
+ req->q.query = req->q.manpath;
+ req->q.manpath = NULL;
+ } else
+ *req->q.query++ = '\0';
+
+ /* Optional trailing section. */
+ if ((req->q.sec = strrchr(req->q.query, '.')) != NULL) {
+ if(isdigit((unsigned char)req->q.sec[1])) {
+ *req->q.sec++ = '\0';
+ req->q.sec = mandoc_strdup(req->q.sec);
+ } else
+ req->q.sec = NULL;
+ }
+
+ /* Handle the case of name[.section] only. */
+ if (req->q.manpath == NULL) {
+ req->q.arch = NULL;
+ return;
+ }
+ req->q.query = mandoc_strdup(req->q.query);
+
+ /* Optional architecture. */
+ dir_done = 0;
+ for (;;) {
+ if ((req->q.arch = strrchr(req->q.manpath, '/')) == NULL)
+ break;
+ *req->q.arch++ = '\0';
+ if (dir_done || strncmp(req->q.arch, "man", 3)) {
+ req->q.arch = mandoc_strdup(req->q.arch);
+ break;
+ }
+
+ /* Optional directory name. */
+ req->q.arch += 3;
+ if (*req->q.arch != '\0') {
+ free(req->q.sec);
+ req->q.sec = mandoc_strdup(req->q.arch);
+ }
+ dir_done = 1;
+ }
+}
+