home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / f2c-93.04.28-src.tgz / tar.out / fsf / f2c / src / sysdep.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  11KB  |  447 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1992, 1993 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. #include "defs.h"
  24. #include "usignal.h"
  25.  
  26. char binread[] = "rb", textread[] = "r";
  27. char binwrite[] = "wb", textwrite[] = "w";
  28. char *c_functions    = "c_functions";
  29. char *coutput        = "c_output";
  30. char *initfname        = "raw_data";
  31. char *initbname        = "raw_data.b";
  32. char *blkdfname        = "block_data";
  33. char *p1_file        = "p1_file";
  34. char *p1_bakfile    = "p1_file.BAK";
  35. char *sortfname        = "init_file";
  36. char *proto_fname    = "proto_file";
  37.  
  38. char link_msg[]        = "-lf2c -lm"; /* was "-lF77 -lI77 -lm -lc"; */
  39.  
  40. #ifndef TMPDIR
  41. #ifdef MSDOS
  42. #define TMPDIR ""
  43. #else
  44. #define TMPDIR "/tmp"
  45. #endif
  46. #endif
  47.  
  48. char *tmpdir = TMPDIR;
  49.  
  50.  void
  51. Un_link_all(cdelete)
  52. {
  53.     if (!debugflag) {
  54.         unlink(c_functions);
  55.         unlink(initfname);
  56.         unlink(p1_file);
  57.         unlink(sortfname);
  58.         unlink(blkdfname);
  59.         if (cdelete && coutput)
  60.             unlink(coutput);
  61.         }
  62.     }
  63.  
  64.  void
  65. set_tmp_names()
  66. {
  67.     int k;
  68.     if (debugflag == 1)
  69.         return;
  70.     k = strlen(tmpdir) + 16;
  71.     c_functions = (char *)ckalloc(7*k);
  72.     initfname = c_functions + k;
  73.     initbname = initfname + k;
  74.     blkdfname = initbname + k;
  75.     p1_file = blkdfname + k;
  76.     p1_bakfile = p1_file + k;
  77.     sortfname = p1_bakfile + k;
  78.     {
  79. #ifdef MSDOS
  80.     char buf[64], *s, *t;
  81.     if (!*tmpdir || *tmpdir == '.' && !tmpdir[1])
  82.         t = "";
  83.     else {
  84.         /* substitute \ for / to avoid confusion with a
  85.          * switch indicator in the system("sort ...")
  86.          * call in formatdata.c
  87.          */
  88.         for(s = tmpdir, t = buf; *s; s++, t++)
  89.             if ((*t = *s) == '/')
  90.                 *t = '\\';
  91.         if (t[-1] != '\\')
  92.             *t++ = '\\';
  93.         *t = 0;
  94.         t = buf;
  95.         }
  96.     sprintf(c_functions, "%sf2c_func", t);
  97.     sprintf(initfname, "%sf2c_rd", t);
  98.     sprintf(blkdfname, "%sf2c_blkd", t);
  99.     sprintf(p1_file, "%sf2c_p1f", t);
  100.     sprintf(p1_bakfile, "%sf2c_p1fb", t);
  101.     sprintf(sortfname, "%sf2c_sort", t);
  102. #else
  103.     int pid = getpid();
  104.     pid %= (1 << 16);    /* Allocation assumes 16 bit pid values? */
  105.     sprintf(c_functions, "%s/f2c%d_func", tmpdir, pid);
  106.     sprintf(initfname, "%s/f2c%d_rd", tmpdir, pid);
  107.     sprintf(blkdfname, "%s/f2c%d_blkd", tmpdir, pid);
  108.     sprintf(p1_file, "%s/f2c%d_p1f", tmpdir, pid);
  109.     sprintf(p1_bakfile, "%s/f2c%d_p1fb", tmpdir, pid);
  110.     sprintf(sortfname, "%s/f2c%d_sort", tmpdir, pid);
  111. #endif
  112.     sprintf(initbname, "%s.b", initfname);
  113.     }
  114.     if (debugflag)
  115.         fprintf(diagfile, "%s %s %s %s %s %s\n", c_functions,
  116.             initfname, blkdfname, p1_file, p1_bakfile, sortfname);
  117.     }
  118.  
  119.  char *
  120. c_name(s,ft)char *s;
  121. {
  122.     char *b, *s0;
  123.     int c;
  124.  
  125.     b = s0 = s;
  126.     while(c = *s++)
  127.         if (c == '/')
  128.             b = s;
  129.     if (--s < s0 + 3 || s[-2] != '.'
  130.              || ((c = *--s) != 'f' && c != 'F')) {
  131.         infname = s0;
  132.         Fatal("file name must end in .f or .F");
  133.         }
  134.     *s = ft;
  135.     b = copys(b);
  136.     *s = c;
  137.     return b;
  138.     }
  139.  
  140.  static void
  141. killed(sig)
  142. {
  143.     signal(SIGINT, SIG_IGN);
  144. #ifdef SIGQUIT
  145.     signal(SIGQUIT, SIG_IGN);
  146. #endif
  147. #ifdef SIGHUP
  148.     signal(SIGHUP, SIG_IGN);
  149. #endif
  150.     signal(SIGTERM, SIG_IGN);
  151.     Un_link_all(1);
  152.     exit(126);
  153.     }
  154.  
  155.  static void
  156. sig1catch(sig)
  157. {
  158.     if (signal(sig, SIG_IGN) != SIG_IGN)
  159.         signal(sig, killed);
  160.     }
  161.  
  162.  static void
  163. flovflo(sig)
  164. {
  165.     Fatal("floating exception during constant evaluation; cannot recover");
  166.     /* vax returns a reserved operand that generates
  167.        an illegal operand fault on next instruction,
  168.        which if ignored causes an infinite loop.
  169.     */
  170.     signal(SIGFPE, flovflo);
  171. }
  172.  
  173.  void
  174. sigcatch(sig)
  175. {
  176.     sig1catch(SIGINT);
  177. #ifdef SIGQUIT
  178.     sig1catch(SIGQUIT);
  179. #endif
  180. #ifdef SIGHUP
  181.     sig1catch(SIGHUP);
  182. #endif
  183.     sig1catch(SIGTERM);
  184.     signal(SIGFPE, flovflo);  /* catch overflows */
  185.     }
  186.  
  187. #ifdef __amigaos__
  188. #define fork vfork
  189. #endif
  190.  
  191. dofork()
  192. {
  193. #ifdef MSDOS
  194.     Fatal("Only one Fortran input file allowed under MS-DOS");
  195. #else
  196.     int pid, status, w;
  197.     extern int retcode;
  198.  
  199.     if (!(pid = fork()))
  200.         return 1;
  201.     if (pid == -1)
  202.         Fatal("bad fork");
  203.     while((w = wait(&status)) != pid)
  204.         if (w == -1)
  205.             Fatal("bad wait code");
  206.     retcode |= status >> 8;
  207. #endif
  208.     return 0;
  209.     }
  210.  
  211. /* Initialization of tables that change with the character set... */
  212.  
  213. char escapes[Table_size];
  214.  
  215. #ifdef non_ASCII
  216. char *str_fmt[Table_size];
  217. static char *str0fmt[127] = { /*}*/
  218. #else
  219. char *str_fmt[Table_size] = {
  220. #endif
  221.  "\\000", "\\001", "\\002", "\\003", "\\004", "\\005", "\\006", "\\007",
  222.    "\\b",   "\\t",   "\\n", "\\013",   "\\f",   "\\r", "\\016", "\\017",
  223.  "\\020", "\\021", "\\022", "\\023", "\\024", "\\025", "\\026", "\\027",
  224.  "\\030", "\\031", "\\032", "\\033", "\\034", "\\035", "\\036", "\\037",
  225.      " ",     "!",  "\\\"",     "#",     "$",     "%%",    "&",     "'",
  226.      "(",     ")",     "*",     "+",     ",",     "-",     ".",     "/",
  227.      "0",     "1",     "2",     "3",     "4",     "5",     "6",     "7",
  228.      "8",     "9",     ":",     ";",     "<",     "=",     ">",     "?",
  229.      "@",     "A",     "B",     "C",     "D",     "E",     "F",     "G",
  230.      "H",     "I",     "J",     "K",     "L",     "M",     "N",     "O",
  231.      "P",     "Q",     "R",     "S",     "T",     "U",     "V",     "W",
  232.      "X",     "Y",     "Z",     "[",  "\\\\",     "]",     "^",     "_",
  233.      "`",     "a",     "b",     "c",     "d",     "e",     "f",     "g",
  234.      "h",     "i",     "j",     "k",     "l",     "m",     "n",     "o",
  235.      "p",     "q",     "r",     "s",     "t",     "u",     "v",     "w",
  236.      "x",     "y",     "z",     "{",     "|",     "}",     "~"
  237.      };
  238.  
  239. #ifdef non_ASCII
  240. char *chr_fmt[Table_size];
  241. static char *chr0fmt[127] = {    /*}*/
  242. #else
  243. char *chr_fmt[Table_size] = {
  244. #endif
  245.    "\\0",   "\\1",   "\\2",   "\\3",   "\\4",   "\\5",   "\\6",   "\\7",
  246.    "\\b",   "\\t",   "\\n",  "\\13",   "\\f",   "\\r",  "\\16",  "\\17",
  247.   "\\20",  "\\21",  "\\22",  "\\23",  "\\24",  "\\25",  "\\26",  "\\27",
  248.   "\\30",  "\\31",  "\\32",  "\\33",  "\\34",  "\\35",  "\\36",  "\\37",
  249.      " ",     "!",    "\"",     "#",     "$",     "%%",    "&",   "\\'",
  250.      "(",     ")",     "*",     "+",     ",",     "-",     ".",     "/",
  251.      "0",     "1",     "2",     "3",     "4",     "5",     "6",     "7",
  252.      "8",     "9",     ":",     ";",     "<",     "=",     ">",     "?",
  253.      "@",     "A",     "B",     "C",     "D",     "E",     "F",     "G",
  254.      "H",     "I",     "J",     "K",     "L",     "M",     "N",     "O",
  255.      "P",     "Q",     "R",     "S",     "T",     "U",     "V",     "W",
  256.      "X",     "Y",     "Z",     "[",  "\\\\",     "]",     "^",     "_",
  257.      "`",     "a",     "b",     "c",     "d",     "e",     "f",     "g",
  258.      "h",     "i",     "j",     "k",     "l",     "m",     "n",     "o",
  259.      "p",     "q",     "r",     "s",     "t",     "u",     "v",     "w",
  260.      "x",     "y",     "z",     "{",     "|",     "}",     "~"
  261.      };
  262.  
  263.  void
  264. fmt_init()
  265. {
  266.     static char *str1fmt[6] =
  267.         { "\\b", "\\t", "\\n", "\\f", "\\r", "\\%03o" };
  268.     register int i, j;
  269.     register char *s;
  270.  
  271.     /* str_fmt */
  272.  
  273. #ifdef non_ASCII
  274.     i = 0;
  275. #else
  276.     i = 127;
  277. #endif
  278.     for(; i < Table_size; i++)
  279.         str_fmt[i] = "\\%03o";
  280. #ifdef non_ASCII
  281.     for(i = 32; i < 127; i++) {
  282.         s = str0fmt[i];
  283.         str_fmt[*(unsigned char *)s] = s;
  284.         }
  285.     str_fmt['"'] = "\\\"";
  286. #else
  287.     if (Ansi == 1)
  288.         str_fmt[7] = chr_fmt[7] = "\\a";
  289. #endif
  290.  
  291.     /* chr_fmt */
  292.  
  293. #ifdef non_ASCII
  294.     for(i = 0; i < 32; i++)
  295.         chr_fmt[i] = chr0fmt[i];
  296. #else
  297.     i = 127;
  298. #endif
  299.     for(; i < Table_size; i++)
  300.         chr_fmt[i] = "\\%o";
  301. #ifdef non_ASCII
  302.     for(i = 32; i < 127; i++) {
  303.         s = chr0fmt[i];
  304.         j = *(unsigned char *)s;
  305.         if (j == '\\')
  306.             j = *(unsigned char *)(s+1);
  307.         chr_fmt[j] = s;
  308.         }
  309. #endif
  310.  
  311.     /* escapes (used in lex.c) */
  312.  
  313.     for(i = 0; i < Table_size; i++)
  314.         escapes[i] = i;
  315.     for(s = "btnfr0", i = 0; i < 6; i++)
  316.         escapes[*(unsigned char *)s++] = "\b\t\n\f\r"[i];
  317.     /* finish str_fmt and chr_fmt */
  318.  
  319.     if (Ansi)
  320.         str1fmt[5] = "\\v";
  321.     if ('\v' == 'v') { /* ancient C compiler */
  322.         str1fmt[5] = "v";
  323. #ifndef non_ASCII
  324.         escapes['v'] = 11;
  325. #endif
  326.         }
  327.     else
  328.         escapes['v'] = '\v';
  329.     for(s = "\b\t\n\f\r\v", i = 0; j = *(unsigned char *)s++;)
  330.         str_fmt[j] = chr_fmt[j] = str1fmt[i++];
  331.     /* '\v' = 11 for both EBCDIC and ASCII... */
  332.     chr_fmt[11] = Ansi ? "\\v" : "\\13";
  333.     }
  334.  
  335.  
  336.  
  337. /* Unless SYSTEM_SORT is defined, the following gives a simple
  338.  * in-core version of dsort().  On Fortran source with huge DATA
  339.  * statements, the in-core version may exhaust the available memory,
  340.  * in which case you might either recompile this source file with
  341.  * SYSTEM_SORT defined (if that's reasonable on your system), or
  342.  * replace the dsort below with a more elaborate version that
  343.  * does a merging sort with the help of auxiliary files.
  344.  */
  345.  
  346. #ifdef SYSTEM_SORT
  347.  
  348. dsort(from, to)
  349.  char *from, *to;
  350. {
  351.     char buf[200];
  352.     sprintf(buf, "sort <%s >%s", from, to);
  353.     return system(buf) >> 8;
  354.     }
  355. #else
  356.  
  357.  static int
  358. compare(a,b)
  359.  char *a, *b;
  360. { return strcmp(*(char **)a, *(char **)b); }
  361.  
  362. dsort(from, to)
  363.  char *from, *to;
  364. {
  365.     extern char *Alloc();
  366.  
  367.     struct Memb {
  368.         struct Memb *next;
  369.         int n;
  370.         char buf[32000];
  371.         };
  372.     typedef struct Memb memb;
  373.     memb *mb, *mb1;
  374.     register char *x, *x0, *xe;
  375.     register int c, n;
  376.     FILE *f;
  377.     char **z, **z0;
  378.     int nn = 0;
  379.  
  380.     f = opf(from, textread);
  381.     mb = (memb *)Alloc(sizeof(memb));
  382.     mb->next = 0;
  383.     x0 = x = mb->buf;
  384.     xe = x + sizeof(mb->buf);
  385.     n = 0;
  386.     for(;;) {
  387.         c = getc(f);
  388.         if (x >= xe && (c != EOF || x != x0)) {
  389.             if (!n)
  390.                 return 126;
  391.             nn += n;
  392.             mb->n = n;
  393.             mb1 = (memb *)Alloc(sizeof(memb));
  394.             mb1->next = mb;
  395.             mb = mb1;
  396.             memcpy(mb->buf, x0, n = x-x0);
  397.             x0 = mb->buf;
  398.             x = x0 + n;
  399.             xe = x0 + sizeof(mb->buf);
  400.             n = 0;
  401.             }
  402.         if (c == EOF)
  403.             break;
  404.         if (c == '\n') {
  405.             ++n;
  406.             *x++ = 0;
  407.             x0 = x;
  408.             }
  409.         else
  410.             *x++ = c;
  411.         }
  412.     clf(&f, from, 1);
  413.     f = opf(to, textwrite);
  414.     if (x > x0) { /* shouldn't happen */
  415.         *x = 0;
  416.         ++n;
  417.         }
  418.     mb->n = n;
  419.     nn += n;
  420.     if (!nn) /* shouldn't happen */
  421.         goto done;
  422.     z = z0 = (char **)Alloc(nn*sizeof(char *));
  423.     for(mb1 = mb; mb1; mb1 = mb1->next) {
  424.         x = mb1->buf;
  425.         n = mb1->n;
  426.         for(;;) {
  427.             *z++ = x;
  428.             if (--n <= 0)
  429.                 break;
  430.             while(*x++);
  431.             }
  432.         }
  433.     qsort((char *)z0, nn, sizeof(char *), compare);
  434.     for(n = nn, z = z0; n > 0; n--)
  435.         fprintf(f, "%s\n", *z++);
  436.     free((char *)z0);
  437.  done:
  438.     clf(&f, to, 1);
  439.     do {
  440.         mb1 = mb->next;
  441.         free((char *)mb);
  442.         }
  443.         while(mb = mb1);
  444.     return 0;
  445.     }
  446. #endif
  447.