#include #include #include int main(int argc, char** argv) { regex_t* preg; int reti; char* linebuf; char insection; if(argc != 2) { fprintf(stderr, "Usage: section \n"); return 1; } reti = regcomp(preg, argv[1], REG_EXTENDED); if (reti) { fprintf(stderr, "Could not compile regex\n"); return 1; } linebuf = (char*) calloc(4096, sizeof(char)); insection = 0; while (fgets(linebuf, 4096, stdin) != NULL) { if (insection) { if (linebuf[0] != ' ' && linebuf[0] != '\t') { insection = 0; } else fprintf(stdout, "%s", linebuf); } else { if (linebuf[0] != ' ' && linebuf[0] != '\t') { reti = regexec(preg, linebuf, 0, NULL, 0); if (!reti) { fprintf(stdout, "---\n%s", linebuf); insection = 1; } } } } free(linebuf); regfree(preg); }