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//extern intvprf(const char * fmt, va_list ap);
52
53bool gVerboseMode;
54bool gErrors;
55
56/* Kabyl: BooterLog */
57#define BOOTER_LOG_SIZE (128 * 1024)
58#define SAFE_LOG_SIZE 134
59
60char *msgbuf = 0;
61char *cursor = 0;
62
63struct putc_info {
64 char * str;
65 char * last_str;
66};
67
68void sputc(int c, struct putc_info * pi)
69{
70if (pi->last_str)
71if (pi->str == pi->last_str)
72{
73*(pi->str) = '\0';
74return;
75}
76*(pi->str)++ = c;
77}
78
79void initBooterLog(void)
80{
81msgbuf = malloc(BOOTER_LOG_SIZE);
82 if (!msgbuf) {
83 printf("Couldn't allocate buffer for booter log\n");
84 return;
85 }
86bzero(msgbuf, BOOTER_LOG_SIZE);
87cursor = msgbuf;
88
89}
90
91char *getConsoleMsg(void)
92{
93 return msgbuf;
94}
95char *getConsoleCursor(void)
96{
97 return cursor;
98}
99void setConsoleMsg(char *p)
100{
101 msgbuf = p;
102}
103void setConsoleCursor(char *p)
104{
105 cursor = p;
106}
107
108
109void msglog(const char * fmt, ...)
110{
111va_list ap;
112struct putc_info pi;
113
114if (!msgbuf)
115return;
116
117if (((cursor - msgbuf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))
118return;
119
120va_start(ap, fmt);
121pi.str = cursor;
122pi.last_str = 0;
123prf(fmt, ap, sputc, &pi);
124va_end(ap);
125cursor += strlen((char *)cursor);
126}
127
128void setupBooterLog(void)
129{
130if (!msgbuf)
131return;
132
133Node *node = DT__FindNode("/", false);
134if (node)
135DT__AddProperty(node, "boot-log", strlen((char *)msgbuf) + 1, msgbuf);
136}
137/* Kabyl: !BooterLog */
138
139
140/*
141 * write one character to console
142 */
143void putchar(int c)
144{
145if ( c == '\t' )
146{
147for (c = 0; c < 8; c++) putc(' ');
148return;
149}
150
151if ( c == '\n' )
152 {
153putc('\r');
154 }
155
156putc(c);
157}
158
159int getc()
160{
161 int c = bgetc();
162
163 if ((c & 0xff) == 0)
164 return c;
165 else
166return (c & 0xff);
167}
168
169// Read and echo a character from console. This doesn't echo backspace
170// since that screws up higher level handling
171
172int getchar()
173{
174register int c = getc();
175
176if ( c == '\r' ) c = '\n';
177
178if ( c >= ' ' && c < 0x7f) putchar(c);
179
180return (c);
181}
182
183int printf(const char * fmt, ...)
184{
185 va_list ap;
186va_start(ap, fmt);
187
188prf(fmt, ap, putchar, 0);
189
190{
191/* Kabyl: BooterLog */
192struct putc_info pi;
193
194if (!msgbuf)
195return 0;
196
197if (((cursor - msgbuf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))
198return 0;
199pi.str = cursor;
200pi.last_str = 0;
201prf(fmt, ap, sputc, &pi);
202cursor += strlen((char *)cursor);
203}
204
205va_end(ap);
206
207 return 0;
208}
209
210int verbose(const char * fmt, ...)
211{
212 va_list ap;
213
214va_start(ap, fmt);
215 if (gVerboseMode)
216 {
217prf(fmt, ap, putchar, 0);
218 }
219
220{
221/* Kabyl: BooterLog */
222struct putc_info pi;
223
224if (!msgbuf)
225return 0;
226
227if (((cursor - msgbuf) > (BOOTER_LOG_SIZE - SAFE_LOG_SIZE)))
228return 0;
229pi.str = cursor;
230pi.last_str = 0;
231prf(fmt, ap, sputc, &pi);
232cursor += strlen((char *)cursor);
233}
234
235 va_end(ap);
236
237 return(0);
238}
239
240int error(const char * fmt, ...)
241{
242 va_list ap;
243 gErrors = true;
244 va_start(ap, fmt);
245
246prf(fmt, ap, putchar, 0);
247
248va_end(ap);
249 return(0);
250}
251
252void stop(const char * fmt, ...)
253{
254va_list ap;
255
256printf("\n");
257va_start(ap, fmt);
258
259prf(fmt, ap, putchar, 0);
260
261va_end(ap);
262printf("\nThis is a non recoverable error! System HALTED!!!");
263halt();
264while (1);
265}
266
267/** Print a "Press a key to continue..." message and wait for a key press. */
268void pause(void)
269{
270 printf("Press a key to continue...");
271 getc();
272}
273
274char * newStringWithFormat(const char * fmt, ...)
275{
276 va_list ap;
277 struct putc_info pi;
278int len;
279char *str = NULL;
280
281 va_start(ap, fmt);
282len = prf(fmt, ap, 0, 0);
283if (len > 0)
284{
285str = newEmptyStringWithLength(len);
286if (str != NULL)
287{
288pi.last_str = 0;
289
290pi.str = str;
291
292prf(fmt, ap, sputc, &pi);
293*pi.str = '\0';
294}
295
296}
297 va_end(ap);
298
299return str;
300
301}

Archive Download this file

Revision: 1919