/* Delete global variable. */ void Var_Deletei(const char *name, const char *ename) { Var *v; uint32_t k; unsigned int slot; int idx; idx = classify_var(name, &ename, &k); if (idx != GLOBAL_INDEX) { Parse_Error(PARSE_FATAL, "Trying to delete dynamic variable $%s", varnames[idx]); return; } slot = ohash_lookup_interval(&global_variables, name, ename, k); v = ohash_find(&global_variables, slot); if (v == NULL) return; if (checkEnvFirst && (v->flags & VAR_FROM_ENV)) return; if (v->flags & VAR_FROM_CMD) return; ohash_remove(&global_variables, slot); delete_var(v); }
/* * build an input token.. * consider only those starting with _ or A-Za-z. */ static ndptr inspect(int c, char *tp) { char *name = tp; char *etp = tp+MAXTOK; ndptr p; *tp++ = c; while ((isalnum(c = gpbc()) || c == '_') && tp < etp) *tp++ = c; if (c != EOF) PUSHBACK(c); *tp = EOS; /* token is too long, it won't match anything, but it can still * be output. */ if (tp == ep) { outputstr(name); while (isalnum(c = gpbc()) || c == '_') { if (sp < 0) reallyputchar(c); else CHRSAVE(c); } *name = EOS; return NULL; } p = ohash_find(¯os, ohash_qlookupi(¯os, name, (const char **)&tp)); if (p == NULL) return NULL; if (macro_getdef(p) == NULL) return NULL; return p; }
struct macro_definition * lookup_macro_definition(const char *name) { ndptr p; p = ohash_find(¯os, ohash_qlookup(¯os, name)); if (p) return p->d; else return NULL; }
/* Create a new global var if necessary, and set it up correctly. * Do not take environment into account. */ static Var * find_global_var_without_env(const char *name, const char *ename, uint32_t k) { unsigned int slot; Var *v; slot = ohash_lookup_interval(&global_variables, name, ename, k); v = ohash_find(&global_variables, slot); if (v == NULL) { v = create_var(name, ename); v->flags = VAR_DUMMY; ohash_insert(&global_variables, slot, v); } return v; }
GNode * Targ_FindNodeih(const char *name, const char *ename, uint32_t hv, int flags) { GNode *gn; unsigned int slot; slot = ohash_lookup_interval(&targets, name, ename, hv); gn = ohash_find(&targets, slot); if (gn == NULL && (flags & TARG_CREATE)) { gn = Targ_NewGNi(name, ename); ohash_insert(&targets, slot, gn); } return gn; }
/* Add stuff to the toBeMade queue. we try to sort things so that stuff * that can be done directly is done right away. This won't be perfect, * since some dependencies are only discovered later (e.g., SuffFindDeps). */ static void add_targets_to_make(Lst todo) { GNode *gn; unsigned int slot; AppendList2Array(todo, &examine); while ((gn = Array_Pop(&examine)) != NULL) { if (gn->must_make) /* already known */ continue; gn->must_make = true; slot = ohash_qlookup(&targets, gn->name); if (!ohash_find(&targets, slot)) ohash_insert(&targets, slot, gn); look_harder_for_target(gn); kludge_look_harder_for_target(gn); /* * Apply any .USE rules before looking for implicit * dependencies to make sure everything that should have * commands has commands ... */ Lst_ForEach(&gn->children, MakeHandleUse, gn); expand_all_children(gn); if (gn->unmade != 0) { if (DEBUG(MAKE)) printf("%s: not queuing (%d unmade children)\n", gn->name, gn->unmade); Lst_ForEach(&gn->children, MakeAddChild, &examine); } else { if (DEBUG(MAKE)) printf("%s: queuing\n", gn->name); Array_Push(&toBeMade, gn); } } if (randomize_queue) randomize_garray(&toBeMade); }
/* * find name in the hash table */ ndptr lookup(const char *name) { return ohash_find(¯os, ohash_qlookup(¯os, name)); }
int mansearch(const struct mansearch *search, const struct manpaths *paths, int argc, char *argv[], struct manpage **res, size_t *sz) { int fd, rc, c, indexbit; int64_t pageid; uint64_t outbit, iterbit; char buf[PATH_MAX]; char *sql; struct manpage *mpage; struct expr *e, *ep; sqlite3 *db; sqlite3_stmt *s, *s2; struct match *mp; struct ohash_info info; struct ohash htab; unsigned int idx; size_t i, j, cur, maxres; info.calloc = hash_calloc; info.alloc = hash_alloc; info.free = hash_free; info.key_offset = offsetof(struct match, pageid); *sz = cur = maxres = 0; sql = NULL; *res = NULL; fd = -1; e = NULL; rc = 0; if (0 == argc) goto out; if (NULL == (e = exprcomp(search, argc, argv))) goto out; outbit = 0; if (NULL != search->outkey) { for (indexbit = 0, iterbit = 1; indexbit < mansearch_keymax; indexbit++, iterbit <<= 1) { if (0 == strcasecmp(search->outkey, mansearch_keynames[indexbit])) { outbit = iterbit; break; } } } /* * Save a descriptor to the current working directory. * Since pathnames in the "paths" variable might be relative, * and we'll be chdir()ing into them, we need to keep a handle * on our current directory from which to start the chdir(). */ if (NULL == getcwd(buf, PATH_MAX)) { perror("getcwd"); goto out; } else if (-1 == (fd = open(buf, O_RDONLY, 0))) { perror(buf); goto out; } sql = sql_statement(e); /* * Loop over the directories (containing databases) for us to * search. * Don't let missing/bad databases/directories phase us. * In each, try to open the resident database and, if it opens, * scan it for our match expression. */ for (i = 0; i < paths->sz; i++) { if (-1 == fchdir(fd)) { perror(buf); free(*res); break; } else if (-1 == chdir(paths->paths[i])) { perror(paths->paths[i]); continue; } c = sqlite3_open_v2(MANDOC_DB, &db, SQLITE_OPEN_READONLY, NULL); if (SQLITE_OK != c) { fprintf(stderr, "%s/%s: %s\n", paths->paths[i], MANDOC_DB, strerror(errno)); sqlite3_close(db); continue; } /* * Define the SQL functions for substring * and regular expression matching. */ c = sqlite3_create_function(db, "match", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, sql_match, NULL, NULL); assert(SQLITE_OK == c); c = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL, sql_regexp, NULL, NULL); assert(SQLITE_OK == c); j = 1; c = sqlite3_prepare_v2(db, sql, -1, &s, NULL); if (SQLITE_OK != c) fprintf(stderr, "%s\n", sqlite3_errmsg(db)); for (ep = e; NULL != ep; ep = ep->next) { if (NULL == ep->substr) { SQL_BIND_BLOB(db, s, j, ep->regexp); } else SQL_BIND_TEXT(db, s, j, ep->substr); if (0 == ((TYPE_Nd | TYPE_Nm) & ep->bits)) SQL_BIND_INT64(db, s, j, ep->bits); } memset(&htab, 0, sizeof(struct ohash)); ohash_init(&htab, 4, &info); /* * Hash each entry on its [unique] document identifier. * This is a uint64_t. * Instead of using a hash function, simply convert the * uint64_t to a uint32_t, the hash value's type. * This gives good performance and preserves the * distribution of buckets in the table. */ while (SQLITE_ROW == (c = sqlite3_step(s))) { pageid = sqlite3_column_int64(s, 2); idx = ohash_lookup_memory(&htab, (char *)&pageid, sizeof(uint64_t), (uint32_t)pageid); if (NULL != ohash_find(&htab, idx)) continue; mp = mandoc_calloc(1, sizeof(struct match)); mp->pageid = pageid; mp->form = sqlite3_column_int(s, 1); mp->bits = sqlite3_column_int64(s, 3); if (TYPE_Nd == outbit) mp->desc = mandoc_strdup((const char *) sqlite3_column_text(s, 0)); ohash_insert(&htab, idx, mp); } if (SQLITE_DONE != c) fprintf(stderr, "%s\n", sqlite3_errmsg(db)); sqlite3_finalize(s); c = sqlite3_prepare_v2(db, "SELECT sec, arch, name, pageid FROM mlinks " "WHERE pageid=? ORDER BY sec, arch, name", -1, &s, NULL); if (SQLITE_OK != c) fprintf(stderr, "%s\n", sqlite3_errmsg(db)); c = sqlite3_prepare_v2(db, "SELECT bits, key, pageid FROM keys " "WHERE pageid=? AND bits & ?", -1, &s2, NULL); if (SQLITE_OK != c) fprintf(stderr, "%s\n", sqlite3_errmsg(db)); for (mp = ohash_first(&htab, &idx); NULL != mp; mp = ohash_next(&htab, &idx)) { if (cur + 1 > maxres) { maxres += 1024; *res = mandoc_reallocarray(*res, maxres, sizeof(struct manpage)); } mpage = *res + cur; mpage->ipath = i; mpage->bits = mp->bits; mpage->sec = 10; mpage->form = mp->form; buildnames(mpage, db, s, mp->pageid, paths->paths[i], mp->form); mpage->output = TYPE_Nd & outbit ? mp->desc : outbit ? buildoutput(db, s2, mp->pageid, outbit) : NULL; free(mp); cur++; } sqlite3_finalize(s); sqlite3_finalize(s2); sqlite3_close(db); ohash_delete(&htab); /* * In man(1) mode, prefer matches in earlier trees * over matches in later trees. */ if (cur && search->firstmatch) break; } qsort(*res, cur, sizeof(struct manpage), manpage_compare); rc = 1; out: if (-1 != fd) { if (-1 == fchdir(fd)) perror(buf); close(fd); } exprfree(e); free(sql); *sz = cur; return(rc); }