aboutsummaryrefslogtreecommitdiffstats
path: root/src/raw.c
blob: 4f4af5e91910a400f6f4bbc55f2558feb206a8d1 (plain)
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
/* 
 * raw.c:
 *
 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id$";

/* 
 * $Log$
 * Revision 1.8  2008/03/10 11:49:33  james
 * *** empty log message ***
 *
 * Revision 1.7  2008/03/07 13:16:02  james
 * *** empty log message ***
 *
 * Revision 1.6  2008/03/07 12:37:04  james
 * *** empty log message ***
 *
 * Revision 1.5  2008/03/06 21:34:09  james
 * *** empty log message ***
 *
 * Revision 1.4  2008/03/06 21:33:02  james
 * *** empty log message ***
 *
 * Revision 1.3  2008/03/06 17:21:41  james
 * *** empty log message ***
 *
 * Revision 1.2  2008/03/06 16:49:39  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/03/06 16:49:05  james
 * *** empty log message ***
 *
 *
 */

#include "project.h"

typedef struct {
  RX_SIGNATURE;
  int rfd;
  int wfd;
} RX_Raw;

typedef struct {
  TTY_SIGNATURE;
} RAW_TERMINAL;


static int
rx_raw_rx (RX * _r, int ch)
{
  RX_Raw *r = (RX_Raw *) _r;
  int ret;
  uint8_t c = ch;
  set_blocking (r->wfd);
  ret = (write (r->wfd, &c, 1) == 1) ? 0 : -1;
}

static void
rx_raw_close (RX * r)
{
  free (r);
}

RX *
rx_new_raw (int rfd, int wfd)
{
  RX_Raw *ret;

  ret = xmalloc (sizeof (RX_Raw));
  memset (ret, 0, sizeof (RX_Raw));

  ret->rx = rx_raw_rx;
  ret->close = rx_raw_close;

  ret->rfd = rfd;
  ret->wfd = wfd;

  return (RX *) ret;
}


static int
raw_terminal_read (TTY * _t, void *buf, int len)
{
  RAW_TERMINAL *t = (RAW_TERMINAL *) _t;
  int red, done = 0;

  set_nonblocking (t->rfd);

  do {

    red = wrap_read (t->rfd, buf, len);
    if (red < 0)
      return -1;
    if (!red)
      return done;

    buf += red;
    len -= red;
    done += red;
  }
  while (len);


  return done;
}


static int
raw_terminal_write (TTY * _t, void *buf, int len)
{
  int writ, done = 0;
  RAW_TERMINAL *t = (RAW_TERMINAL *) _t;

  set_blocking (t->wfd);

  do {

    writ = wrap_write (t->wfd, buf, len);
    if (writ < 0)
      return -1;

    if (!writ)
      usleep (1000);

    buf += writ;
    len -= writ;
    done += writ;
  }
  while (len);


  return done;
}



static void
raw_terminal_close (TTY * _t)
{
  RAW_TERMINAL *t = (RAW_TERMINAL *) _t;
  set_blocking (t->rfd);
  set_blocking (t->wfd);

  free (t);
}


TTY *
terminal_new_raw (int rfd, int wfd)
{
  RAW_TERMINAL *t;

  t = (RAW_TERMINAL *) malloc (sizeof (RAW_TERMINAL));
  memset (t, 0, sizeof (t));

  strcpy (t->name, "raw");
  t->rfd = rfd;
  t->wfd = wfd;

  set_nonblocking (rfd);
  set_nonblocking (wfd);

  t->recv = raw_terminal_read;
  // t->xmit = raw_terminal_write;
  t->close = raw_terminal_close;
  t->blocked = 0;

  return (TTY *) t;
}

static void
ansi_raw_one_shot (ANSI * a, CRT * c)
{
}

static int
ansi_raw_key (ANSI * a, Context * c, int key)
{
  return c->k->key (c->k, c, key);
}

static void
ansi_raw_parse (ANSI * a, Context * c, uint8_t * buf, int red)
{
  while (red--)
    ansi_raw_key (a, c, *(buf++));
}

static int
ansi_raw_dispatch (ANSI * a, Context * c)
{
  char buf[1024];
  int red;

  if (!a->terminal)
    return 0;

  red = a->terminal->recv (a->terminal, buf, sizeof (buf));
  if (red <= 0)
    return red;

  ansi_raw_parse (a, c, buf, red);

  return 0;
}
static void
ansi_raw_free (ANSI * a)
{
  free (a);
}

ANSI *
ansi_new_raw (int rfd, int wfd)
{
  ANSI *ret;

  ret = malloc (sizeof (ANSI));
  memset (ret, 0, sizeof (ANSI));

  ret->terminal = terminal_new_raw (rfd, wfd);
  ret->dispatch = ansi_raw_dispatch;
  ret->close = ansi_raw_free;

  return ret;
}