infotocap.c revision 2
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 2004 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/*
2N/A Routines to convert terminfo string parameters to termcap form.
2N/A Not all terminfo strings will be, or could be, converted.
2N/A
2N/A The following parameter forms will at least be handled.
2N/A
2N/A %p1%d -> %d
2N/A %p1%2.2d -> %2
2N/A %p1%3.3d -> %3
2N/A %p1%02d -> %2
2N/A %p1%03d -> %3
2N/A %p1%c -> %.
2N/A %p1%'x'%+%c -> %+x
2N/A %i -> %i
2N/A %p2 before %p1 -> %r
2N/A %p1%'x'% > %+%p1%'y'%+%; -> % > xy
2N/A*/
2N/A
2N/A#include "curses.h"
2N/A
2N/A/* externs from libc */
2N/Aextern char *strcpy();
2N/A#if !defined(SYSV) && !defined(USG) && !defined(strchr)
2N/A /* handle both Sys Vr2 and Vr3 curses */
2N/A#define strchr index
2N/A#endif /* SYSV || USG */
2N/Aextern char *strchr();
2N/A
2N/A#define NULLPTR ((char *) 0)
2N/A
2N/A/*
2N/A lookat looks at a string such as "%p1%d" and a pattern such as "%p*%d",
2N/A where '*' is the only wild character. Each place that the star matches,
2N/A the corresponding character in the string is placed in args. If the
2N/A pattern matches the string, 1 is returned.
2N/A*/
2N/A
2N/Astatic int
2N/Alookat(char *string, char *pattern, char *args)
2N/A{
2N/A int val, pat;
2N/A
2N/A while ((pat = *pattern++) && (val = *string++))
2N/A if (pat == '*')
2N/A *args++ = val;
2N/A else if (val != pat)
2N/A return (0);
2N/A if (pat == '\0')
2N/A return (1);
2N/A else
2N/A return (0);
2N/A}
2N/A
2N/Astatic int currentparm, otherparm, reversedparms;
2N/Astatic char *newvalue;
2N/Astatic char _newvalue[1024] = "!!! MUST CHANGE BY HAND !!!";
2N/A#define BYHANDMSGLEN 27
2N/A
2N/Astatic void setparms();
2N/A
2N/A/*
2N/A Setparms() and checkparms() are used by infotocap() to make
2N/A sure that the parameters in the terminfo entry alternate and are
2N/A only '1' or '2'. If the order has been reversed, then %r is added
2N/A in to the new value being built.
2N/A */
2N/A
2N/Astatic void
2N/Asetparms()
2N/A{
2N/A currentparm = 1;
2N/A otherparm = 2;
2N/A reversedparms = 0;
2N/A newvalue = &_newvalue[BYHANDMSGLEN];
2N/A return;
2N/A}
2N/A
2N/Astatic int
2N/Acheckparms(int arg)
2N/A{
2N/A arg -= '0';
2N/A if (arg != 1 && arg != 2)
2N/A return (1);
2N/A else if (arg != currentparm)
2N/A if (reversedparms)
2N/A return (1);
2N/A else if (!reversedparms && arg == otherparm) {
2N/A (void) strcpy(newvalue, "%r");
2N/A newvalue += 2;
2N/A reversedparms = TRUE;
2N/A } else
2N/A return (1);
2N/A else {
2N/A otherparm = currentparm;
2N/A currentparm = 3 - currentparm;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A Infotocap looks at the string capability to see if it has any
2N/A stack manipulation operators. If possible, they are converted to
2N/A termcap form. If any operator is found that cannot be modified,
2N/A prepend a message to the beginning of the original value and
2N/A set err to 1. A pointer to the new copy of the string is returned
2N/A in either case.
2N/A*/
2N/A
2N/Achar
2N/A*infotocap(char *value, int *err)
2N/A{
2N/A char args[4];
2N/A char *savevalue;
2N/A
2N/A *err = 0;
2N/A if (strchr(value, '%') == NULLPTR)
2N/A return (value);
2N/A
2N/A setparms();
2N/A
2N/A savevalue = value;
2N/A while (*value)
2N/A if (*value != '%')
2N/A *newvalue++ = *value++;
2N/A else if (lookat(value, "%p*%d", args)) {
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) strcpy(newvalue, "%d");
2N/A newvalue += 2;
2N/A value += 5;
2N/A } else if (lookat(value, "%p*%02d", args)) {
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) strcpy(newvalue, "%2");
2N/A newvalue += 2;
2N/A value += 7;
2N/A } else if (lookat(value, "%p*%03d", args)) {
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) strcpy(newvalue, "%3");
2N/A newvalue += 2;
2N/A value += 7;
2N/A } else if (lookat(value, "%p*%2.2d", args)) {
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) strcpy(newvalue, "%2");
2N/A newvalue += 2;
2N/A value += 8;
2N/A } else if (lookat(value, "%p*%3.3d", args)) {
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) strcpy(newvalue, "%3");
2N/A newvalue += 2;
2N/A value += 8;
2N/A } else if (lookat(value, "%p*%c", args)) {
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) strcpy(newvalue, "%.");
2N/A newvalue += 2;
2N/A value += 5;
2N/A } else if (lookat(value, "%p*%'*'%+%c", args)) {
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) sprintf(newvalue, "%%+%c", args[1]);
2N/A newvalue += 3;
2N/A value += 11;
2N/A } else if (lookat(value, "%i", args)) {
2N/A (void) strcpy(newvalue, "%i");
2N/A newvalue += 2;
2N/A value += 2;
2N/A } else if (lookat(value, "%%", args)) {
2N/A (void) strcpy(newvalue, "%%");
2N/A newvalue += 2;
2N/A value += 2;
2N/A } else if (lookat(value, "p*%'*'%>%+%p*%'*'%+%;", args)) {
2N/A if (args[0] != args[2])
2N/A goto dobyhand;
2N/A if (checkparms(args[0]))
2N/A goto dobyhand;
2N/A (void) sprintf(newvalue, "%%>%c%c", args[1], args[3]);
2N/A newvalue += 2;
2N/A value += 21;
2N/A } else
2N/A goto dobyhand;
2N/A
2N/A *newvalue = '\0';
2N/A return (&_newvalue[BYHANDMSGLEN]);
2N/A
2N/Adobyhand:
2N/A (void) strcpy(&_newvalue[BYHANDMSGLEN], savevalue);
2N/A *err = 1;
2N/A return (_newvalue);
2N/A}