home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume15 / newgetty / newgetty.c < prev    next >
C/C++ Source or Header  |  1988-06-01  |  3KB  |  112 lines

  1. /* @(#)newgetty.c    2.2 4/18/88 17:55:29 ( 4/18/88 17:33:01 ) */
  2. /* New getty program. If called with an argument other than 'b', it invokes
  3.     the old getty program (assumed to be at /etc/lib/getty), otherwise
  4.     it reads the default file (/etc/defaults/getty) for 'prompt=',
  5.     displays the result, reads a character, loops if non-ascii, checks
  6.     the default file for <char>=, executes that program if found, otherwise
  7.     loops. */
  8.  
  9. /* 0.4 Sys 3 mods: set echo, cooked */
  10. /* Gave up on getting it to work with the sys3 structures. Will work fine
  11. for BSD systems, or Xenix systems. If you are pure USG, either link for
  12. version 7 mode (the kernel still has the version 7 stuff hidden inside
  13. for compatibility), or convert it yourself.
  14. */
  15.  
  16. #include <ctype.h>
  17. #include <stdio.h>
  18. #include <sgtty.h>
  19.  
  20. /* 0.3 Does not actually pass arg to next program */
  21. /* Will not work at 2400 */
  22. #define SPEED    (term.sg_ispeed >= B1200 ? "3":"5")
  23. #define fnsleep(x)  (3)    /* Return time for sleep */
  24.  
  25. char *defread(), *mkstr();
  26. main(argc, argv)
  27. char *argv[];
  28. {
  29.     int c;
  30.     char *x;
  31.     struct sgttyb term;
  32.     if (argc != 2 || strcmp (argv[1], "b") != 0)
  33. error:        execv ("/etc/lib/getty", argv);
  34.     if (defopen("/etc/default/getty") != 0)
  35.     {
  36.         perror("new getty:");
  37.         goto error;
  38.     }
  39.     ioctl (1, TIOCGETP, &term);  
  40.     term.sg_ispeed = term.sg_ospeed = B9600;
  41.     term.sg_flags |= CBREAK | ECHO | CRMOD;
  42.     term.sg_flags &= ~RAW;
  43.     ioctl (1, TIOCNXCL);
  44.     for (;;)
  45.     {
  46.         ioctl (1, TIOCSETP, &term);
  47.         puts ("\r");
  48.         fputs(defread("prompt="), stdout);
  49.         /* Flush input */
  50.         while ((c=finkey(stdin)) != '\0' && c!= EOF)
  51.             ;
  52.         sleep(fnsleep(term.sg_ispeed));
  53.         if (isalpha(c=finkey(stdin)) && (x=defread(mkstr(c))) !=NULL)
  54.         {
  55.             putchar('\n');
  56.             term.sg_flags = term.sg_flags &~ CBREAK;
  57.             if (isupper(c))
  58.                 term.sg_flags |= LCASE;
  59.             ioctl (1, TIOCSETP, &term);
  60.             /* No more alarm clock */
  61.             execlp (x, x, NULL);
  62.     /* Speed is no longer passed */
  63.             perror (x);
  64.             term.sg_flags |= CBREAK;
  65.         }
  66.         term.sg_ispeed = term.sg_ospeed = nextspeed (term.sg_ispeed);
  67.                     /* Change speed & try again */
  68.     }
  69. }
  70.  
  71. char *mkstr(c)
  72. char c;
  73. {
  74.     static char chr[]="x=";
  75.     chr[0]=c;
  76.     return chr;
  77. }
  78.  
  79. nextspeed(oldspeed)
  80. {
  81.     switch (oldspeed)
  82.     {    case B300: return B9600;
  83.         case B1200: return B300;
  84.         case B2400: return B1200;
  85.         case B9600: return B2400;
  86.     }
  87. }
  88.  
  89. /* @(#)inkey.c    1.1 4/18/88 17:57:48 ( 10/2/86 18:51:54 ) */
  90. # include <stdio.h>
  91. inkey(fd)
  92. int fd;
  93. {
  94.     char c;
  95.     int temp;
  96.     if (temp=rdchk(fd) > 0)
  97.         if (read (fd, &c, 1) >0)
  98.             return c;
  99.         else return (char) -1;
  100.     else return (temp == 0 ? 0 : -1);
  101. }
  102.  
  103. finkey(fp)
  104. FILE *fp;
  105. {
  106.     if ((fp->_cnt > 0) || rdchk(fileno(fp)) > 0)
  107.         return getc (fp);
  108.     if feof(fp)
  109.         return EOF;
  110.     return 0;
  111. }
  112.