📄 fileindex.c
字号:
/* * Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 * Tama Communications Corporation * * This file is part of GNU GLOBAL. * * GNU GLOBAL is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * GNU GLOBAL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */#ifdef HAVE_CONFIG_H#include "config.h"#endif#include <ctype.h>#include <stdio.h>#ifdef STDC_HEADERS#include <stdlib.h>#endif#ifdef HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include "regex.h"#include "queue.h"#include "global.h"#include "incop.h"#include "path2url.h"#include "htags.h"#include "common.h"#include "test.h"/*----------------------------------------------------------------------*//* File queue *//*----------------------------------------------------------------------*//* * Usage: * * FILE *op; * * open_file_queue("a"); * open_file_queue("b"); * op = select_file_queue("a"); * fputs("xxxxx", op); -- write to file 'a' * op = select_file_queue("b"); * fputs("xxxxx", op); -- write to file 'b' * close_file_queue("a"); * close_file_queue("b");*/struct file { SLIST_ENTRY(file) ptr; char path[MAXPATHLEN]; FILE *op; int compress;};static SLIST_HEAD(, file) file_q;/* * open_file_queue: open file and return file pointer. * * i) path path name or command line. * r) file pointer * * You can get file pointer any time using select_file_queue() with path. */static FILE *open_file_queue(path) const char *path;{ struct file *file = (struct file *)malloc(sizeof(struct file)); if (!file) die("short of memory."); if (strlen(path) > MAXPATHLEN) die("path name too long."); strlimcpy(file->path, path, sizeof(file->path)); if (cflag) { char command[MAXFILLEN]; snprintf(command, sizeof(command), "gzip -c >%s", path); file->op = popen(command, "w"); if (file->op == NULL) die("cannot execute command '%s'.", command); file->compress = 1; } else { file->op = fopen(path, "w"); if (file->op == NULL) die("cannot create file '%s'.", path); file->compress = 0; } SLIST_INSERT_HEAD(&file_q, file, ptr); return file->op;}/* * select_file_queue: return file pointer for path. * * i) path path name * r) file pointer * NULL: path not found. */static FILE *select_file_queue(path) const char *path;{ struct file *file; SLIST_FOREACH(file, &file_q, ptr) { if (!strcmp(file->path, path)) return file->op; } die("cannot select no existent file.");}/* * close_file_queue: close file * * i) path path name */static voidclose_file_queue(path) const char *path;{ struct file *file; SLIST_FOREACH(file, &file_q, ptr) { if (!strcmp(file->path, path)) break; } if (file == NULL) die("cannot close no existent file."); if (file->compress) { if (pclose(file->op) != 0) { char command[MAXFILLEN]; snprintf(command, sizeof(command), "gzip -c >%s", file->path); die("command '%s' failed.", command); } } else fclose(file->op); SLIST_REMOVE(&file_q, file, file, ptr); free(file);}/*----------------------------------------------------------------------*//* Directory stack *//*----------------------------------------------------------------------*//* * Usage: * * struct dirstack *sp = make_stack("stack1"); * set_stack(sp, "aaa/bbb/ccc"); * * sp = ("aaa" "bbb" "ccc") * * push_stack(sp, "ddd"); * * sp = ("aaa" "bbb" "ccc" "ddd") * * char *s = top_stack(sp); * * sp = ("aaa" "bbb" "ccc" "ddd") * s = "ddd" * * char *s = pop_stack(sp); * * sp = ("aaa" "bbb" "ccc") * s = "ddd" * * char *s = shift_stack(sp); * * sp = ("bbb" "ccc") * s = "aaa" * * char *s = join_stack(sp); * * sp = ("bbb" "ccc") * s = "bbb/ccc" * * delete_stack(sp); */static int trace = 0;#define TOTAL_STRING_SIZE 2048struct dirstack { char name[32]; char buf[TOTAL_STRING_SIZE]; char join[TOTAL_STRING_SIZE]; char shift[TOTAL_STRING_SIZE]; char *start; char *last; int leaved; int count;};#define count_stack(sp) ((sp)->count)#define bottom_stack(sp) ((sp)->start)voidsettrace(void){ trace = 1;}/* * dump_stack: print stack list to stderr. * * i) sp stack descriptor */voidstatic dump_stack(sp, label) struct dirstack *sp; const char *label;{ char *start = sp->start; char *last = sp->last - 1; const char *p; fprintf(stderr, "%s(%s): ", label, sp->name); for (p = sp->buf; p < last; p++) { if (p == start) fputs("[", stderr); fputc((*p == 0) ? ' ' : *p, stderr); } if (start == sp->last) fputs("[", stderr); fputs_nl("]", stderr);}/* * make_stack: make new stack. * * r) stack descriptor */static struct dirstack *make_stack(name) const char *name;{ struct dirstack *sp = (struct dirstack *)malloc(sizeof(struct dirstack)); if (!sp) die("short of memory."); strlimcpy(sp->name, name, TOTAL_STRING_SIZE); sp->start = sp->last = sp->buf; sp->leaved = TOTAL_STRING_SIZE; sp->count = 0; return sp;}/* * set_stack: set path with splitting by '/'. * * i) sp stack descriptor * i) path path name * * path = 'aaa/bbb/ccc'; * * sp->buf * +---------------------------+ * |aaa\0bbb\0ccc\0 | * +---------------------------+ * ^ ^ * sp->start sp->last * * */static voidset_stack(sp, path) struct dirstack *sp; const char *path;{ int length = strlen(path) + 1; char *p; if (length > TOTAL_STRING_SIZE) die("path name too long."); sp->start = sp->buf; sp->last = sp->buf + length; sp->leaved = TOTAL_STRING_SIZE - length; if (sp->leaved < 0) abort(); sp->count = 1; strlimcpy(sp->buf, path, TOTAL_STRING_SIZE); /* * Split path by sep char. */ for (p = sp->buf; *p; p++) { if (*p == sep) { *p = '\0'; sp->count++; } } if (trace) dump_stack(sp, "set_stack");}/* * push_stack: push new string on the stack. * * i) sp stack descriptor * i) s string */static voidpush_stack(sp, s) struct dirstack *sp; const char *s;{ int length = strlen(s) + 1; if (sp->leaved < length) { fprintf(stderr, "s = %s, leaved = %d, length = %d, bufsize = %d\n", s, sp->leaved, length, sp->last - sp->buf); dump_stack(sp, "abort in push_stack"); abort(); } strlimcpy(sp->last, s, length); sp->last += length; sp->leaved -= length; if (sp->leaved < 0) abort(); sp->count++; if (trace) dump_stack(sp, "push_stack");}/* * top_stack: return the top value of the stack. * * i) sp stack descriptor * r) string */static const char *top_stack(sp) struct dirstack *sp;{ char *start = sp->start; char *last = sp->last; if (start > last) die("internal error in top_stack(1)."); if (start == last) return NULL; last--; if (*last) die("internal error in top_stack(2)."); if (start == last) return last; /* return NULL string */ for (last--; start < last && *last != 0; last--) ; if (start < last) last++; return last;}/* * next_stack: return the next value of the stack. * * i) sp stack descriptor * i) cur current value * r) string */static const char *next_stack(sp, cur) struct dirstack *sp; const char *cur;{ char *last = sp->last; if (cur >= last) return NULL; for (; cur < last && *cur != 0; cur++) ; cur++; if (cur >= last) return NULL; return cur;}/* * pop_stack: return the top of the stack and discard it. * * i) sp stack descriptor * r) string */static const char *pop_stack(sp) struct dirstack *sp;{ char *last = (char *)top_stack(sp); int length = strlen(last) + 1; if (!last) return NULL; sp->count--; sp->last = last; sp->leaved += length; if (trace) dump_stack(sp, "pop_stack"); return last;}/* * shift_stack: return the bottom of the stack and discard it. * * i) sp stack descriptor * r) string */static const char *shift_stack(sp) struct dirstack *sp;{ char *start = sp->start; char *last = sp->last; int length = strlen(start) + 1; if (start == last) return NULL; sp->start += length; sp->count--; if (sp->count == 0) { strlimcpy(sp->shift, start, TOTAL_STRING_SIZE); start = sp->shift; sp->start = sp->last = sp->buf; sp->leaved = TOTAL_STRING_SIZE; } if (trace) dump_stack(sp, "shift_stack"); return start;}/* * copy_stack: make duplicate stack. * * i) to stack descriptor * i) from stack descriptor */static voidcopy_stack(to, from) struct dirstack *to; struct dirstack *from;{ char *start = from->start; char *last = from->last; char *p = to->buf; to->start = to->buf; to->last = to->start + (last - start); to->leaved = from->leaved; to->count = from->count; while (start < last) *p++ = *start++;}/* * join_stack: join each unit and make a path. * * i) sp stack descriptor * r) path name */static const char *join_stack(sp) struct dirstack *sp;{ char *start = sp->start; char *last = sp->last - 1; char *p = sp->join; for (; start < last; start++) *p++ = (*start) ? *start : sep; *p = 0; if (trace) dump_stack(sp, "join_stack"); return (char *)sp->join;}/* * delete_stack: delete stack. * * i) sp stack descriptor */static voiddelete_stack(sp) struct dirstack *sp;{ free(sp);}/*----------------------------------------------------------------------*//* Main procedure *//*----------------------------------------------------------------------*//* * Encode URL. * * i) url URL * r) encoded URL */static const char *encode(url) const char *url;{ STATIC_STRBUF(sb); const char *p; strbuf_clear(sb); for (p = url; *p; p++) { int c = (unsigned char)*p; if (isalnum(c)) strbuf_putc(sb, c); else strbuf_sprintf(sb, "%%%02x", c); } return strbuf_value(sb);}/* * extract_lastname: extract the last name of include line. * * i) image source image of include * i) is_php 1: is PHP source * r) last name */static const char *extract_lastname(image, is_php) const char *image; int is_php;{ static char buf[MAXBUFLEN]; const char *p; char *q; int sep; /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -