1 /*-
2  * Copyright (c) 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)getlesson.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "stdio.h"
13 #include "lrnref.h"
14 #include "pathnames.h"
15 
16 char *
17 getlesson()
18 {
19 	register char *p;
20 	char ans[80], line[200];
21 	int isnum, found, fd[2];
22 	FILE *fp;
23 
24 	sprintf(ans, "%s/%s/L%s", direct, sname, level);
25 	if (access(ans, 04) == 0)		/* there is a file */
26 		return(level);
27 	isnum = 1;
28 	for (p=level; *p; p++)		/* accept:  (digit|dot)*anychar  */
29 		if (*p != '.' && (*p < '0' || *p > '9') && *(p+1) != '\0')
30 			isnum = 0;
31 	if (isnum) {
32 		strcpy(line, level);
33 		p = level;
34 		while (*p != '.' && *p >= '0' && *p <= '9')
35 			p++;
36 		*p = '\0';
37 		strcat(level, ".1a");
38 		sprintf(ans, "%s/%s/L%s", direct, sname, level);
39 		if (access(ans, 04) == 0) {	/* there is a file */
40 			printf("There is no lesson %s; trying lesson %s instead.\n\n", line, level);
41 			return(level);
42 		}
43 		printf("There is no lesson %s.\n", line);
44 		return(0);
45 	}
46 	/* fgrep through lessons for one containing the string in 'level' */
47 	pipe(fd);
48 	if (fork() == 0) {
49 		close(fd[0]);
50 		dup2(fd[1], 1);
51 		sprintf(ans,"cd %s/%s ; fgrep '%s' L?.* L??.* L???.*", direct, sname, level);
52 		execl(_PATH_CSHELL, "csh", "-cf", ans, 0);
53 		perror(_PATH_CSHELL);
54 		fprintf(stderr, "Getlesson:  can't do %s\n", ans);
55 	}
56 	close(fd[1]);
57 	fp = fdopen(fd[0], "r");
58 	found = 0;
59 	while (fgets(line, 200, fp) != NULL) {
60 		for (p=line; *p != ':'; p++) ;
61 		p++;
62 		if (*p == '#')
63 			continue;
64 		else {
65 			found = 1;
66 			break;
67 		}
68 	}
69 	/*fclose(fp);*/
70 	if (found) {
71 		*--p = '\0';
72 		strcpy(level, &line[1]);
73 		sprintf(ans, "%s/%s/L%s", direct, sname, level);
74 		if (access(ans, 04) == 0) {	/* there is a file */
75 			printf("Trying lesson %s.\n\n", level);
76 			return(level);
77 		}
78 	}
79 	printf("There is no lesson containing \"%s\".\n", level);
80 	return(0);
81 }
82