home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / os / kludge03.tz / kludge03 / mk74 / user / libmach / printf.c < prev    next >
C/C++ Source or Header  |  1992-02-26  |  3KB  |  168 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
  4.  * All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify and distribute this software and its
  7.  * documentation is hereby granted, provided that both the copyright
  8.  * notice and this permission notice appear in all copies of the
  9.  * software, derivative works or modified versions, and any portions
  10.  * thereof, and that both notices appear in supporting documentation.
  11.  * 
  12.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15.  * 
  16.  * Carnegie Mellon requests users of this software to return to
  17.  * 
  18.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  19.  *  School of Computer Science
  20.  *  Carnegie Mellon University
  21.  *  Pittsburgh PA 15213-3890
  22.  * 
  23.  * any improvements or extensions that they make and grant Carnegie Mellon
  24.  * the rights to redistribute these changes.
  25.  */
  26. /*
  27.  * HISTORY
  28.  * $Log:    printf.c,v $
  29.  * Revision 2.5  92/02/19  15:10:55  elf
  30.  *     Created.
  31.  *     [92/02/11            rpd]
  32.  * 
  33.  */
  34.  
  35. #include <mach.h>
  36. #include <device/device.h>
  37. #include <varargs.h>
  38.  
  39. static mach_port_t console_port;
  40.  
  41. void
  42. printf_init(device_server_port)
  43.     mach_port_t device_server_port;
  44. {
  45.     (void) device_open(device_server_port,
  46.                0,
  47.                "console",
  48.                &console_port);
  49. }
  50.  
  51. #define    PRINTF_BUFMAX    128
  52.  
  53. struct printf_state {
  54.     char buf[PRINTF_BUFMAX + 1]; /* extra for '\r\n' */
  55.     unsigned int index;
  56. };
  57.  
  58. static void
  59. flush(state)
  60.     struct printf_state *state;
  61. {
  62.     int amt;
  63.  
  64.     (void) device_write_inband(console_port, 0, 0,
  65.                    state->buf, state->index, &amt);
  66.     state->index = 0;
  67. }
  68.  
  69. static void
  70. putchar(arg, c)
  71.     char *arg;
  72.     int c;
  73. {
  74.     struct printf_state *state = (struct printf_state *) arg;
  75.  
  76.     if (c == '\n') {
  77.         state->buf[state->index] = '\r';
  78.         state->index++;
  79.     }
  80.     state->buf[state->index] = c;
  81.     state->index++;
  82.  
  83.     if (state->index >= PRINTF_BUFMAX)
  84.         flush(state);
  85. }
  86.  
  87. /*
  88.  * Printing (to console)
  89.  */
  90. vprintf(fmt, args)
  91.     char *fmt;
  92.     va_list args;
  93. {
  94.     struct printf_state state;
  95.  
  96.     state.index = 0;
  97.     _doprnt(fmt, args, 0, (void (*)()) putchar, (char *) &state);
  98.  
  99.     if (state.index != 0)
  100.         flush(&state);
  101. }
  102.  
  103. /*VARARGS1*/
  104. printf(fmt, va_alist)
  105.     char *fmt;
  106.     va_dcl
  107. {
  108.     va_list    args;
  109.  
  110.     va_start(args);
  111.     vprintf(fmt, args);
  112.     va_end(args);
  113. }
  114.  
  115. safe_gets(str, maxlen)
  116.     char *str;
  117.     int  maxlen;
  118. {
  119.     register char *lp;
  120.     register int c;
  121.  
  122.     char    inbuf[IO_INBAND_MAX];
  123.     unsigned int count;
  124.     register char *ip;
  125.     char *strmax = str + maxlen - 1; /* allow space for trailing 0 */
  126.  
  127.     lp = str;
  128.     for (;;) {
  129.         count = IO_INBAND_MAX;
  130.         (void) device_read_inband(console_port, 0, 0,
  131.                       sizeof(inbuf), inbuf, &count);
  132.         for (ip = inbuf; ip < &inbuf[count]; ip++) {
  133.         c = *ip;
  134.         switch (c) {
  135.             case '\n':
  136.             case '\r':
  137.             printf("\n");
  138.             *lp++ = 0;
  139.             return;
  140.  
  141.             case '\b':
  142.             case '#':
  143.             case '\177':
  144.             if (lp > str) {
  145.                 printf("\b \b");
  146.                 lp--;
  147.             }
  148.             continue;
  149.             case '@':
  150.             case 'u'&037:
  151.             lp = str;
  152.             printf("\n\r");
  153.             continue;
  154.             default:
  155.             if (c >= ' ' && c < '\177') {
  156.                 if (lp < strmax) {
  157.                 *lp++ = c;
  158.                 printf("%c", c);
  159.                 }
  160.                 else {
  161.                 printf("%c", '\007'); /* beep */
  162.                 }
  163.             }
  164.         }
  165.         }
  166.     }
  167. }
  168.