-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
647 lines (515 loc) · 13.7 KB
/
input.c
File metadata and controls
647 lines (515 loc) · 13.7 KB
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include "zfile.h"
#include "tilde.h"
#include "xmalloc.h"
/***************************************************************************
* InputStr *
* Liest eine Zeichenkette an Position (y,x) mit der max. Laenge length *
* Vorschlagswert fuer die Eingabe ist s selbst *
* Zurueckgegeben wird das Zeichen, mit dem die Eingabe beendet wurde *
***************************************************************************/
char *StrLeft(const char *str, size_t visible_count)
{
char *result;
size_t len;
int left_bytes;
#ifdef WITH_UTF8
mbstate_t state;
const char *s, *s_start;;
int pos = 0;
#endif
if (visible_count == 0)
return(Strdup(""));
len = StrVisualLength(str);
if (visible_count >= len)
return(Strdup(str));
#ifdef WITH_UTF8
s_start = s = str;
while(*s) {
wchar_t wc;
size_t sz;
int width;
s_start = s;
memset(&state, 0, sizeof(state));
sz = mbrtowc(&wc, s, 4, &state);
if(sz == (size_t)-1 || sz == (size_t)-2) {
if( (*s++ & 0xc0) == 0xc0) { /* skip to next char */
while( (*s & 0xc0) == 0x80)
s++;
}
pos += 4; /* assume worst case */
} else {
s += sz;
width = wcwidth(wc);
if(width >= 0)
pos += width;
else
pos++;
}
if(pos > visible_count)
break; /* exceeds limit */
}
if(*s)
left_bytes = s_start - str;
else
left_bytes = s - str;
#else
left_bytes = visible_count;
#endif
result = Strndup(str, left_bytes);
return(result);
}
char *StrRight(const char *str, size_t visible_count)
{
char *result;
size_t visual_len;
#ifdef WITH_UTF8
int left_bytes, pos = 0;
mbstate_t state;
const char *s, *s_start;
#endif
if (visible_count == 0)
return(Strdup(""));
visual_len = StrVisualLength(str);
if(visual_len <= visible_count)
return(Strdup(str));
#ifdef WITH_UTF8
s_start = s = str;
while(*s) {
wchar_t wc;
size_t sz;
int width;
s_start = s;
memset(&state, 0, sizeof(state));
sz = mbrtowc(&wc, s, 4, &state);
if(sz == (size_t)-1 || sz == (size_t)-2) {
if( (*s++ & 0xc0) == 0xc0) { /* skip to next char */
while( (*s & 0xc0) == 0x80)
s++;
}
pos += 4; /* assume worst case */
} else {
s += sz;
width = wcwidth(wc);
if(width >= 0)
pos += width;
else
pos++;
}
if((visual_len - pos) < visible_count)
break; /* fits */
}
if(*s)
left_bytes = s_start - str;
else
left_bytes = s - str;
result = Strdup(&str[left_bytes]);
#else
result = Strdup( &str[visual_len - visible_count] );
#endif
return(result);
}
int StrVisualLength(const char *str)
{
int len;
#ifdef WITH_UTF8
int pos = 0;
size_t sz;
mbstate_t state;
const char *s = str;
wchar_t buffer[PATH_LENGTH+1];
do {
memset(&state, 0, sizeof(state));
sz = mbrtowc(&buffer[pos], s, 4, &state);
if( sz == (size_t) -1 || sz == (size_t)-2 ) {
if( (*s++ & 0xc0) == 0xc0) { /* skip to next char */
while( (*s & 0xc0) == 0x80)
s++;
}
buffer[pos++] = L'#'; /* Assume worst case: */
buffer[pos++] = L'#'; /* 1 wide char produces 4 visible chars */
buffer[pos++] = L'#';
buffer[pos] = L'#';
} else {
s += sz;
}
pos++;
} while(sz != 0);
len = wcswidth(buffer, PATH_LENGTH);
if(len < 0)
len = pos; /* should not happen */
#else
len = strlen(str);
#endif
return len;
}
/* returns byte position for visual position */
int VisualPositionToBytePosition(const char *str, int visual_pos)
{
#ifdef WITH_UTF8
mbstate_t state;
const char *s, *s_start;
int pos = 0;
s_start = s = str;
while(*s) {
wchar_t wc;
size_t sz;
int width;
s_start = s;
memset(&state, 0, sizeof(state));
sz = mbrtowc(&wc, s, 4, &state);
if(sz == (size_t)-1 || sz == (size_t)-2) {
if( (*s++ & 0xc0) == 0xc0) { /* skip to next char */
while( (*s & 0xc0) == 0x80)
s++;
}
pos += 4; /* assume worst case */
} else {
s += sz;
width = wcwidth(wc);
if(width > 0)
pos += width;
else
pos++;
}
if(pos > visual_pos)
return( s_start - str );
}
return( s - str );
#else
return visual_pos;
#endif
}
int InputString(char *s, int y, int x, int cursor_pos, int length, char *term)
/* Ein- und Ausgabestring */
/* Position auf Bildschirm */
/* max. Laenge */
/* Menge von Terminierungszeichen */
{
int p; /* Aktuelle Position */
int c1; /* Gelesenes Zeichen */
int i, n; /* Laufvariable */
char *pp;
BOOL len_flag = FALSE;
char path[PATH_LENGTH + 1];
char buf[2], sbuf[20], *ls, *rs;
static BOOL insert_flag = TRUE;
if(length < 1)
return -1;
buf[1] = '\0';
strcpy(sbuf, "");
/* Feld gefuellt ausgeben */
/*------------------------*/
print_time = FALSE;
curs_set(1);
MvAddStr( y, x, s );
leaveok(stdscr, FALSE);
for(i=strlen(s); i < length; i++)
addch( '_' );
p = cursor_pos;
MvAddStr( y, x, s );
nodelay( stdscr, TRUE );
do {
c1 = wgetch(stdscr);
if ( c1 != ERR ) {
if( c1 >= ' ' && c1 < 0xff && c1 != 127 ) {
if ( len_flag == TRUE) beep();
else {
buf[0] = (char)c1;
strcat(sbuf, buf);
}
} else {
/* Control symbols */
switch( c1 )
{
case 'C' & 0x1f : c1 = 27;
break;
case KEY_LEFT : if( p > 0 )
p--;
else
beep();
break;
case KEY_RIGHT : if( p < StrVisualLength(s) )
p++;
else
beep();
break;
case KEY_UP :
nodelay(stdscr, FALSE);
pp = GetHistory();
nodelay(stdscr, TRUE);
if (pp == NULL) break;
if(*pp)
{
ls = StrLeft(pp, length);
strcpy(s, ls);
free(ls);
p = StrVisualLength(s);
MvAddStr( y, x, s );
for(i=p; i < length; i++)
addch( '_' );
RefreshWindow( stdscr );
doupdate();
}
break;
case KEY_HOME : p = 0;
break;
case KEY_END : p = StrVisualLength( s );
break;
case KEY_DC : n = StrVisualLength(s);
if( p < n )
{
ls = StrLeft(s, p);
rs = StrRight(s, n - p - 1);
strcpy(s, ls);
strcat(s, rs);
free(ls);
free(rs);
MvAddStr( y, x, s );
for(i=StrVisualLength(s); i < n; i++)
addch( '_' );
}
break;
case 0x08 :
case 0x7F :
case KEY_BACKSPACE : if( p > 0 )
{
n = StrVisualLength(s);
ls = StrLeft(s, p - 1);
rs = StrRight(s, n - p);
strcpy(s, ls);
strcat(s, rs);
free(ls);
free(rs);
MvAddStr( y, x, s );
for(i=StrVisualLength(s); i < n; i++)
addch( '_' );
p--;
}
else
beep();
break;
case KEY_DL : for(i=0; i < StrVisualLength(s) - p; i++) addch( '_' );
ls = StrLeft(s, p);
strcpy(s, ls);
free(ls);
break;
case KEY_EIC :
case KEY_IC : insert_flag ^= TRUE;
break;
case '\t' : if(( pp = GetMatches(s)) == NULL) {
break;
}
if(*pp)
{
ls = StrLeft(pp, length);
strcpy(s, ls);
free(ls);
p = StrVisualLength(s);
free(pp);
MvAddStr( y, x, s );
for(i=p; i < length; i++) addch( '_' );
RefreshWindow( stdscr );
doupdate();
}
break;
#ifdef KEY_RESIZE
case KEY_RESIZE: resize_request = TRUE;
break;
#endif
#ifdef KEY_F
case KEY_F(2) :
#endif
case 'F' & 0x1f : if(KeyF2Get( statistic.tree,
statistic.disp_begin_pos,
statistic.cursor_pos, path))
{
/* beep(); */
break;
}
if(*path)
{
ls = StrLeft(path, length);
strcpy(s, ls);
free(ls);
p = StrVisualLength(s);
MvAddStr( y, x, s );
for(i=p; i < length; i++) addch( '_' );
RefreshWindow( stdscr );
doupdate();
}
break;
default : if( c1 == LF ) c1 = CR;
break;
} /* switch */
} /* else control symbols */
} else {
if (strlen(sbuf) > 0) {
if ( insert_flag ) {
/* append symbol */
if ( p >= StrVisualLength(s)) strcat(s, sbuf);
else {
/* insert symbol at cursor position */
if ( p > 0 ) ls = StrLeft(s, p);
else ls = Strdup("");
rs = StrRight(s, StrVisualLength(s) - p);
strcpy(s, ls);
strcat(s, sbuf);
strcat(s, rs);
free(ls);
free(rs);
}
} else {
/* owerwrite symbol at cursor position */
if ( p > StrVisualLength(s) - 1) strcat(s, sbuf);
else {
if (p > 0) ls = StrLeft(s, p);
else ls = Strdup("");
rs = StrRight(s, StrVisualLength(s) - p - 1);
strcpy(s, ls);
strcat(s, sbuf);
strcat(s, rs);
free(ls);
free(rs);
}
}
strcpy(sbuf, "");
p++;
}
if (StrVisualLength(s) >= length) len_flag = TRUE;
else len_flag = FALSE;
MvAddStr( y, x, s );
wmove(stdscr, y, x + p);
}
} while( c1 != 27 && c1 != CR );
nodelay( stdscr, FALSE );
p = strlen( s );
move( y, x + p );
for(i=0; i < length - p; i++ )
addch( ' ' );
move( y, x );
leaveok( stdscr, TRUE);
curs_set(0);
print_time = TRUE;
InsHistory( s );
#ifdef READLINE_SUPPORT
pp = tilde_expand(s);
#else
pp = Strdup(s);
#endif
strncpy( s, pp, length - 1);
s[length]='\0';
xfree(pp);
return( c1 );
}
int InputChoise(char *msg, char *term)
{
int c;
ClearHelp();
curs_set(1);
leaveok(stdscr, FALSE);
mvprintw( LINES - 2, 1, "%s", msg );
RefreshWindow( stdscr );
doupdate();
do
{
c = Getch();
if(c >= 0)
if( islower( c ) ) c = toupper( c );
} while( c != -1 && !strchr( term, c ) );
if(c >= 0)
echochar( c );
move( LINES - 2, 1 ); clrtoeol();
leaveok(stdscr, TRUE);
curs_set(0);
return( c );
}
int GetTapeDeviceName( void )
{
int result;
char path[PATH_LENGTH * 2 +1];
result = -1;
ClearHelp();
(void) strcpy( path, statistic.tape_name );
MvAddStr( LINES - 2, 1, "Tape-Device:" );
if( InputString( path, LINES - 2, 14, 0, COLS - 15, "\r\033" ) == CR )
{
result = 0;
(void) strcpy( statistic.tape_name, path );
}
move( LINES - 2, 1 ); clrtoeol();
return( result );
}
void HitReturnToContinue(void)
{
#ifndef XCURSES
curs_set(1);
vidattr( A_REVERSE );
putp( "[Hit return to continue]" );
vidattr( 0 );
(void) fflush( stdout );
(void) Getch();
#endif /* XCURSES */
curs_set(0);
doupdate();
}
BOOL KeyPressed()
{
BOOL pressed = FALSE;
#if !defined( linux ) || !defined( TERMCAP )
nodelay( stdscr, TRUE );
if( wgetch( stdscr ) != ERR ) pressed = TRUE;
nodelay( stdscr, FALSE );
#endif /* linux/TERMCAP */
return( pressed );
}
BOOL EscapeKeyPressed()
{
BOOL pressed = FALSE;
int c;
#if !defined( linux ) || !defined( TERMCAP )
nodelay( stdscr, TRUE );
if( ( c = wgetch( stdscr ) ) != ERR ) pressed = TRUE;
nodelay( stdscr, FALSE );
#endif /* linux/TERMCAP */
return( ( pressed && c == ESC ) ? TRUE : FALSE );
}
#ifdef VI_KEYS
int ViKey( int ch )
{
switch( ch )
{
case VI_KEY_UP: ch = KEY_UP; break;
case VI_KEY_DOWN: ch = KEY_DOWN; break;
case VI_KEY_RIGHT: ch = KEY_RIGHT; break;
case VI_KEY_LEFT: ch = KEY_LEFT; break;
case VI_KEY_PPAGE: ch = KEY_PPAGE; break;
case VI_KEY_NPAGE: ch = KEY_NPAGE; break;
}
return(ch);
}
#endif /* VI_KEYS */
#ifdef _IBMR2
#undef wgetch
int AixWgetch( WINDOW *w )
{
int c;
if( ( c = wgetch( w ) ) == KEY_ENTER ) c = LF;
return( c );
}
#endif
int WGetch(WINDOW *win)
{
int c;
c = wgetch(win);
#ifdef KEY_RESIZE
if(c == KEY_RESIZE) {
resize_request = TRUE;
c = -1;
}
#endif
return(c);
}
int Getch()
{
return(WGetch(stdscr));
}