Example #1
0
int
restartterm(char *tm, int fd, int *err_return)
{
	size_t	len;
	int	err_code;
	const char	*err_msg, *terminfo;
	char	*old_names, *old_strings, *old_term, *filename;
	static const char	def_termname[] = M_TERM_NAME;
	static const char	def_terminfo[] = M_TERMINFO_DIR;

	err_code = 1;
	filename = NULL;
	old_term = cur_term->_term;
	old_names = cur_term->_names;
	old_strings = cur_term->_str_table;

	terminfo = getenv("TERMINFO");
	if (terminfo == NULL || terminfo[0] == '\0') {
		terminfo = def_terminfo;
	} else {
		terminfo = (const char *) strdup((char *) terminfo);
		if (terminfo == NULL) {
			/* Not really true... */
			err_msg = e_terminal;
			err_code = 2;
			goto error;
		}
	}

	if ((tm == NULL) &&
		(((tm = getenv("TERM")) == NULL) || (*tm == '\0'))) {
		tm = (char *)def_termname;
	}

	/* Remember the terminal name being loaded. */
	cur_term->_term = strdup(tm);
	if (cur_term->_term == NULL) {
		err_msg = e_terminal;
		err_code = 2;
		goto error;
	}

	/* Length of path we're going to construct. */
	len = strlen(terminfo) + 3 + strlen(tm);

	if ((len > PATH_MAX) ||
		((filename = (char *)malloc(PATH_MAX + 1)) == NULL)) {
		err_msg = e_pathmax;
		err_code = 2;
		goto error;
	}

	/* Construct terminfo filename. */
	(void) sprintf(filename, "%s/%c/%s", terminfo, tm[0], tm);

	/* Go looking for compiled terminal definition. */
	if (__m_read_terminfo(filename, cur_term) < 0) {
		/* Length of default terminfo path. */
		len = strlen(def_terminfo) + 3 + strlen(tm);

		if (len > PATH_MAX) {
			err_msg = e_pathmax;
			err_code = 2;
			goto error;
		}

		(void) sprintf(filename, "%s/%c/%s", def_terminfo, tm[0], tm);

		if (__m_read_terminfo(filename, cur_term) < 0) {
			err_msg = e_unknown;
			err_code = 0;
			goto error;
		}
	}

	if (use_environment) {
		char	*env;
#ifdef TIOCGWINSZ
		/*
		 * Use ioctl(TIOCGWINSZ) to get row and column values.  These
		 * values may override the default terminfo settings.
		 */
		{
			struct winsize	wininfo;
			if (ioctl(fd, TIOCGWINSZ, &wininfo) != -1) {
				if (0 < wininfo.ws_col)
					columns = wininfo.ws_col;
				if (0 < wininfo.ws_row)
					lines = wininfo.ws_row;
			}
		}
#endif /* TIOCGWINSZ */

		/* Check to see is the user wants a particular size terminal. */
		if ((env = getenv("LINES")) != NULL) {
			int	nlines = (int) strtol(env, (char **) 0, 10);
			if (0 < nlines)
				lines = nlines;
		}
		if ((env = getenv("COLUMNS")) != NULL) {
			int ncolumns = (int) strtol(env, (char **) 0, 10);
			if (0 < ncolumns)
				columns = ncolumns;
		}
	}

	if (command_character != NULL && getenv("CC") != NULL)
		do_prototype();

	/*
	 * If no_color_video is disabled, then assign it a value that
	 * permits all attributes in combination with colour.
	 */
	if (no_color_video == -1)
		no_color_video = 0;

	__m_mvcur_cost();
error:
	if (filename != NULL)
		free(filename);

	if (terminfo != def_terminfo)
		free((void *) terminfo);

	if (err_return != NULL) {
		*err_return = err_code;

		if (err_code == 1) {
			err_code = OK;
		} else {
			err_code = ERR;
			cur_term->_term = old_term;
			cur_term->_names = old_names;
			cur_term->_str_table = old_strings;
		}
	} else if (err_code != 1) {
		(void) fprintf(stderr, err_msg, tm);
		exit(1);
	}

	if (err_code == OK) {
		if (old_names != NULL)
			free(old_names);
		if (old_strings != NULL)
			free(old_strings);
		if (old_term != NULL)
			free(old_term);
	}

	return (err_code);
}
Example #2
0
int setupterm(const char *tname, int Filedes, int *errret)
{
struct term	*term_ptr;
int status;

	T((T_CALLED("setupterm(\"%s\",%d,%p)"), tname, Filedes, errret));

	if (tname == 0) {
		tname = getenv("TERM");
		if (tname == 0 || *tname == '\0') {
			ret_error0(-1, "TERM environment variable not set.\n");
                }
	}
	if (strlen(tname) > MAX_NAME_SIZE) {
		ret_error(-1, "TERM environment must be <= %d characters.\n",
		    MAX_NAME_SIZE);
	}

	T(("your terminal name is %s", tname));

	term_ptr = typeCalloc(TERMINAL, 1);

	if (term_ptr == 0) {
		ret_error0(-1, "Not enough memory to create terminal structure.\n") ;
        }
#if USE_DATABASE
	status = grab_entry(tname, &term_ptr->type);
#else
	status = 0;
#endif

	/* try fallback list if entry on disk */
	if (status != 1)
	{
	    const TERMTYPE	*fallback = _nc_fallback(tname);

	    if (fallback)
	    {
		memcpy(&term_ptr->type, fallback, sizeof(TERMTYPE));
		status = 1;
	    }
	}

	if (status == -1)
	{
		ret_error0(-1, "terminals database is inaccessible\n");
	}
	else if (status == 0)
	{
		ret_error(0, "'%s': unknown terminal type.\n", tname);
	}

	set_curterm(term_ptr);

	if (command_character  &&  getenv("CC"))
		do_prototype();

	strlcpy(ttytype, cur_term->type.term_names, NAMESIZE);

	/*
	 * Allow output redirection.  This is what SVr3 does.
	 * If stdout is directed to a file, screen updates go
	 * to standard error.
	 */
	if (Filedes == STDOUT_FILENO && !isatty(Filedes))
	    Filedes = STDERR_FILENO;
	cur_term->Filedes = Filedes;

	_nc_get_screensize(&LINES, &COLS);

	if (errret)
		*errret = 1;

	T((T_CREATE("screen %s %dx%d"), tname, LINES, COLS));

	if (generic_type) {
		ret_error(0, "'%s': I need something more specific.\n", tname);
	}
	if (hard_copy) {
		ret_error(1, "'%s': I can't handle hardcopy terminals.\n", tname);
	}
	returnCode(OK);
}
Example #3
0
int setupterm(char *termname, int filedes, int *errret)
{
    static int	_been_here = FALSE;
    char		filename1[1024];
    char		filename2[1024];
    char		*directory = SRCDIR;
    char		*terminfo;
    struct term	*term_ptr;
    char 		*rows, *cols;

    if (_been_here == FALSE) {
        _been_here = TRUE;
#ifdef TRACE
        _init_trace();
        if (_tracing)
            _tracef("setupterm(%s,%d,%x) called", termname, filedes, errret);
#endif

        if (termname == NULL)
        {
            termname = getenv("TERM");
            if (termname == NULL)
                ret_error0(-1, "TERM environment variable not set.\n");
        }

        term_ptr = (struct term *) malloc(sizeof(struct term));

        if (term_ptr == NULL)
            ret_error0(-1, "Not enough memory to create terminal structure.\n") ;

        if ((terminfo = getenv("TERMINFO")) != NULL)
            directory = terminfo;

        sprintf(filename1, "%s/%c/%s", directory, termname[0], termname);
        sprintf(filename2, "%s/%c/%s", SRCDIR, termname[0], termname);

        if (read_entry(filename1, term_ptr) < 0 &&  read_entry(filename2, term_ptr) < 0)
            ret_error(0, "'%s': Unknown terminal type.\n", termname);

        if (command_character  &&  getenv("CC"))
            do_prototype();

        cur_term = term_ptr;
        strncpy(ttytype, cur_term->term_names, NAMESIZE - 1);
        ttytype[NAMESIZE - 1] = '\0';
        cur_term->Filedes = filedes;

        /* figure out the size of the screen */

        rows = getenv("LINES");
        if (rows != (char *)NULL)
            LINES = atoi(rows);

        cols = getenv("COLUMNS");
        if (cols != (char *)NULL)
            COLS = atoi(cols);

        if (_use_env == FALSE)
            if (lines > 0 && columns > 0) {
                LINES = lines;
                COLS  = columns;
            }
        /* use envirronment; if not set then use window size */
#ifdef TRACE
        _tracef("using environment to find screen size");
#endif
        if (LINES <= 0 || COLS <= 0) {
            if (resize(filedes) == 1) {
                fprintf(stderr, "can't find the screen size");
                exit(1);
            }
        }
        lines = LINES;
        columns = COLS;

#ifdef TRACE
        _tracef("screen size is %dx%d and %dx%d", COLS, LINES, columns, lines);
#endif

#ifdef TERMIOS
        tcgetattr(filedes, &cur_term->Ottyb);
        if (cur_term->Ottyb.c_oflag & XTABS)
            tab = back_tab = NULL;

        cur_term->Nttyb = cur_term->Ottyb;
        cur_term->Nttyb.c_oflag &= ~XTABS;
#else
        gtty(filedes, &cur_term->Ottyb);
        if (cur_term->Ottyb.sg_flags & XTABS)
            tab = back_tab = NULL;

        cur_term->Nttyb = cur_term->Ottyb;
        cur_term->Nttyb.sg_flags &= ~XTABS;
#endif

        def_prog_mode();

        if (errret)
            *errret = 1;
    }
    return(1);
}