xv6:初探grep.c

34 阅读1分钟

grep.c

相关函数

int read(int fd, char *buf, int n) //Read at most n bytes into buf; returns number read; or 0 if end of file.
int write(int fd, char *buf, int n) //Write n bytes from buf to file descriptor fd; returns n.
int open(char *file, int flags) //Open a file; flags indicate read/write; returns an fd (file descriptor).
char *strchr(const char *str, int character) //在给定的str字符串中查找第一次出现character(ASCII码作为整数传递)的位置
void *memmove(void *dest, const void *src, size_t n) //从一个内存位置src复制一块数据到另一个内存位置dest,复制数据大小为n,它的特点是可以处理重叠的内存区域

code

// Simple grep.  Only supports ^ . * $ operators.

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

char buf[1024]; //定义缓冲区大小1024B
int match(char*, char*);

void
grep(char *pattern, int fd)
{
  int n, m;//n用来记录每次读取的字节数,m用来存偏移量
  char *p, *q;//字符指针,在缓冲区上移动

  m = 0;
  while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){//将数据从文件描述符不断地读到缓冲区中,直到读完为之,并确保在缓冲区末尾留出一个位置用于放置字符串的 null 终止字符
    m += n;//更新剩余缓冲区的索引
    buf[m] = '\0';//字符串结尾
    p = buf;
    while((q = strchr(p, '\n')) != 0){//不断地从缓冲区开始搜索第一个换行符,并返回其位置,表示找到一个完整的行
      *q = 0;//将提取的文本行转化为C字符串
      if(match(pattern, p)){//如果该行与正则表达式模式匹配,则将该行中匹配的部分写入到文件描述符1中
        *q = '\n';
        write(1, p, q+1 - p);
      }
      p = q+1;//更新位置,继续下一次搜索
    }
    if(m > 0){ //检查是否还有未处理的数据留在buf中,如果有的话,将这些数据移动到缓冲区的开始位置,以准备下一次循环
      m -= p - buf;
      memmove(buf, p, m);
    }
  }
}

int
main(int argc, char *argv[])
{
  int fd, i;
  char *pattern;
    
  //如果没有足够的命令行参数,打印错误信息
  if(argc <= 1){ 
    fprintf(2, "usage: grep pattern [file ...]\n");
    exit(1);
  }
    
  //将用户提供的正则表达式模式存储在pattern变量中
  pattern = argv[1];
    
  //如果没有指定文件名,则搜索标准输入中的数据
  if(argc <= 2){ 
    grep(pattern, 0);
    exit(0);
  }

  //遍历每个文件,以只读的方式打开,如果打开失败,打印错误信息,否则调用grep搜索文件内容,然后关闭文件
  for(i = 2; i < argc; i++){
    if((fd = open(argv[i], 0)) < 0){
      printf("grep: cannot open %s\n", argv[i]);
      exit(1);
    }
    grep(pattern, fd);
    close(fd);
  }
  exit(0);
}

// Regexp matcher from Kernighan & Pike,
// The Practice of Programming, Chapter 9.

int matchhere(char*, char*);
int matchstar(int, char*, char*);

int
match(char *re, char *text)
{
  if(re[0] == '^')
    return matchhere(re+1, text);
  do{  // must look at empty string
    if(matchhere(re, text))
      return 1;
  }while(*text++ != '\0');
  return 0;
}

// matchhere: search for re at beginning of text
int matchhere(char *re, char *text)
{
  if(re[0] == '\0')
    return 1;
  if(re[1] == '*')
    return matchstar(re[0], re+2, text);
  if(re[0] == '$' && re[1] == '\0')
    return *text == '\0';
  if(*text!='\0' && (re[0]=='.' || re[0]==*text))
    return matchhere(re+1, text+1);
  return 0;
}

// matchstar: search for c*re at beginning of text
int matchstar(int c, char *re, char *text)
{
  do{  // a * matches zero or more instances
    if(matchhere(re, text))
      return 1;
  }while(*text!='\0' && (*text++==c || c=='.'));
  return 0;
}

TO DO

match这块涉及到正则表达式引擎的实现,看了半天,各种递归套用还是迷迷糊糊的不是很理解,先挖个坑等以后再来补档吧QAQ