]> pd.if.org Git - pd_readline/blob - mg/cinfo.c
Added mg from an OpenBSD mirror site. Many thanks to the OpenBSD team and the mg...
[pd_readline] / mg / cinfo.c
1 /*      $OpenBSD: cinfo.c,v 1.16 2011/11/28 04:41:39 lum Exp $  */
2
3 /* This file is in the public domain. */
4
5 /*
6  *              Character class tables.
7  * Do it yourself character classification
8  * macros, that understand the multinational character set,
9  * and let me ask some questions the standard macros (in
10  * ctype.h) don't let you ask.
11  */
12 #include        "def.h"
13
14 /*
15  * This table, indexed by a character drawn
16  * from the 256 member character set, is used by my
17  * own character type macros to answer questions about the
18  * type of a character. It handles the full multinational
19  * character set, and lets me ask some questions that the
20  * standard "ctype" macros cannot ask.
21  */
22 /*
23  * Due to incompatible behaviour between "standard" emacs and
24  * ctags word traversing, '_' character's value is changed on 
25  * the fly in ctags mode, hence non-const.
26  */
27 char cinfo[256] = {
28         _MG_C, _MG_C, _MG_C, _MG_C,                                   /* 0x0X */
29         _MG_C, _MG_C, _MG_C, _MG_C,
30         _MG_C, _MG_C, _MG_C, _MG_C,
31         _MG_C, _MG_C, _MG_C, _MG_C,
32         _MG_C, _MG_C, _MG_C, _MG_C,                                   /* 0x1X */
33         _MG_C, _MG_C, _MG_C, _MG_C,
34         _MG_C, _MG_C, _MG_C, _MG_C,
35         _MG_C, _MG_C, _MG_C, _MG_C,
36         0, _MG_P, 0, 0,                                               /* 0x2X */
37         _MG_W, _MG_W, 0, _MG_W,
38         0, 0, 0, 0,
39         0, 0, _MG_P, 0,
40         _MG_D | _MG_W, _MG_D | _MG_W, _MG_D | _MG_W, _MG_D | _MG_W,   /* 0x3X */
41         _MG_D | _MG_W, _MG_D | _MG_W, _MG_D | _MG_W, _MG_D | _MG_W,
42         _MG_D | _MG_W, _MG_D | _MG_W, 0, 0,
43         0, 0, 0, _MG_P,
44         0, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,               /* 0x4X */
45         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
46         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
47         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
48         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,   /* 0x5X */
49         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
50         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, 0,
51         0, 0, 0, 0,
52         0, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,               /* 0x6X */
53         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
54         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
55         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
56         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,   /* 0x7X */
57         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
58         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, 0,
59         0, 0, 0, _MG_C,
60         0, 0, 0, 0,                                                   /* 0x8X */
61         0, 0, 0, 0,
62         0, 0, 0, 0,
63         0, 0, 0, 0,
64         0, 0, 0, 0,                                                   /* 0x9X */
65         0, 0, 0, 0,
66         0, 0, 0, 0,
67         0, 0, 0, 0,
68         0, 0, 0, 0,                                                   /* 0xAX */
69         0, 0, 0, 0,
70         0, 0, 0, 0,
71         0, 0, 0, 0,
72         0, 0, 0, 0,                                                   /* 0xBX */
73         0, 0, 0, 0,
74         0, 0, 0, 0,
75         0, 0, 0, 0,
76         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,   /* 0xCX */
77         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
78         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
79         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
80         0, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,               /* 0xDX */
81         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
82         _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W, _MG_U | _MG_W,
83         _MG_U | _MG_W, _MG_U | _MG_W, 0, _MG_W,
84         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,   /* 0xEX */
85         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
86         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
87         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
88         0, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,               /* 0xFX */
89         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
90         _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W, _MG_L | _MG_W,
91         _MG_L | _MG_W, _MG_L | _MG_W, 0, 0
92 };
93
94 /*
95  * Find the name of a keystroke.  Needs to be changed to handle 8-bit printing
96  * characters and function keys better.  Returns a pointer to the terminating
97  * '\0'.  Returns NULL on failure.
98  */
99 char *
100 getkeyname(char *cp, size_t len, int k)
101 {
102         const char      *np;
103         size_t           copied;
104
105         if (k < 0)
106                 k = CHARMASK(k);        /* sign extended char */
107         switch (k) {
108         case CCHR('@'):
109                 np = "C-SPC";
110                 break;
111         case CCHR('I'):
112                 np = "TAB";
113                 break;
114         case CCHR('M'):
115                 np = "RET";
116                 break;
117         case CCHR('['):
118                 np = "ESC";
119                 break;
120         case ' ':
121                 np = "SPC";
122                 break;          /* yuck again */
123         case CCHR('?'):
124                 np = "DEL";
125                 break;
126         default:
127 #ifdef  FKEYS
128                 if (k >= KFIRST && k <= KLAST &&
129                     (np = keystrings[k - KFIRST]) != NULL)
130                         break;
131 #endif
132                 if (k > CCHR('?')) {
133                         *cp++ = '0';
134                         *cp++ = ((k >> 6) & 7) + '0';
135                         *cp++ = ((k >> 3) & 7) + '0';
136                         *cp++ = (k & 7) + '0';
137                         *cp = '\0';
138                         return (cp);
139                 } else if (k < ' ') {
140                         *cp++ = 'C';
141                         *cp++ = '-';
142                         k = CCHR(k);
143                         if (ISUPPER(k))
144                                 k = TOLOWER(k);
145                 }
146                 *cp++ = k;
147                 *cp = '\0';
148                 return (cp);
149         }
150         copied = strlcpy(cp, np, len);
151         if (copied >= len)
152                 copied = len - 1;
153         return (cp + copied);
154 }