]> andersk Git - moira.git/blob - clients/userreg/display.c
space before destdir
[moira.git] / clients / userreg / display.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Locker$
5  *      $Header$
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>.
10  */
11
12 #ifndef lint
13 static char *rcsid_display_c = "$Header$";
14 #endif  lint
15
16 #include <mit-copyright.h>
17 #include <stdio.h>
18 #include <curses.h>
19 #include <sys/time.h>
20 #include "userreg.h"
21
22 #define DESC_WIDTH 18
23 #define HEADER "*** Project Athena User Registration ***"
24 #if defined(vax) || defined(mips)
25 #define HELP   " Press the key above RETURN to delete a character.  Press Ctrl-C to start over."
26 #endif
27 #ifndef HELP
28 #define HELP   " Press backspace to delete a character.  Press Ctrl-C to start over."
29 #endif
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 */
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
49   noecho ();                    /* And the tty input */
50   raw ();
51   freopen ("/dev/null", "w", stderr);/* Toss the standard error output */
52
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");
90   waddstr (dataw, "Family Name:\n");
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 */
129 input (prompt, buf, maxsize, timeout, emptyok)
130 char *prompt;
131 char *buf;
132 int   maxsize, timeout, emptyok;
133 {
134   query_user (prompt, buf, maxsize, timeout, TRUE, emptyok, TRUE);
135 }
136
137 input_no_echo (prompt, buf, maxsize, timeout)
138 char *prompt;
139 char *buf;
140 int   maxsize, timeout;
141 {
142   query_user (prompt, buf, maxsize, timeout, FALSE, FALSE, TRUE);
143 }
144
145
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
156
157 /* Gets input through the query buffer */
158 /* Exit(1)'s on read errors */
159 /* Signals SIGALRM after 'timeout' seconds */
160 query_user (prompt, buf, maxsize, timeout, echop, emptyok, valuep)
161 char *prompt;
162 char *buf;
163 int   maxsize, timeout;
164 bool echop, emptyok, valuep;
165 {
166   int  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       case EOF:
190         /* We're in raw mode, so EOF means disaster */
191         exit(1);
192         break;
193     delchar:
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         clear();
207         restore_display();
208         exit(0);
209         break;
210       default: 
211         if (c >= ' ') {         /* Ignore all other control chars */
212           buf[i++] = c;
213           if (echop) {
214             waddch (queryw, c);
215             wrefresh (queryw);
216           }
217         }
218         break;
219     }
220     if (valuep && i >= maxsize) {
221       wfeep();
222       wprintw (displayw,
223   "You are not allowed to type more than %d characters for this answer.\n",
224           maxsize-1);
225       wrefresh (displayw);
226       goto delchar;
227     }
228  }
229
230   if (i == 0) {
231       if (emptyok && valuep &&
232           (askyn("Do you really want this field left blank (y/n)? ") == NO))
233         goto retry;
234       if (!emptyok) {
235           wprintw(displayw, "You must enter something here.\n");
236           wrefresh(displayw);
237           goto retry;
238       }
239   }
240     
241
242   /* Input is complete so disable interval timer. */
243   it.it_interval.tv_sec = 0;
244   it.it_interval.tv_usec = 0;
245   it.it_value.tv_sec = 0;
246   it.it_value.tv_usec = 0;
247   setitimer (ITIMER_REAL, &it, (struct itimerval *) 0);
248
249   buf[i] = '\0';                /* Put a null on the end */
250
251   werase (queryw);              /* Clean up the query window */
252   wrefresh (queryw);
253
254   return;                       /* And get out of here. */
255 }
256
257 int
258 askyn(prompt)
259      char *prompt;
260 {
261   int ypos, xpos;
262   int answer;
263   struct itimerval it;
264   int c;
265
266  start:
267   werase (queryw);
268   mvwaddstr (queryw, 0, 0, prompt);
269   wrefresh (queryw);
270
271   xpos = queryw->_curx;
272   ypos = queryw->_cury;
273   answer = 2;                   /* No answer. */
274   
275   /* Reset interval timer for y/n question. */
276   it.it_interval.tv_sec = 0;
277   it.it_interval.tv_usec = 0;
278   it.it_value.tv_sec = YN_TIMEOUT;
279   it.it_value.tv_usec = 0;
280   setitimer (ITIMER_REAL, &it, (struct itimerval *) 0);
281     
282   while ((c = getchar ()) != '\r') {    /* Wait for CR. */
283       switch (c) {
284       case 'n':                 /* No. */
285       case 'N':
286         wmove(queryw, ypos, xpos);
287         wclrtoeol(queryw);
288         waddstr(queryw, "no");
289         wrefresh(queryw);
290         answer = NO;
291         continue;
292       case 'y':                 /* Yes. */
293       case 'Y':
294         wmove(queryw, ypos, xpos);
295         wclrtoeol(queryw);
296         waddstr(queryw, "yes");
297         wrefresh(queryw);
298         answer = YES;
299         continue;
300       case '\177':              /* Delete */
301       case '\010':              /* Backspace */
302       case '\025':              /* Ctl-U */
303         wmove(queryw, ypos, xpos);
304         wclrtoeol(queryw);
305         wrefresh(queryw);
306         answer = 2;             /* No answer. */
307         break;
308       case EOF:
309         /* We're in raw mode, so EOF means disaster */
310         exit(1);
311         break;
312       case '\003':              /* Ctrl-C */
313         clear();
314         restore_display();
315         exit(0);
316         break;
317       default:                  /* Ignore everything else. */
318         break;
319       }
320     }
321
322   if (answer == 2)              /* No answer. */
323     { display_text_line(0);
324       display_text_line("Please answer y or n.");
325       goto start;
326     }
327   
328   return(answer);
329 }
330
331 /* Display_text_line puts up a line of text in the display window */
332 /* Special case: if line is 0, clear the display area */
333 display_text_line (line)
334 char *line;
335 {
336   if (line) {
337     waddstr (displayw, line);
338     waddch (displayw, '\n');
339     wrefresh (displayw);
340   }
341   else {
342     werase (displayw);
343   }
344   wrefresh (displayw);
345 }
346
347 /* Display_text displays a canned message from a file */
348 display_text (filename)
349 char *filename;
350 {
351   FILE * fp;
352   char  buf[100];
353
354   werase (displayw);
355   if ((fp = fopen (filename, "r")) == NULL) {
356     wprintw (displayw, "Can't open file %s for reading.\n", filename);
357     return;
358   }
359
360   while (fgets (buf, 100, fp)) {
361   /* get rid of the newline */
362     buf[strlen (buf) - 1] = 0;
363     display_text_line (buf);
364   }
365
366   fclose (fp);
367 }
368
369 /* Clear_display wipes the display and turns off curses */
370 restore_display()
371 {
372   clear();
373   refresh();
374   noraw();
375   echo();
376   endwin();
377 }
378
379 timer_on()
380 {
381   struct itimerval it;
382
383   it.it_interval.tv_sec = 0;
384   it.it_interval.tv_usec = 0;
385   it.it_value.tv_sec = TIMER_TIMEOUT;
386   it.it_value.tv_usec = 0;
387   setitimer (ITIMER_REAL, &it, (struct itimerval *) 0);
388 }
389
390 timer_off()
391 {
392   struct itimerval it;
393
394   it.it_interval.tv_sec = 0;
395   it.it_interval.tv_usec = 0;
396   it.it_value.tv_sec = 0;
397   it.it_value.tv_usec = 0;
398   setitimer (ITIMER_REAL, &it, (struct itimerval *) 0);
399 }
400
401
402 wfeep()
403 {
404     char buf = '\007';
405     write(1, &buf, 1);
406 }
This page took 1.570455 seconds and 5 git commands to generate.