1 # include	"../hdr/defines.h"
2 # include	"pathnames.h"
3 
4 static char Sccsid[] = "@(#)help.c	4.4	11/11/90";
5 
6 /*
7 	Program to locate helpful info in an ascii file.
8 	The program accepts a variable number of arguments.
9 
10 	The file to be searched is determined from the argument. If the
11 	argument does not contain numerics, the search
12 	will be attempted on '/usr/local/lib/help/cmds', with the search key
13 	being the whole argument.
14 	If the argument begins with non-numerics but contains
15 	numerics (e.g, zz32) the search will be attempted on
16 	'/usr/local/lib/help/<non-numeric prefix>', (e.g,/usr/lib/help/zz),
17 	with the search key being <remainder of arg>, (e.g., 32).
18 	If the argument is all numeric, or if the file as
19 	determined above does not exist, the search will be attempted on
20 	_PATH_OLDHELP, which is the old help file, with the
21 	search key being the entire argument.
22 	In no case will more than one search per argument be performed.
23 
24 	File is formatted as follows:
25 
26 		* comment
27 		* comment
28 		-str1
29 		text
30 		-str2
31 		text
32 		* comment
33 		text
34 		-str3
35 		text
36 
37 	The "str?" that matches the key is found and
38 	the following text lines are printed.
39 	Comments are ignored.
40 
41 	If the argument is omitted, the program requests it.
42 */
43 char	hfile[64];
44 FILE	*iop;
45 char	line [512];
46 
47 
48 main(argc,argv)
49 int argc;
50 char *argv[];
51 {
52 	register int i;
53 	extern int Fcnt;
54 
55 	/*
56 	Tell 'fatal' to issue messages, clean up, and return to its caller.
57 	*/
58 	Fflags = FTLMSG | FTLCLN | FTLJMP;
59 
60 	if (argc == 1)
61 		findprt(ask());		/* ask user for argument */
62 	else
63 		for (i = 1; i < argc; i++)
64 			findprt(argv[i]);
65 
66 	exit(Fcnt ? 1 : 0);
67 }
68 
69 
70 findprt(p)
71 char *p;
72 {
73 	register char *q;
74 	char key[50];
75 
76 	if (setjmp(Fjmp))		/* set up to return here from */
77 		return;			/* 'fatal' and return to 'main' */
78 
79 	if (size(p) > 50)
80 		fatal("argument too long (he2)");
81 
82 	q = p;
83 
84 	while (*q && !numeric(*q))
85 		q++;
86 
87 	if (*q == '\0') {		/* all alphabetics */
88 		copy(p,key);
89 		cat(hfile,_PATH_HELPDIR,"cmds",0);
90 		if (!exists(hfile))
91 			copy(_PATH_OLDHELP,hfile);
92 	}
93 	else
94 		if (q == p) {		/* first char numeric */
95 			copy(p,key);
96 			copy(_PATH_OLDHELP,hfile);
97 		}
98 	else {				/* first char alpha, then numeric */
99 		copy(p,key);		/* key used as temporary */
100 		*(key + (q - p)) = '\0';
101 		cat(hfile,_PATH_HELPDIR,key,0);
102 		copy(q,key);
103 		if (!exists(hfile)) {
104 			copy(p,key);
105 			copy(_PATH_OLDHELP,hfile);
106 		}
107 	}
108 
109 	iop = xfopen(hfile,0);
110 
111 	/*
112 	Now read file, looking for key.
113 	*/
114 	while ((q = fgets(line,512,iop)) != NULL) {
115 		repl(line,'\n','\0');		/* replace newline char */
116 		if (line[0] == '-' && equal(&line[1],key))
117 			break;
118 	}
119 
120 	if (q == NULL) {	/* endfile? */
121 		printf("\n");
122 		sprintf(Error,"%s not found (he1)",p);
123 		fatal(Error);
124 	}
125 
126 	printf("\n%s:\n",p);
127 
128 	while (fgets(line,512,iop) != NULL && line[0] == '-')
129 		;
130 	do {
131 		if (line[0] != '*')
132 			printf("%s",line);
133 	} while (fgets(line,512,iop) != NULL && line[0] != '-');
134 
135 	fclose(iop);
136 }
137 
138 
139 ask()
140 {
141 	static char resp[51];
142 
143 	iop = stdin;
144 
145 	printf("msg number or comd name? ");
146 	fgets(resp,51,iop);
147 	return(repl(resp,'\n','\0'));
148 }
149 
150 
151 clean_up()
152 {
153 	fclose(iop);
154 }
155