home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0636.ZIP / CCE_0636 / CURSES / CRSSRC12.ZOO / src / tputs.c < prev    next >
C/C++ Source or Header  |  1991-09-27  |  5KB  |  228 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *            Copyright (c) 1982, Fred Fish            *
  4.  *                All Rights Reserved                *
  5.  *                                    *
  6.  *    This software and/or documentation is released for public    *
  7.  *    distribution for personal, non-commercial use only.        *
  8.  *    Limited rights to use, modify, and redistribute are hereby    *
  9.  *    granted for non-commercial purposes, provided that all        *
  10.  *    copyright notices remain intact and all changes are clearly    *
  11.  *    documented.  The author makes no warranty of any kind with    *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular    *
  14.  *    purpose.                            *
  15.  *                                    *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20. /*
  21.  *  LIBRARY FUNCTION
  22.  *
  23.  *    tputs     output string with appropriate padding
  24.  *
  25.  *  KEY WORDS
  26.  *
  27.  *    termcap
  28.  *
  29.  *  SYNOPSIS
  30.  *
  31.  *    tputs(cp,affcnt,outc)
  32.  *    char *cp;
  33.  *    int affcnt;
  34.  *    int (*outc)();
  35.  *
  36.  *  DESCRIPTION
  37.  *
  38.  *    Outputs string pointed to by cp, using function outc, and
  39.  *    following it with the appropriate number of padding characters.
  40.  *    Affcnt contains the number of lines affected, which is used
  41.  *    as a multiplier for the specified per line pad time.  If
  42.  *    per line pad count is not applicable, affcnt should be 1,
  43.  *    NOT zero.
  44.  *
  45.  *    The format of the string pointed to by cp is:
  46.  *
  47.  *        [pad time][*]<string to send>
  48.  *
  49.  *        where:    pad time => time to delay in milliseconds
  50.  *            * => specifies that time is per line
  51.  *            
  52.  *    The pad character is assumed to reside in the external
  53.  *    variable "PC".  Also, the external variable "ospeed"
  54.  *    should contain the output speed of the terminal as
  55.  *    encoded in /usr/include/sgtty.h  (B0-B9600).
  56.  *
  57.  *  BUGS
  58.  *
  59.  *    Digit conversion is based on native character set
  60.  *    being ASCII.
  61.  *
  62.  */
  63.  
  64. /*
  65.  *    Miscellaneous stuff
  66.  */
  67.  
  68. #include <stdio.h>
  69. #include <ctype.h>
  70. #include <termcap.h>
  71.  
  72. #ifndef _COMPILER_H
  73. #  include <compiler.h>
  74. #endif
  75.  
  76. # ifdef SGTTY
  77. extern char PC;            /* Pad character to use */
  78. extern char ospeed;        /* Encoding of output speed */
  79.  
  80. static int _times[] = {
  81.     0,                /* Tenths of ms per char 0 baud */
  82.     2000,            /* Tenths of ms per char 50 baud */
  83.     1333,            /* Tenths of ms per char 75 baud */
  84.     909,            /* Tenths of ms per char 110 baud */
  85.     743,            /* Tenths of ms per char 134 baud */
  86.     666,            /* Tenths of ms per char 150 baud */
  87.     500,            /* Tenths of ms per char 200 baud */
  88.     333,            /* Tenths of ms per char 300 baud */
  89.     166,            /* Tenths of ms per char 600 baud */
  90.     83,                /* Tenths of ms per char 1200 baud */
  91.     55,                /* Tenths of ms per char 1800 baud */
  92.     41,                /* Tenths of ms per char 2400 baud */
  93.     20,                /* Tenths of ms per char 4800 baud */
  94.     10                /* Tenths of ms per char 9600 baud */
  95. };
  96. # endif
  97.  
  98. # ifdef SGTTY
  99. static void do_padding __PROTO((int ptime, int (*outc )(int)));
  100. # endif
  101.  
  102.  
  103. /*
  104.  *  PSEUDO CODE
  105.  *
  106.  *    Begin tgoto
  107.  *        If string pointer is invalid then
  108.  *        Return without doing anything.
  109.  *        Else
  110.  *        For each pad digit (if any)
  111.  *            Do decimal left shift.
  112.  *            Accumulate the lower digit.
  113.  *        End for
  114.  *        Adjust scale to tenths of milliseconds
  115.  *        If there is a fractional field
  116.  *            Skip the decimal point.
  117.  *            If there is a valid tenths digit
  118.  *            Accumulate the tenths.
  119.  *            End if
  120.  *            Discard remaining digits.
  121.  *        End if
  122.  *        If per line is specified then
  123.  *            Adjust the pad time.
  124.  *            Discard the per line flag char.
  125.  *        End if
  126.  *        While there are any characters left
  127.  *            Send them out via output function.
  128.  *        End while
  129.  *        Transmit any padding required.
  130.  *        End if
  131.  *    End tgoto
  132.  *
  133.  */
  134.  
  135. void tputs(cp,affcnt,outc)
  136. char *cp;
  137. int affcnt;
  138. int (*outc) __PROTO((int));
  139. {
  140.     int ptime;            /* Pad time in tenths of milliseconds */
  141.  
  142.     if (cp == NULL || *cp == '\0') {
  143.     return;
  144.     } else {
  145.     for (ptime = 0; isdigit(*cp); cp++) {
  146.         ptime *= 10;
  147.         ptime += (*cp - '0');
  148.     }
  149.     ptime *= 10;
  150.     if (*cp == '.') {
  151.         cp++;
  152.         if (isdigit(*cp)) {
  153.         ptime += (*cp++ - '0');
  154.         }
  155.         while (isdigit(*cp)) {cp++;}
  156.     }
  157.     if (*cp == '*') {
  158.         ptime *= affcnt;
  159.         cp++;
  160.     }
  161.     while (*cp != '\0') {
  162.         (*outc)(*cp++);
  163.     }
  164. # ifdef SGTTY
  165.     do_padding(ptime,outc);
  166. # endif
  167.     }
  168. }
  169.  
  170. # ifdef SGTTY
  171. /*
  172.  *  FUNCTION
  173.  *
  174.  *    do_padding    transmit any pad characters required
  175.  *
  176.  *  SYNOPSIS
  177.  *
  178.  *    static do_padding(ptime,outc)
  179.  *    int ptime;
  180.  *    int (*outc)();
  181.  *
  182.  *  DESCRIPTION
  183.  *
  184.  *    Does any padding required as specified by ptime (in tenths
  185.  *    of milliseconds), the output speed given in the external
  186.  *    variable ospeed, and the pad character given in the
  187.  *    external variable PC.
  188.  *
  189.  */
  190.  
  191. /*
  192.  *  PSEUDO CODE
  193.  *
  194.  *    Begin do_padding
  195.  *        If there is a non-zero pad time then
  196.  *        If the external speed is in range then
  197.  *            Look up the delay per pad character.
  198.  *            Round pad time up by half a character.
  199.  *            Compute number of characters to send.
  200.  *            For each pad character to send
  201.  *            Transmit the pad character.
  202.  *            End for
  203.  *        End if
  204.  *        End if
  205.  *    End do_padding
  206.  *
  207.  */
  208.  
  209. static void do_padding(ptime,outc)
  210. int ptime;
  211. int (*outc) __PROTO((int));
  212. {
  213.     register int nchars;
  214.     register int tpc;
  215.  
  216.     if (ptime != 0) {
  217.     if (ospeed >= 0 && ospeed <= (sizeof(_times)/ sizeof(int))) {
  218.         tpc = _times[ospeed];
  219.         ptime += (tpc / 2);
  220.         nchars = ptime / tpc;
  221.         for ( ; nchars > 0; --nchars) {
  222.         (*outc)(PC);
  223.         }
  224.     }
  225.     }
  226. }
  227. # endif
  228.