File: default_console.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (420 lines) | stat: -rw-r--r-- 9,875 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
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
/*
 * Copyright (c) 1997-1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * This file implements some of the methods for "console" fd's.
 * It is intended to serve as the simplest console possible for a
 * default client OS. The posix library contains a linktime dependency
 * on this console stream to allow for printf to work from the getgo.
 *
 * Note that this implementation contans linktime dependencies on the
 * kernel library (or the unix library) for console_putchar and friends.
 */

#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/ioctl.h>

#include <oskit/io/asyncio.h>
#include <oskit/io/ttystream.h>
#include <oskit/io/posixio.h>
#include <oskit/console.h>
#include <oskit/c/fd.h>

static struct oskit_asyncio_ops		asyncio_ops;
static struct oskit_ttystream_ops	ttystream_ops;
static struct oskit_posixio_ops		posixio_ops;

static struct oskit_ttystream  console_ttystream  = { &ttystream_ops };
static struct oskit_asyncio    console_asyncio    = { &asyncio_ops };
static struct oskit_posixio    console_posixio    = { &posixio_ops };

/*
 * I doubt is makes any sense to do tty processing here, but this is
 * intended to be a super simple console interface, and we need to do
 * a little bit of line discipline stuff to make life a little nicer.
 *
 * There is only one copy since there is only one "tty." The right
 * thing to do might be to export an interface from the base console
 * code to support changing its tty values. It should be noted that
 * virtually none of this stuff can be changed in this simple console,
 * but at least we can support a couple of simple things, like turning
 * of echo. 
 */
struct termios my_termios =
{ {
	ICRNL | IXON,			/* input flags */
	OPOST,				/* output flags */
	CS8,				/* control flags */
	ECHO | ECHOE | ECHOK | ICANON,	/* local flags */
	{	'D'-64,			/* VEOF */
		_POSIX_VDISABLE,	/* VEOL */
		0,
		'H'-64,			/* VERASE */
		0,
		'U'-64,			/* VKILL */
		0,
		0,
		'C'-64,			/* VINTR */
		'\\'-64,		/* VQUIT */
		'Z'-64,			/* VSUSP */
		0,
		'Q'-64,			/* VSTART */
		'S'-64,			/* VSTOP */
	},
	B9600,				/* input speed */
	B9600,				/* output speed */
} };

/*
 * This is a common symbol. If the program is linked with the startup
 * library, this will become the definition. Otherwise, the posix
 * library will use its internal default, which exists to ask someone
 * else for a console at runtime.
 */
struct oskit_stream *default_console_stream =
			(struct oskit_stream *) &console_ttystream;

static OSKIT_COMDECL
query(oskit_ttystream_t *si, const struct oskit_guid *iid, void **out_ihandle)
{
	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_ttystream_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_stream_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &console_ttystream;
		return 0;
	}

	if (memcmp(iid, &oskit_posixio_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &console_posixio;
		return 0;
	}

	if (memcmp(iid, &oskit_asyncio_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &console_asyncio;
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
addref(oskit_ttystream_t *si)
{
	return 1;
}

static OSKIT_COMDECL_U
release(oskit_ttystream_t *si)
{
	return 1;
}

static OSKIT_COMDECL
read(oskit_ttystream_t *si,
     void *buf, oskit_u32_t len, oskit_u32_t *out_actual)
{
	int	i = 0, c;
	char	*str = (char *) buf;

	if (len == 0)
		return 0;

	/* implement simple line discipline */
	while (i < len) {
		c = console_getchar();
		if (c == EOF) {
			if (i == 0)
				return NULL;
			break;
		}
		if (c == '\r')
			c = '\n';
		else if (c == '\b') {
			if (i > 0) {
				if (my_termios.c_lflag & ECHO) {
					console_putchar(c);
					console_putchar(' ');
					console_putchar(c);
				}
				i--;
			}
			continue;
		}
		else if (c == '\025') {		/* ^U -- kill line */
			while (i) {
				if (my_termios.c_lflag & ECHO) {
					console_putchar('\b');
					console_putchar(' ');
					console_putchar('\b');
				}
				i--;
			}
			str[0] = '\0';
			continue;
		}
		if (my_termios.c_lflag & ECHO)
			console_putchar(c);
		str[i++] = c;
		if (c == '\n')
			break;
	}
	if (i < len)
		str[i] = '\0';

	*out_actual = i;
	return 0;
}

static OSKIT_COMDECL
write(oskit_ttystream_t *si, const void *buf, oskit_u32_t nb,
      oskit_u32_t *out_actual)
{
	extern int console_putbytes(const char *, int length);

	console_putbytes((const char *) buf, (int) nb);

	*out_actual = nb;
	return 0;
}

static OSKIT_COMDECL
seek(oskit_ttystream_t *si, oskit_s64_t ofs, oskit_seek_t whence,
		  oskit_u64_t *out_newpos)
{
	return OSKIT_ESPIPE;
}

static OSKIT_COMDECL
setsize(oskit_ttystream_t *si, oskit_u64_t new_size)
{
	return OSKIT_EINVAL;
}

static OSKIT_COMDECL
copyto(oskit_ttystream_t *s, oskit_stream_t *dst,
       oskit_u64_t size,
       oskit_u64_t *out_read,
       oskit_u64_t *out_written)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
commit(oskit_ttystream_t *si, oskit_u32_t commit_flags)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
revert(oskit_ttystream_t *si)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
lockregion(oskit_ttystream_t *si, oskit_u64_t offset,
			 oskit_u64_t size, oskit_u32_t lock_type)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
unlockregion(oskit_ttystream_t *si, oskit_u64_t offset,
			   oskit_u64_t size, oskit_u32_t lock_type)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
stat(oskit_ttystream_t *si, oskit_stream_stat_t *out_stat,
		  oskit_u32_t stat_flags)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
clone(oskit_ttystream_t *si, oskit_stream_t **out_stream)
{
	return OSKIT_E_NOTIMPL;
}

/* TTY Stream Ops */

static OSKIT_COMDECL
getattr(oskit_ttystream_t *si, struct oskit_termios *out_attr)
{
	/*
	 * The termios definitions common to the FreeBSD and OSKit interfaces
	 * have been defined to have identical values. Ain't that a peach.
	 */
	out_attr->iflag  = my_termios.c_iflag;
	out_attr->oflag  = my_termios.c_oflag;
	out_attr->cflag  = my_termios.c_cflag;
	out_attr->lflag  = my_termios.c_lflag;
	out_attr->ispeed = my_termios.c_ispeed;
	out_attr->ospeed = my_termios.c_ospeed;
	memcpy(out_attr->cc, my_termios.c_cc, NCCS);

	return 0;
}

static OSKIT_COMDECL
setattr(oskit_ttystream_t *si, int actions, const struct oskit_termios *attr)
{
	my_termios.c_iflag  = attr->iflag;
	my_termios.c_oflag  = attr->oflag;
	my_termios.c_cflag  = attr->cflag;
	my_termios.c_lflag  = attr->lflag;
	my_termios.c_ispeed = attr->ispeed;
	my_termios.c_ospeed = attr->ospeed;
	memcpy(my_termios.c_cc, attr->cc, NCCS);

	return 0;
}

/* ARGSUSED */
static OSKIT_COMDECL
sendbreak(oskit_ttystream_t *si, oskit_u32_t duration)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
drain(oskit_ttystream_t *si)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
flush(oskit_ttystream_t *si, int queue_selector)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
flow(oskit_ttystream_t *si, int action)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
getpgrp(oskit_ttystream_t *si, oskit_pid_t *out_pid)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
setpgrp(oskit_ttystream_t *si, oskit_pid_t new_pid)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
ttyname(oskit_ttystream_t *si, char **out_name)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
getsid(oskit_ttystream_t *si, oskit_pid_t *out_pid)
{
	return OSKIT_E_NOTIMPL;
}

static struct oskit_ttystream_ops ttystream_ops = {
	query, addref, release,
	read, write, seek, setsize, copyto,
	commit, revert, lockregion, unlockregion, stat, clone,
	getattr, 
	setattr, 
	sendbreak, 
	drain, 
	flush, 
	flow, 
	getpgrp, 
	setpgrp,
	ttyname, 
	getsid
};

static OSKIT_COMDECL
posix_stat(oskit_posixio_t *pio, struct oskit_stat *st)
{
	memset(st, 0, sizeof *st);
	st->ino = 1;
        st->mode = OSKIT_S_IFCHR | OSKIT_S_IRWXG | OSKIT_S_IRWXU | OSKIT_S_IRWXO;
	return 0;
}

static OSKIT_COMDECL
setstat(oskit_posixio_t *pio, oskit_u32_t mask, const struct oskit_stat *stats)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
pathconf(oskit_posixio_t *pio, oskit_s32_t option, oskit_s32_t *out_val)
{
	return OSKIT_E_NOTIMPL;
}

static struct oskit_posixio_ops posixio_ops = {
	query, addref, release,
	posix_stat, setstat, pathconf
};

/*
 * this should trigger applications who select on a console fd to
 * go into read->getchar or write->putchar immediately.
 */
static OSKIT_COMDECL
asyncio_poll(oskit_asyncio_t *f)
{
	return OSKIT_ASYNCIO_WRITABLE | OSKIT_ASYNCIO_READABLE;
}

/* add a listener */
static OSKIT_COMDECL
asyncio_add_listener(oskit_asyncio_t *f, struct oskit_listener *l,
	oskit_s32_t mask)
{
	return OSKIT_E_NOTIMPL;
}

/* remove a listener */
static OSKIT_COMDECL
asyncio_remove_listener(oskit_asyncio_t *f, struct oskit_listener *l0)
{
	return OSKIT_E_NOTIMPL;
}

/* how many bytes can be read */
static OSKIT_COMDECL
asyncio_readable(oskit_asyncio_t *f)
{
	return 0;
}

static struct oskit_asyncio_ops asyncio_ops = {
	query, addref, release,	/* will generate three leg. warnings */
	asyncio_poll,
	asyncio_add_listener,
	asyncio_remove_listener,
	asyncio_readable
};