-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint.c
More file actions
executable file
·198 lines (176 loc) · 3.69 KB
/
print.c
File metadata and controls
executable file
·198 lines (176 loc) · 3.69 KB
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
//#include "print.h"
#include "stdarg.h"
#if 0
#define args_list char *
#define _arg_stack_size(type) \
(((sizeof(type)-1)/sizeof(int)+1)*sizeof(int))
// redefine for 64 bit
#define args_start(ap, fmt) do { \
ap = (char *)((char *)&fmt + _arg_stack_size(&fmt)); \
} while (0)
#define args_end(ap)
#define args_next(ap, type) (((type *)(ap+=_arg_stack_size(type)))[-1])
#endif
#define args_list va_list
#define args_start va_start
#define args_end va_end
#define args_next va_arg
static char buf[1024] = {-1};
static int ptr = -1;
void console_write(char *);
/* valid base: 2, 8, 10 */
static int parse_num(unsigned int value, unsigned int base, int width)
{
if (!width) width=10;
unsigned int n = value / base;
int r = value % base;
if (r < 0) {
r += base;
--n;
}
if ((value >= base) && (--width >= 0))
width = parse_num(n, base, width);
buf[ptr++] = "0123456789"[r];
return width;
}
static int parse_hex(unsigned int value, int width)
{
if (!width) width= 8;
int i = width-2;
int set = 0;
buf[ptr++] = '0';
buf[ptr++] = 'x';
#if 0
while (i-- > 0) {
if (!set && !((value>>(i * 4)) & 0xf))
continue;
set = 1;
buf[ptr++] = "0123456789abcdef"[(value>>(i*4))&0xf];
}
#endif
if (!value) {
buf[ptr++] = '0';
return --i;
}
while (i-- > 0)
buf[ptr++] = "0123456789abcdef"[(value>>(i*4))&0xf];
return i;
}
int get_log_level(const char **pbuf)
{
char *p;
int log_level = 0;
if(*p) {
p = *(char **)pbuf;
if( p[0] == '<' && p[1] >= '0' && p[1] <= '7' && p[2] == '>') {
log_level = p[1] - '0';
*pbuf += 3;
}
}
return log_level;
}
#define is_digit(c) ((c) >= '0' && (c) <= '9')
static int skip_atoi(const char *s, int *j)
{
int i=0;
// print ("in skip \n");
// print_num(&s[*j], 10);
while (is_digit(s[*j])){
// putchar(s[*j]);
i = i*10 + s[*j] - '0';
*j = *j + 1;
}
return i;
}
/* %s, %c, %x, %d, %% */
//void
char * printk(const char *fmt, ...)
{
int i = 0;
char *s;
int log_level;
#if 0
/* must be the same size as enum KP_LEVEL */
/* struct KPC_STRUCT {
COLOUR fg;
COLOUR bg;
} KPL[] = {
{BRIGHT_WHITE, BLACK},
{YELLOW, RED},
};
*/
#endif
//log_level = get_log_level(&fmt);
args_list args;
args_start(args, fmt);
ptr = 0;
for (; fmt[i]; ++i) {
if ((fmt[i]!='%') && (fmt[i]!='\\')) {
buf[ptr++] = fmt[i];
continue;
} else if (fmt[i] == '\\') {
/* \a \b \t \n \v \f \r \\ */
switch (fmt[++i]) {
case 'a': buf[ptr++] = '\a'; break;
case 'b': buf[ptr++] = '\b'; break;
case 't': buf[ptr++] = '\t'; break;
case 'n': buf[ptr++] = '\n'; break;
case 'r': buf[ptr++] = '\r'; break;
case '\\':buf[ptr++] = '\\'; break;
}
continue;
}
i++;
int field_width = 0;
int fw = 0;
while (is_digit(fmt[i]))
fw = 10 * fw + fmt[i++] - '0';
if (fw > 0)
field_width = fw;
/* fmt[i] == '%' */
switch (fmt[i]) {
case 's':
s = (char *)args_next(args, char *);
if (!field_width) {
while (*s)
buf[ptr++] = *s++;
}
else {
while (*s && field_width--){
buf[ptr++] = *s++;
}
while(field_width-- > 0)
buf[ptr++] = ' ';
}
break;
case 'c':
/* why is int?? */
buf[ptr++] = (char)args_next(args, int);
break;
case 'x':
field_width = parse_hex((unsigned long)args_next(args, unsigned long), field_width);
while(field_width-- > 0)
buf[ptr++] = ' ';
break;
case 'd':
field_width=parse_num((unsigned int)args_next(args, unsigned int), 10, field_width);
while(field_width-- > 0)
buf[ptr++] = ' ';
break;
case '%':
buf[ptr++] = '%';
break;
default:
buf[ptr++] = fmt[i];
break;
}
}
buf[ptr] = '\0';
args_end(args);
console_write(buf);
#if 0
for (i=0; i<ptr; ++i)
print_c(buf[i], KPL[kl].fg, KPL[kl].bg);
#endif
return buf;
}