12148Seric # include	"../hdr/defines.h"
2*45561Sbostic # include	"pathnames.h"
32148Seric 
4*45561Sbostic static char Sccsid[] = "@(#)help.c	4.4	11/11/90";
52148Seric 
62148Seric /*
72148Seric 	Program to locate helpful info in an ascii file.
82148Seric 	The program accepts a variable number of arguments.
92148Seric 
102148Seric 	The file to be searched is determined from the argument. If the
112148Seric 	argument does not contain numerics, the search
122148Seric 	will be attempted on '/usr/local/lib/help/cmds', with the search key
132148Seric 	being the whole argument.
142148Seric 	If the argument begins with non-numerics but contains
152148Seric 	numerics (e.g, zz32) the search will be attempted on
162148Seric 	'/usr/local/lib/help/<non-numeric prefix>', (e.g,/usr/lib/help/zz),
172148Seric 	with the search key being <remainder of arg>, (e.g., 32).
182148Seric 	If the argument is all numeric, or if the file as
192148Seric 	determined above does not exist, the search will be attempted on
20*45561Sbostic 	_PATH_OLDHELP, which is the old help file, with the
212148Seric 	search key being the entire argument.
222148Seric 	In no case will more than one search per argument be performed.
232148Seric 
242148Seric 	File is formatted as follows:
252148Seric 
262148Seric 		* comment
272148Seric 		* comment
282148Seric 		-str1
292148Seric 		text
302148Seric 		-str2
312148Seric 		text
322148Seric 		* comment
332148Seric 		text
342148Seric 		-str3
352148Seric 		text
362148Seric 
372148Seric 	The "str?" that matches the key is found and
382148Seric 	the following text lines are printed.
392148Seric 	Comments are ignored.
402148Seric 
412148Seric 	If the argument is omitted, the program requests it.
422148Seric */
432148Seric char	hfile[64];
442148Seric FILE	*iop;
452148Seric char	line [512];
462148Seric 
472148Seric 
main(argc,argv)482148Seric main(argc,argv)
492148Seric int argc;
502148Seric char *argv[];
512148Seric {
522148Seric 	register int i;
532148Seric 	extern int Fcnt;
542148Seric 
552148Seric 	/*
562148Seric 	Tell 'fatal' to issue messages, clean up, and return to its caller.
572148Seric 	*/
582148Seric 	Fflags = FTLMSG | FTLCLN | FTLJMP;
592148Seric 
602148Seric 	if (argc == 1)
612148Seric 		findprt(ask());		/* ask user for argument */
622148Seric 	else
632148Seric 		for (i = 1; i < argc; i++)
642148Seric 			findprt(argv[i]);
652148Seric 
662148Seric 	exit(Fcnt ? 1 : 0);
672148Seric }
682148Seric 
692148Seric 
findprt(p)702148Seric findprt(p)
712148Seric char *p;
722148Seric {
732148Seric 	register char *q;
742148Seric 	char key[50];
752148Seric 
762148Seric 	if (setjmp(Fjmp))		/* set up to return here from */
772148Seric 		return;			/* 'fatal' and return to 'main' */
782148Seric 
792148Seric 	if (size(p) > 50)
802148Seric 		fatal("argument too long (he2)");
812148Seric 
822148Seric 	q = p;
832148Seric 
842148Seric 	while (*q && !numeric(*q))
852148Seric 		q++;
862148Seric 
872148Seric 	if (*q == '\0') {		/* all alphabetics */
882148Seric 		copy(p,key);
89*45561Sbostic 		cat(hfile,_PATH_HELPDIR,"cmds",0);
902148Seric 		if (!exists(hfile))
91*45561Sbostic 			copy(_PATH_OLDHELP,hfile);
922148Seric 	}
932148Seric 	else
942148Seric 		if (q == p) {		/* first char numeric */
952148Seric 			copy(p,key);
96*45561Sbostic 			copy(_PATH_OLDHELP,hfile);
972148Seric 		}
982148Seric 	else {				/* first char alpha, then numeric */
992148Seric 		copy(p,key);		/* key used as temporary */
1002148Seric 		*(key + (q - p)) = '\0';
101*45561Sbostic 		cat(hfile,_PATH_HELPDIR,key,0);
1022148Seric 		copy(q,key);
1032148Seric 		if (!exists(hfile)) {
1042148Seric 			copy(p,key);
105*45561Sbostic 			copy(_PATH_OLDHELP,hfile);
1062148Seric 		}
1072148Seric 	}
1082148Seric 
1092148Seric 	iop = xfopen(hfile,0);
1102148Seric 
1112148Seric 	/*
1122148Seric 	Now read file, looking for key.
1132148Seric 	*/
1142148Seric 	while ((q = fgets(line,512,iop)) != NULL) {
1152148Seric 		repl(line,'\n','\0');		/* replace newline char */
1162148Seric 		if (line[0] == '-' && equal(&line[1],key))
1172148Seric 			break;
1182148Seric 	}
1192148Seric 
1202148Seric 	if (q == NULL) {	/* endfile? */
1212148Seric 		printf("\n");
12233423Sbostic 		sprintf(Error,"%s not found (he1)",p);
12333423Sbostic 		fatal(Error);
1242148Seric 	}
1252148Seric 
1262148Seric 	printf("\n%s:\n",p);
1272148Seric 
1282148Seric 	while (fgets(line,512,iop) != NULL && line[0] == '-')
1292148Seric 		;
1302148Seric 	do {
1312148Seric 		if (line[0] != '*')
1322148Seric 			printf("%s",line);
1332148Seric 	} while (fgets(line,512,iop) != NULL && line[0] != '-');
1342148Seric 
1352148Seric 	fclose(iop);
1362148Seric }
1372148Seric 
1382148Seric 
ask()1392148Seric ask()
1402148Seric {
1412148Seric 	static char resp[51];
1422148Seric 
1432148Seric 	iop = stdin;
1442148Seric 
1452148Seric 	printf("msg number or comd name? ");
1462148Seric 	fgets(resp,51,iop);
1472148Seric 	return(repl(resp,'\n','\0'));
1482148Seric }
1492148Seric 
1502148Seric 
clean_up()1512148Seric clean_up()
1522148Seric {
1532148Seric 	fclose(iop);
1542148Seric }
155