Home | History | Annotate | Line # | Download | only in libterminfo
tputs.c revision 1.1
      1 /* $NetBSD: tputs.c,v 1.1 2010/02/03 15:16:32 roy Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Roy Marples.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __RCSID("$NetBSD: tputs.c,v 1.1 2010/02/03 15:16:32 roy Exp $");
     32 
     33 #include <assert.h>
     34 #include <ctype.h>
     35 #include <stdio.h>
     36 #include <string.h>
     37 #include <term_private.h>
     38 #include <term.h>
     39 
     40 static const short tmspc10[] = {
     41 	0, 2000, 1333, 909, 743, 666, 500, 333, 166, 83, 55, 41, 20, 10, 5
     42 };
     43 
     44 short ospeed;
     45 char PC;
     46 
     47 static int
     48 _ti_calcdelay(const char **str, int affcnt, int *mand)
     49 {
     50 	int i;
     51 
     52        	i = 0;
     53 	/* Convert the delay */
     54 	while (isdigit(*(const unsigned char *)*str))
     55 		i = i * 10 + *(*str)++ - '0';
     56 	i *= 10;
     57 	if (*(*str) == '.') {
     58 		(*str)++;
     59 		if (isdigit(*(const unsigned char *)*str))
     60 			i += *(*str) - '0';
     61 		while (isdigit(*(const unsigned char *)*str))
     62 			(*str)++;
     63 	}
     64 	if (*(*str) == '*') {
     65 		(*str)++;
     66 		i *= affcnt;
     67 	} else if (*(*str) == '/') {
     68 		(*str)++;
     69 		if (mand != NULL)
     70 			*mand = 1;
     71 	}
     72 	return i;
     73 }
     74 
     75 static void
     76 _ti_outputdelay(int delay, short os, char pc,
     77     int (*outc)(int, void *), void *args)
     78 {
     79 	int mspc10;
     80 
     81 	if (delay < 1 || os < 1 || (size_t)os >= sizeof(tmspc10))
     82 		return;
     83 
     84 	mspc10 = tmspc10[os];
     85 	delay += mspc10 / 2;
     86 	for (delay /= mspc10; delay > 0; delay--)
     87 		outc(pc, args);
     88 }
     89 
     90 static int
     91 _ti_puts(int dodelay, int os, int pc,
     92     const char *str, int affcnt, int (*outc)(int, void *), void *args)
     93 {
     94 	int taildelay, delay, mand;
     95 
     96 	if (str == NULL)
     97 		return OK;
     98 
     99 	taildelay = _ti_calcdelay(&str, affcnt, NULL);
    100 
    101 	/* Output the string with embedded delays */
    102 	for (; *str != '\0'; str++) {
    103 		if (str[0] != '$' ||
    104 		    str[1] != '<' ||
    105 		    !(isdigit((const unsigned char)str[2]) || str[2] == '.') ||
    106 		    strchr(str + 3, '>') == NULL)
    107 		{
    108 			outc(*str, args);
    109 		} else {
    110 			str += 2;
    111 			mand = 0;
    112 			delay = _ti_calcdelay(&str, affcnt, &mand);
    113 			if (dodelay != 0 || mand != 0)
    114 				_ti_outputdelay(delay, os, pc, outc, args);
    115 		}
    116 	}
    117 
    118 	/* Delay if needed */
    119 	if (dodelay)
    120 		_ti_outputdelay(taildelay, os, pc, outc, args);
    121 
    122 	return OK;
    123 }
    124 
    125 int
    126 ti_puts(const TERMINAL *term, const char *str, int affcnt,
    127     int (*outc)(int, void *), void *args)
    128 {
    129 	int dodelay;
    130 	char pc;
    131 
    132 	_DIAGASSERT(term != NULL);
    133 	_DIAGASSERT(str != NULL);
    134 	_DIAGASSERT(outc != NULL);
    135 
    136 	dodelay = (str == t_bell(term) ||
    137 	    str == t_flash_screen(term) ||
    138 	    (t_xon_xoff(term) == 0 && t_padding_baud_rate(term) != 0));
    139 
    140 	if (t_pad_char(term) == NULL)
    141 	    pc = '\0';
    142 	else
    143 	    pc = *t_pad_char(term);
    144 	return _ti_puts(dodelay, term->_ospeed, pc,
    145 	    str, affcnt, outc, args);
    146 }
    147 
    148 int
    149 ti_putp(const TERMINAL *term, const char *str)
    150 {
    151 
    152 	_DIAGASSERT(term != NULL);
    153 	_DIAGASSERT(str != NULL);
    154 	return ti_puts(term, str, 1, (int (*)(int, void *))putchar, NULL);
    155 }
    156 
    157 int
    158 tputs(const char *str, int affcnt, int (*outc)(int))
    159 {
    160 
    161 	_DIAGASSERT(str != NULL);
    162 	_DIAGASSERT(outc != NULL);
    163 	return _ti_puts(1, ospeed, PC, str, affcnt,
    164 	    (int (*)(int, void *))outc, NULL);
    165 }
    166 
    167 int
    168 putp(const char *str)
    169 {
    170 
    171 	_DIAGASSERT(str != NULL);
    172 	return tputs(str, 1, putchar);
    173 }
    174