summaryrefslogtreecommitdiffstats
path: root/src/lib/user/console.c
blob: 22bdc8c8b23dbd4a4b70df662738cb7c282ce7a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <string.h>
#include <syscall.h>
#include <syscall-nr.h>

/* The standard vprintf() function,
   which is like printf() but uses a va_list. */
int
vprintf (const char *format, va_list args) 
{
  return vhprintf (STDOUT_FILENO, format, args);
}

/* Like printf(), but writes output to the given HANDLE. */
int
hprintf (int handle, const char *format, ...) 
{
  va_list args;
  int retval;

  va_start (args, format);
  retval = vhprintf (handle, format, args);
  va_end (args);

  return retval;
}

/* Writes string S to the console, followed by a new-line
   character. */
int
puts (const char *s) 
{
  write (STDOUT_FILENO, s, strlen (s));
  putchar ('\n');

  return 0;
}

/* Writes C to the console. */
int
putchar (int c) 
{
  char c2 = c;
  write (STDOUT_FILENO, &c2, 1);
  return c;
}

/* Auxiliary data for vhprintf_helper(). */
struct vhprintf_aux 
  {
    char buf[64];       /* Character buffer. */
    char *p;            /* Current position in buffer. */
    int char_cnt;       /* Total characters written so far. */
    int handle;         /* Output file handle. */
  };

static void add_char (char, void *);
static void flush (struct vhprintf_aux *);

/* Formats the printf() format specification FORMAT with
   arguments given in ARGS and writes the output to the given
   HANDLE. */
int
vhprintf (int handle, const char *format, va_list args) 
{
  struct vhprintf_aux aux;
  aux.p = aux.buf;
  aux.char_cnt = 0;
  aux.handle = handle;
  __vprintf (format, args, add_char, &aux);
  flush (&aux);
  return aux.char_cnt;
}

/* Adds C to the buffer in AUX, flushing it if the buffer fills
   up. */
static void
add_char (char c, void *aux_) 
{
  struct vhprintf_aux *aux = aux_;
  *aux->p++ = c;
  if (aux->p >= aux->buf + sizeof aux->buf)
    flush (aux);
  aux->char_cnt++;
}

/* Flushes the buffer in AUX. */
static void
flush (struct vhprintf_aux *aux)
{
  if (aux->p > aux->buf)
    write (aux->handle, aux->buf, aux->p - aux->buf);
  aux->p = aux->buf;
}