-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecuwinutil.c
559 lines (493 loc) · 13.1 KB
/
ecuwinutil.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*+-------------------------------------------------------------------------
ecuwinutil.c - curses window utilities
Defined functions:
clear_area(win, y, x, len)
clear_area_char(win, y, x, len, fillchar)
winbox(win)
window_create(title, title_x, tly, tlx, lines, cols)
window_setup(win, title, title_x)
windows_end(botleft_flag)
windows_end_signal()
windows_start()
winget_single(win, nondelim_list, delim_list)
wingets(win, y, x, buf, bufsize, delim, edit, pwgpos)
It's hard to get ivory in Africa, but in Alabama the
Tuscaloosa. -- Groucho
--------------------------------------------------------------------------*/
/*+:EDITS:*/
/*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
/*:05-12-1997-17:38-wht@kepler-xclnt intl port still screwed winget_single */
/*:01-24-1997-02:37-wht@yuriatin-SOURCE RELEASE 4.00 */
/*:09-11-1996-20:[email protected] telnet,curses,structural overhaul */
/*:09-07-1996-16:18-wht@kepler-CFG_UseACS coined for use outside of config */
/*:09-05-1996-17:33-wht@kepler-try retiring use of typeahead(-1) */
/*:08-22-1996-14:13-wht@fep-decommitted AT chars + fix SVR4 */
/*:12-06-1995-13:30-wht@n4hgf-termecu w/errno -1 consideration */
/*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
/*:11-14-1995-10:[email protected] control point: SOCKETS */
/*:03-12-1995-01:29-wht@kepler-Linux winbox was inaccurate */
/*:01-15-1995-01:52-wht@n4hgf-clean up creeping port rot */
/*:01-12-1995-15:19-wht@n4hgf-apply Andrew Chernov 8-bit clean+FreeBSD patch */
/*:05-17-1994-20:49-wht@n4hgf-winget_single ichar now unsigned char */
/*:05-04-1994-04:39-wht@n4hgf-ECU release 3.30 */
/*:11-12-1993-11:00-wht@n4hgf-Linux changes by [email protected] */
/*:09-15-1993-11:31-wht@n4hgf-endwin on non-SCO per [email protected] */
/*:08-17-1993-14:04-wht@n4hgf-add OLD_WINGETS for compatibility */
/*:08-13-1993-04:04-wht@n4hgf-add highlight_delete function to wingets */
/*:09-10-1992-13:59-wht@n4hgf-ECU release 3.20 */
/*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA */
/*:02-09-1992-16:08-root@n4hgf-ruling characters only on SCO (tcap curses) */
/*:08-25-1991-14:39-wht@n4hgf-SVR4 port thanks to aega84!lh */
/*:08-01-1991-03:52-wht@n4hgf-when editing string, set cursor to end */
/*:07-25-1991-12:57-wht@n4hgf-ECU release 3.10 */
/*:08-14-1990-20:[email protected] old edit history */
#include "ecucurses.h"
#include <errno.h>
#include <ctype.h>
#include "ecukey.h"
#include "ecuxkey.h"
#include "termecu.h"
#include "pc_scr.h"
#if !defined(UINT16)
#define UINT16 unsigned short
#endif
#if !defined(uchar)
#define uchar unsigned char
#endif
#if !defined(UINT)
#define UINT unsigned int
#endif
#if !defined(UINT32)
#define UINT32 unsigned long
#endif
extern int tty_is_multiscreen;
WINCH sTL;
WINCH sTR;
WINCH sBL;
WINCH sBR;
WINCH sLT;
WINCH sRT;
WINCH sVR;
WINCH sHR;
int windows_active = 0;
int ttymode_before_window_start;
/*+-------------------------------------------------------------------------
clear_area_char(win,y,x,len,fillchar)
--------------------------------------------------------------------------*/
void
clear_area_char(win, y, x, len, fillchar)
WINDOW *win;
int y;
int x;
int len;
char fillchar;
{
wmove(win, y, x);
#if 0
wprintw(win, "%02x %d,%d,%d", fillchar, y, x, len);
#endif
while (len-- > 0)
waddch(win, fillchar);
wmove(win, y, x);
} /* end of clear_area_char */
/*+-------------------------------------------------------------------------
clear_area(win,y,x,len)
--------------------------------------------------------------------------*/
void
clear_area(win, y, x, len)
WINDOW *win;
int y;
int x;
int len;
{
clear_area_char(win, y, x, len, ' ');
} /* end of clear_area_char */
/*+-------------------------------------------------------------------------
windows_start()
--------------------------------------------------------------------------*/
void
windows_start()
{
extern int tty_not_char_special;
static int initscr_already_performed = 0;
if (tty_not_char_special)
{
fprintf(stderr, "curses features unavailable when stdin not tty\r\n");
errno = -1;
termecu(TERMECU_CURSES_ERROR);
}
ttymode_before_window_start = get_ttymode();
ttymode(0);
if (!initscr_already_performed && !initscr())
{
fprintf(stderr, "curses init failure ... check terminal type\r\n");
errno = -1;
termecu(TERMECU_CURSES_ERROR);
}
initscr_already_performed = 1;
scrollok(stdscr, 0);
savetty();
raw();
noecho();
nonl();
clear();
windows_active = 1;
#if defined(CFG_UseACS)
sTL = ACS_ULCORNER;
sTR = ACS_URCORNER;
sBL = ACS_LLCORNER;
sBR = ACS_LRCORNER;
sLT = ACS_LTEE;
sRT = ACS_RTEE;
sVR = ACS_VLINE;
sHR = ACS_HLINE;
if ((sTL < 127) && strchr("+-|", (uchar) sTL))
sTL = '.';
if ((sTR < 127) && strchr("+-|", (uchar) sTR))
sTR = '.';
if ((sBR < 127) && strchr("+-|", (uchar) sBR))
sBL = '`';
if ((sBR < 127) && strchr("+-|", (uchar) sBR))
sBR = '\'';
#else
sTL = vanilla_TL;
sTR = vanilla_TR;
sBL = vanilla_BL;
sBR = vanilla_BR;
sLT = vanilla_LT;
sRT = vanilla_RT;
sVR = vanilla_VR;
sHR = vanilla_HR;
#endif /* defined(CFG_UseACS) */
wclear(stdscr);
touchwin(stdscr);
wrefresh(stdscr);
} /* end of windows_start */
/*+-------------------------------------------------------------------------
windows_end(botleft_flag)
--------------------------------------------------------------------------*/
void
windows_end(botleft_flag)
int botleft_flag;
{
if (!windows_active)
return;
#if !defined(M_UNIX) && !defined(M_XENIX) && !defined(SCO32v5)
endwin();
#else
refresh();
#endif
if (botleft_flag)
tcap_cursor(LINES - 1, 0);
ttymode(ttymode_before_window_start);
windows_active = 0;
} /* end of windows_end */
/*+-------------------------------------------------------------------------
windows_end_signal() -- called by termecu()
--------------------------------------------------------------------------*/
void
windows_end_signal()
{
windows_end(0);
} /* end of windows_end_signal */
/*+-------------------------------------------------------------------------
winbox(win)
--------------------------------------------------------------------------*/
void
winbox(win)
WINDOW *win;
{
#ifdef __FreeBSD__ /* ache */
box(win, sVR, sHR);
#else
#if defined(linux) || defined(SVR4) /* wht */
int x, y;
box(win, sVR, sHR);
getmaxyx(win, y, x);
wmove(win, 0, 0);
waddch(win, sTL);
wmove(win, y - 1, 0);
waddch(win, sBL);
wmove(win, y - 1, x - 1);
waddch(win, sBR);
wmove(win, 0, x - 1);
waddch(win, sTR);
#else
/*
* default is not very portable, but has survived well ... too bad
* getmaxyx() is not standard
*/
box(win, sVR, sHR);
wmove(win, 0, 0);
waddch(win, sTL);
wmove(win, win->_maxy - 1, 0);
waddch(win, sBL);
wmove(win, win->_maxy - 1, win->_maxx - 1);
waddch(win, sBR);
wmove(win, 0, win->_maxx - 1);
waddch(win, sTR);
#endif
#endif
} /* end of winbox */
/*+-------------------------------------------------------------------------
window_setup(win,title,title_x)
--------------------------------------------------------------------------*/
void
window_setup(win, title, title_x)
WINDOW *win;
char *title;
int title_x;
{
int stand = (title_x < 0);
if (stand)
title_x = -title_x;
touchwin(win);
scrollok(win, 0); /* do not scroll */
winbox(win);
wmove(win, 0, title_x);
if (stand)
wstandout(win);
waddch(win, '[');
wprintw(win, " %s ", title);
waddch(win, ']');
if (stand)
wstandend(win);
} /* end of window_setup */
/*+-------------------------------------------------------------------------
window_create(title,title_x,tly,tlx,lines,cols)
if title_x negative, make title "stand" out
--------------------------------------------------------------------------*/
WINDOW *
window_create(title, title_x, tly, tlx, lines, cols)
char *title;
int title_x;
int tly;
int tlx;
int lines;
int cols;
{
WINDOW *nwin = newwin(lines, cols, tly, tlx);
errno = -1;
if (nwin)
window_setup(nwin, title, title_x);
else
{
fprintf(stderr, "\r\ncurses error: cannot create new window\r\n");
termecu(TERMECU_CURSES_ERROR);
}
return (nwin);
} /* end of window_create */
/*+-------------------------------------------------------------------------
wingets(win,y,x,buf,bufsize,delim,edit,pwgpos)
This procedure reads a string from win and returns the number
of characters read.
If edit is non-zero and pwgpos is not null, the inital string
position is set by dereferencing the pointer.
The terminating delim is returned in 'delim'.
If pwgpos is not null, the ending string position is returned in
the integer pointed to.
-1 is returned if an ESCape is typed by the keyboard user,
otherwise the count of characters in the string.
The entire line must be contained on one line (no line wrap supported).
--------------------------------------------------------------------------*/
int
wingets(win, y, x, buf, bufsize, delim, edit, pwgpos)
WINDOW *win;
int y;
int x;
char *buf;
int bufsize; /* includes room for null..field is 1 less */
UINT *delim;
int edit;
int *pwgpos;
{
int count = 0;
int pos = 0;
int insert_mode = 0;
#ifndef OLD_WINGETS
int highlight_delete = 0;
#endif
int rtn_val = 0;
bufsize--;
if (edit && strlen(buf))
{
#ifndef OLD_WINGETS
highlight_delete = 1;
#endif
clear_area_char(win, y, x, bufsize, ' ');
wstandout(win);
waddstr(win, buf);
wstandend(win);
count = pos = strlen(buf);
if (pwgpos)
{
pos = *pwgpos;
if ((pos < 0) || (pos > count))
pos = count;
}
}
else
{
clear_area_char(win, y, x, bufsize, '_');
*buf = 0;
}
wmove(win, y, x + pos);
while (1)
{
wrefresh(win);
*delim = ttygetc(1);
if (*delim > 0xFF || !isprint(*delim))
{
#ifndef OLD_WINGETS
if (highlight_delete)
{
clear_area_char(win, y, x, bufsize, '_');
waddstr(win, buf);
wmove(win, y, x + pos);
highlight_delete = 0;
}
#endif
switch (*delim)
{
case CRET:
*delim = NL;
case NL:
wrefresh(win);
rtn_val = count;
goto FUNC_RETURN;
case BS:
case DEL:
if (count)
{
if (count == pos)
{
*(buf + --count) = 0;
wmove(win, y, x + count);
waddch(win, '_');
wmove(win, y, x + count);
pos--;
}
else
{
if (!pos)
continue;
mem_cpy(buf + pos - 1, buf + pos, count - pos);
*(buf + --count) = 0;
wmove(win, y, x + --pos);
waddstr(win, buf + pos);
waddch(win, '_');
wmove(win, y, x + pos);
}
}
continue;
case XFcurlf:
if (pos)
wmove(win, y, x + --pos);
continue;
case XFcurrt:
if (pos < count)
wmove(win, y, x + ++pos);
continue;
case XFins:
insert_mode = !insert_mode;
continue;
case ESC:
rtn_val = -1;
goto FUNC_RETURN;
case CTL_U:
clear_area_char(win, y, x, bufsize, '_');
count = 0;
pos = 0;
*buf = 0;
continue;
default:
*(buf + count) = 0;
rtn_val = count;
goto FUNC_RETURN;
} /* end of switch(*delim) */
/* NOTREACHED */
} /* end of if read delimiter */
#ifndef OLD_WINGETS
if (highlight_delete)
{
clear_area_char(win, y, x, bufsize, '_');
count = 0;
pos = 0;
highlight_delete = 0;
}
#endif
if (count == bufsize)
{
ring_bell();
continue;
}
if (insert_mode && (pos != count))
{
waddch(win, *delim);
waddstr(win, buf + pos);
mem_cpy(buf + pos + 1, buf + pos, count - pos);
*(buf + pos++) = *delim;
*(buf + ++count) = 0;
wmove(win, y, x + pos);
}
else
{
waddch(win, *delim);
*(buf + pos) = *delim;
if (pos == count)
*(buf + ++count) = 0;
pos++;
}
} /* end of while can get character */
FUNC_RETURN:
if (pwgpos)
*pwgpos = pos;
return (rtn_val);
} /* end of wingets */
/*+-------------------------------------------------------------------------
winget_single(win,nondelim_list,delim_list)
This procedure assumes cursor is positioned, repeats reading a non-echoing
character from the keyboard until it matches a character in nondelim_list
or delim_list. delim_list is expected to contain printable characters
and no upper-case characters.
If no match occurs, the bell is rung and the keyboard is read again.
If the input character matches a character in delim_list, the index (0-n)
of the character in delim_list is returned. If a match occurs, an
upper-case version of the matching character is placed in the window.
If the input character matches a character in nondelim_list, the character
is returned or'ed with 0x1000
--------------------------------------------------------------------------*/
int
winget_single(win, nondelim_list, delim_list)
WINDOW *win;
UINT *nondelim_list;
UINT *delim_list;
{
int itmp;
UINT ichar;
wrefresh(win);
while (1)
{
ichar = ttygetc(1);
for (itmp = 0; delim_list[itmp]; itmp++)
{
if (ichar == delim_list[itmp])
return (ichar | 0x1000);
}
ichar = to_lower(ichar);
for (itmp = 0; nondelim_list[itmp]; itmp++)
{
if (ichar == nondelim_list[itmp])
{
if (ichar <= 0xFF && isprint(ichar))
waddch(win, to_upper(ichar));
wrefresh(win);
return (itmp);
}
}
ring_bell();
}
} /* end of winget_single */
/* end of ecuwinutil.c */
/* vi: set tabstop=4 shiftwidth=4: */