10c84517dSeric # include "../hdr/defines.h"
20c84517dSeric
3*824dd99bSbostic static char Sccsid[] = "@(#)help.c 4.3 02/02/88";
40c84517dSeric
50c84517dSeric /*
60c84517dSeric Program to locate helpful info in an ascii file.
70c84517dSeric The program accepts a variable number of arguments.
80c84517dSeric
90c84517dSeric The file to be searched is determined from the argument. If the
100c84517dSeric argument does not contain numerics, the search
110c84517dSeric will be attempted on '/usr/local/lib/help/cmds', with the search key
120c84517dSeric being the whole argument.
130c84517dSeric If the argument begins with non-numerics but contains
140c84517dSeric numerics (e.g, zz32) the search will be attempted on
150c84517dSeric '/usr/local/lib/help/<non-numeric prefix>', (e.g,/usr/lib/help/zz),
160c84517dSeric with the search key being <remainder of arg>, (e.g., 32).
170c84517dSeric If the argument is all numeric, or if the file as
180c84517dSeric determined above does not exist, the search will be attempted on
190c84517dSeric '/usr/local/lib/sccs.hf', which is the old help file, with the
200c84517dSeric search key being the entire argument.
210c84517dSeric In no case will more than one search per argument be performed.
220c84517dSeric
230c84517dSeric File is formatted as follows:
240c84517dSeric
250c84517dSeric * comment
260c84517dSeric * comment
270c84517dSeric -str1
280c84517dSeric text
290c84517dSeric -str2
300c84517dSeric text
310c84517dSeric * comment
320c84517dSeric text
330c84517dSeric -str3
340c84517dSeric text
350c84517dSeric
360c84517dSeric The "str?" that matches the key is found and
370c84517dSeric the following text lines are printed.
380c84517dSeric Comments are ignored.
390c84517dSeric
400c84517dSeric If the argument is omitted, the program requests it.
410c84517dSeric */
4212139005Slepreau char oldfile[] = "/usr/local/lib/sccs.hf";
4312139005Slepreau char helpdir[] = "/usr/local/lib/help/";
440c84517dSeric char hfile[64];
450c84517dSeric FILE *iop;
460c84517dSeric char line [512];
470c84517dSeric
480c84517dSeric
main(argc,argv)490c84517dSeric main(argc,argv)
500c84517dSeric int argc;
510c84517dSeric char *argv[];
520c84517dSeric {
530c84517dSeric register int i;
540c84517dSeric extern int Fcnt;
550c84517dSeric
560c84517dSeric /*
570c84517dSeric Tell 'fatal' to issue messages, clean up, and return to its caller.
580c84517dSeric */
590c84517dSeric Fflags = FTLMSG | FTLCLN | FTLJMP;
600c84517dSeric
610c84517dSeric if (argc == 1)
620c84517dSeric findprt(ask()); /* ask user for argument */
630c84517dSeric else
640c84517dSeric for (i = 1; i < argc; i++)
650c84517dSeric findprt(argv[i]);
660c84517dSeric
670c84517dSeric exit(Fcnt ? 1 : 0);
680c84517dSeric }
690c84517dSeric
700c84517dSeric
findprt(p)710c84517dSeric findprt(p)
720c84517dSeric char *p;
730c84517dSeric {
740c84517dSeric register char *q;
750c84517dSeric char key[50];
760c84517dSeric
770c84517dSeric if (setjmp(Fjmp)) /* set up to return here from */
780c84517dSeric return; /* 'fatal' and return to 'main' */
790c84517dSeric
800c84517dSeric if (size(p) > 50)
810c84517dSeric fatal("argument too long (he2)");
820c84517dSeric
830c84517dSeric q = p;
840c84517dSeric
850c84517dSeric while (*q && !numeric(*q))
860c84517dSeric q++;
870c84517dSeric
880c84517dSeric if (*q == '\0') { /* all alphabetics */
890c84517dSeric copy(p,key);
900c84517dSeric cat(hfile,helpdir,"cmds",0);
910c84517dSeric if (!exists(hfile))
920c84517dSeric copy(oldfile,hfile);
930c84517dSeric }
940c84517dSeric else
950c84517dSeric if (q == p) { /* first char numeric */
960c84517dSeric copy(p,key);
970c84517dSeric copy(oldfile,hfile);
980c84517dSeric }
990c84517dSeric else { /* first char alpha, then numeric */
1000c84517dSeric copy(p,key); /* key used as temporary */
1010c84517dSeric *(key + (q - p)) = '\0';
1020c84517dSeric cat(hfile,helpdir,key,0);
1030c84517dSeric copy(q,key);
1040c84517dSeric if (!exists(hfile)) {
1050c84517dSeric copy(p,key);
1060c84517dSeric copy(oldfile,hfile);
1070c84517dSeric }
1080c84517dSeric }
1090c84517dSeric
1100c84517dSeric iop = xfopen(hfile,0);
1110c84517dSeric
1120c84517dSeric /*
1130c84517dSeric Now read file, looking for key.
1140c84517dSeric */
1150c84517dSeric while ((q = fgets(line,512,iop)) != NULL) {
1160c84517dSeric repl(line,'\n','\0'); /* replace newline char */
1170c84517dSeric if (line[0] == '-' && equal(&line[1],key))
1180c84517dSeric break;
1190c84517dSeric }
1200c84517dSeric
1210c84517dSeric if (q == NULL) { /* endfile? */
1220c84517dSeric printf("\n");
123*824dd99bSbostic sprintf(Error,"%s not found (he1)",p);
124*824dd99bSbostic fatal(Error);
1250c84517dSeric }
1260c84517dSeric
1270c84517dSeric printf("\n%s:\n",p);
1280c84517dSeric
1290c84517dSeric while (fgets(line,512,iop) != NULL && line[0] == '-')
1300c84517dSeric ;
1310c84517dSeric do {
1320c84517dSeric if (line[0] != '*')
1330c84517dSeric printf("%s",line);
1340c84517dSeric } while (fgets(line,512,iop) != NULL && line[0] != '-');
1350c84517dSeric
1360c84517dSeric fclose(iop);
1370c84517dSeric }
1380c84517dSeric
1390c84517dSeric
ask()1400c84517dSeric ask()
1410c84517dSeric {
1420c84517dSeric static char resp[51];
1430c84517dSeric
1440c84517dSeric iop = stdin;
1450c84517dSeric
1460c84517dSeric printf("msg number or comd name? ");
1470c84517dSeric fgets(resp,51,iop);
1480c84517dSeric return(repl(resp,'\n','\0'));
1490c84517dSeric }
1500c84517dSeric
1510c84517dSeric
clean_up()1520c84517dSeric clean_up()
1530c84517dSeric {
1540c84517dSeric fclose(iop);
1550c84517dSeric }
156