]>
Commit | Line | Data |
---|---|---|
fca5dcd4 PM |
1 | /* |
2 | * Copyright (C) 1996-2005 Paul Mackerras. | |
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 <linux/string.h> | |
10 | #include <asm/time.h> | |
11 | #include "nonstdio.h" | |
12 | ||
13 | int xmon_putchar(int c) | |
14 | { | |
15 | char ch = c; | |
16 | ||
17 | if (c == '\n') | |
18 | xmon_putchar('\r'); | |
19 | return xmon_write(&ch, 1) == 1? c: -1; | |
20 | } | |
21 | ||
22 | static char line[256]; | |
23 | static char *lineptr; | |
24 | static int lineleft; | |
25 | ||
88c6d626 | 26 | static int xmon_getchar(void) |
fca5dcd4 PM |
27 | { |
28 | int c; | |
29 | ||
30 | if (lineleft == 0) { | |
31 | lineptr = line; | |
32 | for (;;) { | |
33 | c = xmon_readchar(); | |
34 | if (c == -1 || c == 4) | |
35 | break; | |
36 | if (c == '\r' || c == '\n') { | |
37 | *lineptr++ = '\n'; | |
38 | xmon_putchar('\n'); | |
39 | break; | |
40 | } | |
41 | switch (c) { | |
42 | case 0177: | |
43 | case '\b': | |
44 | if (lineptr > line) { | |
45 | xmon_putchar('\b'); | |
46 | xmon_putchar(' '); | |
47 | xmon_putchar('\b'); | |
48 | --lineptr; | |
49 | } | |
50 | break; | |
51 | case 'U' & 0x1F: | |
52 | while (lineptr > line) { | |
53 | xmon_putchar('\b'); | |
54 | xmon_putchar(' '); | |
55 | xmon_putchar('\b'); | |
56 | --lineptr; | |
57 | } | |
58 | break; | |
59 | default: | |
60 | if (lineptr >= &line[sizeof(line) - 1]) | |
61 | xmon_putchar('\a'); | |
62 | else { | |
63 | xmon_putchar(c); | |
64 | *lineptr++ = c; | |
65 | } | |
66 | } | |
67 | } | |
68 | lineleft = lineptr - line; | |
69 | lineptr = line; | |
70 | } | |
71 | if (lineleft == 0) | |
72 | return -1; | |
73 | --lineleft; | |
74 | return *lineptr++; | |
75 | } | |
76 | ||
77 | char *xmon_gets(char *str, int nb) | |
78 | { | |
79 | char *p; | |
80 | int c; | |
81 | ||
82 | for (p = str; p < str + nb - 1; ) { | |
83 | c = xmon_getchar(); | |
84 | if (c == -1) { | |
85 | if (p == str) | |
86 | return NULL; | |
87 | break; | |
88 | } | |
89 | *p++ = c; | |
90 | if (c == '\n') | |
91 | break; | |
92 | } | |
93 | *p = 0; | |
94 | return str; | |
95 | } | |
96 | ||
97 | void xmon_printf(const char *format, ...) | |
98 | { | |
99 | va_list args; | |
100 | int n; | |
101 | static char xmon_outbuf[1024]; | |
102 | ||
103 | va_start(args, format); | |
104 | n = vsnprintf(xmon_outbuf, sizeof(xmon_outbuf), format, args); | |
105 | va_end(args); | |
106 | xmon_write(xmon_outbuf, n); | |
107 | } | |
4d404edc IK |
108 | |
109 | void xmon_puts(const char *str) | |
110 | { | |
111 | xmon_write(str, strlen(str)); | |
112 | } |