source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Change the mansearch() interface to use the mlinks table in the
@ 2013-12-27 18:51 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2013-12-27 18:51 UTC (permalink / raw)
  To: source

Log Message:
-----------
Change the mansearch() interface to use the mlinks table in the database
and return a list of names with sections, used by apropos(1) for display.
While here, improve uniformity of the interface by allocating the file 
name dynamically, just like the names list and the description.

Modified Files:
--------------
    mdocml:
        mansearch.h
        mansearch.c
        apropos.c
        manpage.c

Revision Data
-------------
Index: manpage.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/manpage.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -Lmanpage.c -Lmanpage.c -u -p -r1.4 -r1.5
--- manpage.c
+++ manpage.c
@@ -106,11 +106,14 @@ main(int argc, char *argv[])
 
 	for (i = 0; i < sz; i++) {
 		printf("%6zu  %s: %s\n", 
-			i + 1, res[i].file, res[i].desc);
+			i + 1, res[i].names, res[i].desc);
+		free(res[i].names);
 		free(res[i].desc);
 	}
 
 	if (0 == term) {
+		for (i = 0; i < sz; i++)
+			free(res[i].file);
 		free(res);
 		return(EXIT_SUCCESS);
 	}
@@ -127,12 +130,16 @@ main(int argc, char *argv[])
 		}
 
 	if (0 == i) {
+		for (i = 0; i < sz; i++)
+			free(res[i].file);
 		free(res);
 		return(EXIT_SUCCESS);
 	}
 show:
 	cmd = res[i - 1].form ? "mandoc" : "cat";
 	strlcpy(buf, res[i - 1].file, PATH_MAX);
+	for (i = 0; i < sz; i++)
+		free(res[i].file);
 	free(res);
 
 	show(cmd, buf);
Index: mansearch.h
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mansearch.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -Lmansearch.h -Lmansearch.h -u -p -r1.4 -r1.5
--- mansearch.h
+++ mansearch.h
@@ -61,7 +61,8 @@
 __BEGIN_DECLS
 
 struct	manpage {
-	char		 file[PATH_MAX]; /* prefixed by manpath */
+	char		*file; /* to be prefixed by manpath */
+	char		*names; /* a list of names with sections */
 	char		*desc; /* description of manpage */
 	int		 form; /* 0 == catpage */
 };
Index: mansearch.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mansearch.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -Lmansearch.c -Lmansearch.c -u -p -r1.9 -r1.10
--- mansearch.c
+++ mansearch.c
@@ -144,7 +144,9 @@ mansearch(const struct mansearch *search
 	int		 fd, rc, c;
 	int64_t		 id;
 	char		 buf[PATH_MAX];
-	char		*sql;
+	char		*sql, *newnames;
+	const char	*oldnames, *sep1, *name, *sec, *sep2, *arch;
+	struct manpage	*mpage;
 	struct expr	*e, *ep;
 	sqlite3		*db;
 	sqlite3_stmt	*s;
@@ -282,7 +284,12 @@ mansearch(const struct mansearch *search
 			fprintf(stderr, "%s\n", sqlite3_errmsg(db));
 
 		sqlite3_finalize(s);
-		sqlite3_close(db);
+
+		c = sqlite3_prepare_v2(db, 
+		    "SELECT * FROM mlinks WHERE pageid=?",
+		    -1, &s, NULL);
+		if (SQLITE_OK != c)
+			fprintf(stderr, "%s\n", sqlite3_errmsg(db));
 
 		for (mp = ohash_first(&htab, &idx);
 				NULL != mp;
@@ -292,16 +299,50 @@ mansearch(const struct mansearch *search
 				*res = mandoc_realloc
 					(*res, maxres * sizeof(struct manpage));
 			}
-			strlcpy((*res)[cur].file, 
-				paths->paths[i], PATH_MAX);
-			strlcat((*res)[cur].file, "/", PATH_MAX);
-			strlcat((*res)[cur].file, mp->file, PATH_MAX);
-			(*res)[cur].desc = mp->desc;
-			(*res)[cur].form = mp->form;
+			mpage = *res + cur;
+			if (-1 == asprintf(&mpage->file, "%s/%s",
+			    paths->paths[i], mp->file)) {
+				perror(0);
+				exit((int)MANDOCLEVEL_SYSERR);
+			}
+			mpage->names = NULL;
+			mpage->desc = mp->desc;
+			mpage->form = mp->form;
+
+			j = 1;
+			SQL_BIND_INT64(db, s, j, mp->id);
+			while (SQLITE_ROW == (c = sqlite3_step(s))) {
+				if (NULL == mpage->names) {
+					oldnames = "";
+					sep1 = "";
+				} else {
+					oldnames = mpage->names;
+					sep1 = ", ";
+				}
+				sec = sqlite3_column_text(s, 1);
+				arch = sqlite3_column_text(s, 2);
+				name = sqlite3_column_text(s, 3);
+				sep2 = '\0' == *arch ? "" : "/";
+				if (-1 == asprintf(&newnames,
+				    "%s%s%s(%s%s%s)", oldnames, sep1,
+				    name, sec, sep2, arch)) {
+					perror(0);
+					exit((int)MANDOCLEVEL_SYSERR);
+				}
+				free(mpage->names);
+				mpage->names = newnames;
+			}
+			if (SQLITE_DONE != c)
+				fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+			sqlite3_reset(s);
+
 			free(mp->file);
 			free(mp);
 			cur++;
 		}
+
+		sqlite3_finalize(s);
+		sqlite3_close(db);
 		ohash_delete(&htab);
 	}
 	rc = 1;
Index: apropos.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/apropos.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -Lapropos.c -Lapropos.c -u -p -r1.34 -r1.35
--- apropos.c
+++ apropos.c
@@ -96,7 +96,9 @@ main(int argc, char *argv[])
 		goto usage;
 
 	for (i = 0; i < sz; i++) {
-		printf("%s - %s\n", res[i].file, res[i].desc);
+		printf("%s - %s\n", res[i].names, res[i].desc);
+		free(res[i].file);
+		free(res[i].names);
 		free(res[i].desc);
 	}
 
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2013-12-27 18:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-27 18:51 mdocml: Change the mansearch() interface to use the mlinks table in the schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).