root/user/grep.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. grep
  2. main
  3. match
  4. matchhere
  5. matchstar

   1 // Simple grep.  Only supports ^ . * $ operators.
   2 
   3 #include "kernel/types.h"
   4 #include "kernel/stat.h"
   5 #include "user/user.h"
   6 
   7 char buf[1024];
   8 int match(char*, char*);
   9 
  10 void
  11 grep(char *pattern, int fd)
  12 {
  13   int n, m;
  14   char *p, *q;
  15 
  16   m = 0;
  17   while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){
  18     m += n;
  19     buf[m] = '\0';
  20     p = buf;
  21     while((q = strchr(p, '\n')) != 0){
  22       *q = 0;
  23       if(match(pattern, p)){
  24         *q = '\n';
  25         write(1, p, q+1 - p);
  26       }
  27       p = q+1;
  28     }
  29     if(m > 0){
  30       m -= p - buf;
  31       memmove(buf, p, m);
  32     }
  33   }
  34 }
  35 
  36 int
  37 main(int argc, char *argv[])
  38 {
  39   int fd, i;
  40   char *pattern;
  41 
  42   if(argc <= 1){
  43     fprintf(2, "usage: grep pattern [file ...]\n");
  44     exit(1);
  45   }
  46   pattern = argv[1];
  47 
  48   if(argc <= 2){
  49     grep(pattern, 0);
  50     exit(0);
  51   }
  52 
  53   for(i = 2; i < argc; i++){
  54     if((fd = open(argv[i], 0)) < 0){
  55       printf("grep: cannot open %s\n", argv[i]);
  56       exit(1);
  57     }
  58     grep(pattern, fd);
  59     close(fd);
  60   }
  61   exit(0);
  62 }
  63 
  64 // Regexp matcher from Kernighan & Pike,
  65 // The Practice of Programming, Chapter 9, or
  66 // https://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
  67 
  68 int matchhere(char*, char*);
  69 int matchstar(int, char*, char*);
  70 
  71 int
  72 match(char *re, char *text)
  73 {
  74   if(re[0] == '^')
  75     return matchhere(re+1, text);
  76   do{  // must look at empty string
  77     if(matchhere(re, text))
  78       return 1;
  79   }while(*text++ != '\0');
  80   return 0;
  81 }
  82 
  83 // matchhere: search for re at beginning of text
  84 int matchhere(char *re, char *text)
  85 {
  86   if(re[0] == '\0')
  87     return 1;
  88   if(re[1] == '*')
  89     return matchstar(re[0], re+2, text);
  90   if(re[0] == '$' && re[1] == '\0')
  91     return *text == '\0';
  92   if(*text!='\0' && (re[0]=='.' || re[0]==*text))
  93     return matchhere(re+1, text+1);
  94   return 0;
  95 }
  96 
  97 // matchstar: search for c*re at beginning of text
  98 int matchstar(int c, char *re, char *text)
  99 {
 100   do{  // a * matches zero or more instances
 101     if(matchhere(re, text))
 102       return 1;
 103   }while(*text!='\0' && (*text++==c || c=='.'));
 104   return 0;
 105 }
 106 

/* [<][>][^][v][top][bottom][index][help] */