/** * 拆分如 "test, quiet" 的字符串为 ["test", "quiet"] */ static void split_groups(const char *groups, std::vector<std::string> *rs) { assert(NULL != groups && NULL != rs); const char GROUP_SPLITER = ','; // 分组分隔符 std::string s; for (size_t i = 0; 0 != groups[i]; ++i) { const char c = groups[i]; if (GROUP_SPLITER == c) { // 去除尾部空格 s = trim_end(s); // 添加到结果 if (s.length() > 0) { rs->push_back(s); s.clear(); } } else if (s.length() > 0 || !is_blank(c)) { s.push_back(c); } } s = trim_end(s); if (s.length() > 0) rs->push_back(s); }
IniFile::IniFile(pcchar file, bool in_cwd) { if (! in_cwd) m_file = strdup(file); else { get_app_path(_tmpbuff_,BUFSZ); strcat(_tmpbuff_,"/"); strcat(_tmpbuff_,file); m_file = strdup(_tmpbuff_); } #ifndef WIN32_INI FILE *in = fopen(file,"r"); while (! feof(in)) { fgets(_tmpbuff_,BUFSZ,in); trim_end(_tmpbuff_); if (*_tmpbuff_ == ';' || *_tmpbuff_ == '\0') continue; if (*_tmpbuff_ == '[') { char* section = _tmpbuff_ + 1; trim_end(section); } } fclose(in); #endif }
struct cgit_repo *cgit_add_repo(const char *url) { struct cgit_repo *ret; if (++cgit_repolist.count > cgit_repolist.length) { if (cgit_repolist.length == 0) cgit_repolist.length = 8; else cgit_repolist.length *= 2; cgit_repolist.repos = xrealloc(cgit_repolist.repos, cgit_repolist.length * sizeof(struct cgit_repo)); } ret = &cgit_repolist.repos[cgit_repolist.count-1]; ret->url = trim_end(url, '/'); ret->name = ret->url; ret->path = NULL; ret->desc = "[no description]"; ret->owner = NULL; ret->group = ctx.cfg.repo_group; ret->defbranch = "master"; ret->snapshots = ctx.cfg.snapshots; ret->enable_log_filecount = ctx.cfg.enable_log_filecount; ret->enable_log_linecount = ctx.cfg.enable_log_linecount; ret->module_link = ctx.cfg.module_link; ret->readme = NULL; return ret; }
void run_command (player * p, char *str) { command *com; char *param; for (; *str == ' '; str++); trim_end (str); if (!*str) return; if (!isalpha (*str)) com = root_commands[0]; else com = root_commands[((tolower (*str) - 'a') + 1)]; for (; com->key; com++) if ((param = match_command_key (com, str)) && com->level <= p->residency) { if (com->sublist) run_subcommand (p, param, com); else actually_do_command (p, com, param); return; } param = next_space (str); if (*param) *param++ = '\0'; vwritep (p, "No command %s available to you.\n", str); return; }
void run_subcommand (player * p, char *str, command * parent) { command *com; char *param; trim_end (str); if (!parent->sublist) { writep (p, "No subcommands found!\n"); vlog ("parser", "run_subcommand: No %s subcommands found!\n", parent->key); return; } if (!*str) { view_subcommands (p, parent); return; } param = next_space (str); if (*param) *param++ = '\0'; com = find_subcommand (p, str, parent); if (com == NULL) { vwritep (p, "No command %s %s available to you.\n", parent->key, str); return; } else actually_do_command (p, com, param); return; }
static void final_reject_message(char* buf, int buflen) { if(buf[0] == 0) strlcpy(buf, "530 Content Rejected", buflen); else trim_end(buf); }
static void overlay_add(char *opt, char *value) { int i = 0; /* step the filled in */ while (i < 1000 && overlay[i].opt) i++; trim_end(opt); trim_end(value); D_("adding \"%s\" == \"%s\" on %i\n", opt, value, i); overlay[i].opt = opt; overlay[i].value = value; }
int main(int argc, const char **argv) { const char *path; char *qry; int err, ttl; prepare_context(&ctx); cgit_repolist.length = 0; cgit_repolist.count = 0; cgit_repolist.repos = NULL; cgit_parse_args(argc, argv); parse_configfile(expand_macros(ctx.env.cgit_config), config_cb); ctx.repo = NULL; http_parse_querystring(ctx.qry.raw, querystring_cb); /* If virtual-root isn't specified in cgitrc, lets pretend * that virtual-root equals SCRIPT_NAME, minus any possibly * trailing slashes. */ if (!ctx.cfg.virtual_root && ctx.cfg.script_name) { ctx.cfg.virtual_root = trim_end(ctx.cfg.script_name, '/'); if (!ctx.cfg.virtual_root) ctx.cfg.virtual_root = ""; } /* If no url parameter is specified on the querystring, lets * use PATH_INFO as url. This allows cgit to work with virtual * urls without the need for rewriterules in the webserver (as * long as PATH_INFO is included in the cache lookup key). */ path = ctx.env.path_info; if (!ctx.qry.url && path) { if (path[0] == '/') path++; ctx.qry.url = xstrdup(path); if (ctx.qry.raw) { qry = ctx.qry.raw; ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); free(qry); } else ctx.qry.raw = xstrdup(ctx.qry.url); cgit_parse_url(ctx.qry.url); } ttl = calc_ttl(); ctx.page.expires += ttl * 60; if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")) ctx.cfg.nocache = 1; if (ctx.cfg.nocache) ctx.cfg.cache_size = 0; err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, ctx.qry.raw, ttl, process_request, &ctx); if (err) cgit_print_error(fmt("Error processing page: %s (%d)", strerror(err), err)); return err; }
command *find_subcommand (player * p, char *key, command * parent) { command *com; trim_end (key); for (com = parent->sublist; com->key; com++) if (com->level <= p->residency && !strncasecmp (key, com->key, strlen (key))) return com; return NULL; }
static void querystring_cb(const char *name, const char *value) { if (!value) value = ""; if (!strcmp(name,"r")) { ctx.qry.repo = xstrdup(value); ctx.repo = cgit_get_repoinfo(value); } else if (!strcmp(name, "p")) { ctx.qry.page = xstrdup(value); } else if (!strcmp(name, "url")) { if (*value == '/') value++; ctx.qry.url = xstrdup(value); cgit_parse_url(value); } else if (!strcmp(name, "qt")) { ctx.qry.grep = xstrdup(value); } else if (!strcmp(name, "q")) { ctx.qry.search = xstrdup(value); } else if (!strcmp(name, "h")) { ctx.qry.head = xstrdup(value); ctx.qry.has_symref = 1; } else if (!strcmp(name, "id")) { ctx.qry.sha1 = xstrdup(value); ctx.qry.has_sha1 = 1; } else if (!strcmp(name, "id2")) { ctx.qry.sha2 = xstrdup(value); ctx.qry.has_sha1 = 1; } else if (!strcmp(name, "ofs")) { ctx.qry.ofs = atoi(value); } else if (!strcmp(name, "path")) { ctx.qry.path = trim_end(value, '/'); } else if (!strcmp(name, "name")) { ctx.qry.name = xstrdup(value); } else if (!strcmp(name, "mimetype")) { ctx.qry.mimetype = xstrdup(value); } else if (!strcmp(name, "s")) { ctx.qry.sort = xstrdup(value); } else if (!strcmp(name, "showmsg")) { ctx.qry.showmsg = atoi(value); } else if (!strcmp(name, "period")) { ctx.qry.period = xstrdup(value); } else if (!strcmp(name, "ss")) { ctx.qry.ssdiff = atoi(value); ctx.qry.has_ssdiff = 1; } else if (!strcmp(name, "all")) { ctx.qry.show_all = atoi(value); } else if (!strcmp(name, "context")) { ctx.qry.context = atoi(value); } else if (!strcmp(name, "ignorews")) { ctx.qry.ignorews = atoi(value); } }
buffer_t massdns_resolvers_from_file(char *filename) { size_t line_buflen = 4096; char *line = safe_malloc(line_buflen); FILE *f = fopen(filename, "r"); if (f == NULL) { perror("Failed to open resolver file"); exit(1); } single_list_t *list = safe_malloc(sizeof(*list)); list->next = NULL; list->data = NULL; single_list_t *start = list; single_list_t *previous = NULL; while (!feof(f)) { if (0 <= getline(&line, &line_buflen, f)) { trim_end(line); struct sockaddr_in *addr = str_to_addr(line); if (addr != NULL) { list->data = addr; list->next = safe_malloc(sizeof(*list)); previous = list; list = list->next; } else { fprintf(stderr, "\"%s\" is not a valid resolver. Skipped.\n", line); } } else if (previous != NULL) { free(list); list = NULL; previous->next = NULL; } free(line); line = NULL; } fclose(f); buffer_t resolvers = single_list_to_array(start); single_list_iterate(start, free_element, NULL); free(list); return resolvers; }
pal::string_t get_directory(const pal::string_t& path) { pal::string_t ret = trim_end(path, DIR_SEPARATOR); // Find the last dir separator auto path_sep = ret.find_last_of(DIR_SEPARATOR); if (path_sep == pal::string_t::npos) { return ret + DIR_SEPARATOR; } int pos = (int) path_sep; while (pos >= 0 && ret[pos] == DIR_SEPARATOR) { pos--; } return ret.substr(0, pos + 1) + DIR_SEPARATOR; }
struct cgit_repo *cgit_add_repo(const char *url) { struct cgit_repo *ret; if (++cgit_repolist.count > cgit_repolist.length) { if (cgit_repolist.length == 0) cgit_repolist.length = 8; else cgit_repolist.length *= 2; cgit_repolist.repos = xrealloc(cgit_repolist.repos, cgit_repolist.length * sizeof(struct cgit_repo)); } ret = &cgit_repolist.repos[cgit_repolist.count-1]; memset(ret, 0, sizeof(struct cgit_repo)); ret->url = trim_end(url, '/'); ret->name = ret->url; ret->path = NULL; ret->desc = cgit_default_repo_desc; ret->owner = NULL; ret->section = ctx.cfg.section; ret->snapshots = ctx.cfg.snapshots; ret->enable_commit_graph = ctx.cfg.enable_commit_graph; ret->enable_log_filecount = ctx.cfg.enable_log_filecount; ret->enable_log_linecount = ctx.cfg.enable_log_linecount; ret->enable_remote_branches = ctx.cfg.enable_remote_branches; ret->enable_subject_links = ctx.cfg.enable_subject_links; ret->max_stats = ctx.cfg.max_stats; ret->branch_sort = ctx.cfg.branch_sort; ret->commit_sort = ctx.cfg.commit_sort; ret->module_link = ctx.cfg.module_link; ret->readme = ctx.cfg.readme; ret->mtime = -1; ret->about_filter = ctx.cfg.about_filter; ret->commit_filter = ctx.cfg.commit_filter; ret->source_filter = ctx.cfg.source_filter; ret->clone_url = ctx.cfg.clone_url; ret->submodules.strdup_strings = 1; return ret; }
/* * url syntax: [repo ['/' cmd [ '/' path]]] * repo: any valid repo url, may contain '/' * cmd: log | commit | diff | tree | view | blob | snapshot * path: any valid path, may contain '/' * */ void cgit_parse_url(const char *url) { char *c, *cmd, *p; struct cgit_repo *repo; ctx.repo = NULL; if (!url || url[0] == '\0') return; ctx.repo = cgit_get_repoinfo(url); if (ctx.repo) { ctx.qry.repo = ctx.repo->url; return; } cmd = NULL; c = strchr(url, '/'); while (c) { c[0] = '\0'; repo = cgit_get_repoinfo(url); if (repo) { ctx.repo = repo; cmd = c; } c[0] = '/'; c = strchr(c + 1, '/'); } if (ctx.repo) { ctx.qry.repo = ctx.repo->url; p = strchr(cmd + 1, '/'); if (p) { p[0] = '\0'; if (p[1]) ctx.qry.path = trim_end(p + 1, '/'); } if (cmd[1]) ctx.qry.page = xstrdup(cmd + 1); return; } }
static int debugger_console (debugger_t * debugger) { static const char * const prompt = "> "; printf ("Debugger console (h to help)\n"); while (1) { char buf[64] = {0}; printf (prompt); if (fgets (buf, sizeof(buf), stdin)) { trim_end (buf, "\r\n"); handle_command (debugger, buf); } } return EOK; }
/* * url syntax: [repo ['/' cmd [ '/' path]]] * repo: any valid repo url, may contain '/' * cmd: log | commit | diff | tree | view | blob | snapshot * path: any valid path, may contain '/' * */ void cgit_parse_url(const char *url) { char *cmd, *p; ctx.repo = NULL; if (!url || url[0] == '\0') return; ctx.repo = cgit_get_repoinfo(url); if (ctx.repo) { ctx.qry.repo = ctx.repo->url; return; } cmd = strchr(url, '/'); while (!ctx.repo && cmd) { cmd[0] = '\0'; ctx.repo = cgit_get_repoinfo(url); if (ctx.repo == NULL) { cmd[0] = '/'; cmd = strchr(cmd + 1, '/'); continue; } ctx.qry.repo = ctx.repo->url; p = strchr(cmd + 1, '/'); if (p) { p[0] = '\0'; if (p[1]) ctx.qry.path = trim_end(p + 1, '/'); } if (cmd[1]) ctx.qry.page = xstrdup(cmd + 1); return; } }
struct cgit_repo *cgit_add_repo(const char *url) { struct cgit_repo *ret; if (++cgit_repolist.count > cgit_repolist.length) { if (cgit_repolist.length == 0) cgit_repolist.length = 8; else cgit_repolist.length *= 2; cgit_repolist.repos = xrealloc(cgit_repolist.repos, cgit_repolist.length * sizeof(struct cgit_repo)); } ret = &cgit_repolist.repos[cgit_repolist.count-1]; memset(ret, 0, sizeof(struct cgit_repo)); ret->url = trim_end(url, '/'); ret->name = ret->url; ret->path = NULL; ret->desc = "[no description]"; ret->owner = NULL; ret->section = ctx.cfg.section; ret->defbranch = "master"; ret->snapshots = ctx.cfg.snapshots; ret->enable_log_filecount = ctx.cfg.enable_log_filecount; ret->enable_log_linecount = ctx.cfg.enable_log_linecount; ret->enable_remote_branches = ctx.cfg.enable_remote_branches; ret->max_stats = ctx.cfg.max_stats; ret->module_link = ctx.cfg.module_link; ret->readme = NULL; ret->mtime = -1; ret->about_filter = ctx.cfg.about_filter; ret->commit_filter = ctx.cfg.commit_filter; ret->source_filter = ctx.cfg.source_filter; return ret; }
void utils::trim(std::string& str) { while (str.length() > 0 && ::isspace(str[0])) { str.erase(0,1); } trim_end(str); }
void massdns_scan(lookup_context_t *context) { memset(&stats, 0, sizeof(dns_stats_t)); size_t line_buflen = 4096; char *line = safe_malloc(line_buflen); size_t line_len = 0; struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = 0; int sock = socket(AF_INET, SOCK_DGRAM, 0); int socketbuf = 1024 * 1024 * 100; if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &socketbuf, sizeof(socketbuf)) != 0) { perror("Failed to adjust socket send buffer size."); } if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &socketbuf, sizeof(socketbuf)) != 0) { perror("Failed to adjust socket receive buffer size."); } bind(sock, (sockaddr_t *) &server_addr, sizeof(server_addr)); fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); if (sock < 0) { perror("Failed to create socket"); exit(1); } FILE *f; if(context->cmd_args.show_progress) { f = fopen(context->cmd_args.domains, "r"); if (f == NULL) { perror("Failed to open domain file"); exit(1); } while(!feof(f)) { if (0 <= getline(&line, &line_buflen, f)) { trim_end(line); strtolower(line); if (strcmp(line, "") != 0) { context->total_domains++; } } } fclose(f); } f = fopen(context->cmd_args.domains, "r"); if (f == NULL) { perror("Failed to open domain file"); exit(1); } if (geteuid() == 0) { fprintf(stderr, "You have started the program with root privileges.\n"); struct passwd *nobody = getpwnam(UNPRIVILEGED_USER); if (!context->cmd_args.root) { if (nobody && setuid(nobody->pw_uid) == 0) { fprintf(stderr, "Privileges have been dropped to \"%s\" for security reasons.\n\n", UNPRIVILEGED_USER); } else if (!context->cmd_args.root) { fprintf(stderr, "Privileges could not be dropped to \"%s\".\n" "For security reasons, this program will only run as root user when supplied with --root" "which is not recommended.\n" "It is better practice to run this program as a different user.\n", UNPRIVILEGED_USER); exit(1); } } else { fprintf(stderr, "[WARNING] Privileges were not dropped. This is not recommended.\n\n"); } } FILE *randomness = fopen("/dev/urandom", "r"); if (!randomness) { fprintf(stderr, "Failed to open /dev/urandom.\n"); exit(1); } context->current_rate = 0; context->sock = sock; context->resolvers = massdns_resolvers_from_file(context->cmd_args.resolvers); context->map = hashmapCreate(context->cmd_args.hashmap_size, hash_string, cmp_lookup); context->initial = true; gettimeofday(&context->start_time, NULL); context->next_update = context->start_time; while (true) { while (hashmapSize(context->map) < context->cmd_args.hashmap_size && !feof(f)) { if (0 <= getline(&line, &line_buflen, f)) { trim_end(line); line_len = strlen(line); strtolower(line); if(strcmp(line, "") == 0) { continue; } if (line_len > 0 && line[line_len - 1] == '.') { // Remove trailing dot from FQDN line[line_len] = 0; } lookup_t *lookup = hashmapGet(context->map, line); if (lookup == NULL) { char *value = safe_malloc(line_len + 1); strcpy(value, line); lookup = safe_malloc(sizeof(*lookup)); lookup->domain = value; lookup->tries = 0; if (fread(&lookup->transaction, 1, sizeof(lookup->transaction), randomness) != sizeof(lookup->transaction)) { fprintf(stderr, "Failed to get randomness for transaction id.\n"); exit(1); } gettimeofday(&lookup->next_lookup, NULL); hashmapPut(context->map, value, lookup); } } } if(!context->cooldown && hashmapSize(context->map) < context->cmd_args.hashmap_size) { context->cooldown = true; gettimeofday(&context->cooldown_time, NULL); } while (massdns_receive_packet(sock, massdns_handle_packet, context)); if (feof(f) && hashmapSize(context->map) == 0) { break; } hashmapForEach(context->map, handle_domain, context); } for (size_t i = 0; i < context->resolvers.len; i++) { free(((sockaddr_in_t **) context->resolvers.data)[i]); ((sockaddr_in_t **) context->resolvers.data)[i] = NULL; } free(line); free(context->resolvers.data); context->resolvers.data = NULL; print_stats(context); hashmapFree(context->map); context->map = NULL; fclose(f); fclose(randomness); }
void config_cb(const char *name, const char *value) { if (!strcmp(name, "section") || !strcmp(name, "repo.group")) ctx.cfg.section = xstrdup(value); else if (!strcmp(name, "repo.url")) ctx.repo = cgit_add_repo(value); else if (ctx.repo && !strcmp(name, "repo.path")) ctx.repo->path = trim_end(value, '/'); else if (ctx.repo && !prefixcmp(name, "repo.")) repo_config(ctx.repo, name + 5, value); else if (!strcmp(name, "root-title")) ctx.cfg.root_title = xstrdup(value); else if (!strcmp(name, "root-desc")) ctx.cfg.root_desc = xstrdup(value); else if (!strcmp(name, "root-readme")) ctx.cfg.root_readme = xstrdup(value); else if (!strcmp(name, "css")) ctx.cfg.css = xstrdup(value); else if (!strcmp(name, "favicon")) ctx.cfg.favicon = xstrdup(value); else if (!strcmp(name, "footer")) ctx.cfg.footer = xstrdup(value); else if (!strcmp(name, "head-include")) ctx.cfg.head_include = xstrdup(value); else if (!strcmp(name, "header")) ctx.cfg.header = xstrdup(value); else if (!strcmp(name, "logo")) ctx.cfg.logo = xstrdup(value); else if (!strcmp(name, "index-header")) ctx.cfg.index_header = xstrdup(value); else if (!strcmp(name, "index-info")) ctx.cfg.index_info = xstrdup(value); else if (!strcmp(name, "logo-link")) ctx.cfg.logo_link = xstrdup(value); else if (!strcmp(name, "module-link")) ctx.cfg.module_link = xstrdup(value); else if (!strcmp(name, "virtual-root")) { ctx.cfg.virtual_root = trim_end(value, '/'); if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) ctx.cfg.virtual_root = ""; } else if (!strcmp(name, "nocache")) ctx.cfg.nocache = atoi(value); else if (!strcmp(name, "noplainemail")) ctx.cfg.noplainemail = atoi(value); else if (!strcmp(name, "noheader")) ctx.cfg.noheader = atoi(value); else if (!strcmp(name, "snapshots")) ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); else if (!strcmp(name, "enable-filter-overrides")) ctx.cfg.enable_filter_overrides = atoi(value); else if (!strcmp(name, "enable-index-links")) ctx.cfg.enable_index_links = atoi(value); else if (!strcmp(name, "enable-log-filecount")) ctx.cfg.enable_log_filecount = atoi(value); else if (!strcmp(name, "enable-log-linecount")) ctx.cfg.enable_log_linecount = atoi(value); else if (!strcmp(name, "enable-tree-linenumbers")) ctx.cfg.enable_tree_linenumbers = atoi(value); else if (!strcmp(name, "max-stats")) ctx.cfg.max_stats = cgit_find_stats_period(value, NULL); else if (!strcmp(name, "cache-size")) ctx.cfg.cache_size = atoi(value); else if (!strcmp(name, "cache-root")) ctx.cfg.cache_root = xstrdup(value); else if (!strcmp(name, "cache-root-ttl")) ctx.cfg.cache_root_ttl = atoi(value); else if (!strcmp(name, "cache-repo-ttl")) ctx.cfg.cache_repo_ttl = atoi(value); else if (!strcmp(name, "cache-scanrc-ttl")) ctx.cfg.cache_scanrc_ttl = atoi(value); else if (!strcmp(name, "cache-static-ttl")) ctx.cfg.cache_static_ttl = atoi(value); else if (!strcmp(name, "cache-dynamic-ttl")) ctx.cfg.cache_dynamic_ttl = atoi(value); else if (!strcmp(name, "about-filter")) ctx.cfg.about_filter = new_filter(value, 0); else if (!strcmp(name, "commit-filter")) ctx.cfg.commit_filter = new_filter(value, 0); else if (!strcmp(name, "embedded")) ctx.cfg.embedded = atoi(value); else if (!strcmp(name, "max-message-length")) ctx.cfg.max_msg_len = atoi(value); else if (!strcmp(name, "max-repodesc-length")) ctx.cfg.max_repodesc_len = atoi(value); else if (!strcmp(name, "max-repo-count")) ctx.cfg.max_repo_count = atoi(value); else if (!strcmp(name, "max-commit-count")) ctx.cfg.max_commit_count = atoi(value); else if (!strcmp(name, "scan-path")) if (!ctx.cfg.nocache && ctx.cfg.cache_size) process_cached_repolist(value); else scan_tree(value, repo_config); else if (!strcmp(name, "source-filter")) ctx.cfg.source_filter = new_filter(value, 1); else if (!strcmp(name, "summary-log")) ctx.cfg.summary_log = atoi(value); else if (!strcmp(name, "summary-branches")) ctx.cfg.summary_branches = atoi(value); else if (!strcmp(name, "summary-tags")) ctx.cfg.summary_tags = atoi(value); else if (!strcmp(name, "agefile")) ctx.cfg.agefile = xstrdup(value); else if (!strcmp(name, "renamelimit")) ctx.cfg.renamelimit = atoi(value); else if (!strcmp(name, "robots")) ctx.cfg.robots = xstrdup(value); else if (!strcmp(name, "clone-prefix")) ctx.cfg.clone_prefix = xstrdup(value); else if (!strcmp(name, "local-time")) ctx.cfg.local_time = atoi(value); else if (!prefixcmp(name, "mimetype.")) add_mimetype(name + 9, value); else if (!strcmp(name, "include")) parse_configfile(value, config_cb); }
char* trim_space(char* data) { data = (char*)trim_start(data); return trim_end(data); }
void config_cb(const char *name, const char *value) { if (!strcmp(name, "section") || !strcmp(name, "repo.group")) ctx.cfg.section = xstrdup(value); else if (!strcmp(name, "repo.url")) ctx.repo = cgit_add_repo(value); else if (ctx.repo && !strcmp(name, "repo.path")) ctx.repo->path = trim_end(value, '/'); else if (ctx.repo && !prefixcmp(name, "repo.")) repo_config(ctx.repo, name + 5, value); else if (!strcmp(name, "readme")) ctx.cfg.readme = xstrdup(value); else if (!strcmp(name, "root-title")) ctx.cfg.root_title = xstrdup(value); else if (!strcmp(name, "root-desc")) ctx.cfg.root_desc = xstrdup(value); else if (!strcmp(name, "root-readme")) ctx.cfg.root_readme = xstrdup(value); else if (!strcmp(name, "css")) ctx.cfg.css = xstrdup(value); else if (!strcmp(name, "favicon")) ctx.cfg.favicon = xstrdup(value); else if (!strcmp(name, "footer")) ctx.cfg.footer = xstrdup(value); else if (!strcmp(name, "head-include")) ctx.cfg.head_include = xstrdup(value); else if (!strcmp(name, "header")) ctx.cfg.header = xstrdup(value); else if (!strcmp(name, "logo")) ctx.cfg.logo = xstrdup(value); else if (!strcmp(name, "index-header")) ctx.cfg.index_header = xstrdup(value); else if (!strcmp(name, "index-info")) ctx.cfg.index_info = xstrdup(value); else if (!strcmp(name, "logo-link")) ctx.cfg.logo_link = xstrdup(value); else if (!strcmp(name, "module-link")) ctx.cfg.module_link = xstrdup(value); else if (!strcmp(name, "strict-export")) ctx.cfg.strict_export = xstrdup(value); else if (!strcmp(name, "virtual-root")) { ctx.cfg.virtual_root = trim_end(value, '/'); if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) ctx.cfg.virtual_root = ""; } else if (!strcmp(name, "nocache")) ctx.cfg.nocache = atoi(value); else if (!strcmp(name, "noplainemail")) ctx.cfg.noplainemail = atoi(value); else if (!strcmp(name, "noheader")) ctx.cfg.noheader = atoi(value); else if (!strcmp(name, "snapshots")) ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); else if (!strcmp(name, "enable-filter-overrides")) ctx.cfg.enable_filter_overrides = atoi(value); else if (!strcmp(name, "enable-http-clone")) ctx.cfg.enable_http_clone = atoi(value); else if (!strcmp(name, "enable-index-links")) ctx.cfg.enable_index_links = atoi(value); else if (!strcmp(name, "enable-index-owner")) ctx.cfg.enable_index_owner = atoi(value); else if (!strcmp(name, "enable-commit-graph")) ctx.cfg.enable_commit_graph = atoi(value); else if (!strcmp(name, "enable-log-filecount")) ctx.cfg.enable_log_filecount = atoi(value); else if (!strcmp(name, "enable-log-linecount")) ctx.cfg.enable_log_linecount = atoi(value); else if (!strcmp(name, "enable-remote-branches")) ctx.cfg.enable_remote_branches = atoi(value); else if (!strcmp(name, "enable-subject-links")) ctx.cfg.enable_subject_links = atoi(value); else if (!strcmp(name, "enable-tree-linenumbers")) ctx.cfg.enable_tree_linenumbers = atoi(value); else if (!strcmp(name, "enable-git-config")) ctx.cfg.enable_git_config = atoi(value); else if (!strcmp(name, "max-stats")) ctx.cfg.max_stats = cgit_find_stats_period(value, NULL); else if (!strcmp(name, "cache-size")) ctx.cfg.cache_size = atoi(value); else if (!strcmp(name, "cache-root")) ctx.cfg.cache_root = xstrdup(expand_macros(value)); else if (!strcmp(name, "cache-root-ttl")) ctx.cfg.cache_root_ttl = atoi(value); else if (!strcmp(name, "cache-repo-ttl")) ctx.cfg.cache_repo_ttl = atoi(value); else if (!strcmp(name, "cache-scanrc-ttl")) ctx.cfg.cache_scanrc_ttl = atoi(value); else if (!strcmp(name, "cache-static-ttl")) ctx.cfg.cache_static_ttl = atoi(value); else if (!strcmp(name, "cache-dynamic-ttl")) ctx.cfg.cache_dynamic_ttl = atoi(value); else if (!strcmp(name, "case-sensitive-sort")) ctx.cfg.case_sensitive_sort = atoi(value); else if (!strcmp(name, "about-filter")) ctx.cfg.about_filter = new_filter(value, ABOUT); else if (!strcmp(name, "commit-filter")) ctx.cfg.commit_filter = new_filter(value, COMMIT); else if (!strcmp(name, "embedded")) ctx.cfg.embedded = atoi(value); else if (!strcmp(name, "max-atom-items")) ctx.cfg.max_atom_items = atoi(value); else if (!strcmp(name, "max-message-length")) ctx.cfg.max_msg_len = atoi(value); else if (!strcmp(name, "max-repodesc-length")) ctx.cfg.max_repodesc_len = atoi(value); else if (!strcmp(name, "max-blob-size")) ctx.cfg.max_blob_size = atoi(value); else if (!strcmp(name, "max-repo-count")) ctx.cfg.max_repo_count = atoi(value); else if (!strcmp(name, "max-commit-count")) ctx.cfg.max_commit_count = atoi(value); else if (!strcmp(name, "project-list")) ctx.cfg.project_list = xstrdup(expand_macros(value)); else if (!strcmp(name, "scan-path")) if (!ctx.cfg.nocache && ctx.cfg.cache_size) process_cached_repolist(expand_macros(value)); else if (ctx.cfg.project_list) scan_projects(expand_macros(value), ctx.cfg.project_list, repo_config); else scan_tree(expand_macros(value), repo_config); else if (!strcmp(name, "scan-hidden-path")) ctx.cfg.scan_hidden_path = atoi(value); else if (!strcmp(name, "section-from-path")) ctx.cfg.section_from_path = atoi(value); else if (!strcmp(name, "repository-sort")) ctx.cfg.repository_sort = xstrdup(value); else if (!strcmp(name, "section-sort")) ctx.cfg.section_sort = atoi(value); else if (!strcmp(name, "source-filter")) ctx.cfg.source_filter = new_filter(value, SOURCE); else if (!strcmp(name, "summary-log")) ctx.cfg.summary_log = atoi(value); else if (!strcmp(name, "summary-branches")) ctx.cfg.summary_branches = atoi(value); else if (!strcmp(name, "summary-tags")) ctx.cfg.summary_tags = atoi(value); else if (!strcmp(name, "side-by-side-diffs")) ctx.cfg.ssdiff = atoi(value); else if (!strcmp(name, "agefile")) ctx.cfg.agefile = xstrdup(value); else if (!strcmp(name, "mimetype-file")) ctx.cfg.mimetype_file = xstrdup(value); else if (!strcmp(name, "renamelimit")) ctx.cfg.renamelimit = atoi(value); else if (!strcmp(name, "remove-suffix")) ctx.cfg.remove_suffix = atoi(value); else if (!strcmp(name, "robots")) ctx.cfg.robots = xstrdup(value); else if (!strcmp(name, "clone-prefix")) ctx.cfg.clone_prefix = xstrdup(value); else if (!strcmp(name, "clone-url")) ctx.cfg.clone_url = xstrdup(value); else if (!strcmp(name, "local-time")) ctx.cfg.local_time = atoi(value); else if (!strcmp(name, "commit-sort")) { if (!strcmp(value, "date")) ctx.cfg.commit_sort = 1; if (!strcmp(value, "topo")) ctx.cfg.commit_sort = 2; } else if (!prefixcmp(name, "mimetype.")) add_mimetype(name + 9, value); else if (!strcmp(name, "include")) parse_configfile(expand_macros(value), config_cb); }
void update_setlist() { /* Calls 'raidutil list raidsetinfo' */ struct timeval now; gettimeofday (&now, NULL); char *data = NULL; size_t data_len = 0; if (xsan_debug()) { /* Use example data */ int fd = open ("../examples/raidutil_list_raidsetinfo.txt", O_RDONLY); data = malloc (65536); data_len = read (fd, data, 65535); data[data_len] = '\0'; close (fd); } else { /* Use live data */ data = x_command_run("raidutil list raidsetinfo"); if (!data) return; data_len = strlen(data); } /* Regex and loop through each raid set row */ const char *error; int erroffset; int ovector[OVECCOUNT]; /* Line is like 'Raid5Set RAID 5 2,3,4 3.92TB 0.00MB No tasks running' */ pcre *re = pcre_compile("^(\\w[^ ]+)\\s+(.*)\\s+((?:\\d\\,?)+)\\s+(\\d+\\.\\d\\d)([TGM])B\\s+(\\d+\\.\\d\\d)([TGM])B\\s+(.*)$", PCRE_MULTILINE, &error, &erroffset, NULL); if (re == NULL) { x_printf ("ERROR: update_setlist failed to compile regex"); free (data); return; } ovector[0] = 0; ovector[1] = 0; while(1) { int options = 0; /* Normally no options */ int start_offset = ovector[1]; /* Start at end of previous match */ if (ovector[0] == ovector[1]) { if (ovector[0] == (int)data_len) break; } int rc = pcre_exec( re, /* the compiled pattern */ NULL, /* no extra data - we didn't study the pattern */ data, /* the subject string */ data_len, /* the length of the subject */ start_offset, /* starting offset in the subject */ options, /* options */ ovector, /* output vector for substring information */ OVECCOUNT); /* number of elements in the output vector */ if (rc == PCRE_ERROR_NOMATCH) { if (options == 0) break; ovector[1] = start_offset + 1; continue; /* Go round the loop again */ } /* Other matching errors are not recoverable. */ if (rc > 0) { /* Matched an RAID Set. Vectors: * 0=FullString 1(2)=Name 2(4)=Type 3(6)=Drives 4(8)=Size 5(10)=SizeUnits 6(12)=Unallocated 7(14)=UnallocatedUnits 8(16)=Comments */ /* Get Volume Name */ char *name_str; asprintf (&name_str, "%.*s", ovector[3] - ovector[2], data + ovector[2]); trim_end(name_str); x_debug ("update_setlist Matched %.*s\n", ovector[3] - ovector[2], data + ovector[2]); /* Find/Create Volume Entry */ struct raidSetTable_entry *entry = raidSetTable_head; while (entry) { if (!strcmp(name_str, entry->raidSetName)) break; entry = entry->next; } if (!entry) { last_index_used++; entry = raidSetTable_createEntry(last_index_used); entry->raidSetName = strdup(name_str); entry->raidSetName_len = strlen (entry->raidSetName); } entry->last_seen = now.tv_sec; free (name_str); name_str = NULL; /* Extract Data from Regex Match */ extract_string_in_range(data+ovector[4], ovector[5]-ovector[4], &entry->raidSetType, &entry->raidSetType_len); entry->raidSetSize = extract_uint_in_range(data+ovector[8], ovector[9]-ovector[8]); scale_value_to_m(data[ovector[10]], &entry->raidSetSize); entry->raidSetUnused = extract_uint_in_range(data+ovector[12], ovector[13]-ovector[12]); scale_value_to_m(data[ovector[14]], &entry->raidSetUnused); extract_string_in_range(data+ovector[16], ovector[17]-ovector[16], &entry->raidSetComments, &entry->raidSetComments_len); } else { pcre_free(re); /* Release memory used for the compiled pattern */ return; } } pcre_free(re); /* Release memory used for the compiled pattern */ /* Check for obsolete entries */ struct raidSetTable_entry *entry = raidSetTable_head; while (entry) { struct raidSetTable_entry *next = entry->next; if (entry->last_seen != now.tv_sec) { /* Entry is obsolete */ raidSetTable_removeEntry(entry); } entry = next; } /* Update cache timestamp */ gettimeofday(&cache_timestamp, NULL); /* Clean up */ free (data); data = NULL; data_len = 0; }
std::string trim(const std::string &str) { return trim_start(trim_end(str)); }
std::string trim_path_separator(const std::string& path) { return trim_end(path, filesystem::path_separator); }
void got_login (player * p, char *str) { char *fault; player *scan; str = trim_end (str); if ((fault = bad_name (str))) { writep (p, fault); login_prompt (p); return; } lower_case (str); sprintf (p->lower_name, str); if (player_load (p)) { if (system_flags & TEMP_CLOSED) if (!(p->residency & SU)) { writep (p, " Sorry, we're currently closed to connections. Try again later.\n\n"); p->flags |= P_LOGOUT; return; } promptp (p, "password: "******"quit")) { p->flags |= P_LOGOUT; return; } if (p->flags & P_RECON) { enter_arena (p, 0); return; } if (system_flags & NO_NEWBIES) { writep (p, " Sorry, but we're currently closed to newbies. Try again later.\n\n"); p->flags |= P_LOGOUT; return; } for (scan = p_top; scan; scan = scan->p_next) { if (!strcasecmp (str, scan->name)) { writep (p, " There is already someone on the program with that name.\n\n"); login_prompt (p); return; } } strncpy (p->name, str, P_NAME_LEN); strncpy (p->lower_name, p->name, P_NAME_LEN); lower_case (p->lower_name); writep (p, "\n"); writep (p, screen_one_msg.data); promptp (p, "[Hit <RETURN> to continue] "); p->func = screen_two; return; }
int run_command(player * p, char *str) { struct comm_list *comm; int found = 0, do_alias = 0; char *command, *params_ptr, *b; char params[512], temp[2]; p->idle = 0; if (!*str) return (0); while (*str == ' ') str++; str = trim_end(str); b = strpbrk(str, ",./?\\\"'[]{}:;<>|-_=+()*&^%$#@!~`"); if (str == b) do_alias = 1; switch (do_alias) { case 1: temp[0] = *str++; temp[1] = '\0'; command = strdup(temp); while (*str == ' ') str++; strcpy(params, str); break; default: case 0: command = strtok(str, " \t\n\r"); params_ptr = strtok(NULL, "\n\r"); if (params_ptr == NULL) memset(params, '\0', sizeof(params)); else strncpy(params, params_ptr, 511); break; } comm = (struct comm_list *) comm_hash[comm_hash_num(command)]; while (!found && comm) { if (p->privs & comm->level) { if (!strcasecmp(command, comm->key)) { char post[MAX_LINE+1]; char pr[MAX_LINE+1]; int wp = p->flags & WRAP_PROMPT ? 1: 0; found = 1; snprintf(post, MAX_LINE, "|int %s(struct s_player *, char *)|", comm->key); if(wp) { strncpy(pr, p->prompt, MAX_LINE); p->prompt[0] = 0; (comm->comm_func)(p, params); strncpy(p->prompt, pr, MAX_LINE); } else { (comm->comm_func)(p, params); } /* task_create(p, idle_loop_thread, post, comm->comm_func, p, tUser); */ prompt(p, p->prompt); } } comm = comm->next; } if (!found) { tell_player(p, "The command '%s' was not found.\n\r", command); prompt(p, p->prompt); } return (0); }