Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/makewhatis Make it parse old man pages, new mdoc pag...



details:   https://anonhg.NetBSD.org/src/rev/92db40019c21
branches:  trunk
changeset: 579985:92db40019c21
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Apr 03 21:32:45 2005 +0000

description:
Make it parse old man pages, new mdoc pages, broken pages, and pages where
the synopsis line does not give us the section number. Add -w flag to print
pages we still don't like. Now we parse 99% of the pages properly.

diffstat:

 libexec/makewhatis/makewhatis.8 |    7 +-
 libexec/makewhatis/makewhatis.c |  139 ++++++++++++++++++++++++++-------------
 2 files changed, 97 insertions(+), 49 deletions(-)

diffs (truncated from 311 to 300 lines):

diff -r 630f354cfc13 -r 92db40019c21 libexec/makewhatis/makewhatis.8
--- a/libexec/makewhatis/makewhatis.8   Sun Apr 03 20:31:18 2005 +0000
+++ b/libexec/makewhatis/makewhatis.8   Sun Apr 03 21:32:45 2005 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: makewhatis.8,v 1.9 2003/07/26 19:32:09 salo Exp $
+.\"    $NetBSD: makewhatis.8,v 1.10 2005/04/03 21:32:45 christos Exp $
 .\"
 .\" Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -34,7 +34,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd March 8, 2002
+.Dd April 3, 2005
 .Dt MAKEWHATIS 8
 .Os
 .Sh NAME
@@ -44,6 +44,7 @@
 .Nm /usr/libexec/makewhatis
 .Op Fl C Ar file
 .Op Fl f
+.Op Fl w
 .Op Ar manpath ...
 .Sh DESCRIPTION
 .Nm
@@ -92,6 +93,8 @@
 .It Fl f
 Don't spawn child processes to generate the individual database files,
 but do all the work synchronously in the foreground.
+.It Fl w
+Print warnings about input files we don't like.
 .El
 .Sh FILES
 .Bl -tag -compact -width /etc/man.conf1
diff -r 630f354cfc13 -r 92db40019c21 libexec/makewhatis/makewhatis.c
--- a/libexec/makewhatis/makewhatis.c   Sun Apr 03 20:31:18 2005 +0000
+++ b/libexec/makewhatis/makewhatis.c   Sun Apr 03 21:32:45 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: makewhatis.c,v 1.33 2005/04/03 20:31:18 christos Exp $ */
+/*     $NetBSD: makewhatis.c,v 1.34 2005/04/03 21:32:45 christos Exp $ */
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -44,7 +44,7 @@
 #if !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1999 The NetBSD Foundation, Inc.\n\
        All rights reserved.\n");
-__RCSID("$NetBSD: makewhatis.c,v 1.33 2005/04/03 20:31:18 christos Exp $");
+__RCSID("$NetBSD: makewhatis.c,v 1.34 2005/04/03 21:32:45 christos Exp $");
 #endif /* not lint */
 
 #include <sys/types.h>
@@ -95,16 +95,18 @@
 char   *findwhitespace(char *);
 char   *strmove(char *,char *);
 char   *GetS(gzFile, char *, size_t);
+int     pathnamesection(const char *, const char *);
 int     manpagesection(char *);
 char   *createsectionstring(char *);
 void    addmanpage(manpage **, ino_t, char *, size_t, size_t);
 void    addwhatis(whatis **, char *, char *);
-char   *replacestring(char *, char *, char *);
+char   *makesection(int);
+char   *makewhatisline(const char *, const char *, const char *);
 void    catpreprocess(char *);
-char   *parsecatpage(gzFile *);
+char   *parsecatpage(const char *, gzFile *);
 int     manpreprocess(char *);
-char   *nroff(gzFile *, const char *);
-char   *parsemanpage(gzFile *, int);
+char   *nroff(const char *, gzFile *);
+char   *parsemanpage(const char *, gzFile *, int);
 char   *getwhatisdata(char *);
 void    processmanpages(manpage **,whatis **);
 void    dumpwhatis(FILE *, whatis *);
@@ -119,6 +121,7 @@
 
 const char *sectionext = "0123456789ln";
 const char *whatisdb   = _PATH_WHATIS;
+static int dowarn = 0;
 
 int
 main(int argc, char *const *argv)
@@ -135,7 +138,7 @@
 
        (void)setlocale(LC_ALL, "");
 
-       while((c = getopt(argc, argv, "C:f")) != -1) {
+       while((c = getopt(argc, argv, "C:fw")) != -1) {
                switch(c) {
                case 'C':
                        conffile = optarg;
@@ -144,8 +147,11 @@
                        /* run all processing on foreground */
                        dofork = 0;
                        break;
+               case 'w':
+                       dowarn++;
+                       break;
                default:
-                       fprintf(stderr, "Usage: %s [-C file] [-f] [path ...]\n",
+                       fprintf(stderr, "Usage: %s [-C file] [-wf] [path ...]\n",
                                getprogname());
                        exit(EXIT_FAILURE);
                }
@@ -338,6 +344,34 @@
        return ptr;
 }
 
+char *
+makesection(int s)
+{
+       char sectionbuffer[24];
+       if (s == -1)
+               return NULL;
+       (void)snprintf(sectionbuffer, sizeof(sectionbuffer),
+               " (%c) - ", sectionext[s]);
+       return estrdup(sectionbuffer);
+}
+
+int
+pathnamesection(const char *pat, const char *name)
+{
+       char *ptr, *ext;
+       size_t len = strlen(pat);
+
+
+       while ((ptr = strstr(name, pat)) != NULL) {
+               if ((ext = strchr(sectionext, ptr[len])) != NULL) {
+                       return ext - sectionext;
+               }
+               name = ptr + 1;
+       }
+       return -1;
+}
+
+
 int
 manpagesection(char *name)
 {
@@ -352,14 +386,13 @@
                int section;
 
                ptr++;
-               section=0;
+               section = 0;
                while (sectionext[section] != '\0')
                        if (sectionext[section] == *ptr)
                                return section;
                        else
                                section++;
        }
-
        return -1;
 }
 
@@ -453,34 +486,50 @@
 }
 
 char *
-replacestring(char *string, char *old, char *new)
-
+makewhatisline(const char *file, const char *line, const char *section)
 {
-       char    *ptr, *result;
-       size_t   slength, olength, nlength, pos;
+       static const char *del[] = {
+               " - ",
+               " -- ",
+               "- ",
+               " -",
+               NULL
+       };
+       size_t i, pos;
+       size_t llen, slen, dlen;
+       char *result, *ptr;
 
-       if (new == NULL)
-               return estrdup(string);
+       if (section == NULL) {
+               if (dowarn)
+                       warnx("%s: No section provided for `%s'", file, line);
+               return estrdup(line);
+       }
 
-       ptr = strstr(string, old);
-       if (ptr == NULL)
-               return estrdup(string);
+       for (i = 0; del[i]; i++)
+               if ((ptr = strstr(line, del[i])) != NULL)
+                       break;
+
+       if (del[i] == NULL) {
+               if (dowarn)
+                       warnx("%s: Bad format line `%s'", file, line);
+               return estrdup(line);
+       }
 
-       slength = strlen(string);
-       olength = strlen(old);
-       nlength = strlen(new);
-       result = emalloc(slength - olength + nlength + 1);
+       slen = strlen(section);
+       llen = strlen(line);
+       dlen = strlen(del[i]);
 
-       pos = ptr - string;
-       (void)memcpy(result, string, pos);
-       (void)memcpy(&result[pos], new, nlength);
-       (void)strcpy(&result[pos + nlength], &string[pos + olength]);
+       result = emalloc(llen - dlen + slen + 1);
+       pos = ptr - line;
 
+       (void)memcpy(result, line, pos);
+       (void)memcpy(&result[pos], section, slen);
+       (void)strcpy(&result[pos + slen], &line[pos + dlen]);
        return result;
 }
 
 char *
-parsecatpage(gzFile *in)
+parsecatpage(const char *name, gzFile *in)
 {
        char     buffer[8192];
        char    *section, *ptr, *last;
@@ -514,6 +563,8 @@
                if (strncmp(buffer, "NAME", 4) == 0)
                        break;
        }
+       if (section == NULL)
+               section = makesection(pathnamesection("/cat", name));
 
        ptr = last = buffer;
        size = sizeof(buffer) - 1;
@@ -526,7 +577,7 @@
                if (length == 0) {
                        *last = '\0';
 
-                       ptr = replacestring(buffer, " -- ", section);
+                       ptr = makewhatisline(name, buffer, section);
                        free(section);
                        return ptr;
                }
@@ -622,7 +673,7 @@
 }
 
 char *
-nroff(gzFile *in, const char *inname)
+nroff(const char *inname, gzFile *in)
 {
        char tempname[MAXPATHLEN], buffer[65536], *data;
        int tempfd, bytes, pipefd[2], status;
@@ -704,7 +755,7 @@
                err(EXIT_FAILURE, "Cannot read from pipe");
        }
 
-       data = parsecatpage(in);
+       data = parsecatpage(inname, in);
        while (gzread(in, buffer, sizeof(buffer)) > 0);
        (void)gzclose(in);
 
@@ -721,7 +772,7 @@
 }
 
 char *
-parsemanpage(gzFile *in, int defaultsection)
+parsemanpage(const char *name, gzFile *in, int defaultsection)
 {
        char    *section, buffer[8192], *ptr;
 
@@ -921,17 +972,11 @@
                }
        }
 
-       if (section == NULL) {
-               char sectionbuffer[24];
+       if (section == NULL)
+               section = makesection(defaultsection);
 
-               (void) snprintf(sectionbuffer, sizeof(sectionbuffer),
-                   " (%c) - ", sectionext[defaultsection]);
-               ptr = replacestring(buffer, " - ", sectionbuffer);
-       }
-       else {
-               ptr = replacestring(buffer, " - ", section);
-               free(section);
-       }
+       ptr = makewhatisline(name, buffer, section);
+       free(section);
        return ptr;
 }
 
@@ -950,12 +995,12 @@
        }
 
        section = manpagesection(name);
-       if (section == 0)
-               data = parsecatpage(in);
-       else {



Home | Main Index | Thread Index | Old Index