File: texfix.c

package info (click to toggle)
dvi2ps 5.1j-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,428 kB
  • sloc: ansic: 15,077; sh: 10,353; makefile: 158
file content (132 lines) | stat: -rw-r--r-- 2,528 bytes parent folder | download | duplicates (7)
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
/*
 *	texfix.c
 *
 *	version 1.5 (September 25, 1992)
 *	written by Kazuhiro Kazama (kazama@square.ntt.jp)
 *
 *	This original program is "retexps" made by takeba@otsl.oki.co.jp.
 */
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<signal.h>
#include	<unistd.h>
char	*mktemp();

#ifdef	STRSTR
char	*strstr();
#endif

#define	PAGECOM	"%%Page:"
#define	BSP	"@beginspecial\n"
#define	ESP	"@endspecial\n"

#if defined(JDVI2KPS) || defined(JDVI2KPS2)
#define	BOP	"@bop0\n"
#define	EOP	"@eop\n"
#else
#ifdef JDVI2KPS3
#define	BOP	"@bop\n"
#define	EOP	"@eop\n"
#else
#define	BOP	"BP\n"
#define	EOP	"EP\n"
#endif
#endif

char	line[BUFSIZ], line2[BUFSIZ];
char	*tmpfn;
int	cleanup();

main()
{
    signal(SIGHUP, cleanup);
    signal(SIGINT, cleanup);
    signal(SIGQUIT, cleanup);
    signal(SIGPIPE, cleanup);
    signal(SIGTERM, cleanup);
    while (fgets(line, BUFSIZ, stdin) != NULL) {
	fputs(line, stdout);
	if (strncmp(line, PAGECOM, sizeof(PAGECOM) - 1) == 0)
	    if (pageout()) {
		fprintf(stderr, "unexpected EOF\n");
		unlink(tmpfn);
		exit(1);
	    }
    }
    unlink(tmpfn);
    exit(0);
}

pageout()
{
    FILE	*tmpfp;
    char	tmpstr[14];

    while (fgets(line, BUFSIZ, stdin) != NULL) {
	fputs(line, stdout);
	if (strcmp(line, BOP) == 0) {
	    strcpy(tmpstr, "/tmp/tfXXXXXX");
	    if ((tmpfp = fdopen(mkstemp(tmpstr), "w")) == NULL) {
		perror("fopen failed.");
		exit(1);
	    }
	    while (fgets(line, BUFSIZ, stdin) != NULL)
		if (strcmp(line, EOP) == 0) {
		    fclose(tmpfp);
		    if ((tmpfp = fopen(tmpstr, "r")) == NULL) {
			perror("fopen failed.");
			exit(1);
		    }
		    while (fgets(line2, BUFSIZ, tmpfp) != NULL)
			fputs(line2, stdout);
		    fclose(tmpfp);
		    fputs(line, stdout);
		    return 0;
		} else if (strstr(line, BSP) != NULL) {
		    fputs(line, stdout);
		    if (specialout()) {
			fclose(tmpfp);
			return 1;
		    }
		} else
		    fputs(line, tmpfp);
	}
    }
    return 0;
}

specialout()
{
    int	inspecial = 1;

    while (fgets(line, BUFSIZ, stdin) != NULL) {
	fputs(line, stdout);
	if (strcmp(line, ESP) == 0 && --inspecial == 0)
	    return 0;
	else if (strstr(line, BSP) != NULL)
	    inspecial++;
    }
    return 1;
}

cleanup()
{
    unlink(tmpfn);
    exit(0);
}

#ifdef	STRSTR
char	*strstr(s1, s2)
register char	*s1, *s2;
{
    register int	l1, l2;
    register char	c2;

    if ((c2 = *s2) != '\0')
	for (l1 = strlen(s1), l2 = strlen(++s2); l1 > l2; l1--)
	    if (*s1++ == c2 && strncmp(s1, s2, l2) == 0)
		return --s1;
    return NULL;
}
#endif