home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 178_01 / tvx_lib.c < prev    next >
C/C++ Source or Header  |  1986-01-17  |  8KB  |  362 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 GEMDOS
  99.     gemdos(5,chr);    /* gemdos version */
  100. #endif
  101.   }
  102.  
  103. /*=============================>>> PROMPT <<<================================*/
  104.   prompt(msg)
  105.   char msg[];
  106.   {
  107.     SLOW int i;
  108.     i = strlen(msg);
  109.     ttwtln(msg,i);
  110.   }
  111.  
  112. /*=============================>>> QUIT <<<================================*/
  113.   quit()
  114.   {
  115.    exit(0);
  116.   }
  117.  
  118. /*=============================>>> RINDEX  <<<================================*/
  119.   rindex(str, c)
  120.   char c, str[];
  121.   {  /* rindex - find last occurrence character  c  in string  str */
  122.  
  123.     FAST int i,j;
  124.  
  125.     j = -1;
  126.     for (i = 0 ; str[i] != 0; i++)
  127.         if (str[i] == c)
  128.             j = i;
  129.     return (j);
  130.   }
  131.  
  132. /*=============================>>> REMARK <<<================================*/
  133.   remark(msg)
  134.   char msg[];
  135.   {
  136.     prompt(msg);
  137.     ttwt(CR);
  138. #ifdef USELF
  139.     ttwt(LF);
  140. #endif
  141.   }
  142.  
  143. /*=============================>>> UPPER  <<<================================*/
  144.   upper(str)
  145.   char str[];
  146.   {
  147.     static int i;
  148.  
  149.     for (i=0 ; str[i] ; ++i)
  150.     str[i]=cupper(str[i]);
  151.   }
  152.  
  153. /*=============================>>> WTINT  <<<================================*/
  154.   wtint(intg)
  155.   int intg;
  156.   {
  157.     char chrep[10];
  158.     itoa(intg,chrep);
  159.     prompt(chrep);
  160.   }
  161.  
  162. /*=============================>>> LREPLY <<<================================*/
  163.   lreply(msg,maxc)
  164.   char msg[];
  165.   int maxc;
  166.   {
  167.     reply(msg,maxc);
  168.     lower(msg);
  169.   }
  170.  
  171. /*=============================>>> UREPLY <<<================================*/
  172.   ureply(msg,maxc)
  173.   char msg[];
  174.   int maxc;
  175.   {
  176.     reply(msg,maxc);
  177.     upper(msg);
  178.   }
  179.  
  180. /*=============================>>> REPLY <<<================================*/
  181.   reply(msg,maxc)
  182.   char msg[];
  183.   int maxc;
  184.   {
  185. #define CBS 8        /* Backspace */
  186. #define CDL1 21        /* ^U */
  187. #define CDL2 24        /* ^X */
  188. #define CABORT 3    /* ^C */
  189. #define CRET 13        /* cr */
  190. #define BACKSPACE 8
  191.  
  192.     static char ch, rp;
  193.     static int i;
  194.     SLOW int oldtty;
  195.  
  196.     oldtty = ttymode;
  197.     ttymode = FALSE;        /* change to regular mode */
  198.  
  199.     for (i = 0 ; i < maxc ; )    /* i -> next char */
  200.       {
  201.     ch = ttrd_();        /* read the character */
  202.     if (ch == CBS)        /* back space */
  203.       {
  204.         if (i > 0)        /* must be something to delete */
  205.           {
  206.         --i;        /* wipe out char */
  207.         ttwt(BACKSPACE); ttwt(' '); ttwt(BACKSPACE);
  208.         if (msg[i] < ' ')    /* double echo ^ chrs */
  209.           {
  210.             ttwt(BACKSPACE); ttwt(' '); ttwt(BACKSPACE);
  211.           }
  212.           }
  213.       }
  214. #ifdef USE_WIPE
  215.     else if (ch == CDL1 || ch == CDL2)    /* wipe whole line */
  216.       {
  217.         i = 0;        /* set for loop ++ */
  218.         remark("#");
  219.         prompt("Re-enter? ");
  220.       }
  221. #endif
  222.     else if (ch == CABORT && !ins_mode)
  223.       {
  224.         remark("^C");
  225.         prompt("Exit to operating system - are you sure? (y/n) ");
  226.         rp = ttrd_();
  227.         if (rp == 'y' || rp =='Y')
  228.          {
  229.         remark("y");
  230.         reset();            /* need to reset things */
  231.         exit(0);
  232.          }
  233.         remark("n");
  234.         msg[i] = 0;
  235.         prompt("Re-enter? "); prompt(msg);        /* re-echo */
  236.       }
  237.     else if (ch == CRET)        /* ret, so done */
  238.       {
  239.         remark("");
  240.         msg[i] = 0;
  241.         ttymode = oldtty;
  242.         return;
  243.       }
  244.     else
  245.       {
  246.         msg[i++] = ch;
  247.         msg[i] = 0;            /* always 0 terminate */
  248.         if (ch < ' ')
  249.           {
  250.         ch += '@';
  251.         ttwt('^');
  252.           }
  253.         ttwt(ch);            /* echo char */
  254.       }
  255.       } /* end for */
  256.  
  257.     ttymode = oldtty;
  258.     remark("");
  259.   }
  260.  
  261. /* ============================>>> TTRD_   <<<================================ */
  262.   ttrd_()
  263.   {
  264.     SLOW int tc;
  265.  
  266. #ifdef OSCPM
  267.     while (!(tc = bdos(6,-1)))        /* cp/m implementation */
  268.     ;
  269. #endif
  270. #ifdef MSDOS
  271.     tc = bdos(7,-1);        /* ms-dos implementation */
  272. #endif
  273. #ifdef GEMDOS
  274.     tc = gemdos(7);        /* ms-dos implementation */
  275. #endif
  276. #ifdef UNIX
  277.     tc = ttrd();
  278. #endif
  279.  
  280.     return (tc & 0377);
  281.  
  282.   }
  283.  
  284. /*=============================>>> RDINT <<<================================*/
  285.   rdint(val)
  286.   int *val;
  287.   {
  288.     char chrrep[12];
  289.     reply(chrrep,11);
  290.     *val = atoi(chrrep);
  291.     return;
  292.   }
  293.  
  294. /* =============================>>> ITOA   <<<============================= */
  295.   itoa(intg, str)
  296.   int intg;
  297.   char str[];
  298.   {  /* itoa - convert integer  int  to char string in  str */
  299.  
  300.     FAST int i;
  301.     int d, intval, j;
  302.     char k;
  303.     static char digits[] = "0123456789";
  304.  
  305.     intval = intg >= 0 ? intg : (-intg);
  306.     str[0] = 0;
  307.     i = 0;
  308.     do
  309.       {                /* generate digits */
  310.         i++;
  311.         d = intval % 10;    /* mod 10 */
  312.         str[i] = digits[d];
  313.         intval = intval / 10;
  314.       }
  315.     while (intval != 0);
  316.     if (intg < 0)
  317.       {                /* then sign */
  318.         str[++i] = '-';
  319.       }
  320.     for (j = 0 ; j < i ; j++ )
  321.       {                /* then reverse */
  322.         k = str[i];
  323.         str[i--] = str[j];
  324.         str[j] = k;
  325.       }
  326.   }
  327.  
  328. /* ------------------------------------------------------------------------- */
  329. #ifdef STANDARD
  330.  
  331. /* ============================>>> ATOI   <<<================================ */
  332.   atoi(in)
  333.   char in[];
  334.   {  /* atoi - convert string : Ascii machines! */
  335.  
  336.     FAST int i;
  337.     int d, val, neg;
  338.     
  339.     for (i=0 ; in[i] == ' ' || in[i] == '\t' ; i++)
  340.         ;
  341.     if (in[i] == '-')        /* look for negative */
  342.       {
  343.     i++;
  344.     neg=1;
  345.       }
  346.     else
  347.     neg=0;
  348.     for (val = 0; in[i] ; i++)
  349.       {
  350.     if (in[i]<'0' || in[i]>'9')
  351.         break;
  352.     d = in[i]-'0';
  353.         val = 10 * val + d;
  354.       }
  355.     if (neg)
  356.     val = (-val);
  357.     return (val);
  358.   }
  359.  
  360. #endif
  361. /*--------------------------------- tvx_lib.c ------------------------- */
  362.