curterm.c revision 1.12 1 1.12 christos /* $NetBSD: curterm.c,v 1.12 2016/04/01 19:59:08 christos Exp $ */
2 1.1 roy
3 1.1 roy /*
4 1.5 roy * Copyright (c) 2009, 2011 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.12 christos __RCSID("$NetBSD: curterm.c,v 1.12 2016/04/01 19:59:08 christos Exp $");
32 1.1 roy
33 1.1 roy #include <assert.h>
34 1.1 roy #include <stdlib.h>
35 1.8 roy #include <string.h>
36 1.1 roy #include <term_private.h>
37 1.1 roy #include <term.h>
38 1.1 roy #include <termios.h>
39 1.2 roy #include <stdio.h>
40 1.8 roy
41 1.1 roy TERMINAL *cur_term;
42 1.1 roy
43 1.8 roy /*
44 1.8 roy * There is no standard way of getting a list of aliases for the
45 1.8 roy * terminal. However, some applications such as telnet want to know this.
46 1.8 roy * ncurses dumps the terminfo header into an undefined variable ttytype
47 1.8 roy * and these applications then parse it to work out the aliases.
48 1.8 roy * We should do the same for now, until a standard mechanism for getting
49 1.8 roy * the information is available or the need for it goes away.
50 1.8 roy */
51 1.8 roy #define NAMESIZE 256
52 1.8 roy char ttytype[NAMESIZE];
53 1.8 roy
54 1.1 roy static const speed_t bauds[] = {
55 1.1 roy 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 2400, 4800, 9600,
56 1.1 roy 19200, 38400, 57600, 115200, 230400, 460800, 921600
57 1.1 roy };
58 1.1 roy
59 1.1 roy void
60 1.1 roy _ti_setospeed(TERMINAL *term)
61 1.1 roy {
62 1.1 roy struct termios termios;
63 1.1 roy speed_t os;
64 1.1 roy size_t i;
65 1.1 roy
66 1.1 roy _DIAGASSERT(term != NULL);
67 1.9 roy
68 1.1 roy term->_ospeed = 0;
69 1.1 roy if (tcgetattr(term->fildes, &termios) == 0) {
70 1.1 roy os = cfgetospeed(&termios);
71 1.1 roy for (i = 0; i < __arraycount(bauds); i++)
72 1.1 roy if (bauds[i] == os) {
73 1.1 roy term->_ospeed = i;
74 1.1 roy break;
75 1.1 roy }
76 1.1 roy }
77 1.1 roy }
78 1.1 roy
79 1.1 roy TERMINAL *
80 1.1 roy set_curterm(TERMINAL *nterm)
81 1.1 roy {
82 1.1 roy TERMINAL *oterm;
83 1.8 roy size_t l, n;
84 1.8 roy char *p;
85 1.1 roy
86 1.1 roy oterm = cur_term;
87 1.1 roy cur_term = nterm;
88 1.1 roy
89 1.1 roy ospeed = 0;
90 1.1 roy if (cur_term == NULL)
91 1.1 roy PC = '\0';
92 1.1 roy else {
93 1.1 roy if (pad_char == NULL)
94 1.1 roy PC = '\0';
95 1.1 roy else
96 1.1 roy PC = *pad_char;
97 1.1 roy _ti_setospeed(nterm);
98 1.1 roy ospeed = nterm->_ospeed;
99 1.8 roy
100 1.8 roy p = ttytype;
101 1.8 roy l = sizeof(ttytype);
102 1.8 roy if ((n = strlcpy(p, nterm->name, l)) == strlen(p)) {
103 1.8 roy p += n;
104 1.8 roy l -= n;
105 1.8 roy *p++ = '|';
106 1.8 roy l--;
107 1.8 roy if (nterm->_alias &&
108 1.8 roy (n = strlcpy(p, nterm->_alias, l)) == strlen(p))
109 1.8 roy {
110 1.8 roy p += n;
111 1.8 roy l -= n;
112 1.8 roy *p++ = '|';
113 1.8 roy l--;
114 1.8 roy }
115 1.8 roy if (nterm->desc &&
116 1.8 roy (n = strlcpy(p, nterm->desc, l)) == strlen(p))
117 1.8 roy {
118 1.8 roy p += n;
119 1.8 roy l -= n;
120 1.8 roy *p++ = '|';
121 1.8 roy l--;
122 1.8 roy }
123 1.8 roy p--;
124 1.8 roy }
125 1.8 roy *p = '\0';
126 1.1 roy }
127 1.1 roy
128 1.1 roy return oterm;
129 1.1 roy }
130 1.1 roy
131 1.1 roy int
132 1.1 roy del_curterm(TERMINAL *oterm)
133 1.1 roy {
134 1.4 roy
135 1.6 roy if (oterm == NULL)
136 1.6 roy return ERR;
137 1.6 roy free(oterm->_area);
138 1.6 roy free(oterm->strs);
139 1.6 roy free(oterm->nums);
140 1.6 roy free(oterm->flags);
141 1.6 roy free(oterm->_userdefs);
142 1.11 christos free(oterm->_buf);
143 1.6 roy free(oterm);
144 1.12 christos if (oterm == cur_term)
145 1.12 christos cur_term = NULL;
146 1.6 roy return OK;
147 1.1 roy }
148 1.5 roy
149 1.5 roy char *
150 1.5 roy termname(void)
151 1.5 roy {
152 1.5 roy
153 1.5 roy _DIAGASSERT(cur_term != NULL);
154 1.5 roy return __UNCONST(cur_term->name);
155 1.5 roy }
156 1.7 roy
157 1.10 joerg static const char * nullname = "";
158 1.7 roy
159 1.7 roy char *
160 1.7 roy longname(void)
161 1.7 roy {
162 1.7 roy
163 1.7 roy _DIAGASSERT(cur_term != NULL);
164 1.7 roy if (cur_term->desc == NULL)
165 1.7 roy return __UNCONST(nullname);
166 1.7 roy return __UNCONST(cur_term->desc);
167 1.7 roy }
168