/* * getURL: get URL of the specified file. * * i) file file name * i) htmldir HTML directory * o) URL URL begin with 'file:' */ void getURL(const char *file, const char *htmldir, STRBUF *URL) { char *p; char buf[MAXPATHLEN]; STRBUF *sb = strbuf_open(0); if (!test("f", file) && !test("d", file)) die("file '%s' not found.", file); p = normalize(file, get_root_with_slash(), cwd, buf, sizeof(buf)); if (p != NULL && convertpath(dbpath, htmldir, p, sb) == 0) makefileurl(strbuf_value(sb), linenumber, URL); else makefileurl(realpath(file, buf), 0, URL); strbuf_close(sb); }
/* * 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"); } }
int decide_tag_by_context(const char *tag, const char *file, int lineno) { STRBUF *sb = NULL; char path[MAXPATHLEN], s_fid[MAXFIDLEN]; const char *tagline, *p; DBOP *dbop; int db = GSYMS; int iscompline = 0; if (normalize(file, get_root_with_slash(), cwd, path, sizeof(path)) == NULL) die("'%s' is out of source tree.", file); /* * get file id */ if (gpath_open(dbpath, 0) < 0) die("GPATH not found."); if ((p = gpath_path2fid(path, NULL)) == NULL) die("path name in the context is not found."); strlimcpy(s_fid, p, sizeof(s_fid)); gpath_close(); /* * read btree records directly to avoid the overhead. */ dbop = dbop_open(makepath(dbpath, dbname(GTAGS), NULL), 0, 0, 0); if (dbop == NULL) die("cannot open GTAGS."); if (dbop_getoption(dbop, COMPLINEKEY)) iscompline = 1; tagline = dbop_first(dbop, tag, NULL, 0); if (tagline) { db = GTAGS; for (; tagline; tagline = dbop_next(dbop)) { /* * examine whether the definition record include the context. */ p = locatestring(tagline, s_fid, MATCH_AT_FIRST); if (p != NULL && *p == ' ') { for (p++; *p && *p != ' '; p++) ; if (*p++ != ' ' || !isdigit(*p)) die("Impossible! decide_tag_by_context(1)"); /* * Standard format n <blank> <image>$ * Compact format d,d,d,d$ */ if (!iscompline) { /* Standard format */ if (atoi(p) == lineno) { db = GRTAGS; goto finish; } } else { /* Compact format */ int n, cur, last = 0; do { if (!isdigit(*p)) die("Impossible! decide_tag_by_context(2)"); NEXT_NUMBER(p); cur = last + n; if (cur == lineno) { db = GRTAGS; goto finish; } last = cur; if (*p == '-') { if (!isdigit(*++p)) die("Impossible! decide_tag_by_context(3)"); NEXT_NUMBER(p); cur = last + n; if (lineno >= last && lineno <= cur) { db = GRTAGS; goto finish; } last = cur; } if (*p) { if (*p == ',') p++; else die("Impossible! decide_tag_by_context(4)"); } } while (*p); } } } } finish: dbop_close(dbop); if (db == GSYMS && getenv("GTAGSLIBPATH")) { char libdbpath[MAXPATHLEN]; char *libdir = NULL, *nextp = NULL; sb = strbuf_open(0); strbuf_puts(sb, getenv("GTAGSLIBPATH")); for (libdir = strbuf_value(sb); libdir; libdir = nextp) { if ((nextp = locatestring(libdir, PATHSEP, MATCH_FIRST)) != NULL) *nextp++ = 0; if (!gtagsexist(libdir, libdbpath, sizeof(libdbpath), 0)) continue; if (!strcmp(dbpath, libdbpath)) continue; dbop = dbop_open(makepath(libdbpath, dbname(GTAGS), NULL), 0, 0, 0); if (dbop == NULL) continue; tagline = dbop_first(dbop, tag, NULL, 0); dbop_close(dbop); if (tagline != NULL) { db = GTAGS; break; } } strbuf_close(sb); } return db; }
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: @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"); } }