svex

tsv explorer
git clone git://nsmpr.xyz/svex.git
Log | Files | Refs | README

commit 80df69e9275a217c6902d4f89a28ee773510b2e0
parent 485d36fbf2b426f1e6a909e8b7aedf6b8bed2568
Author: zavok <an2qzavok@gmail.com>
Date:   Sun, 23 Aug 2020 20:49:24 +0300

merge

Diffstat:
MMakefile | 4+++-
Msvex.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 118 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile @@ -7,6 +7,8 @@ BIN = svex MAN = $(BIN).1 OBJ = $(BIN:=.o) +CPPFLAGS = '-D_POSIX_C_SOURCE=200809L' + all: $(BIN) $(BIN): $(OBJ) @@ -29,4 +31,4 @@ clean: rm -f $(BIN) $(OBJ) .c.o: - $(CC) $(CFLAGS) -c $< + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< diff --git a/svex.c b/svex.c @@ -1,3 +1,6 @@ +/* following line is required to make dprintf work */ +// #define _POSIX_C_SOURCE 200809L + #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -11,9 +14,14 @@ char *argv0; char *fname; char *buff; -ulong blen; +long blen; +long curp, addr[2]; void load(void); +int parseaddr(char*, long*); +char* getrow(long); +int printrow(long); +void edit(); void usage (void) @@ -25,8 +33,8 @@ usage (void) int main (int argc, char *argv[]) { - blen = 0; buff = 0; + blen = 0; ARGBEGIN{ default: usage(); } ARGEND @@ -35,16 +43,78 @@ main (int argc, char *argv[]) fname = argv[0]; load(); } + curp = 1; + for (;;){ + long n; + char ibuf[1024]; + char *cmd; + n = read(0, ibuf, 1024); + ibuf[n] = 0; + cmd = ibuf + parseaddr(ibuf, addr); + switch (cmd[0]){ + case 'p': + printrow(addr[0]); + break; + case '\0': + case 'q': + exit(0); + break; + default : + printf("?\n"); + } + curp = addr[1]; + } return 0; } void +edit(void) +{ + for (;;) { + + } +} + +int +parseaddr(char *buf, long *addr) +{ + long n; + char *bp; + *addr = curp; + bp = buf; + switch (buf[0]){ + case '.': + // current position + *addr = curp; + bp++; + break; + case '$': + // last line + break; + case '/': + // regex search forward + break; + case '?': + // regex search backward + break; + default: + n = strtol(buf, &bp, 10); + if (bp != buf) *addr = n; + } + return (bp - buf); +} + +void load(void) { int f; long n; char b[1024]; f = open(fname, O_RDONLY); + if (f == -1) { + dprintf(2, "failed to open file %s!\n", fname); + exit(-1); + } while ((n = read(f, b, 1024)) > 0){ buff = realloc(buff, blen + n); memcpy(buff+blen, b, n); @@ -52,3 +122,46 @@ load(void) } close(f); } + +char* +getrow(long rn) +{ + char *p; + long n; + n = 1; + p = buff; + if (blen == 0) return 0; + while (n < rn) { + if (*p == '\n') n++; + p++; + if (p > buff + blen) return 0; + } + return p; +} + +int +printrow(long rn) +{ + char *p; + int col; + col = 0; + p = getrow(rn); + if (p == 0) return 0; + printf("----[ %ld ]---------\n", rn); + for (;;) { + switch (*p){ + case '\t': + write(1, "\n", 1); + col++; + break; + case '\n': + write(1, "\n\n", 2); + return 0; + default: + write(1, p, 1); + } + p++; + if (p > buff+blen) return -1; + } + return 0; +}