thermal: fix Mediatek thermal controller build
[linux/fpc-iii.git] / arch / powerpc / xmon / nonstdio.c
blobd00123421e0075c1a0d2e340961183a912af074b
1 /*
2 * Copyright (C) 1996-2005 Paul Mackerras.
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/udbg.h>
11 #include <asm/time.h>
12 #include "nonstdio.h"
14 static bool paginating, paginate_skipping;
15 static unsigned long paginate_lpp; /* Lines Per Page */
16 static unsigned long paginate_pos;
18 void xmon_start_pagination(void)
20 paginating = true;
21 paginate_skipping = false;
22 paginate_pos = 0;
25 void xmon_end_pagination(void)
27 paginating = false;
30 void xmon_set_pagination_lpp(unsigned long lpp)
32 paginate_lpp = lpp;
35 static int xmon_readchar(void)
37 if (udbg_getc)
38 return udbg_getc();
39 return -1;
42 static int xmon_write(const char *ptr, int nb)
44 int rv = 0;
45 const char *p = ptr, *q;
46 const char msg[] = "[Hit a key (a:all, q:truncate, any:next page)]";
48 if (nb <= 0)
49 return rv;
51 if (paginating && paginate_skipping)
52 return nb;
54 if (paginate_lpp) {
55 while (paginating && (q = strchr(p, '\n'))) {
56 rv += udbg_write(p, q - p + 1);
57 p = q + 1;
58 paginate_pos++;
60 if (paginate_pos >= paginate_lpp) {
61 udbg_write(msg, strlen(msg));
63 switch (xmon_readchar()) {
64 case 'a':
65 paginating = false;
66 break;
67 case 'q':
68 paginate_skipping = true;
69 break;
70 default:
71 /* nothing */
72 break;
75 paginate_pos = 0;
76 udbg_write("\r\n", 2);
78 if (paginate_skipping)
79 return nb;
84 return rv + udbg_write(p, nb - (p - ptr));
87 int xmon_putchar(int c)
89 char ch = c;
91 if (c == '\n')
92 xmon_putchar('\r');
93 return xmon_write(&ch, 1) == 1? c: -1;
96 static char line[256];
97 static char *lineptr;
98 static int lineleft;
100 static int xmon_getchar(void)
102 int c;
104 if (lineleft == 0) {
105 lineptr = line;
106 for (;;) {
107 c = xmon_readchar();
108 if (c == -1 || c == 4)
109 break;
110 if (c == '\r' || c == '\n') {
111 *lineptr++ = '\n';
112 xmon_putchar('\n');
113 break;
115 switch (c) {
116 case 0177:
117 case '\b':
118 if (lineptr > line) {
119 xmon_putchar('\b');
120 xmon_putchar(' ');
121 xmon_putchar('\b');
122 --lineptr;
124 break;
125 case 'U' & 0x1F:
126 while (lineptr > line) {
127 xmon_putchar('\b');
128 xmon_putchar(' ');
129 xmon_putchar('\b');
130 --lineptr;
132 break;
133 default:
134 if (lineptr >= &line[sizeof(line) - 1])
135 xmon_putchar('\a');
136 else {
137 xmon_putchar(c);
138 *lineptr++ = c;
142 lineleft = lineptr - line;
143 lineptr = line;
145 if (lineleft == 0)
146 return -1;
147 --lineleft;
148 return *lineptr++;
151 char *xmon_gets(char *str, int nb)
153 char *p;
154 int c;
156 for (p = str; p < str + nb - 1; ) {
157 c = xmon_getchar();
158 if (c == -1) {
159 if (p == str)
160 return NULL;
161 break;
163 *p++ = c;
164 if (c == '\n')
165 break;
167 *p = 0;
168 return str;
171 void xmon_printf(const char *format, ...)
173 va_list args;
174 static char xmon_outbuf[1024];
175 int rc, n;
177 va_start(args, format);
178 n = vsnprintf(xmon_outbuf, sizeof(xmon_outbuf), format, args);
179 va_end(args);
181 rc = xmon_write(xmon_outbuf, n);
183 if (n && rc == 0) {
184 /* No udbg hooks, fallback to printk() - dangerous */
185 printk("%s", xmon_outbuf);
189 void xmon_puts(const char *str)
191 xmon_write(str, strlen(str));