static void emit_union(definition * def) { declaration *dflt; case_list *cl; declaration *cs; char *object; char *format = "&objp->%s_u.%s"; char *briefformat = "&objp->u.%s"; print_stat(&def->def.un.enum_decl); f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { cs = &cl->case_decl; f_print(fout, "\tcase %s:\n", cl->case_name); if (!streq(cs->type, "void")) { object = alloc(strlen(def->def_name) + strlen(format) + strlen(cs->name) + 1); if (brief_flag) s_print(object, briefformat, cs->name); else s_print(object, format, def->def_name, cs->name); print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max, object, cs->name); free(object); } f_print(fout, "\t\tbreak;\n"); } dflt = def->def.un.default_decl; if (dflt != NULL) { if (!streq(dflt->type, "void")) { f_print(fout, "\tdefault:\n"); object = alloc(strlen(def->def_name) + strlen(format) + strlen(dflt->name) + 1); s_print(object, format, def->def_name, dflt->name); print_ifstat(2, dflt->prefix, dflt->type, dflt->rel, dflt->array_max, object, dflt->name); free(object); f_print(fout, "\t\tbreak;\n"); } } else { f_print(fout, "\tdefault:\n"); f_print(fout, "\t\treturn (FALSE);\n"); } f_print(fout, "\t}\n"); }
/* * error, token encountered was not one of 3 expected ones */ void expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3) { s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1), toktostr(exp2), toktostr(exp3)); error(expectbuf); }
/* * error, token encountered was not the expected one */ void expected1 (tok_kind exp1) { s_print (expectbuf, "expected '%s'", toktostr (exp1)); error (expectbuf); }
static void print_hout(declaration * dec) { char prefix[8]; if (hflag) { if (dec->prefix) s_print(prefix, "%s ", dec->prefix); else prefix[0] = 0; f_print(fout, "\ntypedef "); switch (dec->rel) { case REL_ARRAY: f_print(fout, "struct %s {\n", dec->name); f_print(fout, "\tu_int %s_len;\n", dec->name); f_print(fout, "\t%s%s *%s_val;\n", prefix, dec->type, dec->name); f_print(fout, "} %s", dec->name); break; default: break; } f_print(fout, ";\n"); f_print(fout, "bool_t xdr_%s(XDR *xdrs, %s *objp);\n", dec->name, dec->name); } }
static void print_stat(declaration * dec) { char *prefix = dec->prefix; char *type = dec->type; char *amax = dec->array_max; relation rel = dec->rel; char name[256]; if (isvectordef(type, rel)) { s_print(name, "objp->%s", dec->name); } else { s_print(name, "&objp->%s", dec->name); } print_ifstat(1, prefix, type, rel, amax, name, dec->name); }
static void ptypedef (definition * def) { char *name = def->def_name; char *old = def->def.ty.old_type; char prefix[8]; /* enough to contain "struct ", including NUL */ relation rel = def->def.ty.rel; if (!streq (name, old)) { if (streq (old, "string")) { old = "char"; rel = REL_POINTER; } else if (streq (old, "opaque")) { old = "char"; } else if (streq (old, "bool")) { old = "bool_t"; } if (undefined2 (old, name) && def->def.ty.old_prefix) { s_print (prefix, "%s ", def->def.ty.old_prefix); } else { prefix[0] = 0; } f_print (fout, "typedef "); switch (rel) { case REL_ARRAY: f_print (fout, "struct {\n"); #if 0 f_print (fout, "\tu_int %s_len;\n", name); f_print (fout, "\t%s%s *%s_val;\n", prefix, old, name); #else f_print (fout, "\tu_int len;\n"); if (compatflag) f_print (fout, "#ifndef %s_len\n#define %s_len len\n#endif\n", name, name); f_print (fout, "\t%s%s *val;\n", prefix, old); if (compatflag) f_print (fout, "#ifndef %s_val\n#define %s_val val\n#endif\n", name, name); #endif f_print (fout, "} %s", name); break; case REL_POINTER: f_print (fout, "%s%s *%s", prefix, old, name); break; case REL_VECTOR: f_print (fout, "%s%s %s[%s]", prefix, old, name, def->def.ty.array_max); break; case REL_ALIAS: f_print (fout, "%s%s %s", prefix, old, name); break; } f_print (fout, ";\n"); } }
static void ptypedef(definition * def) { char *name = def->def_name; char *old = def->def.ty.old_type; char prefix[8]; /* enough to contain "struct ", including NUL */ relation rel = def->def.ty.rel; if (!streq(name, old)) { if (streq(old, "string")) { old = "char"; rel = REL_POINTER; } else if (!brief_flag && streq(old, "opaque")) { old = "char"; } else if (streq(old, "bool")) { old = "bool_t"; } if (undefined2(old, name) && def->def.ty.old_prefix) { s_print(prefix, "%s ", def->def.ty.old_prefix); } else { prefix[0] = 0; } f_print(fout, "typedef "); switch (rel) { case REL_ARRAY: if (brief_flag) { if (streq(old, "opaque")) { f_print(fout, "struct rx_opaque %s", name); } else { f_print(fout, "struct {\n"); f_print(fout, "\tu_int len;\n"); f_print(fout, "\t%s%s *val;\n", prefix, old); f_print(fout, "} %s", name); } } else { f_print(fout, "struct %s {\n", name); f_print(fout, "\tu_int %s_len;\n", name); f_print(fout, "\t%s%s *%s_val;\n", prefix, old, name); f_print(fout, "} %s", name); } break; case REL_POINTER: f_print(fout, "%s%s *%s", prefix, old, name); break; case REL_VECTOR: f_print(fout, "%s%s %s[%s]", prefix, old, name, def->def.ty.array_max); break; case REL_ALIAS: f_print(fout, "%s%s %s", prefix, old, name); break; } def->pc.rel = rel; STOREVAL(&typedef_defined, def); f_print(fout, ";\n"); } }
void pdeclaration(const char *name, declaration *dec, int tab, const char *separator) { char buf[8]; /* enough to hold "struct ", include NUL */ const char *prefix; const char *type; if (streq(dec->type, "void")) { return; } tabify(fout, tab); if (streq(dec->type, name) && !dec->prefix) { f_print(fout, "struct "); } if (streq(dec->type, "string")) { f_print(fout, "char *%s", dec->name); } else { prefix = ""; if (streq(dec->type, "bool")) { type = "bool_t"; } else if (streq(dec->type, "opaque")) { type = "char"; } else { if (dec->prefix) { s_print(buf, "%s ", dec->prefix); prefix = buf; } type = dec->type; } switch (dec->rel) { case REL_ALIAS: f_print(fout, "%s%s %s", prefix, type, dec->name); break; case REL_VECTOR: f_print(fout, "%s%s %s[%s]", prefix, type, dec->name, dec->array_max); break; case REL_POINTER: f_print(fout, "%s%s *%s", prefix, type, dec->name); break; case REL_ARRAY: f_print(fout, "struct {\n"); tabify(fout, tab); f_print(fout, "\tu_int %s_len;\n", dec->name); tabify(fout, tab); f_print(fout, "\t%s%s *%s_val;\n", prefix, type, dec->name); tabify(fout, tab); f_print(fout, "} %s", dec->name); break; } } f_print(fout, "%s", separator); }
int main(int argc, const char * argv[]) { // 头插法 SLIST singleLinkList0 = s_init(); for (int i = 0; i < 5; i++) { s_add(singleLinkList0, 0, i); } s_print(singleLinkList0); // 尾插法 SLIST singleLinkList = s_init(); SNODE* endNode = singleLinkList; for (int i = 0; i < 5; i++) { SNODE* node = (SNODE*)malloc(sizeof(SNODE)); node->data = i; endNode->next = node; endNode = node; singleLinkList->data++; } s_print(singleLinkList); s_add(singleLinkList, 3, 5); s_print(singleLinkList); printf("%d\n", s_get(singleLinkList, 3)); s_print(singleLinkList); s_delete(singleLinkList, 1); s_print(singleLinkList); s_clear(singleLinkList); s_print(singleLinkList); return 0; }
static void write_table (definition * def) { version_list *vp; proc_list *proc; int current; int expected; char progvers[100]; int warning; for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { warning = 0; s_print (progvers, "%s_%s", locase (def->def_name), vp->vers_num); /* print the table header */ f_print (fout, tbl_hdr, progvers); if (nullproc (vp->procs)) { expected = 0; } else { expected = 1; f_print (fout, "%s", null_entry); } for (proc = vp->procs; proc != NULL; proc = proc->next) { current = atoi (proc->proc_num); if (current != expected++) { f_print (fout, "\n/*\n * WARNING: table out of order\n */\n"); if (warning == 0) { f_print (stderr, "WARNING %s table is out of order\n", progvers); warning = 1; nonfatalerrors = 1; } expected = current + 1; } f_print (fout, " {\n\t(char *(*)())RPCGEN_ACTION("); /* routine to invoke */ if (!newstyle) pvname_svc (proc->proc_name, vp->vers_num); else { if (newstyle) f_print (fout, "_"); /* calls internal func */ pvname (proc->proc_name, vp->vers_num); } f_print (fout, "),\n"); /* argument info */ if (proc->arg_num > 1) printit ((char *) NULL, proc->args.argname); else /* do we have to do something special for newstyle */ printit (proc->args.decls->decl.prefix, proc->args.decls->decl.type); /* result info */ printit (proc->res_prefix, proc->res_type); f_print (fout, " },\n"); } /* print the table trailer */ f_print (fout, "%s", tbl_end); f_print (fout, tbl_nproc, progvers, progvers, progvers); f_print (fout, "const struct rpc_program_tc %s = {\n" "\t%s, %s, %s_table,\n" "\tsizeof (%s_table) / sizeof (%s_table[0])\n" "};\n\n", progvers, def->def_name, vp->vers_num, progvers, progvers, progvers); } }
/* * regprint - print a regexp for debugging */ void regprint(regex_t *r, FILE *d) { #ifdef __NetBSD__ struct re_guts *g = r->re_g; int c; int last; int nincat[NC]; fprintf(d, "%ld states, %zu categories", (long)g->nstates, g->ncategories); fprintf(d, ", first %ld last %ld", (long)g->firststate, (long)g->laststate); if (g->iflags&USEBOL) fprintf(d, ", USEBOL"); if (g->iflags&USEEOL) fprintf(d, ", USEEOL"); if (g->iflags&BAD) fprintf(d, ", BAD"); if (g->nsub > 0) fprintf(d, ", nsub=%ld", (long)g->nsub); if (g->must != NULL) fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen, g->must); if (g->backrefs) fprintf(d, ", backrefs"); if (g->nplus > 0) fprintf(d, ", nplus %ld", (long)g->nplus); fprintf(d, "\n"); s_print(g, d); for (size_t i = 0; i < g->ncategories; i++) { nincat[i] = 0; for (c = CHAR_MIN; c <= CHAR_MAX; c++) if (g->categories[c] == i) nincat[i]++; } fprintf(d, "cc0#%d", nincat[0]); for (size_t i = 1; i < g->ncategories; i++) if (nincat[i] == 1) { for (c = CHAR_MIN; c <= CHAR_MAX; c++) if (g->categories[c] == i) break; fprintf(d, ", %zu=%s", i, regchar(c)); } fprintf(d, "\n"); for (size_t i = 1; i < g->ncategories; i++) if (nincat[i] != 1) { fprintf(d, "cc%zu\t", i); last = -1; for (c = CHAR_MIN; c <= CHAR_MAX+1; c++) /* +1 does flush */ if (c <= CHAR_MAX && g->categories[c] == i) { if (last < 0) { fprintf(d, "%s", regchar(c)); last = c; } } else { if (last >= 0) { if (last != c-1) fprintf(d, "-%s", regchar(c-1)); last = -1; } } fprintf(d, "\n"); } #endif }
/* * Get the next token, printing out any directive that are encountered. */ void get_token (token *tokp) { int commenting; if (pushed) { pushed = 0; *tokp = lasttok; return; } commenting = 0; for (;;) { if (*where == 0) { for (;;) { if (!fgets (curline, MAXLINESIZE, fin)) { tokp->kind = TOK_EOF; *curline = 0; where = curline; return; } linenum++; if (commenting) { break; } else if (cppline (curline)) { docppline (curline, &linenum, &infilename); } else if (directive (curline)) { printdirective (curline); } else { break; } } where = curline; } else if (isspace (*where)) { while (isspace (*where)) { where++; /* eat */ } } else if (commenting) { for (where++; *where; where++) { if (endcomment (where)) { where++; commenting--; break; } } } else if (startcomment (where)) { where += 2; commenting++; } else { break; } } /* * 'where' is not whitespace, comment or directive Must be a token! */ switch (*where) { case ':': tokp->kind = TOK_COLON; where++; break; case ';': tokp->kind = TOK_SEMICOLON; where++; break; case ',': tokp->kind = TOK_COMMA; where++; break; case '=': tokp->kind = TOK_EQUAL; where++; break; case '*': tokp->kind = TOK_STAR; where++; break; case '[': tokp->kind = TOK_LBRACKET; where++; break; case ']': tokp->kind = TOK_RBRACKET; where++; break; case '{': tokp->kind = TOK_LBRACE; where++; break; case '}': tokp->kind = TOK_RBRACE; where++; break; case '(': tokp->kind = TOK_LPAREN; where++; break; case ')': tokp->kind = TOK_RPAREN; where++; break; case '<': tokp->kind = TOK_LANGLE; where++; break; case '>': tokp->kind = TOK_RANGLE; where++; break; case '"': tokp->kind = TOK_STRCONST; findstrconst (&where, &tokp->str); break; case '\'': tokp->kind = TOK_CHARCONST; findchrconst (&where, &tokp->str); break; case '-': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': tokp->kind = TOK_IDENT; findconst (&where, &tokp->str); break; default: if (!(isalpha (*where) || *where == '_')) { char buf[100]; char *p; s_print (buf, ("illegal character in file: ")); p = buf + strlen (buf); if (isprint (*where)) { s_print (p, "%c", *where); } else { s_print (p, "%d", *where); } error (buf); } findkind (&where, tokp); break; } }
static void emit_struct (definition * def) { decl_list *dl; int i, j, size, flag; decl_list *cur = NULL, *psav; bas_type *ptr; char *sizestr, *plus; char ptemp[256]; int can_inline; if (doinline == 0) { for (dl = def->def.st.decls; dl != NULL; dl = dl->next) print_stat (1, &dl->decl); return; } for (dl = def->def.st.decls; dl != NULL; dl = dl->next) if (dl->decl.rel == REL_VECTOR) { f_print (fout, "\t int i;\n"); break; } size = 0; can_inline = 0; for (dl = def->def.st.decls; dl != NULL; dl = dl->next) if ((dl->decl.prefix == NULL) && ((ptr = find_type (dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) { if (dl->decl.rel == REL_ALIAS) size += ptr->length; else { can_inline = 1; break; /* can be inlined */ }; } else { if (size >= doinline) { can_inline = 1; break; /* can be inlined */ } size = 0; } if (size > doinline) can_inline = 1; if (can_inline == 0) { /* can not inline, drop back to old mode */ for (dl = def->def.st.decls; dl != NULL; dl = dl->next) print_stat (1, &dl->decl); return; }; flag = PUT; for (j = 0; j < 2; j++) { if (flag == PUT) f_print (fout, "\n\t if (xdrs->x_op == XDR_ENCODE) {\n"); else f_print (fout, "\n \t return (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n"); i = 0; size = 0; sizestr = NULL; for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */ /* now walk down the list and check for basic types */ if ((dl->decl.prefix == NULL) && ((ptr = find_type (dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))) { if (i == 0) cur = dl; i++; if (dl->decl.rel == REL_ALIAS) size += ptr->length; else { /* this is required to handle arrays */ if (sizestr == NULL) plus = " "; else plus = "+"; if (ptr->length != 1) s_print (ptemp, " %s %s * %d", plus, dl->decl.array_max, ptr->length); else s_print (ptemp, " %s %s ", plus, dl->decl.array_max); /* now concatenate to sizestr !!!! */ if (sizestr == NULL) sizestr = strdup (ptemp); else { sizestr = (char *) realloc (sizestr, strlen (sizestr) + strlen (ptemp) + 1); if (sizestr == NULL) { f_print (stderr, "Fatal error : no memory \n"); crash (); }; sizestr = strcat (sizestr, ptemp); /* build up length of * array */ } } } else { if (i > 0) { if (sizestr == NULL && size < doinline) { /* don't expand into inline * code if size < doinline */ while (cur != dl) { print_stat (1, &cur->decl); cur = cur->next; } } else { /* were already looking at a * xdr_inlineable structure */ if (sizestr == NULL) f_print (fout, "\t buf = (int32_t *)XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);", size); else if (size == 0) f_print (fout, "\t buf = (int32_t *)XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);", sizestr); else f_print (fout, "\t buf = (int32_t *)XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);", size, sizestr); f_print (fout, "\n\t if (buf == NULL) {\n"); psav = cur; while (cur != dl) { print_stat (2, &cur->decl); cur = cur->next; } f_print (fout, "\n\t }\n\t else {\n"); cur = psav; while (cur != dl) { emit_inline (&cur->decl, flag); cur = cur->next; } f_print (fout, "\t }\n"); } } size = 0; i = 0; sizestr = NULL; print_stat (1, &dl->decl); } } if (i > 0) { if (sizestr == NULL && size < doinline) { /* don't expand into inline code if size < * doinline */ while (cur != dl) { print_stat (1, &cur->decl); cur = cur->next; } } else { /* were already looking at a xdr_inlineable * structure */ if (sizestr == NULL) f_print (fout, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs,%d * BYTES_PER_XDR_UNIT);", size); else if (size == 0) f_print (fout, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs,%s * BYTES_PER_XDR_UNIT);", sizestr); else f_print (fout, "\t\tbuf = (int32_t *)XDR_INLINE(xdrs,(%d + %s)* BYTES_PER_XDR_UNIT);", size, sizestr); f_print (fout, "\n\t\tif (buf == NULL) {\n"); psav = cur; while (cur != NULL) { print_stat (2, &cur->decl); cur = cur->next; } f_print (fout, "\n\t }\n\t else {\n"); cur = psav; while (cur != dl) { emit_inline (&cur->decl, flag); cur = cur->next; } f_print (fout, "\t }\n"); } } flag = GET; } f_print (fout, "\t return(TRUE);\n\t}\n\n"); /* now take care of XDR_FREE case */ for (dl = def->def.st.decls; dl != NULL; dl = dl->next) print_stat (1, &dl->decl); }
static void emit_union (definition * def) { declaration *dflt; case_list *cl; declaration *cs; char *object; #if 1 char *vecformat = "objp->RPC_UNION_NAME(%s).%s"; char *format = "&objp->RPC_UNION_NAME(%s).%s"; #else char *vecformat = "objp->u.%s"; char *format = "&objp->u.%s"; #endif print_stat (1, &def->def.un.enum_decl); f_print (fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { f_print (fout, "\tcase %s:\n", cl->case_name); if (cl->contflag == 1) /* a continued case statement */ continue; cs = &cl->case_decl; if (!streq (cs->type, "void")) { object = alloc (strlen (def->def_name) + strlen (format) + strlen (cs->name) + 1); #if 1 if (isvectordef (cs->type, cs->rel)) { s_print (object, vecformat, def->def_name, cs->name); } else { s_print (object, format, def->def_name, cs->name); } #else if (isvectordef (cs->type, cs->rel)) s_print (object, vecformat, cs->name); else s_print (object, format, cs->name); #endif print_ifstat (2, cs->prefix, cs->type, cs->rel, cs->array_max, object, cs->name); free (object); } f_print (fout, "\t\tbreak;\n"); } dflt = def->def.un.default_decl; if (dflt != NULL) { if (!streq (dflt->type, "void")) { f_print (fout, "\tdefault:\n"); object = alloc (strlen (def->def_name) + strlen (format) + strlen (dflt->name) + 1); #if 1 if (isvectordef (dflt->type, dflt->rel)) { s_print (object, vecformat, def->def_name, dflt->name); } else { s_print (object, format, def->def_name, dflt->name); } #else if (isvectordef (dflt->type, dflt->rel)) s_print (object, vecformat, dflt->name); else s_print (object, format, dflt->name); #endif print_ifstat (2, dflt->prefix, dflt->type, dflt->rel, dflt->array_max, object, dflt->name); free (object); f_print (fout, "\t\tbreak;\n"); } } else { f_print (fout, "\tdefault:\n"); f_print (fout, "\t\treturn (FALSE);\n"); } f_print (fout, "\t}\n"); }