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

  1. /*
  2.  * utils.c: hterm utilities (regident or not regident)
  3.  *
  4.  * Author: HIRANO Satoshi
  5.  * (C) 1990  Halca Computer Science Laboratory TM
  6.  *           University of Tokyo
  7.  *
  8.  * 1.1 90/03/29 Halca.Hirano derived from half of setup.c
  9.  * 1.2 90/06/17 Halca.Hirano support filename completion in emacs()
  10.  *
  11.  * $Header: utils.cv  1.5  90/07/05 08:37:26  hirano  Exp $
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <string.h>
  17. #include <io.h>
  18. #include <stdlib.h>
  19. #if !defined(__TURBOC__) || __TURBOC__ >= 0x0200
  20. #include <sys/types.h>
  21. #endif
  22. #include <sys/stat.h>
  23. #include "config.h"
  24. #include "hterm.h"
  25. #include "option.h"
  26. #include "default.h"
  27. #include "global.h"
  28. #include "version.h"
  29. #include "indexer.h"
  30.  
  31. static char buf[60];
  32.  
  33. int emacsPutBuf(int c);
  34.  
  35. /*
  36. ** void putStatus(char *s)
  37.  *
  38.  * display message on status line
  39.  */
  40. void putStatus(s)
  41. char *s;
  42. {
  43.     int attrSave2 = attrib;
  44.     u_short xSave = cursorX, ySave = cursorY;
  45.  
  46.     clearStatus();
  47.     attrib = eraseAttr;
  48.     locate(0, realBottomLine);
  49.     while (*s)
  50.         conWrite(*s++);
  51.     attrib = attrSave2;
  52.     locate(xSave, ySave);
  53. }
  54.  
  55. /*
  56. ** void clearStatus()
  57.  *
  58.  * clear status line
  59.  */
  60. void clearStatus()
  61. {
  62.      u_short attrSave = attrib;
  63.     u_short xSave = cursorX, ySave = cursorY;
  64.  
  65.     attrib = eraseAttr;
  66.     clearLine(realBottomLine, realBottomLine);
  67.     attrib = attrSave;
  68.     locate(xSave, ySave);
  69. }
  70.  
  71. /*
  72. ** void clearCopyright()
  73.  *
  74.  * clear copyright lines
  75.  */
  76. void clearCopyright()
  77. {
  78.     u_short origX = cursorX;
  79.     u_short origY = cursorY;
  80.      u_short attrSave = attrib;
  81.     
  82.     attrib = eraseAttr;
  83.     clearLine(0, SETUP_LINE-1);
  84.     attrib = attrSave;
  85.     locate(origX, origY);
  86. }
  87.  
  88. /*
  89. ** void flushComment(char *s)
  90.  *
  91.  * display comment in short term on the status line
  92.  */
  93. void flushComment(s)
  94. char *s;
  95. {
  96.     putStatus(s);
  97.     kermitTimer = TICK_SEC/3;
  98.     while (kermitTimer > 0)
  99.         nullFunction();
  100.     clearStatus();
  101. }
  102.  
  103. /*
  104. ** void putComment(char *msg, char *msg2)
  105.  *
  106.  * display "hterm: hello world" string
  107.  */
  108. void putComment(msg, msg2)
  109. char *msg, *msg2;
  110. {
  111.     conPrint("hterm: ");
  112.     conPrint(msg); conPrint(msg2);
  113.     conPrint("\n\r");
  114. }
  115.  
  116. /*
  117. ** char *versionString()
  118.  *
  119.  * return hterm version string
  120.  */
  121. char *versionString()
  122. {
  123.     sprintf(tmpBuf, "hterm %u.%u.%d.%d %s  %s\n\r", VERSION, 
  124.         REVISION, EDITION, LOCAL_EDITION, MACHINE, RELDATE);
  125.     tmpBuf[39] = '\0';    /* ensure lenght 40 chars for setup file    */
  126.     return(tmpBuf);        /* please take attention! use immedeatly     */
  127. }
  128.  
  129. /*
  130. ** int checkEvent()
  131.  *
  132.  * return non 0 if key or serial data is available
  133.  */
  134. int checkEvent()
  135. {
  136.     return(checkKey() || checkSerial());
  137. }
  138.  
  139. /*
  140. ** void showPage1()
  141.  * save page0 context, show page1
  142.  */
  143. void showPage1()
  144. {
  145.     savePage(page0Save);
  146.     curXSave = cursorX; curYSave = cursorY;
  147.     attrSave = attrib; 
  148.     lowScrRgSave = lowScrRegion; 
  149.     printMSave = printMode;
  150.     intConSave = intControl;
  151.     kfirstSave = kfirst; kstateSave = kstate;
  152.     lineModeSave = lineMode;
  153.     loggingSave = logging;
  154.     upLoadSave = upLoading;
  155.     cursorSave = cursor;
  156.     kanjiSave = kanjiCode;
  157.     wrapPendingSave = wrapPending;
  158.  
  159.     logging = cursor =     printMode = NO;
  160.     cursorOnOff(NO);
  161.     kfirst = kstate = 0;
  162.     intControl = YES;
  163.  
  164.     attrib = eraseAttr;
  165.     kanjiCode = SJIS;        /* for help system */
  166.  
  167.     setLineMode(LINE_MODE_25);
  168.     initPage();
  169.     lowScrRegion = bottomLine;
  170.     locate(0, 0);
  171. }
  172.  
  173.  
  174. /*
  175. ** void showPage0()
  176.  *
  177.  * restore page1 context, show page0
  178.  */
  179. void showPage0()
  180. {
  181.     setLineMode(lineMode);
  182.     lowScrRegion = lowScrRgSave;
  183.     restorePage(page0Save);
  184.     cursor = cursorSave;
  185.     cursorOnOff(cursor); 
  186.     kfirst = kfirstSave; kstate = kstateSave;
  187.     attrib = attrSave;
  188.     cursorX = curXSave; cursorY = curYSave;
  189.     kanjiCode = kanjiSave;
  190.     awMode(autoWrap);
  191.     locate(cursorX, cursorY);
  192.     printMode = printMSave;
  193.     intControl = intConSave;
  194.     logging = loggingSave;
  195.     upLoading = upLoadSave;
  196.     wrapPending = wrapPendingSave;    /* this must be after locate() */
  197. }
  198.  
  199. /*
  200. ** void callOS()
  201.  *
  202.  * call local operating system user interface
  203.  */
  204. void callOS()
  205. {
  206.     char *p;
  207.     extern char *getenv();
  208.     int oldMode = mode;
  209.     int eraseAttrSave = eraseAttr;
  210.     int softFontSave = softFont;
  211.     int cursorSave = cursor;
  212.     int    attrSave = attrib;
  213.     static char commandLine[MAX_FILE_NAME] = "";
  214.  
  215.     mode = M_SETUP;
  216.     if (emacs("Program: ", commandLine, MAX_FILE_NAME, E_HELP) != ERR) {
  217.         if (commandLine[0] != '\0')
  218.             p = commandLine;
  219.         else if (defaultCommandLine[0] != '\0')
  220.             p = defaultCommandLine;
  221.         else if ((p = getenv("COMSPEC")) == NULL)
  222.             p = "command.com";
  223.         mode = M_OS;
  224.         restoreCRTMode();
  225.         mouseEnd();
  226.         restoreCritHandler();    /* restore critical error handler    */
  227.         keyEnd();                /* restore keyboard handler            */
  228.         portEnd();                /* restore port handler                */
  229.         saverEnd();                /* restore timer handler            */
  230.  
  231.         printf("\x1b[1;1f"); fflush(stdout);    /* move cursor to (0, 0)    */
  232.         system(p);
  233.         printf("\nHit Any Key"); fflush(stdout);
  234.  
  235.         saverReInit();            /* reinitialize timer handler        */
  236.         keyReInit();            /* reinitialize keyboard handler    */
  237.         portReInit();            /* reinstall port handler            */
  238.         portSetup();            /* initialize port handler            */
  239.         setCritHandler();        /* set critical error handler        */
  240.         while (keyin() == -1)
  241.             ;
  242.         eraseAttr = eraseAttrSave;
  243.         attrib = attrSave;
  244.         softFont = softFontSave;
  245.         cursor = cursorSave;
  246.         consoleSetup();
  247.         mouseInit();
  248.     }
  249.     mode = oldMode;
  250. }
  251.  
  252.  
  253. /*
  254. ** char *helpSystem(char *key)
  255.  *
  256.  * print help message for key from hterm database
  257.  */
  258. char *helpSystem(key)
  259. char *key;
  260. {
  261.     static char nextKey[KEY_LEN];
  262.     char buf[100];
  263.  
  264.     if (openIndex(helpFileName) == ERR) {
  265.         sprintf(buf, msg_cantOpen, helpFileName);
  266.         putStatus(buf);
  267.         return((char *)NULL);
  268.     }
  269.     if (getKey(key, nextKey) == ERR) {
  270.         sprintf(buf, "Sorry, help info '%s' is not found.", key);
  271.         putStatus(buf);
  272.         return((char *)NULL);
  273.     }
  274.     clearCopyright();
  275.     locate(0, 0);
  276.     while (getRecord(buf, sizeof(buf)) != ERR) {
  277.         conPrint(buf); conPrint("\r");
  278.     }
  279.     showHelp = YES;
  280.     return(nextKey);
  281. }
  282.  
  283. /*
  284. ** int emacs(char *prompt, char *buf, int limit, int help)
  285.  *
  286.  * 'emacs mini-buffer'
  287.  *
  288.  * put the prompt, input a string into buf (size 'limit')
  289.  * buf should be \0 terminated.
  290.  * If help is YES, display emacs help message to copyright screen
  291.  *
  292.  */
  293. int emacs(prompt, buf, limit, help)
  294. char *prompt, *buf;
  295. int limit;
  296. int help;
  297. {
  298.     int disp;            /* display pointer    */
  299.     int dispTop;        /* display top of line position    */
  300.     int dispEnd;        /* display end of line position    */
  301.     int at;                /* buffer pointer    */
  302.     int max;            /* buffer max pointer    */
  303.     int end = NO;        /* edit end flag    */
  304.     int size;            /* temporary char size    */
  305.     u_short c;
  306.     u_short attrSave2 = attrib;
  307.     u_short origX = cursorX;
  308.     u_short origY = cursorY;
  309.     int cursorSave = cursor;
  310.     char *p;
  311.     char fileNameBuf[100], *buf2, *fileName;
  312.     int fx;
  313.     int first;
  314.     
  315.     if (help & E_HELP)
  316.         helpSystem("emacs");
  317.     cursor = YES;
  318.     cursorOnOff(YES);
  319. redraw:
  320.     at = max = strlen(buf);
  321.     locate(0, realBottomLine);
  322.     clearStatus();
  323.     disp = 0;
  324.  
  325.     /* put  prompt */
  326.     for (p = prompt; *p; p++)
  327.         disp += emacsPutBuf(*p);
  328.     dispTop = disp;                /* display top of line    */
  329.     
  330.     /* put default buffer string */
  331.     locate(dispTop, realBottomLine);
  332.     for (p = buf; *p; p++) {
  333.         disp += emacsPutBuf(*p);
  334.         if (disp >= LAST_COLUMN-2)
  335.             break;
  336.     }
  337.     dispEnd = disp;
  338.  
  339.     while (end == NO) {
  340.         while ((int)(c = keyin()) == -1)
  341.             ;
  342.         if (c == CR) { 
  343.             end = YES;
  344.         } else if (c == ESC || c == bindTab[C_ABORT]) { 
  345.             end = ERR;    /* abort    */
  346.         } else if (c == HOME_KEY || c == bindTab[C_TOL]) { 
  347.             at = 0;     /* top of line    */
  348.             locate(dispTop, realBottomLine);
  349.             disp = dispTop;
  350.         } else if (c == END_KEY || c == bindTab[C_EOL]) {
  351.             at = max;     /* end of line    */
  352.             locate(dispEnd, realBottomLine);
  353.             disp = dispEnd;
  354.         } else if (c == LEFT_KEY || c == bindTab[C_LEFT]) {        /* backward    */
  355.             if (--at < 0) 
  356.                 at = 0;
  357.             else {
  358.                 size = charSize((u_char FAR *)buf, at);
  359.                 moveCursor(LEFT, size);
  360.                 disp -= size;
  361.             }
  362.         } else if (c == RIGHT_KEY || c == bindTab[C_RIGHT]) {    /* forward    */
  363.             if (++at > max) 
  364.                 at = max;
  365.             else {
  366.                 size = charSize((u_char FAR *)buf, at-1);
  367.                 moveCursor(RIGHT, size);
  368.                 disp += size;
  369.             }
  370.         } else if (c == bindTab[C_KILLEND]) { 
  371.             max = at;        /* kill to end of line */
  372.             if (dispEnd-disp > 0)
  373.                 deleteChar(disp, dispEnd-disp);
  374.             dispEnd = disp;
  375.         } else if (c == bindTab[C_KILLTOP]) {        /* kill to the top of line */
  376.             dispEnd -= disp-dispTop;
  377.             deleteChar(dispTop, disp-dispTop);
  378.             disp = dispTop;
  379.             locate(disp, realBottomLine);
  380.             memcpy(buf, &buf[at], max-at);
  381.             max -= at; 
  382.             at = 0;
  383.         } else if (c == DEL || c == bindTab[C_BDEL]) {    /* BS, DEL */
  384.             if (at <= 0) 
  385.                 at = 0;
  386.             else { 
  387.                 --at;
  388.                 size = charSize((u_char FAR *)buf, at);
  389.                 dispEnd -= size;
  390.                 disp -= size;
  391.                 moveCursor(LEFT, size);
  392.                 deleteChar(disp, size);
  393.                 memcpy(&buf[at], &buf[at+1], max-at); 
  394.                 --max; 
  395.             }
  396.         } else if (c == bindTab[C_DEL]) {                /* delete char */
  397.             if (at < max) {    
  398.                 memcpy(&buf[at], &buf[at+1], max-at-1);
  399.                 --max;
  400.                 size = charSize((u_char FAR *)buf, at);
  401.                 deleteChar(disp, size);
  402.                 dispEnd -= size;
  403.             }
  404.         } else if ((help & E_FILE) && c == TAB) {        /* file name completion */
  405.             int x = cursorX, y = cursorY, count = 0;
  406.             int lowScrRegionSave = lowScrRegion;
  407.             buf[max] = '\0';
  408.             strcpy(fileNameBuf, buf);
  409.             toSlash(fileNameBuf);
  410.             if (!(p = strrchr(fileNameBuf, '/')))
  411.                 p = fileNameBuf;
  412.             if (strchr(p, '.'))
  413.                 strcat(fileNameBuf, "*");
  414.             else
  415.                 strcat(fileNameBuf, "*.*");
  416.             clearCopyright();
  417.             first = YES;
  418.             buf2 = fileNameBuf;
  419.             lowScrRegion = SETUP_LINE-2;
  420.             locate(0, 0);
  421.             for (fx = 0; getNextFileName(first, &buf2, &fileName, YES);) {
  422.                 if (fx + strlen(fileName) + 4 > MAX_COLUMN) {
  423.                     fx = 0; conPrint("\n\r");
  424.                 }
  425.                 conPrint(fileName); conPrint("    ");
  426.                 fx += strlen(fileName) + 4;
  427.                 count++;
  428.                 first = NO;
  429.             }
  430.             if (first) {            /* not found    */
  431.                 conPrint(fileNameBuf); conPrint(" not found");
  432.             }
  433.             lowScrRegion = lowScrRegionSave;
  434.             locate(x, y);
  435.             if (count == 1) {        /* just match    */
  436.                 strcpy(buf, fileName);
  437.                 goto redraw;
  438.             }
  439.         } else if (c == bindTab[C_QUOTE]) {                /* control char input */
  440.             if (max >= limit-1) bell();
  441.             else {
  442.                 while ((c = keyin()) == -1);
  443.                 goto force;
  444.             }
  445.         } else if (c == UP_KEY || c == DOWN_KEY) { 
  446.             ;            /* ignore        */
  447.         } else {
  448.             if (' ' <= c && c <= 0x7e)
  449. force:            if (max >= limit-1) { 
  450.                     max = limit-1; bell();
  451.                 } else {
  452.                     int j;
  453.                     for (j = max-at; j > 0; --j)
  454.                         buf[at+j] = buf[at+j-1];
  455.                     buf[at] = c;
  456.                     size = charSize((u_char FAR *)buf, at);
  457.                     insertChar(disp, size);
  458.                     dispEnd += size;
  459.                     disp += size;
  460.                     (void)emacsPutBuf(c);
  461.                     at++;
  462.                     max++;
  463.                 }
  464.         }
  465.     }
  466.     buf[max] = '\0';
  467.     attrib = attrSave2;
  468.     clearStatus();
  469.     cursorOnOff(cursor = cursorSave);
  470.     locate(origX, origY);
  471.     if (end == ERR) {
  472.         putStatus("Abort"); bell();
  473.     }
  474.     if (help)
  475.         clearCopyright();
  476.     return(end == ERR ? ERR : OK);
  477. }
  478.  
  479. /*
  480. ** static int emacsPutBuf(int c)
  481.  *
  482.  * print a char for mini-mini emacs
  483.  */
  484. static int emacsPutBuf(c)
  485. int c;
  486. {
  487.     if (' ' <= c && c <= 0x7e) {
  488.         conWrite(c);
  489.         return(1);
  490.     } else {
  491.         conWrite('^');
  492.         conWrite(c == 0x7f ? '?' : (c+0x40));
  493.         return(2);
  494.     }
  495. }
  496.  
  497. /*
  498. ** int charSize(u_char FAR *buf, int at)
  499.  *
  500.  *  return character size at buf[at]
  501.  */
  502. int charSize(buf, at)
  503. u_char FAR *buf;
  504. int at;
  505. {
  506.     int c = buf[at];
  507.     
  508.     if (' ' <= c && c <= 0x7e)
  509.         return(1);
  510.     else             /* control code like '^A' or kanji    */
  511.         return(2);
  512. }
  513.  
  514. #ifdef SETUP
  515. /*
  516. ** void editKeyTable(char *name[], u_char table[][], int num, int keyLen, int nameLen)
  517.  *
  518.  * edit key table
  519.  *
  520.  * called from keypc.c, key98.c
  521.  */
  522. void editKeyTable(name, table, num, keyLen, nameLen)
  523. char *name[];
  524. u_char table[][MAX_FUNKEY];
  525. int num;            /* number of keys        */
  526. int keyLen;            /* key body length        */
  527. int nameLen;        /* key name length        */
  528. {
  529.     int end = 0;
  530.     int i, j, newi;
  531.     u_short key;
  532.     u_char buf[40], keybuf[MAX_FUNKEY+2];
  533.     char *junk;
  534.     u_char *shortKeyTab;
  535. #define NUM_LINE 14
  536.  
  537.     showKeyTable(name, table, num, keyLen, nameLen);
  538.     if (keyLen <= 1)        /* for single char table */
  539.         shortKeyTab = (u_char *)table;
  540.     i = newi = 0;
  541.     while (!end) {
  542.         clearStatus();
  543.         strcpy(buf, name[i]);
  544.         for (j = strlen(name[i]); j < nameLen; j++)
  545.             buf[j] = SPACE;
  546.         buf[j] = '\0';
  547.         locate((i/NUM_LINE)*(nameLen+keyLen+8), i%NUM_LINE);
  548.         reverseMode();
  549.         conPrint(buf);
  550.         normalMode();
  551.         while ((int)(key = keyin()) == (-1))
  552.             ;
  553.         if ((key & 0xff) == 0x0d) {
  554.             if (keyLen <= 1) {    /* single char */
  555.                 if (' ' < shortKeyTab[i] && shortKeyTab[i] <= 0x7e)    {
  556.                     keybuf[0] = (char)shortKeyTab[i];
  557.                     keybuf[1] = '\0';
  558.                 } else {
  559.                     sprintf(keybuf, "$%02x", shortKeyTab[i]);
  560.                 }
  561.                 if (emacs(msg_newString, keybuf, 6, E_NO_HELP) != ERR) {
  562.                         /* "New string (ESC for abort): " */                
  563.                     if (keybuf[0] == '$')
  564.                         if (keybuf[1] == '\0')
  565.                             shortKeyTab[i] = '$';
  566.                         else
  567.                             shortKeyTab[i] = (u_char)strtol((char *)&keybuf[1], &junk, 16);
  568.                     else
  569.                         shortKeyTab[i] = keybuf[0];
  570.                 }
  571.             } else {            /* string    */
  572.                 strcpy(keybuf, table[i]);
  573.                 if (emacs(msg_newString, keybuf, keyLen, E_NO_HELP) != ERR) {
  574.                         /* "New string (ESC for abort): " */
  575.                     strcpy(table[i], keybuf);
  576.                 }
  577.             }
  578.             showKeyTable(name, table, num, keyLen, nameLen);
  579.             continue;
  580.         }
  581.         if (key == UP_KEY || key == bindTab[C_UP]) {
  582.             if (i > 0) newi = i - 1;
  583.         } else if (key == DOWN_KEY || key == bindTab[C_DOWN]) {
  584.             if (i < num -1)    newi = i + 1;
  585.         } else if (key == bindTab[C_ESC]) {
  586.             end = 1;
  587.         } else if (key == RIGHT_KEY || key == LEFT_KEY) {
  588.             ;        /* ignore    */
  589.         } else {
  590.             bell();
  591.         }
  592.         locate((i/NUM_LINE)*(nameLen+keyLen+8), i%NUM_LINE);
  593.         normalMode();
  594.         conPrint(buf);
  595.         i = newi;
  596.     }
  597.     clearCopyright();
  598. }
  599.     
  600. /* 
  601. ** static void showKeyTable(char *name[], char table[][], int num, int keyLen, int nameLen)
  602.  *
  603.  * display key table;
  604.  * called by editKeyTable()
  605.  */
  606. static void showKeyTable(name, table, num, keyLen, nameLen)
  607. char *name[];
  608. u_char table[][MAX_FUNKEY];
  609. int num;
  610. int keyLen;
  611. int nameLen;
  612. {
  613.     int i;
  614.     char *p;
  615.     u_char *shortKeyTab, key;
  616.     char buf[6];    /* $0000+1 */
  617.  
  618.     if (keyLen <= 1)        /* for single char table */
  619.         shortKeyTab = (u_char *)table;
  620.     for (i = 0; i < num; i++) {
  621.         locate((i/NUM_LINE)*(nameLen+keyLen+8), i%NUM_LINE);
  622.         conPrint("\x1b[0K");
  623.         conPrint(name[i]);
  624.         locate((i/NUM_LINE)*(nameLen+keyLen+8)+nameLen+1, i%NUM_LINE);
  625.         if (keyLen <= 1) {    /* single char */
  626.             key = shortKeyTab[i];
  627.             if (' ' < key && key <= 0x7e)
  628.                 decodeAnsi(key);
  629.             else {
  630.                 sprintf(buf, "$%02x", key);
  631.                 conPrint(buf);
  632.             }
  633.         } else {    /* string */
  634.             for (p = table[i]; *p; p++) {
  635.                 if (' ' <= *p && *p <= 0x7e)
  636.                     decodeAnsi(*p);
  637.                 else {
  638.                     decodeAnsi('^');
  639.                     decodeAnsi(*p == 0x7f ? '?' : (*p+0x40));
  640.                 }
  641.             }
  642.         }
  643.         conPrint("\x1b[0K");
  644.     }
  645.     locate(40, 13);
  646.     conPrint("Caution! Effects immediately");
  647.     locate(40, 14);
  648.     conPrint("Select by UP/DOWN key");
  649.     locate(40, 15);
  650.     conPrint("ESC for end, ENTER to edit");
  651. }
  652. #else
  653. void editKeyTable(){}
  654. #endif /* SETUP */
  655.  
  656. /*
  657. ** char *loadSetup(char *path, int *flag)
  658.  *
  659.  * load setup file then re-initialize variables
  660.  */
  661. char *loadSetup(path, flag)
  662. char *path;
  663. int *flag;
  664. {
  665.     int fd;
  666.     char *msg;
  667.     struct setups *setups = (struct setups *)XFerBuffer;
  668.  
  669.     *flag = NO;        /* as default, setup file will not be loaded */
  670.     msg = (char *)NULL;
  671.     if ((fd = open(path, O_RDONLY|O_BINARY)) < 0) {
  672.         sprintf(buf, msg_cantOpen, path);
  673.         return(buf);
  674.     }
  675.     if (read(fd, (char *)setups, sizeof(struct setups)) != sizeof(struct setups) ||
  676.             strcmp(setups->ids, msg_setupID)) {
  677.         close(fd);
  678.         sprintf(buf, "'%s' is not hterm set-up.", path);
  679.         return(buf);
  680.     }
  681.     if (setups->version != VERSION || setups->revision != REVISION || setups->edition != EDITION) {
  682.         sprintf(buf, "set-up '%s' version mismatch", path);
  683.         if (setups->version *1000 + setups->revision *100 +setups->edition*10 < 
  684.             2530) {
  685.             return(buf);        /* incompatible set-up file    */
  686.         }
  687.         *flag = YES;            /* override flag, be careful!    */
  688.         msg = buf;
  689.     }
  690.  
  691.     strcpy(phone, setups->phone);
  692.     memcpy(tabTable, setups->tab, MAX_COLUMN);
  693.     strcpy(defaultCommandLine, setups->defaultCommandLine);
  694.     strcpy(fontName, setups->fontName);
  695.  
  696.     originMode    = setups->val[ 0];
  697.     formfeed    = setups->val[ 1];
  698.     if (mode == M_SETUP)
  699.         cursorSave    = setups->val[ 2];
  700.     else
  701.         cursor        = setups->val[ 2];
  702.     blinkCursor    = setups->val[ 3];
  703.     autoWrap    = setups->val[ 4];
  704.     if (mode == M_SETUP)
  705.         intConSave    = setups->val[ 5];
  706.     else
  707.         intControl    = setups->val[ 5];
  708.     echoMode    = setups->val[ 6];
  709. #if 0
  710.     statline    = setups->val[ 7];
  711. #endif
  712.     softFont    = setups->val[ 8];
  713.     clickFlag    = setups->val[ 9];
  714.     applKeypad    = setups->val[10];
  715.     newline        = setups->val[11];
  716.     asckey        = setups->val[12];
  717.     if (mode == M_SETUP)
  718.         printMSave    = setups->val[13];
  719.     else
  720.         printMode    = setups->val[13];
  721.     spacing        = setups->val[14];
  722.     online        = setups->val[15];
  723.     baudrate    = setups->val[16];
  724.     baud        = setups->val[17];
  725.     parity        = setups->val[18];
  726.     paritybit    = setups->val[19];
  727.     stopbit        = setups->val[20];
  728.     /*        = setups->val[21];     */
  729.     /*        = setups->val[22];    */
  730.     backQuote    = setups->val[23];
  731.     if (mode == M_SETUP)
  732.         kanjiSave = setups->val[24];
  733.     else
  734.         kanjiCode = setups->val[24];
  735.     blockCursor    = setups->val[25];
  736.     if (mode == M_SETUP)
  737.         lineModeSave    = setups->val[26];
  738.     else
  739.         lineMode    = setups->val[26];
  740.     xonXoff        = setups->val[27];
  741.     spaceTab    = setups->val[28];
  742.     saver        = setups->val[29];
  743.     bsDel        = setups->val[30];
  744.     dialStartup    = setups->val[31];
  745.     visibleBell    = setups->val[32];
  746.     applCursor    = setups->val[33];
  747.     shiftLock    = setups->val[34];
  748.     fepInvoke    = setups->val[35];
  749.     portNo        = setups->val[36];
  750.     mouseSpeed    = setups->val[37];
  751.     saverType    = setups->val[38];
  752.     loggingType    = setups->val[39];
  753.     upLoadType    = setups->val[40];
  754.     xmodemMode    = setups->val[41];
  755.     xmodemType    = setups->val[42];
  756.     xmodemLongP    = setups->val[43];
  757.     upLoadDelay    = setups->val[44];
  758.     bsKeyRatio    = setups->val[45];
  759.     dropER        = setups->val[46];
  760.     eraseAttr    = setups->val[47];
  761.     border        = setups->val[48];
  762.     attrib        = setups->val[49];
  763.  
  764.     loadKey(fd);
  765.     close(fd);
  766.     return(msg);
  767. }
  768.  
  769. #ifdef SETUP
  770. /*
  771.  ** char *saveSetup(char *path)
  772.  *
  773.  * save variables in setup file
  774.  */
  775. char *saveSetup(path)
  776. char *path;
  777. {
  778.     int fd, i;
  779.     
  780.     struct setups *setups = (struct setups *)XFerBuffer;
  781.  
  782.     if ((fd = open(path, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE)) < 0) {
  783.         sprintf(buf, msg_cantOpen, path);
  784.         return(buf);
  785.     }
  786.     sprintf(setups->hello, "hterm %d.%d.%d.%d setup", VERSION, REVISION, EDITION, LOCAL_EDITION);
  787.     for (i = 0; i < 20; i++)
  788.         setups->ids[i] = 0;
  789.     strcpy(setups->ids, msg_setupID);
  790.     setups->version = VERSION;
  791.     setups->revision = REVISION;
  792.     setups->edition = EDITION;
  793.  
  794.     strcpy(setups->phone, phone);
  795.     memcpy(setups->tab, tabTable, MAX_COLUMN);
  796.     strcpy(setups->defaultCommandLine, defaultCommandLine);
  797.     strcpy(setups->fontName, fontName);
  798.  
  799.     for (i = 0; i < NUM_VALUE; i++)
  800.         setups->val[i] = 0;
  801.     setups->val[ 0] = originMode;
  802.     setups->val[ 1] = formfeed;
  803.     setups->val[ 2] = cursorSave;
  804.     setups->val[ 3] = blinkCursor;
  805.     setups->val[ 4] = autoWrap;
  806.     setups->val[ 5] = intConSave;
  807.     setups->val[ 6] = echoMode;
  808.     setups->val[ 7] = 0;    /* statline; */
  809.     setups->val[ 8] = softFont;
  810.     setups->val[ 9] = clickFlag;
  811.     setups->val[10] = applKeypad;
  812.     setups->val[11] = newline;
  813.     setups->val[12] = asckey;
  814.     setups->val[13] = printMSave;
  815.     setups->val[14] = spacing;
  816.     setups->val[15] = online;
  817.     setups->val[16] = baudrate;
  818.     setups->val[17] = baud;
  819.     setups->val[18] = parity;
  820.     setups->val[19] = paritybit;
  821.     setups->val[20] = stopbit;
  822.     setups->val[21] = 0;
  823.     setups->val[22] = 0;
  824.     setups->val[23] = backQuote;
  825.     setups->val[24] = kanjiSave;
  826.     setups->val[25] = blockCursor;
  827.     setups->val[26] = lineMode;
  828.     setups->val[27] = xonXoff;
  829.     setups->val[28] = spaceTab;
  830.     setups->val[29] = saver;
  831.     setups->val[30] = bsDel;
  832.     setups->val[31] = dialStartup;
  833.     setups->val[32] = visibleBell;
  834.     setups->val[33] = applCursor;
  835.     setups->val[34] = shiftLock;
  836.     setups->val[35] = fepInvoke;
  837.     setups->val[36] = portNo;
  838.     setups->val[37] = mouseSpeed;
  839.     setups->val[38] = saverType;
  840.     setups->val[39] = loggingType;
  841.     setups->val[40] = upLoadType;
  842.     setups->val[41] = xmodemMode;
  843.     setups->val[42] = xmodemType;
  844.     setups->val[43] = xmodemLongP;
  845.     setups->val[44] = upLoadDelay;
  846.     setups->val[45] = bsKeyRatio;
  847.     setups->val[46] = dropER;
  848.     setups->val[47] = eraseAttr;
  849.     setups->val[48] = border;
  850.     setups->val[49] = attrSave;
  851.     
  852.     if (write(fd, (char *)setups, sizeof(struct setups)) != sizeof(struct setups)) {
  853.         close(fd);
  854.         return("Warning: write fail");
  855.     }
  856.     saveKey(fd);
  857.     close(fd);
  858.     return(NULL);
  859. }
  860. #endif /* SETUP */
  861.  
  862. /*
  863. ** void clearAllTabs()
  864.  *
  865.  * clear all tab stops
  866.  */
  867. void clearAllTabs()
  868. {
  869.     memset(tabTable, '\0', MAX_COLUMN);
  870. }
  871.  
  872. /*
  873. ** void setTabs(int n)
  874.  *
  875.  * set 'n' tab stops
  876.  */
  877. void setTabs(n)
  878. int n;
  879. {
  880.     int i;
  881.  
  882.     clearAllTabs();
  883.     for (i = n; i < MAX_COLUMN; i += n)
  884.         tabTable[i] = 1;
  885. }
  886.  
  887. #ifdef SETUP
  888. /*
  889. ** void editTabs()
  890.  *
  891.  * edit tab table
  892.  */
  893. void editTabs()
  894. {
  895.     int end = 0;
  896.     u_short key;
  897.     int x, y;
  898.  
  899.     helpSystem("edit tab");
  900.     reverseMode();
  901.     cursor = YES;
  902.     cursorOnOff(YES);
  903.     x = 0;
  904.     y = SETUP_LINE+6;
  905.     while (!end) {
  906.         locate(x, y);
  907.         while ((int)(key = keyin()) == (-1))
  908.             ;
  909.         if (key == TAB) {
  910.             if (++x > LAST_COLUMN) {
  911.                 x = LAST_COLUMN;
  912.             } else {
  913.                 while (x < LAST_COLUMN && !tabTable[x])
  914.                     x++;
  915.             }
  916.         } else if (key == RIGHT_KEY || key == bindTab[C_RIGHT] ||
  917.             key == 'l' || key == CTRL('L')) {
  918.             if (++x > LAST_COLUMN) x = LAST_COLUMN;
  919.         } else if (key == LEFT_KEY || key == 'H' || key == bindTab[C_LEFT] ||
  920.             key == BS || key == DEL) {
  921.             if (--x < 0) x = 0;
  922.         } else if (key == 't' || key == 'T' || key == CR) {
  923.             conWrite('T');
  924.             tabTable[x] = 1;
  925.             if (++x > LAST_COLUMN) x = LAST_COLUMN;
  926.         } else if (key == ' ') {
  927.             conWrite(' ');
  928.             tabTable[x] = 0;
  929.             if (++x > LAST_COLUMN) x = LAST_COLUMN;
  930.         } else if (key == bindTab[C_TOL] || key == HOME_KEY) {
  931.             x = 0; 
  932.         } else if (key == bindTab[C_EOL] || key == END_KEY) {
  933.             x = LAST_COLUMN; 
  934.         } else if (key == UP_KEY || key == DOWN_KEY) {;    /* just ignore */
  935.         } else if (key == ESC) {
  936.             end = 1;
  937.         } else {
  938.             bell();
  939.         }
  940.     }
  941.     clearCopyright();
  942.     cursor = NO;
  943.     cursorOnOff(NO);
  944. }
  945.  
  946. /*
  947. ** void dispTabs()
  948.  *
  949.  * display tab stops
  950.  */
  951. void dispTabs()
  952. {
  953.     int i, j, attrSave2 = attrib;
  954.     int rev = YES;
  955.  
  956.     normalMode();        /* reset attribute    */
  957.     reverseMode();        /* reverse attribute    */
  958.     locate(0, SETUP_LINE+7);
  959.     for (i = MAX_COLUMN/10; i > 0; --i) {
  960.         for (j = 1; j <= 10; j++)
  961.             conWrite(j == 10 ? '0' : ('0'+j));
  962.         rev = !rev;
  963.         if (rev)
  964.             reverseMode();
  965.         else
  966.             normalMode();
  967.     }
  968.     reverseMode();
  969.     locate(0, SETUP_LINE+6);
  970.     for (i = 0; i < MAX_COLUMN; i++)
  971.         conWrite(tabTable[i] ? 'T' : SPACE);
  972.     attrib = attrSave2;
  973. }
  974. #else
  975. void dispTabs(){}
  976. void editTabs(){}
  977. #endif /* SETUP */
  978. /*
  979. ** void toSlash()
  980.  *
  981.  * convert 'abc\def' pathlist to 'abc/def' pathlist
  982.  *
  983.  */
  984. void toSlash(p)
  985. register char *p;
  986. {
  987.     for (; *p; p++)
  988.         if (*p == '\\')
  989.             *p = '/';            
  990. }
  991.