xv6-riscv-kernel/user/grep.c

108 lines
2 KiB
C
Raw Permalink Normal View History

2007-08-28 06:26:34 +02:00
// Simple grep. Only supports ^ . * $ operators.
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
2007-08-28 06:26:34 +02:00
char buf[1024];
2024-06-15 16:55:06 +02:00
int match(char *, char *);
2007-08-28 06:26:34 +02:00
void
grep(char *pattern, int fd)
{
2024-06-15 16:55:06 +02:00
int n, m;
2007-08-28 06:26:34 +02:00
char *p, *q;
2007-08-28 06:26:34 +02:00
m = 0;
2024-06-15 16:55:06 +02:00
while((n = read(fd, buf + m, sizeof(buf) - m - 1)) > 0) {
2007-08-28 06:26:34 +02:00
m += n;
buf[m] = '\0';
2007-08-28 06:26:34 +02:00
p = buf;
2024-06-15 16:55:06 +02:00
while((q = strchr(p, '\n')) != 0) {
2007-08-28 06:26:34 +02:00
*q = 0;
2024-06-15 16:55:06 +02:00
if(match(pattern, p)) {
2007-08-28 06:26:34 +02:00
*q = '\n';
2024-06-15 16:55:06 +02:00
write(1, p, q + 1 - p);
2007-08-28 06:26:34 +02:00
}
2024-06-15 16:55:06 +02:00
p = q + 1;
2007-08-28 06:26:34 +02:00
}
2024-06-15 16:55:06 +02:00
if(m > 0) {
2007-08-28 06:26:34 +02:00
m -= p - buf;
memmove(buf, p, m);
}
}
}
int
main(int argc, char *argv[])
{
2024-06-15 16:55:06 +02:00
int fd, i;
2007-08-28 06:26:34 +02:00
char *pattern;
2024-06-15 16:55:06 +02:00
if(argc <= 1) {
2019-08-27 19:13:03 +02:00
fprintf(2, "usage: grep pattern [file ...]\n");
2019-09-11 16:04:40 +02:00
exit(1);
2007-08-28 06:26:34 +02:00
}
pattern = argv[1];
2024-06-15 16:55:06 +02:00
if(argc <= 2) {
2007-08-28 06:26:34 +02:00
grep(pattern, 0);
exit(0);
2007-08-28 06:26:34 +02:00
}
2024-06-15 16:55:06 +02:00
for(i = 2; i < argc; i++) {
if((fd = open(argv[i], 0)) < 0) {
2019-08-27 19:13:03 +02:00
printf("grep: cannot open %s\n", argv[i]);
2019-09-11 16:04:40 +02:00
exit(1);
2007-08-28 06:26:34 +02:00
}
grep(pattern, fd);
close(fd);
}
exit(0);
2007-08-28 06:26:34 +02:00
}
// Regexp matcher from Kernighan & Pike,
// The Practice of Programming, Chapter 9, or
// https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
2007-08-28 06:26:34 +02:00
2024-06-15 16:55:06 +02:00
int matchhere(char *, char *);
int matchstar(int, char *, char *);
2007-08-28 06:26:34 +02:00
int
match(char *re, char *text)
{
if(re[0] == '^')
2024-06-15 16:55:06 +02:00
return matchhere(re + 1, text);
do { // must look at empty string
2007-08-28 06:26:34 +02:00
if(matchhere(re, text))
return 1;
2024-06-15 16:55:06 +02:00
} while(*text++ != '\0');
2007-08-28 06:26:34 +02:00
return 0;
}
// matchhere: search for re at beginning of text
2024-06-15 16:55:06 +02:00
int
matchhere(char *re, char *text)
2007-08-28 06:26:34 +02:00
{
if(re[0] == '\0')
return 1;
if(re[1] == '*')
2024-06-15 16:55:06 +02:00
return matchstar(re[0], re + 2, text);
2007-08-28 06:26:34 +02:00
if(re[0] == '$' && re[1] == '\0')
return *text == '\0';
2024-06-15 16:55:06 +02:00
if(*text != '\0' && (re[0] == '.' || re[0] == *text))
return matchhere(re + 1, text + 1);
2007-08-28 06:26:34 +02:00
return 0;
}
// matchstar: search for c*re at beginning of text
2024-06-15 16:55:06 +02:00
int
matchstar(int c, char *re, char *text)
2007-08-28 06:26:34 +02:00
{
2024-06-15 16:55:06 +02:00
do { // a * matches zero or more instances
2007-08-28 06:26:34 +02:00
if(matchhere(re, text))
return 1;
2024-06-15 16:55:06 +02:00
} while(*text != '\0' && (*text++ == c || c == '.'));
2007-08-28 06:26:34 +02:00
return 0;
}