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

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