Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/console.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
30 */
31
32/*
33 * INTEL CORPORATION PROPRIETARY INFORMATION
34 *
35 *This software is supplied under the terms of a license agreement or
36 *nondisclosure agreement with Intel Corporation and may not be copied
37 *nor disclosed except in accordance with the terms of that agreement.
38 *
39 *Copyright 1988, 1989 Intel Corporation
40 */
41
42/*
43 * Copyright 1993 NeXT, Inc.
44 * All rights reserved.
45 */
46
47#include "libsaio.h"
48#include "bootstruct.h"
49#include "platform.h"
50
51/* Kabyl: BooterLog */
52#define BOOTER_LOG_SIZE (128 * 1024)
53#define SAFE_LOG_SIZE 134
54
55struct LOG {
56 char *buf;
57 char *cursor;
58};
59typedef struct LOG LOG;
60static LOG booterlog;
61
62struct putc_info {
63 char * str;
64 char * last_str;
65};
66
67void sputc(int c, struct putc_info * pi)
68{
69if (pi->last_str)
70if (pi->str == pi->last_str)
71{
72*(pi->str) = '\0';
73return;
74}
75*(pi->str)++ = c;
76}
77
78void initBooterLog(void)
79{
80booterlog.buf = malloc(BOOTER_LOG_SIZE);
81 if (!booterlog.buf) {
82 printf("Couldn't allocate buffer for booter log\n");
83 booterlog.cursor = 0;
84 booterlog.buf = 0;
85 return;
86 }
87bzero(booterlog.buf, BOOTER_LOG_SIZE);
88booterlog.cursor = booterlog.buf;
89
90}
91
92char *getConsoleMsg(void)
93{
94 return booterlog.buf;
95}
96char *getConsoleCursor(void)
97{
98 return booterlog.buf;
99}
100void setConsoleMsg(char *p)
101{
102 booterlog.buf = p;
103}
104void setConsoleCursor(char *p)
105{
106 booterlog.cursor = p;
107}
108
109void msglog(const char * fmt, ...)
110{
111va_list ap;
112struct putc_info pi;
113
114if (!booterlog.buf)
115return;
116
117if (((booterlog.cursor - booterlog.buf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))
118return;
119
120va_start(ap, fmt);
121pi.str = booterlog.cursor;
122pi.last_str = 0;
123prf(fmt, ap, sputc, &pi);
124va_end(ap);
125booterlog.cursor += strlen((char *)booterlog.cursor);
126}
127
128void setupBooterLog(void)
129{
130if (!booterlog.buf)
131return;
132
133Node *node = DT__FindNode("/", false);
134if (node)
135DT__AddProperty(node, "boot-log", strlen((char *)booterlog.buf) + 1, booterlog.buf);
136}
137
138/* Kabyl: !BooterLog */
139
140
141/*
142 * write one character to console
143 */
144void putchar(int c)
145{
146if ( c == '\t' )
147{
148for (c = 0; c < 8; c++) putc(' ');
149return;
150}
151
152if ( c == '\n' )
153 {
154putc('\r');
155 }
156
157putc(c);
158}
159
160int getc()
161{
162 int c = bgetc();
163
164 if ((c & 0xff) == 0)
165 return c;
166 else
167return (c & 0xff);
168}
169
170// Read and echo a character from console. This doesn't echo backspace
171// since that screws up higher level handling
172
173int getchar()
174{
175register int c = getc();
176
177if ( c == '\r' ) c = '\n';
178
179if ( c >= ' ' && c < 0x7f) putchar(c);
180
181return (c);
182}
183
184int printf(const char * fmt, ...)
185{
186 va_list ap;
187va_start(ap, fmt);
188
189prf(fmt, ap, putchar, 0);
190
191{
192/* Kabyl: BooterLog */
193struct putc_info pi;
194
195if (!booterlog.buf)
196return 0;
197
198if (((booterlog.cursor - booterlog.buf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))
199return 0;
200pi.str = booterlog.cursor;
201pi.last_str = 0;
202prf(fmt, ap, sputc, &pi);
203booterlog.cursor += strlen((char *)booterlog.cursor);
204}
205
206va_end(ap);
207
208 return 0;
209}
210
211int verbose(const char * fmt, ...)
212{
213 va_list ap;
214
215va_start(ap, fmt);
216 if (get_env(envgVerboseMode))
217 {
218prf(fmt, ap, putchar, 0);
219 }
220
221{
222/* Kabyl: BooterLog */
223struct putc_info pi;
224
225if (!booterlog.buf)
226return 0;
227
228if (((booterlog.cursor - booterlog.buf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))
229return 0;
230pi.str = booterlog.cursor;
231pi.last_str = 0;
232prf(fmt, ap, sputc, &pi);
233booterlog.cursor += strlen((char *)booterlog.cursor);
234}
235
236 va_end(ap);
237
238 return(0);
239}
240
241int error(const char * fmt, ...)
242{
243 va_list ap;
244 struct putc_info pi;
245int len;
246char *str = NULL;
247
248 va_start(ap, fmt);
249len = prf(fmt, ap, 0, 0);
250if (len > 0)
251{
252str = newEmptyStringWithLength(len);
253if (str != NULL)
254{
255pi.last_str = 0;
256
257pi.str = str;
258
259prf(fmt, ap, sputc, &pi);
260*pi.str = '\0';
261}
262
263}
264 va_end(ap);
265
266set_env_copy(envConsoleErr, str, len);
267free(str);
268
269 return(0);
270}
271
272void stop(const char * fmt, ...)
273{
274va_list ap;
275
276printf("\n");
277va_start(ap, fmt);
278
279prf(fmt, ap, putchar, 0);
280
281va_end(ap);
282printf("\nThis is a non recoverable error! System HALTED!!!");
283halt();
284while (1);
285}
286
287/** Print a "Press a key to continue..." message and wait for a key press. */
288void pause(void)
289{
290 printf("Press a key to continue...");
291 getc();
292}
293
294char * newStringWithFormat(const char * fmt, ...)
295{
296 va_list ap;
297 struct putc_info pi;
298int len;
299char *str = NULL;
300
301 va_start(ap, fmt);
302len = prf(fmt, ap, 0, 0);
303if (len > 0)
304{
305str = newEmptyStringWithLength(len);
306if (str != NULL)
307{
308pi.last_str = 0;
309
310pi.str = str;
311
312prf(fmt, ap, sputc, &pi);
313*pi.str = '\0';
314}
315
316}
317 va_end(ap);
318
319return str;
320
321}

Archive Download this file

Revision: 1972