tputs.c (5220B)
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 70 # ifndef MSDOS 71 extern char PC; /* Pad character to use */ 72 extern char ospeed; /* Encoding of output speed */ 73 74 static int times[] = { 75 0, /* Tenths of ms per char 0 baud */ 76 2000, /* Tenths of ms per char 50 baud */ 77 1333, /* Tenths of ms per char 75 baud */ 78 909, /* Tenths of ms per char 110 baud */ 79 743, /* Tenths of ms per char 134 baud */ 80 666, /* Tenths of ms per char 150 baud */ 81 500, /* Tenths of ms per char 200 baud */ 82 333, /* Tenths of ms per char 300 baud */ 83 166, /* Tenths of ms per char 600 baud */ 84 83, /* Tenths of ms per char 1200 baud */ 85 55, /* Tenths of ms per char 1800 baud */ 86 41, /* Tenths of ms per char 2400 baud */ 87 20, /* Tenths of ms per char 4800 baud */ 88 10 /* Tenths of ms per char 9600 baud */ 89 }; 90 # endif 91 92 93 /* 94 * PSEUDO CODE 95 * 96 * Begin tgoto 97 * If string pointer is invalid then 98 * Return without doing anything. 99 * Else 100 * For each pad digit (if any) 101 * Do decimal left shift. 102 * Accumulate the lower digit. 103 * End for 104 * Adjust scale to tenths of milliseconds 105 * If there is a fractional field 106 * Skip the decimal point. 107 * If there is a valid tenths digit 108 * Accumulate the tenths. 109 * End if 110 * Discard remaining digits. 111 * End if 112 * If per line is specified then 113 * Adjust the pad time. 114 * Discard the per line flag char. 115 * End if 116 * While there are any characters left 117 * Send them out via output function. 118 * End while 119 * Transmit any padding required. 120 * End if 121 * End tgoto 122 * 123 */ 124 125 tputs(cp,affcnt,outc) 126 char *cp; 127 int affcnt; 128 int (*outc)(); 129 { 130 int ptime; /* Pad time in tenths of milliseconds */ 131 extern int isdigit(); 132 133 if (cp == NULL || *cp == NULL) { 134 return; 135 } else { 136 for (ptime = 0; isdigit(*cp); cp++) { 137 ptime *= 10; 138 ptime += (*cp - '0'); 139 } 140 ptime *= 10; 141 if (*cp == '.') { 142 cp++; 143 if (isdigit(*cp)) { 144 ptime += (*cp++ - '0'); 145 } 146 while (isdigit(*cp)) {cp++;} 147 } 148 if (*cp == '*') { 149 ptime *= affcnt; 150 cp++; 151 } 152 while (*cp != NULL) { 153 (*outc)(*cp++); 154 } 155 # ifndef MSDOS 156 do_padding(ptime,outc); 157 # endif 158 } 159 } 160 161 # ifndef MSDOS 162 /* 163 * FUNCTION 164 * 165 * do_padding transmit any pad characters required 166 * 167 * SYNOPSIS 168 * 169 * static do_padding(ptime,outc) 170 * int ptime; 171 * int (*outc)(); 172 * 173 * DESCRIPTION 174 * 175 * Does any padding required as specified by ptime (in tenths 176 * of milliseconds), the output speed given in the external 177 * variable ospeed, and the pad character given in the 178 * external variable PC. 179 * 180 */ 181 182 /* 183 * PSEUDO CODE 184 * 185 * Begin do_padding 186 * If there is a non-zero pad time then 187 * If the external speed is in range then 188 * Look up the delay per pad character. 189 * Round pad time up by half a character. 190 * Compute number of characters to send. 191 * For each pad character to send 192 * Transmit the pad character. 193 * End for 194 * End if 195 * End if 196 * End do_padding 197 * 198 */ 199 200 static do_padding(ptime,outc) 201 int ptime; 202 int (*outc)(); 203 { 204 register int nchars; 205 register int tpc; 206 207 if (ptime != 0) { 208 if (ospeed >= 0 && ospeed <= (sizeof(times)/ sizeof(int))) { 209 tpc = times[ospeed]; 210 ptime += (tpc / 2); 211 nchars = ptime / tpc; 212 for ( ; nchars > 0; --nchars) { 213 (*outc)(PC); 214 } 215 } 216 } 217 } 218 # endif