Untitled

Anonymous
Plain Text
10/05/2025 3:37 PM
4.6 KB
17
Indexable
Never
// Extended grep with flags support
#include "types.h"
#include "stat.h"
#include "user.h"

#define MAX_LINE 1024

char buf[MAX_LINE];
int flags = 0;

// Flag constants
#define FLAG_I 1  // case-insensitive
#define FLAG_N 2  // show line numbers
#define FLAG_V 4  // invert match

int match(char*, char*);

// Case-insensitive character comparison
int case_insensitive_cmp(char a, char b) {
    if (a >= 'A' && a <= 'Z') a = a - 'A' + 'a';
    if (b >= 'A' && b <= 'Z') b = b - 'A' + 'a';
    return a == b;
}

// Modified matchhere for case-insensitive matching
int matchhere_ci(char *re, char *text);
int matchstar_ci(int c, char *re, char *text);

int match_ci(char *re, char *text) {
    if(re[0] == '^')
        return matchhere_ci(re+1, text);
    do{
        if(matchhere_ci(re, text))
            return 1;
    }while(*text++ != '\0');
    return 0;
}

int matchhere_ci(char *re, char *text) {
    if(re[0] == '\0')
        return 1;
    if(re[1] == '*')
        return matchstar_ci(re[0], re+2, text);
    if(re[0] == '$' && re[1] == '\0')
        return *text == '\0';
    if(*text!='\0' && (re[0]=='.' || case_insensitive_cmp(re[0], *text)))
        return matchhere_ci(re+1, text+1);
    return 0;
}

int matchstar_ci(int c, char *re, char *text) {
    do{
        if(matchhere_ci(re, text))
            return 1;
    }while(*text!='\0' && (*text++==c || c=='.' || case_insensitive_cmp(c, *(text-1))));
    return 0;
}

void grep(char *pattern, int fd, char *filename) {
    int n, m, line_num = 1;
    char *p, *q;

    m = 0;
    while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){
        m += n;
        buf[m] = '\0';
        p = buf;
        while((q = strchr(p, '
')) != 0){
            *q = 0;
            
            // Determine if line matches
            int matches;
            if (flags & FLAG_I) {
                matches = match_ci(pattern, p);
            } else {
                matches = match(pattern, p);
            }
            
            // Apply invert flag if set
            if (flags & FLAG_V) {
                matches = !matches;
            }
            
            if(matches){
                if (flags & FLAG_N) {
                    printf(1, "%d:", line_num);
                }
                *q = '
';
                write(1, p, q+1 - p);
            }
            p = q+1;
            line_num++;
        }
        if(p == buf)
            m = 0;
        if(m > 0){
            m -= p - buf;
            memmove(buf, p, m);
        }
    }
}

int main(int argc, char *argv[]) {
    int fd, i;
    char *pattern;
    int pattern_index = 1;

    // Parse flags
    while (pattern_index < argc && argv[pattern_index][0] == '-') {
        char *flag = argv[pattern_index];
        for (int j = 1; flag[j]; j++) {
            switch (flag[j]) {
                case 'i': flags |= FLAG_I; break;
                case 'n': flags |= FLAG_N; break;
                case 'v': flags |= FLAG_V; break;
                default:
                    printf(2, "grep: invalid option -- '%c'
", flag[j]);
                    exit();
            }
        }
        pattern_index++;
    }

    if(pattern_index >= argc){
        printf(2, "usage: grep [options] pattern [file ...]
");
        exit();
    }
    pattern = argv[pattern_index];
    pattern_index++;

    if(pattern_index >= argc){
        // Read from stdin (piping support)
        grep(pattern, 0, "");
        exit();
    }

    for(i = pattern_index; i < argc; i++){
        if((fd = open(argv[i], 0)) < 0){
            printf(1, "grep: cannot open %s
", argv[i]);
            exit();
        }
        grep(pattern, fd, argv[i]);
        close(fd);
    }
    exit();
}

// Original regex matcher functions (unchanged)
int matchhere(char*, char*);
int matchstar(int, char*, char*);

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

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;
}

int matchstar(int c, char *re, char *text) {
    do{
        if(matchhere(re, text))
            return 1;
    }while(*text!='\0' && (*text++==c || c=='.'));
    return 0;
}
Editor is loading...
Leave a Comment