home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / main.c < prev    next >
C/C++ Source or Header  |  1994-03-05  |  18KB  |  642 lines

  1. /****************************************************************
  2. Copyright 1990 - 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. extern char F2C_version[];
  25.  
  26. #include "defs.h"
  27. #include "parse.h"
  28.  
  29. int complex_seen, dcomplex_seen;
  30.  
  31. LOCAL int Max_ftn_files;
  32.  
  33. char **ftn_files;
  34. int current_ftn_file = 0;
  35.  
  36. flag ftn66flag = NO;
  37. flag nowarnflag = NO;
  38. flag noextflag = NO;
  39. flag  no66flag = NO;        /* Must also set noextflag to this
  40.                        same value */
  41. flag zflag = YES;        /* recognize double complex intrinsics */
  42. flag debugflag = NO;
  43. flag onetripflag = NO;
  44. flag shiftcase = YES;
  45. flag undeftype = NO;
  46. flag checksubs = NO;
  47. flag r8flag = NO;
  48. flag use_bs = YES;
  49. flag keepsubs = NO;
  50. #ifdef TYQUAD
  51. flag use_tyquad = YES;
  52. #endif
  53. int tyreal = TYREAL;
  54. int tycomplex = TYCOMPLEX;
  55.  
  56. int maxregvar = MAXREGVAR;    /* if maxregvar > MAXREGVAR, error */
  57. int maxequiv = MAXEQUIV;
  58. int maxext = MAXEXT;
  59. int maxstno = MAXSTNO;
  60. int maxctl = MAXCTL;
  61. int maxhash = MAXHASH;
  62. int maxliterals = MAXLITERALS;
  63. int maxcontin = MAXCONTIN;
  64. int maxlablist = MAXLABLIST;
  65. int extcomm, ext1comm, useauto;
  66. int can_include = YES;    /* so we can disable includes for netlib */
  67.  
  68. static char *def_i2 = "";
  69.  
  70. static int useshortints = NO;    /* YES => tyint = TYSHORT */
  71. static int uselongints = NO;    /* YES => tyint = TYLONG */
  72. int addftnsrc = NO;        /* Include ftn source in output */
  73. int usedefsforcommon = NO;    /* Use #defines for common reference */
  74. int forcedouble = YES;        /* force real functions to double */
  75. int Ansi = NO;
  76. int def_equivs = YES;
  77. int tyioint = TYLONG;
  78. int szleng = SZLENG;
  79. int inqmask = M(TYLONG)|M(TYLOGICAL);
  80. int wordalign = NO;
  81. int forcereal = NO;
  82. int warn72 = NO;
  83. static int skipC, skipversion;
  84. char *file_name, *filename0, *parens;
  85. int Castargs = 1;
  86. static int Castargs1;
  87. static int typedefs = 0;
  88. int chars_per_wd, gflag, protostatus;
  89. int infertypes = 1;
  90. char used_rets[TYSUBR+1];
  91. extern char *tmpdir;
  92. static int h0align = 0;
  93. char *halign, *ohalign;
  94. int krparens = NO;
  95. int hsize;    /* for padding under -h */
  96. int htype;    /* for wr_equiv_init under -h */
  97.  
  98. #define f2c_entry(swit,count,type,store,size) \
  99.     p_entry ("-", swit, 0, count, type, store, size)
  100.  
  101. static arg_info table[] = {
  102.     f2c_entry ("w66", P_NO_ARGS, P_INT, &ftn66flag, YES),
  103.     f2c_entry ("w", P_NO_ARGS, P_INT, &nowarnflag, YES),
  104.     f2c_entry ("66", P_NO_ARGS, P_INT, &no66flag, YES),
  105.     f2c_entry ("1", P_NO_ARGS, P_INT, &onetripflag, YES),
  106.     f2c_entry ("onetrip", P_NO_ARGS, P_INT, &onetripflag, YES),
  107.     f2c_entry ("I2", P_NO_ARGS, P_INT, &useshortints, YES),
  108.     f2c_entry ("I4", P_NO_ARGS, P_INT, &uselongints, YES),
  109.     f2c_entry ("U", P_NO_ARGS, P_INT, &shiftcase, NO),
  110.     f2c_entry ("u", P_NO_ARGS, P_INT, &undeftype, YES),
  111.     f2c_entry ("O", P_ONE_ARG, P_INT, &maxregvar, 0),
  112.     f2c_entry ("C", P_NO_ARGS, P_INT, &checksubs, YES),
  113.     f2c_entry ("Nq", P_ONE_ARG, P_INT, &maxequiv, 0),
  114.     f2c_entry ("Nx", P_ONE_ARG, P_INT, &maxext, 0),
  115.     f2c_entry ("Ns", P_ONE_ARG, P_INT, &maxstno, 0),
  116.     f2c_entry ("Nc", P_ONE_ARG, P_INT, &maxctl, 0),
  117.     f2c_entry ("Nn", P_ONE_ARG, P_INT, &maxhash, 0),
  118.     f2c_entry ("NL", P_ONE_ARG, P_INT, &maxliterals, 0),
  119.     f2c_entry ("NC", P_ONE_ARG, P_INT, &maxcontin, 0),
  120.     f2c_entry ("Nl", P_ONE_ARG, P_INT, &maxlablist, 0),
  121.     f2c_entry ("c", P_NO_ARGS, P_INT, &addftnsrc, YES),
  122.     f2c_entry ("p", P_NO_ARGS, P_INT, &usedefsforcommon, YES),
  123.     f2c_entry ("R", P_NO_ARGS, P_INT, &forcedouble, NO),
  124.     f2c_entry ("!R", P_NO_ARGS, P_INT, &forcedouble, YES),
  125.     f2c_entry ("A", P_NO_ARGS, P_INT, &Ansi, YES),
  126.     f2c_entry ("ext", P_NO_ARGS, P_INT, &noextflag, YES),
  127.     f2c_entry ("z", P_NO_ARGS, P_INT, &zflag, NO),
  128.     f2c_entry ("a", P_NO_ARGS, P_INT, &useauto, YES),
  129.     f2c_entry ("r8", P_NO_ARGS, P_INT, &r8flag, YES),
  130.     f2c_entry ("i2", P_NO_ARGS, P_INT, &tyioint, NO),
  131.     f2c_entry ("w8", P_NO_ARGS, P_INT, &wordalign, YES),
  132.     f2c_entry ("!I", P_NO_ARGS, P_INT, &can_include, NO),
  133.     f2c_entry ("W", P_ONE_ARG, P_INT, &chars_per_wd, 0),
  134.     f2c_entry ("g", P_NO_ARGS, P_INT, &gflag, YES),
  135.     f2c_entry ("T", P_ONE_ARG, P_STRING, &tmpdir, 0),
  136.     f2c_entry ("E", P_NO_ARGS, P_INT, &extcomm, 1),
  137.     f2c_entry ("e1c", P_NO_ARGS, P_INT, &ext1comm, 1),
  138.     f2c_entry ("ec", P_NO_ARGS, P_INT, &ext1comm, 2),
  139.     f2c_entry ("C++", P_NO_ARGS, P_INT, &Ansi, 2),
  140.     f2c_entry ("P", P_NO_ARGS, P_INT, &Castargs, 3),
  141.     f2c_entry ("Ps", P_NO_ARGS, P_INT, &protostatus, 1),
  142.     f2c_entry ("!P", P_NO_ARGS, P_INT, &Castargs, 0),
  143.     f2c_entry ("!c", P_NO_ARGS, P_INT, &skipC, 1),
  144.     f2c_entry ("!it", P_NO_ARGS, P_INT, &infertypes, 0),
  145.     f2c_entry ("h", P_NO_ARGS, P_INT, &h0align, 1),
  146.     f2c_entry ("hd", P_NO_ARGS, P_INT, &h0align, 2),
  147.     f2c_entry ("kr", P_NO_ARGS, P_INT, &krparens, 1),
  148.     f2c_entry ("krd", P_NO_ARGS, P_INT, &krparens, 2),
  149.     f2c_entry ("!bs", P_NO_ARGS, P_INT, &use_bs, NO),
  150.     f2c_entry ("r", P_NO_ARGS, P_INT, &forcereal, YES),
  151.     f2c_entry ("72", P_NO_ARGS, P_INT, &warn72, 1),
  152.     f2c_entry ("f", P_NO_ARGS, P_INT, &warn72, 2),
  153.     f2c_entry ("s", P_NO_ARGS, P_INT, &keepsubs, 1),
  154.     f2c_entry ("d", P_ONE_ARG, P_STRING, &outbuf, 0),
  155. #ifdef TYQUAD
  156.     f2c_entry ("!i8", P_NO_ARGS, P_INT, &use_tyquad, NO),
  157. #endif
  158.  
  159.     /* options omitted from man pages */
  160.  
  161.     /* -ev ==> implement equivalence with initialized pointers */
  162.     f2c_entry ("ev", P_NO_ARGS, P_INT, &def_equivs, NO),
  163.  
  164.     /* -!it used to be the default when -it was more agressive */
  165.  
  166.     f2c_entry ("it", P_NO_ARGS, P_INT, &infertypes, 1),
  167.  
  168.     /* -Pd is similar to -P, but omits :ref: lines */
  169.     f2c_entry ("Pd", P_NO_ARGS, P_INT, &Castargs, 2),
  170.  
  171.     /* -t ==> emit typedefs (under -A or -C++) for procedure
  172.         argument types used.  This is meant for netlib's
  173.         f2c service, so -A and -C++ will work with older
  174.         versions of f2c.h
  175.         */
  176.     f2c_entry ("t", P_NO_ARGS, P_INT, &typedefs, 1),
  177.  
  178.     /* -!V ==> omit version msg (to facilitate using diff in
  179.         regression testing)
  180.         */
  181.     f2c_entry ("!V", P_NO_ARGS, P_INT, &skipversion, 1),
  182.  
  183.     /* -Dnnn = debug level nnn */
  184.  
  185.     f2c_entry ("D", P_ONE_ARG, P_INT, &debugflag, YES)
  186. }; /* table */
  187.  
  188. extern char *c_functions;    /* "c_functions"    */
  189. extern char *coutput;        /* "c_output"        */
  190. extern char *initfname;        /* "raw_data"        */
  191. extern char *blkdfname;        /* "block_data"        */
  192. extern char *p1_file;        /* "p1_file"        */
  193. extern char *p1_bakfile;    /* "p1_file.BAK"    */
  194. extern char *sortfname;        /* "init_file"        */
  195. extern char *proto_fname;    /* "proto_file"        */
  196. FILE *protofile;
  197.  
  198.  void
  199. set_externs(Void)
  200. {
  201.     static char *hset[3] = { 0, "integer", "doublereal" };
  202.  
  203. /* Adjust the global flags according to the command line parameters */
  204.  
  205.     if (chars_per_wd > 0) {
  206.     typesize[TYADDR] = typesize[TYLONG] = typesize[TYREAL] =
  207.         typesize[TYLOGICAL] = chars_per_wd;
  208.     typesize[TYINT1] = typesize[TYLOGICAL1] = 1;
  209.     typesize[TYDREAL] = typesize[TYCOMPLEX] = chars_per_wd << 1;
  210.     typesize[TYDCOMPLEX] = chars_per_wd << 2;
  211.     typesize[TYSHORT] = typesize[TYLOGICAL2] = chars_per_wd >> 1;
  212.     typesize[TYCILIST] = 5*chars_per_wd;
  213.     typesize[TYICILIST] = 6*chars_per_wd;
  214.     typesize[TYOLIST] = 9*chars_per_wd;
  215.     typesize[TYCLLIST] = 3*chars_per_wd;
  216.     typesize[TYALIST] = 2*chars_per_wd;
  217.     typesize[TYINLIST] = 26*chars_per_wd;
  218.     }
  219.  
  220.     if (wordalign)
  221.     typealign[TYDREAL] = typealign[TYDCOMPLEX] = typealign[TYREAL];
  222.     if (!tyioint) {
  223.     tyioint = TYSHORT;
  224.     szleng = typesize[TYSHORT];
  225.     def_i2 = "#define f2c_i2 1\n";
  226.     inqmask = M(TYSHORT)|M(TYLOGICAL);
  227.     goto checklong;
  228.     }
  229.     else
  230.     szleng = typesize[TYLONG];
  231.     if (useshortints) {
  232.     inqmask = M(TYLONG);
  233.  checklong:
  234.     protorettypes[TYLOGICAL] = typename[TYLOGICAL] = "shortlogical";
  235.     typesize[TYLOGICAL] = typesize[TYSHORT];
  236.     casttypes[TYLOGICAL] = "K_fp";
  237.     if (uselongints)
  238.         err ("Can't use both long and short ints");
  239.     else {
  240.         tyint = tylogical = TYSHORT;
  241.         tylog = TYLOGICAL2;
  242.         }
  243.     }
  244.     else if (uselongints)
  245.     tyint = TYLONG;
  246.  
  247.     if (h0align) {
  248.     if (tyint == TYLONG && wordalign)
  249.         h0align = 1;
  250.         ohalign = halign = hset[h0align];
  251.     htype = h0align == 1 ? tyint : TYDREAL;
  252.     hsize = typesize[htype];
  253.     }
  254.  
  255.     if (no66flag)
  256.     noextflag = no66flag;
  257.     if (noextflag)
  258.     zflag = 0;
  259.  
  260.     if (r8flag) {
  261.     tyreal = TYDREAL;
  262.     tycomplex = TYDCOMPLEX;
  263.     r8fix();
  264.     }
  265.     if (forcedouble) {
  266.     protorettypes[TYREAL] = "E_f";
  267.     casttypes[TYREAL] = "E_fp";
  268.     }
  269.  
  270.     if (maxregvar > MAXREGVAR) {
  271.     warni("-O%d: too many register variables", maxregvar);
  272.     maxregvar = MAXREGVAR;
  273.     } /* if maxregvar > MAXREGVAR */
  274.  
  275. /* Check the list of input files */
  276.  
  277.     {
  278.     int bad, i, cur_max = Max_ftn_files;
  279.  
  280.     for (i = bad = 0; i < cur_max && ftn_files[i]; i++)
  281.         if (ftn_files[i][0] == '-') {
  282.         errstr ("Invalid flag '%s'", ftn_files[i]);
  283.         bad++;
  284.         }
  285.     if (bad)
  286.         exit(1);
  287.  
  288.     } /* block */
  289. } /* set_externs */
  290.  
  291.  
  292.  static int
  293. comm2dcl(Void)
  294. {
  295.     Extsym *ext;
  296.     if (ext1comm)
  297.         for(ext = extsymtab; ext < nextext; ext++)
  298.             if (ext->extstg == STGCOMMON && !ext->extinit)
  299.                 return ext1comm;
  300.     return 0;
  301.     }
  302.  
  303.  static void
  304. #ifdef KR_headers
  305. write_typedefs(outfile)
  306.     FILE *outfile;
  307. #else
  308. write_typedefs(FILE *outfile)
  309. #endif
  310. {
  311.     register int i;
  312.     register char *s, *p = 0;
  313.     static char st[4] = { TYREAL, TYCOMPLEX, TYDCOMPLEX, TYCHAR };
  314.     static char stl[4] = { 'E', 'C', 'Z', 'H' };
  315.  
  316.     for(i = 0; i <= TYSUBR; i++)
  317.         if (s = usedcasts[i]) {
  318.             if (!p) {
  319.                 p = Ansi == 1 ? "()" : "(...)";
  320.                 nice_printf(outfile,
  321.                 "/* Types for casting procedure arguments: */\
  322. \n\n#ifndef F2C_proc_par_types\n");
  323.                 if (i == 0) {
  324.                     nice_printf(outfile,
  325.             "typedef int /* Unknown procedure type */ (*%s)%s;\n",
  326.                          s, p);
  327.                     continue;
  328.                     }
  329.                 }
  330.             nice_printf(outfile, "typedef %s (*%s)%s;\n",
  331.                     c_type_decl(i,1), s, p);
  332.             }
  333.     for(i = !forcedouble; i < 4; i++)
  334.         if (used_rets[st[i]])
  335.             nice_printf(outfile,
  336.                 "typedef %s %c_f; /* %s function */\n",
  337.                 p = i ? "VOID" : "doublereal",
  338.                 stl[i], ftn_types[st[i]]);
  339.     if (p)
  340.         nice_printf(outfile, "#endif\n\n");
  341.     }
  342.  
  343.  static void
  344. #ifdef KR_headers
  345. commonprotos(outfile)
  346.     register FILE *outfile;
  347. #else
  348. commonprotos(register FILE *outfile)
  349. #endif
  350. {
  351.     register Extsym *e, *ee;
  352.     register Argtypes *at;
  353.     Atype *a, *ae;
  354.     int k;
  355.     extern int proc_protochanges;
  356.  
  357.     if (!outfile)
  358.         return;
  359.     for (e = extsymtab, ee = nextext; e < ee; e++)
  360.         if (e->extstg == STGCOMMON && e->allextp)
  361.             nice_printf(outfile, "/* comlen %s %ld */\n",
  362.                 e->cextname, e->maxleng);
  363.     if (Castargs1 < 3)
  364.         return;
  365.  
  366.     /* -Pr: special comments conveying current knowledge
  367.         of external references */
  368.  
  369.     k = proc_protochanges;
  370.     for (e = extsymtab, ee = nextext; e < ee; e++)
  371.         if (e->extstg == STGEXT
  372.         && e->cextname != e->fextname)    /* not a library function */
  373.             if (at = e->arginfo) {
  374.             if ((!e->extinit || at->changes & 1)
  375.                 /* not defined here or
  376.                     changed since definition */
  377.             && at->nargs >= 0) {
  378.                 nice_printf(outfile, "/*:ref: %s %d %d",
  379.                     e->cextname, e->extype, at->nargs);
  380.                 a = at->atypes;
  381.                 for(ae = a + at->nargs; a < ae; a++)
  382.                     nice_printf(outfile, " %d", a->type);
  383.                 nice_printf(outfile, " */\n");
  384.                 if (at->changes & 1)
  385.                     k++;
  386.                 }
  387.             }
  388.             else if (e->extype)
  389.             /* typed external, never invoked */
  390.             nice_printf(outfile, "/*:ref: %s %d :*/\n",
  391.                 e->cextname, e->extype);
  392.     if (k) {
  393.         nice_printf(outfile,
  394.     "/* Rerunning f2c -P may change prototypes or declarations. */\n");
  395.         if (nerr)
  396.             return;
  397.         if (protostatus)
  398.             done(4);
  399.         if (protofile != stdout) {
  400.             fprintf(diagfile,
  401.     "Rerunning \"f2c -P ... %s %s\" may change prototypes or declarations.\n",
  402.                 filename0, proto_fname);
  403.             fflush(diagfile);
  404.             }
  405.         }
  406.     }
  407.  
  408.  int retcode = 0;
  409.  
  410.  int
  411. #ifdef KR_headers
  412. main(argc, argv)
  413.     int argc;
  414.     char **argv;
  415. #else
  416. main(int argc, char **argv)
  417. #endif
  418. {
  419.     int c2d, k;
  420.     FILE *c_output;
  421.     char *cdfilename;
  422.     static char stderrbuf[BUFSIZ];
  423.     extern char **dfltproc, *dflt1proc[];
  424.     extern char link_msg[];
  425.  
  426.     diagfile = stderr;
  427.     setbuf(stderr, stderrbuf);    /* arrange for fast error msgs */
  428.  
  429.     Max_ftn_files = argc - 1;
  430.     ftn_files = (char **)ckalloc((argc+1)*sizeof(char *));
  431.  
  432.     parse_args (argc, argv, table, sizeof(table)/sizeof(arg_info),
  433.         ftn_files, Max_ftn_files);
  434.     if (!can_include && ext1comm == 2)
  435.         ext1comm = 1;
  436.     if (ext1comm && !extcomm)
  437.         extcomm = 2;
  438.     if (protostatus)
  439.         Castargs = 3;
  440.     Castargs1 = Castargs;
  441.     if (!Ansi) {
  442.         Castargs = 0;
  443.         parens = "()";
  444.         }
  445.     else if (!Castargs)
  446.         parens = Ansi == 1 ? "()" : "(...)";
  447.     else
  448.         dfltproc = dflt1proc;
  449.  
  450.     outbuf_adjust();
  451.     set_externs();
  452.     fileinit();
  453.     read_Pfiles(ftn_files);
  454.  
  455.     for(k = 1; ftn_files[k]; k++)
  456.         if (dofork())
  457.             break;
  458.     filename0 = file_name = ftn_files[current_ftn_file = k - 1];
  459.  
  460.     set_tmp_names();
  461.     sigcatch(0);
  462.  
  463.     c_file   = opf(c_functions, textwrite);
  464.     pass1_file=opf(p1_file, binwrite);
  465.     initkey();
  466.     if (file_name && *file_name) {
  467.         cdfilename = coutput;
  468.         if (debugflag != 1) {
  469.             coutput = c_name(file_name,'c');
  470.             cdfilename = copys(outbtail);
  471.             if (Castargs1 >= 2)
  472.                 proto_fname = c_name(file_name,'P');
  473.             }
  474.         if (skipC)
  475.             coutput = 0;
  476.         else if (!(c_output = fopen(coutput, textwrite))) {
  477.             file_name = coutput;
  478.             coutput = 0;    /* don't delete read-only .c file */
  479.             fatalstr("can't open %.86s", file_name);
  480.             }
  481.  
  482.         if (Castargs1 >= 2
  483.         && !(protofile = fopen(proto_fname, textwrite)))
  484.             fatalstr("Can't open %.84s\n", proto_fname);
  485.         }
  486.     else {
  487.         file_name = "";
  488.         cdfilename = "f2c_out.c";
  489.         c_output = stdout;
  490.         coutput = 0;
  491.         if (Castargs1 >= 2) {
  492.             protofile = stdout;
  493.             if (!skipC)
  494.                 printf("#ifdef P_R_O_T_O_T_Y_P_E_S\n");
  495.             }
  496.         }
  497.  
  498.     if(inilex( copys(file_name) ))
  499.         done(1);
  500.     if (filename0) {
  501.         fprintf(diagfile, "%s:\n", file_name);
  502.         fflush(diagfile);
  503.         }
  504.  
  505.     procinit();
  506.     if(k = yyparse())
  507.     {
  508.         fprintf(diagfile, "Bad parse, return code %d\n", k);
  509.         done(1);
  510.     }
  511.  
  512.     commonprotos(protofile);
  513.     if (protofile == stdout && !skipC)
  514.         printf("#endif\n\n");
  515.  
  516.     if (nerr || skipC)
  517.         goto C_skipped;
  518.  
  519.  
  520. /* Write out the declarations which are global to this file */
  521.  
  522.     if ((c2d = comm2dcl()) == 1)
  523.         nice_printf(c_output, "/*>>>'/dev/null'<<<*/\n\n\
  524. /* Split this into several files by piping it through\n\n\
  525. sed \"s/^\\/\\*>>>'\\(.*\\)'<<<\\*\\/\\$/cat >'\\1' <<'\\/*<<<\\1>>>*\\/'/\" | /bin/sh\n\
  526.  */\n\
  527. /*<<</dev/null>>>*/\n\
  528. /*>>>'%s'<<<*/\n", cdfilename);
  529.     if (gflag)
  530.         nice_printf (c_output, "#line 1 \"%s\"\n", file_name);
  531.     if (!skipversion) {
  532.         nice_printf (c_output, "/* %s -- translated by f2c ", file_name);
  533.         nice_printf (c_output, "(version %s).\n", F2C_version);
  534.         nice_printf (c_output,
  535.     "   You must link the resulting object file with the libraries:\n\
  536.     %s   (in that order)\n*/\n\n", link_msg);
  537.         }
  538.     if (Ansi == 2)
  539.         nice_printf(c_output,
  540.             "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
  541.     nice_printf (c_output, "%s#include \"f2c.h\"\n\n", def_i2);
  542.     if (gflag)
  543.         nice_printf (c_output, "#line 1 \"%s\"\n", file_name);
  544.     if (Castargs && typedefs)
  545.         write_typedefs(c_output);
  546.     nice_printf (c_file, "\n");
  547.     fclose (c_file);
  548.     c_file = c_output;        /* HACK to get the next indenting
  549.                        to work */
  550.     wr_common_decls (c_output);
  551.     if (blkdfile)
  552.         list_init_data(&blkdfile, blkdfname, c_output);
  553.     wr_globals (c_output);
  554.     if ((c_file = fopen (c_functions, textread)) == (FILE *) NULL)
  555.         Fatal("main - couldn't reopen c_functions");
  556.     ffilecopy (c_file, c_output);
  557.     if (*main_alias) {
  558.         nice_printf (c_output, "/* Main program alias */ ");
  559.         nice_printf (c_output, "int %s () { MAIN__ ();%s }\n",
  560.             main_alias, Ansi ? " return 0;" : "");
  561.         }
  562.     if (Ansi == 2)
  563.         nice_printf(c_output,
  564.             "#ifdef __cplusplus\n\t}\n#endif\n");
  565.     if (c2d) {
  566.         if (c2d == 1)
  567.             fprintf(c_output, "/*<<<%s>>>*/\n", cdfilename);
  568.         else
  569.             fclose(c_output);
  570.         def_commons(c_output);
  571.         }
  572.     if (c2d != 2)
  573.         fclose (c_output);
  574.  
  575.  C_skipped:
  576.     if(parstate != OUTSIDE)
  577.         {
  578.         warn("missing final end statement");
  579.         endproc();
  580.         }
  581.     done(nerr ? 1 : 0);
  582.     /* NOT REACHED */ return 0;
  583. }
  584.  
  585.  
  586.  FILEP
  587. #ifdef KR_headers
  588. opf(fn, mode)
  589.     char *fn;
  590.     char *mode;
  591. #else
  592. opf(char *fn, char *mode)
  593. #endif
  594. {
  595.     FILEP fp;
  596.     if( fp = fopen(fn, mode) )
  597.         return(fp);
  598.  
  599.     fatalstr("cannot open intermediate file %s", fn);
  600.     /* NOT REACHED */ return 0;
  601. }
  602.  
  603.  
  604.  void
  605. #ifdef KR_headers
  606. clf(p, what, quit)
  607.     FILEP *p;
  608.     char *what;
  609.     int quit;
  610. #else
  611. clf(FILEP *p, char *what, int quit)
  612. #endif
  613. {
  614.     if(p!=NULL && *p!=NULL && *p!=stdout)
  615.     {
  616.         if(ferror(*p)) {
  617.             fprintf(stderr, "I/O error on %s\n", what);
  618.             if (quit)
  619.                 done(3);
  620.             retcode = 3;
  621.             }
  622.         fclose(*p);
  623.     }
  624.     *p = NULL;
  625. }
  626.  
  627.  
  628.  void
  629. #ifdef KR_headers
  630. done(k)
  631.     int k;
  632. #else
  633. done(int k)
  634. #endif
  635. {
  636.     clf(&initfile, "initfile", 0);
  637.     clf(&c_file, "c_file", 0);
  638.     clf(&pass1_file, "pass1_file", 0);
  639.     Un_link_all(k);
  640.     exit(k|retcode);
  641. }
  642.