summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/mpware/makempx.c
blob: 51350126f9b971b46c2ae60566e0b20dc5284a1d (plain)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/* $Id$

 Make an MPX file from the labels in a MetaPost source file,
 using mpto and either dvitomp (TeX) or dmp (troff).

 Started from a shell script initially based on John Hobby's original
 version, that was then translated to C by Akira Kakuto (Aug 1997, 
 Aug 2001), and updated and largely rewritten by Taco Hoekwater (Nov 2006).

 Public Domain.
 
 indent -kr -psl
*/

/* Differences between the script and this C version:

 * The script trapped HUP, INT, QUIT and TERM for cleaning up 
   temporary files. This is a refinement, and not portable.

 * The command 'newer' became a function.

 * The script put its own directory in front of the
   executable search PATH. This is not portable either, and
   it seems a safe bet that normal users do not have 'mpto', 
   'dvitomp', or 'dmp' commands in their path.  

 * The command-line '-troff' now also accepts an optional argument.

 * The troff infile for error diagnostics is renamed "mpxerr.i", 
   not plain "mpxerr".

 * The original script deleted mpx*.* in the cleanup process. 

   That is a bit harder in C, because it requires reading the contents 
   of the current directory.  The current program assumes that 
   opendir(), readdir() and closedir() are known everywhere where 
   the function getcwd() exists (except on WIN32, where it uses
   _findfirst & co).

   If this assumption is false, you can define NO_GETCWD, and makempx
   will revert to trying to delete only a few known extensions

 * There is a -debug switch, preventing the removal of tmp files
*/


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef WIN32
#include <direct.h>
#include <io.h>
#include <process.h>
#else
#include <sys/types.h>
#include <dirent.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>

#if !defined(HAVE_MKSTEMP) && ! defined (HAVE_MKTEMP)
#include "time.h"
#endif

#include <kpathsea/kpathsea.h>
#include "c-auto.h"

#define INLINE_NEWER 1

/* We may have high-res timers in struct stat.  If we do, use them.  */
#ifdef HAVE_STRUCT_STAT_ST_MTIM
#define ISNEWER(S,T) (S.st_mtim.tv_sec > T.st_mtim.tv_sec ||              \
                    (S.st_mtim.tv_sec  == T.st_mtim.tv_sec &&           \
                     S.st_mtim.tv_nsec >= T.st_mtim.tv_nsec))
#else
#define ISNEWER(S,T) (S.st_mtime >= T.st_mtime)
#endif

#ifdef WIN32
#define nuldev "nul"
#define ACCESS_MODE 4
#define DUP _dup
#define DUPP _dup2
#define uexit exit
#define GETCWD _getcwd
#else
#define nuldev "/dev/null"
#define ACCESS_MODE R_OK
#define DUP dup
#define DUPP dup2
#define uexit _exit
#define GETCWD getcwd
#endif

#define version "1.002"

#define ERRLOG "mpxerr.log"
#define TEXERR "mpxerr.tex"
#define DVIERR "mpxerr.dvi"
#define TROFF_INERR "mpxerr.i"
#define TROFF_OUTERR "mpxerr.t"
#define DMP "dmp"
#define DVITOMP "dvitomp"
#define NEWER "newer"
#define MPTO "mpto"
#define MPTOTEXOPT "-tex"
#define MPTOTROPT  "-troff"
#define TROFF   "soelim | eqn -Tps -d$$ | troff -Tps"
#define TEX     "tex"

#define LNAM 255
#define SNAM 128
#define COMMAND_PARTS 10

#define ARGUMENT_IS(a) (!strncmp(av[curarg],(a),strlen((a))))

#define TMPNAME_EXT(a,b) { strcpy(a,tmpname); strcat(a,b); }

#define RENAME(a,b) { if (debug) fprintf(stderr,"%s: renaming %s to %s\n",progname,a,b); \
                      rename(a,b); }

#define split_command(a,b) do_split_command(a,b,' ')

#define split_pipes(a,b) do_split_command(a,b,'|')

/* Define 'getcwd' for systems that do not implement it */

#ifdef NO_GETCWD
#define GETCWD(p,s)	NULL
#endif


/* Temporary files */
char mp_i[SNAM], mp_t[SNAM], mp_tmp[SNAM], mp_log[SNAM];

char *mp_tex = NULL;

int debug = 0;

char *progname;


void
mess(char *progname)
{
    fprintf(stderr, "Try `%s --help' for more information.\n", progname);
    uexit(1);
}

void 
default_erasetmp(void) 
{
    char wrk[SNAM];
    char *p;
    strcpy(wrk, mp_tex);
    p = strrchr(wrk, '.');
    *p = '\0';  strcat(wrk, ".aux");   remove(wrk);
    *p = '\0';  strcat(wrk, ".pdf");   remove(wrk);
    *p = '\0';  strcat(wrk, ".toc");   remove(wrk);
    *p = '\0';  strcat(wrk, ".idx");   remove(wrk);
    *p = '\0';  strcat(wrk, ".ent");   remove(wrk);
    *p = '\0';  strcat(wrk, ".out");   remove(wrk);
    *p = '\0';  strcat(wrk, ".nav");   remove(wrk);
    *p = '\0';  strcat(wrk, ".snm");   remove(wrk);
    *p = '\0';  strcat(wrk, ".tui");   remove(wrk);
}

void
erasetmp(void)
{
    char wrk[SNAM];
    char *p;
    char cur_path[1024];
#ifdef _WIN32
    struct _finddata_t c_file;
    long hFile;
#else
    struct dirent *entry;
    DIR *d;
#endif
    if (debug)
	return;

    if (mp_tex != NULL) {
	remove(mp_tex);
	strcpy(wrk, mp_tex);
	if ((p = strrchr(wrk, '.'))) {
	    *p = '\0';
	    /* now wrk is identical to tmpname */
	    if(GETCWD(cur_path,1020) == NULL) {
	      /* don't know where we are, back to old behaviour */
	      default_erasetmp();
	    } else {
#ifdef _WIN32
	      strcat(cur_path,"/*");
	      if ((hFile = _findfirst (cur_path, &c_file)) == -1L) {
		default_erasetmp();
	      } else {
		if (strstr(c_file.name,wrk)==c_file.name) 
		  remove(c_file.name);
		while (_findnext (hFile, &c_file) != -1L) {
		  if (strstr(c_file.name,wrk)==c_file.name) 
		    remove(c_file.name);
		}
		_findclose (hFile); /* no more entries => close directory */
	      }
#else
	      if ((d = opendir(cur_path)) == NULL) {
		default_erasetmp();
	      } else {
		while ((entry = readdir (d)) != NULL) {
		  if (strstr(entry->d_name,wrk)==entry->d_name) 
		    remove(entry->d_name);
		}
		closedir(d);
	      }
#endif	    
	    }
	}
    }
    if (mp_i[0]) {
	remove(mp_i);
    }
    if (mp_t[0]) {
	remove(mp_t);
    }
    if (mp_tmp[0]) {
	remove(mp_tmp);
    }
    if (mp_log[0]) {
	remove(mp_log);
    }
}

FILE *
makempx_xfopen(char *name, char *mode)
{
    FILE *ret;
    if (!(ret = fopen(name, mode))) {
	if (*mode == 'r') {
	    fprintf(stderr, "Cannot open %s for reading.\n", name);
	} else {
	    fprintf(stderr, "Cannot open %s for writing.\n", name);
	}
	erasetmp();
	uexit(1);
    }
    return ret;
}


int
run_command(int count, char **cmdl)
{
    int i;
    char *cmd;
    int retcode;
#ifndef WIN32
    pid_t child;
#endif
    char **options = NULL;
    /* return non-zero by default, signalling an error */
    retcode = -1;

    if (count < 1 || cmdl == NULL || cmdl[0] == NULL)
	return retcode;

    options = xmalloc(sizeof(char *) * (count + 1));
    cmd = xstrdup(cmdl[0]);

    for (i = 0; i < count; i++)
	options[i] = cmdl[i];
    options[count] = NULL;
    if (debug) {
	fprintf(stderr, "%s: running command", progname);
	for (i = 0; i < count; i++)
	    fprintf(stderr, " %s", options[i]);
	fprintf(stderr, "\n");
    }
#ifndef WIN32
    {
	child = fork();
	if (child >= 0) {
	    if (child == 0) {
		execvp(cmd, options);
	    } else
		wait(&retcode);
	}
	free(cmd);
	free(options);
    }
    return WEXITSTATUS(retcode);
#else
    retcode = spawnvp(P_WAIT, cmd, options);
    free(cmd);
    free(options);
    return retcode;
#endif
}

int
do_split_command(char *maincmd, char **cmdline, char target)
{
    char *piece;
    char *cmd;
    unsigned int i;
    int ret = 0;
    int in_string = 0;
    if (strlen(maincmd) == 0)
	return 0;
    cmd = xstrdup(maincmd);
    i = 0;
    while (cmd[i] == ' ')
	i++;
    piece = cmd;
    for (; i <= strlen(maincmd); i++) {
	if (in_string == 1) {
	    if (cmd[i] == '"') {
		in_string = 0;
	    }
	} else if (in_string == 2) {
	    if (cmd[i] == '\'') {
		in_string = 0;
	    }
	} else {
	    if (cmd[i] == '"') {
		in_string = 1;
	    } else if (cmd[i] == '\'') {
		in_string = 2;
	    } else if (cmd[i] == target) {
		cmd[i] = 0;
		if (ret == COMMAND_PARTS) {
		    fprintf(stderr,
			    "%s: Executable command too complex.\n",
			    progname);
		    mess(progname);
		}
		cmdline[ret++] = xstrdup(piece);
		while (i < strlen(maincmd) && cmd[(i + 1)] == ' ')
		    i++;
		piece = cmd + i + 1;
	    }
	}
    }
    if (*piece) {
	if (ret == COMMAND_PARTS) {
	    fprintf(stderr, "%s: Executable command too complex.\n",
		    progname);
	    mess(progname);
	}
	cmdline[ret++] = xstrdup(piece);
    }
    return ret;
}


int
do_newer(char *source, char *target)
{
#ifndef INLINE_NEWER
    char *cmdline[] = { NEWER, source, target };
    return run_command(3, cmdline);
#else
    struct stat source_stat, target_stat;

    /* check the target file */
    if (stat(target, &target_stat) < 0)
	return 0;		/* true */

    /* check the source file */
    if (stat(source, &source_stat) < 0)
	return 1;		/* false */

    if (ISNEWER(source_stat, target_stat))
	return 0;

    return 1;
#endif
}

#define MBUF 512

void
prepare_tex(char *texname, char *tmpname)
{
    int i;
    FILE *fr, *fw;
    char buffer[MBUF];
    char *tmpfile = NULL;
    char *texkeep = NULL;	/* for debugging */
    char *mptexpre = NULL;

    mptexpre = kpse_var_value("MPTEXPRE");

    if (!mptexpre)
	mptexpre = xstrdup("mptexpre.tex");

    if (!access(mptexpre, ACCESS_MODE)) {
	tmpfile = concat(tmpname, ".tmp");
	fr = makempx_xfopen(mptexpre, "r");
	fw = makempx_xfopen(tmpfile, "wb");

	while ((i = fread(buffer, 1, MBUF, fr)))
	    fwrite(buffer, 1, i, fw);
	fclose(fr);
	fr = makempx_xfopen(texname, "r");
	while ((i = fread(buffer, 1, MBUF, fr)))
	    fwrite(buffer, 1, i, fw);
	fclose(fr);
	fclose(fw);
	if (debug) {
	    texkeep = concat(texname, ".keep");
	    RENAME(texname, texkeep);
	} else {
	    remove(texname);
	}
	RENAME(tmpfile, texname);
    }
}

int
main(int ac, char **av)
{
    char **cmdline, **cmdbits;
    char whatever_to_mpx[SNAM], infile[SNAM], inerror[SNAM];
    int retcode, i, sav_o, sav_e, sav_i;
    FILE *fr, *fw, *few, *fnulr, *fnulw;

    /* TeX command name */
    char maincmd[(LNAM + 1)];

    int mpmode = 0;
    char tmpname[] = "mpXXXXXX";

    char *mpfile = NULL;
    char *mpxfile = NULL;
    char *tmpstring = NULL;

    int cmdlength = 1;
    int cmdbitlength = 1;

    int curarg = 0;

    cmdline = xmalloc(sizeof(char *) * COMMAND_PARTS);
    cmdbits = xmalloc(sizeof(char *) * COMMAND_PARTS);
    for (i = 0; i < COMMAND_PARTS; i++) {
	cmdline[i] = NULL;
	cmdbits[i] = NULL;
    }

    progname = av[0];

    /* Initialize buffer for temporary file names */
    for (i = 0; i < SNAM; i++) {
	mp_i[i] = '\0';
	mp_t[i] = '\0';
	mp_tmp[i] = '\0';
	mp_log[i] = '\0';
    }

    kpse_set_progname(progname);

    /* Set TeX command */
    if ((tmpstring = kpse_var_value("TEX")))
	strncpy(maincmd, tmpstring, LNAM);
    else if ((tmpstring = kpse_var_value("MPXMAINCMD")))
	strncpy(maincmd, tmpstring, LNAM);
    else
	strncpy(maincmd, TEX, LNAM);
    if (tmpstring)
	free(tmpstring);

    strncat(maincmd, " --parse-first-line --interaction=nonstopmode",
	    (LNAM - strlen(maincmd)));

    while (curarg < (ac - 1)) {

	curarg++;

	if (ARGUMENT_IS("-help") || ARGUMENT_IS("--help")) {

	    fprintf(stderr,
		    "Usage: %s [-tex[=<program>]|-troff[=<program>]] MPFILE MPXFILE.\n",
		    av[0]);
	    fputs
		("  If MPXFILE is older than MPFILE, translate the labels from the MetaPost\n"
		 "  input file MPFILE to low-level commands in MPXFILE, by running\n"
		 "    " MPTO " " MPTOTEXOPT ", " TEX ", and " DVITOMP "\n"
		 "  by default; or, if -troff is specified,\n" "    " MPTO
		 " " MPTOTROPT ", " TROFF ", and " DMP ".\n\n"
		 "  The current directory is used for writing temporary files.  Errors are\n"
		 "  left in mpxerr.{tex,log,dvi}.\n\n"
		 "  If the file named in $MPTEXPRE (mptexpre.tex by default) exists, it is\n"
		 "  prepended to the output in tex mode.\n\n"
		 "Email bug reports to metapost@tug.org.\n", stderr);
	    uexit(0);

	} else if (ARGUMENT_IS("-version") || ARGUMENT_IS("--version")) {

	    fprintf(stdout, "%s %s\n", progname, version);
	    fputs
		("There is NO warranty. This program is in the public domain.\n"
		 "Original author: John Hobby.\n"
		 "Current maintainer: Taco Hoekwater.\n", stdout);
	    uexit(0);

	} else if (ARGUMENT_IS("-debug") || ARGUMENT_IS("--debug")) {

	    debug = 1;

	} else if (ARGUMENT_IS("-troff") || ARGUMENT_IS("--troff")) {

	    mpmode = 1;

	    if (ARGUMENT_IS("-troff=") || ARGUMENT_IS("--troff=")) {
		i = 7;
		if (*(av[curarg] + i) == '=')
		    i++;
		if (*(av[curarg] + i) == '\'' || *(av[curarg] + i) == '\"') {
		    strncpy(maincmd, av[curarg] + i + 1, LNAM);
		    *(maincmd + strlen(maincmd)) = 0;
		} else {
		    strncpy(maincmd, av[curarg] + i, LNAM);
		}
	    } else {
		strncpy(maincmd, TROFF, LNAM);
	    }

	} else if (ARGUMENT_IS("-tex") || ARGUMENT_IS("--tex")) {

	    mpmode = 0;

	    if (ARGUMENT_IS("-tex=") || ARGUMENT_IS("--tex=")) {
		i = 5;
		if (*(av[curarg] + i) == '=')
		    i++;
		if (*(av[curarg] + i) == '\'' || *(av[curarg] + i) == '\"') {
		    strncpy(maincmd, av[curarg] + i + 1, LNAM);
		    *(maincmd + strlen(maincmd)) = 0;
		} else {
		    strncpy(maincmd, av[curarg] + i, LNAM);
		}
	    }

	} else if (ARGUMENT_IS("-")) {
	    fprintf(stderr, "%s: Invalid option: %s.\n", progname,
		    av[curarg]);
	    mess(progname);
	} else {
	    if (mpfile == NULL) {

		if (strchr(av[curarg], ' ') && (av[curarg][0] != '\"'))
		    mpfile = concat3("\"", av[curarg], "\"");
		else
		    mpfile = strdup(av[curarg]);

	    } else if (mpxfile == NULL) {

		if (strchr(av[curarg], ' ') && (av[curarg][0] != '\"'))
		    mpxfile = concat3("\"", av[curarg], "\"");
		else
		    mpxfile = strdup(av[curarg]);

	    } else {
		fprintf(stderr, "%s: Extra argument %s.\n", progname,
			av[curarg]);
		mess(progname);
	    }
	}
    }

    if (mpfile == NULL || mpxfile == NULL) {
	fprintf(stderr, "%s: Need exactly two file arguments.\n",
		progname);
	mess(progname);
    }

    /* Check if mpfile is newer than mpxfile */

    retcode = do_newer(mpfile, mpxfile);

    /* If MPX file is up-to-date or if MP file does not exist, do nothing. */
    if (retcode)
	uexit(0);

#ifdef HAVE_MKSTEMP
    i = mkstemp(tmpname);
    if (i == -1)
	uexit(1);
    close(i);
    remove(tmpname);
#else
#ifdef HAVE_MKTEMP
    tmpstring = mktemp(tmpname);
    if ((tmpstring == NULL) || strlen(tmpname) == 0)
	uexit(1);
    /* this should not really be needed, but better
       safe than sorry. */
    if (tmpstring != tmpname) {
	i = strlen(tmpstring);
	if (i > 8) i = 8;
	strncpy(tmpname, tmpstring, i);
    }
#else
    sprintf(tmpname, "mp%06d", (time(NULL) % 1000000));
#endif
#endif

    mp_tex = concat(tmpname, ".tex");

    /* step 1: */

    fw = makempx_xfopen(mp_tex, "wb");
    few = makempx_xfopen(ERRLOG, "wb");

    cmdline[0] = MPTO;
    cmdline[1] = (mpmode == 0 ? MPTOTEXOPT : MPTOTROPT);
    cmdline[2] = mpfile;

    sav_o = DUP(fileno(stdout));
    DUPP(fileno(fw), fileno(stdout));
    sav_e = DUP(fileno(stderr));
    DUPP(fileno(few), fileno(stderr));

    retcode = run_command(3, cmdline);

    DUPP(sav_o, fileno(stdout));
    close(sav_o);
    fclose(fw);

    DUPP(sav_e, fileno(stderr));
    close(sav_e);
    fclose(few);

    if (retcode) {
	fprintf(stderr, "%s: Command failed: %s %s %s\n",
		progname, cmdline[0], cmdline[1], cmdline[2]);
	erasetmp();
	uexit(1);
    }

    /* step 2: */

    if (mpmode == 0) {		/* TeX mode */

	prepare_tex(mp_tex, tmpname);	/* check for mptexpre.tex */

	if (strlen(maincmd) > (LNAM - strlen(mp_tex) - 1)) {
	    fprintf(stderr, "%s: Command too complex: %s\n", progname,
		    maincmd);
	    erasetmp();
	    uexit(1);
	}
	strcat(maincmd, " ");
	strcat(maincmd, mp_tex);
	cmdlength = split_command(maincmd, cmdline);

	fnulr = makempx_xfopen(nuldev, "r");
	sav_i = DUP(fileno(stdin));
	DUPP(fileno(fnulr), fileno(stdin));

	fnulw = makempx_xfopen(nuldev, "w");
	sav_o = DUP(fileno(stdout));
	DUPP(fileno(fnulw), fileno(stdout));

	retcode = run_command(cmdlength, cmdline);

	DUPP(sav_i, fileno(stdin));
	close(sav_i);
	fclose(fnulr);
	DUPP(sav_o, fileno(stdout));
	close(sav_o);
	fclose(fnulw);

	TMPNAME_EXT(mp_log, ".log");

	if (!retcode) {
	    strcpy(whatever_to_mpx, DVITOMP);
	    TMPNAME_EXT(infile, ".dvi");
	    strcpy(inerror, DVIERR);
	} else {
	    RENAME(mp_tex, TEXERR);
	    RENAME(mp_log, ERRLOG);
	    fprintf(stderr, "%s: Command failed:", progname);
	    for (i = 0; i < (cmdlength - 1); i++)
		fprintf(stderr, " %s", cmdline[i]);
	    fprintf(stderr, " %s", TEXERR);
	    fprintf(stderr, "; see mpxerr.log\n");
	    erasetmp();
	    uexit(2);
	}
    } else if (mpmode == 1) {	/* troff mode */

	TMPNAME_EXT(mp_i, ".i");
	RENAME(mp_tex, mp_i);
	TMPNAME_EXT(mp_t, ".t");
	fr = makempx_xfopen(mp_i, "r");
	TMPNAME_EXT(mp_tmp, ".tmp");
	fw = makempx_xfopen(mp_tmp, "wb");

	/* split the command in bits */
	cmdbitlength = split_pipes(maincmd, cmdbits);

	for (i = 0; i < cmdbitlength; i++) {
	    cmdlength = split_command(cmdbits[i], cmdline);

	    sav_i = DUP(fileno(stdin));
	    sav_o = DUP(fileno(stdout));
	    DUPP(fileno(fr), fileno(stdin));
	    DUPP(fileno(fw), fileno(stdout));

	    retcode = run_command(cmdlength, cmdline);

	    DUPP(sav_i, fileno(stdin));
	    close(sav_i);
	    DUPP(sav_o, fileno(stdout));
	    close(sav_o);
	    fclose(fr);
	    fclose(fw);
	    if (retcode) {
		RENAME(mp_i, TROFF_INERR);

		fprintf(stderr, "%s: Command failed:", progname);
		for (i = 0; i < cmdlength; i++) {
		    if (strcmp(cmdline[i], mp_i) == 0)
			fprintf(stderr, " %s", TROFF_INERR);
		    else
			fprintf(stderr, " %s", cmdline[i]);
		}
		fprintf(stderr, "\n");
		erasetmp();
		uexit(2);
	    }
	    if (i < cmdbitlength - 1) {
		if (i % 2 == 0) {
		    fr = makempx_xfopen(mp_tmp, "r");
		    fw = makempx_xfopen(mp_t, "wb");
		    strcpy(infile, mp_t);
		} else {
		    fr = makempx_xfopen(mp_t, "r");
		    fw = makempx_xfopen(mp_tmp, "wb");
		    strcpy(infile, mp_tmp);
		}
	    }
	}
	strcpy(whatever_to_mpx, DMP);
	strcpy(inerror, TROFF_OUTERR);
    }

    /* Step 3: */

    fw = makempx_xfopen(ERRLOG, "wb");

    sav_o = DUP(fileno(stdout));
    DUPP(fileno(fw), fileno(stdout));

    cmdline[0] = whatever_to_mpx;
    cmdline[1] = infile;
    cmdline[2] = mpxfile;
    retcode = run_command(3, cmdline);

    DUPP(sav_o, fileno(stdout));
    close(sav_o);
    fclose(fw);

    if (retcode) {
	RENAME(infile, inerror);
	if (mpmode == 1)
	    RENAME(mp_i, TROFF_INERR);
	if (!debug)
	    remove(mpxfile);
	fprintf(stderr, "%s: Command failed: %s %s %s\n",
		progname, whatever_to_mpx, inerror, mpxfile);
	erasetmp();
	uexit(3);
    }
    if (!debug) {
	remove(ERRLOG);
	remove(infile);
    }
    erasetmp();
    uexit(0);
}