void parsefile(char *const *argv, const char *cwd, const char *root, const char *dbpath, int db) { int count = 0; int file_count = 0; STRBUF *sb = strbuf_open(0); char *langmap; const char *plugin_parser, *av; char path[MAXPATHLEN]; struct parsefile_data data; if (db == GRTAGS + GSYMS) data.target = TARGET_REF|TARGET_SYM; else data.target = 1 << db; data.extractmethod = getconfb("extractmethod"); if (getconfs("langmap", sb)) langmap = check_strdup(strbuf_value(sb)); else langmap = NULL; strbuf_reset(sb); if (getconfs("gtags_parser", sb)) plugin_parser = strbuf_value(sb); else plugin_parser = NULL; data.cv = convert_open(type, format, root, cwd, dbpath, stdout); if (gpath_open(dbpath, 0) < 0) die("GPATH not found."); if (data.target == TARGET_REF || data.target == TARGET_SYM) { data.dbop = dbop_open(makepath(dbpath, dbname(GTAGS), NULL), 0, 0, 0); if (data.dbop == NULL) die("%s not found.", dbname(GTAGS)); } else { data.dbop = NULL; } data.fid = NULL; parser_init(langmap, plugin_parser); if (langmap != NULL) free(langmap); if (*argv && file_list) args_open_both(argv, file_list); else if (*argv) args_open(argv); else if (file_list) args_open_filelist(file_list); else args_open_nop(); while ((av = args_read()) != NULL) { /* * convert the path into relative to the root directory of source tree. */ if (normalize(av, get_root_with_slash(), cwd, path, sizeof(path)) == NULL) if (!qflag) fprintf(stderr, "'%s' is out of source tree.\n", path + 2); /* * Memorize the file id of the path. This is used in put_syms(). */ { static char s_fid[MAXFIDLEN]; const char *p = gpath_path2fid(path, NULL); if (!p) { if (!qflag) fprintf(stderr, "'%s' not found in GPATH.\n", path + 2); continue; } strlimcpy(s_fid, p, sizeof(s_fid)); data.fid = s_fid; } if (!test("f", makepath(root, path, NULL))) { if (test("d", NULL)) { if (!qflag) fprintf(stderr, "'%s' is a directory.\n", av); } else { if (!qflag) fprintf(stderr, "'%s' not found.\n", av); } continue; } if (lflag && !locatestring(path, localprefix, MATCH_AT_FIRST)) continue; data.count = 0; parse_file(path, 0, put_syms, &data); count += data.count; file_count++; } args_close(); parser_exit(); /* * Settlement */ if (data.dbop != NULL) dbop_close(data.dbop); gpath_close(); convert_close(data.cv); strbuf_close(sb); if (vflag) { print_count(count); fprintf(stderr, " (no index used).\n"); } }
/* * grep: grep pattern * * i) pattern POSIX regular expression */ void grep(const char *pattern, char *const *argv, const char *dbpath) { FILE *fp; CONVERT *cv; GFIND *gp = NULL; STRBUF *ib = strbuf_open(MAXBUFLEN); const char *path; char encoded_pattern[IDENTLEN]; const char *buffer; int linenum, count; int flags = 0; int target = GPATH_SOURCE; regex_t preg; int user_specified = 1; /* * convert spaces into %FF format. */ encode(encoded_pattern, sizeof(encoded_pattern), pattern); if (oflag) target = GPATH_BOTH; if (Oflag) target = GPATH_OTHER; if (!Gflag) flags |= REG_EXTENDED; if (iflag) flags |= REG_ICASE; if (regcomp(&preg, pattern, flags) != 0) die("invalid regular expression."); cv = convert_open(type, format, root, cwd, dbpath, stdout); count = 0; if (*argv && file_list) args_open_both(argv, file_list); else if (*argv) args_open(argv); else if (file_list) args_open_filelist(file_list); else { args_open_gfind(gp = gfind_open(dbpath, localprefix, target)); user_specified = 0; } while ((path = args_read()) != NULL) { if (user_specified) { static char buf[MAXPATHLEN]; if (normalize(path, get_root_with_slash(), cwd, buf, sizeof(buf)) == NULL) if (!qflag) fprintf(stderr, "'%s' is out of source tree.\n", path); if (!test("f", buf)) die("'%s' not found. Please remake tag files by invoking gtags(1).", path); path = buf; } if (!(fp = fopen(path, "r"))) die("cannot open file '%s'.", path); linenum = 0; while ((buffer = strbuf_fgets(ib, fp, STRBUF_NOCRLF)) != NULL) { int result = regexec(&preg, buffer, 0, 0, 0); linenum++; if ((!Vflag && result == 0) || (Vflag && result != 0)) { count++; if (format == FORMAT_PATH) { convert_put_path(cv, path); break; } else { convert_put_using(cv, encoded_pattern, path, linenum, buffer, (user_specified) ? NULL : gp->dbop->lastdat); } } } fclose(fp); } args_close(); convert_close(cv); strbuf_close(ib); regfree(&preg); if (vflag) { print_count(count); fprintf(stderr, " (no index used).\n"); } }
/** * grep: @NAME{grep} pattern * * @param[in] pattern @NAME{POSIX} regular expression * @param argv * @param dbpath */ void grep(const char *pattern, char *const *argv, const char *dbpath) { FILE *fp; CONVERT *cv; GFIND *gp = NULL; STRBUF *ib = strbuf_open(MAXBUFLEN); const char *path; char encoded_pattern[IDENTLEN]; const char *buffer; int linenum, count; int flags = 0; int target = GPATH_SOURCE; regex_t preg; int user_specified = 1; /* * convert spaces into %FF format. */ encode(encoded_pattern, sizeof(encoded_pattern), pattern); /* * literal search available? */ if (!literal) { const char *p = pattern; int normal = 1; for (; *p; p++) { if (!(isalpha(*p) || isdigit(*p) || isblank(*p) || *p == '_')) { normal = 0; break; } } if (normal) literal = 1; } if (oflag) target = GPATH_BOTH; if (Oflag) target = GPATH_OTHER; if (literal) { literal_comple(pattern); } else { if (!Gflag) flags |= REG_EXTENDED; if (iflag) flags |= REG_ICASE; if (regcomp(&preg, pattern, flags) != 0) die("invalid regular expression."); } cv = convert_open(type, format, root, cwd, dbpath, stdout, NOTAGS); cv->tag_for_display = encoded_pattern; count = 0; if (*argv && file_list) args_open_both(argv, file_list); else if (*argv) args_open(argv); else if (file_list) args_open_filelist(file_list); else { args_open_gfind(gp = gfind_open(dbpath, localprefix, target)); user_specified = 0; } while ((path = args_read()) != NULL) { if (user_specified) { static char buf[MAXPATHLEN]; if (normalize(path, get_root_with_slash(), cwd, buf, sizeof(buf)) == NULL) { warning("'%s' is out of the source project.", path); continue; } if (test("d", buf)) { warning("'%s' is a directory. Ignored.", path); continue; } if (!test("f", buf)) { warning("'%s' not found. Ignored.", path); continue; } path = buf; } if (Sflag && !locatestring(path, localprefix, MATCH_AT_FIRST)) continue; if (literal) { int n = literal_search(cv, path); if (n > 0) count += n; } else { if (!(fp = fopen(path, "r"))) die("cannot open file '%s'.", path); linenum = 0; while ((buffer = strbuf_fgets(ib, fp, STRBUF_NOCRLF)) != NULL) { int result = regexec(&preg, buffer, 0, 0, 0); linenum++; if ((!Vflag && result == 0) || (Vflag && result != 0)) { count++; if (format == FORMAT_PATH) { convert_put_path(cv, NULL, path); break; } else { convert_put_using(cv, pattern, path, linenum, buffer, (user_specified) ? NULL : gp->dbop->lastdat); } } } fclose(fp); } } args_close(); convert_close(cv); strbuf_close(ib); if (literal == 0) regfree(&preg); if (vflag) { print_count(count); fprintf(stderr, " (no index used).\n"); } }