Print this page
Add catman, makewhatis functionality.  Print an error if the whatis database
is missing.


 142 };
 143 
 144 struct man_node {
 145         char            *path;          /* mandir path */
 146         char            **secv;         /* submandir suffices */
 147         int             defsrch;        /* hint for man -p */
 148         int             frompath;       /* hint for man -d */
 149         struct man_node *next;
 150 };
 151 
 152 static int      all = 0;
 153 static int      apropos = 0;
 154 static int      debug = 0;
 155 static int      found = 0;
 156 static int      list = 0;
 157 static int      makewhatis = 0;
 158 static int      printmp = 0;
 159 static int      sargs = 0;
 160 static int      psoutput = 0;
 161 static int      whatis = 0;

 162 
 163 static char     *mansec;
 164 static char     *pager;
 165 
 166 static char     *addlocale(char *);
 167 static struct man_node *build_manpath(char **, int);
 168 static void     do_makewhatis(struct man_node *);
 169 static char     *check_config(char *);
 170 static int      cmp(const void *, const void *);
 171 static int      dupcheck(struct man_node *, struct dupnode **);
 172 static int      format(char *, char *, char *, char *);
 173 static void     free_dupnode(struct dupnode *);
 174 static void     free_manp(struct man_node *manp);
 175 static void     freev(char **);
 176 static void     fullpaths(struct man_node **);
 177 static void     get_all_sect(struct man_node *);
 178 static int      getdirs(char *, char ***, int);
 179 static void     getpath(struct man_node *, char **);
 180 static void     getsect(struct man_node *, char **);
 181 static void     init_bintoman(void);
 182 static void     lower(char *);
 183 static void     mandir(char **, char *, char *, int);
 184 static int      manual(struct man_node *, char *);
 185 static char     *map_section(char *, char *);
 186 static char     *path_to_manpath(char *);
 187 static void     print_manpath(struct man_node *);
 188 static void     search_whatis(char *, char *);
 189 static int      searchdir(char *, char *, char *);
 190 static void     sortdir(DIR *, char ***);
 191 static char     **split(char *, char);
 192 static void     usage_man(void);
 193 static void     usage_whatapro(void);


 194 static void     whatapro(struct man_node *, char *);
 195 
 196 static char     language[MAXPATHLEN];   /* LC_MESSAGES */
 197 static char     localedir[MAXPATHLEN];  /* locale specific path component */
 198 
 199 static char     *newsection = NULL;
 200 
 201 static int      manwidth = 0;
 202 
 203 extern const char       *__progname;
 204 
 205 int
 206 main(int argc, char **argv)
 207 {
 208         int             c, i;
 209         char            **pathv;
 210         char            *manpath = NULL;
 211         static struct man_node *mandirs = NULL;
 212         int             bmp_flags = 0;
 213         int             ret = 0;
 214         char            *opts;
 215         char            *mwstr;

 216 
 217         (void) setlocale(LC_ALL, "");
 218         (void) strcpy(language, setlocale(LC_MESSAGES, (char *)NULL));
 219         if (strcmp("C", language) != 0)
 220                 (void) strlcpy(localedir, language, MAXPATHLEN);
 221 
 222 #if !defined(TEXT_DOMAIN)
 223 #define TEXT_DOMAIN "SYS_TEST"
 224 #endif
 225         (void) textdomain(TEXT_DOMAIN);
 226 
 227         if (strcmp(__progname, "apropos") == 0) {
 228                 apropos++;
 229                 opts = "M:ds:";
 230         } else if (strcmp(__progname, "whatis") == 0) {
 231                 apropos++;
 232                 whatis++;
 233                 opts = "M:ds:";









 234         } else {
 235                 opts = "M:adfklps:tw";
 236         }
 237 
 238         opterr = 0;
 239         while ((c = getopt(argc, argv, opts)) != -1) {
 240                 switch (c) {
 241                 case 'M':       /* Respecify path for man pages */
 242                         manpath = optarg;
 243                         break;
 244                 case 'a':
 245                         all++;
 246                         break;
 247                 case 'd':
 248                         debug++;
 249                         break;
 250                 case 'f':
 251                         whatis++;
 252                         /*FALLTHROUGH*/
 253                 case 'k':


 256                 case 'l':
 257                         list++;
 258                         /*FALLTHROUGH*/
 259                 case 'p':
 260                         printmp++;
 261                         break;
 262                 case 's':
 263                         mansec = optarg;
 264                         sargs++;
 265                         break;
 266                 case 't':
 267                         psoutput++;
 268                         break;
 269                 case 'w':
 270                         makewhatis++;
 271                         break;
 272                 case '?':
 273                 default:
 274                         if (apropos)
 275                                 usage_whatapro();




 276                         else
 277                                 usage_man();
 278                 }
 279         }
 280         argc -= optind;
 281         argv += optind;
 282 
 283         if (argc == 0) {
 284                 if (apropos) {
 285                         (void) fprintf(stderr, gettext("%s what?\n"),
 286                             __progname);
 287                         exit(1);
 288                 } else if (!printmp && !makewhatis) {
 289                         (void) fprintf(stderr,
 290                             gettext("What manual page do you want?\n"));
 291                         exit(1);
 292                 }
 293         }
 294 
 295         init_bintoman();


 673                         free(ldir);
 674                 }
 675                 (void) snprintf(whatpath, sizeof (whatpath), "%s/%s", b->path,
 676                     WHATIS);
 677                 search_whatis(whatpath, word);
 678         }
 679 }
 680 
 681 static void
 682 search_whatis(char *whatpath, char *word)
 683 {
 684         FILE            *fp;
 685         char            *line = NULL;
 686         size_t          linecap = 0;
 687         char            *pkwd;
 688         regex_t         preg;
 689         char            **ss = NULL;
 690         char            s[MAXNAMELEN];
 691         int             i;
 692 
 693         if ((fp = fopen(whatpath, "r")) == NULL)

 694                 return;

 695 
 696         DPRINTF("-- Found %s: %s\n", WHATIS, whatpath);
 697 
 698         /* Build keyword regex */
 699         if (asprintf(&pkwd, "%s%s%s", (whatis) ? "\\<" : "",
 700             word, (whatis) ? "\\>" : "") == -1)
 701                 err(1, "asprintf");
 702 
 703         if (regcomp(&preg, pkwd, REG_BASIC | REG_ICASE | REG_NOSUB) != 0)
 704                 err(1, "regcomp");
 705 
 706         if (sargs)
 707                 ss = split(mansec, ',');
 708 
 709         while (getline(&line, &linecap, fp) > 0) {
 710                 if (regexec(&preg, line, 0, NULL, 0) == 0) {
 711                         if (sargs) {
 712                                 /* Section-restricted search */
 713                                 for (i = 0; ss[i] != NULL; i++) {
 714                                         (void) snprintf(s, sizeof (s), "(%s)",


1547 usage_man(void)
1548 {
1549 
1550         (void) fprintf(stderr, gettext(
1551 "usage: man [-alptw] [-M path] [-s section] name ...\n"
1552 "       man [-M path] [-s section] -k keyword -- emulate apropos\n"
1553 "       man [-M path] [-s section] -f keyword -- emulate whatis\n"));
1554 
1555         exit(1);
1556 }
1557 
1558 static void
1559 usage_whatapro(void)
1560 {
1561 
1562         (void) fprintf(stderr, gettext(
1563 "usage: %s [-M path] [-s section] keyword ...\n"),
1564             whatis ? "whatis" : "apropos");
1565 
1566         exit(1);

















1567 }


 142 };
 143 
 144 struct man_node {
 145         char            *path;          /* mandir path */
 146         char            **secv;         /* submandir suffices */
 147         int             defsrch;        /* hint for man -p */
 148         int             frompath;       /* hint for man -d */
 149         struct man_node *next;
 150 };
 151 
 152 static int      all = 0;
 153 static int      apropos = 0;
 154 static int      debug = 0;
 155 static int      found = 0;
 156 static int      list = 0;
 157 static int      makewhatis = 0;
 158 static int      printmp = 0;
 159 static int      sargs = 0;
 160 static int      psoutput = 0;
 161 static int      whatis = 0;
 162 static int      makewhatishere = 0;
 163 
 164 static char     *mansec;
 165 static char     *pager;
 166 
 167 static char     *addlocale(char *);
 168 static struct man_node *build_manpath(char **, int);
 169 static void     do_makewhatis(struct man_node *);
 170 static char     *check_config(char *);
 171 static int      cmp(const void *, const void *);
 172 static int      dupcheck(struct man_node *, struct dupnode **);
 173 static int      format(char *, char *, char *, char *);
 174 static void     free_dupnode(struct dupnode *);
 175 static void     free_manp(struct man_node *manp);
 176 static void     freev(char **);
 177 static void     fullpaths(struct man_node **);
 178 static void     get_all_sect(struct man_node *);
 179 static int      getdirs(char *, char ***, int);
 180 static void     getpath(struct man_node *, char **);
 181 static void     getsect(struct man_node *, char **);
 182 static void     init_bintoman(void);
 183 static void     lower(char *);
 184 static void     mandir(char **, char *, char *, int);
 185 static int      manual(struct man_node *, char *);
 186 static char     *map_section(char *, char *);
 187 static char     *path_to_manpath(char *);
 188 static void     print_manpath(struct man_node *);
 189 static void     search_whatis(char *, char *);
 190 static int      searchdir(char *, char *, char *);
 191 static void     sortdir(DIR *, char ***);
 192 static char     **split(char *, char);
 193 static void     usage_man(void);
 194 static void     usage_whatapro(void);
 195 static void     usage_catman(void);
 196 static void     usage_makewhatis(void);
 197 static void     whatapro(struct man_node *, char *);
 198 
 199 static char     language[MAXPATHLEN];   /* LC_MESSAGES */
 200 static char     localedir[MAXPATHLEN];  /* locale specific path component */
 201 
 202 static char     *newsection = NULL;
 203 
 204 static int      manwidth = 0;
 205 
 206 extern const char       *__progname;
 207 
 208 int
 209 main(int argc, char **argv)
 210 {
 211         int             c, i;
 212         char            **pathv;
 213         char            *manpath = NULL;
 214         static struct man_node *mandirs = NULL;
 215         int             bmp_flags = 0;
 216         int             ret = 0;
 217         char            *opts;
 218         char            *mwstr;
 219         int             catman = 0;
 220 
 221         (void) setlocale(LC_ALL, "");
 222         (void) strcpy(language, setlocale(LC_MESSAGES, (char *)NULL));
 223         if (strcmp("C", language) != 0)
 224                 (void) strlcpy(localedir, language, MAXPATHLEN);
 225 
 226 #if !defined(TEXT_DOMAIN)
 227 #define TEXT_DOMAIN "SYS_TEST"
 228 #endif
 229         (void) textdomain(TEXT_DOMAIN);
 230 
 231         if (strcmp(__progname, "apropos") == 0) {
 232                 apropos++;
 233                 opts = "M:ds:";
 234         } else if (strcmp(__progname, "whatis") == 0) {
 235                 apropos++;
 236                 whatis++;
 237                 opts = "M:ds:";
 238         } else if (strcmp(__progname, "catman") == 0) {
 239                 catman++;
 240                 makewhatis++;
 241                 opts = "M:w";
 242         } else if (strcmp(__progname, "makewhatis") == 0) {
 243                 makewhatis++;
 244                 makewhatishere++;
 245                 manpath = ".";
 246                 opts = "";
 247         } else {
 248                 opts = "M:adfklps:tw";
 249         }
 250 
 251         opterr = 0;
 252         while ((c = getopt(argc, argv, opts)) != -1) {
 253                 switch (c) {
 254                 case 'M':       /* Respecify path for man pages */
 255                         manpath = optarg;
 256                         break;
 257                 case 'a':
 258                         all++;
 259                         break;
 260                 case 'd':
 261                         debug++;
 262                         break;
 263                 case 'f':
 264                         whatis++;
 265                         /*FALLTHROUGH*/
 266                 case 'k':


 269                 case 'l':
 270                         list++;
 271                         /*FALLTHROUGH*/
 272                 case 'p':
 273                         printmp++;
 274                         break;
 275                 case 's':
 276                         mansec = optarg;
 277                         sargs++;
 278                         break;
 279                 case 't':
 280                         psoutput++;
 281                         break;
 282                 case 'w':
 283                         makewhatis++;
 284                         break;
 285                 case '?':
 286                 default:
 287                         if (apropos)
 288                                 usage_whatapro();
 289                         else if (catman)
 290                                 usage_catman();
 291                         else if (makewhatishere)
 292                                 usage_makewhatis();
 293                         else
 294                                 usage_man();
 295                 }
 296         }
 297         argc -= optind;
 298         argv += optind;
 299 
 300         if (argc == 0) {
 301                 if (apropos) {
 302                         (void) fprintf(stderr, gettext("%s what?\n"),
 303                             __progname);
 304                         exit(1);
 305                 } else if (!printmp && !makewhatis) {
 306                         (void) fprintf(stderr,
 307                             gettext("What manual page do you want?\n"));
 308                         exit(1);
 309                 }
 310         }
 311 
 312         init_bintoman();


 690                         free(ldir);
 691                 }
 692                 (void) snprintf(whatpath, sizeof (whatpath), "%s/%s", b->path,
 693                     WHATIS);
 694                 search_whatis(whatpath, word);
 695         }
 696 }
 697 
 698 static void
 699 search_whatis(char *whatpath, char *word)
 700 {
 701         FILE            *fp;
 702         char            *line = NULL;
 703         size_t          linecap = 0;
 704         char            *pkwd;
 705         regex_t         preg;
 706         char            **ss = NULL;
 707         char            s[MAXNAMELEN];
 708         int             i;
 709 
 710         if ((fp = fopen(whatpath, "r")) == NULL) {
 711                 perror(whatpath);
 712                 return;
 713         }
 714 
 715         DPRINTF("-- Found %s: %s\n", WHATIS, whatpath);
 716 
 717         /* Build keyword regex */
 718         if (asprintf(&pkwd, "%s%s%s", (whatis) ? "\\<" : "",
 719             word, (whatis) ? "\\>" : "") == -1)
 720                 err(1, "asprintf");
 721 
 722         if (regcomp(&preg, pkwd, REG_BASIC | REG_ICASE | REG_NOSUB) != 0)
 723                 err(1, "regcomp");
 724 
 725         if (sargs)
 726                 ss = split(mansec, ',');
 727 
 728         while (getline(&line, &linecap, fp) > 0) {
 729                 if (regexec(&preg, line, 0, NULL, 0) == 0) {
 730                         if (sargs) {
 731                                 /* Section-restricted search */
 732                                 for (i = 0; ss[i] != NULL; i++) {
 733                                         (void) snprintf(s, sizeof (s), "(%s)",


1566 usage_man(void)
1567 {
1568 
1569         (void) fprintf(stderr, gettext(
1570 "usage: man [-alptw] [-M path] [-s section] name ...\n"
1571 "       man [-M path] [-s section] -k keyword -- emulate apropos\n"
1572 "       man [-M path] [-s section] -f keyword -- emulate whatis\n"));
1573 
1574         exit(1);
1575 }
1576 
1577 static void
1578 usage_whatapro(void)
1579 {
1580 
1581         (void) fprintf(stderr, gettext(
1582 "usage: %s [-M path] [-s section] keyword ...\n"),
1583             whatis ? "whatis" : "apropos");
1584 
1585         exit(1);
1586 }
1587 
1588 static void
1589 usage_catman(void)
1590 {
1591         (void) fprintf(stderr, gettext(
1592 "usage: catman [-M path] [-w]\n"));
1593 
1594         exit(1);
1595 }
1596 
1597 static void
1598 usage_makewhatis(void)
1599 {
1600         (void) fprintf(stderr, gettext("usage: makewhatis\n"));
1601 
1602         exit(1);
1603 }