File: termcap.c

package info (click to toggle)
ytree 1.94-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 836 kB
  • ctags: 1,510
  • sloc: ansic: 15,271; makefile: 208
file content (222 lines) | stat: -rw-r--r-- 4,317 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************************
 *
 * $Header: /usr/local/cvsroot/utils/ytree/termcap.c,v 1.9 1997/08/13 12:24:58 werner Rel $
 *
 * Termcap Term-I/O
 *
 ***************************************************************************/


#include "ytree.h"



#ifdef TERMCAP



#ifdef __NeXT__

char	tcdummy[256], tcbuf[1024];
char	*tcdptr = tcdummy;

#else
# define tcbuf		NULL
# define tcdummy	NULL
#endif /* __NeXT__ */


#undef wgetch
#undef initscr
#undef endwin


typedef struct
{
  int   key_code;
  char  *termcap_name;
  char  *key_sequence;
} SpecialKey;


static SpecialKey special_key[] = {
                                    { KEY_BTAB,      "kB", NULL },
                                    { KEY_DOWN,      "kd", NULL },
                                    { KEY_UP,        "ku", NULL },
                                    { KEY_LEFT,      "kl", NULL },
                                    { KEY_RIGHT,     "kr", NULL },
                                    { KEY_END,       "kH", NULL },
                                    { KEY_HOME,      "kh", NULL },
                                    { KEY_NPAGE,     "kN", NULL },
                                    { KEY_PPAGE,     "kP", NULL },
                                    { KEY_DC,        "kD", NULL },
                                    { KEY_BACKSPACE, "kb", NULL },
                                    { KEY_EIC,       "kM", NULL },
                                    { KEY_IC,        "kI", NULL },
                                    { KEY_DL,        "kL", NULL },
                                    { -1, NULL, NULL }
		                  };



static int TermcapMultiByte( WINDOW *win, int ch );
static void SigAlarm( int signo );
static jmp_buf env_alarm;
static char *reverse;
static char *normal;



void TermcapVidattr( int attr )
{
  if( normal && reverse )
  {
    if( attr == 0 ) putp( normal );
    else
    {
      if( attr & A_REVERSE ) putp( reverse );
    }
  }
}



int TermcapWgetch( WINDOW *win )
{
  int        ch;
  SpecialKey *sk_ptr;
  BOOL       one_matched = FALSE;
  int        i;

  ch = wgetch( stdscr );

  for( i=0, sk_ptr = special_key; 
       sk_ptr->key_code > 0; 
       sk_ptr = &special_key[++i] 
     )
  {
    /* Ein-Byte Codes ausblenden */
    /*---------------------------*/

    if( sk_ptr->key_sequence && *(sk_ptr->key_sequence) == ch )
    {
      /* Erstes Zeichen matched */
      /*------------------------*/

      if( sk_ptr->key_sequence[1] == '\0' )
      { 
        /* 1 Byte-Code */
        /*-------------*/

        ch = sk_ptr->key_code;
        break;
      }
      one_matched = TRUE;
    }
  }

  if( one_matched )
  {
    /* Multi-Byte Code */
    /*-----------------*/

    ch = TermcapMultiByte( win, ch );
  }
  return( ch );
}



static int TermcapMultiByte( WINDOW *win, int ch )
{
  SpecialKey *sk_ptr;
  char buffer[80];
  BOOL found;
  int i, j;

  memset( buffer, 0, sizeof( buffer ) );
  *buffer = ch;

  if( signal( SIGALRM, SigAlarm ) == SIG_ERR )
  {
    ERROR_MSG( "signal SIGALRM failed*ABORT" );
    exit( 1 );
  }

  if( setjmp( env_alarm ) )
  {
    return( ch );
  }

  alarm( 1 );

  for( j=1, found = FALSE; !found && j < sizeof( buffer ); j++ )
  {
    buffer[j] = wgetch( win );

    for( i=0, sk_ptr = special_key; 
	 sk_ptr->key_code > 0; 
	 sk_ptr = &special_key[++i] 
       )
    {
      if( sk_ptr->key_sequence && !strcmp( sk_ptr->key_sequence, buffer ) )
      {
        ch = sk_ptr->key_code;
        found = TRUE;
        break;
      }
    }
  }
  
  alarm( 0 );

  return( ch );
}



static void SigAlarm( int signo )
{
  longjmp( env_alarm, 1 );
}


void TermcapInitscr()
{
  int i;

  initscr();

  tgetent( tcbuf, getenv( "TERM" ) );

  for( i=0; special_key[i].key_code > 0; i++ )
  {
#ifdef __NeXT__
    tcdptr = tcdummy;
    special_key[i].key_sequence = Strdup(tgetstr( special_key[i].termcap_name, &tcdptr ));
#else
    special_key[i].key_sequence = tgetstr( special_key[i].termcap_name, NULL );
#endif
  }
#ifdef __NeXT__
  reverse = Strdup(tgetstr( "so", &tcdptr ));
  normal  = Strdup(tgetstr( "se", &tcdptr ));
#else
  reverse = tgetstr( "so", tcdummy );
  normal  = tgetstr( "se", tcdummy );
#endif
}



void TermcapEndwin()
{
  nl();
  endwin();
  putchar( '\n' );
}


#endif /* TERMCAP */