home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / gettyfe < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  7.4 KB

  1. From decwrl!labrea!rutgers!mailrus!tut.cis.ohio-state.edu!ucbvax!unisoft!uunet!allbery Sat Feb  4 10:49:53 PST 1989
  2. Article 809 of comp.sources.misc:
  3. Path: granite!decwrl!labrea!rutgers!mailrus!tut.cis.ohio-state.edu!ucbvax!unisoft!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i034: gettyfe - XENIX 286/386 getty front end
  7. Message-ID: <48163@uunet.UU.NET>
  8. Date: 4 Feb 89 03:15:39 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: wht@tridom.UUCP (Warren Tucker)
  11. Lines: 241
  12. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  13.  
  14. Posting-number: Volume 6, Issue 34
  15. Submitted-by: wht@tridom.UUCP (Warren Tucker)
  16. Archive-name: gettyfe
  17.  
  18. Here is yet-another-getty-front-end-for-autobauding, this
  19. one for SCO XENIX 286/386.  It is likely to be portable
  20. to future releases.  The source has the README built in.
  21.  
  22. ---- Cut Here and unpack ----
  23. #!/bin/sh
  24. # shar:    Shell Archiver  (v1.22)
  25. #
  26. #    Run the following text with /bin/sh to create:
  27. #      gettyfe.c
  28. #
  29. if test -f gettyfe.c; then echo "File gettyfe.c exists"; else
  30. echo "x - extracting gettyfe.c (Text)"
  31. sed 's/^X//' << 'SHAR_EOF' > gettyfe.c &&
  32. X/* CHK=0x71AC */
  33. X/* vi: set tabstop=4 shiftwidth=4: */
  34. X/*+-------------------------------------------------------------------------
  35. X    gettyfe.c - front end to SCO XENIX getty
  36. X    by Warren Tucker ...!gatech!{emory,kd4nc}!wht
  37. X
  38. XAs exec'd by init, getty gets:
  39. Xargv[0] = "-"
  40. Xargv[1] = "ttyxx"
  41. Xargv[2] = "?" where ? is gettydefs character from /etc/ttys
  42. X
  43. XThis program becomes /etc/getty, the supplied getty being renamed
  44. X/etc/real-getty.  My sole dial-in line is tty2D (your mileage may vary).
  45. XThus, if gettyfe sees that it is invoked for a tty other than tty2D,
  46. Xit immediately chains to "real-getty".
  47. X
  48. Xgettyfe counts on the following two gettydefs entries:
  49. X
  50. X2 # B1200 HUPCL OPOST CR0 ECHOE NL1 #
  51. X    B1200 CS8 SANE HUPCL TAB3 ECHOE IXANY #@\r\nlogin: # 3
  52. X
  53. X3 # B2400  HUPCL OPOST CR0 ECHOE NL1 #
  54. X    B2400 CS8 SANE HUPCL TAB3 ECHOE IXANY #@\r\nlogin: # 2
  55. X
  56. XThe port is opened and the file descriptor is dup(2)'d three times.
  57. XThis means that fd 0 (stdin), 1 (stdout) and 2 (stderr) is established
  58. Xas well as fd 3, the use of which is described below.
  59. X
  60. XThe port is set up for 2400 baud, NL mapping to CR and otherwise very
  61. Xraw.  The caller is prompted to set to 8-N-1 and to press ENTER.  If the
  62. Xcaller is calling at 2400 baud, if he/she/it presses either CR or NL
  63. X(via INLCR), then gettyfe executes "real-getty" such that it will prompt
  64. Xfor a login at 2400 baud.  Otherwise, gettyfe changes the baud rate to
  65. X1200, repeats the prompt and so on until CR or NL is found at the proper
  66. Xbaud rate.
  67. X
  68. XWhen a chain to "real-getty" happens after the tty has been opened, fds
  69. X0-2 are closed since getty expects to be able to open them.  fd 3 is
  70. Xleft open so that the tty will not be closed.  I'm not sure if this
  71. Xis necessary or not, or whether or not the problem can be fixed by
  72. Xomitting HUPCL, but I didn't find out.
  73. X
  74. XFinally, an alarm(30) call gives the caller 30 seconds to start a getty.
  75. X
  76. XNotes for variations:
  77. Xwith port set at 2400 baud, a 1200 baud 8-N CR appears as 0xE6
  78. Xwith port set at 1200 baud, a 2400 baud 8-N CR appears as 0xFE
  79. X
  80. XTo compile for XENIX 286 or 386:
  81. X% cc -O -i -s gettyfe.c -o gettyfe
  82. X
  83. XIt probably will work under BSD4 if you hacked the code to replace
  84. Xnap calls with select() calls of equivalent (I have put my copy in here)
  85. X
  86. X--------------------------------------------------------------------------*/
  87. X/*+:EDITS:*/
  88. X/*:01-30-1989-20:08-wht-creation */
  89. X
  90. X#include <fcntl.h>
  91. X#include <signal.h>
  92. X#include <termio.h>
  93. X
  94. Xint fdtty;
  95. Xstruct termio tty_termio;
  96. X
  97. X#if defined(DEBUG)
  98. X#include <stdio.h>
  99. X/*+-------------------------------------------------------------------------
  100. X    debugit(cbaud,user_char)
  101. X--------------------------------------------------------------------------*/
  102. Xdebugit(cbaud,user_char)
  103. Xint cbaud;
  104. Xunsigned char user_char;
  105. X{
  106. XFILE *fp = fopen("/gettyfe.log","a");
  107. X    fprintf(fp,"baud %d user_char %02x\n",(cbaud == B2400) ? 2400 : 1200,
  108. X        user_char);
  109. X    fclose(fp);
  110. X}    /* end of debugit */
  111. X#endif
  112. X
  113. X#if defined(BSD4)
  114. X/*+-------------------------------------------------------------------------
  115. X    nap(msec) - BSD4 nap(); needs <time.h>
  116. X--------------------------------------------------------------------------*/
  117. Xlong
  118. Xnap(msec)
  119. Xlong    msec;
  120. X{
  121. Xstruct timeval tp;
  122. Xstruct timezone tzp;
  123. Xu_long    start_sec;
  124. Xlong    start_usec;
  125. Xlong    target_usec;
  126. Xlong    now_usec;
  127. X
  128. X    gettimeofday(&tp,&tzp);
  129. X    start_sec = tp.tv_sec;
  130. X    start_usec = tp.tv_usec;
  131. X    target_usec = start_usec + (msec * 1000L);
  132. X    now_usec = start_usec;
  133. X    while(now_usec < target_usec)
  134. X    {
  135. X        gettimeofday(&tp,&tzp);
  136. X        now_usec = ((tp.tv_sec - start_sec) * 1000000L) + tp.tv_usec;
  137. X    }
  138. X    return((now_usec - start_usec) / 1000L);
  139. X}    /* end of nap */
  140. X#endif
  141. X
  142. X/*+-------------------------------------------------------------------------
  143. X    bye(code)
  144. X--------------------------------------------------------------------------*/
  145. Xvoid
  146. Xbye(code)
  147. Xint code;
  148. X{
  149. X#if defined(DEBUG)
  150. XFILE *fp = fopen("/gettyfe.log","a");
  151. X    fprintf(fp,"exit(%d)\n",code);
  152. X    fclose(fp);
  153. X#endif
  154. X    exit(code);
  155. X}    /* end of bye */
  156. X
  157. X/*+-------------------------------------------------------------------------
  158. X    login_timeout()
  159. X--------------------------------------------------------------------------*/
  160. Xvoid
  161. Xlogin_timeout()
  162. X{
  163. X    bye(13);
  164. X}    /* end of login_timeout */
  165. X
  166. X/*+-------------------------------------------------------------------------
  167. X    setline(cbaud)
  168. X--------------------------------------------------------------------------*/
  169. Xsetline(cbaud)
  170. Xint cbaud;
  171. X{
  172. X    tty_termio.c_cflag &= ~CBAUD;
  173. X    tty_termio.c_cflag |= cbaud;
  174. X    ioctl(fdtty,TCSETA,(char *)&tty_termio);
  175. X    nap(100L);
  176. X    ioctl(fdtty,TCFLSH,0);
  177. X}    /* end of setline */
  178. X
  179. X/*+-------------------------------------------------------------------------
  180. X    main(argc,argv,envp)
  181. X--------------------------------------------------------------------------*/
  182. Xmain(argc,argv,envp)
  183. Xint        argc;
  184. Xchar    **argv;
  185. Xchar    **envp;
  186. X{
  187. Xint cbaud;
  188. Xunsigned char user_char;
  189. Xchar ttyname[64];
  190. Xchar *prompt = "\r\nUse 8-N-1 and press ENTER: ";
  191. Xint promptlen = strlen(prompt);
  192. X
  193. X    if(strcmp(argv[1],"tty2D"))
  194. X    {
  195. X        execv("/etc/real-getty",argv);
  196. X        bye(1);
  197. X    }
  198. X
  199. X    strcpy(ttyname,"/dev/");
  200. X    strcat(ttyname,argv[1]);
  201. X
  202. X    if((fdtty = open(ttyname,O_RDWR)) < 0)        /* open stdin */
  203. X        bye(2);
  204. X
  205. X    dup(0);            /* create stdout */
  206. X    dup(0);            /* create stderr */
  207. X    dup(0);            /* create an extra we will not close on exec */
  208. X
  209. X    tty_termio.c_iflag = IGNPAR | IGNBRK | INLCR;
  210. X    tty_termio.c_oflag = 0;
  211. X    tty_termio.c_cflag = CREAD | HUPCL | CS8 | CSTOPB | B2400;
  212. X    tty_termio.c_lflag = 0;
  213. X    tty_termio.c_cc[VMIN]  = 1;
  214. X    tty_termio.c_cc[VTIME] = 0;
  215. X    ioctl(fdtty,TCSETA,(char *)&tty_termio);
  216. X
  217. X    nap(1750L);        /* allow for other end carrier detect */
  218. X
  219. X    signal(SIGALRM,login_timeout);
  220. X    alarm(30);        /* 30 secs to finish this */
  221. X
  222. X    cbaud = B2400;
  223. X
  224. X    while(1)
  225. X    {
  226. X        write(1,prompt,promptlen);
  227. X        read(0,&user_char,1);
  228. X#if defined(DEBUG)
  229. X        debugit(cbaud,user_char);
  230. X#endif
  231. X        switch(user_char)
  232. X        {
  233. X            case 0x0D:
  234. X                close(0);    /* getty will re-open */
  235. X                close(1);
  236. X                close(2);    /* keep fd 3 open */
  237. X                alarm(0);
  238. X                execl("/etc/real-getty","-",argv[1],
  239. X                    (cbaud == B2400) ? "3" : "2",(char *)0);
  240. X                bye(3);
  241. X
  242. X            default:
  243. X                cbaud = (cbaud == B2400) ? B1200 : B2400;
  244. X                setline(cbaud);
  245. X        }
  246. X    }
  247. X    /*NOTREACHED*/
  248. X
  249. X}    /* end of main */
  250. X
  251. SHAR_EOF
  252. chmod 0644 gettyfe.c || echo "restore of gettyfe.c fails"
  253. fi
  254. exit 0
  255.  
  256.  
  257.