1 /*
2 * Copyright (C) Paul Mackerras 1997.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9 #include <stdarg.h>
10 #include "of1275.h"
11
12 int (*prom)(void *args);
13
14 phandle stdin;
15 phandle stdout;
16 phandle stderr;
17
18 void printk(char *fmt, ...);
19
20 extern void chrpboot(int a1, int a2, void *prom);
21 extern int strlen(const char *s);
22
23 void
start(int a1,int a2,void * promptr)24 start(int a1, int a2, void *promptr)
25 {
26 ofinit(promptr);
27 if (ofstdio(&stdin, &stdout, &stderr))
28 exit();
29 chrpboot(a1, a2, promptr);
30 for (;;)
31 exit();
32 }
33
34 int
putc(int c,void * f)35 putc(int c, void *f)
36 {
37 char ch = c;
38
39 if (c == '\n')
40 putc('\r', f);
41 return write(f, &ch, 1) == 1? c: -1;
42 }
43
44 int
putchar(int c)45 putchar(int c)
46 {
47 return putc(c, stdout);
48 }
49
50 int
fputs(char * str,void * f)51 fputs(char *str, void *f)
52 {
53 int n = strlen(str);
54
55 return write(f, str, n) == n? 0: -1;
56 }
57
58 int
readchar(void)59 readchar(void)
60 {
61 char ch;
62
63 for (;;) {
64 switch (read(stdin, &ch, 1)) {
65 case 1:
66 return ch;
67 case -1:
68 printk("read(stdin) returned -1\r\n");
69 return -1;
70 }
71 }
72 }
73
74 static char line[256];
75 static char *lineptr;
76 static int lineleft;
77
78 int
getchar(void)79 getchar(void)
80 {
81 int c;
82
83 if (lineleft == 0) {
84 lineptr = line;
85 for (;;) {
86 c = readchar();
87 if (c == -1 || c == 4)
88 break;
89 if (c == '\r' || c == '\n') {
90 *lineptr++ = '\n';
91 putchar('\n');
92 break;
93 }
94 switch (c) {
95 case 0177:
96 case '\b':
97 if (lineptr > line) {
98 putchar('\b');
99 putchar(' ');
100 putchar('\b');
101 --lineptr;
102 }
103 break;
104 case 'U' & 0x1F:
105 while (lineptr > line) {
106 putchar('\b');
107 putchar(' ');
108 putchar('\b');
109 --lineptr;
110 }
111 break;
112 default:
113 if (lineptr >= &line[sizeof(line) - 1])
114 putchar('\a');
115 else {
116 putchar(c);
117 *lineptr++ = c;
118 }
119 }
120 }
121 lineleft = lineptr - line;
122 lineptr = line;
123 }
124 if (lineleft == 0)
125 return -1;
126 --lineleft;
127 return *lineptr++;
128 }
129
130 extern int vsprintf(char *buf, const char *fmt, va_list args);
131 static char sprint_buf[1024];
132
133 void
printk(char * fmt,...)134 printk(char *fmt, ...)
135 {
136 va_list args;
137 int n;
138
139 va_start(args, fmt);
140 n = vsprintf(sprint_buf, fmt, args);
141 va_end(args);
142 write(stdout, sprint_buf, n);
143 }
144
145 int
printf(char * fmt,...)146 printf(char *fmt, ...)
147 {
148 va_list args;
149 int n;
150
151 va_start(args, fmt);
152 n = vsprintf(sprint_buf, fmt, args);
153 va_end(args);
154 write(stdout, sprint_buf, n);
155 return n;
156 }
157