| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) Paul Mackerras 1997. | 
 | 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 <stdarg.h> | 
 | 10 | #include "of1275.h" | 
 | 11 |  | 
 | 12 | extern int strlen(const char *s); | 
 | 13 | extern void boot(int a1, int a2, void *prom); | 
 | 14 |  | 
 | 15 | phandle stdin; | 
 | 16 | phandle stdout; | 
 | 17 | phandle stderr; | 
 | 18 |  | 
 | 19 | void printk(char *fmt, ...); | 
 | 20 |  | 
 | 21 | void | 
 | 22 | start(int a1, int a2, void *promptr) | 
 | 23 | { | 
 | 24 |     ofinit(promptr); | 
 | 25 |     if (ofstdio(&stdin, &stdout, &stderr)) | 
 | 26 | 	exit(); | 
 | 27 |  | 
 | 28 |     boot(a1, a2, promptr); | 
 | 29 |     for (;;) | 
 | 30 | 	exit(); | 
 | 31 | } | 
 | 32 |  | 
 | 33 | int writestring(void *f, char *ptr, int nb) | 
 | 34 | { | 
 | 35 | 	int w = 0, i; | 
 | 36 | 	char *ret = "\r"; | 
 | 37 |  | 
 | 38 | 	for (i = 0; i < nb; ++i) { | 
 | 39 | 		if (ptr[i] == '\n') { | 
 | 40 | 			if (i > w) { | 
 | 41 | 				write(f, ptr + w, i - w); | 
 | 42 | 				w = i; | 
 | 43 | 			} | 
 | 44 | 			write(f, ret, 1); | 
 | 45 | 		} | 
 | 46 | 	} | 
 | 47 | 	if (w < nb) | 
 | 48 | 		write(f, ptr + w, nb - w); | 
 | 49 | 	return nb; | 
 | 50 | } | 
 | 51 |  | 
 | 52 | int | 
 | 53 | putc(int c, void *f) | 
 | 54 | { | 
 | 55 |     char ch = c; | 
 | 56 |  | 
 | 57 |     return writestring(f, &ch, 1) == 1? c: -1; | 
 | 58 | } | 
 | 59 |  | 
 | 60 | int | 
 | 61 | putchar(int c) | 
 | 62 | { | 
 | 63 |     return putc(c, stdout); | 
 | 64 | } | 
 | 65 |  | 
 | 66 | int | 
 | 67 | fputs(char *str, void *f) | 
 | 68 | { | 
 | 69 |     int n = strlen(str); | 
 | 70 |  | 
 | 71 |     return writestring(f, str, n) == n? 0: -1; | 
 | 72 | } | 
 | 73 |  | 
 | 74 | int | 
 | 75 | readchar(void) | 
 | 76 | { | 
 | 77 |     char ch; | 
 | 78 |  | 
 | 79 |     for (;;) { | 
 | 80 | 	switch (read(stdin, &ch, 1)) { | 
 | 81 | 	case 1: | 
 | 82 | 	    return ch; | 
 | 83 | 	case -1: | 
 | 84 | 	    printk("read(stdin) returned -1\n"); | 
 | 85 | 	    return -1; | 
 | 86 | 	} | 
 | 87 |     } | 
 | 88 | } | 
 | 89 |  | 
 | 90 | static char line[256]; | 
 | 91 | static char *lineptr; | 
 | 92 | static int lineleft; | 
 | 93 |  | 
 | 94 | int | 
 | 95 | getchar(void) | 
 | 96 | { | 
 | 97 |     int c; | 
 | 98 |  | 
 | 99 |     if (lineleft == 0) { | 
 | 100 | 	lineptr = line; | 
 | 101 | 	for (;;) { | 
 | 102 | 	    c = readchar(); | 
 | 103 | 	    if (c == -1 || c == 4) | 
 | 104 | 		break; | 
 | 105 | 	    if (c == '\r' || c == '\n') { | 
 | 106 | 		*lineptr++ = '\n'; | 
 | 107 | 		putchar('\n'); | 
 | 108 | 		break; | 
 | 109 | 	    } | 
 | 110 | 	    switch (c) { | 
 | 111 | 	    case 0177: | 
 | 112 | 	    case '\b': | 
 | 113 | 		if (lineptr > line) { | 
 | 114 | 		    putchar('\b'); | 
 | 115 | 		    putchar(' '); | 
 | 116 | 		    putchar('\b'); | 
 | 117 | 		    --lineptr; | 
 | 118 | 		} | 
 | 119 | 		break; | 
 | 120 | 	    case 'U' & 0x1F: | 
 | 121 | 		while (lineptr > line) { | 
 | 122 | 		    putchar('\b'); | 
 | 123 | 		    putchar(' '); | 
 | 124 | 		    putchar('\b'); | 
 | 125 | 		    --lineptr; | 
 | 126 | 		} | 
 | 127 | 		break; | 
 | 128 | 	    default: | 
 | 129 | 		if (lineptr >= &line[sizeof(line) - 1]) | 
 | 130 | 		    putchar('\a'); | 
 | 131 | 		else { | 
 | 132 | 		    putchar(c); | 
 | 133 | 		    *lineptr++ = c; | 
 | 134 | 		} | 
 | 135 | 	    } | 
 | 136 | 	} | 
 | 137 | 	lineleft = lineptr - line; | 
 | 138 | 	lineptr = line; | 
 | 139 |     } | 
 | 140 |     if (lineleft == 0) | 
 | 141 | 	return -1; | 
 | 142 |     --lineleft; | 
 | 143 |     return *lineptr++; | 
 | 144 | } | 
 | 145 |  | 
 | 146 | extern int vsprintf(char *buf, const char *fmt, va_list args); | 
 | 147 | static char sprint_buf[1024]; | 
 | 148 |  | 
 | 149 | void | 
 | 150 | printk(char *fmt, ...) | 
 | 151 | { | 
 | 152 | 	va_list args; | 
 | 153 | 	int n; | 
 | 154 |  | 
 | 155 | 	va_start(args, fmt); | 
 | 156 | 	n = vsprintf(sprint_buf, fmt, args); | 
 | 157 | 	va_end(args); | 
 | 158 | 	writestring(stdout, sprint_buf, n); | 
 | 159 | } | 
 | 160 |  | 
 | 161 | int | 
 | 162 | printf(char *fmt, ...) | 
 | 163 | { | 
 | 164 | 	va_list args; | 
 | 165 | 	int n; | 
 | 166 |  | 
 | 167 | 	va_start(args, fmt); | 
 | 168 | 	n = vsprintf(sprint_buf, fmt, args); | 
 | 169 | 	va_end(args); | 
 | 170 | 	writestring(stdout, sprint_buf, n); | 
 | 171 | 	return n; | 
 | 172 | } |