xref: /original-bsd/usr.bin/learn/learn/selsub.c (revision cde01d6c)
1 /*-
2  * Copyright (c) 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)selsub.c	4.8 (Berkeley) 04/17/91";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <sys/file.h>
14 #include <sys/stat.h>
15 #include <stdio.h>
16 #include "lrnref.h"
17 #include "pathnames.h"
18 
19 char learnrc[100];
20 
21 selsub(argc,argv)
22 char *argv[];
23 {
24 	char ans1[100];
25 	static char ans2[40];
26 	static char dirname[40];
27 	static char subname[40];
28 	FILE *fp;
29 	char *getenv();
30 	char *home;
31 
32 	if (argc > 1 && argv[1][0] == '-') {
33 		direct = argv[1]+1;
34 		argc--;
35 		argv++;
36 	}
37 	if (chdir(direct) != 0) {
38 		perror(direct);
39 		fprintf(stderr, "Selsub:  couldn't cd to non-standard directory\n");
40 		exit(1);
41 	}
42 	sname = argc > 1 ? argv[1] : 0;
43 	if (argc > 2) {
44 		strcpy (level=ans2, argv[2]);
45 		if (strcmp(level, "-") == 0)	/* no lesson name is - */
46 			ask = 1;
47 		else if (strcmp(level, "0") == 0)
48 			level = 0;
49 		else
50 			again = 1;	/* treat as if "again" lesson */
51 	}
52 	else
53 		level = 0;
54 	if (argc > 3 )
55 		speed = atoi(argv[3]);
56 	if ((home = getenv("HOME")) != NULL) {
57 		sprintf(learnrc, "%s/.learnrc", home);
58 		if ((fp=fopen(learnrc, "r")) != NULL) {
59 			char xsub[40], xlev[40]; int xsp;
60 			fscanf(fp, "%s %s %d", xsub, xlev, &xsp);
61 			fclose(fp);
62 			if (*xsub && *xlev && xsp >= 0	/* all read OK */
63 			    && (argc == 2 && strcmp(sname, xsub) == 0
64 			      || argc <= 1)) {
65 				strcpy(sname = subname, xsub);
66 				strcpy(level = ans2, xlev);
67 				speed = xsp;
68 				again = 1;
69 	printf("[ Taking up where you left off last time:  learn %s %s.\n",
70 		sname, level);
71 	printf("%s\n  \"rm $HOME/.learnrc\", and re-enter with \"learn %s\". ]\n",
72 		"  To start this sequence over leave learn by typing \"bye\", then",
73 		sname);
74 			}
75 		}
76 	}
77 	if (!sname) {
78 		printf("These are the available courses -\n");
79 		list("Linfo");
80 		printf("If you want more information about the courses,\n");
81 		printf("or if you have never used 'learn' before,\n");
82 		printf("press RETURN; otherwise type the name of\n");
83 		printf("the course you want, followed by RETURN.\n");
84 		fflush(stdout);
85 		if (gets(sname=subname) == NULL)
86 			exit(0);
87 		if (sname[0] == '\0') {
88 			list("Xinfo");
89 			do {
90 				printf("\nWhich subject?  ");
91 				fflush(stdout);
92 				if (gets(sname=subname) == NULL)
93 					exit(0);
94 			} while (sname[0] == '\0');
95 		}
96 	}
97 	chknam(sname);
98 	total = cntlessons(sname);
99 	if (!level) {
100 		printf("If you were in the middle of this subject\n");
101 		printf("and want to start where you left off, type\n");
102 		printf("the last lesson number the computer printed.\n");
103 		printf("If you don't know the number, type in a word\n");
104 		printf("you think might appear in the lesson you want,\n");
105 		printf("and I will look for the first lesson containing it.\n");
106 		printf("To start at the beginning, just hit RETURN.\n");
107 		fflush(stdout);
108 		if (gets(ans2) == NULL)
109 			exit(0);
110 		if (ans2[0]==0)
111 			strcpy(ans2,"0");
112 		else
113 			again = 1;
114 		level=ans2;
115 		getlesson();
116 	}
117 
118 	/* make new directory for user to play in */
119 	if (chdir(_PATH_TMP) != 0) {
120 		perror(_PATH_TMP);
121 		fprintf(stderr, "Selsub:  couldn't cd to public directory\n");
122 		exit(1);
123 	}
124 	sprintf(dir=dirname, "pl%da", getpid());
125 	sprintf(ans1, "mkdir %s", dir);
126 	system(ans1);
127 	if (chdir(dir) < 0) {
128 		perror(dir);
129 		fprintf(stderr, "Selsub:  couldn't make play directory with %s.\nBye.\n", ans1);
130 		exit(1);
131 	}
132 	/* after this point, we have a working directory. */
133 	/* have to call wrapup to clean up */
134 	(void)sprintf(ans1, "%s/%s/Init", direct, sname);
135 	if (access(ans1, R_OK)==0) {
136 		(void)sprintf(ans1, "%s/%s/Init %s", direct, sname, level);
137 		if (system(ans1) != 0) {
138 			printf("Leaving learn.\n");
139 			wrapup(1);
140 		}
141 	}
142 }
143 
144 chknam(name)
145 char *name;
146 {
147 	if (access(name, 05) < 0) {
148 		printf("Sorry, there is no subject or lesson named %s.\nBye.\n", name);
149 		exit(1);
150 	}
151 }
152 
153 #include <dirent.h>
154 
155 cntlessons(sname)	/* return number of entries in lesson directory; */
156 char *sname;		/* approximate at best since I don't count L0, Init */
157 {			/* and lessons skipped by good students */
158 	struct dirent dbuf;
159 	register struct dirent *ep = &dbuf;	/* directory entry pointer */
160 	int n = 0;
161 	DIR *dp;
162 
163 	if ((dp = opendir(sname)) == NULL) {
164 		perror(sname);
165 		wrapup(1);
166 	}
167 	for (ep = readdir(dp); ep != NULL; ep = readdir(dp)) {
168 		if (ep->d_ino != 0)
169 			n++;
170 	}
171 	closedir(dp);
172 	return n - 2;				/* minus . and .. */
173 }
174