home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / misc / b186_1 / Source / c / io < prev    next >
Text File  |  1994-02-15  |  6KB  |  313 lines

  1. /*
  2.  
  3.        This file is part of the PDP software package.
  4.          
  5.        Copyright 1987 by James L. McClelland and David E. Rumelhart.
  6.        
  7.        Please refer to licensing information in the file license.txt,
  8.        which is in the same directory with this source file and is
  9.        included here by reference.
  10. */
  11.  
  12.  
  13. /* Low Level io
  14.  
  15.  
  16.     Generic low level I/O based on curses.
  17.     Use only on an IBM/PC/XT/AT.
  18.     On a real machine, use the curses library instead.
  19.  
  20.     First version implemented by Elliot Jaffe.
  21.     
  22.     Date of last revision:  8-12-87/JLM.
  23. */
  24.  
  25. /* the following commands are implemented from the standard
  26.    unix curses library:
  27.    
  28.    io_initscr()    initialize the terminal routines.  Must be called
  29.            before any other screen i/o is used.
  30.  
  31.    io_endwin()    reset the terminal to the state it was in before
  32.            calling io_initscr().
  33.  
  34.    io_clear()    io_clear the screen.
  35.    
  36.    io_clrtoeol()    io_clear from the current position to the end of line.
  37.    
  38.    io_move(line,col)    io_move the current position to line,col.
  39.    
  40.    io_standout()    do all further printing in reverse video.
  41.    
  42.    io_standend()    do all further printing in standard video.
  43.    
  44.    io_printw(fmt,arg)        print at the current screen position
  45.                    using standard printf formating.
  46.                 These must always be a format and a 
  47.                 single argument.
  48.    io_printw modified 6-Apr-87 by MAF: must be io_printw(fmt, arg1);
  49.    (i.e., io_printw must have exactly two arguments)
  50.  
  51.    All of these commands are defined by printing the required screen
  52.    commands directly to the PC screen.  For a different PC, the only
  53.    changes needed should be to the #define's of the screen commands.
  54. */
  55.  
  56. #include <stdio.h>
  57. #include "io.h"
  58. #ifdef MSDOS
  59. char    xxtemp[200];    /* added 6-Apr-87 by MAF; used by newprint(), below */
  60. int    io_type = IO_NORMAL;
  61. #endif
  62.  
  63. extern FILE *in_stream;
  64. extern int start_up;
  65. #ifdef CURSES
  66.  
  67. #include <curses.h>
  68.  
  69. io_initscr() {
  70.     initscr();
  71.     crmode();
  72.     noecho();
  73. }
  74.  
  75. io_endwin() {
  76.     if (start_up) return;
  77.     endwin();
  78.     nocrmode();
  79.     echo();
  80. }
  81.  
  82. io_addch(c) char c; {
  83.     if (c != '\b')
  84.       addch(c);
  85.     else{
  86.       mvdelch(getcury(stdscr),getcurx(stdscr)-1);
  87.     }
  88. }
  89.  
  90. io_clear() {
  91.     if (start_up) return;
  92.     clear();
  93. }
  94.  
  95. io_clrtoeol() {
  96.     if (start_up) return;
  97.     clrtoeol();
  98. }
  99.  
  100. io_move(line, col) {
  101.     if (start_up) return;
  102.     move(line, col);
  103. }
  104.  
  105. io_standout() {
  106.     if (start_up) return;
  107.     standout();
  108. }
  109.  
  110. io_standend() {
  111.     if (start_up) return;
  112.     standend();
  113. }
  114.  
  115. io_refresh() {
  116.     if (start_up) return;
  117.     refresh();
  118. }
  119.  
  120. io_inch() {
  121.     if (start_up) return;
  122.     return(inch());
  123. }
  124.  
  125. #else  (if not CURSES)
  126.  
  127. #ifdef MSDOS
  128. #include <dos.h>
  129. #define VIDEO_INTERRUPT    0x10
  130. #define PAGE        0
  131. #define SET_POSITION    2
  132. #define READ_POSITION    3
  133. #define WRITE_CHAR    9
  134. #define READ_CHAR    8
  135. #define SCROLL_UP    6
  136. #endif
  137.  
  138. io_initscr() {
  139. }
  140.  
  141. io_endwin() {
  142. }
  143.  
  144. io_clear() {
  145. #ifdef MSDOS
  146.     union REGS vregs;
  147.     
  148.     if (start_up) return;
  149.     
  150.     vregs.h.ah = SCROLL_UP;
  151.     vregs.h.al = 0; /* clear screen */
  152.     vregs.h.ch = 0;
  153.     vregs.h.cl = 0;
  154.     vregs.h.dh = 23;
  155.     vregs.h.dl = 79;
  156.     vregs.h.bh = IO_NORMAL;
  157.     
  158.     int86(VIDEO_INTERRUPT, &vregs, &vregs);
  159.  
  160. #else (if not MSDOS)
  161.  
  162.     if (start_up) return;
  163.     print(CLEARSCREEN);
  164.  
  165. #endif MSDOS        
  166. }
  167.  
  168. io_clrtoeol() {  /* clear to end of line */
  169. #ifdef MSDOS
  170.     static union REGS inregs1, outregs1, inregs2, outregs2;
  171.     static int row,col;
  172.     
  173.     if (start_up) return;
  174.     
  175.     /* first get current cursor position */
  176.     inregs2.h.ah = READ_POSITION;
  177.     inregs2.h.bh = PAGE;
  178.     int86(VIDEO_INTERRUPT, &inregs2, &outregs2);
  179.     row = outregs2.h.dh;
  180.     col = outregs2.h.dl;
  181.     
  182.     /* now set up inregs1 for the interrupt to output a space */
  183.     inregs1.h.ah = WRITE_CHAR;
  184.     inregs1.h.bh = PAGE;
  185.     inregs1.x.cx = 1;
  186.     inregs1.h.bl = IO_NORMAL;
  187.     inregs1.h.al = ' ';
  188.     
  189.     /* now set up inregs2 for the interrupt to move cursor right */
  190.     inregs2.h.ah = SET_POSITION;
  191.     inregs2.h.dh = row;
  192.     while (col < 80) {
  193.         /* write a blank space at column number col */
  194.     int86(VIDEO_INTERRUPT, &inregs1, &outregs1);
  195.     
  196.     /* move cursor right */
  197.     inregs2.h.dl = (++ col);
  198.     int86(VIDEO_INTERRUPT, &inregs2, &outregs2);
  199.     }
  200.  
  201. #else (if not MSDOS)
  202.  
  203.     if (start_up) return;
  204.     print(CLEARTOEOL);
  205.     
  206. #endif MSDOS
  207. }
  208.  
  209.  
  210. io_move(line, col) {
  211.  
  212. #ifdef MSDOS
  213.     /* cprintf("%s%d;%dH", MOVECURSOR, line + 1, col + 1);
  214.     Previous version used the above line for cursor movement.
  215.     This version uses a software interrupt, below.  -- 10-Mar-87 MAF */
  216.  
  217.     union REGS vregs;
  218.  
  219.     if (start_up) return;
  220.  
  221.     vregs.h.ah = SET_POSITION;
  222.     vregs.h.dh = line;
  223.     vregs.h.dl = col;
  224.     vregs.h.bh = PAGE;
  225.     
  226.     int86(VIDEO_INTERRUPT, &vregs, &vregs);
  227. #else
  228.     if (start_up) return;
  229.     printf("%s%c%c", MOVECURSOR,(char)(line + 32),(char)(col + 32));
  230. #endif MSDOS
  231. }
  232.  
  233.  
  234. io_standout() {
  235.     if (start_up) return;
  236. #ifdef MSDOS
  237.     io_type = IO_REVERSE; /* 6-Apr-87 MAF */
  238. #else if not MSDOS
  239.     print(STANDOUT);
  240. #endif
  241. }
  242.  
  243. io_standend() {
  244.     if (start_up) return;
  245. #ifdef MSDOS
  246.     io_type = IO_NORMAL; /* 6-Apr-87 MAF */
  247. #else if not MSDOS    
  248.     print(STANDEND);
  249. #endif MSDOS    
  250. }
  251.  
  252.  
  253. #ifdef MSDOS
  254. newprint(xx)
  255. char xx[];
  256. {
  257.     static int i, row, column;
  258.     extern int io_type;
  259.     static union REGS inregs1, outregs1, inregs2, outregs2;
  260.     
  261.     inregs1.h.ah = WRITE_CHAR;
  262.     inregs1.h.bh = PAGE;
  263.     inregs1.x.cx = 1; /* count of characters to write */
  264.     inregs1.h.bl = io_type; /* char attribute */
  265.     
  266.     inregs2.h.ah = READ_POSITION;
  267.     inregs2.h.bh = PAGE;
  268.     int86(VIDEO_INTERRUPT, &inregs2, &outregs2); /* read row & column */
  269.     row = outregs2.h.dh;
  270.     column = outregs2.h.dl;
  271.     
  272.     inregs2.h.ah = SET_POSITION;
  273.     inregs2.h.dh = row;
  274.     
  275.     i = -1;
  276.     while (xx[++i] != 0) {
  277.         inregs1.h.al = xx[i];
  278.     int86(VIDEO_INTERRUPT, &inregs1, &outregs1); /* print character */
  279.     
  280.     inregs2.h.dl = ++column;
  281.     int86(VIDEO_INTERRUPT, &inregs2, &outregs2); /* move cursor right */
  282.     }
  283. }
  284. #endif MSDOS
  285.  
  286. io_refresh() {
  287.     if (start_up) return;
  288.     fflush(stdout);
  289. }
  290.  
  291.  
  292. io_inch() {
  293.     
  294. #ifdef MSDOS
  295.     union REGS vregs;
  296.     
  297.     if (start_up) return;
  298.     
  299.     vregs.h.bh = PAGE;
  300.     vregs.h.ah = READ_CHAR;
  301.     
  302.     int86(VIDEO_INTERRUPT, &vregs, &vregs);
  303.     
  304.     return( (char) vregs.h.al );
  305. #else
  306.     if (start_up) return;
  307.     return('\0');
  308. #endif
  309. }
  310.  
  311. #endif  not CURSES
  312.  
  313.