2 * descriptions.c: manipulate man page descriptions
4 * Copyright (C) 2002, 2003, 2006, 2007, 2008, 2009, 2010, 2011 Colin Watson.
6 * This file is part of man-db.
8 * man-db is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * man-db is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with man-db; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #endif /* HAVE_CONFIG_H */
30 #include "manconfig.h"
31 #include "descriptions.h"
33 /* Parse the description in a whatis line returned by find_name() into a
34 * sequence of names and whatis descriptions.
36 struct page_description *parse_descriptions (const char *base,
39 const char *sep, *nextsep;
40 struct page_description *desc = NULL, *head = NULL;
55 /* Use a while loop so that we skip over things like the
56 * result of double line breaks.
58 while (*sep == 0x11 || *sep == ' ')
60 nextsep = strchr (sep, 0x11);
62 /* Get this record as a null-terminated string. */
64 length = (size_t) (nextsep - sep);
66 length = strlen (sep);
70 record = xstrndup (sep, length);
71 debug ("record = '%s'\n", record);
73 /* Split the record into name and whatis description. */
74 dash = strstr (record, " - ");
76 names = xstrndup (record, dash - record);
78 /* Some pages have a NAME section with just the page
79 * name and no whatis. We might as well include
82 names = xstrdup (record);
84 /* Once at least one record has been seen, further
85 * cases where there is no whatis usually amount to
86 * garbage following the useful records, and can
87 * cause problems due to false WHATIS_MAN entries in
88 * the database. On the whole it seems best to
93 for (token = strtok (names, ","); token;
94 token = strtok (NULL, ",")) {
95 char *name = trim_spaces (token);
97 /* Skip name tokens containing whitespace. They are
98 * almost never useful as manual page names.
100 if (strpbrk (name, " \t") != NULL) {
105 /* Allocate new description node. */
107 desc->next = xmalloc (sizeof *desc);
110 desc = xmalloc (sizeof *desc);
113 desc->name = name; /* steal memory */
114 desc->whatis = dash ? trim_spaces (dash + 3) : NULL;
117 if (base && STREQ (base, desc->name))
128 /* If it isn't there already, add the base name onto the returned
131 if (base && !seen_base) {
133 desc->next = xmalloc (sizeof *desc);
136 head->whatis ? xstrdup (head->whatis) : NULL;
138 desc = xmalloc (sizeof *desc);
142 desc->name = xstrdup (base);
149 /* Free a description list and all its contents. */
150 void free_descriptions (struct page_description *head)
152 struct page_description *desc = head, *prev;