-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdio.cpp
272 lines (229 loc) · 6.02 KB
/
stdio.cpp
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
#include "stdio.h"
#include "string.h"
#include "console.h"
// Because we only have kernel, just crash if write to stdin or read from stdout
static FILE file_stdin = { nullptr, Console::getc };
static FILE file_stdout = { Console::putc, nullptr };
static FILE file_stderr = { UART::putc, nullptr };
FILE * const stdin = &file_stdin;
FILE * const stdout = &file_stdout;
FILE * const stderr = &file_stderr;
int printf(const char *format, ...)
{
va_list arg;
int ret;
va_start(arg, format);
ret = vfprintf(stdout, format, arg);
va_end(arg);
return ret;
}
int fprintf(FILE *stream, const char *format, ...)
{
va_list arg;
int ret;
va_start(arg, format);
ret = vfprintf(stream, format, arg);
va_end(arg);
return ret;
}
const int buflen = 1024;
static char buf[buflen];
char * readline(const char *prompt)
{
if (prompt)
fputs(prompt, stdout);
int len = 0;
while (true) {
char ch = getchar();
switch (ch) {
case '\n':
buf[len] = '\0';
return buf;
case '\b':
if (len > 0)
--len;
break;
default:
buf[len++] = ch;
}
}
}
/* vfprintf part, mostly copied from JOS */
const char digits[] = "0123456789abcdef";
// Print dec number, using software division.
static int printnum_d(FILE *s, unsigned long long num, int width, int padc)
{
int ret = 0;
if (num >= 10) {
ret += printnum_d(s, num / 10, width - 1, padc);
} else {
ret += width;
while (--width > 0)
s->putc(padc);
}
s->putc(digits[num % 10]);
return ret + 1;
}
// Print oct or hex number, using shift.
static int printnum_ox(FILE *s, unsigned long long num, unsigned log_base, int width, int padc)
{
int ret = 0;
// make sure shift operation generates a unsigned long long
unsigned long long tmp = 1;
tmp <<= log_base;
if (num >= tmp) {
ret += printnum_ox(s, num >> log_base, log_base, width - 1, padc);
} else {
ret += width;
while (--width > 0)
s->putc(padc);
}
s->putc(digits[num & (tmp - 1)]);
return ret + 1;
}
static unsigned long long getuint(va_list *ap, int lflag)
{
if (lflag >= 2)
return va_arg(*ap, unsigned long long);
else if (lflag)
return va_arg(*ap, unsigned long);
else
return va_arg(*ap, unsigned int);
}
static long long getint(va_list *ap, int lflag)
{
if (lflag >= 2)
return va_arg(*ap, long long);
else if (lflag)
return va_arg(*ap, long);
else
return va_arg(*ap, int);
}
int vfprintf(FILE *s, const char *fmt, va_list ap)
{
int ret = 0;
char ch, padc;
int width, precision, lflag, altflag, log_base;
unsigned long long num;
const char *p;
while (true) {
while ((ch = *fmt++) != '%') {
if (ch == '\0')
return ret;
s->putc(ch);
++ret;
}
padc = ' ';
width = -1;
precision = -1;
lflag = 0;
altflag = 0;
reswitch:
switch (ch = *fmt++) {
case '-':
padc = '-';
goto reswitch;
case '0':
padc = '0';
goto reswitch;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
for (precision = 0; ; ++fmt) {
precision = precision * 10 + ch - '0';
ch = *fmt;
if (ch < '0' || ch > '9')
break;
}
goto process_precision;
case '*':
precision = va_arg(ap, int);
goto process_precision;
case '.':
if (width < 0)
width = 0;
goto reswitch;
case '#':
altflag = 1;
goto reswitch;
process_precision:
if (width < 0) {
width = precision;
precision = -1;
}
goto reswitch;
case 'l':
lflag++;
goto reswitch;
case 'c':
s->putc(va_arg(ap, int));
++ret;
break;
//case 'e':
case 's':
if ((p = va_arg(ap, char *)) == nullptr)
p = "(null)";
if (width > 0 && padc != '-') {
width -= strnlen(p, precision);
ret += width;
for (; width > 0; --width)
s->putc(padc);
}
for (; (ch = *p++) != '\0' && (precision < 0 || --precision >= 0); --width) {
if (altflag && (ch < ' ' || ch > '~'))
s->putc('?');
else
s->putc(ch);
++ret;
}
ret += width;
for (; width > 0; --width)
s->putc(' ');
break;
case 'd':
num = getint(&ap, lflag);
if ((long long) num < 0) {
s->putc('-');
++ret;
num = -(long long) num;
}
goto dec_number;
case 'u':
num = getuint(&ap, lflag);
dec_number:
printnum_d(s, num, width, padc);
break;
case 'o':
num = getuint(&ap, lflag);
log_base = 3; // log2(8)
goto number;
case 'p':
s->putc('0');
s->putc('x');
ret += 2;
num = (unsigned long long) (uintptr_t) va_arg(ap, void *);
log_base = 4; // log2(16)
goto number;
case 'x':
num = getuint(&ap, lflag);
log_base = 4; // log2(16)
number:
printnum_ox(s, num, log_base, width, padc);
break;
case '%':
s->putc(ch);
++ret;
break;
default:
s->putc('%');
for (fmt--; fmt[-1] != '%'; fmt--) { }
break;
}
}
}