]>
Commit | Line | Data |
---|---|---|
989a2c2c | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Locker$ | |
5 | * $Header$ | |
7189310c | 6 | * |
7 | * (c) Copyright 1988 by the Massachusetts Institute of Technology. | |
8 | * For copying and distribution information, please see the file | |
9 | * <mit-copyright.h>. | |
989a2c2c | 10 | */ |
11 | ||
12 | #ifndef lint | |
13 | static char *rcsid_display_c = "$Header$"; | |
14 | #endif lint | |
15 | ||
7189310c | 16 | #include <mit-copyright.h> |
989a2c2c | 17 | #include <stdio.h> |
18 | #include <curses.h> | |
19 | #include <sys/time.h> | |
20 | #include "userreg.h" | |
21 | ||
22 | #define DESC_WIDTH 18 | |
4127ae1e | 23 | #define HEADER "*** Project Athena User Registration ***" |
759f815b | 24 | #if defined(vax) || defined(mips) |
94464a85 | 25 | #define HELP " Press the key marked <X| to delete a character. Press Ctrl-C to start over." |
26 | #endif | |
759f815b | 27 | #ifndef HELP |
28 | #define HELP " Press backspace to delete a character. Press Ctrl-C to start over." | |
94464a85 | 29 | #endif |
989a2c2c | 30 | #define BORDER_CHAR '-' |
31 | #define MIN_COLS 80 | |
32 | #define MIN_LINES 24 | |
33 | ||
34 | WINDOW * displayw, *queryw; | |
35 | WINDOW * dataw, *helpw; | |
36 | WINDOW * fnamew, *midw, *lnamew, *idw, *loginw; | |
37 | ||
38 | /* Set up the windows and subwindows on the display */ | |
39 | setup_display () { | |
40 | FILE * freopen (); | |
41 | ||
42 | initscr (); /* Start up curses */ | |
989a2c2c | 43 | |
44 | if (COLS < MIN_COLS || LINES < MIN_LINES) { | |
45 | fprintf (stderr, "Screen must be at least %d x %d\n", MIN_LINES, MIN_COLS); | |
46 | exit (1); | |
47 | } | |
48 | ||
94464a85 | 49 | noecho (); /* And the tty input */ |
50 | raw (); | |
51 | freopen ("/dev/null", "w", stderr);/* Toss the standard error output */ | |
52 | ||
989a2c2c | 53 | /* Make sure the place is clean */ |
54 | clear (); | |
55 | ||
56 | /* Set up the top-level windows */ | |
57 | /* First line is the header */ | |
58 | displayw = subwin (stdscr, 12, 0, 2, 0);/* Lines 2-13 */ | |
59 | scrollok (displayw, TRUE); | |
60 | ||
61 | queryw = subwin (stdscr, 1, 0, 15, 0);/* Line 15 */ | |
62 | scrollok (queryw, TRUE); | |
63 | ||
64 | dataw = subwin (stdscr, 5, 0, 17, 0);/* Lines 17-21 */ | |
65 | ||
66 | /* Set up the data windows */ | |
67 | fnamew = subwin (stdscr, 1, 0, 17, DESC_WIDTH); | |
68 | midw = subwin (stdscr, 1, 0, 18, DESC_WIDTH); | |
69 | lnamew = subwin (stdscr, 1, 0, 19, DESC_WIDTH); | |
70 | idw = subwin (stdscr, 1, 0, 20, DESC_WIDTH); | |
71 | loginw = subwin (stdscr, 1, 0, 21, DESC_WIDTH); | |
72 | ||
73 | } | |
74 | ||
75 | /* Clear and restore the display */ | |
76 | reset_display () { | |
77 | clear (); | |
78 | ||
79 | /* Put back the borders */ | |
80 | make_border (1); | |
81 | make_border (14); | |
82 | make_border (16); | |
83 | make_border (22); | |
84 | ||
85 | ||
86 | /* Put in the window dressing */ | |
87 | wmove (dataw, 0, 0); | |
88 | waddstr (dataw, "First Name:\n"); | |
89 | waddstr (dataw, "Middle Initial:\n"); | |
4127ae1e | 90 | waddstr (dataw, "Family Name:\n"); |
989a2c2c | 91 | waddstr (dataw, "MIT ID #:\n\n"); |
92 | waddstr (dataw, "Username:\n"); | |
93 | wclrtoeol (dataw); | |
94 | ||
95 | /* Set up the header */ | |
96 | mvaddstr (0, (COLS - strlen (HEADER)) / 2, HEADER); | |
97 | mvaddstr (23, 0, HELP); | |
98 | ||
99 | /* Put it all up */ | |
100 | refresh (); | |
101 | } | |
102 | ||
103 | ||
104 | /* Make a one-line border on line l of stdscr */ | |
105 | make_border (l) | |
106 | int l; | |
107 | { | |
108 | int i; | |
109 | ||
110 | move (l, 0); | |
111 | for (i = 0; i < COLS - 1; i++) { | |
112 | addch (BORDER_CHAR); | |
113 | } | |
114 | } | |
115 | ||
116 | /* This replaces several "useful" display functions in the old userreg */ | |
117 | redisp () { | |
118 | mvwprintw (fnamew, 0, 0, "%-24s", user.u_first); | |
119 | mvwprintw (midw, 0, 0, "%-24s", user.u_mid_init); | |
120 | mvwprintw (lnamew, 0, 0, "%-24s", user.u_last); | |
121 | mvwprintw (idw, 0, 0, "%-24s", typed_mit_id); | |
122 | mvwprintw (loginw, 0, 0, "%-24s", user.u_login); | |
123 | ||
124 | wrefresh (dataw); | |
125 | } | |
126 | ||
127 | ||
128 | /* Input and input_no_echo exist only to save on retyping */ | |
4127ae1e | 129 | input (prompt, buf, maxsize, timeout, emptyok) |
989a2c2c | 130 | char *prompt; |
131 | char *buf; | |
4127ae1e | 132 | int maxsize, timeout, emptyok; |
989a2c2c | 133 | { |
4127ae1e | 134 | query_user (prompt, buf, maxsize, timeout, TRUE, emptyok, TRUE); |
989a2c2c | 135 | } |
136 | ||
137 | input_no_echo (prompt, buf, maxsize, timeout) | |
138 | char *prompt; | |
139 | char *buf; | |
140 | int maxsize, timeout; | |
141 | { | |
4127ae1e | 142 | query_user (prompt, buf, maxsize, timeout, FALSE, FALSE, TRUE); |
989a2c2c | 143 | } |
144 | ||
145 | ||
4127ae1e | 146 | /* make the user press any key to continue */ |
147 | wait_for_user () | |
148 | { | |
149 | char buf[BUFSIZ]; | |
150 | ||
151 | redisp(); | |
152 | query_user ("Press RETURN or ENTER to continue", buf, 1, | |
153 | 15 * 60, FALSE, TRUE, FALSE); | |
154 | } | |
155 | ||
989a2c2c | 156 | |
157 | /* Gets input through the query buffer */ | |
158 | /* Exit(1)'s on read errors */ | |
159 | /* Signals SIGALRM after 'timeout' seconds */ | |
4127ae1e | 160 | query_user (prompt, buf, maxsize, timeout, echop, emptyok, valuep) |
989a2c2c | 161 | char *prompt; |
162 | char *buf; | |
163 | int maxsize, timeout; | |
4127ae1e | 164 | bool echop, emptyok, valuep; |
989a2c2c | 165 | { |
166 | char c; | |
167 | int i; | |
168 | struct itimerval it; | |
169 | ||
170 | retry: | |
171 | /* Set up interval timer */ | |
172 | it.it_interval.tv_sec = 0; | |
173 | it.it_interval.tv_usec = 0; | |
174 | it.it_value.tv_sec = timeout; | |
175 | it.it_value.tv_usec = 0; | |
176 | setitimer (ITIMER_REAL, &it, (struct itimerval *) 0); | |
177 | ||
178 | /* Erase the query window and put up a prompt */ | |
179 | werase (queryw); | |
180 | mvwaddstr (queryw, 0, 0, prompt); | |
181 | waddch (queryw, ' '); /* Put in a space, as Blox does */ | |
182 | wrefresh (queryw); | |
183 | ||
184 | i = 0; | |
185 | while ((c = getchar ()) != '\r') { | |
186 | switch (c) { | |
187 | case '\025': /* Ctl-U */ | |
188 | goto retry; | |
189 | break; | |
190 | case EOF: | |
191 | /* We're in raw mode, so EOF means disaster */ | |
192 | exit(1); | |
193 | break; | |
194 | case '\177': /* Delete */ | |
195 | case '\010': /* Backspace */ | |
196 | if (i) { | |
197 | i--; | |
198 | if (echop) { | |
199 | wmove (queryw, queryw -> _cury, queryw -> _curx - 1); | |
200 | wclrtoeol (queryw); | |
201 | wrefresh (queryw); | |
202 | } | |
203 | } | |
204 | break; | |
205 | case '\003': /* Ctrl-C */ | |
206 | if (getuid() != 0) | |
207 | { clear(); /* Exit if not root. */ | |
208 | restore_display(); | |
209 | exit(0); | |
210 | } | |
211 | else | |
212 | restart(); | |
213 | break; | |
214 | default: | |
215 | if (c >= ' ') { /* Ignore all other control chars */ | |
216 | buf[i++] = c; | |
217 | if (echop) { | |
218 | waddch (queryw, c); | |
219 | wrefresh (queryw); | |
220 | } | |
221 | } | |
222 | break; | |
223 | } | |
4127ae1e | 224 | if (valuep && i >= maxsize) { |
989a2c2c | 225 | wprintw (displayw, |
226 | "You are not allowed to type more than %d characters for this answer.\n", | |
227 | maxsize); | |
228 | wrefresh (displayw); | |
229 | goto retry; | |
230 | } | |
231 | } | |
232 | ||
4127ae1e | 233 | if (i == 0) { |
234 | if (emptyok && valuep && | |
235 | (askyn("Do you really want this field left blank (y/n)? ") == NO)) | |
236 | goto retry; | |
237 | if (!emptyok) { | |
238 | wprintw(displayw, "You must enter something here.\n"); | |
239 | wrefresh(displayw); | |
240 | goto retry; | |
241 | } | |
242 | } | |
989a2c2c | 243 | |
244 | ||
245 | /* Input is complete so disable interval timer. */ | |
246 | it.it_interval.tv_sec = 0; | |
247 | it.it_interval.tv_usec = 0; | |
248 | it.it_value.tv_sec = 0; | |
249 | it.it_value.tv_usec = 0; | |
250 | setitimer (ITIMER_REAL, &it, (struct itimerval *) 0); | |
251 | ||
252 | buf[i] = '\0'; /* Put a null on the end */ | |
253 | ||
254 | werase (queryw); /* Clean up the query window */ | |
255 | wrefresh (queryw); | |
256 | ||
257 | return; /* And get out of here. */ | |
258 | } | |
259 | ||
260 | int | |
261 | askyn(prompt) | |
262 | char *prompt; | |
263 | { | |
264 | int ypos, xpos; | |
265 | int answer; | |
266 | struct itimerval it; | |
267 | char c; | |
268 | ||
269 | start: | |
270 | werase (queryw); | |
271 | mvwaddstr (queryw, 0, 0, prompt); | |
272 | wrefresh (queryw); | |
273 | ||
274 | xpos = queryw->_curx; | |
275 | ypos = queryw->_cury; | |
276 | answer = 2; /* No answer. */ | |
277 | ||
278 | /* Reset interval timer for y/n question. */ | |
279 | it.it_interval.tv_sec = 0; | |
280 | it.it_interval.tv_usec = 0; | |
281 | it.it_value.tv_sec = YN_TIMEOUT; | |
282 | it.it_value.tv_usec = 0; | |
283 | setitimer (ITIMER_REAL, &it, (struct itimerval *) 0); | |
284 | ||
285 | while ((c = getchar ()) != '\r') { /* Wait for CR. */ | |
286 | switch (c) { | |
287 | case 'n': /* No. */ | |
288 | case 'N': | |
289 | wmove(queryw, ypos, xpos); | |
290 | wclrtoeol(queryw); | |
291 | waddstr(queryw, "no"); | |
292 | wrefresh(queryw); | |
293 | answer = NO; | |
294 | continue; | |
295 | case 'y': /* Yes. */ | |
296 | case 'Y': | |
297 | wmove(queryw, ypos, xpos); | |
298 | wclrtoeol(queryw); | |
299 | waddstr(queryw, "yes"); | |
300 | wrefresh(queryw); | |
301 | answer = YES; | |
302 | continue; | |
303 | case '\177': /* Delete */ | |
304 | case '\010': /* Backspace */ | |
305 | case '\025': /* Ctl-U */ | |
306 | wmove(queryw, ypos, xpos); | |
307 | wclrtoeol(queryw); | |
308 | wrefresh(queryw); | |
309 | answer = 2; /* No answer. */ | |
310 | break; | |
311 | case EOF: | |
312 | /* We're in raw mode, so EOF means disaster */ | |
313 | exit(1); | |
314 | break; | |
315 | case '\003': /* Ctrl-C */ | |
316 | /* if (getuid() != 0) */ | |
317 | { clear(); /* Exit if not root. */ | |
318 | restore_display(); | |
319 | exit(0); | |
320 | } | |
321 | break; | |
322 | default: /* Ignore everything else. */ | |
323 | break; | |
324 | } | |
325 | } | |
326 | ||
327 | if (answer == 2) /* No answer. */ | |
328 | { display_text_line(0); | |
329 | display_text_line("Please answer y or n."); | |
330 | goto start; | |
331 | } | |
332 | ||
333 | return(answer); | |
334 | } | |
335 | ||
336 | /* Display_text_line puts up a line of text in the display window */ | |
337 | /* Special case: if line is 0, clear the display area */ | |
338 | display_text_line (line) | |
339 | char *line; | |
340 | { | |
341 | if (line) { | |
342 | waddstr (displayw, line); | |
343 | waddch (displayw, '\n'); | |
344 | wrefresh (displayw); | |
345 | } | |
346 | else { | |
347 | werase (displayw); | |
348 | } | |
349 | wrefresh (displayw); | |
350 | } | |
351 | ||
352 | /* Display_text displays a canned message from a file */ | |
353 | display_text (filename) | |
354 | char *filename; | |
355 | { | |
356 | FILE * fp; | |
357 | char buf[100]; | |
358 | ||
359 | werase (displayw); | |
360 | if ((fp = fopen (filename, "r")) == NULL) { | |
361 | wprintw (displayw, "Can't open file %s for reading.\n", filename); | |
362 | return; | |
363 | } | |
364 | ||
365 | while (fgets (buf, 100, fp)) { | |
366 | /* get rid of the newline */ | |
367 | buf[strlen (buf) - 1] = 0; | |
368 | display_text_line (buf); | |
369 | } | |
370 | ||
371 | fclose (fp); | |
372 | } | |
373 | ||
374 | /* Clear_display wipes the display and turns off curses */ | |
375 | restore_display() | |
376 | { | |
377 | clear(); | |
378 | refresh(); | |
379 | noraw(); | |
380 | echo(); | |
381 | endwin(); | |
382 | } | |
383 | ||
384 | timer_on() | |
385 | { | |
386 | struct itimerval it; | |
387 | ||
388 | it.it_interval.tv_sec = 0; | |
389 | it.it_interval.tv_usec = 0; | |
390 | it.it_value.tv_sec = TIMER_TIMEOUT; | |
391 | it.it_value.tv_usec = 0; | |
392 | setitimer (ITIMER_REAL, &it, (struct itimerval *) 0); | |
393 | } | |
394 | ||
395 | timer_off() | |
396 | { | |
397 | struct itimerval it; | |
398 | ||
399 | it.it_interval.tv_sec = 0; | |
400 | it.it_interval.tv_usec = 0; | |
401 | it.it_value.tv_sec = 0; | |
402 | it.it_value.tv_usec = 0; | |
403 | setitimer (ITIMER_REAL, &it, (struct itimerval *) 0); | |
404 | } | |
405 | ||
4127ae1e | 406 | |
407 | wfeep() | |
408 | { | |
409 | char buf = '\007'; | |
410 | write(1, &buf, 1); | |
411 | } |