Merge tag 'linux-kselftest-kunit-fixes-5.11-rc3' of git://git.kernel.org/pub/scm...
[linux/fpc-iii.git] / arch / powerpc / xmon / nonstdio.c
blob9b0d85bff021e9cea00fc1a26fa8a5f6949b43c3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 1996-2005 Paul Mackerras.
4 */
5 #include <linux/string.h>
6 #include <asm/udbg.h>
7 #include <asm/time.h>
8 #include "nonstdio.h"
10 static bool paginating, paginate_skipping;
11 static unsigned long paginate_lpp; /* Lines Per Page */
12 static unsigned long paginate_pos;
14 void xmon_start_pagination(void)
16 paginating = true;
17 paginate_skipping = false;
18 paginate_pos = 0;
21 void xmon_end_pagination(void)
23 paginating = false;
26 void xmon_set_pagination_lpp(unsigned long lpp)
28 paginate_lpp = lpp;
31 static int xmon_readchar(void)
33 if (udbg_getc)
34 return udbg_getc();
35 return -1;
38 static int xmon_write(const char *ptr, int nb)
40 int rv = 0;
41 const char *p = ptr, *q;
42 const char msg[] = "[Hit a key (a:all, q:truncate, any:next page)]";
44 if (nb <= 0)
45 return rv;
47 if (paginating && paginate_skipping)
48 return nb;
50 if (paginate_lpp) {
51 while (paginating && (q = strchr(p, '\n'))) {
52 rv += udbg_write(p, q - p + 1);
53 p = q + 1;
54 paginate_pos++;
56 if (paginate_pos >= paginate_lpp) {
57 udbg_write(msg, strlen(msg));
59 switch (xmon_readchar()) {
60 case 'a':
61 paginating = false;
62 break;
63 case 'q':
64 paginate_skipping = true;
65 break;
66 default:
67 /* nothing */
68 break;
71 paginate_pos = 0;
72 udbg_write("\r\n", 2);
74 if (paginate_skipping)
75 return nb;
80 return rv + udbg_write(p, nb - (p - ptr));
83 int xmon_putchar(int c)
85 char ch = c;
87 if (c == '\n')
88 xmon_putchar('\r');
89 return xmon_write(&ch, 1) == 1? c: -1;
92 static char line[256];
93 static char *lineptr;
94 static int lineleft;
96 static int xmon_getchar(void)
98 int c;
100 if (lineleft == 0) {
101 lineptr = line;
102 for (;;) {
103 c = xmon_readchar();
104 if (c == -1 || c == 4)
105 break;
106 if (c == '\r' || c == '\n') {
107 *lineptr++ = '\n';
108 xmon_putchar('\n');
109 break;
111 switch (c) {
112 case 0177:
113 case '\b':
114 if (lineptr > line) {
115 xmon_putchar('\b');
116 xmon_putchar(' ');
117 xmon_putchar('\b');
118 --lineptr;
120 break;
121 case 'U' & 0x1F:
122 while (lineptr > line) {
123 xmon_putchar('\b');
124 xmon_putchar(' ');
125 xmon_putchar('\b');
126 --lineptr;
128 break;
129 default:
130 if (lineptr >= &line[sizeof(line) - 1])
131 xmon_putchar('\a');
132 else {
133 xmon_putchar(c);
134 *lineptr++ = c;
138 lineleft = lineptr - line;
139 lineptr = line;
141 if (lineleft == 0)
142 return -1;
143 --lineleft;
144 return *lineptr++;
147 char *xmon_gets(char *str, int nb)
149 char *p;
150 int c;
152 for (p = str; p < str + nb - 1; ) {
153 c = xmon_getchar();
154 if (c == -1) {
155 if (p == str)
156 return NULL;
157 break;
159 *p++ = c;
160 if (c == '\n')
161 break;
163 *p = 0;
164 return str;
167 void xmon_printf(const char *format, ...)
169 va_list args;
170 static char xmon_outbuf[1024];
171 int rc, n;
173 va_start(args, format);
174 n = vsnprintf(xmon_outbuf, sizeof(xmon_outbuf), format, args);
175 va_end(args);
177 rc = xmon_write(xmon_outbuf, n);
179 if (n && rc == 0) {
180 /* No udbg hooks, fallback to printk() - dangerous */
181 pr_cont("%s", xmon_outbuf);
185 void xmon_puts(const char *str)
187 xmon_write(str, strlen(str));