home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d034 / less.lha / Less / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-09-03  |  2.7 KB  |  153 lines

  1. /*  io.c */
  2.  
  3. #include <ctype.h>
  4.  
  5. /*
  6.  * Name:    MicroEMACS
  7.  *        AmigaDOS terminal I/O
  8.  * Version:    31
  9.  * Compiler:    Manx Aztec C
  10.  * Created:    19-Apr-86 ...!ihnp4!seismo!ut-sally!ut-ngp!mic
  11.  */
  12. #include    <libraries/dos.h>
  13. #include    <libraries/dosextens.h>
  14. #undef TRUE
  15. #undef FALSE
  16.  
  17. #define    NIBUF    128        /* Probably excessive.        */
  18. #define    NOBUF    512        /* Not too big for 750/730.    */
  19.  
  20. struct    FileHandle    *tty;
  21. struct    FileHandle    *Open();
  22. char    obuf[NOBUF];    /* Output buffer        */
  23. int    nobuf;                /* # of bytes in above        */
  24. char    ibuf[NIBUF];    /* Input buffer            */
  25. int    nibuf;                /* # of bytes in above        */
  26. int    nrow = 0;                /* Terminal size, rows.        */
  27. int    ncol;                /* Terminal size, columns.    */
  28.  
  29. #if    MANX
  30. extern    int    Enable_Abort;
  31. #endif
  32. extern char version[];
  33.  
  34. /*
  35.  * This routine gets called once, to set up the
  36.  * terminal channel.
  37.  */
  38. ttopen()
  39. {
  40.     char WindowName[80];
  41.  
  42.     if(nrow) return;
  43.     
  44.     nrow = 23;
  45.     ncol = 77;
  46.     nobuf = nibuf = 0;
  47. #if    MANX
  48.     Enable_Abort = 0;    /* Disable ^C during file I/O */
  49. #endif
  50.     strcpy(WindowName,"RAW:1/1/639/199/");
  51.     strcat(WindowName, version);
  52.     tty = Open(WindowName, MODE_NEWFILE);
  53.     if (tty == (struct FileHandle *) 0) {
  54.         printf("Can't open window!\n");
  55.         exit(200);
  56.     }
  57. }
  58.  
  59. /*
  60.  * This function gets called just
  61.  * before we go back home to the command interpreter.
  62.  * On the Amiga it closes up the virtual terminal window.
  63.  */
  64. ttclose()
  65. {
  66.     if (tty != (struct FileHandle *) 0L) {
  67.         ttflush();
  68.         Close(tty);
  69.     }
  70.     tty = /*(struct FileHandle *)*/ NULL;
  71. #if    MANX
  72.     Enable_Abort = 1;
  73. #endif
  74. }
  75.  
  76. /*
  77.  * Write a character to the display.
  78.  * On the Amiga, terminal output is buffered, and
  79.  * we just put the characters in the big array,
  80.  * after cheching for overflow.
  81.  */
  82. ttputc(c)
  83. {
  84.     if (nobuf >= NOBUF)
  85.         ttflush();
  86.     obuf[nobuf++] = c;
  87. }
  88.  
  89. /*
  90.  * This function does the real work of
  91.  * flushing out buffered I/O on the Amiga. All
  92.  * we do is blast out the block with a write call.
  93.  */
  94. ttflush()
  95. {
  96.     if (nobuf > 0) {
  97.         Write(tty,obuf,(long) nobuf);
  98.         nobuf = 0;
  99.     }
  100. }
  101.  
  102. /*
  103.  * Read a character from the terminal,
  104.  * performing no editing and doing conditional echo 
  105.  */
  106. int do_echo = 1; /* echo flag */
  107.  
  108. ttgetc()
  109. {
  110.     unsigned char c, ignore;    /* must be unsigned! */
  111.  
  112.     ttflush();
  113.  
  114.     Read(tty,&c,1L);
  115.     if (c == '\x9b') {
  116.         Read(tty, &c, 1L);
  117.  
  118.         /* was it a function key  */
  119.         if (isdigit(c) || c == '?')
  120.             Read(tty, &ignore, 1L);
  121.  
  122.         /* return the char with top bit set */
  123.         c |= 0x80;
  124.     } else 
  125.         if (do_echo) 
  126.             ttputc(c);
  127.  
  128.     return ((int) c);
  129. }
  130.  
  131. /*
  132.  * Write a string to the terminal 
  133.  */
  134. ttputs(s)
  135. char *s;
  136. {
  137.     while(*s) ttputc(*s++);
  138.     ttflush();
  139. }
  140.  
  141. /* fake termcap output */
  142. tputs(s, ignore_heigth, ignore_func)
  143. char *s;
  144. int ignore_heigth, ignore_func;
  145. {
  146.     if(nrow == 0)
  147.         ttopen();
  148.  
  149.     flush();
  150.     while(*s) ttputc(*s++);
  151.     ttflush();
  152. }
  153.