home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / elvis184.zip / src / pc.c < prev    next >
C/C++ Source or Header  |  1994-03-26  |  11KB  |  483 lines

  1. /* pc.c */
  2.  
  3. /* Author:
  4.  *    Guntram Blohm
  5.  *    Buchenstrasse 19
  6.  *    7904 Erbach, West Germany
  7.  *    Tel. ++49-7305-6997
  8.  *    sorry - no regular network connection
  9.  */
  10.  
  11. /* This file implements the ibm pc bios interface. See IBM documentation
  12.  * for details.
  13.  * If TERM is set upon invocation of elvis, this code is ignored completely,
  14.  * and the standard termcap functions are used, thus, even not-so-close
  15.  * compatibles can run elvis. For close compatibles however, bios output
  16.  * is much faster (and permits reverse scrolling, adding and deleting lines,
  17.  * and much more ansi.sys isn't capable of). GB.
  18.  */
  19.  
  20. /* The routines for setting raw mode were written by Dan Kegel.  They were
  21.  * taken [with slight modifications] from the "raw.c" file that he distributes
  22.  * with his NANSI.SYS console driver.  Email: dank@moc.jpl.nasa.gov
  23.  */
  24.  
  25. #include "config.h"
  26. #include "vi.h"
  27.  
  28. #if MSDOS
  29.  
  30. #include <dos.h>
  31.  
  32. #if TURBOC
  33. #include <conio.h>
  34. #endif
  35.  
  36. static void video P_((int, int *, int *));
  37.  
  38. /* vmode contains the screen attribute index and is set by attrset.*/
  39.  
  40. int vmode;
  41.  
  42. /* The following array contains attribute definitions for
  43.  * color/monochrome attributes. Screen selects one of the sets.
  44.  * Maybe i'll put them into elvis options one day.
  45.  */
  46.  
  47. static int screen;
  48. static char attr[2][8] =
  49. {
  50.     /* :se:  :so:  :VB:  quit  :ul:  :as:  popup visible */
  51.     {  0x1f, 0x1d, 0x1e, 0x07, 0x1a, 0x1c, 0x2f, 0x3f}, /* color */
  52.     {  0x07, 0x70, 0x0f, 0x07, 0x01, 0x0f, 0x70, 0x70}, /* mono */
  53. };
  54.  
  55. /*
  56.  * bios interface functions for elvis - pc version
  57.  */
  58.  
  59. int biosquit()
  60. {
  61.     int    cx = 1;
  62.  
  63.     vmode = A_QUIT;
  64.     v_ce();
  65.     return TRUE;
  66. }
  67.  
  68.  
  69. /* This function changes the table of attribute bytes used during BIOS output.
  70.  */
  71. int bioscolor(mode, attrbyte)
  72.     int    mode;    /* e.g. A_NORMAL */
  73.     int    attrbyte;/* color code, as a PC attribute byte */
  74. {
  75.     attr[0][mode] = attrbyte;
  76.     return 0;
  77. }
  78.  
  79. /* IOCTL GETBITS/SETBITS bits. */
  80. #define DEVICE        0x80
  81. #define RAW        0x20
  82.  
  83. /* IOCTL operations */
  84. #define GETBITS        0
  85. #define SETBITS        1
  86. #define GETINSTATUS    6
  87.  
  88. /* DOS function numbers. */
  89. #define BREAKCHECK    0x33
  90. #define IOCTL        0x44
  91.  
  92. /* A nice way to call the DOS IOCTL function */
  93. static int
  94. elvis_ioctl(int handle, int mode, unsigned setvalue)
  95. {
  96.     union REGS regs;
  97.  
  98.     regs.h.ah = IOCTL;
  99.     regs.h.al = (char) mode;
  100.     regs.x.bx = handle;
  101.     regs.h.dl = (char) setvalue;
  102.     regs.h.dh = 0;            /* Zero out dh */
  103.     intdos(®s, ®s);
  104.     return (regs.x.dx);
  105. }
  106.  
  107.  
  108. /*--------------------------------------------------------------------------
  109.  Call this routine to set or clear RAW mode for the device associated with
  110.  the given file.
  111.  Example: raw_set(1, TRUE);
  112. --------------------------------------------------------------------------*/
  113. void raw_set(fd, rawstate)
  114.     int fd;
  115.     int rawstate;
  116. {
  117.     int bits;
  118.     bits = elvis_ioctl(fd, GETBITS, 0);
  119.     if (DEVICE & bits) {
  120.         if (rawstate)
  121.             bits |= RAW;
  122.         else
  123.             bits &= ~RAW;
  124.         (void) elvis_ioctl(fd, SETBITS, bits);
  125.     }
  126. }
  127.  
  128. /* A nice way to call the DOS BREAKCHECK function */
  129. static int breakctl(int mode, int setvalue)
  130. {
  131.     union REGS regs;
  132.  
  133.     regs.h.ah = BREAKCHECK;
  134.     regs.h.al = (char) mode;
  135.     regs.h.dl = (char) setvalue;
  136.     intdos(®s, ®s);
  137.     return (regs.x.dx & 0xff);
  138. }
  139.  
  140. /*--------------------------------------------------------------------------
  141.  Call this routine to determine whether DOS is checking for break (Control-C)
  142.  before it executes any DOS function call.
  143.  Return value is FALSE if it only checks before console I/O function calls,
  144.  TRUE if it checks before any function call.
  145. --------------------------------------------------------------------------*/
  146. int break_get(void)
  147. {
  148.     return ( 0 != breakctl(GETBITS, 0));
  149. }
  150.  
  151. /*--------------------------------------------------------------------------
  152.  Call this routine with TRUE to tell DOS to check for break (Control-C)
  153.  before it executes any DOS function call.
  154.  Call this routine with FALSE to tell DOS to only check for break before
  155.  it executes console I/O function calls.
  156. --------------------------------------------------------------------------*/
  157. void break_set(check)
  158.     int check;
  159. {
  160.     (void) breakctl(SETBITS, check);
  161. }
  162.  
  163. /*--------------------------------------------------------------------------
  164.  One routine to set (or clear) raw mode on stdin and stdout,
  165.  clear (or restore) break checking, and turn off input buffering on stdin.
  166.  This is the most common configuration; under MS-DOS, since setting raw mode
  167.  on stdout sometimes sets it on stdin, it's best to set it on both & be done
  168.  with it.
  169. --------------------------------------------------------------------------*/
  170. void raw_set_stdio(rawstate)
  171.     int rawstate;    /* TRUE -> set raw mode; FALSE -> clear raw mode */
  172. {
  173.     static int was_break_checking = 0;
  174.  
  175.     raw_set(0, rawstate);
  176.     raw_set(1, rawstate);
  177.     if (rawstate) {
  178.         was_break_checking = break_get();
  179.         break_set(0);
  180.     } else {
  181.         break_set(was_break_checking);
  182.     }
  183. }
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190. /* cursor up: determine current position, decrement row, set position */
  191.  
  192. void v_up()
  193. {
  194.     int dx;
  195.     video(0x300,(int *)0,&dx);
  196.     dx-=0x100;
  197.     video(0x200,(int *)0,&dx);
  198. }
  199.  
  200. #ifndef NO_CURSORSHAPE
  201. /* cursor big: set begin scan to end scan - 4 */
  202. void v_cb()
  203. {
  204.     int cx;
  205.     video(0x300, &cx, (int *)0);
  206.     cx=((cx&0xff)|(((cx&0xff)-4)<<8));
  207.     video(0x100, &cx, (int *)0);
  208. }
  209.  
  210. /* cursor small: set begin scan to end scan - 1 */
  211. void v_cs()
  212. {
  213.     int cx;
  214.     video(0x300, &cx, (int *)0);
  215.     cx=((cx&0xff)|(((cx&0xff)-1)<<8));
  216.     video(0x100, &cx, (int *)0);
  217. }
  218. #endif
  219.  
  220. /* clear to end: get cursor position and emit the aproppriate number
  221.  * of spaces, without moving cursor.
  222.  */
  223.  
  224. void v_ce()
  225. {
  226.     int cx, dx;
  227.     video(0x300,(int *)0,&dx);
  228.     cx=COLS-(dx&0xff);
  229.     video(0x920,&cx,(int *)0);
  230. }
  231.  
  232. /* clear screen: clear all and set cursor home */
  233.  
  234. void v_cl()
  235. {
  236.     int cx=0, dx=((LINES-1)<<8)+COLS-1;
  237.     video(0x0600,&cx,&dx);
  238.     dx=0;
  239.     video(0x0200,&cx,&dx);
  240. }
  241.  
  242. /* clear to bottom: get position, clear to eol, clear next line to end */
  243.  
  244. void v_cd()
  245. {
  246.     int cx, dx, dxtmp;
  247.     video(0x0300,(int *)0,&dx);
  248.     dxtmp=(dx&0xff00)|(COLS-1);
  249.     cx=dx;
  250.     video(0x0600,&cx,&dxtmp);
  251.     cx=(dx&0xff00)+0x100;
  252.     dx=((LINES-1)<<8)+COLS-1;
  253.     video(0x600,&cx,&dx);
  254. }
  255.  
  256. /* add line: scroll rest of screen down */
  257.  
  258. void v_al()
  259. {
  260.     int cx,dx;
  261.     video(0x0300,(int *)0,&dx);
  262.     cx=(dx&0xff00);
  263.     dx=((LINES-1)<<8)+COLS-1;
  264.     video(0x701,&cx,&dx);
  265. }
  266.  
  267. /* delete line: scroll rest up */
  268.  
  269. void v_dl()
  270. {
  271.     int cx,dx;
  272.     video(0x0300,(int *)0,&dx);
  273.     cx=(dx&0xff00)/*+0x100*/;
  274.     dx=((LINES-1)<<8)+COLS-1;
  275.     video(0x601,&cx,&dx);
  276. }
  277.  
  278. /* scroll reverse: scroll whole screen */
  279.  
  280. void v_sr()
  281. {
  282.     int cx=0, dx=((LINES-1)<<8)+COLS-1;
  283.     video(0x0701,&cx,&dx);
  284. }
  285.  
  286. /* set cursor */
  287.  
  288. void v_move(x,y)
  289.     int x, y;
  290. {
  291.     int dx=(y<<8)+x;
  292.     video(0x200,(int *)0,&dx);
  293. }
  294.  
  295. /* put character: set attribute first, then execute char.
  296.  * Also remember if current line has changed.
  297.  */
  298.  
  299. int v_put(ch)
  300.     int ch;
  301. {
  302.     int cx=1;
  303.     ch&=0xff;
  304.     if (ch>=' ')
  305.         video(0x900|ch,&cx,(int *)0);
  306.     video(0xe00|ch,(int *)0, (int *)0);
  307.     if (ch=='\n')
  308.     {    exwrote = TRUE;
  309.         video(0xe0d, (int *)0, (int *)0);
  310.     }
  311.     return ch;
  312. }
  313.  
  314. /* determine number of screen columns. Also set attrset according
  315.  * to monochrome/color screen.
  316.  */
  317.  
  318. int v_cols()
  319. {
  320.     union REGS regs;
  321.     regs.h.ah=0x0f;
  322.     int86(0x10, ®s, ®s);
  323.     if (regs.h.al==7)            /* monochrome mode ? */
  324.         screen=1;
  325.     else
  326.         screen=0;
  327.     return regs.h.ah;
  328. }
  329.  
  330. /* Getting the number of rows is hard. Most screens support 25 only,
  331.  * EGA/VGA also support 43/50 lines, and some OEM's even more.
  332.  * Unfortunately, there is no standard bios variable for the number
  333.  * of lines, and the bios screen memory size is always rounded up
  334.  * to 0x1000. So, we'll really have to cheat.
  335.  * When using the screen memory size, keep in mind that each character
  336.  * byte has an associated attribute byte.
  337.  *
  338.  * uses:    word at 40:4c contains    memory size
  339.  *        byte at 40:84         # of rows-1 (sometimes)
  340.  *        byte at    40:4a        # of columns
  341.  */
  342.  
  343. int v_rows()
  344. {
  345.     int line, oldline;
  346.  
  347.     /* screen size less then 4K? then we have 25 lines only */
  348.  
  349.     if (*(int far *)(0x0040004cl)<=4096)
  350.         return 25;
  351.  
  352.     /* VEGA vga uses the bios byte at 0x40:0x84 for # of rows.
  353.      * Use that byte, if it makes sense together with memory size.
  354.      */
  355.  
  356.     if ((((*(unsigned char far *)(0x0040004aL)*2*
  357.         (*(unsigned char far *)(0x00400084L)+1))+0xfff)&(~0xfff))==
  358.         *(unsigned int far *)(0x0040004cL))
  359.             return *(unsigned char far *)(0x00400084L)+1;
  360.  
  361.     /* uh oh. Emit '\n's until screen starts scrolling. */
  362.  
  363.     v_move(oldline=0, 0);
  364.     for (;;)
  365.     {
  366.         video(0xe0a,(int *)0,(int *)0);
  367.         video(0x300,(int *)0,&line);
  368.         line>>=8;
  369.         if (oldline==line)
  370.             return line+1;
  371.         oldline=line;    
  372.     }
  373. }
  374.  
  375. /* the REAL bios interface -- used internally only. */
  376.  
  377. static void video(ax, cx, dx)
  378.     int ax, *cx, *dx;
  379. {
  380.     union REGS regs;
  381.  
  382.     regs.x.ax=ax;
  383.     if ((ax&0xff00)==0x600 || (ax&0xff00)==0x700)
  384.         regs.h.bh=attr[screen][vmode];
  385.     else
  386.     {
  387.         regs.h.bh=0;
  388.         regs.h.bl=attr[screen][vmode];
  389.     }
  390.     if (cx) regs.x.cx=*cx;
  391.     if (dx) regs.x.dx=*dx;
  392.     int86(0x10, ®s, ®s);
  393.     if (dx) *dx=regs.x.dx;
  394.     if (cx) *cx=regs.x.cx;
  395. }
  396.  
  397. /* The following function determines which character is used for
  398.  * commandline-options by command.com. This system call is undocumented
  399.  * and valid for versions < 4.00 only.
  400.  */
  401.  
  402. int switchar()
  403. {
  404.     union REGS regs;
  405.     regs.x.ax=0x3700;
  406.     int86(0x21, ®s, ®s);
  407.     return regs.h.dl;
  408. }
  409.  
  410.  
  411. /* this function returns the DOS time, as a 32-bit long int representing
  412.  * hundredths of a second since midnight.  Some systems may be limited to
  413.  * a resolution of whole seconds, but the values will still represent
  414.  * hundredths.
  415.  */
  416. static long dostime P_((void))
  417. {
  418.     union REGS    regs;
  419.  
  420.     regs.h.ah = 0x2c;    /* MS-DOS "get time" service */
  421.     intdos(®s, ®s);
  422.     return (((regs.h.ch * 60L) + regs.h.cl) * 60L + regs.h.dh) * 100L + regs.h.dl;
  423. }
  424.  
  425.  
  426. /*ARGSUSED*/
  427. /* This function implements a raw read from the keyboard, with timeout. */
  428. int ttyread(buf, len, time)
  429.     char    *buf;    /* where to store the keystrokes */
  430.     int    len;    /* maximum number of characters to get -- ignored */
  431.     int    time;    /* maximum time to wait, in 1/9th second increments */
  432. {
  433.     long    stop;
  434.  
  435.     /* are we going to timeout? */
  436.     if (time != 0)
  437.     {
  438.         /* compute the time when we'll give up */
  439.         stop = dostime() + time * 10L;
  440.  
  441.         /* wait for either keystroke or timeout */
  442.         while (!kbhit())
  443.         {
  444.             if (dostime() > stop)
  445.             {
  446.                 /* we couldn't read any characters
  447.                  * before timeout
  448.                  */
  449.                 return 0;
  450.             }
  451.         }
  452.     }
  453.  
  454.     /* get a keystroke */
  455.     buf[0] = getch();
  456.     if (buf[0] == 0) /* function key? */
  457.     {
  458.         buf[0] = '#';
  459.         buf[1] = getch();
  460.         return 2;
  461.     }
  462.     else
  463.     {
  464.         return 1;
  465.     }
  466. }
  467.  
  468. #if !TURBOC 
  469. /* Turboc provides sleep, declared as void sleep(unsigned seconds) */
  470. int sleep(seconds)
  471.     unsigned seconds;
  472. {
  473.     long    stop;
  474.  
  475.     stop = dostime() + 100L * seconds;
  476.     while (dostime() < stop)
  477.     {
  478.     }
  479.     return 0;
  480. }
  481. #endif
  482. #endif
  483.