tn_array *capreq_arr_restore_st(tn_alloc *na, tn_stream *st) { struct restore_struct rs = { na, NULL }; n_buf_restore_ex(st, NULL, TN_BUF_STORE_16B, (int (*)(tn_buf *, void*))capreq_arr_restore_fn, &rs); DBGF("AFTER capreq_arr_restore_f %lu, %lu\n", off, n_stream_tell(st)); return rs.arr; }
tn_buf *capreq_arr_store(tn_array *arr, tn_buf *nbuf, int n) { int32_t size; int16_t arr_size; int i, off; n_assert(n_array_size(arr) < INT16_MAX); arr_size = n; if (n == 0) { for (i=0; i < n_array_size(arr); i++) { struct capreq *cr = n_array_nth(arr, i); if (!capreq_is_bastard(cr)) arr_size++; } } n_assert(arr_size); if (arr_size == 0) return NULL; n_array_isort_ex(arr, (tn_fn_cmp)capreq_strcmp_name_evr); if (nbuf == NULL) nbuf = n_buf_new(16 * arr_size); off = n_buf_tell(nbuf); n_buf_add_int16(nbuf, 0); /* fake size */ n_buf_add_int16(nbuf, arr_size); for (i=0; i < n_array_size(arr); i++) { struct capreq *cr = n_array_nth(arr, i); if (!capreq_is_bastard(cr)) { capreq_store(cr, nbuf); //printf("STORE %s %d -> %d\n", capreq_snprintf_s(cr), // strlen(capreq_snprintf_s(cr)), capreq_sizeof(cr)); } } n_buf_puts(nbuf, "\n"); //printf("tells %d\n", n_buf_tell(nbuf)); size = n_buf_tell(nbuf) - off - sizeof(uint16_t); n_assert(size < UINT16_MAX); n_buf_seek(nbuf, off, SEEK_SET); n_buf_add_int16(nbuf, size); n_buf_seek(nbuf, 0, SEEK_END); DBGF("capreq_arr_store %d (at %d), arr_size = %d\n", size, off, arr_size); return nbuf; }
static struct tndb *do_open_dscr(struct pndir *idx, int vfmode, struct vfile **vf, time_t ts, const char *lang) { char buf[128], tmpath[PATH_MAX], tss[32]; const char *suffix; char *idxpath; const char *dbid, *langid; pndir_db_dscr_idstr(lang, &dbid, &langid); idxpath = idx->idxpath; *tss = '\0'; if (ts) { int len, tss_len; char *p; tss_len = pndir_tsstr(tss, sizeof(tss), ts); len = strlen(idx->idxpath) + 1; idxpath = alloca(len); memcpy(idxpath, idx->idxpath, len); if ((p = strstr(idxpath, tss)) && p != idxpath && *(p - 1) == '.') { //*(p - 1) = '\0'; strcpy(p - 1, p + tss_len); } else { *tss = '\0'; } } suffix = pndir_desc_suffix; if (*langid == '\0') { if (*tss) { snprintf(buf, sizeof(buf), "%s.%s", pndir_desc_suffix, tss); suffix = buf; } } else { snprintf(buf, sizeof(buf), "%s.%s%s%s", pndir_desc_suffix, langid, *tss ? "." : "", *tss ? tss : ""); suffix = buf; } DBGF("mk %s, %s\n", idxpath, suffix); pndir_mkidx_pathname(tmpath, sizeof(tmpath), idxpath, suffix); msgn(3, _("Opening %s..."), vf_url_slim_s(tmpath, 0)); return do_dbopen(tmpath, vfmode, vf, idx->srcnam); }
static void pkg_store_fl(const struct pkg *pkg, tn_buf *nbuf, tn_array *exclpath, tn_array *depdirs, unsigned flags) { struct pkgflist *flist; if ((flags & PKGSTORE_NOANYFL) == PKGSTORE_NOANYFL) return; DBGF("%s\n", pkg_snprintf_s(pkg)); flist = pkg_get_flist(pkg); DBGF("flist = %p\n", flist); if (flist == NULL) return; if (depdirs == NULL) { if ((flags & PKGSTORE_NOFL) == 0) { pkg_store_bintag(PKG_STORETAG_FL, nbuf); pkgfl_store(flist->fl, nbuf, exclpath, depdirs, PKGFL_ALL); } } else { if ((flags & PKGSTORE_NODEPFL) == 0) { pkg_store_bintag(PKG_STORETAG_DEPFL, nbuf); pkgfl_store(flist->fl, nbuf, exclpath, depdirs, PKGFL_DEPDIRS); } if ((flags & PKGSTORE_NOFL) == 0) { pkg_store_bintag(PKG_STORETAG_FL, nbuf); pkgfl_store(flist->fl, nbuf, exclpath, depdirs, PKGFL_NOTDEPDIRS); } } DBGF("END %s\n", pkg_snprintf_s(pkg)); pkgflist_free(flist); }
const struct ike_alg *alg_byname(struct proposal_parser *parser, const struct ike_alg_type *type, shunk_t name, shunk_t print_name) { const struct proposal_protocol *protocol = parser->protocol; const struct ike_alg *alg = ike_alg_byname(type, name); if (alg == NULL) { /* * Known at all? Poke around in the enum tables to * see if it turns up. */ if (ike_alg_enum_match(type, protocol->ikev1_alg_id, name) >= 0 || ike_alg_enum_match(type, IKEv2_ALG_ID, name) >= 0) { proposal_error(parser, "%s %s algorithm '"PRI_SHUNK"' is not supported", protocol->name, ike_alg_type_name(type), PRI_shunk(print_name)); } else { proposal_error(parser, "%s %s algorithm '"PRI_SHUNK"' is not recognized", protocol->name, ike_alg_type_name(type), PRI_shunk(print_name)); } passert(parser->error[0] != '\0'); DBGF(DBG_PROPOSAL_PARSER, "ike_alg_byname() failed: %s", parser->error); return NULL; } /* * Does it pass muster? */ if (!alg_byname_ok(parser, alg, print_name)) { passert(parser->error[0] != '\0'); DBGF(DBG_PROPOSAL_PARSER, "alg_byname_ok() failed: %s", parser->error); return NULL; } return alg; }
static int is_upgradeable(struct poclidek_ctx *cctx, struct pkg *pkg, int reverse) { struct pkg *ipkg = NULL; tn_array *dents; char name[256]; int n, name_len; if (reverse) dents = poclidek_get_dent_ents(cctx, POCLIDEK_AVAILDIR); else dents = poclidek_get_dent_ents(cctx, POCLIDEK_INSTALLEDDIR); if (dents == NULL) return 1; name_len = snprintf(name, sizeof(name), "%s-", pkg->name); n = n_array_bsearch_idx_ex(dents, name, (tn_fn_cmp)pkg_dent_strncmp); if (n == -1) return 0; while (n < n_array_size(dents)) { struct pkg_dent *ent = n_array_nth(dents, n++); int cmprc; if (pkg_dent_isdir(ent)) continue; if (strncmp(name, ent->name, name_len) != 0) break; ipkg = ent->pkg_dent_pkg; if (!pkg_is_kind_of(ipkg, pkg)) continue; if ((cmprc = pkg_cmp_evr(pkg, ipkg)) != 0) { DBGF("%s %s %d (%d)\n", pkg_id(pkg), pkg_id(ipkg), cmprc, reverse); if (!reverse && cmprc > 0) return 1; if (reverse && cmprc < 0) return 1; } } return 0; }
/* * Kick the watchdog. * This is done by sending a heartbeat message to the board controller watchdog * through drbcc-core. */ static int wd_keepalive(void) { int ret; struct bcc_packet pkt = { .cmd = DRBCC_REQ_HEARTBEAT, .payloadlen = 2, }; pkt.data[0] = timeout >> 8; pkt.data[1] = timeout; DBGF(BWD "Send timeout to board controller (current timeout: %d).", timeout); if ((ret = transmit_packet(&pkt)) < 0) { ERR(BWD "Error while trying to send heartbeat to board controller."); return -EFAULT; } if(pkt.cmd != cmd_responses[DRBCC_REQ_HEARTBEAT]) { if(pkt.cmd == DRBCC_TIMEOUT) { DBG("Waiting for ACK for HEARTBEAT message timed out."); } else { DBGF("Received message with wrong response type: %x", pkt.cmd); } return -EFAULT; } return ret; } static void blocking_wd_keepalive_thread(struct work_struct *work) { wd_keepalive(); queue_delayed_work(timeout_keepalive_wq, &tm_work, WD_KEEPALIVE_TIME*HZ); }
/** * @brief Serialize the supplied RSA public key to the supplied buffer. * * Stores the RSA public key in the supplied buffer. If the space required is * larger than len (the size of the buffer), the only the first len bytes will * be placed in the buffer. Check that the return value is <= len. * * @param keypair The keypair to serialize * @param buf Buffer for the result. * @param len Size of the buffer. * * @return The size of the serialized keypair. This may be bigger than len, * indicating the result has been truncated and should not be used. */ uint32_t serialize_rsa_pub_key(RSA *keypair, char *buf, size_t len) { size_t bufSpaceLeft = len; char *hexformod = BN_bn2hex(keypair->n); uint32_t hexformodsize = strlen(hexformod); char *hexforexp = BN_bn2hex(keypair->e); uint32_t hexforexpsize = strlen(hexforexp); DBGF("hexformod: %s (%d)", hexformod, hexformodsize); DBGF("hexforexp: %s (%d)", hexforexp, hexforexpsize); if (bufSpaceLeft > sizeof(uint32_t)) { uint32_t *modsizeptr = (uint32_t*)buf; *modsizeptr = hexformodsize; bufSpaceLeft -= sizeof(uint32_t); } if (bufSpaceLeft > hexformodsize) { memcpy(&buf[sizeof(uint32_t)], hexformod, hexformodsize); bufSpaceLeft -= hexformodsize; } if (bufSpaceLeft > sizeof(uint32_t)) { uint32_t *expsizeptr = (uint32_t*)&buf[sizeof(uint32_t) + hexformodsize]; *expsizeptr = hexforexpsize; bufSpaceLeft -= sizeof(uint32_t); } if (bufSpaceLeft > hexforexpsize) { memcpy(&buf[2*sizeof(uint32_t) + hexformodsize], hexforexp, hexforexpsize); bufSpaceLeft -= hexforexpsize; } uint32_t total_size = strlen(hexformod) + strlen(hexforexp) + 2*sizeof(uint32_t); free(hexformod); free(hexforexp); return total_size; }
/* ------------------------------------------------------------------------ * lua: x = eventloop.new() -- create a new eventloop object */ static int l_new (lua_State *L) { struct lel_eventloop *el; DBGF("** eventloop.new () **\n"); el = (struct lel_eventloop*)lua_newuserdata(L, sizeof (struct lel_eventloop)); luaL_getmetatable (L, L_EVENTLOOP_MT); lua_setmetatable (L, -2); memset (el, 0, sizeof(*el)); FD_ZERO (&el->all_fds); return 1; }
/* Return pointer to value in table or NULL if not in table. */ void * hashtable_get(HashTable *table, const char *key, uint32_t len) { Entry *pEntry; void *result = NULL; uint64_t hash = HASH(key, len); /* TODO: Check if table is init'd. */ //DBGF("Get: key %s hashed to %lu.\n", key, hash); pEntry = table->entries[hash & table->mask]; DBGF("Get: chain head @ %p.\n", pEntry); while (!result && pEntry) { if (hashtable_entry_test(pEntry, hash, key, len)) { result = pEntry->value; } pEntry = pEntry->pNext; } return result; }
static int __init drbcc_wd_init_module(void) { int ret = 0; DBGF("HydraIP DRBCC Watchdog driver: %s.\n", __FUNCTION__); ret = misc_register(&drbcc_wd_miscdev); if (ret) { ERR(BWD "Cannot register miscdev on minor=%d (err=%d)\n", drbcc_wd_miscdev.minor, ret); } timeout_keepalive_wq = create_singlethread_workqueue("timeout_blocking_keepalive_wq"); wd_timer_start(); return ret; }
/* find packages satisfies req and (optionally) best fitted to pkg */ int pkgset_find_match_packages(struct pkgset *ps, const struct pkg *pkg, const struct capreq *req, tn_array **packages, int strict) { struct pkg **suspkgs, pkgsbuf[1024], **matches; int nsuspkgs = 0, nmatches = 0, found = 0; nsuspkgs = 1024; /* size of pkgsbuf */ found = psreq_lookup(ps, req, &suspkgs, (struct pkg **)pkgsbuf, &nsuspkgs); if (!found) return found; if (nsuspkgs == 0) /* self match or rpmlib() or other internal caps */ return found; #if ENABLE_TRACE do { int i; DBGF("%s: found %d suspected packages: ", capreq_snprintf_s(req), nsuspkgs); for (i=0; i < nsuspkgs; i++) msg(0, "%s, ", pkg_id(suspkgs[i])); msg("\n"); } while(0); #endif found = 0; matches = alloca(sizeof(*matches) * nsuspkgs); if (psreq_match_pkgs(pkg, req, strict, suspkgs, nsuspkgs, matches, &nmatches)) { found = 1; if (nmatches && packages) { int i; if (*packages == NULL) *packages = pkgs_array_new(nmatches); for (i=0; i < nmatches; i++) n_array_push(*packages, pkg_link(matches[i])); } } return found; }
static void hashtable_resize(HashTable *table) { Entry **new_entries; Entry *pEntry, *pNext, *pTail; uint32_t pos = 0; uint32_t new_size = table->size << 1; uint32_t new_mask = new_size - 1; DBGF("Resize: Resizing to %u.\n", new_size); /* PHP handles this using realloc as they have a linked list over all entries to maintain order. */ new_entries = calloc(new_size, sizeof(Entry *)); if (!new_entries) { DBG("Resize: Warning hash table resize failed."); } else { for (;pos < table->size; pos++) { pEntry = table->entries[pos]; while (pEntry) { pNext = pEntry->pNext; if (new_entries[pEntry->hash & new_mask]) { pTail = new_entries[pEntry->hash & new_mask]; /* Find the tail */ while (pTail->pNext) { pTail = pTail->pNext; } pTail->pNext = pEntry; pEntry->pPrev = pTail; pEntry->pNext = NULL; } else { /* Item is to be head in new entries */ new_entries[pEntry->hash & new_mask] = pEntry; pEntry->pPrev = NULL; pEntry->pNext = NULL; } pEntry = pNext; } } /* Free what we had and update struct. */ free(table->entries); table->entries = new_entries; table->size = new_size; table->mask = new_mask; } }
static int pdir_difftoc_update(const char *diffpath, const char *suffix, const char *line, int line_len) { char path[PATH_MAX]; struct vfile *vf; int rc = 0; if (mkidx_pathname(path, sizeof(path), diffpath, suffix) == NULL) return 0; if ((vf = vfile_open(path, VFT_TRURLIO, VFM_APPEND))) { rc = (n_stream_write(vf->vf_tnstream, line, line_len) == line_len); vfile_close(vf); } DBGF("%s: added %s: [%s]\n", path, line, rc ? "OK" : "FAILED"); return rc; }
/* Set recodable pkgu member, set _flags accordinggly to trigger recoding in pkguinf_get() */ static void pkgu_set_recodable(struct pkguinf *pkgu, int tag, char *val, const char *lang) { char **member = NULL; unsigned doneflag = 0, needflag = 0; char *usrencoding = NULL; switch (tag) { case PKGUINF_SUMMARY: member = &pkgu->_summary; doneflag = SUMMARY_RECODED; needflag = RECODE_SUMMMARY; break; case PKGUINF_DESCRIPTION: member = &pkgu->_description; doneflag = DESCRITPION_RECODED; needflag = RECODE_SUMMMARY; break; default: n_assert(0); break; } if (*member && (pkgu->_flags & doneflag)) { free((char*)*member); *member = NULL; } *member = val; pkgu->_flags &= ~needflag; if (strstr(lang, "UTF-8") == NULL) { *member = val; return; } usrencoding = nl_langinfo(CODESET); DBGF("CODE %s\n", usrencoding); if (usrencoding && n_str_ne(usrencoding, "UTF-8")) pkgu->_flags |= needflag; }
static int run_ipoldek(struct poclidek_ctx *cctx) { int rc = 1; #if ENABLE_TRACE i = 0; DBGF("verbose %d, argc = %d\n", verbose, args.argc); while (args.argv[i]) printf(" %s", args.argv[i++]); printf("\n"); #endif if (args.argc > 0) rc = poclidek_exec(cctx, args.ts, args.argc, (const char **)args.argv); else /* lonely ipoldek -> run shell as poldek does */ rc = run_poldek(cctx); return rc; }
static int do_source_clean(struct source *src, const char *idxdir, const char *idxbn, unsigned flags) { int urltype; n_assert(src->type); if ((urltype = vf_url_type(idxdir)) == VFURL_UNKNOWN) return 1; DBGF("%s: %s, %s\n", src->path, idxdir, idxbn); /* clean-pkg makes no sense for local repositories */ if ((urltype & VFURL_LOCAL) && (flags & PKGSOURCE_CLEAN)) { char path[PATH_MAX]; vf_localdirpath(path, sizeof(path), idxdir); pkgdir__cache_clean(path, "*", flags & PKGSOURCE_CLEAN_TEST); } else { char amask[1024], *mask = NULL; if ((flags & PKGSOURCE_CLEANA) == PKGSOURCE_CLEANA) { mask = "*"; } else if (flags & PKGSOURCE_CLEAN) { n_assert(idxbn); n_snprintf(amask, sizeof(amask), "%s.*", idxbn); mask = amask; } else { // (flags & PKGSOURCE_CLEANPKG -- default n_snprintf(amask, sizeof(amask), "*.rpm"); mask = amask; } n_assert(mask); pkgdir__cache_clean(idxdir, mask, flags & PKGSOURCE_CLEAN_TEST); } return 1; }
int poclidek_load_installed(struct poclidek_ctx *cctx, int reload) { struct pkgdir *pkgdir; DBGF("%d\n", reload); if (cctx->dbpkgdir && !reload) return 0; if ((pkgdir = load_installed_pkgdir(cctx, reload)) == NULL) return 0; if ((poclidek_dent_find(cctx, POCLIDEK_INSTALLEDDIR)) == NULL || reload) poclidek_dent_setup(cctx, POCLIDEK_INSTALLEDDIR, pkgdir->pkgs, reload); if (cctx->dbpkgdir) pkgdir_free(cctx->dbpkgdir); cctx->pkgs_installed = pkgdir->pkgs; /* "weak" ref */ cctx->dbpkgdir = pkgdir; cctx->ts_dbpkgdir = pkgdir->ts; return 1; }
static void wbsd_tasklet_timeout(unsigned long param) { struct wbsd_host* host = (struct wbsd_host*)param; struct mmc_data* data; spin_lock(&host->lock); WARN_ON(!host->mrq); if (!host->mrq) goto end; data = wbsd_get_data(host); if (!data) goto end; DBGF("Timeout\n"); data->error = MMC_ERR_TIMEOUT; tasklet_schedule(&host->finish_tasklet); end: spin_unlock(&host->lock); }
static void wbsd_tasklet_block(unsigned long param) { struct wbsd_host* host = (struct wbsd_host*)param; struct mmc_data* data; spin_lock(&host->lock); if ((wbsd_read_index(host, WBSD_IDX_CRCSTATUS) & WBSD_CRC_MASK) != WBSD_CRC_OK) { data = wbsd_get_data(host); if (!data) goto end; DBGF("CRC error\n"); data->error = MMC_ERR_BADCRC; tasklet_schedule(&host->finish_tasklet); } end: spin_unlock(&host->lock); }
static inline char *register_capreq_name(const char *name, int len) { char *nname; if (capreqname_h == NULL) { capreqname_na = n_alloc_new(128, TN_ALLOC_OBSTACK); capreqname_h = n_hash_new_na(capreqname_na, 1024 * 64, NULL); n_hash_ctl(capreqname_h, TN_HASH_NOCPKEY); } capreqname_cnt++; if (capreqname_cnt % 10000 == 0) DBGF_F("cnt %d\n", capreqname_cnt); DBGF("%s\n", name); if ((nname = n_hash_get(capreqname_h, name))) return nname; if (len == 0) len = strlen(name); nname = capreqname_na->na_malloc(capreqname_na, len + 1); memcpy(nname, name, len + 1); n_hash_insert(capreqname_h, nname, nname); return nname; }
int do_poclidek_execline(struct poclidek_ctx *cctx, struct poldek_ts *ts, const char *cmdline, struct cmd_pipe *cmd_pipe) { tn_array *cmd_chain; int rc = 0, i, verbose; DBGF("%s\n", cmdline); /* keep verbose setting as it changes when '-q' option is used */ verbose = poldek_verbose(); cmd_chain = poclidek_prepare_cmdline(cctx, cmdline); if (cmd_chain == NULL) return 0; for (i=0; i < n_array_size(cmd_chain); i++) { struct cmd_chain_ent *ent; ent = n_array_nth(cmd_chain, i); if (ent->flags & (CMD_CHAIN_ENT_SEMICOLON | CMD_CHAIN_ENT_PIPE)) { n_assert(0); continue; } if (cmd_pipe && i == n_array_size(cmd_chain) - 1) rc = poclidek_exec_cmd_ent(cctx, ts, ent, cmd_pipe); else rc = poclidek_exec_cmd_ent(cctx, ts, ent, NULL); } /* restore verbose setting */ poldek_set_verbose(verbose); n_array_free(cmd_chain); return rc; }
/* ------------------------------------------------------------------------ * lua: x = ixp.new("unix!/tmp/ns.bart.:0/wmii") -- create a new ixp object */ static int l_new (lua_State *L) { const char *adr; IxpClient *cli; struct ixp *ixp; adr = luaL_checkstring (L, 1); DBGF("** ixp.new ([%s]) **\n", adr); cli = ixp_mount(adr); if (!cli) return lixp_pusherror (L, "could not open ixp connection"); ixp = (struct ixp*)lua_newuserdata(L, sizeof (struct ixp)); luaL_getmetatable (L, L_IXP_MT); lua_setmetatable (L, -2); ixp->address = strdup (adr); ixp->client = cli; return 1; }
static void isort_pkgs(struct pkg *pkgs[], size_t size) { register size_t i, j; #if ENABLE_TRACE printf("before isort(): "); for (i = 0; i < size; i++) { register struct pkg *p = pkgs[i]; printf("%s, ", pkg_id(p)); } printf("\n"); #endif for (i = 1; i < size; i++) { register void *tmp = pkgs[i]; j = i; while (j > 0 && pkg_cmp_name_evr_rev(tmp, pkgs[j - 1]) < 0) { DBGF(" %s < %s\n", pkg_id(tmp), pkg_id(pkgs[j - 1])); pkgs[j] = pkgs[j - 1]; j--; } pkgs[j] = tmp; } #if ENABLE_TRACE printf("after isort(): "); for (i = 0; i < size; i++) { register struct pkg *p = pkgs[i]; printf("%s, ", pkg_id(p)); } printf("\n"); #endif }
/* trying to resolve conflict by package upgrade */ static int resolve_conflict(int indent, struct i3ctx *ictx, struct pkg *pkg, const struct capreq *cnfl, struct pkg *dbpkg) { struct capreq *req = NULL; struct pkg *tomark = NULL; tn_array *candidates = NULL; int found = 0, by_replacement = 0; if (!ictx->ts->getop(ictx->ts, POLDEK_OP_FOLLOW)) return 0; if (!capreq_versioned(cnfl)) return 0; req = capreq_clone(NULL, cnfl); #if 0 /* debug */ printf("B %s -> ", capreq_stra(req)); capreq_revrel(req); printf("%s -> ", capreq_stra(req)); capreq_revrel(req); printf("%s\n", capreq_stra(req)); #endif DBGF("find_req %s %s\n", pkg_id(pkg), capreq_stra(req)); capreq_revrel(req); DBGF("find_req %s %s\n", pkg_id(pkg), capreq_stra(req)); if (i3_is_user_choosable_equiv(ictx->ts)) candidates = pkgs_array_new(8); found = i3_find_req(indent, ictx, pkg, req, &tomark, candidates); capreq_revrel(req); if (!found) { found = find_replacement(ictx, dbpkg, cnfl, &tomark); by_replacement = 1; } else { struct pkg *real_tomark = tomark; /* tomark == NULL ? req satsfied by already installed set */ if (tomark && candidates && n_array_size(candidates) > 1) { real_tomark = i3_choose_equiv(ictx->ts, pkg, req, candidates, tomark); n_array_cfree(&candidates); if (real_tomark == NULL) { /* user aborts */ ictx->abort = 1; found = 0; } } tomark = real_tomark; } if (!found) goto l_end; if (tomark == NULL) /* already in inset */ goto l_end; found = 0; if (by_replacement || pkg_obsoletes_pkg(tomark, dbpkg)) { struct i3pkg *i3tomark; found = 1; i3tomark = i3pkg_new(tomark, 0, pkg, req, I3PKGBY_REQ); i3_process_package(indent, ictx, i3tomark); } l_end: n_array_cfree(&candidates); capreq_free(req); return found; }
static struct capreq *capreq_restore(tn_alloc *na, tn_buf_it *nbufi) { struct capreq *cr; uint8_t size8, name_len, head0 = 0; uint8_t flagsbuf[5], *flagsbuf_p; /* flagsbuf is placeholder, for sizeof */ char *name, *cr_bufp = NULL; int size; n_buf_it_get_int8(nbufi, &size8); size = size8; flagsbuf_p = n_buf_it_get(nbufi, sizeof(flagsbuf)); if (flagsbuf_p == NULL) return NULL; size -= sizeof(flagsbuf); /* flags are read */ cr = alloca(sizeof(*cr) + size + 1); cr_bufp = n_buf_it_get(nbufi, size); name_len = size; name = cr_bufp; if (*name == '\0') { /* < 0.19 */ name++; name_len--; head0 = 1; /* poldek < 0.19 */ } if (flagsbuf_p[0] & REL_ALL) { /* capreq_versioned(cr) */ char *p = strchr(name, '\0'); /* end of name */ name_len = p - name; cr_bufp = p + 1; /* just after name's '\0' */ } else { /* no version => no trailing '\0' */ char *nam = alloca(name_len + 1); memcpy(nam, name, name_len); nam[name_len] = '\0'; name = nam; cr_bufp = NULL; /* no more data */ } DBGF("name = %s, %d, %d, size = %d, head0 = %d\n", name, name_len, strlen(name), size, head0); size -= name_len + head0; n_assert(size >= 0); cr = capreq_malloc(na, size); if (size) { n_assert(cr_bufp); memcpy(&cr->_buf[1], cr_bufp, size); } cr->name = register_capreq_name(name, name_len); /* restore flags */ cr->cr_relflags |= flagsbuf_p[0]; /* relflags are setup by capreq_malloc */ cr->cr_flags = flagsbuf_p[1]; cr->cr_ep_ofs = flagsbuf_p[2]; cr->cr_ver_ofs = flagsbuf_p[3]; cr->cr_rel_ofs = flagsbuf_p[4]; if (head0 && capreq_versioned(cr)) { register int diff = name_len + head0; if (cr->cr_ep_ofs) cr->cr_ep_ofs -= diff; if (cr->cr_ver_ofs) cr->cr_ver_ofs -= diff; if (cr->cr_rel_ofs) cr->cr_rel_ofs -= diff; } if (cr->cr_ep_ofs) { int32_t epoch = n_ntoh32(capreq_epoch(cr)); memcpy(&cr->_buf[cr->cr_ep_ofs], &epoch, sizeof(epoch)); } DBGF("%s\n", capreq_snprintf_s(cr)); //printf("cr %s: %d, %d, %d, %d, %d\n", capreq_snprintf_s(cr), //cr_bufp[0], cr_bufp[1], cr_bufp[2], cr_bufp[3], cr_bufp[4]); //printf("REST* %s %d -> %d\n", capreq_snprintf_s(cr), // strlen(capreq_snprintf_s(cr)), capreq_sizeof(cr)); return cr; }
/* executes command chain (a pipeline) */ static int poclidek_exec_cmd_ent(struct poclidek_ctx *cctx, struct poldek_ts *ts, struct cmd_chain_ent *ent, struct cmd_pipe *cmd_pipe) { struct cmdctx cmdctx; char **argv; int rc = 0, runit = 1; DBGF("ent %s, %d, %p\n", ent->cmd->name, n_array_size(ent->a_argv), ent->next_piped); memset(&cmdctx, 0, sizeof(cmdctx)); cmdctx.cmd = ent->cmd; cmdctx.cctx = cctx; if ((cmdctx.ts = ts) == NULL) cmdctx.ts = poldek_ts_new(cctx->ctx, 0); if (ent->next_piped) { ent->pipe_right = cmd_pipe_new(); cmdctx.pipe_right = ent->pipe_right; } else if (cmd_pipe) { DBGF("piped %s\n", ent->cmd->name); ent->pipe_right = cmd_pipe_link(cmd_pipe); cmdctx.pipe_right = ent->pipe_right; } if (ent->prev_piped) { /* | cmd */ struct cmd_pipe *pipe; tn_array *pipe_args = NULL; pipe = ent->prev_piped->pipe_right; ent->prev_piped->pipe_right = NULL; cmdctx.pipe_left = pipe; if (ent->cmd->flags & COMMAND_PIPE_XARGS) { if (ent->cmd->flags & COMMAND_PIPE_PACKAGES) pipe_args = cmd_pipe_xargs(pipe, CMD_PIPE_CTX_PACKAGES); else pipe_args = cmd_pipe_xargs(pipe, CMD_PIPE_CTX_ASCII); if (pipe_args == NULL) { runit = 0; /* do not execute command if pipe is empty */ rc = 0; goto l_end; } else { while (n_array_size(pipe_args)) n_array_push(ent->a_argv, n_array_shift(pipe_args)); n_array_free(pipe_args); } } } argv = alloca((n_array_size(ent->a_argv) + 1) * sizeof(*argv)); a_argv_to_argv(ent->a_argv, argv); rc = do_exec_cmd_ent(&cmdctx, n_array_size(ent->a_argv), argv); l_end: if (ts == NULL) poldek_ts_free(cmdctx.ts); if (runit && ent->next_piped) return poclidek_exec_cmd_ent(cctx, ts, ent->next_piped, cmd_pipe); return rc; }
static int do_exec_cmd_ent(struct cmdctx *cmdctx, int argc, char **argv) { int rc = 1; unsigned parse_flags; struct poclidek_cmd *cmd = cmdctx->cmd; struct argp cmd_argp = { cmd->argp_opts, cmd->parse_opt_fn, cmd->arg, cmd->doc, 0, 0, 0}; struct argp_child cmd_argp_child[2] = { { &cmd_argp, 0, NULL, 0 }, { NULL, 0, NULL, 0 }, }; struct argp argp = { common_options, parse_opt, 0, 0, cmd_argp_child, 0, 0 }; if (argv == NULL) return 0; cmd = cmdctx->cmd; if (poclidek_argv_is_help(argc, (const char**)argv)) { cmdctx->rtflags |= CMDCTX_ISHELP; DBGF("is_help!\n"); } if (poldek_verbose() < 0) cmdctx->rtflags |= CMDCTX_NOCTRLMSGS; cmdctx->_data = NULL; if (cmd->init_cmd_arg_d) cmdctx->_data = cmd->init_cmd_arg_d(); if (cmd->cmd_fn) { /* option parses its args itself */ DBGF("run cmd_fn(arc, argv)\n"); rc = cmd->cmd_fn(cmdctx, argc, (const char**)argv, &argp); goto l_end; } if ((cmd->flags & COMMAND_NOHELP) && (cmd->flags & COMMAND_NOARGS) && (cmd->flags & COMMAND_NOOPTS)) { rc = cmd->do_cmd_fn(cmdctx); goto l_end; } argp.help_filter = help_filter; parse_flags = argp_parse_flags; argp_parse(&argp, argc, (char**)argv, parse_flags, 0, cmdctx); if (cmdctx->rtflags & CMDCTX_ERR) { rc = 0; goto l_end; } if (cmdctx->rtflags & CMDCTX_ISHELP) { rc = 1; goto l_end; } rc = cmd->do_cmd_fn(cmdctx); l_end: if (cmd->destroy_cmd_arg_d && cmdctx->_data) cmd->destroy_cmd_arg_d(cmdctx->_data); return rc; }
/* default parse_opt */ static error_t parse_opt(int key, char *arg, struct argp_state *state) { struct cmdctx *cmdctx = state->input; int rc = 0; switch (key) { case ARGP_KEY_INIT: state->child_inputs[0] = cmdctx; state->child_inputs[1] = NULL; break; case 'v': { if ((cmdctx->cmd->flags & COMMAND_HASVERBOSE) == 0) { argp_usage (state); cmdctx->rtflags |= CMDCTX_ERR; } else { poldek_set_verbose(poldek_VERBOSE + 1); } } break; case 'q': cmdctx->rtflags |= CMDCTX_NOCTRLMSGS; poldek_set_verbose(-1); break; case ARGP_KEY_ARG: DBGF("cli.arg %s, %d\n", arg, cmdctx->cmd->flags & COMMAND_SELFARGS); if (cmdctx->cmd->flags & COMMAND_SELFARGS) return ARGP_ERR_UNKNOWN; if (cmdctx->cmd->flags & COMMAND_NOARGS) { argp_usage (state); cmdctx->rtflags |= CMDCTX_ERR; return EINVAL; } poldek_ts_add_pkgmask(cmdctx->ts, arg); break; case 'h': argp_state_help(state, stdout, ARGP_HELP_LONG | ARGP_HELP_DOC | ARGP_HELP_USAGE); return EINVAL; break; case ARGP_KEY_NO_ARGS: DBGF("NOARGS %d\n", cmdctx->rtflags & (CMDCTX_ISHELP|CMDCTX_GOTARGS)); if (cmdctx->rtflags & (CMDCTX_ISHELP | CMDCTX_GOTARGS)) break; if ((cmdctx->cmd->flags & (COMMAND_NOARGS|COMMAND_EMPTYARGS)) == 0) { argp_usage (state); cmdctx->rtflags |= CMDCTX_ERR; return EINVAL; } break; case ARGP_KEY_ERROR: cmdctx->rtflags |= CMDCTX_ERR; return EINVAL; break; default: return ARGP_ERR_UNKNOWN; } //printf("key = %d, rc = %d\n", key, rc); return rc; }
int poclidek_shell(struct poclidek_ctx *cctx) { const char *prompt_prefix = "poldek"; char *line, *s, *home; if (cctx->htcnf) { tn_hash *global = poldek_conf_get_section(cctx->htcnf, "global"); const char *s = global ? poldek_conf_get(global, "prompt", NULL) : NULL; if (s) { prompt_prefix = s; DBGF("prompt_prefix %s\n", s); } } if (!isatty(fileno(stdout))) { logn(LOGERR, _("not a tty")); return 0; } if (!init_shell(cctx)) exit(EXIT_FAILURE); initialize_readline(); histfile = NULL; if ((home = getenv("HOME"))) { int len = strlen(home) + strlen("/.poldek_history") + 2; histfile = alloca(len); snprintf(histfile, len, "%s/.poldek_history", home); read_history(histfile); } sigint_init(); sigint_push(sigint_cb); signal(SIGTERM, shell_end); signal(SIGQUIT, shell_end); printf(_("\nWelcome to the poldek shell mode. " "Type \"help\" for help with commands.\n\n")); shDone = 0; while (!shDone) { struct pkg_dent *currdir = sh_ctx.cctx->currdir; char prompt[255]; sigint_reset(); n_snprintf(prompt, sizeof(prompt), "%s:%s%s> ", prompt_prefix, currdir == NULL ? "/" : *currdir->name == '/' ? "" : "/", currdir == NULL ? "" : currdir->name); if ((line = readline(prompt)) == NULL) break; /* add to history? */ s = line; while (isspace(*s)) s++; if (*s) add_history(line); s = n_str_strip_ws(line); if (*s) { shInCmd = 1; DBGF("(%s)\n", s); MEMINF("BEFORE %s\n", s); poclidek_execline(cctx, NULL, s); MEMINF("AFTER %s\n", s); sigint_reset(); shDone = 0; shInCmd = 0; } free(line); signal(SIGTERM, shell_end); signal(SIGQUIT, shell_end); if (shQuit) shDone = 1; } if (histfile) write_history(histfile); sigint_pop(); msg(0, "\n"); return 1; }