1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1995, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*
28  * unctrl.c
29  *
30  * XCurses Library
31  *
32  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  */
35 
36 #if M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/unctrl.c 1.2 1995/10/02 19:34:15 ant Exp $";
39 #endif
40 #endif
41 
42 #include <private.h>
43 #include <limits.h>
44 #include <ctype.h>
45 
46 static const char *carat[] = {
47 	"^?",
48 	"^@",
49 	"^A",
50 	"^B",
51 	"^C",
52 	"^D",
53 	"^E",
54 	"^F",
55 	"^G",
56 	"^H",
57 	"^I",
58 	"^J",
59 	"^K",
60 	"^L",
61 	"^M",
62 	"^N",
63 	"^O",
64 	"^P",
65 	"^Q",
66 	"^R",
67 	"^S",
68 	"^T",
69 	"^U",
70 	"^V",
71 	"^W",
72 	"^X",
73 	"^Y",
74 	"^Z",
75 	"^[",
76 	"^\\",
77 	"^]",
78 	"^^",
79 	"^_"
80 };
81 
82 const char *
unctrl(chtype ch)83 unctrl(chtype ch)
84 {
85 	char *str;
86 	int c, msb;
87 	static char chr[5];
88 
89 #ifdef M_CURSES_TRACE
90 	__m_trace("unctrl(%ld)", ch);
91 #endif
92 
93         /* Map wide character to a wide string. */
94 	c = ch & A_CHARTEXT;
95 	msb = 1 << (CHAR_BIT-1);
96 
97 	if (iscntrl(c)) {
98 		/* ASCII DEL */
99 		if (c == 127)
100 			return __m_return_pointer("unctrl", carat[0]);
101 
102 		/* ASCII control codes. */
103 		if (0 <= c && c < 32)
104 			return __m_return_pointer("unctrl", carat[c+1]);
105 
106 		/* Something we don't know what to do with. */
107 		return __m_return_pointer("unctrl", (char *) 0);
108 	} else if (c & msb) {
109 		/* Meta key notation if high bit is set on character. */
110 		c &= ~msb;
111 
112 		chr[0] = 'M';
113 		chr[1] = '-';
114 
115 		if (iscntrl(c)) {
116 			str = (char *) unctrl(c);
117 			chr[2] = *str++;
118 			chr[3] = *str;
119 			chr[4] = '\0';
120 		} else {
121 			chr[2] = c;
122 			chr[3] = '\0';
123 		}
124 	} else {
125 		/* Return byte as is. */
126 		chr[0] = c;
127 		chr[1] = '\0';
128 	}
129 
130 	return __m_return_pointer("unctrl", chr);
131 }
132