usr.bin: add shtest

shtest is a simple userland shell test program that accepts input and
echoes it back to the user
This commit is contained in:
Michael Shaw 2024-02-19 01:47:59 -05:00
commit 3b1dc7e153
4 changed files with 96 additions and 1 deletions

View file

@ -1,4 +1,4 @@
usrprog=('ftest' 'test1' 'vfs')
usrprog=('ftest' 'test1' 'vfs' 'shtest')
for prog in ${usrprog[@]}; do
make -C $prog $1 --no-print-directory

38
usr.bin/shtest/Makefile Normal file
View file

@ -0,0 +1,38 @@
# SPDX-License-Identifier: GPL-2.0-only
# shtest: simple userspace shell test
CC := x86_64-elf-gcc
LD := x86_64-elf-ld
OUT := shtest.elf
CSRC := shtest.c
SSRC := syscall.S
OBJ := $(CSRC:.c=.o) $(SSRC:.S=.o)
DEP := $(OBJ:.o=.d)
CFLAGS := -Wall -ffreestanding -mno-mmx -mno-sse2 -mno-sse -m64 -MMD -g
LDFLAGS := -nostdlib -static -m elf_x86_64
CURDIR := $(shell pwd | awk -F '/' '{print $$(NF-1),"/",$$NF}' | sed 's/ //g')
$(OUT): $(OBJ)
@$(LD) $^ $(LDFLAGS) -o $@
@echo " USR.LD $(CURDIR)/$@"
-include $(DEP)
%.o: %.S
@$(CC) $(CFLAGS) -c $< -o $@
@echo " USR.CC $(CURDIR)/$@"
%.o: %.c
@$(CC) $(CFLAGS) -c $< -o $@
@echo " USR.CC $(CURDIR)/$@"
clean:
@rm -rf $(OUT) $(OBJ) $(DEP)
install: $(OUT)
@cp $(OUT) ../../bin
@echo " INSTALL $(CURDIR)/$(OUT)"

38
usr.bin/shtest/shtest.c Normal file
View file

@ -0,0 +1,38 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* shtest: simple userland shell test */
#include <stdint.h>
#include <stddef.h>
typedef int64_t ssize_t;
int open(const char *pathname, int flags);
ssize_t read(int fd, void *buf, int count);
ssize_t write(int fd, void *buf, int count);
int close(int fd);
int exit(int status);
void promptfd(int fd)
{
write(fd, "shtest> ", 8);
}
void _start()
{
int fd = open("/dev/tty", 0);
if (fd < 0)
exit(1);
promptfd(fd);
char c;
while (read(fd, &c, 1) == 1) {
write(fd, &c, 1);
if (c == '\n') {
promptfd(fd);
}
}
close(fd);
exit(0);
}

19
usr.bin/shtest/syscall.S Normal file
View file

@ -0,0 +1,19 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* syscall handler for simple userland shell test */
#define __ASM__
.macro _sc_define name, num
.global \name
\name:
movq $\num, %rax
movq %rcx, %r10
int $0x80
ret
.endm
_sc_define read, 0
_sc_define write, 1
_sc_define open, 2
_sc_define close, 3
_sc_define exit, 60