/** * args_read: read path From args. * * @return path (@VAR{NULL}: end of argument) */ const char * args_read(void) { const char *p; STATIC_STRBUF(sb); strbuf_clear(sb); switch (type) { case ARGS_NOP: p = NULL; break; case ARGS_ARGS: p = *argslist++; break; case ARGS_FILELIST: p = strbuf_fgets(sb, ip, STRBUF_NOCRLF); break; case ARGS_GFIND: p = gfind_read(gp); break; case ARGS_BOTH: if (*argslist != NULL) p = *argslist++; else p = strbuf_fgets(sb, ip, STRBUF_NOCRLF); break; default: die("args_read: invalid type."); } return p; }
static void load_notfunction(const char *filename) { FILE *ip; STRBUF *sb = strbuf_open(0); STRBUF *ib = strbuf_open(0); char *p; int i; if ((ip = fopen(filename, "r")) == NULL) die("'%s' cannot read.", filename); for (tablesize = 0; (p = strbuf_fgets(ib, ip, STRBUF_NOCRLF)) != NULL; tablesize++) strbuf_puts0(sb, p); fclose(ip); words = (struct words *)check_malloc(sizeof(struct words) * tablesize); /* * Don't free *p. */ p = (char *)check_malloc(strbuf_getlen(sb) + 1); memcpy(p, strbuf_value(sb), strbuf_getlen(sb) + 1); for (i = 0; i < tablesize; i++) { words[i].name = p; p += strlen(p) + 1; } qsort(words, tablesize, sizeof(struct words), cmp); strbuf_close(sb); strbuf_close(ib); }
/* * getdefinitionURL: get URL includes specified definition. * * i) arg definition name * i) htmldir HTML directory * o) URL URL begin with 'file:' */ void getdefinitionURL(const char *arg, const char *htmldir, STRBUF *URL) { FILE *fp; char *p; SPLIT ptable; int status = -1; STRBUF *sb = strbuf_open(0); const char *path = makepath(htmldir, "MAP", NULL); if (!test("f", path)) die("'%s' not found. Please invoke htags(1) with the --map-file option.", path); fp = fopen(path, "r"); if (!fp) die("cannot open '%s'.", path); while ((p = strbuf_fgets(sb, fp, STRBUF_NOCRLF)) != NULL) { if (split(p, 2, &ptable) != 2) die("illegal format."); if (!strcmp(arg, ptable.part[0].start)) { status = 0; break; } } fclose(fp); if (status == -1) die("definition %s not found.", arg); strbuf_reset(URL); /* * convert path into URL. */ makefileurl(makepath(htmldir, ptable.part[1].start, NULL), 0, URL); recover(&ptable); strbuf_close(sb); }
/* * load_alias: load alias value. * * [$HOME/.gozillarc] * +----------------------- * |a:http://www.gnu.org * |f = file:/usr/share/xxx.html * |www http://www.xxx.yyy/ */ static void load_alias(void) { FILE *ip; STRBUF *sb = strbuf_open(0); char *p; int flag = STRBUF_NOCRLF; struct sh_entry *ent; sh = strhash_open(10); if (!(p = get_home_directory())) goto end; if (!test("r", makepath(p, gozillarc, NULL))) #ifdef __DJGPP__ if (!test("r", makepath(p, dos_gozillarc, NULL))) #endif goto end; if (!(ip = fopen(makepath(p, gozillarc, NULL), "r"))) #ifdef __DJGPP__ if (!(ip = fopen(makepath(p, dos_gozillarc, NULL), "r"))) #endif goto end; while ((p = strbuf_fgets(sb, ip, flag)) != NULL) { char *name, *value; flag &= ~STRBUF_APPEND; if (*p == '#') continue; if (strbuf_unputc(sb, '\\')) { flag |= STRBUF_APPEND; continue; } while (*p && isblank(*p)) /* skip spaces */ p++; name = p; while (*p && isalnum(*p)) /* get name */ p++; *p++ = 0; while (*p && isblank(*p)) /* skip spaces */ p++; if (*p == '=' || *p == ':') { p++; while (*p && isblank(*p))/* skip spaces */ p++; } value = p; while (*p && !isblank(*p)) /* get value */ p++; *p = 0; ent = strhash_assign(sh, name, 1); if (ent->value) (void)free(ent->value); ent->value = check_strdup(value); } fclose(ip); end: strbuf_close(sb); }
/** * dbop_close: close db * * @param[in] dbop dbop descripter */ void dbop_close(DBOP *dbop) { DB *db = dbop->db; /* * Load sorted tag records and write them to the tag file. */ if (dbop->sortout != NULL) { STRBUF *sb = strbuf_open(256); char *p; /* * End of the former stage of sorted writing. * fclose() and sortout = NULL is important. * * fclose(): enables reading from sortin descriptor. * sortout = NULL: makes the following dbop_put write to the tag file directly. */ fclose(dbop->sortout); dbop->sortout = NULL; /* * The last stage of sorted writing. */ while (strbuf_fgets(sb, dbop->sortin, STRBUF_NOCRLF)) { for (p = strbuf_value(sb); *p && *p != SORT_SEP; p++) ; if (!*p) die("unexpected end of record."); *p++ = '\0'; dbop_put(dbop, strbuf_value(sb), p); } fclose(dbop->sortin); strbuf_close(sb); terminate_sort_process(dbop); } #ifdef USE_SQLITE3 if (dbop->openflags & DBOP_SQLITE3) { dbop3_close(dbop); return; } #endif #ifdef USE_DB185_COMPAT (void)db->close(db); #else /* * If dbname = NULL, omit writing to the disk in __bt_close(). */ (void)db->close(db, dbop->dbname[0] == '\0' ? 1 : 0); #endif if (dbop->dbname[0] != '\0') { if (dbop->perm && chmod(dbop->dbname, dbop->perm) < 0) die("chmod(2) failed."); } (void)free(dbop); }
/** * Load file. */ void loadfile(const char *file, STRBUF *result) { STRBUF *sb = strbuf_open(0); FILE *ip = fopen(file, "r"); if (!ip) die("file '%s' not found.", file); while (strbuf_fgets(sb, ip, STRBUF_NOCRLF) != NULL) strbuf_puts_nl(result, strbuf_value(sb)); fclose(ip); strbuf_close(sb); }
/* * Read line with a prompt. * * This is part of cscope protocol. */ static char * get_line(void) { STATIC_STRBUF(sb); /* Prompt */ fputs(">> ", stdout); fflush(stdout); if (strbuf_fgets(sb, stdin, STRBUF_NOCRLF) == NULL) return NULL; return strbuf_value(sb); }
/** * xargs_read: read a record from xargs stream * * @param[in] xp xargs structure * @return result line */ char * xargs_read(XARGS *xp) { assert(xp != NULL); if (xp->end_of_arg) return NULL; if (xp->unread) { xp->unread = 0; return strbuf_value(xp->result); } if (xp->pipe && strbuf_fgets(xp->result, xp->pipe, STRBUF_NOCRLF) != NULL) { if (xp->trim_line) strbuf_trim(xp->result); return strbuf_value(xp->result); } if (xp->pipe) if (pclose(xp->pipe) != 0 && !xp->ignore_error) die("command failed in xargs_read()."); /* * Switch to the next segment. */ do { xp->pipe = execute_command(xp); if (xp->pipe && strbuf_fgets(xp->result, xp->pipe, STRBUF_NOCRLF) != NULL) { if (xp->trim_line) strbuf_trim(xp->result); return strbuf_value(xp->result); } if (xp->pipe) { if (pclose(xp->pipe) != 0 && !xp->ignore_error) die("command failed in xargs_read()."); } else { xp->end_of_arg = 1; } } while (!xp->end_of_arg); return NULL; }
/** * readrecord: read recoed indexed by label. * * @param[in] label label in config file * @return record or NULL * * Jobs: * - skip comment. * - append following line. * - format check. */ static const char * readrecord(FILE *fp, const char *label) { char *p; int flag = STRBUF_NOCRLF|STRBUF_SHARPSKIP; int count = 0; rewind(fp); while ((p = strbuf_fgets(ib, fp, flag)) != NULL) { count++; /* * ignore \<new line>. */ flag &= ~STRBUF_APPEND; if (*p == '\0') continue; if (strbuf_unputc(ib, '\\')) { flag |= STRBUF_APPEND; continue; } trim(p); for (;;) { const char *candidate; /* * pick up candidate. */ if ((candidate = strmake(p, "|:")) == NULL) die("invalid config file format (line %d).", count); if (!strcmp(label, candidate)) { if (!(p = locatestring(p, ":", MATCH_FIRST))) die("invalid config file format (line %d).", count); return check_strdup(p); } /* * locate next candidate. */ p += strlen(candidate); if (*p == ':') break; else if (*p == '|') p++; else die("invalid config file format (line %d).", count); } } /* * config line not found. */ return NULL; }
/** * completion_idutils: print completion list of specified @a prefix * * @param[in] dbpath dbpath directory * @param[in] root root directory * @param[in] prefix prefix of primary key */ void completion_idutils(const char *dbpath, const char *root, const char *prefix) { FILE *ip; STRBUF *sb = strbuf_open(0); const char *lid = usable("lid"); char *line, *p; if (prefix && *prefix == 0) /* In the case global -c '' */ prefix = NULL; /* * make lid command line. * Invoke lid with the --result=grep option to generate grep format. */ if (!lid) die("lid(idutils) not found."); strbuf_puts(sb, lid); strbuf_sprintf(sb, " --file=%s/ID", quote_shell(dbpath)); strbuf_puts(sb, " --key=token"); if (iflag) strbuf_puts(sb, " --ignore-case"); if (prefix) { strbuf_putc(sb, ' '); strbuf_putc(sb, '"'); strbuf_putc(sb, '^'); strbuf_puts(sb, prefix); strbuf_putc(sb, '"'); } if (debug) fprintf(stderr, "completion_idutils: %s\n", strbuf_value(sb)); if (chdir(root) < 0) die("cannot move to '%s' directory.", root); if (!(ip = popen(strbuf_value(sb), "r"))) die("cannot execute '%s'.", strbuf_value(sb)); while ((line = strbuf_fgets(sb, ip, STRBUF_NOCRLF)) != NULL) { for (p = line; *p && *p != ' '; p++) ; if (*p == '\0') { warning("Invalid line: %s", line); continue; } *p = '\0'; puts(line); } if (pclose(ip) != 0) die("terminated abnormally (errno = %d).", errno); strbuf_close(sb); }
/* * Make duplicate object index. * * If referred tag is only one, direct link which points the tag is generated. * Else if two or more tag exists, indirect link which points the tag list * is generated. */ int makedupindex(void) { STRBUF *sb = strbuf_open(0); STRBUF *tmp = strbuf_open(0); STRBUF *command = strbuf_open(0); int definition_count = 0; char srcdir[MAXPATHLEN]; int db; FILEOP *fileop = NULL; FILE *op = NULL; FILE *ip = NULL; snprintf(srcdir, sizeof(srcdir), "../%s", SRCS); for (db = GTAGS; db < GTAGLIM; db++) { const char *kind = kinds[db]; const char *option = options[db]; int writing = 0; int count = 0; int entry_count = 0; const char *ctags_xid, *ctags_x; char tag[IDENTLEN], prev[IDENTLEN], first_line[MAXBUFLEN]; if (gtags_exist[db] == 0) continue; prev[0] = 0; first_line[0] = 0; /* * construct command line. */ strbuf_reset(command); strbuf_sprintf(command, "%s -x%s --result=ctags-xid --encode-path=\" \t\" --nofilter=path", quote_shell(global_path), option); /* * Optimization when the --dynamic option is specified. */ if (dynamic) { strbuf_puts(command, " --nosource"); if (db != GSYMS) strbuf_puts(command, " --nofilter=sort"); } strbuf_puts(command, " \".*\""); if ((ip = popen(strbuf_value(command), "r")) == NULL) die("cannot execute command '%s'.", strbuf_value(command)); while ((ctags_xid = strbuf_fgets(sb, ip, STRBUF_NOCRLF)) != NULL) { char fid[MAXFIDLEN]; ctags_x = parse_xid(ctags_xid, fid, NULL); /* tag name */ (void)strcpy_withterm(tag, ctags_x, sizeof(tag), ' '); if (strcmp(prev, tag)) { count++; if (vflag) fprintf(stderr, " [%d] adding %s %s\n", count, kind, tag); if (writing) { if (!dynamic) { fputs_nl(gen_list_end(), op); fputs_nl(body_end, op); fputs_nl(gen_page_end(), op); close_file(fileop); html_count++; } writing = 0; /* * cache record: " <fid>\0<entry number>\0" */ strbuf_reset(tmp); strbuf_putc(tmp, ' '); strbuf_putn(tmp, count - 1); strbuf_putc(tmp, '\0'); strbuf_putn(tmp, entry_count); cache_put(db, prev, strbuf_value(tmp), strbuf_getlen(tmp) + 1); } /* single entry */ if (first_line[0]) { char fid[MAXFIDLEN]; const char *ctags_x = parse_xid(first_line, fid, NULL); const char *lno = nextelement(ctags_x); strbuf_reset(tmp); strbuf_puts_withterm(tmp, lno, ' '); strbuf_putc(tmp, '\0'); strbuf_puts(tmp, fid); cache_put(db, prev, strbuf_value(tmp), strbuf_getlen(tmp) + 1); } /* * Chop the tail of the line. It is not important. * strlimcpy(first_line, ctags_x, sizeof(first_line)); */ strncpy(first_line, ctags_xid, sizeof(first_line)); first_line[sizeof(first_line) - 1] = '\0'; strlimcpy(prev, tag, sizeof(prev)); entry_count = 0; } else { /* duplicate entry */ if (first_line[0]) { char fid[MAXFIDLEN]; const char *ctags_x = parse_xid(first_line, fid, NULL); if (!dynamic) { char path[MAXPATHLEN]; snprintf(path, sizeof(path), "%s/%s/%d.%s", distpath, dirs[db], count, HTML); fileop = open_output_file(path, cflag); op = get_descripter(fileop); fputs_nl(gen_page_begin(tag, SUBDIR), op); fputs_nl(body_begin, op); fputs_nl(gen_list_begin(), op); fputs_nl(gen_list_body(srcdir, ctags_x, fid), op); } writing = 1; entry_count++; first_line[0] = 0; } if (!dynamic) { fputs_nl(gen_list_body(srcdir, ctags_x, fid), op); } entry_count++; } } if (db == GTAGS) definition_count = count; if (pclose(ip) != 0) die("'%s' failed.", strbuf_value(command)); if (writing) { if (!dynamic) { fputs_nl(gen_list_end(), op); fputs_nl(body_end, op); fputs_nl(gen_page_end(), op); close_file(fileop); html_count++; } /* * cache record: " <fid>\0<entry number>\0" */ strbuf_reset(tmp); strbuf_putc(tmp, ' '); strbuf_putn(tmp, count); strbuf_putc(tmp, '\0'); strbuf_putn(tmp, entry_count); cache_put(db, prev, strbuf_value(tmp), strbuf_getlen(tmp) + 1); } if (first_line[0]) { char fid[MAXFIDLEN]; const char *ctags_x = parse_xid(first_line, fid, NULL); const char *lno = nextelement(ctags_x); strbuf_reset(tmp); strbuf_puts_withterm(tmp, lno, ' '); strbuf_putc(tmp, '\0'); strbuf_puts(tmp, fid); cache_put(db, prev, strbuf_value(tmp), strbuf_getlen(tmp) + 1); } } strbuf_close(sb); strbuf_close(tmp); strbuf_close(command); return definition_count; }
/* * makedefineindex: make definition index (including alphabetic index) * * @param[in] file definition index file * @param[in] total definitions total * @param[out] defines @defines * Globals used (input): * tag cache XXX: should this be global output, not input? */ int makedefineindex(const char *file, int total, STRBUF *defines) { int count = 0; int alpha_count = 0; FILEOP *fileop_MAP = NULL, *fileop_DEFINES, *fileop_ALPHA = NULL; FILE *MAP = NULL; FILE *DEFINES, *STDOUT, *TAGS, *ALPHA = NULL; STRBUF *sb = strbuf_open(0); STRBUF *url = strbuf_open(0); /* Index link */ const char *target = (Fflag) ? "mains" : "_top"; const char *indexlink; const char *index_string = "Index Page"; char command[1024], buf[1024], alpha[32], alpha_f[32], *_; if (!aflag && !Fflag) indexlink = "mains"; else if (Fflag) indexlink = "../defines"; else indexlink = "../mains"; if (map_file) { fileop_MAP = open_output_file(makepath(distpath, "MAP", NULL), 0); MAP = get_descripter(fileop_MAP); } fileop_DEFINES = open_output_file(makepath(distpath, file, NULL), 0); DEFINES = get_descripter(fileop_DEFINES); fputs_nl(gen_page_begin(title_define_index, TOPDIR), DEFINES); fputs_nl(body_begin, DEFINES); fputs(header_begin, DEFINES); if (Fflag) fputs(gen_href_begin(NULL, "defines", normal_suffix, NULL), DEFINES); fputs(title_define_index, DEFINES); if (Fflag) fputs(gen_href_end(), DEFINES); fputs_nl(header_end, DEFINES); if (!aflag && !Fflag) { fputs(gen_href_begin_with_title(NULL, indexlink, normal_suffix, NULL, index_string), DEFINES); if (Iflag) fputs(gen_image(CURRENT, back_icon, ".."), DEFINES); else fputs("[..]", DEFINES); fputs_nl(gen_href_end(), DEFINES); } if (!aflag) { if (!no_order_list) fputs_nl(list_begin, DEFINES); } /* * map DEFINES to STDOUT. */ STDOUT = DEFINES; snprintf(command, sizeof(command), PQUOTE "%s -c" PQUOTE, quote_shell(global_path)); if ((TAGS = popen(command, "r")) == NULL) die("cannot execute '%s'.", command); alpha[0] = '\0'; while ((_ = strbuf_fgets(sb, TAGS, STRBUF_NOCRLF)) != NULL) { const char *tag, *line; char guide[1024], url_for_map[1024]; count++; tag = _; message(" [%d/%d] adding %s", count, total, tag); if (aflag && (alpha[0] == '\0' || !locatestring(tag, alpha, MATCH_AT_FIRST))) { const char *msg = (alpha_count == 1) ? "definition" : "definitions"; int c; if (alpha[0]) { char tmp[128]; snprintf(tmp, sizeof(tmp), "%d %s", alpha_count, msg); strbuf_puts(defines, gen_href_begin_with_title("defines", alpha_f, HTML, NULL, tmp)); strbuf_sprintf(defines, "[%s]", alpha); strbuf_puts_nl(defines, gen_href_end()); alpha_count = 0; if (!no_order_list) fputs_nl(list_end, ALPHA); else fputs_nl(br, ALPHA); fputs(gen_href_begin_with_title(NULL, indexlink, normal_suffix, NULL, index_string), ALPHA); if (Iflag) fputs(gen_image(PARENT, back_icon, ".."), ALPHA); else fputs("[..]", ALPHA); fputs_nl(gen_href_end(), ALPHA); fputs_nl(body_end, ALPHA); fputs_nl(gen_page_end(), ALPHA); close_file(fileop_ALPHA); html_count++; } /* * setup index char (for example, 'a' of '[a]'). * alpha is used for display. * alpha_f is used for part of path. */ c = (unsigned char)*tag; if (c > 127) { int i2 = *(tag + 1) & 0xff; /* * for multi-byte(EUC) code. */ alpha[0] = *tag; alpha[1] = *(tag + 1); alpha[2] = '\0'; snprintf(alpha_f, sizeof(alpha_f), "%03d%03d", c, i2); } else if (isalpha(c) || c == '_') { alpha[0] = *tag; alpha[1] = '\0'; /* * for CD9660 or FAT file system */ if (islower(c)) { alpha_f[0] = 'l'; alpha_f[1] = *tag; alpha_f[2] = '\0'; } else { alpha_f[0] = *tag; alpha_f[1] = '\0'; } } else { alpha[0] = *tag; alpha[1] = '\0'; snprintf(alpha_f, sizeof(alpha_f), "%03d", c); } snprintf(buf, sizeof(buf), "%s/defines/%s.%s", distpath, alpha_f, HTML); fileop_ALPHA = open_output_file(buf, 0); ALPHA = get_descripter(fileop_ALPHA); snprintf(buf, sizeof(buf), "[%s]", alpha); fputs_nl(gen_page_begin(buf, SUBDIR), ALPHA); fputs_nl(body_begin, ALPHA); fprintf(ALPHA, "%s[%s]%s\n", header_begin, alpha, header_end); fputs(gen_href_begin_with_title(NULL, indexlink, normal_suffix, NULL, index_string), ALPHA); if (Iflag) fputs(gen_image(PARENT, back_icon, ".."), ALPHA); else fputs("[..]", ALPHA); fputs_nl(gen_href_end(), ALPHA); if (!no_order_list) fputs_nl(list_begin, ALPHA); else fprintf(ALPHA, "%s%s\n", br, br); STDOUT = ALPHA; } alpha_count++; /* * generating url for function definition. */ line = cache_get(GTAGS, tag); strbuf_reset(url); if (line == NULL) die("internal error in makedefineindex()."); /* * About the format of 'line', please see the head comment of cache.c. */ if (*line == ' ') { const char *fid = line + 1; const char *enumber = nextstring(fid); snprintf(url_for_map, sizeof(url_for_map), "%s/%s.%s", DEFS, fid, HTML); if (dynamic) { if (*action != '/' && aflag) strbuf_puts(url, "../"); strbuf_puts(url, action); strbuf_sprintf(url, "?pattern=%s%stype=definitions", tag, quote_amp); } else { if (aflag) strbuf_puts(url, "../"); strbuf_sprintf(url, "%s/%s.%s", DEFS, fid, HTML); } snprintf(guide, sizeof(guide), "Multiple defined in %s places.", enumber); } else { const char *lno = line; const char *fid = nextstring(line); const char *path = gpath_fid2path(fid, NULL); path += 2; /* remove './' */ snprintf(url_for_map, sizeof(url_for_map), "%s/%s.%s#L%s", SRCS, fid, HTML, lno); if (aflag) strbuf_puts(url, "../"); strbuf_sprintf(url, "%s/%s.%s#L%s", SRCS, fid, HTML, lno); snprintf(guide, sizeof(guide), "Defined at %s in %s.", lno, path); } if (!no_order_list) fputs(item_begin, STDOUT); fputs(gen_href_begin_with_title_target(NULL, strbuf_value(url), NULL, NULL, guide, target), STDOUT); fputs(tag, STDOUT); fputs(gen_href_end(), STDOUT); if (!no_order_list) fputs(item_end, STDOUT); else fputs(br, STDOUT); fputc('\n', STDOUT); if (map_file) fprintf(MAP, "%s\t%s\n", tag, url_for_map); } if (pclose(TAGS) != 0) die("terminated abnormally '%s' (errno = %d).", command, errno); if (aflag && alpha[0]) { char tmp[128]; const char *msg = (alpha_count == 1) ? "definition" : "definitions"; snprintf(tmp, sizeof(tmp), "%d %s", alpha_count, msg); strbuf_puts(defines, gen_href_begin_with_title("defines", alpha_f, HTML, NULL, tmp)); strbuf_sprintf(defines, "[%s]", alpha); strbuf_puts_nl(defines, gen_href_end()); if (!no_order_list) fputs_nl(list_end, ALPHA); else fputs_nl(br, ALPHA); fputs(gen_href_begin_with_title(NULL, indexlink, normal_suffix, NULL, index_string), ALPHA); if (Iflag) fputs(gen_image(PARENT, back_icon, ".."), ALPHA); else fputs("[..]", ALPHA); fputs_nl(gen_href_end(), ALPHA); fputs_nl(body_end, ALPHA); fputs_nl(gen_page_end(), ALPHA); close_file(fileop_ALPHA); html_count++; fputs(strbuf_value(defines), DEFINES); } if (!no_order_list && !aflag) fputs_nl(list_end, DEFINES); if (!aflag && !Fflag) { fputs(gen_href_begin_with_title(NULL, "mains", normal_suffix, NULL, index_string), DEFINES); if (Iflag) fputs(gen_image(CURRENT, back_icon, ".."), DEFINES); else fputs("[..]", DEFINES); fputs_nl(gen_href_end(), DEFINES); } fputs_nl(body_end, DEFINES); fputs_nl(gen_page_end(), DEFINES); close_file(fileop_DEFINES); html_count++; if (map_file) close_file(fileop_MAP); strbuf_close(sb); strbuf_close(url); return count; }
int main(int argc, char **argv) { const char *av = NULL; int db; int optchar; int option_index = 0; int status = 0; /* * get path of following directories. * o current directory * o root of source tree * o dbpath directory * * if GTAGS not found, exit with an error message. */ status = setupdbpath(0); if (status == 0) { cwd = get_cwd(); root = get_root(); dbpath = get_dbpath(); } /* * Setup GTAGSCONF and GTAGSLABEL environment variable * according to the --gtagsconf and --gtagslabel option. */ preparse_options(argc, argv); /* * Open configuration file. */ openconf(root); setenv_from_config(); logging_arguments(argc, argv); while ((optchar = getopt_long(argc, argv, "acde:EifFgGIlL:MnoOpPqrsS:tTuvVx", long_options, &option_index)) != EOF) { switch (optchar) { case 0: break; case 'a': aflag++; break; case 'c': cflag++; setcom(optchar); break; case 'd': dflag++; break; case 'e': av = optarg; break; case 'E': Gflag = 0; break; case 'f': fflag++; xflag++; setcom(optchar); break; case 'F': Tflag = 0; break; case 'g': gflag++; setcom(optchar); break; case 'G': Gflag++; break; case 'i': iflag++; break; case 'I': Iflag++; setcom(optchar); break; case 'l': Sflag++; scope = "."; break; case 'L': file_list = optarg; break; case 'M': Mflag++; iflag = 0; break; case 'n': nflag++; if (optarg) { if (!strcmp(optarg, "sort")) nofilter |= SORT_FILTER; else if (!strcmp(optarg, "path")) nofilter |= PATH_FILTER; } else { nofilter = BOTH_FILTER; } break; case 'o': oflag++; break; case 'O': Oflag++; break; case 'p': pflag++; setcom(optchar); break; case 'P': Pflag++; setcom(optchar); break; case 'q': qflag++; setquiet(); break; case 'r': rflag++; break; case 's': sflag++; break; case 'S': Sflag++; scope = optarg; break; case 't': tflag++; break; case 'T': Tflag++; break; case 'u': uflag++; setcom(optchar); break; case 'v': vflag++; setverbose(); break; case 'V': Vflag++; break; case 'x': xflag++; break; case OPT_USE_COLOR: if (optarg) { if (!strcmp(optarg, "never")) use_color = 0; else if (!strcmp(optarg, "always")) use_color = 1; else if (!strcmp(optarg, "auto")) use_color = 2; else die_with_code(2, "unknown color type."); } else { use_color = 2; } break; case OPT_ENCODE_PATH: encode_chars = optarg; break; case OPT_FROM_HERE: { char *p = optarg; const char *usage = "usage: global --from-here=lineno:path."; context_lineno = p; while (*p && isdigit(*p)) p++; if (*p != ':') die_with_code(2, usage); *p++ = '\0'; if (!*p) die_with_code(2, usage); context_file = p; } break; case OPT_GTAGSCONF: case OPT_GTAGSLABEL: /* These options are already parsed in preparse_options() */ break; case OPT_MATCH_PART: if (!strcmp(optarg, "first")) match_part = MATCH_PART_FIRST; else if (!strcmp(optarg, "last")) match_part = MATCH_PART_LAST; else if (!strcmp(optarg, "all")) match_part = MATCH_PART_ALL; else die_with_code(2, "unknown part type for the --match-part option."); break; case OPT_PATH_CONVERT: do_path = 1; if (!strcmp("absolute", optarg)) convert_type = PATH_ABSOLUTE; else if (!strcmp("relative", optarg)) convert_type = PATH_RELATIVE; else if (!strcmp("through", optarg)) convert_type = PATH_THROUGH; else die("Unknown path type."); break; case OPT_PATH_STYLE: path_style = optarg; break; case OPT_RESULT: if (!strcmp(optarg, "ctags-x")) format = FORMAT_CTAGS_X; else if (!strcmp(optarg, "ctags-xid")) format = FORMAT_CTAGS_XID; else if (!strcmp(optarg, "ctags")) format = FORMAT_CTAGS; else if (!strcmp(optarg, "ctags-mod")) format = FORMAT_CTAGS_MOD; else if (!strcmp(optarg, "path")) format = FORMAT_PATH; else if (!strcmp(optarg, "grep")) format = FORMAT_GREP; else if (!strcmp(optarg, "cscope")) format = FORMAT_CSCOPE; else die_with_code(2, "unknown format type for the --result option."); break; case OPT_SINGLE_UPDATE: single_update = optarg; break; default: usage(); break; } } if (qflag) vflag = 0; if (show_version) version(av, vflag); if (show_help) help(); if (dbpath == NULL) die_with_code(-status, gtags_dbpath_error); /* * decide format. * The --result option is given to priority more than the -t and -x option. */ if (format == 0) { if (tflag) { /* ctags format */ format = FORMAT_CTAGS; } else if (xflag) { /* print details */ format = FORMAT_CTAGS_X; } else { /* print just a file name */ format = FORMAT_PATH; } } /* * GTAGSBLANKENCODE will be used in less(1). */ switch (format) { case FORMAT_CTAGS_X: case FORMAT_CTAGS_XID: if (encode_chars == NULL && getenv("GTAGSBLANKENCODE")) encode_chars = " \t"; break; } if (encode_chars) { if (strlen(encode_chars) > 255) die("too many encode chars."); if (strchr(encode_chars, '/') || strchr(encode_chars, '.')) warning("cannot encode '/' and '.' in the path. Ignored."); set_encode_chars((unsigned char *)encode_chars); } if (getenv("GTAGSTHROUGH")) Tflag++; if (use_color) { #if defined(_WIN32) && !defined(__CYGWIN__) if (!(getenv("ANSICON") || LoadLibrary("ANSI32.dll")) && use_color == 2) use_color = 0; #endif if (use_color == 2 && !isatty(1)) use_color = 0; if (Vflag) use_color = 0; } argc -= optind; argv += optind; /* * Path filter */ if (do_path) { /* * This code is needed for globash.rc. * This code extract path name from tag line and * replace it with the relative or the absolute path name. * * By default, if we are in src/ directory, the output * should be converted like follows: * * main 10 ./src/main.c main(argc, argv)\n * main 22 ./libc/func.c main(argc, argv)\n * v * main 10 main.c main(argc, argv)\n * main 22 ../libc/func.c main(argc, argv)\n * * Similarly, the --path-convert=absolute option specified, then * v * main 10 /prj/xxx/src/main.c main(argc, argv)\n * main 22 /prj/xxx/libc/func.c main(argc, argv)\n */ STRBUF *ib = strbuf_open(MAXBUFLEN); CONVERT *cv; char *ctags_x; if (argc < 3) die("global --path-convert: 3 arguments needed."); cv = convert_open(convert_type, FORMAT_CTAGS_X, argv[0], argv[1], argv[2], stdout, NOTAGS); while ((ctags_x = strbuf_fgets(ib, stdin, STRBUF_NOCRLF)) != NULL) convert_put(cv, ctags_x); convert_close(cv); strbuf_close(ib); exit(0); } /* * At first, we pickup pattern from -e option. If it is not found * then use argument which is not option. */ if (!av) { av = *argv; /* * global -g pattern [files ...] * av argv */ if (gflag && av) argv++; } if (single_update) { if (command == 0) { uflag++; command = 'u'; } else if (command != 'u') { ; /* ignored */ } } /* * only -c, -u, -P and -p allows no argument. */ if (!av) { switch (command) { case 'c': case 'u': case 'p': case 'P': break; case 'f': if (file_list) break; default: usage(); break; } } /* * -u and -p cannot have any arguments. */ if (av) { switch (command) { case 'u': case 'p': usage(); default: break; } } if (tflag) xflag = 0; if (nflag > 1) nosource = 1; /* to keep compatibility */ if (print0) set_print0(); if (cflag && match_part == 0) match_part = MATCH_PART_ALL; /* * remove leading blanks. */ if (!Iflag && !gflag && av) for (; *av == ' ' || *av == '\t'; av++) ; if (cflag && !Pflag && av && isregex(av)) die_with_code(2, "only name char is allowed with -c option."); /* * print dbpath or rootdir. */ if (pflag) { fprintf(stdout, "%s\n", (rflag) ? root : dbpath); exit(0); } /* * incremental update of tag files. */ if (uflag) { STRBUF *sb = strbuf_open(0); char *gtags_path = usable("gtags"); if (!gtags_path) die("gtags command not found."); if (chdir(root) < 0) die("cannot change directory to '%s'.", root); #if defined(_WIN32) && !defined(__CYGWIN__) /* * Get around CMD.EXE's weird quoting rules by sticking another * perceived whitespace in front (also works with Take Command). */ strbuf_putc(sb, ';'); #endif strbuf_puts(sb, quote_shell(gtags_path)); strbuf_puts(sb, " -i"); if (vflag) strbuf_puts(sb, " -v"); if (single_update) { if (!isabspath(single_update)) { static char regular_path_name[MAXPATHLEN]; if (rel2abs(single_update, cwd, regular_path_name, sizeof(regular_path_name)) == NULL) die("rel2abs failed."); single_update = regular_path_name; } strbuf_puts(sb, " --single-update "); strbuf_puts(sb, quote_shell(single_update)); } strbuf_putc(sb, ' '); strbuf_puts(sb, quote_shell(dbpath)); if (system(strbuf_value(sb))) exit(1); strbuf_close(sb); exit(0); } /* * decide tag type. */ if (context_file) { if (isregex(av)) die_with_code(2, "regular expression is not allowed with the --from-here option."); db = decide_tag_by_context(av, context_file, atoi(context_lineno)); } else { if (dflag) db = GTAGS; else if (rflag && sflag) db = GRTAGS + GSYMS; else db = (rflag) ? GRTAGS : ((sflag) ? GSYMS : GTAGS); } /* * complete function name */ if (cflag) { if (Iflag) completion_idutils(dbpath, root, av); else if (Pflag) completion_path(dbpath, av); else completion(dbpath, root, av, db); exit(0); } /* * make local prefix. * local prefix must starts with './' and ends with '/'. */ if (Sflag) { STRBUF *sb = strbuf_open(0); static char buf[MAXPATHLEN]; const char *path = scope; /* * normalize the path of scope directory. */ if (!test("d", path)) die("'%s' not found or not a directory.", scope); if (!isabspath(path)) path = makepath(cwd, path, NULL); if (realpath(path, buf) == NULL) die("cannot get real path of '%s'.", scope); if (!in_the_project(buf)) die("'%s' is out of the source project.", scope); scope = buf; /* * make local prefix. */ strbuf_putc(sb, '.'); if (strcmp(root, scope) != 0) { const char *p = scope + strlen(root); if (*p != '/') strbuf_putc(sb, '/'); strbuf_puts(sb, p); } strbuf_putc(sb, '/'); localprefix = check_strdup(strbuf_value(sb)); strbuf_close(sb); #ifdef DEBUG fprintf(stderr, "root=%s\n", root); fprintf(stderr, "cwd=%s\n", cwd); fprintf(stderr, "localprefix=%s\n", localprefix); #endif } /* * convert the file-list path into an absolute path. */ if (file_list && strcmp(file_list, "-") && !isabspath(file_list)) { static char buf[MAXPATHLEN]; if (realpath(file_list, buf) == NULL) die("'%s' not found.", file_list); file_list = buf; } /* * decide path conversion type. */ if (nofilter & PATH_FILTER) type = PATH_THROUGH; else if (aflag) type = PATH_ABSOLUTE; else type = PATH_RELATIVE; if (path_style) { if (!strcmp(path_style, "relative")) type = PATH_RELATIVE; else if (!strcmp(path_style, "absolute")) type = PATH_ABSOLUTE; else if (!strcmp(path_style, "through")) type = PATH_THROUGH; else if (!strcmp(path_style, "shorter")) type = PATH_SHORTER; else if (!strcmp(path_style, "abslib")) { type = PATH_RELATIVE; abslib++; } else die("invalid path style."); } /* * exec lid(idutils). */ if (Iflag) { chdir(root); idutils(av, dbpath); } /* * search pattern (regular expression). */ else if (gflag) { chdir(root); grep(av, argv, dbpath); } /* * locate paths including the pattern. */ else if (Pflag) { chdir(root); pathlist(av, dbpath); } /* * parse source files. */ else if (fflag) { chdir(root); parsefile(argv, cwd, root, dbpath, db); } /* * tag search. */ else { tagsearch(av, cwd, root, dbpath, db); } return 0; }
/* * makecflowindex: make call-tree based on cflow's output * * i) output output file name * i) cflow_file input file which is the output of Cflow with --format=posix */ int makecflowindex(const char *output, const char *cflow_file) { STRBUF *input = strbuf_open(0); FILE *ip, *op; char *cflow_posix, *p; const char *m0 = "Gave up making call-tree because of illegal POSIX cflow format."; const char *m1 = ""; const char *title = locatestring(output, "callee", MATCH_AT_FIRST) ? title_callee_tree : title_call_tree; int line = 0; int status = 0; #define ERROR do { warning("%s\n%s:%d %s.", m0, cflow_file, line, m1); status = -1; goto finish; } while(0) /* * If syntax error occured then stop the jobs and return error status. * Don't die() because htags has already done a lot of work. */ if ((ip = fopen(cflow_file, "r")) == NULL) { warning("cannot open cflow file '%s'.", cflow_file); return -1; } if ((op = fopen(makepath(distpath, output, NULL), "w")) == NULL) { warning("cannot create file '%s'.", output); fclose(ip); return -1; } fputs_nl(gen_page_begin(title, TOPDIR), op); fputs_nl(body_begin, op); fputs(header_begin, op); fputs(gen_href_begin_simple(output), op); /* fputs(gen_href_begin(NULL, "cflow", normal_suffix, NULL), op);*/ fputs(title, op); fputs(gen_href_end(), op); fputs_nl(header_end, op); fputs_nl(verbatim_begin, op); /* * Cflow's output format (with the --format=posix) * +---------------------------------------------------------------- * | 25 isregex: int (const char *s), <libutil/char.c 54>... * | 32 func: 10 * +---------------------------------------------------------------- * cflow_posix * v * 25 isregex: int ... , <libutil/char.c 54>... * ^ ^ ^ ^ * anchor name path lineno * * cflow_posix * v * 32 func: 10 * ^ ^ ^ * anchor name lineno */ while ((cflow_posix = strbuf_fgets(input, ip, STRBUF_NOCRLF)) != NULL) { char *anchor, *name, *path, *lineno; char *anchor_end, *name_end, *path_end, *lineno_end; anchor = name = path = lineno = anchor_end = name_end = path_end = lineno_end = NULL; line++; for (p = cflow_posix; *p && isspace(*p); p++) ; m1 = "line number at the head not found"; if (!*p || !isdigit(*p)) ERROR; anchor = p; /* anchor */ for (; *p && isdigit(*p); p++) ; if (!*p || !isspace(*p)) ERROR; anchor_end = p; /* seek to the function name */ for (; *p; p++) { /* skip special characters of HTML like '├'*/ if (*p == '&') { for (p++; *p && *p != ';'; p++) ; if (*p != ';') ERROR; } else if (isalpha(*p) || *p == '_') break; } m1 = "function name not found"; if (!*p || !isalpha(*p)) ERROR; name = p; /* name */ for (; *p && *p != ':'; p++) ; if (*p != ':') ERROR; name_end = p++; if (*p++ != ' ') ERROR; if (isdigit(*p)) { /* (1) name: 999 */ lineno = p; /* lineno */ for (; *p && isdigit(*p); p++) ; lineno_end = p; } else if (*p == '<' && *(p + 1) == '>') { /* (2) name: <> */ ; } else { /* (3) name: ... <path lineno> */ m1 = "<path lineno> not found"; for (; *p && *p != '<'; p++) ; if (!*p++) ERROR; path = p; m1 = "path not found"; for (; *p && !isspace(*p); p++) if (*p == '>') ERROR; if (!*p || *p != ' ') ERROR; path_end = p++; m1 = "lineno not found"; if (!isdigit(*p)) ERROR; lineno = p; for (; *p && isdigit(*p); p++) ; if (*p != '>') ERROR; lineno_end = p; } /* * print anchor */ fprintf(op, gen_name_number(atoi(anchor))); /* * print until name */ fwrite(cflow_posix, name - cflow_posix, 1, op); /* * print name */ if (path) { const char *fid = NULL; int path_save = *path_end; int lineno_save = *lineno_end; *path_end = *lineno_end = 0; if (test("f", path) && (fid = path2fid_readonly(path)) != NULL) fprintf(op, gen_href_begin(SRCS, fid, HTML, lineno)); else path = lineno = NULL; /* not to print </a> */ *path_end = path_save; *lineno_end = lineno_save; } else if (lineno) { int lineno_save = *lineno_end; *lineno_end = 0; fprintf(op, gen_href_begin(NULL, NULL, NULL, lineno)); *lineno_end = lineno_save; } fwrite(name, name_end - name, 1, op); if (path || lineno) fputs(gen_href_end(), op); /* * print the rest */ for (p = name_end; *p; p++) { if (*p == '<') fputs(quote_little, op); else if (*p == '>') fputs(quote_great, op); else fputc(*p, op); } fputc('\n', op); } finish: fputs_nl(verbatim_end, op); fputs_nl(body_end, op); fputs_nl(gen_page_end(), op); strbuf_close(input); fclose(ip); fclose(op); return status; }
/* * Execute global(1) and write the output to the 'sb' string buffer. * * o) sb output * i) com cscope command (0-8) * i) opt option for global(1) * i) arg argument for global(1) * r) number of output */ static int execute_command(STRBUF *sb, const int com, const int opt, const char *arg) { #ifdef _WIN32 #define QUOTE '"' #else #define QUOTE '\'' #endif STATIC_STRBUF(command); STATIC_STRBUF(ib); FILE *ip; int count = 0; strbuf_clear(command); strbuf_puts(command, global_path); strbuf_puts(command, " --result=cscope"); if (opt == FROM_HERE) { strbuf_puts(command, " --from-here="); strbuf_puts(command, context); } else if (opt) { strbuf_puts(command, " -"); strbuf_putc(command, opt); } if (ignore_case) strbuf_puts(command, " --ignore-case"); strbuf_putc(command, ' '); strbuf_putc(command, QUOTE); strbuf_puts(command, arg); strbuf_putc(command, QUOTE); if (!(ip = popen(strbuf_value(command), "r"))) die("cannot execute '%s'.", strbuf_value(command)); if (vflag) fprintf(stderr, "gscope: %s\n", strbuf_value(command)); /* * Copy records with little modification. */ strbuf_clear(ib); while (strbuf_fgets(ib, ip, 0)) { count++; if (opt == 0) { strbuf_puts(sb, strbuf_value(ib)); } else { char *p = strbuf_value(ib); /* path name */ while (*p && *p != ' ') strbuf_putc(sb, *p++); if (*p != ' ') die("illegal cscope format. (%s)", strbuf_value(ib)); strbuf_putc(sb, *p++); /* replace pattern with "<unknown>" or "<global>" */ while (*p && *p != ' ') p++; if (*p != ' ') die("illegal cscope format. (%s)", strbuf_value(ib)); if (com == '8') strbuf_puts(sb, "<global>"); else strbuf_puts(sb, "<unknown>"); strbuf_putc(sb, *p++); /* line number */ while (*p && *p != ' ') strbuf_putc(sb, *p++); if (*p != ' ') die("illegal cscope format. (%s)", strbuf_value(ib)); strbuf_putc(sb, *p++); /* line image */ if (*p == '\n') strbuf_puts(sb, "<unknown>\n"); else strbuf_puts(sb, p); } } if (pclose(ip) < 0) die("terminated abnormally."); return count; }
/** * makecommonpart: make a common part for "mains.html" and "index.html" * * @param[in] title * @param[in] defines * @param[in] files * @return index common part */ static char * makecommonpart(const char *title, const char *defines, const char *files) { FILE *ip; STRBUF *sb = strbuf_open(0); STRBUF *ib = strbuf_open(0); char buf[MAXFILLEN]; const char *tips = "Go to the GLOBAL project page."; const char *_, *item; strbuf_puts(sb, title_begin); strbuf_puts(sb, title); strbuf_puts_nl(sb, title_end); strbuf_puts_nl(sb, poweredby_begin); strbuf_sprintf(sb, "Last updated %s%s\n", now(), br); if (Iflag) { snprintf(buf, sizeof(buf), "Powered by GLOBAL-%s.", get_version()); strbuf_puts(sb, gen_href_begin_with_title_target(NULL, www, NULL, NULL, tips,"_top")); strbuf_puts(sb, gen_image(CURRENT, "pglobe", buf)); strbuf_puts(sb, gen_href_end()); strbuf_puts(sb, br); } else { strbuf_sprintf(sb, "Powered by %sGLOBAL-%s%s.%s\n", gen_href_begin_with_title_target(NULL, www, NULL, NULL, tips, "_top"), get_version(), gen_href_end(), br); } strbuf_puts_nl(sb, poweredby_end); strbuf_puts_nl(sb, hr); /* * Print items according to the value of variable 'item_order'. */ for (item = item_order; *item; item++) { switch (*item) { case 'c': if (caution) { strbuf_puts_nl(sb, caution_begin); strbuf_sprintf(sb, "<font size='+2' color='red'>CAUTION</font>%s\n", br); strbuf_sprintf(sb, "This hypertext consists of %d files.\n", html_count); strbuf_puts_nl(sb, "Please don't download the whole hypertext using a hypertext copy tool."); strbuf_puts_nl(sb, "Our network cannot afford such traffic."); strbuf_puts_nl(sb, "Instead, you can generate the same thing in your computer using"); strbuf_puts(sb, gen_href_begin_with_title_target(NULL, www, NULL, NULL, NULL, "_top")); strbuf_puts(sb, "GLOBAL source code tag system"); strbuf_puts_nl(sb, gen_href_end()); strbuf_puts_nl(sb, "Thank you."); strbuf_puts_nl(sb, caution_end); strbuf_sprintf(sb, "\n%s\n", hr); } break; case 's': if (fflag) { strbuf_puts(sb, makesearchpart(NULL)); strbuf_puts_nl(sb, hr); } break; case 't': if (call_file || callee_file) { strbuf_puts(sb, header_begin); if (call_file) { strbuf_puts(sb, gen_href_begin(NULL, "call", normal_suffix, NULL)); strbuf_puts(sb, title_call_tree); strbuf_puts(sb, gen_href_end()); } if (call_file && callee_file) strbuf_puts(sb, " / "); if (callee_file) { strbuf_puts(sb, gen_href_begin(NULL, "callee", normal_suffix, NULL)); strbuf_puts(sb, title_callee_tree); strbuf_puts(sb, gen_href_end()); } strbuf_puts_nl(sb, header_end); strbuf_puts_nl(sb, hr); } break; case 'm': strbuf_sprintf(sb, "%sMAINS%s\n", header_begin, header_end); snprintf(buf, sizeof(buf), PQUOTE "%s --result=ctags-xid --encode-path=\" \t\" --nofilter=path %s" PQUOTE, quote_shell(global_path), main_func); ip = popen(buf, "r"); if (!ip) die("cannot execute '%s'.", buf); strbuf_puts_nl(sb, gen_list_begin()); while ((_ = strbuf_fgets(ib, ip, STRBUF_NOCRLF)) != NULL) { char fid[MAXFIDLEN]; const char *ctags_x = parse_xid(_, fid, NULL); strbuf_puts_nl(sb, gen_list_body(SRCS, ctags_x, fid)); } strbuf_puts_nl(sb, gen_list_end()); if (pclose(ip) != 0) die("terminated abnormally '%s' (errno = %d).", buf, errno); strbuf_puts_nl(sb, hr); break; case 'd': if (aflag && !Fflag) { strbuf_puts(sb, header_begin); strbuf_puts(sb, title_define_index); strbuf_puts_nl(sb, header_end); strbuf_puts(sb, defines); } else { strbuf_puts(sb, header_begin); strbuf_puts(sb, gen_href_begin(NULL, "defines", normal_suffix, NULL)); strbuf_puts(sb, title_define_index); strbuf_puts(sb, gen_href_end()); strbuf_puts_nl(sb, header_end); } strbuf_puts_nl(sb, hr); break; case 'f': if (Fflag) { strbuf_puts(sb, header_begin); strbuf_puts(sb, gen_href_begin(NULL, "files", normal_suffix, NULL)); strbuf_puts(sb, title_file_index); strbuf_puts(sb, gen_href_end()); strbuf_puts_nl(sb, header_end); } else { strbuf_puts(sb, header_begin); strbuf_puts(sb, title_file_index); strbuf_puts_nl(sb, header_end); if (tree_view) { strbuf_puts_nl(sb, tree_control); strbuf_puts_nl(sb, tree_loading); if (tree_view_type) { strbuf_sprintf(sb, tree_begin_using, tree_view_type); strbuf_putc(sb, '\n'); } else { strbuf_puts_nl(sb, tree_begin); } } else if (table_flist) strbuf_puts_nl(sb, flist_begin); else if (!no_order_list) strbuf_puts_nl(sb, list_begin); strbuf_puts(sb, files); if (tree_view) strbuf_puts_nl(sb, tree_end); else if (table_flist) strbuf_puts_nl(sb, flist_end); else if (!no_order_list) strbuf_puts_nl(sb, list_end); else strbuf_puts_nl(sb, br); } strbuf_puts_nl(sb, hr); break; default: warning("unknown item '%c'. (Ignored)", *item); break; } } strbuf_close(ib); return strbuf_value(sb); /* doesn't close string buffer */ }
/** * execute_command * * @param[in] xp xargs structure * @return !=NULL: file pointer <br> * ==NULL: end of argument * * This function constructs command line from the following, <br> * @STRONG{command}: @CODE{xp-\>command} <br> * @STRONG{argument}: each argument provider <br> * execute it on a pipe line, and return the file pointer. */ static FILE * execute_command(XARGS *xp) { int limit; STRBUF *comline = strbuf_open(0); int count = 0; int length; FILE *pipe = NULL; char *p, *meta_p; #if defined(_WIN32) && !defined(__CYGWIN__) /* * If the command starts with a quote, CMD.EXE requires the entire * command line to be quoted. */ if (*xp->command == '"') strbuf_putc(comline, '"'); #endif /* * Copy the part before '%s' of the command skeleton. * The '%s' in the skeleton is replaced with given arguments. */ meta_p = locatestring(xp->command, "%s", MATCH_FIRST); if (meta_p) { strbuf_nputs(comline, xp->command, meta_p - xp->command); limit = exec_line_limit(strlen(meta_p + 2)); } else { strbuf_puts(comline, xp->command); limit = exec_line_limit(0); } /* * Append arguments as many as possible. */ switch (xp->type) { case XARGS_FILE: for ( /* initial */ fseek(xp->ip, xp->fptr, SEEK_SET) ; /* continuation condition */ (LT_MAX && ((p = (strbuf_getlen(xp->path) > 0 ? strbuf_value(xp->path) : strbuf_fgets(xp->path, xp->ip, STRBUF_NOCRLF))) != NULL)) ; /* preparation */ strbuf_reset(xp->path) ) APPEND_ARGUMENT(p); xp->fptr = ftell(xp->ip); break; case XARGS_ARGV: for (; LT_MAX && xp->argc > 0; xp->argc--, xp->argv++) APPEND_ARGUMENT(xp->argv[0]) break; case XARGS_STRBUF: for (; LT_MAX && xp->curp < xp->endp; xp->curp += length + 1) APPEND_ARGUMENT(xp->curp) break; case XARGS_FIND: for (; LT_MAX && (p = repeat_find_read()) != NULL; repeat_find_next()) APPEND_ARGUMENT(p) break; } /* * Copy the left part of the command skeleton. */ if (meta_p) { strbuf_putc(comline, ' '); strbuf_puts(comline, meta_p + 2); } #if defined(_WIN32) && !defined(__CYGWIN__) if (*xp->command == '"') strbuf_putc(comline, '"'); #endif if (count > 0) { pipe = popen(strbuf_value(comline), "r"); if (pipe == NULL) die("cannot execute command '%s'.", strbuf_value(comline)); } strbuf_close(comline); return pipe; }
/** * save_environment: save configuration data and arguments. */ static void save_environment(int argc, char *const *argv) { char command[MAXFILLEN]; STRBUF *sb = strbuf_open(0); STRBUF *save_c = strbuf_open(0); STRBUF *save_a = strbuf_open(0); int i; const char *p; FILE *ip; /* * save config values. */ snprintf(command, sizeof(command), PQUOTE "%s --config" PQUOTE, quote_shell(gtags_path)); if ((ip = popen(command, "r")) == NULL) die("cannot execute '%s'.", command); while (strbuf_fgets(sb, ip, STRBUF_NOCRLF) != NULL) { for (p = strbuf_value(sb); *p; p++) { if (*p == '\'') { strbuf_putc(save_c, '\''); strbuf_putc(save_c, '"'); strbuf_putc(save_c, '\''); strbuf_putc(save_c, '"'); strbuf_putc(save_c, '\''); } else strbuf_putc(save_c, *p); } } if (pclose(ip) != 0) die("terminated abnormally '%s' (errno = %d).", command, errno); strbuf_close(sb); save_config = strbuf_value(save_c); /* doesn't close string buffer for save config. */ /* strbuf_close(save_c); */ /* * save arguments. */ { char *opt_gtagsconf = "--gtagsconf"; for (i = 1; i < argc; i++) { char *blank; /* * skip --gtagsconf because it is already read * as config value. */ if ((p = locatestring(argv[i], opt_gtagsconf, MATCH_AT_FIRST))) { if (*p == '\0') i++; continue; } blank = locatestring(argv[i], " ", MATCH_FIRST); strbuf_putc(save_a, ' '); if (blank) strbuf_putc(save_a, '\''); strbuf_puts(save_a, argv[i]); if (blank) strbuf_putc(save_a, '\''); } } save_argv = strbuf_value(save_a); /* doesn't close string buffer for save arguments. */ /* strbuf_close(save_a); */ }
/* * 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"); } }
/* * find_read_filelist: read path from file * * r) path */ static char * find_read_filelist(void) { STATIC_STRBUF(ib); static char buf[MAXPATHLEN + 1]; static char *path; strbuf_clear(ib); for (;;) { path = strbuf_fgets(ib, ip, STRBUF_NOCRLF); if (path == NULL) { /* EOF */ find_eof = 1; return NULL; } if (*path == '\0') { /* skip empty line. */ continue; } /* * Lines which start with ". " are considered to be comments. */ if (*path == '.' && *(path + 1) == ' ') continue; /* * Skip the following: * o directory * o file which does not exist * o dead symbolic link */ if (!test("f", path)) { if (test("d", path)) warning("'%s' is a directory. (Ignored)", path); else warning("'%s' not found. (Ignored)", path); continue; } /* * normalize path name. * * rootdir /a/b/ * buf /a/b/c/d.c -> c/d.c -> ./c/d.c */ if (normalize(path, rootdir, cwddir, buf, sizeof(buf)) == NULL) { warning("'%s' is out of source tree. (Ignored)", path); continue; } path = buf; /* * GLOBAL cannot treat path which includes blanks. * It will be improved in the future. */ if (!allow_blank && locatestring(path, " ", MATCH_LAST)) { warning("'%s' ignored, because it includes blank.", path + 2); continue; } if (skipthisfile(path)) continue; /* * A blank at the head of path means * other than source file. */ if (regexec(suff, path, 0, 0, 0) != 0) *--path = ' '; return path; } }
/* * idutils: lid(idutils) pattern * * i) pattern POSIX regular expression * i) dbpath GTAGS directory */ void idutils(const char *pattern, const char *dbpath) { FILE *ip; CONVERT *cv; STRBUF *ib = strbuf_open(0); char encoded_pattern[IDENTLEN]; char path[MAXPATHLEN]; const char *lid; int linenum, count; char *p, *q, *grep; lid = usable("lid"); if (!lid) die("lid(idutils) not found."); /* * convert spaces into %FF format. */ encode(encoded_pattern, sizeof(encoded_pattern), pattern); /* * make lid command line. * Invoke lid with the --result=grep option to generate grep format. */ strbuf_puts(ib, lid); strbuf_sprintf(ib, " --file='%s/ID'", dbpath); strbuf_puts(ib, " --separator=newline"); if (format == FORMAT_PATH) strbuf_puts(ib, " --result=filenames --key=none"); else strbuf_puts(ib, " --result=grep"); if (iflag) strbuf_puts(ib, " --ignore-case"); strbuf_putc(ib, ' '); strbuf_puts(ib, quote_string(pattern)); if (debug) fprintf(stderr, "idutils: %s\n", strbuf_value(ib)); if (!(ip = popen(strbuf_value(ib), "r"))) die("cannot execute '%s'.", strbuf_value(ib)); cv = convert_open(type, format, root, cwd, dbpath, stdout); count = 0; strcpy(path, "./"); while ((grep = strbuf_fgets(ib, ip, STRBUF_NOCRLF)) != NULL) { q = path + 2; /* extract path name */ if (*grep == '/') die("The path in the output of lid is assumed absolute. '%s'", grep); p = grep; while (*p && *p != ':') *q++ = *p++; *q = '\0'; if ((xflag || tflag) && !*p) die("invalid lid(idutils) output format(1). '%s'", grep); p++; if (lflag) { if (!locatestring(path, localprefix, MATCH_AT_FIRST)) continue; } count++; switch (format) { case FORMAT_PATH: convert_put_path(cv, path); break; default: /* extract line number */ while (*p && isspace(*p)) p++; linenum = 0; for (linenum = 0; *p && isdigit(*p); linenum = linenum * 10 + (*p++ - '0')) ; if (*p != ':') die("invalid lid(idutils) output format(2). '%s'", grep); if (linenum <= 0) die("invalid lid(idutils) output format(3). '%s'", grep); p++; /* * print out. */ convert_put_using(cv, encoded_pattern, path, linenum, p, NULL); break; } } if (pclose(ip) < 0) die("terminated abnormally."); convert_close(cv); strbuf_close(ib); if (vflag) { print_count(count); fprintf(stderr, " (using idutils index in '%s').\n", dbpath); } }
int search(const char *pattern, const char *root, const char *cwd, const char *dbpath, int db) { CONVERT *cv; int count = 0; GTOP *gtop; GTP *gtp; int flags = 0; STRBUF *sb = NULL, *ib = NULL; char curpath[MAXPATHLEN], curtag[IDENTLEN]; FILE *fp = NULL; const char *src = ""; int lineno, last_lineno; lineno = last_lineno = 0; curpath[0] = curtag[0] = '\0'; /* * open tag file. */ gtop = gtags_open(dbpath, root, db, GTAGS_READ, 0); cv = convert_open(type, format, root, cwd, dbpath, stdout); /* * search through tag file. */ if (nofilter & SORT_FILTER) flags |= GTOP_NOSORT; if (iflag) { if (!isregex(pattern)) { sb = strbuf_open(0); strbuf_putc(sb, '^'); strbuf_puts(sb, pattern); strbuf_putc(sb, '$'); pattern = strbuf_value(sb); } flags |= GTOP_IGNORECASE; } if (Gflag) flags |= GTOP_BASICREGEX; if (format == FORMAT_PATH) flags |= GTOP_PATH; if (gtop->format & GTAGS_COMPACT) ib = strbuf_open(0); for (gtp = gtags_first(gtop, pattern, flags); gtp; gtp = gtags_next(gtop)) { if (lflag && !locatestring(gtp->path, localprefix, MATCH_AT_FIRST)) continue; if (format == FORMAT_PATH) { convert_put_path(cv, gtp->path); count++; } else if (gtop->format & GTAGS_COMPACT) { /* * Compact format: * a b * tagline = <file id> <tag name> <line no>,... */ char *p = (char *)gtp->tagline; const char *fid, *tagname; int n = 0; fid = p; while (*p != ' ') p++; *p++ = '\0'; /* a */ tagname = p; while (*p != ' ') p++; *p++ = '\0'; /* b */ /* * Reopen or rewind source file. */ if (!nosource) { if (strcmp(gtp->path, curpath) != 0) { if (curpath[0] != '\0' && fp != NULL) fclose(fp); strlimcpy(curtag, tagname, sizeof(curtag)); strlimcpy(curpath, gtp->path, sizeof(curpath)); /* * Use absolute path name to support GTAGSROOT * environment variable. */ fp = fopen(makepath(root, curpath, NULL), "r"); if (fp == NULL) warning("source file '%s' is not available.", curpath); last_lineno = lineno = 0; } else if (strcmp(gtp->tag, curtag) != 0) { strlimcpy(curtag, gtp->tag, sizeof(curtag)); if (atoi(p) < last_lineno && fp != NULL) { rewind(fp); lineno = 0; } last_lineno = 0; } } /* * Unfold compact format. */ if (!isdigit(*p)) die("illegal compact format."); if (gtop->format & GTAGS_COMPLINE) { /* * * If GTAGS_COMPLINE flag is set, each line number is expressed as * the difference from the previous line number except for the head. * Please see flush_pool() in libutil/gtagsop.c for the details. */ int last = 0, cont = 0; while (*p || cont > 0) { if (cont > 0) { n = last + 1; if (n > cont) { cont = 0; continue; } } else if (isdigit(*p)) { GET_NEXT_NUMBER(p); } else if (*p == '-') { GET_NEXT_NUMBER(p); cont = n + last; n = last + 1; } else if (*p == ',') { GET_NEXT_NUMBER(p); n += last; } if (last_lineno != n && fp) { while (lineno < n) { if (!(src = strbuf_fgets(ib, fp, STRBUF_NOCRLF))) { src = ""; fclose(fp); fp = NULL; break; } lineno++; } } if (gtop->format & GTAGS_COMPNAME) tagname = (char *)uncompress(tagname, gtp->tag); convert_put_using(cv, tagname, gtp->path, n, src, fid); count++; last_lineno = last = n; } } else { /* * In fact, when GTAGS_COMPACT is set, GTAGS_COMPLINE is allways set. * Therefore, the following code are not actually used. * However, it is left for some test. */ while (*p) { for (n = 0; isdigit(*p); p++) n = n * 10 + *p - '0'; if (*p == ',') p++; if (last_lineno == n) continue; if (last_lineno != n && fp) { while (lineno < n) { if (!(src = strbuf_fgets(ib, fp, STRBUF_NOCRLF))) { src = ""; fclose(fp); fp = NULL; break; } lineno++; } } if (gtop->format & GTAGS_COMPNAME) tagname = (char *)uncompress(tagname, gtp->tag); convert_put_using(cv, tagname, gtp->path, n, src, fid); count++; last_lineno = n; } } } else { /* * Standard format: * a b c * tagline = <file id> <tag name> <line no> <line image> */ char *p = (char *)gtp->tagline; char namebuf[IDENTLEN]; const char *fid, *tagname, *image; fid = p; while (*p != ' ') p++; *p++ = '\0'; /* a */ tagname = p; while (*p != ' ') p++; *p++ = '\0'; /* b */ if (gtop->format & GTAGS_COMPNAME) { strlimcpy(namebuf, (char *)uncompress(tagname, gtp->tag), sizeof(namebuf)); tagname = namebuf; } if (nosource) { image = " "; } else { while (*p != ' ') p++; image = p + 1; /* c + 1 */ if (gtop->format & GTAGS_COMPRESS) image = (char *)uncompress(image, gtp->tag); } convert_put_using(cv, tagname, gtp->path, gtp->lineno, image, fid); count++; } } convert_close(cv); if (sb) strbuf_close(sb); if (ib) strbuf_close(ib); if (fp) fclose(fp); gtags_close(gtop); return count; }
/** * 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"); } }
/*----------------------------------------------------------------------*/ void makeincludeindex(void) { FILE *PIPE; STRBUF *input = strbuf_open(0); char *ctags_x; struct data *inc; char *target = (Fflag) ? "mains" : "_top"; char command[MAXFILLEN]; /* * Pick up include pattern. * * C: #include "xxx.h" * PHP: include("xxx.inc.php"); */ /* * Unlike Perl regular expression, POSIX regular expression doesn't support C-style escape sequence. * Therefore, we can not use "\\t" here. */ snprintf(command, sizeof(command), PQUOTE "%s -gnx --encode-path=\" \t\" \"^[ \t]*(#[ \t]*(import|include)|include[ \t]*\\()\"" PQUOTE, quote_shell(global_path)); if ((PIPE = popen(command, "r")) == NULL) die("cannot execute '%s'.", command); strbuf_reset(input); while ((ctags_x = strbuf_fgets(input, PIPE, STRBUF_NOCRLF)) != NULL) { SPLIT ptable; char buf[MAXBUFLEN]; int is_php = 0; const char *last, *lang, *suffix; if (split(ctags_x, 4, &ptable) < 4) { recover(&ptable); die("too small number of parts in makefileindex()."); } if ((suffix = locatestring(ptable.part[PART_PATH].start, ".", MATCH_LAST)) != NULL && (lang = decide_lang(suffix)) != NULL && strcmp(lang, "php") == 0) is_php = 1; last = extract_lastname(ptable.part[PART_LINE].start, is_php); if (last == NULL || (inc = get_inc(last)) == NULL) continue; recover(&ptable); /* * s/^[^ \t]+/$last/; */ { const char *p; char *q = buf; for (p = last; *p; p++) *q++ = *p; for (p = ctags_x; *p && *p != ' ' && *p != '\t'; p++) ; for (; *p; p++) *q++ = *p; *q = '\0'; } put_included(inc, buf); } if (pclose(PIPE) != 0) die("terminated abnormally '%s' (errno = %d).", command, errno); for (inc = first_inc(); inc; inc = next_inc()) { const char *last = inc->name; int no = inc->id; FILEOP *fileop_INCLUDE; FILE *INCLUDE; if (inc->count > 1) { char path[MAXPATHLEN]; snprintf(path, sizeof(path), "%s/%s/%d.%s", distpath, INCS, no, HTML); fileop_INCLUDE = open_output_file(path, 0); INCLUDE = get_descripter(fileop_INCLUDE); fputs_nl(gen_page_begin(last, SUBDIR), INCLUDE); fputs_nl(body_begin, INCLUDE); fputs_nl(verbatim_begin, INCLUDE); { const char *filename = strbuf_value(inc->contents); int count = inc->count; for (; count; filename += strlen(filename) + 1, count--) { fputs(gen_href_begin_with_title_target(upperdir(SRCS), path2fid(filename), HTML, NULL, NULL, target), INCLUDE); fputs(removedotslash(filename), INCLUDE); fputs_nl(gen_href_end(), INCLUDE); } } fputs_nl(verbatim_end, INCLUDE); fputs_nl(body_end, INCLUDE); fputs_nl(gen_page_end(), INCLUDE); close_file(fileop_INCLUDE); html_count++; /* * inc->contents == NULL means that information already * written to file. */ strbuf_close(inc->contents); inc->contents = NULL; } if (!inc->ref_count) continue; if (inc->ref_count == 1) { SPLIT ptable; char buf[1024]; if (split(strbuf_value(inc->ref_contents), 4, &ptable) < 4) { recover(&ptable); die("too small number of parts in makefileindex()."); } snprintf(buf, sizeof(buf), "%s %s", ptable.part[PART_LNO].start, decode_path(ptable.part[PART_PATH].start)); recover(&ptable); strbuf_reset(inc->ref_contents); strbuf_puts(inc->ref_contents, buf); } else { char path[MAXPATHLEN]; snprintf(path, sizeof(path), "%s/%s/%d.%s", distpath, INCREFS, no, HTML); fileop_INCLUDE = open_output_file(path, 0); INCLUDE = get_descripter(fileop_INCLUDE); fputs_nl(gen_page_begin(last, SUBDIR), INCLUDE); fputs_nl(body_begin, INCLUDE); fputs_nl(gen_list_begin(), INCLUDE); { const char *line = strbuf_value(inc->ref_contents); int count = inc->ref_count; for (; count; line += strlen(line) + 1, count--) fputs_nl(gen_list_body(upperdir(SRCS), line, NULL), INCLUDE); } fputs_nl(gen_list_end(), INCLUDE); fputs_nl(body_end, INCLUDE); fputs_nl(gen_page_end(), INCLUDE); close_file(fileop_INCLUDE); html_count++; /* * inc->ref_contents == NULL means that information already * written to file. */ strbuf_close(inc->ref_contents); inc->ref_contents = NULL; } } strbuf_close(input); }
int main(int argc, char **argv) { char dbpath[MAXPATHLEN]; char cwd[MAXPATHLEN]; STRBUF *sb = strbuf_open(0); int optchar; int option_index = 0; STATISTICS_TIME *tim; while ((optchar = getopt_long(argc, argv, "cd:f:iuIn:oOqvwse", long_options, &option_index)) != EOF) { switch (optchar) { case 0: /* already flags set */ break; case OPT_CONFIG: show_config = 1; if (optarg) config_name = optarg; break; case OPT_GTAGSCONF: gtagsconf = optarg; break; case OPT_GTAGSLABEL: gtagslabel = optarg; break; case OPT_PATH: do_path = 1; if (!strcmp("absolute", optarg)) convert_type = PATH_ABSOLUTE; else if (!strcmp("relative", optarg)) convert_type = PATH_RELATIVE; else if (!strcmp("through", optarg)) convert_type = PATH_THROUGH; else die("Unknown path type."); break; case OPT_SINGLE_UPDATE: iflag++; single_update = optarg; break; case OPT_ENCODE_PATH: if (strlen(optarg) > 255) die("too many encode chars."); if (strchr(optarg, '/') || strchr(optarg, '.')) die("cannot encode '/' and '.' in the path."); set_encode_chars((unsigned char *)optarg); break; case 'c': cflag++; break; case 'd': dump_target = optarg; break; case 'f': file_list = optarg; break; case 'i': iflag++; break; case 'u': uflag++; iflag++; break; case 'I': Iflag++; break; case 'o': /* * Though the -o(--omit-gsyms) was removed, this code * is left for compatibility. */ break; case 'O': Oflag++; break; case 'q': qflag++; setquiet(); break; case 'w': wflag++; break; case 'v': vflag++; setverbose(); break; default: usage(); break; } } if (gtagsconf) { char path[MAXPATHLEN]; if (realpath(gtagsconf, path) == NULL) die("%s not found.", gtagsconf); set_env("GTAGSCONF", path); } if (gtagslabel) { set_env("GTAGSLABEL", gtagslabel); } if (qflag) vflag = 0; if (show_version) version(NULL, vflag); if (show_help) help(); argc -= optind; argv += optind; /* If dbpath is specified, -O(--objdir) option is ignored. */ if (argc > 0) Oflag = 0; if (show_config) { if (config_name) printconf(config_name); else fprintf(stdout, "%s\n", getconfline()); exit(0); } else if (do_path) { /* * This is the main body of path filter. * This code extract path name from tag line and * replace it with the relative or the absolute path name. * * By default, if we are in src/ directory, the output * should be converted like follws: * * main 10 ./src/main.c main(argc, argv)\n * main 22 ./libc/func.c main(argc, argv)\n * v * main 10 main.c main(argc, argv)\n * main 22 ../libc/func.c main(argc, argv)\n * * Similarly, the --path=absolute option specified, then * v * main 10 /prj/xxx/src/main.c main(argc, argv)\n * main 22 /prj/xxx/libc/func.c main(argc, argv)\n */ STRBUF *ib = strbuf_open(MAXBUFLEN); CONVERT *cv; char *ctags_x; if (argc < 3) die("gtags --path: 3 arguments needed."); cv = convert_open(convert_type, FORMAT_CTAGS_X, argv[0], argv[1], argv[2], stdout); while ((ctags_x = strbuf_fgets(ib, stdin, STRBUF_NOCRLF)) != NULL) convert_put(cv, ctags_x); convert_close(cv); strbuf_close(ib); exit(0); } else if (dump_target) { /* * Dump a tag file. */ DBOP *dbop = NULL; const char *dat = 0; int is_gpath = 0; char* target_file = NULL; if (!test("f", dump_target)) { target_file = strchr(dump_target, ':'); if (target_file == NULL) die("file '%s' not found", dump_target); *target_file++ = 0; //move to the next char, which starts the target file. if (!test("f", dump_target)) { die("file '%s' not found.", dump_target); } } if ((dbop = dbop_open(dump_target, 0, 0, DBOP_RAW)) == NULL) die("file '%s' is not a tag file.", dump_target); /* * The file which has a NEXTKEY record is GPATH. */ if (dbop_get(dbop, NEXTKEY)) is_gpath = 1; if (target_file && !is_gpath) { die("dump target_file can only be used with GPATH"); } if (target_file) { dat = dbop_get(dbop, target_file); if (dat == NULL) { die("target_file %s not found in GPATH", target_file); } time_t t = gpath_mtime(dbop, target_file); printf("%d\n", t); } else { for (dat = dbop_first(dbop, NULL, NULL, 0); dat != NULL; dat = dbop_next(dbop)) { const char *flag = is_gpath ? dbop_getflag(dbop) : ""; if (*flag) if (is_gpath) { time_t t = gpath_mtime(dbop, dbop->lastkey); printf("%s\t%s\t%s\t%s\n", dbop->lastkey, dat, flag, ctime(&t)); } else printf("%s\t%s\t%s\n", dbop->lastkey, dat, flag); else printf("%s\t%s\n", dbop->lastkey, dat); } } dbop_close(dbop); exit(0); } else if (Iflag) { if (!usable("mkid")) die("mkid not found."); } /* * If 'gtags.files' exists, use it as a file list. * If the file_list other than "-" is given, it must be readable file. */ if (file_list == NULL && test("f", GTAGSFILES)) file_list = GTAGSFILES; if (file_list && strcmp(file_list, "-")) { if (test("d", file_list)) die("'%s' is a directory.", file_list); else if (!test("f", file_list)) die("'%s' not found.", file_list); else if (!test("r", file_list)) die("'%s' is not readable.", file_list); } /* * Regularize the path name for single updating (--single-update). */ if (single_update) { static char regular_path_name[MAXPATHLEN]; char *p = single_update; if (!test("f", p)) die("'%s' not found.", p); if (isabspath(p)) die("--single-update requires relative path name."); if (!(p[0] == '.' && p[1] == '/')) { snprintf(regular_path_name, MAXPATHLEN, "./%s", p); p = regular_path_name; } single_update = p; } if (!getcwd(cwd, MAXPATHLEN)) die("cannot get current directory."); canonpath(cwd); /* * Decide directory (dbpath) in which gtags make tag files. * * Gtags create tag files at current directory by default. * If dbpath is specified as an argument then use it. * If the -i option specified and both GTAGS and GRTAGS exists * at one of the candidate directories then gtags use existing * tag files. */ if (iflag) { if (argc > 0) realpath(*argv, dbpath); else if (!gtagsexist(cwd, dbpath, MAXPATHLEN, vflag)) strlimcpy(dbpath, cwd, sizeof(dbpath)); } else { if (argc > 0) realpath(*argv, dbpath); else if (Oflag) { char *objdir = getobjdir(cwd, vflag); if (objdir == NULL) die("Objdir not found."); strlimcpy(dbpath, objdir, sizeof(dbpath)); } else strlimcpy(dbpath, cwd, sizeof(dbpath)); } if (iflag && (!test("f", makepath(dbpath, dbname(GTAGS), NULL)) || !test("f", makepath(dbpath, dbname(GRTAGS), NULL)) || !test("f", makepath(dbpath, dbname(GPATH), NULL)))) { if (wflag) warning("GTAGS, GRTAGS or GPATH not found. -i option ignored."); iflag = 0; } if (!test("d", dbpath)) die("directory '%s' not found.", dbpath); if (vflag) fprintf(stderr, "[%s] Gtags started.\n", now()); /* * load configuration file. */ openconf(); if (getconfb("extractmethod")) extractmethod = 1; strbuf_reset(sb); if (getconfs("langmap", sb)) langmap = check_strdup(strbuf_value(sb)); strbuf_reset(sb); if (getconfs("gtags_parser", sb)) gtags_parser = check_strdup(strbuf_value(sb)); /* * initialize parser. */ if (vflag && gtags_parser) fprintf(stderr, " Using plug-in parser.\n"); parser_init(langmap, gtags_parser); if (vflag && file_list) fprintf(stderr, " Using '%s' as a file list.\n", file_list); /* * Start statistics. */ init_statistics(); /* * incremental update. */ if (iflag) { /* * Version check. If existing tag files are old enough * gtagsopen() abort with error message. */ GTOP *gtop = gtags_open(dbpath, cwd, GTAGS, GTAGS_MODIFY, 0); gtags_close(gtop); /* * GPATH is needed for incremental updating. * Gtags check whether or not GPATH exist, since it may be * removed by mistake. */ if (!test("f", makepath(dbpath, dbname(GPATH), NULL))) die("Old version tag file found. Please remake it."); (void)incremental(dbpath, cwd); print_statistics(statistics); exit(0); } /* * create GTAGS and GRTAGS */ createtags(dbpath, cwd); /* * create idutils index. */ if (Iflag) { tim = statistics_time_start("Time of creating ID"); if (vflag) fprintf(stderr, "[%s] Creating indexes for idutils.\n", now()); strbuf_reset(sb); strbuf_puts(sb, "mkid"); if (vflag) strbuf_puts(sb, " -v"); strbuf_sprintf(sb, " --file='%s/ID'", dbpath); if (vflag) { #ifdef __DJGPP__ if (is_unixy()) /* test for 4DOS as well? */ #endif strbuf_puts(sb, " 1>&2"); } else { strbuf_puts(sb, " >/dev/null"); } if (debug) fprintf(stderr, "executing mkid like: %s\n", strbuf_value(sb)); if (system(strbuf_value(sb))) die("mkid failed: %s", strbuf_value(sb)); if (chmod(makepath(dbpath, "ID", NULL), 0644) < 0) die("cannot chmod ID file."); statistics_time_end(tim); } if (vflag) fprintf(stderr, "[%s] Done.\n", now()); closeconf(); strbuf_close(sb); print_statistics(statistics); return 0; }