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

  1. /* Terminal interface for POSIX
  2.    Copyright (C) 1991 Joseph H. Allen
  3.    (Contributed by Mike Lijewski)
  4.  
  5. This file is part of JOE (Joe's Own Editor)
  6.  
  7. JOE is free software; you can redistribute it and/or modify it under the terms
  8. of the GNU General Public License as published by the Free Software
  9. Foundation; either version 1, or (at your option) any later version.  
  10.  
  11. JOE is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  13. A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  
  14.  
  15. You should have received a copy of the GNU General Public License along with
  16. JOE; see the file COPYING.  If not, write to the Free Software Foundation, 675
  17. Mass Ave, Cambridge, MA 02139, USA.  */ 
  18.  
  19. #include <stdio.h>
  20. #include <signal.h>
  21. #include <fcntl.h>
  22. #include <sys/time.h>
  23. #include <sys/param.h>
  24. #include <termios.h>
  25. #include <unistd.h>
  26. #include "async.h"
  27.  
  28. #define DIVISOR 12000000
  29. #define TIMES 2
  30.  
  31. static struct termios oldterm;
  32.  
  33. static unsigned char *obuf=0;
  34. static unsigned obufp=0;
  35. static unsigned obufsiz;
  36. static unsigned long ccc;
  37.  
  38. static speed_t speeds[]=
  39. {
  40. B50,50,B75,75,B110,110,B134,134,B150,150,B200,200,B300,300,B600,600,B1200,1200,
  41. B1800,1800,B2400,2400,B4800,4800,B9600,9600,EXTA,19200,EXTB,38400,B19200,19200,
  42. B38400,38400
  43. };
  44.  
  45. aopen()
  46. {
  47. int x;
  48. struct sigaction action;
  49. speed_t baud;
  50. struct termios newterm;
  51. fflush(stdout);
  52. tcdrain(STDOUT_FILENO);
  53. sigemptyset(&action.sa_mask);
  54. action.sa_handler=tsignal;
  55. sigaction(SIGHUP,&action,NULL);
  56. sigaction(SIGTERM,&action,NULL);
  57. action.sa_handler=SIG_IGN;
  58. sigaction(SIGINT,&action,NULL);
  59. sigaction(SIGQUIT,&action,NULL);
  60. sigaction(SIGPIPE,&action,NULL);
  61. sigaction(SIGALRM,&action,NULL);
  62. sigaction(SIGUSR1,&action,NULL);
  63. sigaction(SIGUSR2,&action,NULL);
  64. tcgetattr(STDIN_FILENO,&oldterm);
  65. newterm=oldterm;
  66. newterm.c_lflag=0;
  67. newterm.c_iflag&=~(ICRNL|IGNCR);
  68. newterm.c_oflag=0;
  69. /* we needn't worry about special characters since SIGS are disabled */
  70. newterm.c_cc[VMIN]=1;
  71. newterm.c_cc[VTIME]=0;
  72. tcsetattr(STDIN_FILENO,TCSANOW,&newterm);
  73. ccc=0;
  74. baud=cfgetospeed(&newterm);
  75. for(x=0;x!=34;x+=2)
  76.  if(baud==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. struct sigaction action;
  97. aflush();
  98. tcsetattr(STDIN_FILENO,TCSANOW,&oldterm);
  99. sigemptyset(&action.sa_mask);
  100. action.sa_handler=SIG_DFL;
  101. sigaction(SIGHUP,&action,NULL);
  102. sigaction(SIGINT,&action,NULL);
  103. sigaction(SIGQUIT,&action,NULL);
  104. sigaction(SIGPIPE,&action,NULL);
  105. sigaction(SIGALRM,&action,NULL);
  106. sigaction(SIGTERM,&action,NULL);
  107. sigaction(SIGUSR1,&action,NULL);
  108. sigaction(SIGUSR2,&action,NULL);
  109. }
  110.  
  111. int have=0;
  112. static unsigned char havec;
  113. static int yep;
  114.  
  115. static dosig()
  116. {
  117. yep=1;
  118. }
  119.  
  120. aflush()
  121. {
  122. if(obufp)
  123.  {
  124.  struct itimerval a,b;
  125.  unsigned long usec=obufp*ccc;
  126.  if(usec>=500000/HZ)
  127.   {
  128.   a.it_value.tv_sec=usec/1000000;
  129.   a.it_value.tv_usec=usec%1000000;
  130.   a.it_interval.tv_usec=0;
  131.   a.it_interval.tv_sec=0;
  132.   signal(SIGALRM,dosig);
  133.   yep=0;
  134.   sigsetmask(sigmask(SIGALRM));
  135.   setitimer(ITIMER_REAL,&a,&b);
  136.   write(fileno(stdout),obuf,obufp);
  137.   while(!yep) sigpause(0);
  138.   signal(SIGALRM,SIG_DFL);
  139.   }
  140.  else write(fileno(stdout),obuf,obufp);
  141.  obufp=0;
  142.  }
  143. if(!have)
  144.  {
  145.  fcntl(STDIN_FILENO,F_SETFL,O_NDELAY);
  146.  if(read(STDIN_FILENO,&havec,1)==1) have=1;
  147.  fcntl(STDIN_FILENO,F_SETFL,0);
  148.  }
  149. }
  150.  
  151. anext()
  152. {
  153. aflush();
  154. if(have) have=0;
  155. else if(read(STDIN_FILENO,&havec,1)<1) tsignal();
  156. return havec;
  157. }
  158.  
  159. eputc(c)
  160. unsigned char c;
  161. {
  162. obuf[obufp++]=c;
  163. if(obufp==obufsiz) aflush();
  164. }
  165.  
  166. eputs(s)
  167. char *s;
  168. {
  169. while(*s)
  170.  {
  171.  obuf[obufp++]= *(s++);
  172.  if(obufp==obufsiz) aflush();
  173.  }
  174. }
  175.  
  176. getsize()
  177. {
  178. #ifdef TIOCGSIZE
  179. struct ttysize getit;
  180. #else
  181. #ifdef TIOCGWINSZ
  182. struct winsize getit;
  183. #endif
  184. #endif
  185. #ifdef TIOCGSIZE
  186. if(ioctl(fileno(stdout),TIOCGSIZE,&getit)!= -1)
  187.  {
  188.  if(getit.ts_lines>=3) height=getit.ts_lines;
  189.  if(getit.ts_cols>=2) width=getit.ts_cols;
  190.  }
  191. #else
  192. #ifdef TIOCGWINSZ
  193. if(ioctl(fileno(stdout),TIOCGWINSZ,&getit)!= -1)
  194.  {
  195.  if(getit.ws_row>=3) height=getit.ws_row;
  196.  if(getit.ws_col>=2) width=getit.ws_col;
  197.  }
  198. #endif
  199. #endif
  200. }
  201.  
  202. termtype()
  203. {
  204. unsigned char entry[1024];
  205. unsigned char area[1024];
  206. unsigned char *foo=area;
  207. unsigned char *x=(unsigned char *)getenv("TERM");
  208. if(!x) goto down;
  209. if(tgetent(entry,x)!=1) goto down;
  210. height=tgetnum("li");
  211. if(height<3) height=24;
  212. width=tgetnum("co");
  213. if(width<2) width=80;
  214. if(!tgetstr("cs",&foo)) scroll=0;
  215. down:
  216. getsize();
  217. }
  218.  
  219. shell(s)
  220. char *s;
  221. {
  222. aclose();
  223. if(fork()) wait(0);
  224. else
  225.  {
  226.  execl(s,s,0);
  227.  _exit(0);
  228.  }
  229. aopen();
  230. }
  231.