makeproto.c
来自「把pascal程序转成C语言程序 把pascal程序转成C语言程序」· C语言 代码 · 共 512 行 · 第 1/2 页
C
512 行
dprintf("numstars is %d\n", numstars); /***/ dprintf("ftype is %s\n", ftype); /***/ dprintf("cp after ftype is %s\n", cp); /***/ if (strbeginsword(ftype, "static") || strbeginsword(ftype, "Static")) { if (staticness == 0) bounce("Function is static"); } else { if (staticness == 1) bounce("Function is not static"); if (useextern && !strbeginsword(ftype, "extern") && !strbeginsword(ftype, "Extern")) { sprintf(temp, useextern == 2 ? "Extern %s" : "extern %s", ftype); strcpy(ftype, temp); } } while (isspace(*cp)) cp++; if (!*cp) { readline(buf, inf); cp = buf; } dprintf("cp before fname is %s\n", cp); /***/ if (!isidchar(*cp)) bounce("No function name"); cp2 = fname; while (isidchar(*cp)) *cp2++= *cp++; *cp2 = 0; dprintf("fname is %s\n", fname); /***/ dprintf("cp after fname is %s\n", cp); /***/ while (isspace(*cp)) cp++; if (*cp++ != '(') bounce("No function '('"); nargs = 0; if (!*cp) { readline(buf, inf); cp = buf; } while (isspace(*cp)) cp++; while (*cp != ')') { if (!isidchar(*cp)) bounce("Missing argument name"); if (nargs >= MAXARGS) bounce("Too many arguments"); cp2 = argnames[nargs]; argdecls[nargs][0] = 0; nargs++; while (isidchar(*cp)) *cp2++ = *cp++; *cp2 = 0; dprintf("Argument %d is named %s\n", nargs-1, argnames[nargs-1]); /***/ while (isspace(*cp)) cp++; if (*cp == ',') { cp++; if (!*cp) { readline(buf, inf); cp = buf; } while (isspace(*cp)) cp++; } else if (*cp != ')') bounce("Missing function ')'"); } if (cp[1]) bounce("Characters after function ')'"); readline(buf, inf); cp = buf; for (;;) { while (isspace(*cp)) cp++; if (isidchar(*cp)) { cp2 = dtype; if (strbeginsword(cp, "register")) { cp += 8; while (isspace(*cp)) cp++; } while (isspace(*cp) || isidchar(*cp)) *cp2++ = *cp++; if (*cp == ',' || *cp == ';' || *cp == '[') { while (cp2 > dtype && isspace(cp2[-1])) cp--, cp2--; while (cp2 > dtype && isidchar(cp2[-1])) cp--, cp2--; } else if (*cp != '(' && *cp != '*') bounce("Strange character in arg decl"); while (cp2 > dtype && isspace(cp2[-1])) cp2--; *cp2 = 0; if (!*dtype) bounce("Empty argument type"); for (;;) { cp2 = decl; cp3 = dname; while (*cp == '*' || *cp == '(' || isspace(*cp)) *cp2++ = *cp++; if (!isidchar(*cp)) bounce("Missing arg decl name"); while (isidchar(*cp)) { if (usenames) *cp2++ = *cp; *cp3++ = *cp++; } if (!usenames) { while (cp2 > decl && isspace(cp2[-1])) cp2--; while (isspace(*cp)) cp++; } i = 0; while (*cp && *cp != ';' && (*cp != ',' || i > 0)) { if (*cp == '(' || *cp == '[') i++; if (*cp == ')' || *cp == ']') i--; *cp2++ = *cp++; } *cp2 = 0; *cp3 = 0; dprintf("Argument %s is %s\n", dname, decl); /***/ if (i > 0) bounce("Unbalanced parens in arg decl"); if (!*cp) bounce("Missing ';' or ',' in arg decl"); for (i = 0; i < nargs && strcmp(argnames[i], dname); i++) ; if (i >= nargs) bounce("Arg decl name not in argument list"); if (*decl) sprintf(argdecls[i], "%s %s", dtype, decl); else strcpy(argdecls[i], dtype); if (*cp == ',') { cp++; if (!*cp) { readline(buf, inf); cp = buf; } while (isspace(*cp)) cp++; } else break; } cp++; if (!*cp) { readline(buf, inf); cp = buf; } } else break; } if (*cp != '{') bounce("Missing function '{'"); if (firstdecl) { firstdecl = 0; if (argc > 2) fprintf(outf, "\n/* Declarations from %s */\n", argv[whichf]); if (useifdefs && inf != stdin) { strcpy(ifdefname, argv[whichf]); cp = ifdefname; for (cp2 = ifdefname; *cp2; ) { if (*cp2++ == '/') cp = cp2; } for (cp2 = ifdefname; *cp; cp++, cp2++) { if (islower(*cp)) *cp2 = toupper(*cp); else if (isalnum(*cp)) *cp2 = *cp; else *cp2 = '_'; } fprintf(outf, "#ifdef PROTO_%s\n", ifdefname); } } for (i = 0; i < nargs; i++) { if (!argdecls[i][0]) sprintf(argdecls[i], "int %s", argnames[i]); for (j = 0; warntypes[j].bad && !strbeginsword(argdecls[i], warntypes[j].bad); j++) ; if (warntypes[j].bad) { cp = argdecls[i]; while (isspace(*cp) || isidchar(*cp)) cp++; if (!*cp) { /* not, e.g., "char *" */ sprintf(temp, "%s%s", warntypes[j].good, argdecls[i] + strlen(warntypes[j].bad)); strcpy(argdecls[i], temp); fprintf(stderr, "Warning: Argument %s of %s has type %s\n", argnames[i], fname, warntypes[j].bad); } } } if (verbose && outf != stdout) printf("Found declaration for %s\n", fname); fprintf(outf, "%s", ftype); pos = strlen(ftype) + numstars; do { putc(' ', outf); pos++; } while (pos < typetab); for (i = 1; i <= numstars; i++) putc('*', outf); fprintf(outf, "%s", fname); pos += strlen(fname); do { putc(' ', outf); pos++; } while (pos < argtab); if (nargs == 0) { if (usemacros) fprintf(outf, "PV();"); else fprintf(outf, "(void);"); } else { if (usemacros) fprintf(outf, "PP( ("), pos += 5; else fprintf(outf, "("), pos++; thistab = pos; for (i = 0; i < nargs; i++) { len = strlen(argdecls[i]); if (i > 0) { putc(',', outf); pos++; if (pos > thistab && pos + len >= width) { putc('\n', outf); for (j = 1; j <= thistab; j++) putc(' ', outf); pos = thistab; } else { putc(' ', outf); pos++; } } fprintf(outf, "%s", argdecls[i]); pos += len; } if (usemacros) fprintf(outf, ") );"); else fprintf(outf, ");"); } putc('\n', outf);Lbounce: ; } if (inf != stdin) { if (useifdefs && !firstdecl) fprintf(outf, "#endif /*PROTO_%s*/\n", ifdefname); fclose(inf); } } if (hasheader) { fprintf(outf, "\n\n/* End. */\n\n"); } if (outf != stdout) fclose(outf); if (errors) exit(1); else exit(0);}/* End. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?