File: mincurses.c

package info (click to toggle)
squishyball 0.1~svn19085-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 264 kB
  • ctags: 306
  • sloc: ansic: 4,777; sh: 115; makefile: 21
file content (731 lines) | stat: -rw-r--r-- 14,070 bytes parent folder | download | duplicates (3)
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
 *
 *  mincurses
 *
 *      Copyright (C) 2010 Xiph.Org
 *      Portions Copyright (C) 1998-2009 Free Software Foundation, Inc.
 *
 *  mincurses is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  mincurses is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with rtrecord; see the file COPYING.  If not, write to the
 *  Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 */

/* Encapsulate the curses/terminfo calls for presenting a little panel
   display at the bottom of a terminal window. */

#define _GNU_SOURCE
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

#ifndef _REENTRANT
# define _REENTRANT
#endif

#include <ncurses.h>
#include <curses.h>
#include <term.h>
#include <term_entry.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <poll.h>
#ifdef __APPLE__
#include <sys/select.h>
#include <sys/time.h>
#endif

#include "mincurses.h"

#define FIFO_SIZE    160
#define BUFF_SIZE    160

/***************************** INPUT SIDE *******************************/

struct tinfo_fkeys {
  unsigned offset;
  chtype code;
};

#if USE_FKEYSF
#define _nc_tinfo_fkeys _nc_tinfo_fkeysf()
#else
extern const struct tinfo_fkeys _nc_tinfo_fkeys[];
#endif

/* In ncurses, all the keybindings are tied to the SP. Calls into the
   keybinding code all require the SP be set. Low level SP
   management/creation is not exposed, and the high level entry points
   that initialize the SP also drool on the rest of the terminal.  For
   this reason, we duplicate some code. */

typedef struct tries TRIES;

struct tries {
  TRIES    *child;
  TRIES    *sibling;
  unsigned char    ch;
  unsigned short   value;
};

TRIES *keytree=NULL;
int fifo[FIFO_SIZE];
int key_init=0;

/*
 * Returns the keycode associated with the given string.  If none is found,
 * return OK.  If the string is only a prefix to other strings, return ERR.
 * Otherwise, return the keycode's value (neither OK/ERR).
 */

static int find_definition(TRIES * tree, const char *str){
  TRIES *ptr;
  int result = OK;

  if (str != 0 && *str != '\0') {
    for (ptr = tree; ptr != 0; ptr = ptr->sibling) {
      if ((unsigned char)(*str) == ptr->ch) {
        if (str[1] == '\0' && ptr->child != 0) {
          result = ERR;
        } else if ((result = find_definition(ptr->child, str + 1)) == OK) {
          result = ptr->value;
        } else if (str[1] == '\0') {
          result = ERR;
        }
      }
      if (result != OK)
        break;
    }
  }
  return (result);
}

extern int _nc_add_to_try(TRIES ** tree, const char *str, unsigned code);

static void minc_init_keytry(){
  size_t n;
  TERMTYPE *tp=&(cur_term->type);

  for (n = 0; _nc_tinfo_fkeys[n].code; n++) {
    if (_nc_tinfo_fkeys[n].offset < STRCOUNT) {
      _nc_add_to_try(&keytree,tp->Strings[_nc_tinfo_fkeys[n].offset],
                     _nc_tinfo_fkeys[n].code);
    }
  }
#if NCURSES_XNAMES
  for (n = STRCOUNT; n < NUM_STRINGS(tp); ++n) {
    const char *name = ExtStrname(tp, n, strnames);
    char *value = tp->Strings[n];
    if (name && *name == 'k' && value && find_definition(keytree,value) == OK) {
      _nc_add_to_try(&keytree, value, n - STRCOUNT + KEY_MAX);
    }
  }
#endif
  key_init=1;
}

static int head = -1;
static int tail = 0;
static int peek = 0;

#define h_inc() { head == FIFO_SIZE-1 ? head = 0 : head++; if (head == tail) head = -1, tail = 0;}
#define t_inc() { tail == FIFO_SIZE-1 ? tail = 0 : tail++; if (tail == head) tail = -1;}
#define t_dec() { tail == 0 ? tail = FIFO_SIZE-1 : tail--; if (head == tail) fifo_clear();}
#define p_inc() { peek == FIFO_SIZE-1 ? peek = 0 : peek++;}

#define cooked_key_in_fifo()    ((head != -1) && (peek != head))
#define raw_key_in_fifo()       ((head != -1) && (peek != tail))

static inline int fifo_peek(){
   int ch = fifo[peek];
   p_inc();
   return ch;
}

static inline int fifo_pull(){
  int ch;
  ch = fifo[head];

  if (peek == head) {
    h_inc();
    peek = head;
  } else
    h_inc();

  return ch;
}

static inline int fifo_push(int nonblock){
  int n;
  int ch = 0;
  unsigned char c2 = 0;

  if (tail == -1)
    return ERR;

  if(nonblock){
#ifdef __APPLE__
    int ret;
    fd_set fds;
    struct timeval zeroto = {0,0};
    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO,&fds);
    ret=select(STDIN_FILENO+1,&fds,NULL,NULL,&zeroto);
    if(ret!=1){
      return ERR;
    }
#else
    struct pollfd fds={STDIN_FILENO,POLLIN,0};
    poll(&fds, 1, 0);
    if(!fds.revents&(POLLIN)){
      return ERR;
    }
#endif
  }
  n = read(STDIN_FILENO, &c2, 1);
  ch = c2;
  if (n<=0) ch = ERR;
  fifo[tail] = ch;
  if (head == -1)
    head = peek = tail;
  t_inc();
  return ch;
}

static inline void fifo_clear(){
  memset(fifo, 0, sizeof(fifo));
  head = -1;
  tail = peek = 0;
}

int min_getch(int nonblock){
  TRIES *ptr;
  int ch = 0;

  if(!key_init) minc_init_keytry();
  ptr=keytree;

  while (1) {
    if (cooked_key_in_fifo() && fifo[head] >= KEY_MIN) {
      break;
    } else if (!raw_key_in_fifo()) {
      ch = fifo_push(nonblock);
      if (ch == ERR) {
        peek = head;
        return ERR;
      }
    }

    ch = fifo_peek();
    if (ch >= KEY_MIN) {
      peek = head;
      t_dec();
      return ch;
    }

    while ((ptr != NULL) && (ptr->ch != (unsigned char) ch))
      ptr = ptr->sibling;

    if (ptr == NULL) break;
    if (ptr->value != 0) {
      if (peek == tail)
        fifo_clear();
      else
        head = peek;
      return (ptr->value);
    }
    ptr = ptr->child;
  }
  ch = fifo_pull();
  peek = head;
  return ch;
}

/***************************** OUTPUT SIDE ******************************/

TTY orig;
TTY term;

static int outfd;
static int initted=0;
static int cursor_line_offset=0;
static int panel_lines=0;
static char outbuf[BUFF_SIZE];
static int buf_fill=0;

int min_flush(){
  int len = buf_fill;
  char *b = outbuf;
  while(len){
    int ret;
    errno=0;
    ret=write(outfd,b,len);
    if(ret<=0){
      if(ret==0 || errno==EINTR)continue;
      return EOF;
    }else{
      b+=ret;
      len-=ret;
    }
  }
  buf_fill=0;
  return 0;
}

int min_putchar(int c){
  outbuf[buf_fill]=c;
  buf_fill++;
  if(buf_fill==BUFF_SIZE)
    return min_flush();
  return 0;
}

int min_putp(const char *str){
  return tputs(str, 1, min_putchar);
}

int min_write(const char *str,int len){
  int ret=0;
  while(len){
    int bytes = (buf_fill+len>BUFF_SIZE ? BUFF_SIZE-buf_fill : len);
    memcpy(outbuf+buf_fill,str,bytes);
    buf_fill+=bytes;
    if(buf_fill==BUFF_SIZE){
      ret=min_flush();
      if(ret)return ret;
    }
    str+=bytes;
    len-=bytes;
  }
  return 0;
}

int min_putstr(const char *str){
  int len=strlen(str);
  return min_write(str,len);
}

/************** high level operations ***********************/

void min_mvcur(int x, int y){
  int yoff = y - cursor_line_offset;

  while(yoff<0){
    if(cursor_up)min_putp(cursor_up);
    cursor_line_offset--;
    yoff++;
  }
  while(yoff>0){
    if(cursor_down)min_putp(cursor_down);
    cursor_line_offset++;
    yoff--;
  }

  if(column_address)min_putp(tparm(column_address,x));
}

static void setup_term_customize(void){
  if (cur_term != 0) {
    term = cur_term->Nttyb;
#ifdef TERMIOS
    term.c_lflag &= ~ICANON;
    term.c_iflag &= ~ICRNL;
    term.c_lflag &= ~(ECHO | ECHONL);
    term.c_iflag &= ~(ICRNL | INLCR | IGNCR);
    term.c_oflag &= ~(ONLCR);
    term.c_lflag &= ~(ISIG);
    term.c_cc[VMIN] = 1;
    term.c_cc[VTIME] = 0;
#else
    term.sg_flags |= RAW;
    term.sg_flags &= ~(ECHO | CRMOD);
#endif
    SET_TTY(outfd,&term);
    cur_term->Nttyb = term;
  }
}

extern void _nc_init_acs(void);
int min_panel_init(int pl){
  int i,ret = OK;
  if(!initted){

    if(isatty(STDOUT_FILENO))
      outfd=STDOUT_FILENO;
    else
      outfd=STDERR_FILENO;

    /* save original terminal setup for purposes of restoring later */
    GET_TTY(outfd,&orig);

    /* low level terminfo setup with defaults and no error return */
    setupterm(0,outfd,0);

    /* terminal settings now in a known state; further configure */
    setup_term_customize();

    /* enable graphics character set */
    //if(ena_acs)min_putp(ena_acs);
    _nc_init_acs();

    if(!cursor_up || !cursor_down || !column_address || pl>lines){
      SET_TTY(outfd,&orig);
      return ERR;
    }

    /* set up keytables */
    if(keypad_xmit)min_putp(tparm(keypad_xmit,1));
    minc_init_keytry();

    panel_lines=pl;
    for(i=0;i<pl-1;i++)
      min_putstr("\r\n");
    cursor_line_offset=pl-1;
    initted=1;
    min_flush();
  }
  return ret;
}

int min_panel_expand(int l,int bottomp){
  int i,ret=0;

  if(panel_lines+l>lines)return 1;

  if(bottomp){
    min_mvcur(0,panel_lines);
    for(i=0;i<l-1;i++)
      ret|=min_putstr("\n\r");
    panel_lines+=l;
    cursor_line_offset=panel_lines-1;
  }else{
    min_mvcur(0,panel_lines+l-1);
    min_mvcur(0,0);
    if(parm_insert_line){
      ret|=min_putp(tparm(parm_insert_line,l));
    }else
      ret=1;
    panel_lines+=l;
    cursor_line_offset=0;
  }
  return ret;
}

int min_panel_contract(int l,int bottomp){
  int ret=0;
  if(l>0){
    if(l>panel_lines)l=panel_lines;
    if(bottomp){
      min_mvcur(0,panel_lines-l);
      if(parm_delete_line){
        ret|=min_putp(tparm(parm_delete_line,l));
      }else
        ret=1;
    }else{
      min_mvcur(0,0);
      if(parm_delete_line){
        ret|=min_putp(tparm(parm_delete_line,l));
      }else
        ret=1;
    }
    panel_lines-=l;
  }
  return ret;
}

void min_panel_remove(){
  if(initted){
    if(parm_delete_line){
      min_mvcur(0,0);
      min_putp(tparm(parm_delete_line,panel_lines));
    }
    min_unset();
    min_showcur();
    min_flush();
    SET_TTY(outfd,&orig);
    initted=0;
  }
}

int min_hidecur(){
  if(cursor_invisible){
    min_putp(cursor_invisible);
    return 0;
  }else
    return 1;
}

int min_showcur(){
  if(cursor_visible){
    min_putp(cursor_visible);
    return 0;
  }else
    return 1;
}

int min_clreol(void){
  if(clr_eol){
    min_putp(clr_eol);
    return 0;
  }else
    return 1;
}

int min_clrbol(void){
  if(clr_bol){
    min_putp(clr_bol);
    return 0;
  }else
    return 1;
}

static int acs=0;
static int fg=-1;
static int bg=-1;
static int bold=0;
static int blink=0;
static int rev=0;
static int ul=0;

static int unset(){
  int ret=0;
  /* some terminals do not consider acs mode an attribute to be cleared, some do */
  if(exit_alt_charset_mode){
     min_putp(exit_alt_charset_mode);
  }else
    ret|=1;
  if(exit_attribute_mode){
    min_putp(exit_attribute_mode);
    min_putp(exit_alt_charset_mode);
    return ret;
  }else
    return 1;
}

int min_unset(){
  fg=-1;
  bg=-1;
  bold=0;
  rev=0;
  blink=0;
  acs=0;
  ul=0;
  return unset();
}

static int setfg(int c){
  if(set_a_foreground){
    fg=c;
    min_putp(tparm(set_a_foreground,c));
    return 0;
  }
  return 1;
}

static int setbg(int c){
  if(set_a_background){
    bg=c;
    min_putp(tparm(set_a_background,c));
    return 0;
  }
  return 1;
}

static int setbold(void){
  if(enter_bold_mode){
    min_putp(enter_bold_mode);
    bold=1;
    return 0;
  }
  return 1;
}

static int setblink(void){
  if(enter_blink_mode){
    min_putp(enter_blink_mode);
    blink=1;
    return 0;
  }
  return 1;
}

static int setunderline(void){
  if(enter_underline_mode){
    ul=1;
    min_putp(enter_underline_mode);
    return 0;
  }
  return 1;
}

static int setgfx(void){
  if(enter_alt_charset_mode){
    acs=1;
    min_putp(enter_alt_charset_mode);
    return 0;
  }
  return 1;
}

static int setreverse(void){
  if(enter_reverse_mode){
    min_putp(enter_reverse_mode);
    rev=1;
    return 0;
  }
  return 1;
}

static int reset(){
  int ret=0;
  if(acs)ret|=setgfx();
  if(fg>=0)ret|=setfg(fg);
  if(bg>=0)ret|=setbg(bg);
  if(bold)ret|=setbold();
  if(blink)ret|=setblink();
  if(rev)ret|=setreverse();
  if(ul)ret|=setunderline();
  return ret;
}

int min_fg(int c){
  int ret=0;
  if(c<0){
    if(fg>=0){
      fg=-1;
      ret|=unset();
      ret|=reset();
    }
  }else{
    if(fg!=c)
      ret=setfg(c);
  }
  return ret;
}

int min_bg(int c){
  int ret=0;
  if(c<0){
    if(bg>=0){
      bg=-1;
      ret|=unset();
      ret|=reset();
    }
  }else{
    if(bg!=c)
      ret=setbg(c);
  }
  return ret;
}

int min_color(int f,int b){
  int ret=0;
  fg=-1;
  bg=-1;
  ret|=unset();
  ret|=reset();
  ret|=min_fg(f);
  ret|=min_bg(b);
  return ret;
}

int min_bold(int flag){
  int ret=0;
  if(!flag){
    if(bold){
      bold=0;
      ret|=unset();
      ret|=reset();
    }
  }else{
    if(!bold)
      ret=setbold();
  }
  return ret;
}

int min_blink(int flag){
  int ret=0;
  if(!flag){
    if(blink){
      blink=0;
      ret|=unset();
      ret|=reset();
    }
  }else{
    if(!blink)
      ret=setblink();
  }
  return ret;
}

int min_underline(int flag){
  int ret=0;
  if(!flag){
    if(ul){
      if(exit_underline_mode){
        ul=0;
        min_putp(exit_underline_mode);
      }else{
        ul=0;
        ret|=unset();
        ret|=reset();
      }
    }
  }else{
    if(!ul)
      ret=setunderline();
  }
  return ret;
}

int min_gfx(int flag){
  int ret=0;
  if(!flag){
    if(acs){
      if(exit_alt_charset_mode){
        acs=0;
        min_putp(exit_alt_charset_mode);
      }else{
        acs=0;
        ret|=unset();
        ret|=reset();
      }
    }
  }else{
    if(!acs)
      ret=setgfx();
  }
  return ret;
}

int min_reverse(int flag){
  int ret=0;
  if(!flag){
    if(rev){
      rev=0;
      ret|=unset();
      ret|=reset();
    }
  }else{
    if(!rev)
      ret=setreverse();
  }
  return ret;
}