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

  1. /* Terminal interface for ESIX
  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 <stdio.h>
  19. #include <signal.h>
  20. #include <fcntl.h>
  21. #include <sys/time.h>
  22. #include <sys/param.h>
  23. #include <termio.h>
  24. #include "async.h"
  25.  
  26. struct winsize
  27. {
  28. unsigned short ws_row;
  29. unsigned short ws_col;
  30. unsigned short ws_xpixel;
  31. unsigned short ws_ypixel;
  32. };
  33.  
  34. #define DIVISOR 12000000
  35. #define TIMES 2
  36.  
  37. static struct termio oldterm;
  38.  
  39. static unsigned char *obuf=0;
  40. static unsigned obufp=0;
  41. static unsigned obufsiz;
  42. static unsigned long ccc;
  43.  
  44. static unsigned speeds[]=
  45. {
  46. B50,50,B75,75,B110,110,B134,134,B150,150,B200,200,B300,300,B600,600,B1200,1200,
  47. B1800,1800,B2400,2400,B4800,4800,B9600,9600,EXTA,19200,EXTB,38400
  48. };
  49.  
  50. aopen()
  51. {
  52. int x;
  53. struct termio newterm;
  54. fflush(stdout);
  55. signal(SIGHUP,tsignal);
  56. signal(SIGINT,SIG_IGN);
  57. signal(SIGQUIT,SIG_IGN);
  58. signal(SIGPIPE,SIG_IGN);
  59. signal(SIGALRM,SIG_IGN);
  60. signal(SIGTERM,tsignal);
  61. signal(SIGUSR1,SIG_IGN);
  62. signal(SIGUSR2,SIG_IGN);
  63. signal(SIGPWR,tsignal);
  64. ioctl(fileno(stdin),TCGETA,&oldterm);
  65. newterm=oldterm;
  66. newterm.c_lflag=0;
  67. newterm.c_iflag&=~(ICRNL|IGNCR);
  68. newterm.c_oflag=0;
  69. newterm.c_cc[VINTR]= -1;
  70. newterm.c_cc[VQUIT]= -1;
  71. newterm.c_cc[VMIN]=1;
  72. newterm.c_cc[VTIME]=0;
  73. ioctl(fileno(stdin),TCSETAW,&newterm);
  74. ccc=0;
  75. for(x=0;x!=30;x+=2)
  76.  if((newterm.c_cflag&CBAUD)==speeds[x])
  77.   {
  78.   ccc=DIVISOR/speeds[x+1];
  79.   break;
  80.   }
  81. if(!obuf)
  82.  {
  83.  if(!(TIMES*ccc)) obufsiz=4096;
  84.  else
  85.   {
  86.   obufsiz=1000000/(TIMES*ccc);
  87.   if(obufsiz>4096) obufsiz=4096;
  88.   }
  89.  if(!obufsiz) obufsiz=1;
  90.  obuf=(unsigned char *)malloc(obufsiz);
  91.  }
  92. }
  93.  
  94. aclose()
  95. {
  96. aflush();
  97. ioctl(fileno(stdin),TCSETAW,&oldterm);
  98. signal(SIGHUP,SIG_DFL);
  99. signal(SIGINT,SIG_DFL);
  100. signal(SIGQUIT,SIG_DFL);
  101. signal(SIGPIPE,SIG_DFL);
  102. signal(SIGALRM,SIG_DFL);
  103. signal(SIGTERM,SIG_DFL);
  104. signal(SIGUSR1,SIG_DFL);
  105. signal(SIGUSR2,SIG_DFL);
  106. signal(SIGPWR,SIG_DFL);
  107. }
  108.  
  109. int have=0;
  110. static unsigned char havec;
  111. static int yep;
  112.  
  113. static dosig()
  114. {
  115. yep=1;
  116. }
  117.  
  118. aflush()
  119. {
  120. if(obufp)
  121.  {
  122.  struct itimerval a,b;
  123.  unsigned long usec=obufp*ccc;
  124.  if(usec>=500000/HZ)
  125.   {
  126.   a.it_value.tv_sec=usec/1000000;
  127.   a.it_value.tv_usec=usec%1000000;
  128.   a.it_interval.tv_usec=0;
  129.   a.it_interval.tv_sec=0;
  130.   signal(SIGALRM,dosig);
  131.   yep=0;
  132.   sigsetmask(sigmask(SIGALRM));
  133.   setitimer(ITIMER_REAL,&a,&b);
  134.   write(fileno(stdout),obuf,obufp);
  135.   while(!yep) sigpause(0);
  136.   signal(SIGALRM,SIG_DFL);
  137.   }
  138.  else write(fileno(stdout),obuf,obufp);
  139.  obufp=0;
  140.  }
  141. if(!have)
  142.  {
  143.  fcntl(fileno(stdin),F_SETFL,O_NDELAY);
  144.  if(read(fileno(stdin),&havec,1)==1) have=1;
  145.  fcntl(fileno(stdin),F_SETFL,0);
  146.  }
  147. }
  148.  
  149. anext()
  150. {
  151. aflush();
  152. if(have) have=0;
  153. else if(read(fileno(stdin),&havec,1)<1) tsignal();
  154. return havec;
  155. }
  156.  
  157. eputc(c)
  158. unsigned char c;
  159. {
  160. obuf[obufp++]=c;
  161. if(obufp==obufsiz) aflush();
  162. }
  163.  
  164. eputs(s)
  165. char *s;
  166. {
  167. while(*s)
  168.  {
  169.  obuf[obufp++]= *(s++);
  170.  if(obufp==obufsiz) aflush();
  171.  }
  172. }
  173.  
  174. getsize()
  175. {
  176. #ifdef TIOCGSIZE
  177. struct ttysize getit;
  178. #else
  179. #ifdef TIOCGWINSZ
  180. struct winsize getit;
  181. #endif
  182. #endif
  183. #ifdef TIOCGSIZE
  184. if(ioctl(fileno(stdout),TIOCGSIZE,&getit)!= -1)
  185.  {
  186.  if(getit.ts_lines>=3) height=getit.ts_lines;
  187.  if(getit.ts_cols>=2) width=getit.ts_cols;
  188.  }
  189. #else
  190. #ifdef TIOCGWINSZ
  191. if(ioctl(fileno(stdout),TIOCGWINSZ,&getit)!= -1)
  192.  {
  193.  if(getit.ws_row>=3) height=getit.ws_row;
  194.  if(getit.ws_col>=2) width=getit.ws_col;
  195.  }
  196. #endif
  197. #endif
  198. }
  199.  
  200. termtype()
  201. {
  202. unsigned char entry[1024];
  203. unsigned char area[1024];
  204. unsigned char *foo=area;
  205. unsigned char *x=(unsigned char *)getenv("TERM");
  206. if(!x) goto down;
  207. if(tgetent(entry,x)!=1) goto down;
  208. height=tgetnum("li");
  209. if(height<3) height=24;
  210. width=tgetnum("co");
  211. if(width<2) width=80;
  212. if(!tgetstr("cs",&foo)) scroll=0;
  213. down:
  214. getsize();
  215. }
  216.  
  217. shell(s)
  218. char *s;
  219. {
  220. aclose();
  221. if(fork()) wait(0);
  222. else
  223.  {
  224.  execl(s,s,0);
  225.  _exit(0);
  226.  }
  227. aopen();
  228. }
  229.