home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / forth / pfe-0.000 / pfe-0 / pfe-0.9.13 / src / term-wat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  6.4 KB  |  334 lines

  1. /*
  2.  * This file is part of the portable Forth environment written in ANSI C.
  3.  * Copyright (C) 1995  Dirk Uwe Zoller
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  * See the GNU Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * This file is version 0.9.13 of 17-July-95
  20.  * Check for the latest version of this package via anonymous ftp at
  21.  *    roxi.rz.fht-mannheim.de:/pub/languages/forth/pfe-VERSION.tar.gz
  22.  * or    sunsite.unc.edu:/pub/languages/forth/pfe-VERSION.tar.gz
  23.  * or    ftp.cygnus.com:/pub/forth/pfe-VERSION.tar.gz
  24.  *
  25.  * Please direct any comments via internet to
  26.  *    duz@roxi.rz.fht-mannheim.de.
  27.  * Thank You.
  28.  */
  29. /*
  30.  * term-wat.c ---    Terminal driver for Watcom C, uses functions from
  31.  *            conio.h and ANSI escape sequences for attributes.
  32.  * (duz 24Feb94)
  33.  */
  34.  
  35. #include "forth.h"
  36. #include "term.h"
  37. #include <conio.h>
  38.  
  39. #if defined WC_OS2V2        /* __OS2__ should do here but doesn't */
  40. # define INCL_KBD 1
  41. # define INCL_VIO 1
  42. # include <string.h>
  43. # include <os2.h>
  44. #elif defined WC_DOS4G
  45. # include <dos.h>
  46. #elif defined WC_WINDOWS
  47. # include <stdio.h>
  48. #endif
  49.  
  50.  
  51. char *
  52. rawkey_string [NO_OF_KEYS] =    /* what function keys send */
  53. {
  54.   "\377;", "\377<", "\377=", "\377>", "\377?",
  55.   "\377@", "\377A", "\377B", "\377C", "\377D",
  56.   "\377K", "\377M", "\377H", "\377P",
  57.   "\377G", "\377O", "\377Q", "\377I",
  58.   NULL,       "\377S", NULL,    "\377R",
  59.   NULL,    NULL,    NULL,    NULL,    /*"\r"*/
  60. };
  61.  
  62. int tty_interrupt_key (char ch)        { return 0; }
  63. void interactive_terminal (void)    {}
  64. void system_terminal (void)        {}
  65. void query_winsize (void)        {}
  66.  
  67. #if defined WC_DOS4G
  68. static int page;        /* display screen, 0-7 */
  69. #endif
  70.  
  71. int
  72. prepare_terminal (void)
  73. {
  74. #if defined WC_OS2V2
  75.  
  76.   VIOMODEINFO viomi;
  77.   VioGetMode (&viomi, 0);
  78.   rows = viomi.row;
  79.   cols = viomi.col;
  80.   VioSetAnsi (1, 0);
  81.  
  82. #elif defined WC_DOS4G
  83.  
  84.   union REGS regs;
  85.   regs.h.ah = 15;        /* int 10 subfunction 15, get video mode */
  86.   int386 (0x10, ®s, ®s);
  87.   switch (regs.h.al)
  88.     {
  89.     default:
  90.       page = regs.h.bh;
  91.       rows = 25;
  92.       cols = regs.h.ah;
  93.     }
  94.  
  95. #endif
  96.  
  97.   return 1;
  98. }
  99.  
  100. int
  101. c_keypressed (void)
  102. {
  103. #if 0
  104.   KBDKEYINFO kbci;
  105.   KbdPeek (&kbci, 0);
  106.   return kbci.fbStatus >> 6 & 1;
  107. #else
  108.   return kbhit ();
  109. #endif
  110. }
  111.  
  112. int                /* return '\377' instead of DOS' '\0' */
  113. c_getkey (void)            /* for function keys. */
  114. {
  115.   int c;
  116. #if 0
  117.   KBDKEYINFO kbci;
  118.   KbdCharIn (&kbci, 0, 0);
  119.   c = kbci.chChar;
  120. #else
  121.   c = getch ();
  122. #endif
  123.   return (c &0xFF) == 0 ? '\377' : c;
  124. }
  125.  
  126. #if defined WC_OS2V2 && 0
  127.   /* the following gives an internal compiler error */
  128. # define CPUTC(C) VioWrtTTY (&(C), 1, 0)
  129. # define CPUTS(S) VioWrtTTY (S, (strlen (S)), 0)
  130. #elif defined WC_WINDOWS
  131. # define CPUTC(C) putchar (C)
  132. # define CPUTS(S) fputs (S, stdout)
  133. #else
  134. # define CPUTC(C) putch (C)
  135. # define CPUTS(S) cputs (S)
  136. #endif
  137.  
  138. void
  139. c_putc_noflush (char c)
  140. {
  141.   switch (c)
  142.     {
  143.     case '\n':
  144.       CPUTS ("\r\n");
  145.       break;
  146.     default:
  147.       CPUTC (c);
  148.       break;
  149.     }
  150. }
  151.  
  152. void
  153. c_flush (void)
  154. {
  155. #if defined WC_WINDOWS
  156.   fflush (stdout);
  157. #endif
  158. }
  159.  
  160. void
  161. c_putc (char c)
  162. {
  163.   c_putc_noflush (c);
  164.   c_flush ();
  165. }
  166.  
  167. void
  168. c_puts (const char *s)
  169. {
  170.   while (*s)
  171.     c_putc_noflush (*s++);
  172.   c_flush ();
  173. }
  174.  
  175. void
  176. c_gotoxy (int x, int y)
  177. {
  178. #if defined WC_OS2V2
  179.  
  180.   VioSetCurPos (y, x, 0);
  181.  
  182. #elif defined WC_DOS4G
  183.  
  184.   union REGS regs;
  185.   regs.h.ah = 2;        /* int 10 subfunction 2, set cursor pos */
  186.   regs.h.bh = page;        /* logical screen */
  187.   regs.h.dl = x;
  188.   regs.h.dh = y;
  189.   int386 (0x10, ®s, ®s);
  190.  
  191. #else
  192.  
  193.   cprintf ("\033[%d;%dH", y, x);
  194.  
  195. #endif
  196. }
  197.  
  198. void
  199. c_wherexy (int *x, int *y)
  200. {
  201. #if defined WC_OS2V2
  202.  
  203.   USHORT row, col;
  204.   VioGetCurPos (&row, &col, 0);
  205.   *x = col;
  206.   *y = row;
  207.  
  208. #elif defined WC_DOS4G
  209.  
  210.   union REGS regs;
  211.   regs.h.ah = 3;        /* int 10 subfunction 3, get cursor pos */
  212.   regs.h.bh = page;        /* logical screen */
  213.   int386 (0x10, ®s, ®s);
  214.   *x = regs.h.dl;
  215.   *y = regs.h.dh;
  216.  
  217. #else
  218.  
  219.   *x = *y = 0;            /* uargh! */
  220.  
  221. #endif
  222. }
  223.  
  224. static void            /* move cursor in x and y */
  225. addxy (int dx, int dy)
  226. {
  227.   int x, y;
  228.   c_wherexy (&x, &y);
  229.   x += dx;
  230.   y += dy;
  231.   c_gotoxy (x, y);
  232.   OUT = x;
  233. }
  234.  
  235. void c_goleft (void)        { addxy (-1,  0); }
  236. void c_goright (void)        { addxy ( 1,  0); }
  237. void c_goup (void)        { addxy ( 0, -1); }
  238. void c_godown (void)        { addxy ( 0,  1); }
  239.  
  240. void c_home (void)        { c_gotoxy (0, 0); }
  241. void c_clrscr (void)        { c_home (); c_clrdown (); }
  242. void c_bell (void)        { CPUTS ("\a"); }
  243.  
  244. void
  245. c_clreol (void)
  246. {
  247. #if defined WC_OS2V2
  248.  
  249.   static char cell [2] = { ' ', 0x07 };
  250.   USHORT row, col;
  251.   VioGetCurPos (&row, &col, 0);
  252.   VioWrtNCell (cell, cols - col, row, col, 0);
  253.  
  254. #else
  255.  
  256.   CPUTS ("\033[K");
  257.  
  258. #endif
  259. }
  260.  
  261. void
  262. c_clrdown (void)
  263. {
  264. #if defined WC_OS2V2
  265.  
  266.   static char cell [2] = { ' ', 0x07 };
  267.   int r;
  268.   USHORT row, col;
  269.  
  270.   VioGetCurPos (&row, &col, 0);
  271.   VioWrtNCell (cell, cols - col, row, col, 0);
  272.   for (r = row + 1; r < rows; r++)
  273.     VioWrtNCell (cell, cols, r, 0, 0);
  274.  
  275. #else /*if defined WC_DOS4G */
  276.  
  277.   int x, y, i;
  278.  
  279.   c_clreol ();
  280.   c_wherexy (&x, &y);
  281.   for (i = y + 1; i < rows; i++)
  282.     {
  283.       c_gotoxy (i, 0);
  284.       c_clreol ();
  285.     }
  286.   c_gotoxy (x, y);
  287.  
  288. #endif
  289. }
  290.  
  291. enum {
  292.   none, bold, faint, italic, blink = 5,
  293.   rapid_blink, reverse_video, concealed
  294. };
  295.  
  296. static int attrib;
  297.  
  298. static void
  299. setattr (int attr)
  300. {
  301.   if (attr == none)
  302.     {
  303.       attrib = 0;
  304.       CPUTS ("\033[0m");
  305.     }
  306.   else
  307.     {
  308.       attrib |= 1 << attr;
  309.       cprintf ("\033[%dm", attr);
  310.     }
  311. }
  312.  
  313. static void
  314. clrattr (int attr)
  315. {
  316.   int i;
  317.  
  318.   attrib &= ~(1 << attr);
  319.   CPUTS ("\033[0");
  320.   for (i = bold; i <= concealed; i++)
  321.     if (attrib >> i & 1)
  322.       cprintf (";%d", i);
  323.   CPUTS ("m");
  324. }
  325.  
  326. void c_standout_on (void)    { setattr (bold); }
  327. void c_standout_off (void)    { clrattr (bold); }
  328. void c_reverse (void)        { setattr (reverse_video); }
  329. void c_bright (void)        { setattr (bold); }
  330. void c_blinking (void)        { setattr (blink); }
  331. void c_normal (void)        { setattr (none); }
  332. void c_underline_on (void)    { setattr (italic); }
  333. void c_underline_off (void)    { clrattr (italic); }
  334.