1996-01-28 00:41:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995 by Devin Reade <gdr@myrias.com>. For distribution
|
|
|
|
* information see the README file that is part of the manpack archive,
|
|
|
|
* or contact the author, above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
segment "makewhatis";
|
1996-01-28 00:31:34 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
1996-01-28 00:33:14 +00:00
|
|
|
#include "getopt.h"
|
1996-01-28 00:31:34 +00:00
|
|
|
#include <libc.h>
|
|
|
|
|
|
|
|
#include "makewhatis.h"
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
#ifdef STACK_CHECK
|
|
|
|
extern void begin_stack_check(void);
|
1996-01-28 00:33:14 +00:00
|
|
|
extern int end_stack_check(void);
|
|
|
|
#endif
|
|
|
|
|
1996-01-28 00:31:34 +00:00
|
|
|
/*
|
|
|
|
* Options:
|
|
|
|
*
|
1996-01-28 00:41:22 +00:00
|
|
|
* -c check cat* subdirectories as well
|
|
|
|
* -C check _only_ cat* subdirectories, not man* subdirectories.
|
|
|
|
* -f outfile force whatis status output to <outfile>
|
|
|
|
* -l logfile log errors to <logfile>
|
|
|
|
* -o dbfile force whatis database output to <dbfile>
|
|
|
|
* -p path use the <path> rather than $MANPATH, $USRMAN, or $MANDIR
|
|
|
|
* -s sort the whatis database by manual page name
|
|
|
|
* -v{1|2|3} verbose
|
|
|
|
* -V show version and usage info and exit
|
1996-01-28 00:31:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
char *man_subdir[] = {
|
1996-01-28 00:41:22 +00:00
|
|
|
"man1",
|
1996-01-28 00:31:34 +00:00
|
|
|
"man2",
|
|
|
|
"man3",
|
1996-01-28 00:41:22 +00:00
|
|
|
"man3f",
|
1996-01-28 00:31:34 +00:00
|
|
|
"man4",
|
|
|
|
"man5",
|
|
|
|
"man6",
|
|
|
|
"man7",
|
|
|
|
"man8",
|
|
|
|
"mann",
|
|
|
|
"manl",
|
1996-01-28 00:41:22 +00:00
|
|
|
"manp",
|
|
|
|
"mano",
|
|
|
|
NULL
|
1996-01-28 00:31:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we include cat* since some Gno utility man pages aren't written in
|
|
|
|
* either nroff or aroff source ... go figure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *cat_subdir[] = {
|
|
|
|
"cat1",
|
|
|
|
"cat2",
|
1996-01-28 00:41:22 +00:00
|
|
|
"cat3f",
|
1996-01-28 00:31:34 +00:00
|
|
|
"cat4",
|
|
|
|
"cat5",
|
|
|
|
"cat6",
|
|
|
|
"cat7",
|
|
|
|
"cat8",
|
|
|
|
"catn",
|
|
|
|
"catl",
|
1996-01-28 00:41:22 +00:00
|
|
|
"catp",
|
|
|
|
"cato",
|
|
|
|
NULL /* _must_ be NULL terminated! */
|
1996-01-28 00:31:34 +00:00
|
|
|
};
|
|
|
|
|
1996-01-28 00:33:14 +00:00
|
|
|
/* For the various command line flags */
|
|
|
|
|
|
|
|
short c_flag = 0;
|
|
|
|
short C_flag = 0;
|
|
|
|
short o_flag = 0;
|
|
|
|
short p_flag = 0;
|
|
|
|
short v_flag = 0;
|
1996-01-28 00:41:22 +00:00
|
|
|
short errflag = 0; /* This is set if there is a usage error or -V flag */
|
1996-01-28 00:33:14 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
static char filebuffer[FILENAME_MAX]; /* used when traversing cat* pages */
|
|
|
|
static char progdir[FILENAME_MAX]; /* the directory where makewhatis started */
|
1996-01-28 00:33:14 +00:00
|
|
|
|
1996-01-28 00:31:34 +00:00
|
|
|
FILE *output_fp;
|
|
|
|
FILE *error_fp;
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, char **argv) {
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
char *manpath; /* the location of the man pages */
|
|
|
|
char *path; /* the current path; taken from manpath */
|
|
|
|
char *p=NULL; /* a temporary pointer */
|
|
|
|
struct dirent *file; /* the current file we have open */
|
|
|
|
char tmp_file[L_tmpnam]; /* a scratch file */
|
|
|
|
FILE *tmp_fp; /* pointer to tmp_file */
|
|
|
|
FILE *whatis_fp; /* pointer to the current whatis database */
|
|
|
|
DIR *subdir; /* the current man subdirectory -- eg: /usr/man/man3 */
|
|
|
|
char *dbfile; /* non-default name of the whatis database */
|
1996-01-28 00:31:34 +00:00
|
|
|
char *dirsep; /* the directory separator, either "/" or ":" */
|
|
|
|
int i;
|
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
|
|
|
|
|
|
|
/* make sure Gno is running */
|
|
|
|
if (needsgno()==0) {
|
1996-01-28 00:41:22 +00:00
|
|
|
fprintf(stderr,"Requires Gno/ME\n");
|
1996-01-28 00:31:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
#ifdef STACK_CHECK
|
|
|
|
begin_stack_check();
|
1996-01-28 00:33:14 +00:00
|
|
|
#endif
|
|
|
|
|
1996-01-28 00:31:34 +00:00
|
|
|
/*
|
|
|
|
* set the defaults
|
|
|
|
*/
|
|
|
|
|
|
|
|
output_fp = stdout;
|
|
|
|
error_fp = stderr;
|
1996-01-28 00:41:22 +00:00
|
|
|
if (getwd(progdir) == NULL) {
|
|
|
|
perror("getwd() failed");
|
1996-01-28 00:31:34 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse the command line
|
|
|
|
*/
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
while((i = getopt(argc,argv,"cCf:l:o:p:v:V")) != EOF)
|
|
|
|
switch(i) {
|
1996-01-28 00:31:34 +00:00
|
|
|
case 'c':
|
1996-01-28 00:41:22 +00:00
|
|
|
if (C_flag) errflag++;
|
1996-01-28 00:31:34 +00:00
|
|
|
else c_flag++;
|
|
|
|
break;
|
|
|
|
case 'C':
|
1996-01-28 00:41:22 +00:00
|
|
|
if (c_flag) errflag++;
|
|
|
|
else C_flag++;
|
1996-01-28 00:31:34 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
1996-01-28 00:41:22 +00:00
|
|
|
output_fp = fopen (optarg,"w");
|
1996-01-28 00:31:34 +00:00
|
|
|
if (output_fp == NULL) {
|
1996-01-28 00:41:22 +00:00
|
|
|
fprintf(stderr,"Could not open output file %s; using stdout.\n",
|
|
|
|
optarg);
|
1996-01-28 00:31:34 +00:00
|
|
|
output_fp = stdout;
|
|
|
|
}
|
1996-01-28 00:41:22 +00:00
|
|
|
break;
|
1996-01-28 00:31:34 +00:00
|
|
|
case 'l':
|
1996-01-28 00:41:22 +00:00
|
|
|
error_fp = fopen (optarg,"w");
|
1996-01-28 00:31:34 +00:00
|
|
|
if (error_fp == NULL) {
|
1996-01-28 00:41:22 +00:00
|
|
|
fprintf(stderr,"Could not open log file %s; using stderr.\n",
|
|
|
|
optarg);
|
1996-01-28 00:31:34 +00:00
|
|
|
error_fp = stderr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'o':
|
1996-01-28 00:41:22 +00:00
|
|
|
o_flag++;
|
1996-01-28 00:31:34 +00:00
|
|
|
dbfile = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
1996-01-28 00:41:22 +00:00
|
|
|
p_flag++;
|
1996-01-28 00:31:34 +00:00
|
|
|
p = optarg;
|
|
|
|
break;
|
|
|
|
case 'v':
|
1996-01-28 00:41:22 +00:00
|
|
|
v_flag = (short) atoi(optarg);
|
|
|
|
if ((v_flag<1) && (v_flag>3)) errflag++;
|
1996-01-28 00:31:34 +00:00
|
|
|
break;
|
|
|
|
case 'V':
|
1996-01-28 00:41:22 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s --\n\tCreate the %s database.\n\tVersion %s by Devin Reade\n\n",
|
1996-01-28 00:31:34 +00:00
|
|
|
argv[0],WHATIS,VERSIONSTRING);
|
|
|
|
errflag++;
|
|
|
|
break;
|
|
|
|
default:
|
1996-01-28 00:41:22 +00:00
|
|
|
errflag++;
|
1996-01-28 00:31:34 +00:00
|
|
|
break;
|
|
|
|
}
|
1996-01-28 00:41:22 +00:00
|
|
|
if (errflag) {
|
|
|
|
fprintf(error_fp,
|
1996-01-28 00:33:14 +00:00
|
|
|
"Usage:\n%s\t[-c|-C] [-f outfile] [-l logfile] [-o dbfile] [-p path]\n\
|
|
|
|
\t\t[-v 1|2] [-V]\n\n\
|
1996-01-28 00:31:34 +00:00
|
|
|
-c\t\tCheck catX subdirectories as well\n\
|
|
|
|
-C\t\tCheck _only_ catX subdirectories, not manX subdirectories.\n\
|
|
|
|
-f outfile\tForce whatis output to <outfile>.\n\
|
|
|
|
-l logfile\tLog errors to <logfile>.\n\
|
|
|
|
-o dbfile\tforce whatis database output to <dbfile>.\n\
|
|
|
|
-p path\t\tUse the <path> rather than $MANPATH, $USRMAN, or $MANDIR.\n\
|
1996-01-28 00:41:22 +00:00
|
|
|
-v n\t\t<n>=1: Slightly verbose, only displaying major errors.\n\
|
|
|
|
\t\t<n>=2: Verbose, displaying processed file names.\n\
|
|
|
|
\t\t<n>=3: Very verbose, displaying more processing info.\n\
|
1996-01-28 00:33:14 +00:00
|
|
|
-V\t\tShow version and usage information, then exit.\n",argv[0]);
|
1996-01-28 00:41:22 +00:00
|
|
|
return -1;
|
1996-01-28 00:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the location of the man pages; p is already set if -p flag used
|
|
|
|
*/
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
if (p == NULL) p = getenv("MANPATH");
|
|
|
|
if (p == NULL) p = getenv("USRMAN");
|
|
|
|
if (p == NULL) p = getenv("MANDIR");
|
|
|
|
if (p == NULL) p = DEFAULT_MANPATH;
|
1996-01-28 00:31:34 +00:00
|
|
|
|
|
|
|
/* define the directory separator */
|
|
|
|
dirsep = (strchr(p,':')==NULL) ? "/" : ":";
|
|
|
|
|
|
|
|
/* make a copy of the location */
|
|
|
|
if ((manpath = malloc (strlen(p) + 1)) == NULL) {
|
1996-01-28 00:41:22 +00:00
|
|
|
if (v_flag) fprintf(error_fp,
|
|
|
|
"malloc failed while making copy of \"%s\"\nAborted.\n",p);
|
1996-01-28 00:31:34 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
strcpy(manpath,p);
|
|
|
|
|
|
|
|
/* get the name of the temporary file */
|
|
|
|
tmpnam (tmp_file);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* loop over all the paths in manpath. Don't use ':' as a delimiter since
|
|
|
|
* the colon is a pathname separator under GS/OS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
path = strtok (manpath," ");
|
|
|
|
while (path != NULL) {
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
if (access(path,F_OK)==0) {
|
1996-01-28 00:31:34 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
if (strcmp(path,".") == 0) {
|
|
|
|
chdir(progdir);
|
1996-01-28 00:31:34 +00:00
|
|
|
} else {
|
1996-01-28 00:41:22 +00:00
|
|
|
chdir(path);
|
1996-01-28 00:31:34 +00:00
|
|
|
}
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/* open the whatis database file */
|
|
|
|
if (!o_flag) dbfile = WHATIS;
|
|
|
|
whatis_fp = fopen(dbfile,"w");
|
1996-01-28 00:31:34 +00:00
|
|
|
if (whatis_fp == NULL) {
|
1996-01-28 00:41:22 +00:00
|
|
|
fprintf (error_fp,
|
|
|
|
"Could not create whatis database file %s in directory %s.\n\
|
1996-01-28 00:33:14 +00:00
|
|
|
Aborted.\n",
|
1996-01-28 00:41:22 +00:00
|
|
|
dbfile,path);
|
|
|
|
exit(-1);
|
|
|
|
}
|
1996-01-28 00:31:34 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/*
|
1996-01-28 00:31:34 +00:00
|
|
|
* loop over the expected man* subdirectories within path
|
|
|
|
*/
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
if (!C_flag) for (i=0; man_subdir[i] != NULL; i++) {
|
|
|
|
subdir = opendir(man_subdir[i]);
|
|
|
|
if (subdir != NULL) {
|
1996-01-28 00:31:34 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/* print status */
|
|
|
|
if (v_flag>=3) fprintf(output_fp,
|
|
|
|
"Now working on directory %s\t%s ...\n",path,man_subdir[i]);
|
1996-01-28 00:31:34 +00:00
|
|
|
|
|
|
|
/* no need to error check because of opendir() */
|
1996-01-28 00:41:22 +00:00
|
|
|
chdir(man_subdir[i]);
|
|
|
|
|
|
|
|
/* loop over files within subdirectory */
|
|
|
|
while ((file = readdir(subdir)) != NULL) {
|
|
|
|
process (file->d_name,tmp_file,whatis_fp,&man_subdir[i][3]);
|
|
|
|
}
|
|
|
|
closedir(subdir);
|
|
|
|
} else {
|
|
|
|
if (v_flag>=3) fprintf(output_fp,
|
|
|
|
"Could not access files in %s\t%s ...\n",path,man_subdir[i]);
|
|
|
|
}
|
|
|
|
chdir(path);
|
|
|
|
}
|
1996-01-28 00:31:34 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/*
|
1996-01-28 00:31:34 +00:00
|
|
|
* loop over the expected cat* subdirectories within path,
|
|
|
|
* adding only those files without man* versions.
|
|
|
|
*/
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
if (c_flag||C_flag) for (i=0; cat_subdir[i] != NULL; i++) {
|
|
|
|
subdir = opendir(cat_subdir[i]);
|
|
|
|
if (subdir != NULL) {
|
1996-01-28 00:31:34 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/* print status */
|
|
|
|
if (v_flag>=3) fprintf(output_fp,
|
|
|
|
"Now working on directory %s\t%s ...\n",path,cat_subdir[i]);
|
1996-01-28 00:31:34 +00:00
|
|
|
|
|
|
|
/* no need to error check because of opendir() */
|
1996-01-28 00:41:22 +00:00
|
|
|
chdir(cat_subdir[i]);
|
1996-01-28 00:31:34 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/* make filebuffer contain path to matching man* subdirectory */
|
1996-01-28 00:31:34 +00:00
|
|
|
strcpy(filebuffer,path);
|
|
|
|
strcat(filebuffer,dirsep);
|
|
|
|
strcat(filebuffer,man_subdir[i]);
|
|
|
|
strcat(filebuffer,dirsep);
|
|
|
|
p = filebuffer + strlen(filebuffer); /* p points to '\0' */
|
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/* loop over files that don't have a man* counterpart */
|
|
|
|
while ((file = readdir(subdir)) != NULL) {
|
|
|
|
strcpy(p,file->d_name);
|
1996-01-28 00:31:34 +00:00
|
|
|
if (access(filebuffer,F_OK)!=0)
|
1996-01-28 00:41:22 +00:00
|
|
|
process (file->d_name,tmp_file,whatis_fp,
|
|
|
|
&man_subdir[i][3]);
|
|
|
|
}
|
|
|
|
closedir(subdir);
|
|
|
|
} else {
|
|
|
|
if (v_flag>=3) fprintf(output_fp,
|
|
|
|
"Could not access files in %s\t%s ...\n",path,cat_subdir[i]);
|
|
|
|
}
|
|
|
|
chdir(path);
|
|
|
|
}
|
1996-01-28 00:31:34 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
/* close the database */
|
|
|
|
fclose(whatis_fp);
|
1996-01-28 00:31:34 +00:00
|
|
|
|
|
|
|
}
|
1996-01-28 00:41:22 +00:00
|
|
|
/* get the next path in manpath */
|
|
|
|
path = strtok (NULL," ");
|
1996-01-28 00:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* clean up and exit */
|
1996-01-28 00:41:22 +00:00
|
|
|
unlink(tmp_file);
|
1996-01-28 00:31:34 +00:00
|
|
|
if (output_fp != stdout) fclose(output_fp);
|
|
|
|
if (error_fp != stderr) fclose(error_fp);
|
1996-01-28 00:33:14 +00:00
|
|
|
|
1996-01-28 00:41:22 +00:00
|
|
|
#ifdef STACK_CHECK
|
|
|
|
fprintf(stderr,"Makewhatis stack usage: %d bytes\n",end_stack_check());
|
1996-01-28 00:33:14 +00:00
|
|
|
#endif
|
|
|
|
|
1996-01-28 00:31:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|