home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / kpathsea / tex-make.c < prev    next >
C/C++ Source or Header  |  2000-01-15  |  11KB  |  352 lines

  1. /* tex-make.c: Run external programs to make TeX-related files.
  2.  
  3. Copyright (C) 1993, 94, 95, 96, 97 Karl Berry.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/c-fopen.h>
  22. #include <kpathsea/c-pathch.h>
  23. #include <kpathsea/concatn.h>
  24. #include <kpathsea/db.h>
  25. #include <kpathsea/fn.h>
  26. #include <kpathsea/magstep.h>
  27. #include <kpathsea/readable.h>
  28. #include <kpathsea/tex-make.h>
  29. #include <kpathsea/variable.h>
  30.  
  31.  
  32. /* We never throw away stdout, since that is supposed to be the filename
  33.    found, if all is successful.  This variable controls whether stderr
  34.    is thrown away.  */
  35. boolean kpse_make_tex_discard_errors = false;
  36.  
  37. /* We set the envvar MAKETEX_MAG, which is part of the default spec for
  38.    MakeTeXPK above, based on KPATHSEA_DPI and MAKETEX_BASE_DPI.  */
  39.  
  40. static void
  41. set_maketex_mag P1H(void)
  42. {
  43.   char q[MAX_INT_LENGTH * 3 + 3];
  44.   int m;
  45.   string dpi_str = getenv ("KPATHSEA_DPI");
  46.   string bdpi_str = getenv ("MAKETEX_BASE_DPI");
  47.   unsigned dpi = dpi_str ? atoi (dpi_str) : 0;
  48.   unsigned bdpi = bdpi_str ? atoi (bdpi_str) : 0;
  49.  
  50.   /* If the environment variables aren't set, it's a bug.  */
  51.   assert (dpi != 0 && bdpi != 0);
  52.   
  53.   /* Fix up for roundoff error.  Hopefully the driver has already fixed
  54.      up DPI, but may as well be safe, and also get the magstep number.  */
  55.   (void) kpse_magstep_fix (dpi, bdpi, &m);
  56.   
  57.   if (m == 0)
  58.     sprintf (q, "%d+%d/%d", dpi / bdpi, dpi % bdpi, bdpi);
  59.   else
  60.     { /* m is encoded with LSB being a ``half'' bit (see magstep.h).  Are
  61.          we making an assumption here about two's complement?  Probably.
  62.          In any case, if m is negative, we have to put in the sign
  63.          explicitly, since m/2==0 if m==-1.  */
  64.       const_string sign = "";
  65.       if (m < 0)
  66.         {
  67.           m *= -1;
  68.           sign = "-";
  69.         }
  70.       sprintf (q, "magstep\\(%s%d.%d\\)", sign, m / 2, (m & 1) * 5);
  71.     }  
  72.   xputenv ("MAKETEX_MAG", q);
  73. }
  74.  
  75. /* This mktex... program was disabled, or the script failed.  If this
  76.    was a font creation (according to FORMAT), append CMD
  77.    to a file missfont.log in the current directory.  */
  78.  
  79. static void
  80. misstex P2C(kpse_file_format_type, format,  const_string, cmd)
  81. {
  82.   static FILE *missfont = NULL;
  83.  
  84.   /* If we weren't trying to make a font, do nothing.  Maybe should
  85.      allow people to specify what they want recorded?  */
  86.   if (format > kpse_any_glyph_format && format != kpse_tfm_format
  87.       && format != kpse_vf_format)
  88.     return;
  89.  
  90.   /* If this is the first time, have to open the log file.  But don't
  91.      bother logging anything if they were discarding errors.  */
  92.   if (!missfont && !kpse_make_tex_discard_errors) {
  93.     const_string missfont_name = kpse_var_value ("MISSFONT_LOG");
  94.     if (!missfont_name || *missfont_name == '1') {
  95.       missfont_name = "missfont.log"; /* take default name */
  96.     } else if (missfont_name
  97.                && (*missfont_name == 0 || *missfont_name == '0')) {
  98.       missfont_name = NULL; /* user requested no missfont.log */
  99.     } /* else use user's name */
  100.  
  101.     missfont = missfont_name ? fopen (missfont_name, FOPEN_A_MODE) : NULL;
  102.     if (!missfont && kpse_var_value ("TEXMFOUTPUT")) {
  103.       missfont_name = concat3 (kpse_var_value ("TEXMFOUTPUT"), DIR_SEP_STRING,
  104.                                missfont_name);
  105.       missfont = fopen (missfont_name, FOPEN_A_MODE);
  106.     }
  107.  
  108.     if (missfont)
  109.       fprintf (stderr, "kpathsea: Appending font creation commands to %s.\n",
  110.                missfont_name);
  111.   }
  112.   
  113.   /* Write the command if we have a log file.  */
  114.   if (missfont) {
  115.     fputs (cmd, missfont);
  116.     putc ('\n', missfont);
  117.   }
  118. }  
  119.  
  120.  
  121. /* Assume the script outputs the filename it creates (and nothing
  122.    else) on standard output; hence, we run the script with `popen'.  */
  123.  
  124. static string
  125. maketex P2C(kpse_file_format_type, format,  const_string, passed_cmd)
  126. {
  127.   string ret;
  128.   unsigned i;
  129.   FILE *f;
  130.   string cmd = xstrdup (passed_cmd);
  131.  
  132. #if defined (MSDOS) || defined (WIN32)
  133.   /* For discarding stderr.  This is so we don't require an MSDOS user
  134.      to istall a unixy shell (see kpse_make_tex below): they might
  135.      devise their own ingenious ways of running mktex... even though
  136.      they don't have such a shell.  */
  137.   int temp_stderr = -1;
  138.   int save_stderr = -1;
  139. #endif
  140.  
  141.   /* If the user snuck `backquotes` or $(command) substitutions into the
  142.      name, foil them.  */
  143.   for (i = 0; i < strlen (cmd); i++) {
  144.     if (cmd[i] == '`' || (cmd[i] == '$' && cmd[i+1] == '(')) {
  145.       cmd[i] = '#';
  146.     }
  147.   }
  148.  
  149.   /* Tell the user we are running the script, so they have a clue as to
  150.      what's going on if something messes up.  But if they asked to
  151.      discard output, they probably don't want to see this, either.  */
  152.   if (!kpse_make_tex_discard_errors) {
  153.     fprintf (stderr, "kpathsea: Running %s\n", cmd);
  154.   }
  155. #if defined (MSDOS) || defined (WIN32)
  156.   else {
  157.     temp_stderr = open ("NUL", O_WRONLY);
  158.     if (temp_stderr >= 0) {
  159.       save_stderr = dup (2);
  160.       if (save_stderr >= 0)
  161.         dup2 (temp_stderr, 2);
  162.     }
  163.     /* Else they lose: the errors WILL be shown.  However, open/dup
  164.        aren't supposed to fail in this case, it's just my paranoia. */
  165.   }
  166. #endif
  167.   
  168.   /* Run the script.  The Amiga has a different interface.  */
  169. #ifdef AMIGA
  170.   ret = system (cmd) == 0 ? getenv ("LAST_FONT_CREATED") : NULL;
  171. #else /* not AMIGA */
  172.   f = popen (cmd, FOPEN_R_MODE);
  173.  
  174. #if defined (MSDOS) || defined (WIN32)
  175.   if (kpse_make_tex_discard_errors) {
  176.     /* Close /dev/null and revert stderr.  */
  177.     if (save_stderr >= 0) {
  178.       dup2 (save_stderr, 2);
  179.       close (save_stderr);
  180.     }
  181.     if (temp_stderr >= 0)
  182.       close (temp_stderr);
  183.   }
  184. #endif
  185.  
  186.   if (f) {
  187.     int c;
  188.     string fn;             /* The final filename.  */
  189.     unsigned len;          /* And its length.  */
  190.     fn_type output;
  191.     output = fn_init ();   /* Collect the script output.  */
  192.  
  193.     /* Read all the output and terminate with a null.  */
  194.     while ((c = getc (f)) != EOF)
  195.       fn_1grow (&output, c);
  196.     fn_1grow (&output, 0);
  197.  
  198.     /* Maybe should check for `EXIT_SUCCESS' status before even
  199.        looking at the output?  In some versions of Linux, pclose fails
  200.        with ECHILD (No child processes), maybe only if we're being run
  201.        by lpd.  So don't make this a fatal error.  */
  202.     if (pclose (f) == -1) {
  203.       perror ("pclose(mktexpk)");
  204.       WARNING ("kpathsea: This is probably the Linux pclose bug; continuing");
  205.     }
  206.  
  207.     len = FN_LENGTH (output);
  208.     fn = FN_STRING (output);
  209.  
  210.     /* Remove trailing newlines and returns.  */
  211.     while (len > 1 && (fn[len - 2] == '\n' || fn[len - 2] == '\r')) {
  212.       fn[len - 2] = 0;
  213.       len--;
  214.     }
  215.  
  216.     /* If no output from script, return NULL.  Otherwise check
  217.        what it output.  */
  218.     ret = len == 1 ? NULL : kpse_readable_file (fn);
  219.     if (!ret && len > 1) {
  220.       WARNING1 ("kpathsea: mktexpk output `%s' instead of a filename", fn);
  221.     }
  222.  
  223.     /* Free the name if we're not returning it.  */
  224.     if (fn != ret)
  225.       free (fn);
  226.   } else {
  227.     /* popen failed.  */
  228.     perror ("kpathsea");
  229.     ret = NULL;
  230.   }
  231. #endif /* not AMIGA */
  232.  
  233.   if (ret == NULL)
  234.     misstex (format, cmd);
  235.   else
  236.     kpse_db_insert (ret);
  237.     
  238.   return ret;
  239. }
  240.  
  241.  
  242. /* Create BASE in FORMAT and return the generated filename, or
  243.    return NULL.  */
  244.  
  245. string
  246. kpse_make_tex P2C(kpse_file_format_type, format,  const_string, base)
  247. {
  248.   kpse_format_info_type spec; /* some compilers lack struct initialization */
  249.   string ret = NULL;
  250.   
  251.   spec = kpse_format_info[format];
  252.   if (!spec.type) { /* Not initialized yet? */
  253.     kpse_init_format (format);
  254.     spec = kpse_format_info[format];
  255.   }
  256.   
  257.   if (spec.program && spec.program_enabled_p) {
  258.     /* See the documentation for the envvars we're dealing with here.  */
  259.     string args, cmd;
  260.     const_string prog = spec.program;
  261.     const_string arg_spec = spec.program_args;
  262.  
  263.     if (format <= kpse_any_glyph_format)
  264.       set_maketex_mag ();
  265.  
  266.     /* Here's an awful kludge: if the mode is `/', mktexpk recognizes
  267.        it as a special case.  `kpse_prog_init' sets it to this in the
  268.        first place when no mode is otherwise specified; this is so
  269.        when the user defines a resolution, they don't also have to
  270.        specify a mode; instead, mktexpk's guesses will take over.
  271.        They use / for the value because then when it is expanded as
  272.        part of the PKFONTS et al. path values, we'll wind up searching
  273.        all the pk directories.  We put $MAKETEX_MODE in the path
  274.        values in the first place so that sites with two different
  275.        devices with the same resolution can find the right fonts; but
  276.        such sites are uncommon, so they shouldn't make things harder
  277.        for everyone else.  */
  278.     args = arg_spec ? kpse_var_expand (arg_spec) : (string) "";
  279.  
  280.     /* The command is the program name plus the arguments.  */
  281.     cmd = concatn (prog, " ", args, " ", base, NULL);
  282.  
  283.     /* Only way to discard errors is redirect stderr inside another
  284.        shell; otherwise, if the mktex... script doesn't exist, we
  285.        will see the `sh: mktex...: not found' error.  No point in
  286.        doing this if we're not actually going to run anything.  */
  287. #if !defined(MSDOS) && !defined(WIN32) && !defined(AMIGA)
  288.     /* We don't want to require that a Unix-like shell be installed
  289.        on MS-DOS or WIN32 systems, so we will redirect stderr by hand
  290.        (in maketex).  */
  291.     if (kpse_make_tex_discard_errors) {
  292.       string old_cmd = cmd;
  293. #ifdef OS2
  294.       cmd = concat3 ("cmd /c \"", cmd, "\" 2>/dev/nul");
  295. #else
  296.       cmd = concat3 ("sh -c \"", cmd, "\" 2>/dev/null");
  297. #endif
  298.       free (old_cmd);
  299.     }
  300. #endif
  301.     
  302.     ret = maketex (format, cmd);
  303.  
  304.     free (cmd);
  305.     if (*args)
  306.       free (args);
  307.   }
  308.  
  309.   return ret;
  310. }
  311.  
  312. #ifdef TEST
  313.  
  314. void
  315. test_make_tex (kpse_file_format_type fmt, const_string base)
  316. {
  317.   string answer;
  318.   
  319.   printf ("\nAttempting %s in format %d:\n", base, fmt);
  320.  
  321.   answer = kpse_make_tex (fmt, base);
  322.   puts (answer ? answer : "(nil)");
  323. }
  324.  
  325.  
  326. int
  327. main ()
  328. {
  329.   xputenv ("KPATHSEA_DPI", "781"); /* call mktexpk */
  330.   xputenv ("MAKETEX_BASE_DPI", "300"); /* call mktexpk */
  331.   KPSE_MAKE_SPEC_ENABLED (kpse_make_specs[kpse_pk_format]) = true;
  332.   test_make_tex (kpse_pk_format, "cmr10");
  333.  
  334.   /* Fail with mktextfm.  */
  335.   KPSE_MAKE_SPEC_ENABLED (kpse_make_specs[kpse_tfm_format]) = true;
  336.   test_make_tex (kpse_tfm_format, "foozler99");
  337.   
  338.   /* Call something disabled.  */
  339.   test_make_tex (kpse_bst_format, "no-way");
  340.   
  341.   return 0;
  342. }
  343.  
  344. #endif /* TEST */
  345.  
  346.  
  347. /*
  348. Local variables:
  349. test-compile-command: "gcc -g -I. -I.. -DTEST tex-make.c kpathsea.a"
  350. End:
  351. */
  352.