openblt

a hobby OS from the late 90s
git clone http://frotz.net/git/openblt.git
Log | Files | Refs | LICENSE

console.c (1503B)


      1 /* Copyright 2000, Brian J. Swetland. All rights reserved.
      2 ** Distributed under the terms of the OpenBLT License
      3 */
      4 
      5 #include <string.h>
      6 #include <blt/namer.h>
      7 #include <blt/syscall.h>
      8 #include <blt/libsyms.h>
      9 
     10 static int __libc_console_local_port = -1;
     11 static int __libc_console_out_port = -1;
     12 
     13 /* 0 = register, 1 = send, 2 = recv */
     14 
     15 int __libc_bind_console (void) 
     16 {	
     17 	if(__libc_console_local_port < 0) {
     18 		__libc_console_out_port = namer_find("console",1);
     19 		__libc_console_local_port = port_create(0, "console_io");
     20 		
     21 		if(port_send(__libc_console_local_port,
     22 					 __libc_console_out_port,
     23 					 NULL, 0, 0) == 0) {
     24 			return 0;
     25 		}
     26 		
     27 		port_destroy(__libc_console_local_port);
     28 		__libc_console_local_port = -1;
     29 	}
     30 	return -1;
     31 }
     32 
     33 size_t __libc_write_console(const void *buf, size_t len)
     34 {
     35 	const char *x = (const char *) buf;
     36 	ssize_t l = len;
     37 	
     38 	while(len > 0){
     39 		if(len > 255) {
     40 			port_send(__libc_console_local_port, __libc_console_out_port,
     41 					  (void*) x, 255, 1);
     42 			len -= 255;
     43 			x += 255;
     44 		} else {
     45 			port_send(__libc_console_local_port, __libc_console_out_port,
     46 					  (void*) x, len, 1);
     47 			len = 0;
     48 		}		  
     49 	}
     50 	return l;
     51 }
     52 
     53 ssize_t __libc_read_console(void *buf, ssize_t len)
     54 {
     55 	char *x = (char *) buf;
     56 	ssize_t r, t;
     57 	
     58 	t = 0;
     59 	while(len) {
     60 		r = port_recv(__libc_console_local_port, NULL, x, 1, NULL);
     61 		if(r < 0) return 0;
     62 		x++;
     63 		t++;
     64 		len--;
     65 	}
     66 	
     67 	return t;	
     68 }
     69 
     70 void grab_console(void)
     71 {
     72 	port_send(__libc_console_local_port, __libc_console_out_port,
     73 			  NULL, 0, 0);
     74 }