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