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

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