aboutsummaryrefslogtreecommitdiffstats
path: root/usr/chat.c
blob: 987b33674f6c2bd7ff331a301228924016c250c3 (plain) (blame)
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
#include "netlib.h"
/* chat: a link-port chat client. Incoming SLIP frames from the host gateway
   print as messages; SELECT brings up the on-screen keyboard, type a line and
   press START to send it. A poll loop interleaves receiving and typing so
   messages arrive while you're not mid-line. */

static char rxbuf[96];
static unsigned char rxlen, rxin, rxesc;

/* feed one non-blocking byte to the SLIP receiver; returns a frame length when
   a whole frame has arrived (copied into out), else 0. */
static unsigned char slip_poll(char *out) {
    int b = srecv_nb();
    unsigned char c, i, n;
    if (b < 0) return 0;
    c = (unsigned char)b;
    if (c == SLIP_END) {
        if (rxin && rxlen) {
            n = rxlen;
            for (i = 0; i < n; i++) out[i] = rxbuf[i];
            rxlen = 0; rxin = 0; rxesc = 0;
            return n;
        }
        rxin = 1; rxlen = 0; rxesc = 0;
        return 0;
    }
    if (!rxin) return 0;
    if (rxesc) {
        if (c == SLIP_ESC_END) c = SLIP_END;
        else if (c == SLIP_ESC_ESC) c = SLIP_ESC;
        rxesc = 0;
    } else if (c == SLIP_ESC) { rxesc = 1; return 0; }
    if (rxlen < 96) rxbuf[rxlen++] = (char)c;
    return 0;
}

void main(void) {
    char line[64], frame[96];
    unsigned char linelen = 0, n, i, c;
    puts("chat: SELECT=keyboard  START=send"); nl();
    for (;;) {
        n = slip_poll(frame);              /* incoming message? */
        if (n) {
            for (i = 0; i < n; i++) putc(frame[i]);
            nl();
        }
        c = pollin();                      /* typed a key? */
        if (c == '\n') {
            if (linelen) { slip_send(line, linelen); linelen = 0; }
        } else if (c == 8) {
            if (linelen) { linelen--; putc(8); }
        } else if (c) {
            if (linelen < 63) { line[linelen++] = (char)c; putc((char)c); }
        }
        yield();
    }
}