home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / gettyfe < prev    next >
Internet Message Format  |  1989-02-03  |  7KB

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