Home | History | Annotate | Line # | Download | only in libterminfo
setupterm.c revision 1.2
      1  1.2  roy /* $NetBSD: setupterm.c,v 1.2 2010/02/11 00:27:09 roy Exp $ */
      2  1.1  roy 
      3  1.1  roy /*
      4  1.1  roy  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.1  roy  *
      6  1.1  roy  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  roy  * by Roy Marples.
      8  1.1  roy  *
      9  1.1  roy  * Redistribution and use in source and binary forms, with or without
     10  1.1  roy  * modification, are permitted provided that the following conditions
     11  1.1  roy  * are met:
     12  1.1  roy  * 1. Redistributions of source code must retain the above copyright
     13  1.1  roy  *    notice, this list of conditions and the following disclaimer.
     14  1.1  roy  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  roy  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  roy  *    documentation and/or other materials provided with the distribution.
     17  1.1  roy  *
     18  1.1  roy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  roy  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  roy  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  roy  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  roy  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  roy  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  roy  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  roy  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  roy  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1  roy  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  roy  */
     29  1.1  roy 
     30  1.1  roy #include <sys/cdefs.h>
     31  1.2  roy __RCSID("$NetBSD: setupterm.c,v 1.2 2010/02/11 00:27:09 roy Exp $");
     32  1.1  roy 
     33  1.1  roy #include <assert.h>
     34  1.1  roy #include <err.h>
     35  1.1  roy #include <stdio.h>
     36  1.1  roy #include <stdlib.h>
     37  1.1  roy #include <strings.h>
     38  1.1  roy #include <unistd.h>
     39  1.1  roy #include <term_private.h>
     40  1.1  roy #include <term.h>
     41  1.1  roy 
     42  1.1  roy #define reterr(code, msg)						      \
     43  1.1  roy 	do {								      \
     44  1.1  roy 		if (errret == NULL)					      \
     45  1.1  roy 			errx(EXIT_FAILURE, msg);			      \
     46  1.1  roy 		else {							      \
     47  1.1  roy 			*errret = code;					      \
     48  1.1  roy 			return ERR;					      \
     49  1.1  roy 		}							      \
     50  1.1  roy 	} while (0 /* CONSTCOND */)
     51  1.1  roy 
     52  1.1  roy #define reterrarg(code, msg, arg) \
     53  1.1  roy 	do {								      \
     54  1.1  roy 		if (errret == NULL)					      \
     55  1.1  roy 			errx(EXIT_FAILURE, msg, arg);			      \
     56  1.1  roy 		else {							      \
     57  1.1  roy 			*errret = code;					      \
     58  1.1  roy 			return ERR;					      \
     59  1.1  roy 		}							      \
     60  1.1  roy 	} while (0 /* CONSTCOND */)
     61  1.1  roy 
     62  1.1  roy 
     63  1.1  roy int
     64  1.1  roy ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret)
     65  1.1  roy {
     66  1.1  roy 	int error;
     67  1.1  roy 
     68  1.1  roy 	_DIAGASSERT(nterm != NULL);
     69  1.1  roy 
     70  1.1  roy        	if (term == NULL)
     71  1.1  roy 		term = getenv("TERM");
     72  1.2  roy 	if (term == NULL || *term == '\0') {
     73  1.2  roy 		*nterm = NULL;
     74  1.2  roy 		reterr(0, "TERM environment variable not set");
     75  1.2  roy 	}
     76  1.1  roy 	if (fildes == STDOUT_FILENO && !isatty(fildes))
     77  1.1  roy 		fildes = STDERR_FILENO;
     78  1.1  roy 
     79  1.1  roy 	*nterm = calloc(1, sizeof(**nterm));
     80  1.1  roy 	if (*nterm == NULL)
     81  1.1  roy 		reterr(-1, "not enough memory to create terminal structure");
     82  1.1  roy 
     83  1.1  roy 	error = _ti_getterm(*nterm, term, 0);
     84  1.1  roy 	if (error != 1) {
     85  1.1  roy 		free(*nterm);
     86  1.1  roy 		*nterm = NULL;
     87  1.1  roy 		switch (error) {
     88  1.1  roy 		case -1:
     89  1.1  roy 			reterr(error, "cannot access the terminfo database");
     90  1.1  roy 			/* NOTREACHED */
     91  1.1  roy 		case 0:
     92  1.1  roy 			reterrarg(error,
     93  1.1  roy 			    "%s: terminal not listed in terminfo datase",
     94  1.1  roy 			    term);
     95  1.1  roy 			/* NOTREACHED */
     96  1.1  roy 		default:
     97  1.1  roy 			reterr(-1, "unknown error");
     98  1.1  roy 			/* NOTREACHED */
     99  1.1  roy 		}
    100  1.1  roy 	}
    101  1.1  roy 
    102  1.1  roy 	(*nterm)->fildes = fildes;
    103  1.1  roy 	_ti_setospeed(*nterm);
    104  1.1  roy 	if (t_generic_type(*nterm))
    105  1.1  roy 		reterrarg(0, "%s: generic terminal", term);
    106  1.1  roy 	if (t_hard_copy(*nterm))
    107  1.1  roy 		reterrarg(1, "%s: hardcopy terminal", term);
    108  1.1  roy 	/* POSIX requires 1 for success */
    109  1.1  roy 	if (errret)
    110  1.1  roy 		*errret = 1;
    111  1.1  roy 	return OK;
    112  1.1  roy }
    113  1.1  roy 
    114  1.1  roy int
    115  1.1  roy setupterm(const char *term, int fildes, int *errret)
    116  1.1  roy {
    117  1.1  roy 	TERMINAL *nterm;
    118  1.1  roy 	int ret;
    119  1.1  roy 
    120  1.1  roy 	if (errret != NULL)
    121  1.1  roy 		*errret = ERR;
    122  1.1  roy 	ret = ti_setupterm(&nterm, term, fildes, errret);
    123  1.1  roy 	if (nterm != NULL)
    124  1.1  roy 		set_curterm(nterm);
    125  1.1  roy 	return ret;
    126  1.1  roy }
    127