commit 02c269e2b30419cc19a65e7ee1f552cfee98c7b0
parent fd22de38ff77200ac4c4da8b23c6cdec1a3076ae
Author: zavok <an2qzavok@gmail.com>
Date: Mon, 9 Mar 2020 20:15:55 +0300
half of addr code
Diffstat:
M | Makefile | | | 4 | +++- |
M | svex.c | | | 53 | +++++++++++++++++++++++++++++++++++++++++++++++------ |
2 files changed, 50 insertions(+), 7 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,5 +1,5 @@
/* following line is required to make dprintf work */
-#define _POSIX_C_SOURCE 200809L
+// #define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
@@ -15,8 +15,10 @@ char *fname;
char *buff;
long blen;
+long curp, addr[2];
void load(void);
+int parseaddr(char*);
char* getrow(long);
int printrow(long);
@@ -30,8 +32,6 @@ usage (void)
int
main (int argc, char *argv[])
{
- char *ip, ibuf[1024];
- long n;
blen = 0;
buff = 0;
ARGBEGIN{
@@ -41,21 +41,59 @@ main (int argc, char *argv[])
if (argc > 1) usage();
fname = argv[0];
load();
+ curp = 1;
for (;;){
+ long n;
+ char ibuf[1024];
+ char *cmd;
n = read(0, ibuf, 1024);
- ip = ibuf;
ibuf[n] = 0;
- switch (ip[0]){
+ cmd = ibuf + parseaddr(ibuf);
+ switch (cmd[0]){
+ case 'p':
+ printrow(addr[1]);
+ break;
+ case '\0':
case 'q':
exit(0);
break;
default :
printf("?\n");
}
+ curp = addr[1];
}
return 0;
}
+int
+parseaddr(char *buf)
+{
+ long n;
+ char *bp;
+ addr[1] = curp;
+ bp = buf;
+ switch (buf[0]){
+ case '.':
+ // current position
+ addr[1] = 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[1] = n;
+ }
+ return (bp - buf);
+}
+
void
load(void)
{
@@ -80,7 +118,7 @@ getrow(long rn)
{
char *p;
long n;
- n = 0;
+ n = 1;
p = buff;
while (n < rn) {
if (*p == '\n') n++;
@@ -94,12 +132,15 @@ int
printrow(long rn)
{
char *p;
+ int col;
+ col = 0;
p = getrow(rn);
printf("---[ %d ]----------\n", rn);
for (;;) {
switch (*p){
case '\t':
write(1, "\n", 1);
+ col++;
break;
case '\n':
write(1, "\n\n", 2);