1 /*
2  *	POLS (Print Out program for Laser Shot)
3  *
4  *			by T.Shirai
5  */
6 
7 #include "lipsf.h"
8 #include "term.h"
9 #include "var.h"
10 #include "kanji.h"
11 #include "pversion.h"
12 #include <time.h>
13 
14 #if	MSDOS
15 #include <dos.h>
16 extern char *strrdelim __P_((char *));
17 #else
18 #define	strrdelim(s)	strrchr(s, _SC_)
19 #endif
20 
21 static char *stylefile = NULL;
22 char *printfile = NULL;
23 FILE *infile;
24 FILE *null;
25 int pagesum;
26 
27 extern int line;
28 extern int step;
29 extern int page;
30 extern u_char *styletitle;
31 extern int kanjicode;
32 
33 VOID error __P_((int));
34 VOID usage __P_((int));
35 VOID title __P_((VOID_A));
36 static VOID showfilename __P_((char *, char *));
37 static VOID savestring __P_((FILE *, char *));
38 static VOID savestyle __P_((char *));
39 static VOID doprintout __P_((char *, FILE *));
40 static VOID mainloop __P_((VOID_A));
41 int main __P_((int, char *[]));
42 
43 extern int warning __P_((int, char *));
44 extern VOID varinit __P_((VOID_A));
45 extern VOID argfile __P_((char *));
46 extern int menu __P_((int));
47 extern int yesno __P_((char *));
48 extern VOID changestyle __P_((VOID_A));
49 extern VOID changeheadfoot __P_((VOID_A));
50 extern VOID changeoption __P_((VOID_A));
51 extern FILE *selectprint __P_((VOID_A));
52 extern VOID loadstyle __P_((char *));
53 extern char *getfilename __P_((char *, char *, int));
54 
55 extern VOID getfileinfo __P_((char *, FILE *));
56 extern FILE *lpropen __P_((VOID_A));
57 extern VOID frontprocess __P_((FILE *));
58 extern VOID backprocess __P_((FILE *));
59 extern VOID printout __P_((FILE *));
60 #if	MSDOS
61 extern VOID initdir __P_((int));
62 extern VOID far fatalerr __P_((VOID_A));
63 extern VOID setfatalorg __P_((VOID_A));
64 #endif
65 
66 
error(no)67 VOID error(no)
68 int no;
69 {
70 	char *cp;
71 
72 	if (no < 0) {
73 #if	MSDOS
74 		initdir(0);
75 #endif
76 		stdiomode();
77 		endterm();
78 		perror("\007\n");
79 		inittty(1);
80 		exit(127);
81 	}
82 
83 	cp = strrdelim(stylefile);
84 	if (cp) cp++;
85 	else cp = stylefile;
86 	warning(no, cp);
87 }
88 
89 /*ARGSUSED*/
usage(no)90 VOID usage(no)
91 int no;
92 {
93 	error(EINVAL);
94 }
95 
title(VOID_A)96 VOID title(VOID_A)
97 {
98 	putterm(t_clear);
99 	putterm(t_standout);
100 	cputs2("  POLS(PrintOut for LaserShot)   ");
101 	cprintf2("Version%-17.17s", strchr(version, ' '));
102 	cputs2("(c)1994-2002 T.Shirai  ");
103 	putterm(end_standout);
104 }
105 
showfilename(prompt,file)106 static VOID showfilename(prompt, file)
107 char *prompt, *file;
108 {
109 	int ch;
110 
111 	ch = '\0';
112 	if (!file) file = "(none)";
113 	else if (strlen(file) > 61) {
114 		ch = file[61];
115 		file[61] = '\0';
116 	}
117 	cputs2("  ");
118 	putterm(t_standout);
119 	cprintf2("%s:", prompt);
120 	putterm(end_standout);
121 	cputs2(file);
122 	if (ch) file[61] = ch;
123 }
124 
savestring(fp,str)125 static VOID savestring(fp, str)
126 FILE *fp;
127 char *str;
128 {
129 	int i, len;
130 
131 	len = strlen(str);
132 	for (i = 0; i < len; i++) {
133 		if (str[i] == '\\') fprintf(fp, "\\\\");
134 		else fputc(str[i], fp);
135 	}
136 }
137 
savestyle(file)138 static VOID savestyle(file)
139 char *file;
140 {
141 	FILE *fp;
142 
143 	if (!(fp = fopen(file, "w"))) {
144 		warning(-1, file);
145 		return;
146 	}
147 
148 	if (styletitle) fprintf(fp, ";%s\n", styletitle);
149 
150 	if (papersize != PAPERSIZE) {
151 		fprintf(fp, "-p ");
152 		switch (papersize) {
153 			case 12:
154 				fprintf(fp, "A3");
155 				break;
156 			case 14:
157 				fprintf(fp, "A4");
158 				break;
159 			case 16:
160 				fprintf(fp, "A5");
161 				break;
162 			case 18:
163 				fprintf(fp, "Hagaki");
164 				break;
165 			case 24:
166 				fprintf(fp, "B4");
167 				break;
168 			case 26:
169 				fprintf(fp, "B5");
170 				break;
171 			default:
172 				fprintf(fp, "%dx%d", paperwidth, paperheight);
173 				break;
174 		}
175 		fputc('\n', fp);
176 	}
177 
178 	if (cassette != CASSETTE) {
179 		fprintf(fp, "-m ");
180 		switch (cassette) {
181 			case 1:
182 				fprintf(fp, "Manual");
183 				break;
184 			case 2:
185 				fprintf(fp, "Lower");
186 				break;
187 			case 3:
188 				fprintf(fp, "Upper");
189 				break;
190 			default:
191 				fprintf(fp, "Auto");
192 				break;
193 		}
194 		fputc('\n', fp);
195 	}
196 
197 	if (islandscape != ISLANDSCAPE) {
198 		fprintf(fp, "-d ");
199 		if (islandscape) fprintf(fp, "Landscape");
200 		else fprintf(fp, "Portrait");
201 		fputc('\n', fp);
202 	}
203 
204 	if (fontpattern != FONTPATTERN) {
205 		fprintf(fp, "-f ");
206 		switch (fontpattern) {
207 			case 81:
208 				fprintf(fp, "Gothic");
209 				break;
210 			case 82:
211 				fprintf(fp, "Round-Gothic");
212 				break;
213 			default:
214 				fprintf(fp, "Mincho");
215 				break;
216 		}
217 		fputc('\n', fp);
218 	}
219 
220 	if (kanjicode != DEF_KCODE) {
221 		if (kanjicode == SJIS) fprintf(fp, "-J sjis\n");
222 		else if (kanjicode == EUC) fprintf(fp, "-J euc\n");
223 	}
224 
225 	if (usrlettersize != AUTOVALUE) fprintf(fp, "-s %d\n", usrlettersize);
226 	if (usrlineheight != AUTOVALUE) fprintf(fp, "-h %d\n", usrlineheight);
227 	if (usrcolumnwidth != AUTOVALUE)
228 		fprintf(fp, "-w %d\n", usrcolumnwidth);
229 	if (controlmode) {
230 		if (controlchar == '\\') fprintf(fp, "-C \\\\\n");
231 		else fprintf(fp, "-C %c\n", controlchar);
232 	}
233 	if (headerstr && headermargin != HEADERMARGIN)
234 		fprintf(fp, "-A H%d\n", headermargin);
235 	if (footerstr && footermargin != FOOTERMARGIN)
236 		fprintf(fp, "-A F%d\n", footermargin);
237 	if (iszerosup != ISZEROSUP) fprintf(fp, "-A z\n");
238 	if (iskanjinum != ISKANJINUM) fprintf(fp, "-A k\n");
239 	if (istoupper != ISTOUPPER) fprintf(fp, "-A U\n");
240 	if (istolower != ISTOLOWER) fprintf(fp, "-A L\n");
241 	if (tabposition != TABPOSITION) fprintf(fp, "-t %d\n", tabposition);
242 	if (usrmaxcolumns != AUTOVALUE) fprintf(fp, "-c %d\n", usrmaxcolumns);
243 	if (usrmaxlines != AUTOVALUE) fprintf(fp, "-l %d\n", usrmaxlines);
244 	if (topmargin != TOPMARGIN) fprintf(fp, "-T %d\n", topmargin);
245 	if (bottommargin != AUTOVALUE) fprintf(fp, "-B %d\n", bottommargin);
246 	if (leftmargin != LEFTMARGIN) fprintf(fp, "-L %d\n", leftmargin);
247 	if (rightmargin != AUTOVALUE) fprintf(fp, "-R %d\n", rightmargin);
248 	if (headerstr) {
249 		fprintf(fp, "-H \"");
250 		savestring(fp, headerstr);
251 		fprintf(fp, "\"\n");
252 	}
253 	if (footerstr) {
254 		fprintf(fp, "-F \"");
255 		savestring(fp, footerstr);
256 		fprintf(fp, "\"\n");
257 	}
258 	if (overlaymode != OVERLAYMODE) fprintf(fp, "-O %d\n", overlaymode);
259 
260 	if ((maxsteps > 1 && maxsteps != MAXSTEPS)
261 	|| (maxsteps == 1 && MAXSTEPS > 1)) {
262 		fprintf(fp, "-S %d", maxsteps);
263 		if (stepcolumns != AUTOVALUE) fprintf(fp, ",%d", stepcolumns);
264 		fputc('\n', fp);
265 	}
266 	if (isvertical != ISVERTICAL) fprintf(fp, "-v\n");
267 	if (isnewjis != ISNEWJIS) fprintf(fp, "-j\n");
268 	if (isroman != ISROMAN) fprintf(fp, "-a\n");
269 	if (isconcat != ISCONCAT) fprintf(fp, "-k\n");
270 	if (iskinsoku) fprintf(fp, "-K\n");
271 	if (lipsmode != LIPSMODE) fprintf(fp, "-%1d\n", lipsmode);
272 
273 	if (finemode != FINEMODE) fprintf(fp, "-X f\n");
274 	if (doubleside != DOUBLESIDE) fprintf(fp, "-X d\n");
275 	if (colormode != COLORMODE) {
276 		fprintf(fp, "-X c");
277 		if (defcolor < 0);
278 		else if (defcolor < 0x01000000L) fprintf(fp, "%d", defcolor);
279 		else if (defcolor & 0x02000000L)
280 			fprintf(fp, "%%%d", defcolor & 0xff);
281 		else fprintf(fp, "#%06x", defcolor & 0xffffffL);
282 		fputc('\n', fp);
283 	}
284 
285 	fclose(fp);
286 }
287 
doprintout(file,fp)288 static VOID doprintout(file, fp)
289 char *file;
290 FILE *fp;
291 {
292 	FILE *outfile;
293 
294 	if (!file || !fp) return;
295 
296 	if (!(infile = fopen(file, "r"))) {
297 		warning(-1, file);
298 		return;
299 	}
300 
301 	if (!(null = fopen(NULLDEVICE, "w"))) error(-1);
302 
303 	if (isinfo) outfile = null;
304 	else if (fp != stdout) outfile = fp;
305 	else outfile = lpropen();
306 
307 	tzset();
308 	frontprocess(outfile);
309 	getfileinfo(file, infile);
310 
311 	if ((headerstr && strstr(headerstr, "%P"))
312 	|| (footerstr && strstr(footerstr, "%P"))) {
313 		printout(null);
314 		if (fseek(infile, 0L, 0)) error(-1);
315 		pagesum = page;
316 	}
317 
318 	printout(outfile);
319 	fclose(infile);
320 	if (isinfo) {
321 		if (line == 0) {
322 			line = maxlines;
323 			if (--step < 0) {
324 				step = maxsteps - 1;
325 				if (--page < 0) page = step = line = 0;
326 			}
327 		}
328 		locate(2, 13);
329 		cprintf2("%-24.24s: %4d %s\n", PAGSM_K, page + 1, PAGES_K);
330 		locate(2, 14);
331 		cprintf2("%-24.24s: %4d %s\n", STPSM_K, step + 1, STEPS_K);
332 		locate(2, 15);
333 		cprintf2("%-24.24s: %4d %s\n", LINSM_K, line, LINES_K);
334 		yesno(CNFOK_K);
335 	}
336 	backprocess(outfile);
337 
338 	if (!isinfo) {
339 		if (fp != stdout) fclose(outfile);
340 #if	!MSDOS
341 		else if (pclose(outfile)) warning(EPIPE, NULL);
342 #endif
343 	}
344 	fclose(null);
345 }
346 
mainloop(VOID_A)347 static VOID mainloop(VOID_A)
348 {
349 	int no;
350 	char *name;
351 
352 	no = 0;
353 
354 	for (;;) {
355 		title();
356 		locate(0, 1);
357 		showfilename(FILEN_K, printfile);
358 		locate(0, 2);
359 		showfilename(STYF_K, stylefile);
360 
361 		if (stylefile || stylefile) {
362 			locate(0, 4);
363 			showfilename(STYN_K, styletitle);
364 		}
365 
366 		switch (no = menu(no)) {
367 			case 0:
368 				name = getfilename(printfile, NULL, 0);
369 				if (name) {
370 					if (printfile) free(printfile);
371 					printfile = name;
372 				}
373 				break;
374 			case 1:
375 				locate(0, 4);
376 				putterm(l_clear);
377 				changeheadfoot();
378 				break;
379 			case 2:
380 				name = getfilename(stylefile, POLSEXT, 0);
381 				if (name) {
382 					if (stylefile) free(stylefile);
383 					stylefile = name;
384 					loadstyle(name);
385 				}
386 				break;
387 			case 3:
388 				locate(0, 4);
389 				putterm(l_clear);
390 				doprintout(printfile, selectprint());
391 				break;
392 			case 4:
393 				locate(0, 4);
394 				putterm(l_clear);
395 				changestyle();
396 				break;
397 			case 5:
398 				locate(0, 4);
399 				putterm(l_clear);
400 				changeoption();
401 				break;
402 			case 6:
403 				name = getfilename(stylefile, POLSEXT, 1);
404 				if (name) {
405 					if (!yesno(REGOK_K)) free(name);
406 					else {
407 						if (stylefile) free(stylefile);
408 						stylefile = name;
409 						savestyle(name);
410 					}
411 				}
412 				break;
413 			case 7:
414 				if (yesno(OVROK_K)) return;
415 				break;
416 			default:
417 				break;
418 		}
419 	}
420 }
421 
main(argc,argv)422 int main(argc, argv)
423 int argc;
424 char *argv[];
425 {
426 	char *path;
427 #if	MSDOS
428 	int drv;
429 #endif
430 
431 	inittty(0);
432 	getterment();
433 	ttyiomode();
434 	initterm();
435 	getwsize(80, 24);
436 
437 	path = (argc > 1) ? argv[1] : NULL;
438 
439 #if	MSDOS
440 	initdir(1);
441 	setfatalorg();
442 	_dos_setvect(0x24, (VOID (far *)(VOID_A))fatalerr);
443 
444 	if (path && isalpha(drv = path[0]) && path[1] == ':') {
445 		path += 2;
446 		if (drv >= 'a' && drv <= 'z') drv += 'A' - 'a';
447 		drv -= 'A' - 1;
448 		_dos_setdrive(drv, &drv);
449 	}
450 #endif
451 	if (path && chdir(path) < 0 && access(path, 0) >= 0) {
452 		printfile = (char *)malloc(strlen(path) + 1);
453 		strcpy(printfile, path);
454 	}
455 
456 	varinit();
457 	mainloop();
458 
459 	locate(0, n_line);
460 #if	MSDOS
461 	initdir(0);
462 #endif
463 	exit2(0);
464 	return(0);
465 }
466