Skip to content

Commit

Permalink
Lecture 109 - Implementing the ability to read lines
Browse files Browse the repository at this point in the history
  • Loading branch information
nibblebits committed Dec 16, 2020
1 parent b10cefb commit bea44ff
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
9 changes: 5 additions & 4 deletions programs/blank/blank.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ int main(int argc, char** argv)
void* ptr = malloc(512);
free(ptr);

char buf[1024];
peachos_terminal_readline(buf, sizeof(buf), true);

print(buf);

while(1)
{
if (getkey() != 0)
{
print("key was pressed\n");
}
}
return 0;
}
11 changes: 7 additions & 4 deletions programs/stdlib/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
FILES=./build/start.o ./build/peachos.o ./build/stdlib.o ./build/stdio.o
FILES=./build/start.o ./build/peachos.asm.o ./build/peachos.o ./build/stdlib.o ./build/stdio.o
INCLUDES=-I./src
FLAGS= -g -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc
FLAGS= -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc

all: ${FILES}
i686-elf-ld -m elf_i386 -relocatable ${FILES} -o ./stdlib.elf

./build/start.o: ./src/start.asm
nasm -f elf ./src/start.asm -o ./build/start.o

./build/peachos.o: ./src/peachos.asm
nasm -f elf ./src/peachos.asm -o ./build/peachos.o
./build/peachos.asm.o: ./src/peachos.asm
nasm -f elf ./src/peachos.asm -o ./build/peachos.asm.o

./build/peachos.o: ./src/peachos.c
i686-elf-gcc ${INCLUDES} $(FLAGS) -std=gnu99 -c ./src/peachos.c -o ./build/peachos.o

./build/stdlib.o: ./src/stdlib.c
i686-elf-gcc ${INCLUDES} $(FLAGS) -std=gnu99 -c ./src/stdlib.c -o ./build/stdlib.o
Expand Down
6 changes: 3 additions & 3 deletions programs/stdlib/src/peachos.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
section .asm

global print:function
global getkey:function
global peachos_getkey:function
global peachos_malloc:function
global peachos_free:function
global peachos_putchar:function
Expand All @@ -19,8 +19,8 @@ print:
pop ebp
ret

; int getkey()
getkey:
; int peachos_getkey()
peachos_getkey:
push ebp
mov ebp, esp
mov eax, 2 ; Command getkey
Expand Down
46 changes: 46 additions & 0 deletions programs/stdlib/src/peachos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "peachos.h"

int peachos_getkeyblock()
{
int val = 0;
do
{
val = peachos_getkey();
}
while(val == 0);
return val;
}

void peachos_terminal_readline(char* out, int max, bool output_while_typing)
{
int i = 0;
for (i = 0; i < max -1; i++)
{
char key = peachos_getkeyblock();

// Carriage return means we have read the line
if (key == 13)
{
break;
}

if (output_while_typing)
{
peachos_putchar(key);
}

// Backspace
if (key == 0x08 && i >= 1)
{
out[i-1] = 0x00;
// -2 because we will +1 when we go to the continue
i -= 2;
continue;
}

out[i] = key;
}

// Add the null terminator
out[i] = 0x00;
}
6 changes: 5 additions & 1 deletion programs/stdlib/src/peachos.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#ifndef PEACHOS_H
#define PEACHOS_H
#include <stddef.h>
#include <stdbool.h>

void print(const char* filename);
int getkey();
int peachos_getkey();

void* peachos_malloc(size_t size);
void peachos_free(void* ptr);
void peachos_putchar(char c);
int peachos_getkeyblock();
void peachos_terminal_readline(char* out, int max, bool output_while_typing);


#endif

0 comments on commit bea44ff

Please sign in to comment.