File: tex-make.c

package info (click to toggle)
octave 2.0.13-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 23,828 kB
  • ctags: 13,172
  • sloc: cpp: 66,241; fortran: 37,245; ansic: 26,548; sh: 7,269; makefile: 3,808; lex: 1,943; yacc: 1,844; perl: 1,676; lisp: 1,662; exp: 123
file content (351 lines) | stat: -rw-r--r-- 11,192 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* tex-make.c: Run external programs to make TeX-related files.

Copyright (C) 1993, 94, 95, 96, 97 Karl Berry.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <kpathsea/config.h>

#include <kpathsea/c-fopen.h>
#include <kpathsea/c-pathch.h>
#include <kpathsea/concatn.h>
#include <kpathsea/db.h>
#include <kpathsea/fn.h>
#include <kpathsea/magstep.h>
#include <kpathsea/readable.h>
#include <kpathsea/tex-make.h>
#include <kpathsea/variable.h>


/* We never throw away stdout, since that is supposed to be the filename
   found, if all is successful.  This variable controls whether stderr
   is thrown away.  */
boolean kpse_make_tex_discard_errors = false;

/* We set the envvar MAKETEX_MAG, which is part of the default spec for
   MakeTeXPK above, based on KPATHSEA_DPI and MAKETEX_BASE_DPI.  */

static void
set_maketex_mag P1H(void)
{
  char q[MAX_INT_LENGTH * 3 + 3];
  int m;
  string dpi_str = getenv ("KPATHSEA_DPI");
  string bdpi_str = getenv ("MAKETEX_BASE_DPI");
  unsigned dpi = dpi_str ? atoi (dpi_str) : 0;
  unsigned bdpi = bdpi_str ? atoi (bdpi_str) : 0;

  /* If the environment variables aren't set, it's a bug.  */
  assert (dpi != 0 && bdpi != 0);
  
  /* Fix up for roundoff error.  Hopefully the driver has already fixed
     up DPI, but may as well be safe, and also get the magstep number.  */
  (void) kpse_magstep_fix (dpi, bdpi, &m);
  
  if (m == 0)
    sprintf (q, "%d+%d/%d", dpi / bdpi, dpi % bdpi, bdpi);
  else
    { /* m is encoded with LSB being a ``half'' bit (see magstep.h).  Are
         we making an assumption here about two's complement?  Probably.
         In any case, if m is negative, we have to put in the sign
         explicitly, since m/2==0 if m==-1.  */
      const_string sign = "";
      if (m < 0)
        {
          m *= -1;
          sign = "-";
        }
      sprintf (q, "magstep\\(%s%d.%d\\)", sign, m / 2, (m & 1) * 5);
    }  
  xputenv ("MAKETEX_MAG", q);
}

/* This mktex... program was disabled, or the script failed.  If this
   was a font creation (according to FORMAT), append CMD
   to a file missfont.log in the current directory.  */

static void
misstex P2C(kpse_file_format_type, format,  const_string, cmd)
{
  static FILE *missfont = NULL;

  /* If we weren't trying to make a font, do nothing.  Maybe should
     allow people to specify what they want recorded?  */
  if (format > kpse_any_glyph_format && format != kpse_tfm_format
      && format != kpse_vf_format)
    return;

  /* If this is the first time, have to open the log file.  But don't
     bother logging anything if they were discarding errors.  */
  if (!missfont && !kpse_make_tex_discard_errors) {
    const_string missfont_name = kpse_var_value ("MISSFONT_LOG");
    if (!missfont_name || *missfont_name == '1') {
      missfont_name = "missfont.log"; /* take default name */
    } else if (missfont_name
               && (*missfont_name == 0 || *missfont_name == '0')) {
      missfont_name = NULL; /* user requested no missfont.log */
    } /* else use user's name */

    missfont = missfont_name ? fopen (missfont_name, FOPEN_A_MODE) : NULL;
    if (!missfont && kpse_var_value ("TEXMFOUTPUT")) {
      missfont_name = concat3 (kpse_var_value ("TEXMFOUTPUT"), DIR_SEP_STRING,
                               missfont_name);
      missfont = fopen (missfont_name, FOPEN_A_MODE);
    }

    if (missfont)
      fprintf (stderr, "kpathsea: Appending font creation commands to %s.\n",
               missfont_name);
  }
  
  /* Write the command if we have a log file.  */
  if (missfont) {
    fputs (cmd, missfont);
    putc ('\n', missfont);
  }
}  


/* Assume the script outputs the filename it creates (and nothing
   else) on standard output; hence, we run the script with `popen'.  */

static string
maketex P2C(kpse_file_format_type, format,  const_string, passed_cmd)
{
  string ret;
  unsigned i;
  FILE *f;
  string cmd = xstrdup (passed_cmd);

#if defined (MSDOS) || defined (WIN32)
  /* For discarding stderr.  This is so we don't require an MSDOS user
     to istall a unixy shell (see kpse_make_tex below): they might
     devise their own ingenious ways of running mktex... even though
     they don't have such a shell.  */
  int temp_stderr = -1;
  int save_stderr = -1;
#endif

  /* If the user snuck `backquotes` or $(command) substitutions into the
     name, foil them.  */
  for (i = 0; i < strlen (cmd); i++) {
    if (cmd[i] == '`' || (cmd[i] == '$' && cmd[i+1] == '(')) {
      cmd[i] = '#';
    }
  }

  /* Tell the user we are running the script, so they have a clue as to
     what's going on if something messes up.  But if they asked to
     discard output, they probably don't want to see this, either.  */
  if (!kpse_make_tex_discard_errors) {
    fprintf (stderr, "kpathsea: Running %s\n", cmd);
  }
#if defined (MSDOS) || defined (WIN32)
  else {
    temp_stderr = open ("NUL", O_WRONLY);
    if (temp_stderr >= 0) {
      save_stderr = dup (2);
      if (save_stderr >= 0)
        dup2 (temp_stderr, 2);
    }
    /* Else they lose: the errors WILL be shown.  However, open/dup
       aren't supposed to fail in this case, it's just my paranoia. */
  }
#endif
  
  /* Run the script.  The Amiga has a different interface.  */
#ifdef AMIGA
  ret = system (cmd) == 0 ? getenv ("LAST_FONT_CREATED") : NULL;
#else /* not AMIGA */
  f = popen (cmd, FOPEN_R_MODE);

#if defined (MSDOS) || defined (WIN32)
  if (kpse_make_tex_discard_errors) {
    /* Close /dev/null and revert stderr.  */
    if (save_stderr >= 0) {
      dup2 (save_stderr, 2);
      close (save_stderr);
    }
    if (temp_stderr >= 0)
      close (temp_stderr);
  }
#endif

  if (f) {
    int c;
    string fn;             /* The final filename.  */
    unsigned len;          /* And its length.  */
    fn_type output;
    output = fn_init ();   /* Collect the script output.  */

    /* Read all the output and terminate with a null.  */
    while ((c = getc (f)) != EOF)
      fn_1grow (&output, c);
    fn_1grow (&output, 0);

    /* Maybe should check for `EXIT_SUCCESS' status before even
       looking at the output?  In some versions of Linux, pclose fails
       with ECHILD (No child processes), maybe only if we're being run
       by lpd.  So don't make this a fatal error.  */
    if (pclose (f) == -1) {
      perror ("pclose(mktexpk)");
      WARNING ("kpathsea: This is probably the Linux pclose bug; continuing");
    }

    len = FN_LENGTH (output);
    fn = FN_STRING (output);

    /* Remove trailing newlines and returns.  */
    while (len > 1 && (fn[len - 2] == '\n' || fn[len - 2] == '\r')) {
      fn[len - 2] = 0;
      len--;
    }

    /* If no output from script, return NULL.  Otherwise check
       what it output.  */
    ret = len == 1 ? NULL : kpse_readable_file (fn);
    if (!ret && len > 1) {
      WARNING1 ("kpathsea: mktexpk output `%s' instead of a filename", fn);
    }

    /* Free the name if we're not returning it.  */
    if (fn != ret)
      free (fn);
  } else {
    /* popen failed.  */
    perror ("kpathsea");
    ret = NULL;
  }
#endif /* not AMIGA */

  if (ret == NULL)
    misstex (format, cmd);
  else
    kpse_db_insert (ret);
    
  return ret;
}


/* Create BASE in FORMAT and return the generated filename, or
   return NULL.  */

string
kpse_make_tex P2C(kpse_file_format_type, format,  const_string, base)
{
  kpse_format_info_type spec; /* some compilers lack struct initialization */
  string ret = NULL;
  
  spec = kpse_format_info[format];
  if (!spec.type) { /* Not initialized yet? */
    kpse_init_format (format);
    spec = kpse_format_info[format];
  }
  
  if (spec.program && spec.program_enabled_p) {
    /* See the documentation for the envvars we're dealing with here.  */
    string args, cmd;
    const_string prog = spec.program;
    const_string arg_spec = spec.program_args;

    if (format <= kpse_any_glyph_format)
      set_maketex_mag ();

    /* Here's an awful kludge: if the mode is `/', mktexpk recognizes
       it as a special case.  `kpse_prog_init' sets it to this in the
       first place when no mode is otherwise specified; this is so
       when the user defines a resolution, they don't also have to
       specify a mode; instead, mktexpk's guesses will take over.
       They use / for the value because then when it is expanded as
       part of the PKFONTS et al. path values, we'll wind up searching
       all the pk directories.  We put $MAKETEX_MODE in the path
       values in the first place so that sites with two different
       devices with the same resolution can find the right fonts; but
       such sites are uncommon, so they shouldn't make things harder
       for everyone else.  */
    args = arg_spec ? kpse_var_expand (arg_spec) : (string) "";

    /* The command is the program name plus the arguments.  */
    cmd = concatn (prog, " ", args, " ", base, NULL);

    /* Only way to discard errors is redirect stderr inside another
       shell; otherwise, if the mktex... script doesn't exist, we
       will see the `sh: mktex...: not found' error.  No point in
       doing this if we're not actually going to run anything.  */
#if !defined(MSDOS) && !defined(WIN32) && !defined(AMIGA)
    /* We don't want to require that a Unix-like shell be installed
       on MS-DOS or WIN32 systems, so we will redirect stderr by hand
       (in maketex).  */
    if (kpse_make_tex_discard_errors) {
      string old_cmd = cmd;
#ifdef OS2
      cmd = concat3 ("cmd /c \"", cmd, "\" 2>/dev/nul");
#else
      cmd = concat3 ("sh -c \"", cmd, "\" 2>/dev/null");
#endif
      free (old_cmd);
    }
#endif
    
    ret = maketex (format, cmd);

    free (cmd);
    if (*args)
      free (args);
  }

  return ret;
}

#ifdef TEST

void
test_make_tex (kpse_file_format_type fmt, const_string base)
{
  string answer;
  
  printf ("\nAttempting %s in format %d:\n", base, fmt);

  answer = kpse_make_tex (fmt, base);
  puts (answer ? answer : "(nil)");
}


int
main ()
{
  xputenv ("KPATHSEA_DPI", "781"); /* call mktexpk */
  xputenv ("MAKETEX_BASE_DPI", "300"); /* call mktexpk */
  KPSE_MAKE_SPEC_ENABLED (kpse_make_specs[kpse_pk_format]) = true;
  test_make_tex (kpse_pk_format, "cmr10");

  /* Fail with mktextfm.  */
  KPSE_MAKE_SPEC_ENABLED (kpse_make_specs[kpse_tfm_format]) = true;
  test_make_tex (kpse_tfm_format, "foozler99");
  
  /* Call something disabled.  */
  test_make_tex (kpse_bst_format, "no-way");
  
  return 0;
}

#endif /* TEST */


/*
Local variables:
test-compile-command: "gcc -g -I. -I.. -DTEST tex-make.c kpathsea.a"
End:
*/