400f26ac9d443bc49e7ee63e4ac4a14bf714e0d0
[mw/milkymist.git] / software / baselib / console.c
1 /*
2  * Milkymist VJ SoC (Software)
3  * Copyright (C) 2007, 2008, 2009 Sebastien Bourdeauducq
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3 of the License.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <libc.h>
19 #include <uart.h>
20 #include <console.h>
21
22 int puts(const char *s)
23 {
24         while(*s) {
25                 writechar(*s);
26                 s++;
27         }
28         writechar('\n');
29         return 1;
30 }
31
32 void putsnonl(const char *s)
33 {
34         while(*s) {
35                 writechar(*s);
36                 s++;
37         }
38 }
39
40 void readstr(char *s, int size)
41 {
42         char c;
43         int ptr;
44         
45         ptr = 0;
46         while(1) {
47                 c = readchar();
48                 switch(c) {
49                         case 0x7f:
50                         case 0x08:
51                                 if(ptr > 0) {
52                                         ptr--;
53                                         putsnonl("\x08 \x08");
54                                 }
55                                 break;
56                         case '\r':
57                         case '\n':
58                                 s[ptr] = 0x00;
59                                 putsnonl("\n");
60                                 return;
61                         default:
62                                 writechar(c);
63                                 s[ptr] = c;
64                                 ptr++;
65                                 break;
66                 }
67         }
68 }
69
70 int printf(const char *fmt, ...)
71 {
72         va_list args;
73         int len;
74         char outbuf[256];
75
76         va_start(args, fmt);
77         len = vscnprintf(outbuf, sizeof(outbuf), fmt, args);
78         va_end(args);
79         outbuf[len] = 0;
80         putsnonl(outbuf);
81
82         return len;
83 }