]> andersk Git - moira.git/blob - clients/mailmaint/mailmaint.c
sms -> moira
[moira.git] / clients / mailmaint / mailmaint.c
1 /*
2  *      $Source$
3  *      $Header$
4  */
5
6 /*  (c) Copyright 1988 by the Massachusetts Institute of Technology. */
7 /*  For copying and distribution information, please see the file */
8 /*  <mit-copyright.h>. */
9
10 #ifndef lint
11 static char rcsid_mailmaint_c[] = "$Header$";
12 #endif lint
13
14 /***********************************************************************/
15 /*  mailmaint.c - pjlevine - 20 August 1987 
16     
17 */
18 /***********************************************************************/
19 #include <stdio.h>
20 #include <pwd.h>
21 #include <signal.h>
22 #include <strings.h>
23 #include <curses.h>
24 #include <sys/types.h>
25 #include <varargs.h>
26 #include <com_err.h>
27 #include <ctype.h>
28 #include <moira.h>
29 #include <moira_site.h>
30 #include <mit-copyright.h>
31
32
33 #define STARTCOL 0
34 #define STARTROW 3
35 #define SECONDCOL 30
36 #define DISPROW 15
37 #define LISTMAX 50
38 #define LISTSIZE 32
39 #define CTL(ch)  ((ch) & 037)
40 #define MAX(A,B) ((A) > (B) ? (A) : (B))
41
42 char *whoami;           /* should not be static, for logging package */
43 static int status;
44 static int scream();
45 extern char *strsave();
46 #ifdef __STDC__
47 void menu_err_hook(const char *who, long code, const char *fmt, va_list args);
48 #else
49 void menu_err_hook();
50 #define const
51 #endif
52
53 typedef struct list_info {
54     int active;
55     int public;
56     int hidden;
57     int maillist;
58     int group;
59     char *acl_type;
60     char *acl_name;
61     char *desc;
62     char *modtime;
63     char *modby;
64     char *modwith;
65 }         List_info;
66
67 static char *ascbuff = {"0123456789"};
68 static int print_2(), print_1();
69 static List_info *current_li = (List_info *) NULL;
70 static int get_list_info();
71 static int fetch_list_info();
72
73 char *malloc();
74 char *rindex();
75 char *getlogin();
76 extern char *strsave();
77 char *getenv();
78 char *calloc();
79
80 uid_t getuid();
81
82 typedef struct _menu {
83     int num_items;
84     char *title;
85     char **items;
86 }     MENU;
87
88 MENU *main_menu, *help_menu;
89
90 int position[2], oldpos[2];
91 int level, found_some, currow, page, num_members;
92 int moreflg, toggle, first_time;
93 char *uname;
94
95
96 /****************************************************/
97 /*ARGSUSED*/
98 main(argc, argv)
99     int argc;
100     char *argv[];
101
102 {
103 #ifdef __STDC__
104     void (*old_hook)(const char *, long, const char *, va_list);
105 #else
106     void (*old_hook)();
107 #endif
108     int use_menu = 1;
109     char buf[BUFSIZ], *motd;
110
111     if ((whoami = rindex(argv[0], '/')) == NULL)
112         whoami = argv[0];
113     else
114         whoami++;
115     uname = calloc(20, 1);
116     if ((current_li = (List_info *) malloc(sizeof(List_info)))
117         == (List_info *) NULL) {
118         (void) sprintf(buf, ": allocating list info");
119         goto punt;
120     }
121     else {
122         current_li->acl_type = (char *) NULL;
123         current_li->acl_name = (char *) NULL;
124         current_li->desc = (char *) NULL;
125         current_li->modtime = (char *) NULL;
126         current_li->modby = (char *) NULL;
127         current_li->modwith = (char *) NULL;
128     }
129     if ((uname = getlogin()) == NULL) {
130         struct passwd *getpwuid();
131
132         uname = getpwuid((int) getuid())->pw_name;
133     }
134     uname = (uname && strlen(uname)) ? strsave(uname) : "";
135
136     printf("Connecting to database for %s...please hold on.\n", uname);
137
138     status = mr_connect(NULL);
139     if (status) {
140         (void) sprintf(buf, "\nConnection to Moira server failed");
141         goto punt;
142     }
143
144     status = mr_motd(&motd);
145     if (status) {
146         com_err(whoami, status, " unable to check server status");
147         mr_disconnect();
148         exit(2);
149     }
150     if (motd) {
151         fprintf(stderr, "The Moira server is currently unavailable:\n%s\n", motd);
152         mr_disconnect();
153         exit(2);
154     }
155     status = mr_auth("mailmaint");
156     if (status) {
157         (void) sprintf(buf, "\nAuthorization failed.\n");
158         goto punt;
159     }
160
161     if (use_menu) {
162         (void) initscr();
163         if ((LINES < 24) || (COLS < 60)) {
164             display_buff("Display window too small.\n\n");
165             (void) sprintf(buf, "Current window parameters are (%d \
166 lines, %d columns)\n", LINES, COLS);
167             display_buff(buf);
168             display_buff("Please resize your window\n");
169             display_buff("to at least 24 lines and 60 columns.\n");
170             exit(0);
171         }
172         raw();
173         noecho();
174         old_hook = set_com_err_hook(menu_err_hook);
175         position[0] = oldpos[0] = 1;
176         level = 0;
177         pack_main_menu();
178         pack_help_menu();
179         display_menu(main_menu);
180         get_main_input();
181         cls();
182         endwin();
183         set_com_err_hook(old_hook);
184     }
185     exit(0);
186
187 punt:
188     com_err(whoami, status, buf);
189     exit(1);
190 }
191
192 /****************************************************/
193 get_main_input()
194 {
195     int c;
196     int retflg;
197
198     while (1) {
199         oldpos[level] = position[level];
200         retflg = 0;
201         currow = DISPROW + 2;
202         page = 1;
203         toggle = num_members = moreflg = 0;
204         c = getchar();
205         if (c == 13) {
206             if (position[level] == 7)
207                 c = 'q';
208             else
209                 c = ascbuff[position[level]];
210             retflg = 1;
211         }
212         switch (c) {
213         case 'L' & 037: /* clear screen */
214             display_menu(main_menu);
215             break;
216         case 'q':
217         case 'Q':               /* quit */
218             position[level] = 7;
219             highlight(main_menu);
220             if (retflg) {
221                 cls();
222                 return;
223             }
224             break;
225         case '1':               /* show all lists */
226             position[level] = 1;
227             if (retflg) {
228                 show_all();
229             }
230             break;
231         case '2':               /* get all members of a list */
232             position[level] = 2;
233             if (retflg) {
234                 (void) list_members();
235             }
236             break;
237         case '3':               /* display list which user is a recipient */
238             position[level] = 3;
239             if (retflg) {
240                 list_by_member();
241             }
242             break;
243         case '4':               /* show description */
244             position[level] = 4;
245             if (retflg) {
246                 show_list_info();
247             }
248             break;
249         case '5':               /* add to list */
250             position[level] = 5;
251             if (retflg) {
252                 add_member();
253             }
254             break;
255         case '6':               /* delete */
256             position[level] = 6;
257             if (retflg) {
258                 delete_member();
259             }
260             break;
261         case 27:                /* up arrow */
262             c = getchar();
263             if (c == 91) {
264                 c = getchar();
265                 if (c == 65) {
266                     position[level]--;
267                     if (!position[level])
268                         position[level] = 7;
269                 }
270                 else {
271                     if (c == 66) {
272                         position[level]++;
273                         if (position[level] > 7)
274                             position[level] = 1;
275                     }
276                 }
277             }
278             break;
279         default:
280             printf("%c", 7);
281             break;
282         }
283         highlight(main_menu);
284     }
285 }
286
287 /****************************************************/
288 show_list_info()
289 {
290     char *buf;
291
292     show_text(DISPROW, STARTCOL, "Show information about a list.\n\r");
293     buf = calloc((unsigned)1024, 1);
294     if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
295         display_buff("\n\r");
296         if (fetch_list_info(buf, current_li) == 0) {
297             (void) sprintf(buf, "Description: %s\n\r", current_li->desc);
298             if (strlen(buf) > 60)
299                 (void) display_buff(buf);
300             else
301                 show_text(currow, STARTCOL, buf);
302             currow++;
303             (void) sprintf(buf, "List Administrator: %s %s",
304                            current_li->acl_type, current_li->acl_name);
305             show_text(currow, STARTCOL, buf);
306             currow++;
307             (void) sprintf(buf, "Modified on %s by user %s with %s",
308                            current_li->modtime, current_li->modby,
309                            current_li->modwith);
310             show_text(currow, STARTCOL, buf);
311             currow++;
312         }
313         else {
314             show_text(currow, STARTCOL, "mailmaint: No such list found.");
315             currow++;
316         }
317         show_text(currow, STARTCOL, "Press any Key to continue...");
318         (void) getchar();
319     }
320     clrwin(DISPROW);
321 }
322
323 /****************************************************/
324 display_buff(buf)
325     char *buf;
326 {
327     int i, cnt;
328     char *printbuf;
329     int maxcol;
330
331     maxcol = COLS;
332     if (maxcol >= 80)
333         maxcol = 80;
334
335     cnt = 0;
336     printbuf = calloc((unsigned)maxcol, 1);
337     for (i = 0; i <= strlen(buf); i++) {
338         printbuf[cnt] = buf[i];
339         cnt++;
340         if (cnt >= maxcol) {
341             (void) start_display_buff(printbuf);
342             cnt = 0;
343             free(printbuf);
344             printbuf = calloc((unsigned)maxcol, 1);
345         }
346     }
347     if (strlen(buf) % maxcol != 0) {
348         (void) start_display_buff(printbuf);
349         free(printbuf);
350     }
351     return (0);
352 }
353
354 /****************************************************/
355 start_display_buff(buff)
356     char *buff;
357 {
358     char buffer[5];
359
360     num_members++;
361     if (moreflg)
362         return (0);
363     if (currow >= LINES - 2) {
364         page++;
365         currow++;
366         mvcur(0, 0, currow, STARTCOL);
367         refresh();
368         if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
369             display_buff("Flushing query...");
370             moreflg = 1;
371             return (0);
372         }
373         clrwin(DISPROW + 2);
374         currow = DISPROW + 2;
375         show_text(currow, STARTCOL, "continued");
376         currow++;
377     }
378     show_text(currow, STARTCOL, buff);
379     currow++;
380     return (0);
381 }
382
383 /****************************************************/
384 add_member()
385 {
386     char *argv[3];
387     char *buf;
388
389     show_text(DISPROW, STARTCOL, "Add yourself to a list\n\r");
390     buf = calloc(LISTMAX, 1);
391     if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
392         display_buff("\r\n");
393         argv[0] = strsave(buf);
394         argv[1] = strsave("user");
395         argv[2] = strsave(uname);
396         if (status = mr_query("add_member_to_list", 3, argv,
397                                scream, (char *) NULL)) {
398             display_buff("\r\n");
399             com_err(whoami, status, " found.\n");
400         }
401         else {
402             (void) sprintf(buf, "User %s added to list\r", uname);
403             show_text(DISPROW + 3, STARTCOL, buf);
404         }
405         currow = DISPROW + 4;
406         show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
407         (void) getchar();
408     }
409     clrwin(DISPROW);
410 }
411
412 /****************************************************/
413 delete_member()
414 {
415     char *argv[3];
416     char *buf;
417
418     show_text(DISPROW, STARTCOL, "Remove yourself from a list\n\r");
419     buf = calloc(LISTMAX, 1);
420     if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
421         display_buff("\r\n");
422         argv[0] = strsave(buf);
423         argv[1] = strsave("user");
424         argv[2] = strsave(uname);
425         if (status = mr_query("delete_member_from_list", 3, argv,
426                                scream, (char *) NULL)) {
427             display_buff("\r\n");
428             com_err(whoami, status, " found.\n");
429         }
430         else {
431             (void) sprintf(buf, "User %s deleted from list\r", uname);
432             show_text(DISPROW + 3, STARTCOL, buf);
433         }
434         currow = DISPROW + 4;
435         show_text(DISPROW + 4, STARTCOL, "Press any Key to continue...");
436         (void) getchar();
437     }
438     clrwin(DISPROW);
439 }
440
441 /****************************************************/
442 list_by_member()
443 {
444     char *nargv[3];
445     char *buf;
446
447     nargv[1] = strsave("ruser");
448     nargv[2] = strsave(uname);
449     buf = calloc(BUFSIZ, 1);
450     (void) sprintf(buf, "%s is on the following lists:\r", uname);
451     show_text(DISPROW, STARTCOL, buf);
452     mvcur(0, 0, currow, STARTCOL);
453     refresh();
454     if (status = mr_query("get_lists_of_member", 2, nargv + 1,
455                            print_1, (char *) NULL)) {
456         display_buff("\r\n");
457         com_err(whoami, status, " in get_lists_of_member");
458     }
459     currow++;
460     show_text(currow, STARTCOL, "Press any Key to continue...");
461     (void) getchar();
462     clrwin(DISPROW);
463     return;
464 }
465
466 /****************************************************/
467 show_all()
468 {
469     char c;
470
471     show_text(DISPROW, STARTCOL, "This function may take a \
472 while... proceed? [y] ");
473     c = getchar();
474     if (c == 'y' || c == 'Y' || c == '\n') {
475         move(DISPROW + 1, STARTCOL);
476         addstr("Processing query...please hold");
477         refresh();
478         (void) list_all_groups();
479     }
480     else
481         erase_line(DISPROW, STARTCOL);
482     return;
483
484 }
485
486 /****************************************************/
487 /*ARGSUSED*/
488 static int
489 print_1(argc, argv, callback)
490     int argc;
491     char *argv[], *callback;
492 {
493     char buf[BUFSIZ];
494
495     /* no newline 'cause display_buff adds one */
496     (void) sprintf(buf, "%s\r", argv[0]);
497     (void) start_display(buf);
498
499     return (0);
500 }
501
502 /****************************************************/
503 /*ARGSUSED*/
504 static int
505 print_all(argc, argv, callback)
506     int argc;
507     char *argv[], *callback;
508 {
509     char buf[BUFSIZ];
510
511     if (moreflg)
512         return (0);
513     if (first_time) {
514         erase_line(DISPROW + 1, STARTCOL);
515         show_text(DISPROW + 1, STARTCOL, "All mailing lists:");
516         first_time = 0;
517     }
518     (void) sprintf(buf, "%s\r", argv[0]);
519     (void) start_display(buf);
520
521     return (0);
522 }
523
524 /****************************************************/
525 list_all_groups()
526 {
527     char *argv[5];
528     argv[0] = argv[3] = "true";
529     argv[1] = argv[4] = "dontcare";
530     argv[2] = "false";
531     first_time = 1;
532     if (status = mr_query("qualified_get_lists", 5, argv,
533                            print_all, (char *) NULL)) {
534         display_buff("\r\n");
535         com_err(whoami, status, " in list_all_groups\n");
536     }
537     end_display();
538
539     return (0);
540 }
541
542 /****************************************************/
543 list_members()
544 {
545     char *argv[1];
546     char *buf;
547     char buffer[80];
548
549     found_some = 0;
550     move(DISPROW, STARTCOL);
551     mvcur(0, 0, DISPROW, STARTCOL);
552     refresh();
553     buf = calloc(LISTMAX, 1);
554     if (Prompt("Enter List Name: ", buf, LISTSIZE, 1) == 1) {
555         (void) sprintf(buffer, "The members of list '%s' are:", buf);
556         show_text(DISPROW + 1, STARTCOL, buffer);
557         argv[0] = buf;
558         if (status = mr_query("get_members_of_list", 1, argv,
559                                print_2, (char *) NULL)) {
560             display_buff("\r\n");
561             com_err(whoami, status, " found.\n");
562             currow++;
563         }
564         if (!found_some) {
565             show_text(currow, STARTCOL, "List is empty (no members).");
566             currow++;
567             show_text(currow, STARTCOL, "Press any key to continue...");
568             getchar();
569             clrwin(DISPROW);
570             return;
571         }
572         end_display();
573         return(0);
574     }
575     clrwin(DISPROW);
576     return (0);
577 }
578
579 /****************************************************/
580 /*ARGSUSED*/
581 static int
582 print_2(argc, argv, callback)
583     int argc;
584     char *argv[], *callback;
585 {
586     char buf[BUFSIZ];
587
588     found_some = 1;
589     (void) sprintf(buf, "%s %s", argv[0], argv[1]);
590     (void) start_display(buf);
591
592     return (0);
593 }
594
595 /****************************************************/
596 start_display(buff)
597     char *buff;
598 {
599     char *buffer;
600
601     num_members++;
602     if (moreflg)
603         return(0);
604     buffer = calloc(50, 1);
605     if (currow >= LINES - 2) {
606         page++;
607         currow++;
608         mvcur(0, 0, currow, STARTCOL);
609         refresh();
610         if (Prompt("--RETURN for more, ctl-c to exit--", buffer, 1, 0) == 0) {
611             display_buff("Flushing query...");
612             moreflg = 1;
613             return (0);
614         }
615         clrwin(DISPROW + 2);
616         currow = DISPROW + 2;
617         (void) sprintf(buffer, "Continued (Page %d)", page);
618         show_text(currow, STARTCOL, buffer);
619         currow++;
620         toggle = 0;
621     }
622     if (!toggle)
623         show_text(currow, STARTCOL, buff);
624     else {
625         show_text(currow, SECONDCOL, buff);
626         currow++;
627     }
628     toggle = !toggle;
629     return(0);
630 }
631
632 /****************************************************/
633 end_display()
634 {
635     char *buffer;
636
637     if (moreflg) {
638         clrwin(DISPROW);
639         return;
640     }
641
642     buffer = calloc(50, 1);
643     currow++;
644     (void) sprintf(buffer, "End of List. %d Total Members\r", num_members);
645     show_text(currow, STARTCOL, buffer);
646     currow++;
647     show_text(currow, STARTCOL, "Press any key to continue...");
648     (void) getchar();
649     clrwin(DISPROW);
650
651 }
652
653 /****************************************************/
654 display_menu(menu)
655     MENU *menu;
656 {
657     int i;
658
659     cls();
660     title(menu->title);
661     mvcur(0, 0, STARTROW, STARTCOL);
662     refresh();
663     for (i = 0; i <= menu->num_items - 1; i++) {
664         move(STARTROW + i, STARTCOL);
665         standend();
666         addstr(menu->items[i]);
667         refresh();
668     }
669     center_text(STARTROW + menu->num_items + 2,
670                 "Enter a number, <up arrow>, or <down arrow>.");
671     if (!level)
672         center_text(STARTROW + menu->num_items + 3,
673                     "Press 'q' to exit, <return> to confirm choice.");
674     else
675         center_text(STARTROW + menu->num_items + 3,
676         "Press 'q' to exit, 'r' for main menu, <return> to confirm choice.");
677
678     if (!level)
679         highlight(main_menu);
680 }
681
682 /****************************************************/
683 pack_main_menu()
684 {
685     char *buf;
686
687     main_menu = (MENU *) malloc((unsigned) sizeof(MENU));
688     main_menu->num_items = 7;
689     main_menu->items = (char **) malloc((unsigned) sizeof(char *) * main_menu->num_items);
690
691     buf = calloc(50, 1);
692     (void) sprintf(buf, "Mail List Program for %s", uname);
693     main_menu->title = strsave(buf);
694     main_menu->items[0] = strsave("1.  Show all mailing lists.");
695     main_menu->items[1] = strsave("2.  Get all members of a mailing list.");
696     main_menu->items[2] = strsave("3.  Display lists of which you are a member.");
697     main_menu->items[3] = strsave("4.  Show description of list.");
698     main_menu->items[4] = strsave("5.  Add yourself to a mailing list.");
699     main_menu->items[5] = strsave("6.  Delete yourself from a mailing list.");
700     main_menu->items[6] = strsave("q.  Quit.");
701 }
702
703 /****************************************************/
704 pack_help_menu()
705 {
706     help_menu = (MENU *) malloc((unsigned) sizeof(MENU));
707     help_menu->num_items = 5;
708     help_menu->items = (char **) malloc((unsigned) sizeof(char *) * help_menu->num_items);
709
710     help_menu->title = strsave("mailmaint is designed as a basic mail list administration program.");
711     help_menu->items[0] = strsave("if you need to perform more advanced list manipulation like");
712     help_menu->items[1] = strsave("adding lists, or changing list characteristics, refer to the");
713     help_menu->items[2] = strsave("program listmaint.");
714     help_menu->items[3] = strsave(" ");
715     help_menu->items[4] = strsave("Press any key to continue.");
716 }
717
718 /****************************************************/
719 highlight(menu)
720     MENU *menu;
721 {
722
723
724     if (oldpos[level] != position[level]) {
725         move(STARTROW + oldpos[level] - 1, STARTCOL);
726         standend();
727         addstr(menu->items[oldpos[level] - 1]);
728         refresh();
729     }
730
731     move(STARTROW + position[level] - 1, STARTCOL);
732     standout();
733     addstr(menu->items[position[level] - 1]);
734     refresh();
735     standend();
736     refresh();
737 }
738
739 /****************************************************/
740 title(buff)
741     char *buff;
742 {
743     move(0, MAX(0, (COLS - strlen(buff)) >> 1));
744     standout();
745     addstr(buff);
746     refresh();
747     standend();
748 }
749
750 /****************************************************/
751 center_text(row, buff)
752     int row;
753     char *buff;
754 {
755     move(row, MAX(0, (COLS - strlen(buff)) >> 1));
756     addstr(buff);
757     refresh();
758 }
759
760 /****************************************************/
761 show_text(row, col, buff)
762     int row, col;
763     char *buff;
764 {
765     mvcur(0, 0, row, col);
766     refresh();
767     printf("%s", buff);
768 }
769
770 /****************************************************/
771 erase_line(row, col)
772     int row, col;
773 {
774     char *buff;
775     int i;
776
777     buff = calloc((unsigned)COLS, 1);
778     for (i = 0; i <= COLS - 2; i++)
779         buff[i] = ' ';
780     buff[i] = 0;                /* just to be sure ! */
781     move(row, col);
782     mvcur(0, 0, row, col);
783     printf("%s", buff);
784     refresh();
785 }
786
787 /****************************************************/
788 cls()
789 {
790     clear();
791     refresh();
792 }
793
794 /****************************************************/
795 clrwin(erase_row)
796     int erase_row;
797 {
798     int i;
799     char *buff;
800     int maxcol;
801
802     maxcol = COLS;
803     if (maxcol > 80)
804         maxcol = 80;            /* limit width */
805
806     buff = calloc((unsigned)maxcol + 1, 1);
807     for (i = 0; i <= maxcol - 1; i++)
808         buff[i] = ' ';
809     buff[i] = 0;                /* just to be sure ! */
810     mvcur(0, 0, erase_row, STARTCOL);
811     refresh();
812     for (i = erase_row; i <= currow - 1; i++) {
813         printf("%s\n\r", buff);
814     }
815     printf("%s", buff);
816     mvcur(erase_row, STARTCOL, STARTROW + oldpos[level] - 1, STARTCOL);
817     refresh();
818 }
819
820 /****************************************************/
821
822 static int
823 scream()
824 {
825     com_err(whoami, status, "\nA Moira update returned a value -- programmer \
826 botch\n");
827     mr_disconnect();
828     exit(1);
829     return(0);  /* to keep compiler happy */
830 }
831
832 /****************************************************/
833 /*ARGSUSED*/
834 static int 
835 fetch_list_info(list, li)
836     char *list;
837     List_info *li;
838 {
839     char *argv[1];
840
841     argv[0] = list;
842     return mr_query("get_list_info", 1, argv, get_list_info, (char *) NULL);
843 }
844
845 /* ARGSUSED */
846 static int 
847 get_list_info(argc, argv)
848     int argc;
849     char **argv;
850 {
851
852     if (current_li->acl_type)
853         free(current_li->acl_type);
854     current_li->acl_type = strsave(argv[7]);
855     if (current_li->acl_name)
856         free(current_li->acl_name);
857     current_li->acl_name = strsave(argv[8]);
858     if (current_li->desc)
859         free(current_li->desc);
860     current_li->desc = strsave(argv[9]);
861     if (current_li->modtime)
862         free(current_li->modtime);
863     current_li->modtime = strsave(argv[10]);
864     if (current_li->modby)
865         free(current_li->modby);
866     current_li->modby = strsave(argv[11]);
867     if (current_li->modwith)
868         free(current_li->modwith);
869     current_li->modwith = strsave(argv[12]);
870     return (0);
871 }
872
873
874
875
876 /****************************************************/
877 /* Prompt the user for input */
878 int 
879 Prompt(prompt, buf, buflen, crok)
880     char *prompt;
881     char *buf;
882     int buflen;
883     int crok;
884 {
885     int c;
886     char *p;
887
888     printf("%s", prompt);
889     refresh();
890     for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) {
891         refresh();
892         c = getchar();
893         switch (c) {
894         case CTL('C'):
895             return 0;
896         case CTL('Z'):
897             return 0;
898         case CTL('L'):
899             cls();
900             display_menu(main_menu);
901             return (0);
902         case '\n':
903         case '\r':
904             if (crok)
905                 display_buff("\r");
906             *p = '\0';
907             if (strlen(buf) < 1)/* only \n or \r in buff */
908                 return (-1);
909             else
910                 return (1);
911         case '\b':
912         case '\177':
913             if (p > buf) {
914                 p--;
915                 printf("\b \b");
916             }
917             break;
918         case CTL('U'):
919         case CTL('G'):
920         case CTL('['):
921             while (p-- > buf)
922                 printf("\b \b");
923             p = buf;
924             break;
925         default:
926             if (abs(strlen(p) - strlen(buf)) >= buflen) {
927                 printf("%c", 7);
928                 break;
929             }
930             if (isprint(c)) {
931                 (void) addch(c);
932                 *p++ = c;
933             }
934             else
935                 (void) putchar(CTL('G'));
936             break;
937         }
938     }
939     return(0);
940 }
941
942
943 /*
944  * Hook function to cause error messages to be printed through
945  * curses instead of around it.
946  */
947
948 void
949 menu_err_hook(who, code, fmt, args)
950     const char *who;
951     long code;
952     const char *fmt;
953     va_list args;
954 {
955     char buf[BUFSIZ], *cp;
956
957     (void) strcpy(buf, who);
958     for (cp = buf; *cp; cp++);
959     *cp++ = ':';
960     *cp++ = ' ';
961     if (code) {
962         (void) strcpy(cp, error_message(code));
963         while (*cp)
964             cp++;
965     }
966 #if defined(AIX386) || defined(sun)
967     vsprintf(cp, fmt, args);
968 #else
969     /* can do this because we never pass more than 1 arg here anyway... */
970     sprintf(cp, fmt, args);
971 #endif
972     display_buff(buf);
973 }
This page took 0.906981 seconds and 5 git commands to generate.