home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / editor / tvx_edit.arc / TVX_LIB.C < prev    next >
C/C++ Source or Header  |  1986-03-17  |  8KB  |  386 lines

  1. /* ------------------------ tvx_lib.c ---------------------------- */
  2. #include "tvx_defs.ic"
  3. #include "tvx_glbl.ic"
  4.  
  5. #ifdef COMPILESTANDARD
  6. #define STANDARD    /* the set of standard functions TVX use */
  7. #endif
  8.  
  9. #define LOCAL static    /* make locals to this module */
  10.  
  11. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
  12.  
  13. /* following are some non-standard routines required by TVX */
  14.  
  15. /* =============================>>> STCOPY <<<============================= */
  16.   stcopy(from, i, to, j)
  17.   char from[],to[];
  18.   BUFFINDEX i,*j;
  19.   { /* ## stcopy string, increment j */
  20.  
  21.     BUFFINDEX k1, k2;
  22.  
  23.     k2 = *j;
  24.     for (k1 = i; from[k1] ; )
  25.       {
  26.     to[k2++] = from[k1++];
  27.       }
  28.     to[k2] = 0;
  29.     *j = k2;
  30.   }
  31.  
  32. /* =============================>>> STRCOPY <<<============================= */
  33.   strcopy(from, i, to, j)
  34.   char from[],to[];
  35.   int i,*j;
  36.   { /* ## stcopy string, increment j */
  37.  
  38.     FAST int k1, k2;
  39.  
  40.     k2 = *j;
  41.     for (k1 = i; from[k1] ; )
  42.       {
  43.     to[k2++] = from[k1++];
  44.       }
  45.     to[k2] = 0;
  46.     *j = k2;
  47.   }
  48.  
  49. #ifndef GEMDOS
  50. /* =============================>>> MIN <<<============================= */
  51.   min(v1,v2)
  52.   int v1,v2;
  53.   {
  54.     return (v1 > v2 ? v2 : v1);
  55.   }
  56.  
  57. /* =============================>>> MAX <<<============================= */
  58.   max(v1,v2)
  59.   int v1,v2;
  60.   {
  61.     return (v1 > v2 ? v1 : v2);
  62.   }
  63. #endif
  64.  
  65. /*=============================>>> CLOWER  <<<================================*/
  66.   char clower(ch)
  67.   char ch;
  68.   {
  69.     return ((ch >='A' && ch<='Z') ? ch + ' ' : ch);
  70.   }
  71.  
  72. /*=============================>>> CUPPER  <<<================================*/
  73.   char cupper(ch)
  74.   char ch;
  75.   {
  76.     return ((ch >= 'a' && ch <= 'z') ? ch - ' ' : ch);
  77.   }
  78.  
  79. /* =========================>>> LOWER  <<<==============================*/
  80.   lower(str)
  81.   char str[];
  82.   {
  83.     FAST int i;
  84.  
  85.     for (i=0 ; str[i] ; ++i)
  86.     str[i]=clower(str[i]);
  87.  
  88.   }
  89.  
  90. /* ===========================>>> PRINTC <<<============================== */
  91.   printc(chr)
  92.   char chr;
  93.   { /* send one character to the printer */
  94.  
  95. #ifdef MSDOS
  96.     bdos(5,chr);    /* cp/m, ms-dos version */
  97. #endif
  98. #ifdef OSCPM
  99.     bdos(5,chr);    /* cp/m, ms-dos version */
  100. #endif
  101. #ifdef GEMDOS
  102.     gemdos(5,chr);    /* gemdos version */
  103. #endif
  104.   }
  105.  
  106. /*=============================>>> PROMPT <<<================================*/
  107.   prompt(msg)
  108.   char msg[];
  109.   {
  110.     SLOW int i;
  111.     i = strlen(msg);
  112.     ttwtln(msg,i);
  113. #ifdef SCR_BUF
  114.     ttflush();
  115. #endif
  116.   }
  117.  
  118. /*=============================>>> QUIT <<<================================*/
  119.   quit()
  120.   {
  121.    exit(0);
  122.   }
  123.  
  124. /*=============================>>> RINDX  <<<================================*/
  125.   rindx(str, c)
  126.   char c, str[];
  127.   {  /* rindx - find last occurrence character  c  in string  str */
  128.  
  129.     FAST int i,j;
  130.  
  131.     j = -1;
  132.     for (i = 0 ; str[i] != 0; i++)
  133.         if (str[i] == c)
  134.             j = i;
  135.     return (j);
  136.   }
  137.  
  138. /*=============================>>> REMARK <<<================================*/
  139.   remark(msg)
  140.   char msg[];
  141.   {
  142.     prompt(msg);
  143.     ttwt(CR);
  144. #ifdef USELF
  145.     ttwt(LF);
  146. #endif
  147. #ifdef SCR_BUF
  148.     ttflush();
  149. #endif
  150.   }
  151.  
  152. /*=============================>>> UPPER  <<<================================*/
  153.   upper(str)
  154.   char str[];
  155.   {
  156.     static int i;
  157.  
  158.     for (i=0 ; str[i] ; ++i)
  159.     str[i]=cupper(str[i]);
  160.   }
  161.  
  162. /*=============================>>> WTINT  <<<================================*/
  163.   wtint(intg)
  164.   int intg;
  165.   {
  166.     char chrep[10];
  167.     itoa(intg,chrep);
  168.     prompt(chrep);
  169.   }
  170.  
  171. /*=============================>>> LREPLY <<<================================*/
  172.   lreply(msg,maxc)
  173.   char msg[];
  174.   int maxc;
  175.   {
  176.     reply(msg,maxc);
  177.     lower(msg);
  178.   }
  179.  
  180. /*=============================>>> UREPLY <<<================================*/
  181.   ureply(msg,maxc)
  182.   char msg[];
  183.   int maxc;
  184.   {
  185.     reply(msg,maxc);
  186.     upper(msg);
  187.   }
  188.  
  189. /*=============================>>> REPLY <<<================================*/
  190.   reply(msg,maxc)
  191.   char msg[];
  192.   int maxc;
  193.   {
  194. #define CBS 8        /* Backspace */
  195. #define CDL1 21        /* ^U */
  196. #define CDL2 24        /* ^X */
  197. #define CABORT 3    /* ^C */
  198. #define CRET 13        /* cr */
  199. #define CESCAPE    27    /* ESC to allow any char to be entered */
  200. #define BKSPC 8
  201.  
  202.     static char ch, rp;
  203.     static int i;
  204.     SLOW int oldtty;
  205.  
  206.     oldtty = ttymode;
  207.     ttymode = FALSE;        /* change to regular mode */
  208.  
  209.     for (i = 0 ; i < maxc ; )    /* i -> next char */
  210.       {
  211.     ch = ttrd_();        /* read the character */
  212.     if (ch == CESCAPE)    /* literal next */
  213.       {
  214.         ch = ttrd_();
  215.         goto ESC_CONT;
  216.        }
  217.     if (ch == CBS)        /* back space */
  218.       {
  219.         if (i > 0)        /* must be something to delete */
  220.           {
  221.         --i;        /* wipe out char */
  222.         ttwt(BKSPC); ttwt(' '); ttwt(BKSPC);
  223.         if (msg[i] < ' ')    /* double echo ^ chrs */
  224.           {
  225.             ttwt(BKSPC); ttwt(' '); ttwt(BKSPC);
  226.           }
  227.           }
  228. #ifdef SCR_BUF
  229.         ttflush();
  230. #endif
  231.       }
  232. #ifdef USE_WIPE
  233.     else if (ch == CDL1 || ch == CDL2)    /* wipe whole line */
  234.       {
  235.         i = 0;        /* set for loop ++ */
  236.         remark("#");
  237.         prompt("Re-enter? ");
  238.       }
  239. #endif
  240.     else if (ch == CABORT && !ins_mode)
  241.       {
  242.         remark("^C");
  243.         prompt("Exit to operating system - are you sure? (y/n) ");
  244.         rp = ttrd_();
  245.         if (rp == 'y' || rp =='Y')
  246.          {
  247.         remark("y");
  248.         reset();            /* need to reset things */
  249.         exit(0);
  250.          }
  251.         remark("n");
  252.         msg[i] = 0;
  253.         prompt("Re-enter? "); prompt(msg);        /* re-echo */
  254.       }
  255.     else if (ch == CRET)        /* ret, so done */
  256.       {
  257.         remark("");
  258.         msg[i] = 0;
  259.         ttymode = oldtty;
  260.         return;
  261.       }
  262.     else
  263.       {
  264. ESC_CONT:
  265.         msg[i++] = ch;
  266.         msg[i] = 0;            /* always 0 terminate */
  267.         if (ch < ' ')
  268.           {
  269.         ch += '@';
  270.         ttwt('^');
  271.           }
  272.         ttwt(ch);            /* echo char */
  273. #ifdef SCR_BUF
  274.         ttflush();
  275. #endif
  276.       }
  277.       } /* end for */
  278.  
  279.     ttymode = oldtty;
  280.     remark("");
  281.   }
  282.  
  283. /* ============================>>> TTRD_   <<<================================ */
  284.   ttrd_()
  285.   {
  286.     SLOW char tc;
  287. #ifdef RD_FROM_CONSOLE_DIRECTLY
  288. #ifdef OSCPM
  289.     while (!(tc = bdos(6,-1)))        /* cp/m implementation */
  290.     ;
  291. #endif
  292. #ifdef MSDOS
  293.     tc = bdos(7,-1);        /* ms-dos implementation */
  294. #endif
  295. #ifdef GEMDOS
  296.     tc = gemdos(7);        /* ms-dos implementation */
  297. #endif
  298. #ifdef UNIX
  299.     tc = ttrd();
  300. #endif
  301. #else
  302.     gkbd(&tc);            /* this should work */
  303. #endif
  304.  
  305.     return (tc & 0377);
  306.  
  307.   }
  308.  
  309. /*=============================>>> RDINT <<<================================*/
  310.   rdint(val)
  311.   int *val;
  312.   {
  313.     char chrrep[12];
  314.     reply(chrrep,11);
  315.     *val = atoi(chrrep);
  316.     return;
  317.   }
  318.  
  319. /* =============================>>> ITOA   <<<============================= */
  320.   itoa(intg, str)
  321.   int intg;
  322.   char str[];
  323.   {  /* itoa - convert integer  int  to char string in  str */
  324.  
  325.     FAST int i;
  326.     int d, intval, j;
  327.     char k;
  328.     static char digits[] = "0123456789";
  329.  
  330.     intval = intg >= 0 ? intg : (-intg);
  331.     str[0] = 0;
  332.     i = 0;
  333.     do
  334.       {                /* generate digits */
  335.         i++;
  336.         d = intval % 10;    /* mod 10 */
  337.         str[i] = digits[d];
  338.         intval = intval / 10;
  339.       }
  340.     while (intval != 0);
  341.     if (intg < 0)
  342.       {                /* then sign */
  343.         str[++i] = '-';
  344.       }
  345.     for (j = 0 ; j < i ; j++ )
  346.       {                /* then reverse */
  347.         k = str[i];
  348.         str[i--] = str[j];
  349.         str[j] = k;
  350.       }
  351.   }
  352.  
  353. /* ------------------------------------------------------------------------- */
  354. #ifdef STANDARD
  355.  
  356. /* ============================>>> ATOI   <<<================================ */
  357.   atoi(in)
  358.   char in[];
  359.   {  /* atoi - convert string : Ascii machines! */
  360.  
  361.     FAST int i;
  362.     int d, val, neg;
  363.     
  364.     for (i=0 ; in[i] == ' ' || in[i] == '\t' ; i++)
  365.         ;
  366.     if (in[i] == '-')        /* look for negative */
  367.       {
  368.     i++;
  369.     neg=1;
  370.       }
  371.     else
  372.     neg=0;
  373.     for (val = 0; in[i] ; i++)
  374.       {
  375.     if (in[i]<'0' || in[i]>'9')
  376.         break;
  377.     d = in[i]-'0';
  378.         val = 10 * val + d;
  379.       }
  380.     if (neg)
  381.     val = (-val);
  382.     return (val);
  383.   }
  384. #endif
  385. /* ------------------------ tvx_lib.c ---------------------------- */
  386.