home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3965 / asyncbsd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-04  |  4.8 KB  |  241 lines

  1. /* Terminal interface for BSD
  2.    Copyright (C) 1991 Joseph H. Allen
  3.  
  4. This file is part of JOE (Joe's Own Editor)
  5.  
  6. JOE is free software; you can redistribute it and/or modify it under the terms
  7. of the GNU General Public License as published by the Free Software
  8. Foundation; either version 1, or (at your option) any later version. 
  9.  
  10. JOE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with JOE; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sgtty.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include <sys/time.h>
  23. #include "async.h"
  24.  
  25. #define HZ 10            /* Clock ticks/second */
  26.  
  27. #define DIVISOR 11000000    /* The baud rate divided into this should
  28.                    give the number of microseconds per
  29.                    character.  It should attempt to
  30.                    reflect the true throughput, which is
  31.                    usually slower than the best possible
  32.                    for a given baud rate */
  33.  
  34. #define TIMES 3            /* Times per second that we check for
  35.                    typeahead */
  36.  
  37. static struct sgttyb oarg;
  38. static struct tchars otarg;
  39. static struct ltchars oltarg;
  40.  
  41. static unsigned char *obuf=0;
  42. static unsigned obufp=0;
  43. static unsigned obufsiz;
  44. static unsigned long ccc;
  45.  
  46. static unsigned speeds[]=
  47. {
  48. B50,50,B75,75,B110,110,B134,134,B150,150,B200,200,B300,300,B600,600,B1200,1200,
  49. B1800,1800,B2400,2400,B4800,4800,B9600,9600,EXTA,19200,EXTB,38400
  50. };
  51.  
  52. aopen()
  53. {
  54. int x;
  55. struct sgttyb arg;
  56. struct tchars targ;
  57. struct ltchars ltarg;
  58. fflush(stdout);
  59. signal(SIGHUP,tsignal);
  60. signal(SIGINT,SIG_IGN);
  61. signal(SIGQUIT,SIG_IGN);
  62. signal(SIGPIPE,SIG_IGN);
  63. signal(SIGALRM,SIG_IGN);
  64. signal(SIGTERM,tsignal);
  65. signal(SIGUSR1,SIG_IGN);
  66. signal(SIGUSR2,SIG_IGN);
  67. ioctl(fileno(stdin),TIOCGETP,&arg);
  68. ioctl(fileno(stdin),TIOCGETC,&targ);
  69. ioctl(fileno(stdin),TIOCGLTC,<arg);
  70. oarg=arg; otarg=targ; oltarg=ltarg;
  71. arg.sg_flags=( (arg.sg_flags&~(ECHO|CRMOD) ) | CBREAK) ;
  72. targ.t_intrc= -1;
  73. targ.t_quitc= -1;
  74. targ.t_eofc= -1;
  75. targ.t_brkc= -1;
  76. ltarg.t_suspc= -1;
  77. ltarg.t_dsuspc= -1;
  78. ltarg.t_rprntc= -1;
  79. ltarg.t_flushc= -1;
  80. ltarg.t_werasc= -1;
  81. ltarg.t_lnextc= -1;
  82. ioctl(fileno(stdin),TIOCSETN,&arg);
  83. ioctl(fileno(stdin),TIOCSETC,&targ);
  84. ioctl(fileno(stdin),TIOCSLTC,<arg);
  85. ccc=0;
  86. for(x=0;x!=30;x+=2)
  87.  if(arg.sg_ospeed==speeds[x])
  88.   {
  89.   ccc=DIVISOR/speeds[x+1];
  90.   break;
  91.   }
  92. if(!obuf)
  93.  {
  94.  if(!(TIMES*ccc)) obufsiz=4096;
  95.  else
  96.   {
  97.   obufsiz=1000000/(TIMES*ccc);
  98.   if(obufsiz>4096) obufsiz=4096;
  99.   }
  100.  if(!obufsiz) obufsiz=1;
  101.  obuf=(unsigned char *)malloc(obufsiz);
  102.  }
  103. }
  104.  
  105. aclose()
  106. {
  107. aflush();
  108. ioctl(fileno(stdin),TIOCSETN,&oarg);
  109. ioctl(fileno(stdin),TIOCSETC,&otarg);
  110. ioctl(fileno(stdin),TIOCSLTC,&oltarg);
  111. signal(SIGHUP,SIG_DFL);
  112. signal(SIGINT,SIG_DFL);
  113. signal(SIGQUIT,SIG_DFL);
  114. signal(SIGPIPE,SIG_DFL);
  115. signal(SIGALRM,SIG_DFL);
  116. signal(SIGTERM,SIG_DFL);
  117. signal(SIGUSR1,SIG_DFL);
  118. signal(SIGUSR2,SIG_DFL);
  119. }
  120.  
  121. int have=0;
  122. static unsigned char havec;
  123. static int yep;
  124.  
  125. static dosig()
  126. {
  127. yep=1;
  128. }
  129.  
  130. aflush()
  131. {
  132. if(obufp)
  133.  {
  134.  struct itimerval a,b;
  135.  unsigned long usec=obufp*ccc;
  136.  if(usec>=500000/HZ)
  137.   {
  138.   a.it_value.tv_sec=usec/1000000;
  139.   a.it_value.tv_usec=usec%1000000;
  140.   a.it_interval.tv_usec=0;
  141.   a.it_interval.tv_sec=0;
  142.   signal(SIGALRM,dosig);
  143.   yep=0;
  144.   sigsetmask(sigmask(SIGALRM));
  145.   setitimer(ITIMER_REAL,&a,&b);
  146.   write(fileno(stdout),obuf,obufp);
  147.   while(!yep) sigpause(0);
  148.   signal(SIGALRM,SIG_DFL);
  149.   }
  150.  else write(fileno(stdout),obuf,obufp);
  151.  obufp=0;
  152.  }
  153. if(!have)
  154.  {
  155.  fcntl(fileno(stdin),F_SETFL,FNDELAY);
  156.  if(read(fileno(stdin),&havec,1)==1) have=1;
  157.  fcntl(fileno(stdin),F_SETFL,0);
  158.  }
  159. }
  160.  
  161. anext()
  162. {
  163. aflush();
  164. if(have) have=0;
  165. else if(read(fileno(stdin),&havec,1)<1) tsignal();
  166. return havec;
  167. }
  168.  
  169. eputc(c)
  170. unsigned char c;
  171. {
  172. obuf[obufp++]=c;
  173. if(obufp==obufsiz) aflush();
  174. }
  175.  
  176. eputs(s)
  177. char *s;
  178. {
  179. while(*s)
  180.  {
  181.  obuf[obufp++]= *(s++);
  182.  if(obufp==obufsiz) aflush();
  183.  }
  184. }
  185.  
  186. getsize()
  187. {
  188. #ifdef TIOCGSIZE
  189. struct ttysize getit;
  190. #else
  191. #ifdef TIOCGWINSZ
  192. struct winsize getit;
  193. #endif
  194. #endif
  195. #ifdef TIOCGSIZE
  196. if(ioctl(fileno(stdout),TIOCGSIZE,&getit)!= -1)
  197.  {
  198.  if(getit.ts_lines>=3) height=getit.ts_lines;
  199.  if(getit.ts_cols>=2) width=getit.ts_cols;
  200.  }
  201. #else
  202. #ifdef TIOCGWINSZ
  203. if(ioctl(fileno(stdout),TIOCGWINSZ,&getit)!= -1)
  204.  {
  205.  if(getit.ws_row>=3) height=getit.ws_row;
  206.  if(getit.ws_col>=2) width=getit.ws_col;
  207.  }
  208. #endif
  209. #endif
  210. }
  211.  
  212. termtype()
  213. {
  214. unsigned char entry[1024];
  215. unsigned char area[1024];
  216. unsigned char *foo=area;
  217. unsigned char *x=(unsigned char *)getenv("TERM");
  218. if(!x) goto down;
  219. if(tgetent(entry,x)!=1) goto down;
  220. height=tgetnum("li");
  221. if(height<3) height=24;
  222. width=tgetnum("co");
  223. if(width<2) width=80;
  224. if(!tgetstr("cs",&foo)) scroll=0;
  225. down:
  226. getsize();
  227. }
  228.  
  229. shell(s)
  230. char *s;
  231. {
  232. aclose();
  233. if(fork()) wait(0);
  234. else
  235.  {
  236.  execl(s,s,0);
  237.  _exit(0);
  238.  }
  239. aopen();
  240. }
  241.