summaryrefslogtreecommitdiff
path: root/dviware/dvitops/main.c
blob: 5deb24c869e13ac2c544ba1398b87ce49c8169f2 (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
static char rcsid[] = "$Header: /usr/jjc/dvitops/RCS/main.c,v 1.8 90/08/14 13:10:37 jjc Rel $";

#include "dvitops.h"
 
char *texfonts = TEXFONTS;
char *texinputs = TEXINPUTS;
char *texpk = TEXPK;
int dpi = DPI;
char *page_used[MAXFONTS];
long f_memory = 200*1024L;
/* each region has a number which indexes into the `region' vector */
/* the default region is `NO_REGION', and doesn't have an entry */
 
int current_region = NO_REGION;
int nregions = 0;
int quiet = FALSE;

#ifdef PROTO
static void usage(void);
#else
static void usage();
#endif

#ifdef PROTO
static char **newargv(char **argv, int *argcp);
#else 
static char **newargv();
#endif
 
char *program_name; 

void main(argc, argv)
int argc; 
char *argv[];
{
	int c;
	int i;
	char *ptr;
#ifndef HAVE_REDIRECTION
	FILE *ofp;
#endif
	FILE *fp;
	struct option_info option;
	int exit_code;
	program_name = argv[0];
	opterr = 0; /* disable message from getopt */
	option.mag = 0; /* specified by default in dvi file */
	option.voffset = option.hoffset = 72.0; /* 1in in PostScript points */
	option.reverse = 0;
	option.maxpages = INT_MAX;
	option.copies = 1;
	option.sort = FALSE;
	option.paper = PAPER;
	if ((ptr = getenv("TEXINPUTS")) != NULL)
		texinputs = ptr;
	if ((ptr = getenv("TEXFONTS")) != NULL)
		texfonts = ptr;	
	if ((ptr = getenv("TEXPK")) != NULL)
		texpk = ptr;
	for (i = 0; i < 10; i++)
		option.start_page[i] = WILD;
	argv = newargv(argv, &argc);
	while ((c = getopt(argc, argv, "qf:y:c:m:rp:n:sh:v:d:")) != EOF)
		switch (c) {
		case 'q':
			quiet = TRUE;
			break;
		case 'p':
			option.paper = optarg;
			break;
		case 'y':
			f_memory = atol(optarg)*1024L;
			break;
		case 'c':
			option.copies = atoi(optarg);
			if (option.copies == 0)
				option.copies = 1;
			break;
		case 'm':
			option.mag = atoi(optarg);
			if (option.mag <= 0) {
				message(ERROR, "bad magnification: using 1000 instead");
				option.mag = 1000;
			}
			break;
		case 'r':
			option.reverse = !option.reverse;
			break;
		case 'f':
			ptr = optarg;
			for (i = 0; i < 10 && *ptr != '\0'; i++) {
				if (*ptr == '*') {
					option.start_page[i] = WILD;
					++ptr;
				}
				else {
					option.start_page[i] = atol(ptr);
					if (*ptr == '+' || *ptr == '-')
						++ptr;
					while (isdigit(*ptr))
						ptr++;
				}
				if (*ptr == '.')
					++ptr;
				else if (*ptr == '\0')
					break;
				else
					usage();
			}
			break;
		case 'n':
			option.maxpages = atoi(optarg);
			break;
		case 's':
			option.sort = TRUE;
			break;
		case 'h':
			if (bigpoint(optarg, (char **)NULL,&option.hoffset) == 0)
				usage();
			break;
		case 'v':
			if (bigpoint(optarg, (char **)NULL, &option.voffset) == 0)
				usage();
			break;
		case 'd':
			dpi = atoi(optarg);
			break;
		case '?':
			usage();
		default:
			cant_happen();
		}
	if (argc - optind == 1) {
#ifndef HAVE_REDIRECTION
		int len;
		char ofilename[FILENAME_MAX+1];
#endif
		if ((fp = xfopen(argv[optind], TRUE, (char *)NULL, ".dvi")) == NULL)
			message(FATAL_ERROR, "can't find %s", argv[optind]);
#ifndef HAVE_REDIRECTION
		len = strlen(argv[optind]);
		if (len >= 4 && 
#ifdef CASE_INSENSITIVE_FILENAMES
			stricmp(argv[optind] - 4, ".dvi") == 0
#else
			strcmp(argv[optind] - 4, ".dvi") == 0
#endif
			) {
			if (len < FILENAME_MAX) {
				strcpy(ofilename, argv[optind]);
				strcpy(ofilename + len - 3, "ps");
			}
			else
				message(FATAL_ERROR, "input filename `%s' too long", argv[optind]);
		}
		else if (len <= FILENAME_MAX - 3) {
			strcpy(ofilename, argv[optind]);
			strcpy(ofilename + len, ".ps");
		}
		else
			message(FATAL_ERROR, "input filename `%s' too long", argv[optind]);
		if ((ofp = fopen(ofilename, "w")) == NULL)
			message(FATAL_ERROR, "can't open `%s' for output", ofilename);
#endif
	}
#ifdef HAVE_REDIRECTION
	else if (argc == optind) {
#ifdef HAVE_ISATTY
		if (isatty(fileno(stdin)))
			message(FATAL_ERROR, "can't seek on standard input");
#endif
		fp = stdin;
	}
#endif
	else
		usage();
#ifdef HAVE_SETVBUF
	setvbuf(fp, (char *)NULL, _IOFBF, 16384); /* ignore any error */
#endif
	read_dvi_file(fp,
#ifdef HAVE_REDIRECTION
				  stdout,
#else
				  ofp, 
#endif
				  &option);
	switch (history) {
	case INFORMATION:
		exit_code = NORMAL_EXIT_CODE;
		break;
	case WARNING:
		exit_code = WARNING_EXIT_CODE;
		break;
	case ERROR:
		exit_code = ERROR_EXIT_CODE;
		break;
	case FATAL_ERROR:
		exit_code = FATAL_ERROR_EXIT_CODE;
		break;
	default:
		cant_happen();
		break;
	}
	exit(exit_code);
}
 
/* this looks for an environmental variable called DVITOPS
   and types it into the command line */

#define MAXARGS 256
 
static char **newargv(argv, argcp)
char **argv;
int *argcp;
{
	char **p;
	int i;
	int argc = 0;
	char *s, *env;
	if ((s = getenv("DVITOPS")) == NULL)
		return argv;
	if ((env = (char *)malloc(strlen(s) + 1)) == NULL)
		out_of_memory();
	if ((p = (char **)malloc(sizeof(char *)*MAXARGS)) == NULL)
		out_of_memory();
	strcpy(env, s);
	p[argc++] = argv[0];
	s = strtok(env, " \t");
	while (s != NULL && argc < MAXARGS) {
		p[argc++] = s;
		s = strtok((char *)NULL, " \t");
	}
	for (i = 1; i < *argcp && argc < MAXARGS; i++)
		p[argc++] = argv[i];
	if (argc == MAXARGS) {
		message(ERROR, "too many arguments in environment variable DVITOPS");
		return argv;
	}
	p[argc] = NULL;
	*argcp = argc;
	return p;
}
 
 
static void usage()
{
	message(FATAL_ERROR, 
#ifdef HAVE_REDIRECTION
	"command line error: usage:\n\
dvitops [-qrs] [-c ncopies] [-f firstpage] [-n npages] [-m magnification]\n\
        [-y kmem] [-h hoffset] [-v voffset] [-p papersize] [-d dpi] [dvifile]"
#else
	"command line error: usage:\n\
dvitops [-Vqrs] [-c ncopies] [-f firstpage] [-n npages] [-m magnification]\n\
        [-y kmem] [-h hoffset] [-v voffset] [-p papersize] [-d dpi] dvifile"
#endif
			);
}

/*
Local Variables:
tab-width: 4
c-indent-level: 4
c-continued-statement-offset: 4
c-brace-offset: -4
c-argdecl-indent: 0
c-label-offset: -4
End:
*/