2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * University Copyright- Copyright (c) 1982, 1986, 1988
2N/A * The Regents of the University of California
2N/A * All Rights Reserved
2N/A *
2N/A * University Acknowledgment- Portions of this document are derived from
2N/A * software developed by the University of California, Berkeley, and its
2N/A * contributors.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <ctype.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include "print.h"
2N/A
2N/A#define BACKSLASH '\\'
2N/A#define BACKBACK "\\\\"
2N/A
2N/Aextern char *strcat();
2N/A
2N/Astatic char retbuffer[1024];
2N/Astatic char ret2buffer[1024];
2N/A
2N/A/*
2N/A * Remove the padding sequences from the input string.
2N/A * Return the new string without the padding sequences
2N/A * and the padding itself in padbuffer.
2N/A */
2N/Achar
2N/A*rmpadding(char *str, char *padbuffer, int *padding)
2N/A{
2N/A static char rmbuffer[1024];
2N/A register char ch;
2N/A register char *pbufptr;
2N/A register char *retptr = rmbuffer;
2N/A char *svbufptr;
2N/A int padbylines = 0;
2N/A int paddigits = 0;
2N/A int paddecimal = 0;
2N/A
2N/A padbuffer[0] = rmbuffer[0] = '\0';
2N/A if (padding)
2N/A *padding = 0;
2N/A if (str == NULL)
2N/A return (rmbuffer);
2N/A
2N/A while (ch = (*str++ & 0377))
2N/A switch (ch) {
2N/A case '$':
2N/A if (*str == '<') {
2N/A svbufptr = ++str; /* skip '<' */
2N/A
2N/A /* copy number */
2N/A pbufptr = padbuffer;
2N/A for (; *str && isdigit(*str); str++) {
2N/A *svbufptr++ = *str;
2N/A *pbufptr++ = *str;
2N/A }
2N/A *pbufptr = '\0';
2N/A paddigits += atoi(padbuffer);
2N/A /* check for decimal */
2N/A if (*str == '.') {
2N/A str++;
2N/A pbufptr = padbuffer;
2N/A for (; *str && isdigit(ch);
2N/A str++) {
2N/A *svbufptr++ = *str;
2N/A *pbufptr++ = *str;
2N/A }
2N/A *pbufptr = '\0';
2N/A paddecimal += atoi(padbuffer);
2N/A }
2N/A for (; (*str == '*') || (*str == '/');
2N/A str++) {
2N/A if (*str == '*')
2N/A padbylines = 1;
2N/A /* Termcap does not support mandatory padding */
2N/A /* marked with /. Just remove it. */
2N/A else {
2N/A extern char *progname;
2N/A (void) fprintf(stderr,
2N/A "%s: mandatory "
2N/A "padding removed\n",
2N/A progname);
2N/A }
2N/A }
2N/A /* oops, not a padding spec after all */
2N/A /* put us back after the '$<' */
2N/A if (*str != '>') {
2N/A str = svbufptr;
2N/A *retptr++ = '$';
2N/A *retptr++ = '<';
2N/A } else
2N/A str++; /* skip the '>' */
2N/A /* Flag padding info that is not at the end */
2N/A /* of the string. */
2N/A if (*str != '\0') {
2N/A extern char *progname;
2N/A (void) fprintf(stderr,
2N/A "%s: padding information "
2N/A "moved to end\n", progname);
2N/A }
2N/A } else
2N/A *retptr++ = ch;
2N/A break;
2N/A
2N/A default:
2N/A *retptr++ = ch;
2N/A }
2N/A *retptr = '\0';
2N/A
2N/A if (paddecimal > 10) {
2N/A paddigits += paddecimal / 10;
2N/A paddecimal %= 10;
2N/A }
2N/A
2N/A if (paddigits > 0 && paddecimal > 0)
2N/A (void) sprintf(padbuffer, "%d.%d", paddigits, paddecimal);
2N/A else if (paddigits > 0)
2N/A (void) sprintf(padbuffer, "%d", paddigits);
2N/A else if (paddecimal > 0)
2N/A (void) sprintf(padbuffer, ".%d", paddecimal);
2N/A if (padbylines)
2N/A (void) strcat(padbuffer, "*");
2N/A if (padding)
2N/A *padding = paddecimal;
2N/A return (rmbuffer);
2N/A}
2N/A
2N/A/*
2N/A * Convert a character, making appropriate changes to make it printable
2N/A * for a termcap source entry. Change escape, tab, etc., into their
2N/A * appropriate equivalents. Return the number of characters printed.
2N/A */
2N/Achar
2N/A*cconvert(char *string)
2N/A{
2N/A register int c;
2N/A register char *retptr = retbuffer;
2N/A
2N/A retbuffer[0] = '\0';
2N/A if (string == NULL)
2N/A return (retbuffer);
2N/A
2N/A while (c = *string++) {
2N/A /* should check here to make sure that there is enough room */
2N/A /* in retbuffer and realloc it if necessary. */
2N/A c &= 0377;
2N/A /* we ignore the return value from sprintf because BSD/V7 */
2N/A /* systems return a (char *) rather than an int. */
2N/A if (c >= 0200) {
2N/A (void) sprintf(retptr, "\\%.3o", c); retptr += 4; }
2N/A else if (c == '\033') {
2N/A (void) sprintf(retptr, "\\E"); retptr += 2; }
2N/A else if (c == '\t') {
2N/A (void) sprintf(retptr, "\\t"); retptr += 2; }
2N/A else if (c == '\b') {
2N/A (void) sprintf(retptr, "\\b"); retptr += 2; }
2N/A else if (c == '\f') {
2N/A (void) sprintf(retptr, "\\f"); retptr += 2; }
2N/A else if (c == '\n') {
2N/A (void) sprintf(retptr, "\\n"); retptr += 2; }
2N/A else if (c == '\r') {
2N/A (void) sprintf(retptr, "\\r"); retptr += 2; }
2N/A
2N/A /* unfortunately \: did not work */
2N/A else if (c == ':') {
2N/A (void) sprintf(retptr, "\\072"); retptr += 4; }
2N/A else if (c == '^') {
2N/A (void) sprintf(retptr, "\\^"); retptr += 2; }
2N/A else if (c == BACKSLASH) {
2N/A (void) sprintf(retptr, BACKBACK); retptr += 2; }
2N/A else if (c < ' ' || c == 0177) {
2N/A (void) sprintf(retptr, "^%c", c ^ 0100); retptr += 2; }
2N/A else {
2N/A (void) sprintf(retptr, "%c", c); retptr++; }
2N/A }
2N/A *retptr = '\0';
2N/A return (retbuffer);
2N/A}
2N/A
2N/A/*
2N/A * Convert the terminfo string into a termcap string.
2N/A * Most of the work is done by rmpadding() above and cconvert(); this
2N/A * function mainly just pieces things back together. A pointer to the
2N/A * return buffer is returned.
2N/A *
2N/A * NOTE: Some things can not be done at all: converting the terminfo
2N/A * parameterized strings into termcap parameterized strings.
2N/A */
2N/A
2N/Achar
2N/A*cexpand(char *str)
2N/A{
2N/A char padbuffer[512];
2N/A char *retptr;
2N/A
2N/A retptr = rmpadding(str, padbuffer, (int *)0);
2N/A (void) sprintf(ret2buffer, "%s%s", padbuffer, cconvert(retptr));
2N/A
2N/A return (ret2buffer);
2N/A}
2N/A
2N/A/*
2N/A * Print out a string onto a stream, changing unprintables into
2N/A * termcap printables.
2N/A */
2N/Aint
2N/Acpr(FILE *stream, char *string)
2N/A{
2N/A register char *ret;
2N/A if (string != NULL) {
2N/A ret = cexpand(string);
2N/A (void) fprintf(stream, "%s", ret);
2N/A return (strlen(ret));
2N/A } else
2N/A return (0);
2N/A}