home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / src / main.c < prev    next >
C/C++ Source or Header  |  1999-12-13  |  17KB  |  598 lines

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