Checkpoint: add printf() for debugging, remove query of E881 (seems
[profile/ivi/syslinux.git] / memdisk / conio.c
1 #ident "$Id$"
2 /* ----------------------------------------------------------------------- *
3  *   
4  *   Copyright 2001 H. Peter Anvin - All Rights Reserved
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9  *   Bostom MA 02111-1307, USA; either version 2 of the License, or
10  *   (at your option) any later version; incorporated herein by reference.
11  *
12  * ----------------------------------------------------------------------- */
13
14 /*
15  * conio.c
16  *
17  * Output to the screen
18  */
19
20 #include <stdint.h>
21 #include "conio.h"
22
23 int putchar(int ch)
24 {
25   if ( ch == '\n' ) {
26     /* \n -> \r\n */
27     asm volatile("movw $0x0e0d,%%ax ; "
28                  "movw $0x0007,%%bx ; "
29                  "int $0x10"
30                  ::: "eax", "ebx", "ecx", "edx",
31                  "esi", "edi", "ebp");
32   }
33
34   asm volatile("movw $0x0007,%%bx ; "
35                "int $0x10"
36                :: "a" ((uint16_t)(0x0e00|(ch&0xff)))
37                : "eax", "ebx", "ecx", "edx",
38                "esi", "edi", "ebp");
39
40   return ch;
41 }
42
43 int puts(const char *s)
44 {
45   int count = 0;
46
47   while ( *s ) {
48     putchar(*s);
49     count++;
50     s++;
51   }
52
53   return count;
54 }
55
56 /*
57  * Oh, it's a waste of space, but oh-so-yummy for debugging.  It's just
58  * initialization code anyway, so it doesn't take up space when we're
59  * actually running.  This version of printf() does not include 64-bit
60  * support.  "Live with it."
61  *
62  * Most of this code was shamelessly snarfed from the Linux kernel, then
63  * modified.
64  */
65
66 static inline int
67 isdigit(int ch)
68 {
69   return (ch >= '0') && (ch <= '9');
70 }
71
72 static int skip_atoi(const char **s)
73 {
74   int i=0;
75   
76   while (isdigit(**s))
77     i = i*10 + *((*s)++) - '0';
78   return i;
79 }
80
81 static int strnlen(const char *s, int maxlen)
82 {
83   const char *es = s;
84   while ( *es && maxlen ) {
85     es++; maxlen--;
86   }
87
88   return (es-s);
89 }
90
91 #define ZEROPAD 1               /* pad with zero */
92 #define SIGN    2               /* unsigned/signed long */
93 #define PLUS    4               /* show plus */
94 #define SPACE   8               /* space if plus */
95 #define LEFT    16              /* left justified */
96 #define SPECIAL 32              /* 0x */
97 #define LARGE   64              /* use 'ABCDEF' instead of 'abcdef' */
98
99 #define do_div(n,base) ({ \
100 int __res; \
101 __res = ((unsigned long) n) % (unsigned) base; \
102 n = ((unsigned long) n) / (unsigned) base; \
103 __res; })
104
105 static char * number(char * str, long num, int base, int size, int precision
106         ,int type)
107 {
108   char c,sign,tmp[66];
109   const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
110   int i;
111   
112   if (type & LARGE)
113     digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
114   if (type & LEFT)
115     type &= ~ZEROPAD;
116   if (base < 2 || base > 36)
117     return 0;
118   c = (type & ZEROPAD) ? '0' : ' ';
119   sign = 0;
120   if (type & SIGN) {
121     if (num < 0) {
122       sign = '-';
123       num = -num;
124       size--;
125     } else if (type & PLUS) {
126       sign = '+';
127       size--;
128     } else if (type & SPACE) {
129       sign = ' ';
130       size--;
131     }
132   }
133   if (type & SPECIAL) {
134     if (base == 16)
135       size -= 2;
136     else if (base == 8)
137       size--;
138   }
139   i = 0;
140   if (num == 0)
141     tmp[i++]='0';
142   else while (num != 0)
143     tmp[i++] = digits[do_div(num,base)];
144   if (i > precision)
145     precision = i;
146   size -= precision;
147   if (!(type&(ZEROPAD+LEFT)))
148     while(size-->0)
149       *str++ = ' ';
150   if (sign)
151     *str++ = sign;
152   if (type & SPECIAL) {
153     if (base==8)
154       *str++ = '0';
155     else if (base==16) {
156       *str++ = '0';
157       *str++ = digits[33];
158     }
159   }
160   if (!(type & LEFT))
161     while (size-- > 0)
162       *str++ = c;
163   while (i < precision--)
164     *str++ = '0';
165   while (i-- > 0)
166     *str++ = tmp[i];
167   while (size-- > 0)
168     *str++ = ' ';
169   return str;
170 }
171
172 /* Forward decl. needed for IP address printing stuff... */
173 int sprintf(char * buf, const char *fmt, ...);
174
175 int vsprintf(char *buf, const char *fmt, va_list args)
176 {
177   int len;
178   unsigned long num;
179   int i, base;
180   char * str;
181   const char *s;
182   
183   int flags;            /* flags to number() */
184   
185   int field_width;      /* width of output field */
186   int precision;                /* min. # of digits for integers; max
187                                    number of chars for from string */
188   int qualifier;                /* 'h', 'l', or 'L' for integer fields */
189   
190   for (str=buf ; *fmt ; ++fmt) {
191     if (*fmt != '%') {
192       *str++ = *fmt;
193       continue;
194     }
195     
196     /* process flags */
197     flags = 0;
198   repeat:
199     ++fmt;              /* this also skips first '%' */
200     switch (*fmt) {
201     case '-': flags |= LEFT; goto repeat;
202     case '+': flags |= PLUS; goto repeat;
203     case ' ': flags |= SPACE; goto repeat;
204     case '#': flags |= SPECIAL; goto repeat;
205     case '0': flags |= ZEROPAD; goto repeat;
206     }
207     
208     /* get field width */
209     field_width = -1;
210     if (isdigit(*fmt))
211       field_width = skip_atoi(&fmt);
212     else if (*fmt == '*') {
213       ++fmt;
214       /* it's the next argument */
215       field_width = va_arg(args, int);
216       if (field_width < 0) {
217         field_width = -field_width;
218         flags |= LEFT;
219       }
220     }
221     
222     /* get the precision */
223     precision = -1;
224     if (*fmt == '.') {
225       ++fmt;    
226       if (isdigit(*fmt))
227         precision = skip_atoi(&fmt);
228       else if (*fmt == '*') {
229         ++fmt;
230         /* it's the next argument */
231         precision = va_arg(args, int);
232       }
233       if (precision < 0)
234         precision = 0;
235     }
236     
237     /* get the conversion qualifier */
238     qualifier = -1;
239     if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
240       qualifier = *fmt;
241       ++fmt;
242     }
243     
244     /* default base */
245     base = 10;
246     
247     switch (*fmt) {
248     case 'c':
249       if (!(flags & LEFT))
250         while (--field_width > 0)
251           *str++ = ' ';
252       *str++ = (unsigned char) va_arg(args, int);
253       while (--field_width > 0)
254         *str++ = ' ';
255       continue;
256       
257     case 's':
258       s = va_arg(args, char *);
259       if (!s)
260         s = "(null)";
261       
262       len = strnlen(s, precision);
263       
264       if (!(flags & LEFT))
265         while (len < field_width--)
266           *str++ = ' ';
267       for (i = 0; i < len; ++i)
268         *str++ = *s++;
269       while (len < field_width--)
270         *str++ = ' ';
271       continue;
272       
273     case 'p':
274       if (field_width == -1) {
275         field_width = 2*sizeof(void *);
276         flags |= ZEROPAD;
277       }
278       str = number(str,
279                    (unsigned long) va_arg(args, void *), 16,
280                    field_width, precision, flags);
281       continue;
282       
283       
284     case 'n':
285       if (qualifier == 'l') {
286         long * ip = va_arg(args, long *);
287         *ip = (str - buf);
288       } else {
289         int * ip = va_arg(args, int *);
290         *ip = (str - buf);
291       }
292       continue;
293       
294     case '%':
295       *str++ = '%';
296       continue;
297       
298       /* integer number formats - set up the flags and "break" */
299     case 'o':
300       base = 8;
301       break;
302       
303     case 'X':
304       flags |= LARGE;
305     case 'x':
306       base = 16;
307       break;
308       
309     case 'd':
310     case 'i':
311       flags |= SIGN;
312     case 'u':
313       break;
314       
315     default:
316       *str++ = '%';
317       if (*fmt)
318         *str++ = *fmt;
319       else
320         --fmt;
321       continue;
322     }
323     if (qualifier == 'l')
324       num = va_arg(args, unsigned long);
325     else if (qualifier == 'h') {
326       num = (unsigned short) va_arg(args, int);
327       if (flags & SIGN)
328         num = (short) num;
329     } else if (flags & SIGN)
330       num = va_arg(args, int);
331     else
332       num = va_arg(args, unsigned int);
333     str = number(str, num, base, field_width, precision, flags);
334   }
335   *str = '\0';
336   return str-buf;
337 }
338
339 int sprintf(char * buf, const char *fmt, ...)
340 {
341   va_list args;
342   int i;
343   
344   va_start(args, fmt);
345   i=vsprintf(buf,fmt,args);
346   va_end(args);
347   return i;
348 }
349
350 int printf(const char *fmt, ...)
351 {
352   char printf_buf[1024];
353   va_list args;
354   int printed;
355
356   va_start(args, fmt);
357   printed = vsprintf(printf_buf, fmt, args);
358   va_end(args);
359
360   puts(printf_buf);
361
362   return printed;
363 }
364