//------- // ^FUNCTION: CmdLine::print_descriptions // // ^SYNOPSIS: // unsigned CmdLine::print_descriptions(syntax, os, cols, longest) // // ^PARAMETERS: // CmdLine::CmdLineSyntax syntax; // -- the syntax to use (long-option, short-option, or both) // when printing the synopsis. // // ostream & os; // -- where to print. // // int cols; // -- the maximum width of a line. // // unsigned longest; // -- value returned by print_synopsis. // // ^DESCRIPTION: // Print a command argument descriptions (using the command-line syntax). // The descriptions should be printed to "os" using the desired syntax, // in lines that are no more than "cols" characters wide. // // ^REQUIREMENTS: // "longest" should correspond to a value returned by print_synopsis // that used the same "cmd" and syntax. // // ^SIDE-EFFECTS: // Prints on "os". // // ^RETURN-VALUE: // None. // // ^ALGORITHM: // Print the description for each argument. //-^^---- void CmdLine::print_descriptions(CmdLine::CmdLineSyntax syntax, ostream & os, int cols, unsigned longest) const { int positionals, options = 0; char buf[256]; for (positionals = 0 ; positionals < 2 ; positionals++) { CmdArgListListIter list_iter(cmd_args); for (CmdArgList * alist = list_iter() ; alist ; alist = list_iter()) { CmdArgListIter iter(alist); for (CmdArg * cmdarg = iter() ; cmdarg ; cmdarg = iter()) { // don't display hidden arguments if (cmdarg->syntax() & CmdArg::isHIDDEN) continue; if (!positionals && (cmdarg->syntax() & CmdArg::isPOS)) continue; if (positionals && !(cmdarg->syntax() & CmdArg::isPOS)) continue; #ifdef vms_style if ( !options++ ) os << "Qualifiers/Parameters:\n" ; #else if ( !options++ ) os << "Options/Arguments:\n" ; #endif if (! fmt_arg(cmdarg, buf, sizeof(buf), syntax, TERSE_USAGE)) { continue; } const char * description = cmdarg->description(); if ((description == NULL) || (! *description)) continue; strindent(os, cols, 8, buf, (longest + 2), description); } //for each cmdarg } //for each arg-list } //for each parm-type }
static void test_list(void) { List *list = make_list(); assert_int(0, list_len(list)); list_push(list, (void *)1); assert_int(1, list_len(list)); list_push(list, (void *)2); assert_int(2, list_len(list)); Iter *iter = list_iter(list); assert_int(1, (long)iter_next(iter)); assert_int(false, iter_end(iter)); assert_int(2, (long)iter_next(iter)); assert_int(true, iter_end(iter)); assert_int(0, (long)iter_next(iter)); assert_int(true, iter_end(iter)); List *copy = list_copy(list); assert_int(2, list_len(copy)); assert_int(1, (long)list_get(copy, 0)); assert_int(2, (long)list_get(copy, 1)); List *rev = list_reverse(list); iter = list_iter(rev); assert_int(2, (long)iter_next(iter)); assert_int(1, (long)iter_next(iter)); assert_int(0, (long)iter_next(iter)); assert_int(2, list_len(rev)); assert_int(1, (long)list_pop(rev)); assert_int(1, list_len(rev)); assert_int(2, (long)list_pop(rev)); assert_int(0, list_len(rev)); assert_int(0, (long)list_pop(rev)); List *list2 = make_list(); list_push(list2, (void *)5); list_push(list2, (void *)6); assert_int(5, (long)list_shift(list2)); assert_int(6, (long)list_shift(list2)); assert_int(0, (long)list_shift(list2)); List *list3 = make_list(); assert_int(0, (long)list_head(list3)); assert_int(0, (long)list_tail(list3)); list_push(list3, (void *)1); assert_int(1, (long)list_head(list3)); assert_int(1, (long)list_tail(list3)); list_push(list3, (void *)2); assert_int(1, (long)list_head(list3)); assert_int(2, (long)list_tail(list3)); List *list4 = make_list(); list_push(list4, (void *)1); list_push(list4, (void *)2); assert_int(1, (long)list_get(list4, 0)); assert_int(2, (long)list_get(list4, 1)); assert_int(0, (long)list_get(list4, 2)); }
caddr_t ctf_gen(iiburst_t *iiburst, size_t *resszp, int do_compress) { ctf_buf_t *buf = ctf_buf_new(); ctf_header_t h; caddr_t outbuf; int i; /* * Prepare the header, and create the CTF output buffers. The data * object section and function section are both lists of 2-byte * integers; we pad these out to the next 4-byte boundary if needed. */ h.cth_magic = CTF_MAGIC; h.cth_version = CTF_VERSION; h.cth_flags = do_compress ? CTF_F_COMPRESS : 0; h.cth_parlabel = strtab_insert(&buf->ctb_strtab, iiburst->iib_td->td_parlabel); h.cth_parname = strtab_insert(&buf->ctb_strtab, iiburst->iib_td->td_parname); h.cth_lbloff = 0; (void) list_iter(iiburst->iib_td->td_labels, write_label, buf); pad_buffer(buf, 2); h.cth_objtoff = ctf_buf_cur(buf); for (i = 0; i < iiburst->iib_nobjts; i++) write_objects(iiburst->iib_objts[i], buf); pad_buffer(buf, 2); h.cth_funcoff = ctf_buf_cur(buf); for (i = 0; i < iiburst->iib_nfuncs; i++) write_functions(iiburst->iib_funcs[i], buf); pad_buffer(buf, 4); h.cth_typeoff = ctf_buf_cur(buf); (void) list_iter(iiburst->iib_types, write_type, buf); debug(2, "CTF wrote %d types\n", list_count(iiburst->iib_types)); h.cth_stroff = ctf_buf_cur(buf); h.cth_strlen = strtab_size(&buf->ctb_strtab); /* * We only do compression for ctfmerge, as ctfconvert is only * supposed to be used on intermediary build objects. This is * significantly faster. */ if (do_compress) outbuf = write_compressed_buffer(&h, buf, resszp); else outbuf = write_buffer(&h, buf, resszp); ctf_buf_free(buf); return (outbuf); }
void cpp_eval(char *buf) { FILE *fp = fmemopen(buf, strlen(buf), "r"); set_input_file("(eval)", NULL, fp); List *toplevels = read_toplevels(); for (Iter *i = list_iter(toplevels); !iter_end(i);) emit_toplevel(iter_next(i)); }
void case_list_iter() { struct list *list = list(); char *s1 = "s1", *s2 = "s2", *s3 = "s3"; list_push(list, s1); list_push(list, s2); list_push(list, s3); struct list_iter *iter = list_iter(list); assert(iter->list == list); assert(iter->node == list->head); assert(list_iter_next(iter) == s1); assert(list_iter_next(iter) == s2); assert(list_iter_next(iter) == s3); list_iter_seek_tail(iter); assert(list_iter_prev(iter) == s3); assert(list_iter_prev(iter) == s2); assert(list_iter_prev(iter) == s1); list_iter_free(iter); int i = 0; struct list_node *node; list_each(list, node) { char *s = node->data; assert(s[1] - 49 == i); i += 1; }
static void emit_data_int(List *inits, int size, int off, int depth) { SAVE; Iter *iter = list_iter(inits); while (!iter_end(iter) && 0 < size) { Node *node = iter_next(iter); Node *v = node->initval; emit_padding(node, off); if (node->totype->bitsize > 0) { assert(node->totype->bitoff == 0); long data = eval_intexpr(v); Ctype *totype = node->totype; while (!iter_end(iter)) { node = iter_next(iter); if (node->totype->bitsize <= 0) { break; } v = node->initval; totype = node->totype; data |= ((((long)1 << totype->bitsize) - 1) & eval_intexpr(v)) << totype->bitoff; } emit_data_primtype(totype, &(Node){ AST_LITERAL, totype, .ival = data }); off += totype->size; size -= totype->size; if (iter_end(iter)) break; } else {
static char *get_caller_list(void) { String *s = make_string(); for (Iter *i = list_iter(functions); !iter_end(i);) { string_appendf(s, "%s", iter_next(i)); if (!iter_end(i)) string_appendf(s, " -> "); } return get_cstring(s); }
static void set_reg_nums(List *args) { numgp = numfp = 0; for (Iter *i = list_iter(args); !iter_end(i);) { Node *arg = iter_next(i); if (is_flotype(arg->ctype)) numfp++; else numgp++; } }
symbol_t* symbol_get_in_scope(symbol_table_t* symbol_table, char* name) { if (!symbol_table) return NULL; list_item_t* iter = list_iter_init(symbol_table->symbols); for (; iter; iter = list_iter(iter)) { symbol_t* candidate = iter->val; if (strcmp(candidate->name, name) == 0) { return candidate; } } return NULL; }
static void emit_args(List *vals) { SAVE; Iter *iter = list_iter(vals); while (!iter_end(iter)) { Node *v = iter_next(iter); emit_expr(v); if (is_flotype(v->ctype)) push_xmm(0); else push("rax"); } }
static void classify_args(List *ints, List *floats, List *rest, List *args) { SAVE; int ireg = 0, xreg = 0; int imax = 6, xmax = 8; Iter *iter = list_iter(args); while (!iter_end(iter)) { Node *v = iter_next(iter); if (is_flotype(v->ctype)) list_push((xreg++ < xmax) ? floats : rest, v); else list_push((ireg++ < imax) ? ints : rest, v); } }
static void emit_decl_init(List *inits, int off) { Iter *iter = list_iter(inits); while (!iter_end(iter)) { Node *node = iter_next(iter); assert(node->type == AST_INIT); if (node->initval->type == AST_LITERAL && node->totype->bitsize <= 0) { emit_save_literal(node->initval, node->totype, node->initoff + off); } else { emit_expr(node->initval); emit_lsave(node->totype, node->initoff + off); } } }
void pq_dump(pq_t * pq) { printf("pq_dump: %p\n", pq); printf("-----------------\n"); if (pq == NULL) { printf(" NULL\n"); } else { printf(" cmp: "); _pr_addr_(pq->cmp); printf(" sz_lst: %lu\n", pq->sz_lst); printf(" sz_obj: %lu\n", pq->sz_obj); printf(" list contents:\n"); list_iter(pq->list, &_pr_addr_); printf("\n"); } }
void case_list_iter() { struct list *list = list(); char *s1 = "s1", *s2 = "s2", *s3 = "s3"; list_push(list, s1); list_push(list, s2); list_push(list, s3); struct list_iter *iter = list_iter(list); assert(iter->list == list); assert(iter->node == list->head); assert(list_iter_next(iter) == s1); assert(list_iter_next(iter) == s2); assert(list_iter_next(iter) == s3); list_iter_seek_tail(iter); assert(list_iter_prev(iter) == s3); assert(list_iter_prev(iter) == s2); assert(list_iter_prev(iter) == s1); list_iter_free(iter); list_free(list); }
void dbg_alts(hash_table_t * ht, char a[][BUFSIZ]) { uint32_t i; for (i = 0; i < ht->size; i++) { list_dump(ht->row[i]); printf("\n"); list_iter(ht->row[i], &print_kv); } size_t maxlen = 0; for (i = 0; i < ht->size; i++) { size_t len = list_length(ht->row[i]); maxlen = len > maxlen ? len : maxlen; printf("%d -> %lu\n", i, len); } printf("maxlen = %lu\n", maxlen); for (i = 0; i < NUMA; i++) { printf("%d -> %s\n", i, a[i]); } }
int nm(const char * filename) { void *data; t_elf elf; t_sym_list *list; int msize; if ((msize = map_file(&data, filename)) == -1) return (1); if (init_elf_data(&elf, data) == -1) return (1); list = extract_symbols(&elf); list_sort(list, symcmp); list_iter(list, print_symbol); delete_list(list); if (elf.ehdr->e_ident[EI_CLASS] == ELFCLASS32) { free(elf.ehdr); free(elf.shtab); } (void)munmap(data, msize); return (0); }
void list_append(List *a, List *b) { for (Iter *i = list_iter(b); !iter_end(i);) list_push(a, iter_next(i)); }
//------- // ^FUNCTION: CmdLine::print_synopsis // // ^SYNOPSIS: // unsigned CmdLine::print_synopsis(syntax, os, cols) // // ^PARAMETERS: // CmdLine::CmdLineSyntax syntax; // -- the syntax to use (long-option, short-option, or both) // when printing the synopsis. // // ostream & os; // -- where to print. // // int cols; // -- the maximum width of a line. // // ^DESCRIPTION: // Print a command-line synopsis (the command-line syntax). // The synopsis should be printed to "os" using the desired syntax, // in lines that are no more than "cols" characters wide. // // ^REQUIREMENTS: // // ^SIDE-EFFECTS: // Prints on "os". // // ^RETURN-VALUE: // The length of the longest argument-buf that was printed. // // ^ALGORITHM: // It's kind of complicated so follow along! //-^^---- unsigned CmdLine::print_synopsis(CmdLine::CmdLineSyntax syntax, ostream & os, int cols) const { #ifdef vms_style static char usg_prefix[] = "Format: "; #else static char usg_prefix[] = "Usage: "; #endif unsigned ll, positionals, longest = 0; // first print the command name os << usg_prefix << cmd_name ; ll = (cmd_name ? ::strlen(cmd_name) : 0) + (sizeof(usg_prefix) - 1); // set margin so that we always start printing arguments in a column // that is *past* the command name. // unsigned margin = ll + 1; // print option-syntax followed by positional parameters int first; char buf[256] ; for (positionals = 0 ; positionals < 2 ; positionals++) { first = 1; CmdArgListListIter list_iter(cmd_args); for (CmdArgList * alist = list_iter() ; alist ; alist = list_iter()) { CmdArgListIter iter(alist); for (CmdArg * cmdarg = iter() ; cmdarg ; cmdarg = iter()) { unsigned len, pl; // don't display hidden arguments if (cmdarg->syntax() & CmdArg::isHIDDEN) continue; if (!positionals && (cmdarg->syntax() & CmdArg::isPOS)) continue; if (positionals && !(cmdarg->syntax() & CmdArg::isPOS)) continue; // figure out how wide this parameter is (for printing) pl = len = fmt_arg(cmdarg, buf, sizeof(buf), syntax, VERBOSE_USAGE); if (! len) continue; if (cmdarg->syntax() & CmdArg::isLIST) pl -= 4 ; // " ..." if (! (cmdarg->syntax() & CmdArg::isREQ)) pl -= 2 ; // "[]" if (pl > longest) longest = pl; // Will this fit? if ((ll + len + 1) > (cols - first)) { os << char('\n') ; os.width(margin); os << "" ; // No - start a new line; ll = margin; } else { os << char(' ') ; // Yes - just throw in a space ++ll; } ll += len; os << buf; first = 0; } //for each cmdarg } //for each arg-list } //for each parm-type os << endl ; return longest ; }
static void emit_compound_stmt(Node *node) { SAVE; for (Iter *i = list_iter(node->stmts); !iter_end(i);) emit_expr(iter_next(i)); }
List *list_reverse(List *list) { List *r = make_list(); for (Iter *i = list_iter(list); !iter_end(i);) list_unshift(r, iter_next(i)); return r; }
List *list_copy(List *list) { List *r = make_list(); for (Iter *i = list_iter(list); !iter_end(i);) list_push(r, iter_next(i)); return r; }