xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/unctrl.c (revision edd580643f2cf1434e252cd7779e83182ea84945)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * unctrl.c
31  *
32  * XCurses Library
33  *
34  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35  *
36  */
37 
38 #if M_RCSID
39 #ifndef lint
40 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/unctrl.c 1.2 1995/10/02 19:34:15 ant Exp $";
41 #endif
42 #endif
43 
44 #include <private.h>
45 #include <limits.h>
46 #include <ctype.h>
47 
48 static const char *carat[] = {
49 	"^?",
50 	"^@",
51 	"^A",
52 	"^B",
53 	"^C",
54 	"^D",
55 	"^E",
56 	"^F",
57 	"^G",
58 	"^H",
59 	"^I",
60 	"^J",
61 	"^K",
62 	"^L",
63 	"^M",
64 	"^N",
65 	"^O",
66 	"^P",
67 	"^Q",
68 	"^R",
69 	"^S",
70 	"^T",
71 	"^U",
72 	"^V",
73 	"^W",
74 	"^X",
75 	"^Y",
76 	"^Z",
77 	"^[",
78 	"^\\",
79 	"^]",
80 	"^^",
81 	"^_"
82 };
83 
84 const char *
85 unctrl(chtype ch)
86 {
87 	char *str;
88 	int c, msb;
89 	static char chr[5];
90 
91 #ifdef M_CURSES_TRACE
92 	__m_trace("unctrl(%ld)", ch);
93 #endif
94 
95         /* Map wide character to a wide string. */
96 	c = ch & A_CHARTEXT;
97 	msb = 1 << (CHAR_BIT-1);
98 
99 	if (iscntrl(c)) {
100 		/* ASCII DEL */
101 		if (c == 127)
102 			return __m_return_pointer("unctrl", carat[0]);
103 
104 		/* ASCII control codes. */
105 		if (0 <= c && c < 32)
106 			return __m_return_pointer("unctrl", carat[c+1]);
107 
108 		/* Something we don't know what to do with. */
109 		return __m_return_pointer("unctrl", (char *) 0);
110 	} else if (c & msb) {
111 		/* Meta key notation if high bit is set on character. */
112 		c &= ~msb;
113 
114 		chr[0] = 'M';
115 		chr[1] = '-';
116 
117 		if (iscntrl(c)) {
118 			str = (char *) unctrl(c);
119 			chr[2] = *str++;
120 			chr[3] = *str;
121 			chr[4] = '\0';
122 		} else {
123 			chr[2] = c;
124 			chr[3] = '\0';
125 		}
126 	} else {
127 		/* Return byte as is. */
128 		chr[0] = c;
129 		chr[1] = '\0';
130 	}
131 
132 	return __m_return_pointer("unctrl", chr);
133 }
134