Loading [MathJax]/extensions/tex2jax.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
console.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <sys/spinlock.h>
#include <sys/kmem.h>
#include <sys/thread.h>
#include "console.h"
#include "x86/vgacons.h"
#include "x86/sercons.h"
#include "x86/debugcons.h"
Include dependency graph for console.c:

Go to the source code of this file.

Functions

void Console_Init ()
 
void Console_LateInit ()
 
char Console_Getc ()
 
void Console_EnqueueKey (char key)
 
void Console_Gets (char *str, size_t n)
 
void Console_Putc (char ch)
 
void Console_Puts (const char *str)
 
int Console_Read (Handle *handle, void *buf, uint64_t off, uint64_t len)
 
int Console_Write (Handle *handle, void *buf, uint64_t off, uint64_t len)
 
int Console_Flush (Handle *handle)
 
int Console_Close (Handle *handle)
 
HandleConsole_OpenHandle ()
 

Variables

Spinlock consoleLock
 
Console consoles
 

Function Documentation

◆ Console_Close()

int Console_Close ( Handle handle)

Definition at line 193 of file console.c.

194{
195 Handle_Free(handle);
196 return 0;
197}
Here is the caller graph for this function:

◆ Console_EnqueueKey()

void Console_EnqueueKey ( char  key)

Definition at line 75 of file console.c.

76{
80 return;
81 }
85}
Console consoles
Definition: console.c:15
char keyBuf[CONSOLE_KEYBUF_MAXLEN]
Definition: console.h:26
Spinlock keyLock
Definition: console.h:27
int lastKey
Definition: console.h:25
#define CONSOLE_KEYBUF_MAXLEN
Definition: console.h:20
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
void Spinlock_Lock(Spinlock *lock) __LOCK_EX(*lock)
Definition: spinlock.c:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_Flush()

int Console_Flush ( Handle handle)

Definition at line 187 of file console.c.

188{
189 return 0;
190}
Here is the caller graph for this function:

◆ Console_Getc()

char Console_Getc ( )

Definition at line 47 of file console.c.

48{
49 while (1) {
52 char key = consoles.keyBuf[consoles.nextKey];
55 return key;
56 }
58
59 // Support serial debugging if interrupts are disabled
60 if (Serial_HasData()) {
61 char key = Serial_Getc();
62 switch (key) {
63 case '\r':
64 return '\n';
65 case 0x7f:
66 return '\b';
67 default:
68 return key;
69 }
70 }
71 }
72}
int nextKey
Definition: console.h:24
bool Serial_HasData()
Definition: sercons.c:73
char Serial_Getc()
Definition: sercons.c:78
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_Gets()

void Console_Gets ( char *  str,
size_t  n 
)

Definition at line 88 of file console.c.

89{
90 int i;
91
92 for (i = 0; i < (n - 1); i++)
93 {
94 char ch = Console_Getc();
95 if (ch == '\b') {
96 if (i > 0) {
97 Console_Putc(ch);
98 i--;
99 }
100 i--;
101 continue;
102 }
103 if (ch == '\n') {
104 Console_Putc('\n');
105 str[i] = '\0';
106 return;
107 }
108 if (ch == '\r') {
109 Console_Putc('\n');
110 str[i] = '\0';
111 return;
112 }
113 if (ch == '\t') {
114 Console_Putc('\t');
115 str[i] = '\t';
116 continue;
117 }
118 if (ch < 0x20)
119 {
120 // Unprintable character
121 continue;
122 }
123 Console_Putc(ch);
124 str[i] = ch;
125 }
126
127 str[i+1] = '\0';
128}
void Console_Putc(char ch)
Definition: console.c:131
char Console_Getc()
Definition: console.c:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_Init()

void Console_Init ( )

Definition at line 22 of file console.c.

23{
24 VGA_Init();
27
28 Console_Puts("Castor Operating System\n");
29
31
32 Spinlock_Init(&consoles.keyLock, "Console Keyboard Lock", SPINLOCK_TYPE_NORMAL);
33 consoles.nextKey = 0;
34 consoles.lastKey = 0;
35}
void Console_Puts(const char *str)
Definition: console.c:141
Spinlock consoleLock
Definition: console.c:14
void DebugConsole_Init()
Definition: debugcons.c:13
void Serial_Init(void)
Definition: sercons.c:40
#define SPINLOCK_TYPE_NORMAL
Definition: spinlock.h:12
void Spinlock_Init(Spinlock *lock, const char *name, uint64_t type)
Definition: spinlock.c:43
void VGA_Init(void)
Definition: vgacons.c:75
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_LateInit()

void Console_LateInit ( )

Definition at line 41 of file console.c.

42{
44}
void Serial_LateInit(void)
Definition: sercons.c:59
Here is the call graph for this function:

◆ Console_OpenHandle()

Handle * Console_OpenHandle ( )

Definition at line 200 of file console.c.

201{
202 Handle *handle = Handle_Alloc();
203 if (!handle)
204 return NULL;
205
206 handle->read = &Console_Read;
207 handle->write = &Console_Write;
208 handle->flush = &Console_Flush;
209 handle->close = &Console_Close;
210
211 return handle;
212}
int Console_Close(Handle *handle)
Definition: console.c:193
int Console_Write(Handle *handle, void *buf, uint64_t off, uint64_t len)
Definition: console.c:167
int Console_Read(Handle *handle, void *buf, uint64_t off, uint64_t len)
Definition: console.c:151
int Console_Flush(Handle *handle)
Definition: console.c:187
#define NULL
Definition: stddef.h:6
Definition: handle.h:17
int(* close)(Handle *)
Definition: handle.h:26
int(* write)(Handle *, void *, uint64_t, uint64_t)
Definition: handle.h:24
int(* read)(Handle *, void *, uint64_t, uint64_t)
Definition: handle.h:23
int(* flush)(Handle *)
Definition: handle.h:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_Putc()

void Console_Putc ( char  ch)

Definition at line 131 of file console.c.

132{
134 VGA_Putc(ch);
135 Serial_Putc(ch);
138}
void DebugConsole_Putc(short c)
Definition: debugcons.c:17
void Serial_Putc(char ch)
Definition: sercons.c:87
void VGA_Putc(short c)
Definition: vgacons.c:115
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_Puts()

void Console_Puts ( const char *  str)

Definition at line 141 of file console.c.

142{
144 VGA_Puts(str);
145 Serial_Puts(str);
148}
void DebugConsole_Puts(const char *str)
Definition: debugcons.c:22
void Serial_Puts(const char *str)
Definition: sercons.c:102
void VGA_Puts(const char *str)
Definition: vgacons.c:157
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_Read()

int Console_Read ( Handle handle,
void *  buf,
uint64_t  off,
uint64_t  len 
)

Definition at line 151 of file console.c.

152{
154 uint64_t i;
155
156 for (i = 0; i < len; i++)
157 {
158 char c = Console_Getc();
159 Console_Putc(c);
160 Copy_Out(&c, b+i, 1);
161 }
162
163 return len;
164}
static char buf[4096]
Definition: ethdump.c:10
int Copy_Out(void *fromkernel, uintptr_t touser, uintptr_t len)
Definition: copy.c:70
uint64_t len
Definition: multiboot.h:2
uint64_t uintptr_t
Definition: types.h:16
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Console_Write()

int Console_Write ( Handle handle,
void *  buf,
uint64_t  off,
uint64_t  len 
)

Definition at line 167 of file console.c.

168{
169 int i;
171 char kbuf[512];
172 uint64_t nbytes = 0;
173
174 while (len > nbytes) {
175 uint64_t chunksz = len > 512 ? 512 : len;
176 Copy_In(b + nbytes, &kbuf, chunksz);
177 nbytes += chunksz;
178
179 for (i = 0; i < chunksz; i++)
180 Console_Putc(kbuf[i]);
181 }
182
183 return nbytes;
184}
int Copy_In(uintptr_t fromuser, void *tokernel, uintptr_t len)
Definition: copy.c:34
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ consoleLock

Spinlock consoleLock

Definition at line 14 of file console.c.

◆ consoles

Console consoles

Definition at line 15 of file console.c.