From 18e3ddf6058d86f22df9fd90f6ad7f3a3833909f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 11 Nov 2018 20:42:41 +0100 Subject: [PATCH] Add a naive /bin/fgrep for testing pipes. --- Kernel/TTY.cpp | 2 +- Kernel/sync.sh | 1 + LibC/string.cpp | 19 +++++++++++++++++++ LibC/string.h | 1 + LibC/termcap.cpp | 15 ++++++++------- Userland/.gitignore | 1 + Userland/Makefile | 5 +++++ Userland/fgrep.cpp | 20 ++++++++++++++++++++ 8 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 Userland/fgrep.cpp diff --git a/Kernel/TTY.cpp b/Kernel/TTY.cpp index d0e66f95bf..ce2eaf83c8 100644 --- a/Kernel/TTY.cpp +++ b/Kernel/TTY.cpp @@ -20,7 +20,7 @@ ssize_t TTY::read(byte* buffer, size_t size) if (nread == (ssize_t)m_buffer.size()) m_buffer.clear(); else { - dbgprintf("had %u, read %u\n", m_buffer.size(), nread); + kprintf("had %u, read %u\n", m_buffer.size(), nread); ASSERT_NOT_REACHED(); } return nread; diff --git a/Kernel/sync.sh b/Kernel/sync.sh index c02ce4cbeb..f7a313fc10 100755 --- a/Kernel/sync.sh +++ b/Kernel/sync.sh @@ -8,6 +8,7 @@ cp -v ../Userland/sh mnt/bin/sh cp -v ../Userland/id mnt/bin/id cp -v ../Userland/ps mnt/bin/ps cp -v ../Userland/ls mnt/bin/ls +cp -v ../Userland/fgrep mnt/bin/fgrep cp -v ../Userland/sleep mnt/bin/sleep cp -v ../Userland/date mnt/bin/date cp -v ../Userland/true mnt/bin/true diff --git a/LibC/string.cpp b/LibC/string.cpp index 1f638997ae..36be3fcb8a 100644 --- a/LibC/string.cpp +++ b/LibC/string.cpp @@ -191,4 +191,23 @@ char* strsignal(int signum) return const_cast(sys_siglist[signum]); } +char* strstr(const char* haystack, const char* needle) +{ + char nch; + char hch; + + if ((nch = *needle++) != 0) { + size_t len = strlen(needle); + do { + do { + if ((hch = *haystack++) == 0) + return nullptr; + } while (hch != nch); + } while (strncmp(haystack, needle, len) != 0); + --haystack; + } + return const_cast(haystack); } + +} + diff --git a/LibC/string.h b/LibC/string.h index cb0c9ddd9b..c121dfc862 100644 --- a/LibC/string.h +++ b/LibC/string.h @@ -17,6 +17,7 @@ void* memset(void*, int, size_t); char* strcpy(char* dest, const char* src); char* strncpy(char* dest, const char* src, size_t); char* strchr(const char*, int c); +char* strstr(const char* haystack, const char* needle); char* strrchr(const char*, int c); char* strcat(char *dest, const char *src); char* strncat(char *dest, const char *src, size_t); diff --git a/LibC/termcap.cpp b/LibC/termcap.cpp index a5c6a6ab12..9dbd7a7dc3 100644 --- a/LibC/termcap.cpp +++ b/LibC/termcap.cpp @@ -23,13 +23,13 @@ int tgetent(char* bp, const char* name) assert(false); } -static HashMap* caps = nullptr; +static HashMap* caps = nullptr; void ensure_caps() { if (caps) return; - caps = new HashMap; + caps = new HashMap; caps->set("DC", "\033[%p1%dP"); caps->set("IC", "\033[%p1%d@"); caps->set("ce", "\033[K"); @@ -60,12 +60,13 @@ void ensure_caps() char* tgetstr(char* id, char** area) { ensure_caps(); - fprintf(stderr, "tgetstr: id='%s', area=%p\n", id, area); + fprintf(stderr, "tgetstr: id='%s', area=%p", id, area); auto it = caps->find(id); if (it != caps->end()) { char* ret = *area; - strcpy(*area, (*it).value.characters()); - *area += (*it).value.length() + 1; + const char* val = (*it).value; + strcpy(*area, val); + *area += strlen(val) + 1; return ret; } assert(false); @@ -85,7 +86,7 @@ int tgetnum(char* id) fprintf(stderr, "tgetnum: '%s'\n", id); auto it = caps->find(id); if (it != caps->end()) { - return atoi((*it).value.characters()); + return atoi((*it).value); } assert(false); } @@ -97,7 +98,7 @@ char* tgoto(const char* cap, int col, int row) int tputs(const char* str, int affcnt, int (*putc)(int)) { - assert(false); + printf("%s", str); } } diff --git a/Userland/.gitignore b/Userland/.gitignore index 33447cf7c0..ab59c7d305 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -19,3 +19,4 @@ tty ft ft2 strsignal +fgrep diff --git a/Userland/Makefile b/Userland/Makefile index cbd895b1f3..355dee1d8f 100644 --- a/Userland/Makefile +++ b/Userland/Makefile @@ -17,6 +17,7 @@ OBJS = \ ft.o \ ft2.o \ strsignal.o \ + fgrep.o \ tty.o APPS = \ @@ -38,6 +39,7 @@ APPS = \ ft \ ft2 \ strsignal \ + fgrep \ tty ARCH_FLAGS = @@ -70,6 +72,9 @@ ps: ps.o ls: ls.o $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a +fgrep: fgrep.o + $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a + sleep: sleep.o $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a diff --git a/Userland/fgrep.cpp b/Userland/fgrep.cpp new file mode 100644 index 0000000000..db4ffd36cf --- /dev/null +++ b/Userland/fgrep.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +int main(int argc, char** argv) +{ + if (argc < 2) { + printf("usage: fgrep \n"); + return 0; + } + for (;;) { + char buf[4096]; + fgets(buf, sizeof(buf), stdin); + if (feof(stdin)) + return 0; + if (strstr(buf, argv[1])) + write(1, buf, strlen(buf)); + } + return 0; +}