fix typo in comment
[fw/altos] / src / ao_stdio.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 /*
21  * Basic I/O functions to support SDCC stdio package
22  */
23
24 #define AO_NUM_STDIOS   2
25
26 static __xdata struct ao_stdio stdios[AO_NUM_STDIOS];
27 static __data int8_t ao_cur_stdio;
28 static __data int8_t ao_num_stdios;
29
30 void
31 putchar(char c)
32 {
33         if (c == '\n')
34                 (*stdios[ao_cur_stdio].putchar)('\r');
35         (*stdios[ao_cur_stdio].putchar)(c);
36 }
37
38 void
39 flush(void)
40 {
41         stdios[ao_cur_stdio].flush();
42 }
43
44 __xdata uint8_t ao_stdin_ready;
45
46 char
47 getchar(void) __reentrant
48 {
49         char c;
50         int8_t stdio = ao_cur_stdio;
51
52         for (;;) {
53                 c = stdios[stdio].pollchar();
54                 if (c != AO_READ_AGAIN)
55                         break;
56                 if (++stdio == ao_num_stdios)
57                         stdio = 0;
58                 if (stdio == ao_cur_stdio)
59                         ao_sleep(&ao_stdin_ready);
60         }
61         ao_cur_stdio = stdio;
62         return c;
63 }
64
65 void
66 ao_add_stdio(char (*pollchar)(void),
67              void (*putchar)(char),
68              void (*flush)(void))
69 {
70         if (ao_num_stdios == AO_NUM_STDIOS)
71                 ao_panic(AO_PANIC_STDIO);
72         stdios[ao_num_stdios].pollchar = pollchar;
73         stdios[ao_num_stdios].putchar = putchar;
74         stdios[ao_num_stdios].flush = flush;
75         ao_num_stdios++;
76 }