home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / borland.c < prev    next >
C/C++ Source or Header  |  1998-05-14  |  10KB  |  563 lines

  1. /*
  2.  * Uses Borland console i/o routines.
  3.  * (Created for OS/2 by Charles Moschel 29-MAR-94.)
  4.  * (modified to be more generic, not os/2 specific, pgf, april '94)
  5.  *
  6.  * Supported monitor cards include
  7.  *    CGA, MONO, EGA, VGA.
  8.  *
  9.  * Note: Visual flashes are not yet supported.
  10.  *
  11.  *
  12.  * $Header: /usr/build/vile/vile/RCS/borland.c,v 1.25 1998/05/14 23:14:25 tom Exp $
  13.  *
  14.  */
  15.  
  16.  
  17. #define    termdef    1            /* don't define "term" external */
  18.  
  19. #include        "estruct.h"
  20. #include        "edef.h"
  21.  
  22. #if SYS_OS2
  23. #define INCL_VIO
  24. #define INCL_NOPMAPI
  25. #include <os2.h>
  26. #undef OPT_COLOR
  27. #endif /* SYS_OS2 */
  28.  
  29.  
  30. #if !DISP_BORLAND || DISP_IBMPC
  31. #error misconfigured:  DISP_BORLAND should be defined if using borland.c
  32. #error (and DISP_IBMPC should not be defined)
  33. #endif
  34.  
  35. #define NROW    50            /* Max Screen size.        */
  36. #define NCOL    80            /* Edit if you want to.         */
  37. #define    MARGIN    8            /* size of minimum margin and    */
  38. #define    SCRSIZ    64            /* scroll size for extended lines */
  39. #define    NPAUSE    200            /* # times thru update to pause */
  40. #define    SPACE    32            /* space character        */
  41.  
  42. /* We assume that most users have a color display.  */
  43.  
  44. #include <conio.h>
  45.  
  46. static  void    borflush  (void);
  47. static  void    bormove   (int,int);
  48. static  void    boreeol   (void);
  49. static  void    boreeop   (void);
  50. static  void    borbeep   (void);
  51. static  void    boropen   (void);
  52. static    void    borrev    (UINT);
  53. static    int    borcres   (const char *);
  54. static    void    borclose  (void);
  55. static    void    borputc   (int);
  56. static    void    borkopen  (void);
  57. static    void    borkclose (void);
  58.  
  59. #if OPT_COLOR
  60. static    void    borfcol   (int);
  61. static    void    borbcol   (int);
  62. static    void    borspal   (const char *);
  63. #endif
  64.  
  65. #if OPT_ICURSOR
  66. static    void    boricursor(int);
  67. #endif
  68.  
  69. int    cfcolor = -1;        /* current forground color */
  70. int    cbcolor = -1;        /* current background color */
  71. /* ansi to ibm color translation table */
  72. static    const char *initpalettestr = "0 4 2 6 1 5 3 7 8 12 10 14 9 13 11 15";
  73. /* black, red, green, yellow, blue, magenta, cyan, white   */
  74.  
  75. static    void    borscroll (int,int,int);
  76.  
  77. static    int    scinit    (int);
  78.  
  79.  
  80. static    char    linebuf[128];
  81. static    int    bufpos = 0;
  82.  
  83. static struct {
  84.     char  *seq;
  85.     int   code;
  86. } keyseqs[] = {
  87.     /* Arrow keys */
  88.     {"\0\110",     KEY_Up},
  89.     {"\0\120",     KEY_Down},
  90.     {"\0\115",     KEY_Right},
  91.     {"\0\113",     KEY_Left},
  92.     /* page scroll */
  93.     {"\0\121",     KEY_Next},
  94.     {"\0\111",     KEY_Prior},
  95.     {"\0\107",     KEY_Home},
  96.     {"\0\117",     KEY_End},
  97.     /* editing */
  98.         {"\0R",        KEY_Insert},
  99.     {"\0\123",     KEY_Delete},
  100.     /* function keys */
  101.         {"\0;",        KEY_F1},
  102.     {"\0<",        KEY_F2},
  103.     {"\0=",        KEY_F3},
  104.     {"\0>",        KEY_F4},
  105.     {"\0?",        KEY_F5},
  106.     {"\0@",        KEY_F6},
  107.     {"\0A",        KEY_F7},
  108.     {"\0B",        KEY_F8},
  109.     {"\0C",        KEY_F9},
  110.         {"\0D",        KEY_F10},
  111. };
  112.  
  113. int ibmtype;
  114.  
  115. static int borlastchar = -1;
  116. static int bortttypahead(void);
  117. static int borttgetc(void);
  118.  
  119. /*
  120.  * Standard terminal interface dispatch table. Most of the fields point into
  121.  * "termio" code.
  122.  */
  123. TERM    term    = {
  124.     NROW,
  125.     NROW,
  126.     NCOL,
  127.     NCOL,
  128.     MARGIN,
  129.     SCRSIZ,
  130.     NPAUSE,
  131.     boropen,
  132.     borclose,
  133.     borkopen,
  134.     borkclose,
  135.     borttgetc,
  136.     borputc,
  137.     bortttypahead,
  138.     borflush,
  139.     bormove,
  140.     boreeol,
  141.     boreeop,
  142.     borbeep,
  143.     borrev,
  144.     borcres,
  145. #if OPT_COLOR
  146.     borfcol,
  147.     borbcol,
  148.     borspal,
  149. #else
  150.     null_t_setfor,
  151.     null_t_setback,
  152.     null_t_setpal,
  153. #endif
  154.     borscroll,
  155.     null_t_pflush,
  156. #if OPT_ICURSOR
  157.     boricursor,
  158. #else
  159.     null_t_icursor,
  160. #endif
  161.     null_t_title,
  162.     null_t_watchfd,
  163.     null_t_unwatchfd,
  164. };
  165.  
  166. #if OPT_ICURSOR
  167. static void
  168. boricursor(int cmode)
  169. {
  170.     switch (cmode) {
  171.     case -1: _setcursortype( _NOCURSOR);        break;
  172.     case  0: _setcursortype( _NORMALCURSOR);    break;
  173.     case  1: _setcursortype( _SOLIDCURSOR);        break;
  174.     }
  175. }
  176. #endif
  177.  
  178. #if OPT_COLOR
  179. static void
  180. borfcol(int color)        /* set the current output color */
  181. {
  182.     cfcolor = ctrans[color];
  183.     textcolor(cfcolor & 15);
  184. }
  185.  
  186. static void
  187. borbcol(int color)        /* set the current background color */
  188. {
  189.     cbcolor = ctrans[color];
  190.     textbackground(cbcolor & 7);
  191. }
  192.  
  193. static void
  194. borspal(const char *thePalette)    /* reset the palette registers */
  195. {
  196.     borflush();
  197.     set_ctrans(thePalette);
  198. }
  199.  
  200. static void
  201. setup_colors(void)
  202. {
  203.     borfcol(gfcolor);
  204.     borbcol(gbcolor);
  205. }
  206.  
  207. static void
  208. reset_colors(void)
  209. {
  210.     borfcol(7 /* not necessarily C_WHITE */);
  211.     borbcol(C_BLACK);
  212. }
  213. #else
  214. #define setup_colors() /* nothing */
  215. #define reset_colors() /* nothing */
  216. #endif
  217.  
  218. static void
  219. borflush(void)
  220. {
  221.     if (bufpos) {
  222.         linebuf[bufpos] = '\0';
  223.         cputs(linebuf);
  224.         bufpos = 0;
  225.     }
  226. }
  227.  
  228. static void
  229. bormove(int row, int col)
  230. {
  231.     borflush();
  232.     gotoxy(col+1, row+1);
  233. }
  234.  
  235. /* erase to the end of the line */
  236. static void
  237. boreeol(void)
  238. {
  239.     borflush();
  240.     clreol();        /* pointer to the destination line */
  241. }
  242.  
  243. /* put a character at the current position in the current colors */
  244. static void
  245. borputc(int ch)
  246. {
  247.     linebuf[bufpos++] = ch;
  248. }
  249.  
  250.  
  251. static void
  252. boreeop(void)
  253. {
  254.     int x, y, i;
  255.     struct text_info t;
  256.  
  257.     borflush();
  258.     x = wherex();
  259.     y = wherey();
  260.     gettextinfo(&t);
  261.     clreol();
  262.     for (i = x + 1; i <= t.screenheight; i++) {
  263.         gotoxy(1, i);
  264.         clreol();
  265.     }
  266.     gotoxy(x, y);
  267. }
  268.  
  269. static void
  270. borrev(UINT reverse)        /* change reverse video state */
  271. {
  272.     borflush();
  273.     if (reverse) {
  274.         textbackground(cfcolor & 7);
  275.         textcolor(cbcolor & 15);
  276.     } else {
  277.         textbackground(cbcolor & 7);
  278.         textcolor(cfcolor & 15);
  279.     }
  280. }
  281.  
  282. static int
  283. borcres(const char *res)    /* change screen resolution */
  284. {
  285.     char    *dst;
  286.     register int i;        /* index */
  287.     int    status = FALSE;
  288.  
  289.     strcpy(current_res_name, res);
  290.     borflush();
  291.     /* find the default configuration */
  292.     if (!strcmp(res, "?")) {
  293.         status = scinit(-1);
  294.     } else {    /* specify a number */
  295.         if ((i = (int)strtol(res, &dst, 0)) >= 0 && !*dst)
  296.         {
  297.         /* only allow valid row selections */
  298.         /* Are these all valid under dos?  */
  299.         if (i==2)  status=scinit(25);
  300.         if (i==4)  status=scinit(43);
  301.         if (i==5)  status=scinit(50);
  302.         if (i==6)  status=scinit(60);
  303.  
  304.         if (i>6 && i<28)
  305.             status=scinit(25);
  306.  
  307.         if (i>=28 && i<43)
  308.             status=scinit(28);
  309.  
  310.         if (i>=43 && i<50)
  311.             status=scinit(43);
  312.  
  313.         if (i>=50 && i<60)
  314.             status=scinit(50);
  315.  
  316.         if (i>=60)
  317.             status=scinit(60);
  318.  
  319.         }
  320.     }
  321.     sgarbf = TRUE;
  322.     return status;
  323. }
  324.  
  325.  
  326. static void
  327. borbeep(void)
  328. {
  329.     putch('\a');
  330. }
  331.  
  332.  
  333. static void
  334. boropen(void)
  335. {
  336.     int i;
  337.  
  338.     set_palette(initpalettestr);
  339.     setup_colors();
  340.  
  341.     if (!borcres(current_res_name))
  342.         (void)scinit(-1);
  343.     ttopen();
  344.     for (i = TABLESIZE(keyseqs) - 1; i >= 0; i--)
  345.         addtosysmap(keyseqs[i].seq, 2, keyseqs[i].code);
  346. }
  347.  
  348.  
  349. static void
  350. borclose(void)
  351. {
  352.     int    current_type = ibmtype;
  353.  
  354.     borflush();
  355. #ifdef OPT_ICURSOR
  356.     _setcursortype(_NORMALCURSOR);
  357. #endif
  358.     ibmtype = current_type;    /* ...so subsequent TTopen restores us */
  359.  
  360.     reset_colors();
  361.     kbd_erase_to_end(0);
  362.     kbd_flush();
  363. }
  364.  
  365. static void
  366. borkopen(void)    /* open the keyboard */
  367. {
  368.     setup_colors();
  369.     TTmove(term.t_nrow-1, 0);    /* cf: dumbterm.c */
  370.     TTeeol();
  371.  
  372.     /* ms_install(); */
  373. }
  374.  
  375. static void
  376. borkclose(void)    /* close the keyboard */
  377. {
  378.     /* ms_deinstall(); */
  379.  
  380.     reset_colors();
  381.     TTmove(term.t_nrow-1, 0);    /* cf: dumbterm.c */
  382.     TTeeol();
  383. }
  384.  
  385. static
  386. int borttgetc(void)
  387. {
  388.     return (borlastchar = ttgetc());
  389. }
  390.  
  391.  
  392. /* bortttypahead:  Check to see if any characters are already in the
  393.  * keyboard buffer.In Borland C OS/2 1.5, kbhit doesn't return non-zero for
  394.  * the 2nd part of an extended character, but 1st part is still 0 so use
  395.  * that as indicator as well (why it's saved in borttgetc).
  396. */
  397. static int
  398. bortttypahead(void)
  399. {
  400.  
  401.     return (kbhit() != 0 || borlastchar == 0);
  402.  
  403. }
  404.  
  405. #if SYS_OS2          /* all modes are available under OS/2 */
  406. static int
  407. scinit(int rows)    /* initialize the screen head pointers */
  408. {
  409.  
  410.     /* and set up the various parameters as needed */
  411.  
  412.     if (rows == -1)
  413.     {
  414.         struct text_info ti;
  415.         gettextinfo(&ti);
  416.         rows = ti.screenheight;
  417.     }
  418.  
  419.     switch (rows) {
  420.  
  421. /* these are enum's, and thus cannot easily be checked, ie. #ifdef C80X21 */
  422.         case 21:    /* color C80X21 */
  423.                 textmode(C80X21);
  424.                 newscreensize(21, term.t_ncol);
  425.                 (void)strcpy(sres, "C80X21");
  426.                 break;
  427.  
  428.         default:
  429.         case 25:    /* Color graphics adapter */
  430.                 textmode(C80);
  431.                 newscreensize(25, term.t_ncol);
  432.                 (void)strcpy(sres, "C80");
  433.                 break;
  434.  
  435.         case 28:    /* Enhanced graphics adapter */
  436.                 textmode(C80X28);
  437.                 newscreensize(28, term.t_ncol);
  438.                 (void)strcpy(sres, "C80X28");
  439.                 break;
  440.  
  441.         case 43:    /* Enhanced graphics adapter */
  442.                 textmode(C80X43);
  443.                 newscreensize(43, term.t_ncol);
  444.                 (void)strcpy(sres, "C80X43");
  445.                 break;
  446.  
  447.         case 50:    /* VGA adapter */
  448.                 textmode(C80X50);
  449.                 newscreensize(50, term.t_ncol);
  450.                 (void)strcpy(sres, "C80X50");
  451.                 break;
  452.  
  453.         case 60:    /* Enhanced graphics adapter */
  454.                 textmode(C80X60);
  455.                 newscreensize(60, term.t_ncol);
  456.                 (void)strcpy(sres, "C80X60");
  457.                 break;
  458.  
  459.  
  460.     }
  461.  
  462.     ibmtype = rows;
  463.  
  464.     return(TRUE);
  465. }
  466.  
  467. #else /* SYS_OS2 */
  468.  
  469. static int
  470. scinit(int rows)    /* initialize the screen head pointers */
  471. {
  472.  
  473.     /* and set up the various parameters as needed */
  474.  
  475.     struct text_info ti;
  476.     int oldrows;
  477.  
  478.     gettextinfo(&ti);
  479.     oldrows = ti.screenheight;
  480.     if (rows == -1)
  481.         rows = oldrows;
  482.  
  483.     switch (rows) {
  484.  
  485. /* DOS has only BW40, C40, BW80, C80, MONO, and C4350 */
  486.  
  487.         default:
  488.         case 25:    /* Color graphics adapter */
  489.                 if (oldrows != 25)
  490.                     textmode(C80);
  491.                 newscreensize(25, term.t_ncol);
  492.                 (void)strcpy(sres, "C80");
  493.                 break;
  494.  
  495.         case 43:
  496.         case 50:
  497.         case 60:
  498.                 if (rows != oldrows)
  499.                     textmode(C4350);
  500.                 gettextinfo(&ti);
  501.                 rows = ti.screenheight;
  502.                 newscreensize(rows, term.t_ncol);
  503.                 sprintf(sres, "C80X%d", rows);
  504.                 break;
  505.  
  506.     }
  507.  
  508.     ibmtype = rows;
  509.  
  510.     return(TRUE);
  511. }
  512.  
  513. #endif /* SYS_OS2 */
  514.  
  515. /*
  516.  * Move 'n' lines starting at 'from' to 'to'
  517.  *
  518.  * OPT_PRETTIER_SCROLL is prettier but slower -- it scrolls a line at a time
  519.  *    instead of all at once.
  520.  */
  521.  
  522. /* move howmany lines starting at from to to */
  523. static void
  524. borscroll(int from, int to,int n)
  525. {
  526.     int i;
  527.     struct text_info t;
  528.  
  529.     borflush();
  530.     if (to == from) return;
  531. #if OPT_PRETTIER_SCROLL
  532.     if (absol(from-to) > 1) {
  533.         borscroll(from, (from<to) ? to-1:to+1, n);
  534.         if (from < to)
  535.             from = to-1;
  536.         else
  537.             from = to+1;
  538.     }
  539. #endif
  540.     gettextinfo(&t);
  541.     if (to < from) {
  542.         window(1, to + 1, t.screenwidth, from + n);
  543.         gotoxy(1, 1);
  544.         for (i = from - to; i > 0; i--)
  545.             delline();
  546.         gotoxy(1, n + 1);
  547.         for (i = from - to; i > 0; i--)
  548.             insline();
  549.     } else {
  550.         window(1, from + 1, t.screenwidth, to + n);
  551.         gotoxy(1, n + 1);
  552.         for (i = to - from; i > 0; i--)
  553.             delline();
  554.         gotoxy(1, 1);
  555.         for (i = to - from; i > 0; i--)
  556.             insline();
  557.     }
  558.     window(1, 1, t.screenwidth, t.screenheight);
  559. }
  560.  
  561. /*--------------------------------------------------------------------------*/
  562.  
  563.