Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / obp / os / unix / termcap.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: termcap.c
5*
6* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
7*
8* - Do no alter or remove copyright notices
9*
10* - Redistribution and use of this software in source and binary forms, with
11* or without modification, are permitted provided that the following
12* conditions are met:
13*
14* - Redistribution of source code must retain the above copyright notice,
15* this list of conditions and the following disclaimer.
16*
17* - Redistribution in binary form must reproduce the above copyright notice,
18* this list of conditions and the following disclaimer in the
19* documentation and/or other materials provided with the distribution.
20*
21* Neither the name of Sun Microsystems, Inc. or the names of contributors
22* may be used to endorse or promote products derived from this software
23* without specific prior written permission.
24*
25* This software is provided "AS IS," without a warranty of any kind.
26* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
27* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
28* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
29* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
30* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
31* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
32* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
33* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
34* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
35* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
36* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*
38* You acknowledge that this software is not designed, licensed or
39* intended for use in the design, construction, operation or maintenance of
40* any nuclear facility.
41*
42* ========== Copyright Header End ============================================
43*/
44/*
45 * termcap.c 2.5 01/05/18
46 * Copyright 1985-1990 Bradley Forthware
47 * Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved
48 */
49
50/*
51 * Interface to the Unix "termcap" facility for use by Forthmacs running
52 * under the C wrapper program.
53 * Operations:
54 *
55 * int t_init() Called once to initialize things. Returns 0 if
56 * okay, nonzero if an error occurred.
57 *
58 * t_op(op) "Generic" no-arguments terminal operation. Op is
59 * a number between 0 and 12, corresponding to an
60 * operation in the "opnames" table.
61 *
62 * t_move(col, row) Move the cursor to the indicated postion. (0 origin)
63 *
64 * int t_rows() The number of rows (lines) on the terminal screen
65 *
66 * int t_cols() The number of columns on the terminal screen
67 */
68
69#include <stdio.h>
70#include <stdlib.h>
71
72#ifdef SYS5
73#include <curses.h>
74#include <term.h>
75#endif SYS5
76
77char *opnames[] = {
78 "", /* 0 left */
79 "nd", /* 1 right */
80 "up", /* 2 up */
81 "do", /* 3 down */
82 "ic", /* 4 insert char */
83 "dc", /* 5 delete char */
84 "ce", /* 6 clear rest of line */
85 "cd", /* 7 clear rest of screen */
86 "al", /* 8 insert line */
87 "dl", /* 9 delete line */
88 "cl", /* 10 clear screen */
89 "so", /* 11 start stand-out mode */
90 "se", /* 12 end stand-out mode */
91 "cm", /* 13 move cursor */
92};
93char *opstrings[14];
94
95#define TCAPSLEN 315
96
97extern char *tgoto();
98char tcapbuf[TCAPSLEN];
99
100char PC;
101
102#define PUTC ((int (*)(char))putchar)
103
104static void
105putstring(char *str)
106{
107 char c;
108
109 while (c = *str++)
110 PUTC(c);
111 fflush(stdout);
112}
113
114static int t_inited;
115static int nrows = 24;
116static int ncols = 80;
117
118void
119t_op(int op)
120{
121 tputs(opstrings[op], 1, PUTC);
122}
123
124void
125t_move(int col, int row)
126{
127 tputs(tgoto(opstrings[13], col, row), 1, PUTC);
128}
129
130int
131t_rows(void)
132{
133 return (nrows);
134}
135
136int
137t_cols(void)
138{
139 return (ncols);
140}
141
142int
143t_init(void)
144{
145 char *getenv();
146 char *t, *p, *tgetstr();
147 char tcbuf[1024];
148 char *tv_stype;
149 int i, num;
150
151 if (t_inited)
152 return (0);
153
154 if ((tv_stype = getenv("TERM")) == 0) {
155 putstring("Environment variable TERM not defined!\n");
156 return (-1);
157 }
158
159 if ((tgetent(tcbuf, tv_stype)) != 1) {
160 putstring("Unknown terminal type ");
161 putstring(tv_stype);
162 putstring("\n");
163 if ((tgetent(tcbuf, "dumb")) != 1) /* Default to "dumb" */
164 return (-2);
165 }
166
167 p = tcapbuf;
168 t = tgetstr("pc", &p);
169 if (t)
170 PC = *t;
171
172 /* Entry 0 (left) is a special case */
173 i = 0;
174 if (tgetflag("bs")) {
175 opstrings[0] = "\b";
176 i = 1;
177 }
178
179 for (; i <= 13; i++) {
180 t = tgetstr(opnames[i], &p);
181 opstrings[i] = t ? t : "";
182 }
183
184 /* Unfortunately, the tty driver may turn the lf into crlf. Sigh. */
185 if (*opstrings[3] == '\0')
186 opstrings[3] = "\12";
187
188 if ((num = tgetnum("li")) != -1)
189 nrows = num - 1;
190 if ((num = tgetnum("co")) != -1)
191 ncols = num;
192
193 if (p >= &tcapbuf[TCAPSLEN]) {
194 putstring("Terminal description too big!\n");
195 return (-3);
196 }
197
198 t_inited = 1;
199 return (0);
200}