File: user_pipe.c

package info (click to toggle)
kbtin 1.0.15-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,852 kB
  • ctags: 1,074
  • sloc: ansic: 17,345; sh: 7,584; makefile: 135; perl: 118
file content (114 lines) | stat: -rw-r--r-- 2,583 bytes parent folder | download
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
#include "tintin.h"
#include "unicode.h"
#include "ui.h"
#include "protos/utils.h"


extern int wc_to_utf8(char *d, const wchar_t *s, int n, int maxb);
extern int getcolor(char **ptr,int *color,const int flag);
extern int one_utf8_to_mb(char **d, char **s, mbstate_t *cs);
mbstate_t outstate;
#define OUTSTATE &outstate
extern void user_illegal();
extern void user_noop();
extern int need_resize;


static char *i_pos;


static void userpipe_init(void)
{
    tty=isatty(1);
    color=lastcolor=7;
    i_pos=done_input;
    memset(&outstate, 0, sizeof(outstate));
}

static void userpipe_textout(char *txt)
{
    char buf[BUFFER_SIZE],*a,*b;

    for(a=txt,b=buf; *a; )
        switch(*a)
        {
        case '~':
            if (getcolor(&a,&color,1))
            {
                if (color==-1)
                    color=lastcolor;
                if(tty)
                    b+=sprintf(b,COLORCODE(color));
            }
            else
                *b++='~';
            a++;
            break;
        case '\n':
            lastcolor=color;
        default:
            one_utf8_to_mb(&b, &a, &outstate);
        }
    write_stdout(buf,b-buf);
}

static int userpipe_process_kbd(struct session *ses, WC ch)
{

    switch(ch)
    {
    case '\n':
        *i_pos=0;
        i_pos=done_input;
        return 1;
    case 8:
        if (i_pos!=done_input)
            i_pos--;
        return 0;
    default:
        if (i_pos-done_input>=BUFFER_SIZE-8)
            return 0;
        i_pos+=wc_to_utf8(i_pos, &ch, 1, BUFFER_SIZE-(i_pos-done_input));
    case 0:
        ;
    }
    return 0;
}

static void userpipe_beep(void)
{
    write_stdout("\007",1);
    /* should it beep if we're redirected to a pipe? */
}

static void userpipe_resize(void)
{
    need_resize=0;
}

void userpipe_initdriver()
{
    ui_sep_input=0;
    ui_con_buffer=0;
    ui_keyboard=0;
    ui_own_output=0;
    ui_tty=1;
    ui_drafts=0;

    user_init           = userpipe_init;
    user_done           = user_noop;
    user_pause          = user_illegal;
    user_resume         = user_illegal;
    user_textout        = userpipe_textout;
    user_textout_draft  = user_noop;
    user_process_kbd    = userpipe_process_kbd;
    user_beep           = userpipe_beep;
    user_keypad         = user_illegal;
    user_retain         = user_illegal;
    user_passwd         = user_noop;
    user_condump        = user_illegal;
    user_title          = (printffunc*)user_illegal;
    user_resize         = userpipe_resize;
    user_show_status    = user_illegal;
    user_mark_greeting  = user_noop;
}