File: user_pipe.c

package info (click to toggle)
kbtin 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,856 kB
  • sloc: ansic: 18,627; perl: 179; sh: 88; makefile: 17
file content (125 lines) | stat: -rw-r--r-- 2,818 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
115
116
117
118
119
120
121
122
123
124
125
#include "tintin.h"
#include "protos/colors.h"
#include "protos/globals.h"
#include "protos/unicode.h"
#include "protos/user.h"
#include "protos/utils.h"


static mbstate_t outstate;
#define OUTSTATE &outstate
static char *i_pos;

static void userpipe_init(void)
{
    tty=isatty(1);
    color=lastcolor=7;
    i_pos=done_input;
    ZERO(outstate);
}

static void userpipe_textout(const char *txt)
{
    char buf[BUFFER_SIZE+64], *b=buf;

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

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

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

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

static void userpipe_passwd(bool x)
{
    // TODO: stty echo off?
}

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

static void user_illegal(void)
{
    die("DRIVER: operation not supported");
}

static void user_noop(void)
{
}

void userpipe_initdriver(void)
{
    ui_sep_input=false;
    ui_con_buffer=false;
    ui_keyboard=false;
    ui_own_output=false;
    ui_drafts=false;

    user_init           = userpipe_init;
    user_done           = user_noop;
    user_pause          = user_noop;
    user_resume         = user_noop;
    user_textout        = userpipe_textout;
    user_textout_draft  = (void (*)(const char*, bool))user_noop;
    user_process_kbd    = userpipe_process_kbd;
    user_beep           = userpipe_beep;
    user_keypad         = (void (*)(bool))user_illegal;
    user_retain         = user_illegal;
    user_passwd         = userpipe_passwd;
    user_condump        = (void (*)(FILE *))user_illegal;
    user_title          = (void (*)(const char*, ...))user_illegal;
    user_resize         = userpipe_resize;
    user_show_status    = user_illegal;
    user_mark_greeting  = user_noop;
    user_input_color    = (void (*)(int))user_illegal;
}