home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / COMM / MISC / SRC26_2.ZIP / SRC / CONSOLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  8.4 KB  |  398 lines

  1. /*
  2.  * console.c: hterm console control
  3.  *
  4.  * Author: HIRANO Satoshi
  5.  * (C) 1989  Halca Computer Science Laboratory TM
  6.  *           University of Tokyo
  7.  *
  8.  * 2.2 89/05/16 Halca.Hirano V2.2 distribution
  9.  * 2.3 89/06/14 Halca.Hirano
  10.  *    1 remove #else, add #kanji
  11.  *    2 add non direct GDC access, use CRT BIOS for PC98XA or future machines
  12.  * 2.4 89/07/20 Halca.Hirano add 19/20/24/25 line mode, and many changes
  13.  * 2.5 89/07/22 Halca.Hirano add history
  14.  *    ---- V2.3.-1 distribution ----
  15.  * 2.6 89/09/15 Halca.Hirano fix download kanji bug
  16.  *      ---- V2.4.0 distribution ----
  17.  * 2.7 89/11/10 Tominaga@Titech ported to J3100
  18.  *  ---- V2.4.2 distribution ----
  19.  * 2.8 90/06/19 Halca.Hirano
  20.  *    rewrite conWrite() faster and support soft font
  21.  *
  22.  * $Header: console.cv  1.11  90/07/05 08:37:26  hirano  Exp $
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "config.h"
  29. #include "hterm.h"
  30. #include "option.h"
  31. #include "default.h"
  32. #include "global.h"
  33.  
  34. #ifdef KANJI
  35. /*
  36.  * JIS X0208-83 keisen conversion table
  37.  */
  38. static unsigned int keiConv[32] = {
  39.     0x240c, 0x260c, 0x300c, 0x340c, 0x3c0c, 0x380c, 0x400c, 0x500c,
  40.     0x480c, 0x580c, 0x600c, 0x250c, 0x270c, 0x330c, 0x370c, 0x3f0c,
  41.     0x3b0c, 0x470c, 0x570c, 0x4f0c, 0x5f0c, 0x6f0c, 0x440c, 0x530c,
  42.     0x4c0c, 0x5b0c, 0x630c, 0x410c, 0x540c, 0x490c, 0x5c0c, 0x660c
  43. };
  44. #endif /* KANJI */
  45.  
  46. /*
  47. ** void consoleInit()
  48.  *
  49.  * initialize console related variables
  50.  */
  51. void consoleInit()
  52. {
  53.     CRTInit();                        /* iniz CRT                */
  54.     attrib = eraseAttr = _NORMAL;        /* normal attribute        */
  55.     border = 0;                        /* black border            */
  56.  
  57.     /* set defaults */
  58.     lineMode = DEFAULT_LINE_MODE;
  59.     formfeed = DEFAULT_FORMFEED;
  60.     cursor = DEFAULT_CURSOR;    
  61.     blinkCursor = DEFAULT_BLINK_CURSOR;
  62.     blockCursor = DEFAULT_BLOCK_CURSOR;
  63.     autoWrap = DEFAULT_AUTO_WRAP;
  64.     printMode = DEFAULT_PRINT_MODE;
  65.     intControl = DEFAULT_INTCONTROL;
  66.     echoMode = DEFAULT_ECHO_MODE;
  67.     kanjiCode = DEFAULT_KANJI_CODE;        /* kanji code        */
  68.     visibleBell = DEFAULT_VISIBLE_BELL;    /* visible bell        */
  69.  
  70. #ifdef PC98
  71.     backQuote = HAVE_REAL_BACK_QUOTE;
  72. #endif /* PC98 */
  73.     clearSavedPage(page0Save);
  74. }
  75.  
  76. /*
  77. ** void consoleSetup()
  78.  *
  79.  * initialize CRT mode and clear current page
  80.  */
  81. void consoleSetup()
  82. {
  83.     cursorX = cursorY = 0;        /* home position        */
  84.     wrapPending = NO;            /* not pending            */
  85.     setLineMode(lineMode);        /* set line mode            */
  86.     upScrRegion = 0;            /* upper scroll region        */
  87.     lowScrRegion = bottomLine;    /* lower scroll region        */
  88.     setCRTMode();
  89.     setBorder(border);
  90.     initPage();
  91.     cursorOnOff(cursor);
  92. }
  93.  
  94. /*
  95. ** void consoleEnd()
  96.  *
  97.  * deinitialize console
  98.  */
  99. void consoleEnd()
  100. {
  101.     CRTEnd();                /* deiniz CRT        */
  102. }
  103.  
  104. /*
  105. ** void initPage()
  106.  *
  107.  * initialize current console page; clear and move cursor to home
  108.  */
  109. void initPage()
  110. {
  111.     clearCurrentPage();
  112.     locate(0, 0);
  113.     wrapPending = NO;
  114. }
  115.  
  116. /*
  117. ** void setLineMode(int mode)
  118.  *
  119.  * set line mode to 'mode'
  120.  */
  121. void setLineMode(mode)
  122. int mode;
  123. {
  124.     switch (mode) {
  125.     case LINE_MODE_19:
  126.         maxLine = MAX_LINE_19_MODE;
  127.         realMaxLine = MAX_LINE_20_MODE;
  128.         bottomLine = BOTTOM_LINE_19_MODE;
  129.         realBottomLine = BOTTOM_LINE_20_MODE;
  130.         break;
  131.     case LINE_MODE_20:
  132.         realMaxLine = maxLine = MAX_LINE_20_MODE;
  133.         realBottomLine = bottomLine = BOTTOM_LINE_20_MODE;
  134.         break;
  135.     case LINE_MODE_24:
  136.         maxLine = MAX_LINE_24_MODE;
  137.         bottomLine = BOTTOM_LINE_24_MODE;
  138.         realBottomLine = BOTTOM_LINE_25_MODE;
  139.         realMaxLine = MAX_LINE_25_MODE;
  140.         break;
  141.     case LINE_MODE_25:
  142.         realMaxLine = maxLine = MAX_LINE_25_MODE;
  143.         realBottomLine = bottomLine = BOTTOM_LINE_25_MODE;
  144.         break;
  145.     }
  146. #ifdef PC98
  147.     setCRTLineMode(realMaxLine < 21 ? 0 : 1);
  148. #endif /* PC98 */
  149. }
  150.  
  151. /*
  152. ** void conWrite(register u_short c)
  153.  *
  154.  * write a char on console; char is hterm kanji code
  155.  */
  156. void conWrite(c)
  157. register u_short c;
  158. {
  159.     register u_short c2;
  160.     int attrSave;
  161.  
  162. #ifdef KANJI
  163.     c2 = c>>8;
  164.     if (c2) {                        /* kanji        */
  165.         /*
  166.          * ensure that one kanji char never be devided into two lines
  167.          */
  168.         if (cursorX == LAST_COLUMN && wrapPending == NO) {
  169.             putChar(SPACE);
  170.             wrapPending = YES;        /* force move to next line    */
  171.         }
  172.     }
  173. #endif /* KANJI */
  174.  
  175.     /*
  176.      * process wrap pending
  177.      */
  178.     if (wrapPending && cursorX == LAST_COLUMN) {
  179.         if (cursorY == lowScrRegion) {
  180.             cursorX = 0;
  181.             scrlUp();
  182.         } else if (cursorY < bottomLine) {
  183.             cursorX = 0;
  184.             cursorY++;
  185.         } /* else stay LAST_COLUMN */
  186.         locate(cursorX, cursorY);
  187.     }
  188.     wrapPending = NO;
  189.  
  190. #ifdef SOFT_FONT
  191.     if (softFont)
  192.         softCursorOnOff(NO);
  193. #endif
  194.  
  195. #ifdef KANJI
  196.     if (c2) {
  197.         /*
  198.          * display kanji character
  199.          */
  200. #ifdef SOFT_FONT         
  201.         if (softFont)
  202.             softFontPutChar(c);
  203. #endif /* SOFT_FONT */
  204. #ifdef PC98    
  205.         if (0x2821 <= c && c <= 0x2840) /* keisen    */
  206.             c2 = keiConv[c - 0x2821];
  207.         else
  208.             c2 = ((c << 8) | (c2 - 0x20));
  209.         putChar(c2);
  210.         cursorX++;
  211.         putChar(c2 | 0x8000);
  212. #endif /* PC98 */
  213. #if defined(IBMPC) || defined(J3100)
  214.         c2 = JIStoSJIS(c2, c & 0xff);
  215.         putChar(c2 >> 8);
  216.         cursorX++;
  217. #ifdef J3100    /* the char will be written at cursor position in putChar()    */
  218.         locate(cursorX, cursorY);
  219. #endif
  220.         putChar(c2 & 0xff);
  221. #endif /* IBMPC || J3100 */
  222.     } else
  223. #endif /* KANJI */
  224.     {
  225.         attrSave = attrib;
  226.         /*
  227.          * if no interpret control mode, make reverse mode
  228.          */
  229.         if (intControl == NO && iscntrl(c)) {
  230.             reverseMode();
  231.             c = (c == 0x7f ? c : c + 0x40);
  232.         }
  233.         /*
  234.          * display ascii or kana character
  235.          */
  236. #ifdef SOFT_FONT
  237.         if (softFont)
  238.             softFontPutChar(c);
  239. #endif
  240. #ifdef PC98
  241.         /* I don't wont to display yen mark. */
  242.         if (c == 0x5c)                    /* backslash         */
  243.             c = backQuote ? c = 0xfc : 0xef;  /* *real* or emitation */
  244.         else if (c == 0x60) {            /* back quote        */
  245.             if (!backQuote)
  246.                 c = 0xde;                /* kana chon chon    */
  247.         }
  248. #endif /* PC98 */
  249.         putChar(c);
  250.         attrib = attrSave;            /* recover from reverse mode    */
  251.     }
  252.     /*
  253.      * move cursor to the next position
  254.      */
  255.     if (++cursorX > LAST_COLUMN) {    /* if kanji, already advanced one    */
  256.         cursorX = LAST_COLUMN;
  257.         if (autoWrap == YES) {
  258.             wrapPending = YES;
  259.         }
  260.     }
  261.  
  262.     /*
  263.      * cursor optimization;
  264.      * set cursor only if no more chars coming 
  265.      *    because locate() makes BIOS call which is very time consuming
  266.      */
  267. #ifndef J3100
  268.     if (!checkSerial() || mode != M_COMM || !online)
  269. #else    /* J3100 will use BIOS for next putChar() */
  270.     if (!checkSerial() || mode != M_COMM || !online || softFont == NO)
  271. #endif /* J3100 */
  272.         locate(cursorX, cursorY);
  273. noLocate:
  274.  
  275.     /*
  276.      * printing and logging
  277.      */
  278.     if (printMode)
  279.         outPrinter(c);
  280.     if (logging == LOG_CONVERT)
  281.         logPut(c);
  282. }
  283.  
  284. /*
  285. ** void moveCursor(int action, register int num)
  286.  *
  287.  * move cursor relatively
  288.  */
  289. void moveCursor(action, num)
  290. int action;
  291. register int num;
  292. {
  293.     wrapPending = NO;
  294.     if (num == 0)
  295.         num++;
  296.     switch (action) {
  297.     case    UP:
  298.         if (cursorY > bottomLine)
  299.             cursorY = bottomLine;
  300.         else if (cursorY >= upScrRegion && cursorY-num < upScrRegion)
  301.             cursorY = upScrRegion;
  302.         else if (cursorY - num < 0)
  303.             cursorY = 0;
  304.         else
  305.             cursorY -= num;
  306.         break;
  307.     case    DOWN:
  308.         if (cursorY >= bottomLine)
  309.             cursorY = bottomLine;
  310.         else if (cursorY <= lowScrRegion && cursorY+num > lowScrRegion)
  311.             cursorY = lowScrRegion;
  312.         else if (cursorY + num > bottomLine)
  313.             cursorY = bottomLine;
  314.         else
  315.             cursorY += num;
  316.         break;
  317.     case    RIGHT:
  318.         if (cursorX + num > LAST_COLUMN)
  319.             cursorX = LAST_COLUMN;
  320.         else 
  321.             cursorX += num;
  322.         break;
  323.     case    LEFT:
  324.         if (cursorX - num < 0) 
  325.             cursorX = 0;
  326.         else 
  327.             cursorX -= num;
  328.         break;
  329.     }
  330.     locate(cursorX, cursorY);
  331. }
  332.             
  333. /*
  334. ** void scrlUp()
  335.  *
  336.  * scroll up current page
  337.  */
  338. void scrlUp()
  339. {
  340.     int tmp = cursorY;
  341.  
  342.     wrapPending = NO;
  343.     cursorY = upScrRegion;
  344. #ifdef COPY_PASTE
  345.     saveHist(cursorY, YES);
  346. #endif /* COPY_PASTE */
  347.     deleteLine(1);
  348.     cursorY = tmp;
  349. }
  350.  
  351. /*
  352. ** void clearLine(from, to)
  353.  *
  354.  * clear lines from 'from' to 'to'
  355.  */
  356. void clearLine(from, to)
  357. u_short from;
  358. register u_short to;
  359. {
  360.     register int i;
  361.     int tmp;
  362.  
  363.     wrapPending = NO;
  364.     tmp = cursorY;
  365.     for (i = from; i <= to; i++) {
  366.         cursorY = i;
  367.         if (softFont) {
  368. #ifdef SOFT_FONT
  369.             if (cursor)
  370.                 cursorOnOff(NO);
  371.             softFont = NO;
  372.             clearColumn(0, MAX_COLUMN);
  373.             softFont = YES;
  374.             softFontClearLine(cursorY);
  375.             if (cursor)
  376.                 cursorOnOff(YES);                
  377. #endif /* SOFT_FONT */
  378.         } else
  379.             clearColumn(0, MAX_COLUMN);
  380.     }
  381.     cursorY = tmp;
  382. }
  383.  
  384. /*
  385. ** void  awMode(int mode)
  386.  *
  387.  * auto wrap mode 
  388.  */
  389. void awMode(mode)
  390. int mode;
  391. {
  392.     autoWrap = mode;
  393.     if (autoWrap == YES)
  394.         wrapPending = NO;
  395. }
  396.  
  397.  
  398.