Debian Codegraph @

texk/dvipsk/dospecial.c

raw
1 /* $Id: dospecial.c 57073 2020-12-05 05:22:50Z takuji $
2 * This routine handles special commands;
3 * predospecial() is for the prescan, dospecial() for the real thing.
4 */
5 #include "dvips.h" /* The copyright notice in that file is included too! */
6
7 #ifdef KPATHSEA
8 #include <kpathsea/c-ctype.h>
9 #include <kpathsea/tex-hush.h>
10 #else /* ! KPATHSEA */
11 #include <ctype.h>
12 #include <stdlib.h>
13 #ifndef WIN32
14 extern int atoi();
15 extern int system();
16 #endif /* WIN32*/
17 #endif
18 /*
19 * The external declarations:
20 */
21 #include "protos.h"
22
23 static int specialerrors = 20;
24
25 struct bangspecial {
26 struct bangspecial *next;
27 char *actualstuff;
28 } *bangspecials = NULL;
29
30 void
31 specerror(const char *s)
32 {
33 if (specialerrors > 0
34 #ifdef KPATHSEA
35 && !kpse_tex_hush ("special")
36 #endif
37 ) {
38 error(s);
39 specialerrors--;
40 } else if (specialerrors == 0
41 #ifdef KPATHSEA
42 && !kpse_tex_hush ("special")
43 #endif
44 ) {
45 error("more errors in special, being ignored . . .");
46 error("(perhaps dvips doesn't support your macro package?)");
47 specialerrors--;
48 }
49 }
50
51 static void
52 outputstring(register char *p)
53 {
54 putc('\n', bitfile);
55 while(*p) {
56 putc(*p, bitfile);
57 p++;
58 }
59 putc('\n', bitfile);
60 }
61
62 static void
63 trytobreakout(register char *p)
64 {
65 register int i;
66 register int instring = 0;
67 int lastc = 0;
68
69 i = 0;
70 putc('\n', bitfile);
71
72 if(*p == '%') {
73 while(*p) {
74 putc(*p, bitfile);
75 p++;
76 }
77 putc('\n', bitfile);
78 return;
79 }
80
81 while (*p) {
82 if (i > 65 && *p == ' ' && instring == 0) {
83 putc('\n', bitfile);
84 i = 0;
85 } else {
86 putc(*p, bitfile);
87 i++;
88 }
89 if (*p == '(' && lastc != '\\')
90 instring = 1;
91 else if (*p == ')' && lastc != '\\')
92 instring = 0;
93 lastc = *p;
94 p++;
95 }
96 putc('\n', bitfile);
97 }
98
99 static void
100 dobs(register struct bangspecial *q)
101 {
102 if (q) {
103 dobs(q->next);
104 trytobreakout(q->actualstuff);
105 }
106 }
107
108 /* added for dvi2ps & jdvi2kps format */
109 static char *mfgets(char *buf, unsigned int bytes, FILE *fp);
110
111 static void
112 fgetboundingbox(char *f, float *llx_p, float *lly_p, float *urx_p, float *ury_p)
113 {
114 FILE *fp;
115 char buf[BUFSIZ+1];
116
117 fp = search(figpath, f, READ);
118 if (fp == 0)
119 fp = search(headerpath, f, READ);
120 if (fp) {
121 while (mfgets(buf, BUFSIZ, fp) != 0) {
122 if (buf[0] == '%' && buf[1] == '%'
123 && strncmp(buf+2, "BoundingBox:", 12) == 0) {
124 if (sscanf(buf+14, "%f %f %f %f", llx_p, lly_p, urx_p, ury_p) == 4) {
125 fclose(fp);
126 return;
127 }
128 }
129 }
130 fclose(fp);
131 }
132 sprintf(errbuf, "Couldn't get BoundingBox of %.500s: assuming full A4 size", f);
133 specerror(errbuf);
134 *llx_p = 0.0;
135 *lly_p = 0.0;
136 *urx_p = 595.0;
137 *ury_p = 842.0;
138 }
139
140 static char *
141 mfgets(char *buf, unsigned int bytes, FILE *fp)
142 {
143 int i, cc;
144
145 for (i = 0; i < bytes; i++) {
146 cc = fgetc(fp);
147 if (cc == 0x0a || cc == 0x0d) {
148 if (cc == 0x0d) {
149 cc = fgetc(fp);
150 if (cc != 0x0a) {
151 ungetc(cc, fp);
152 }
153 }
154 cc = 0x0a;
155 buf[i] = cc;
156 buf[i+1] = '\0';
157 return buf;
158 } else if (cc == EOF) {
159 buf[i] = '\0';
160 if (i == 0) return NULL;
161 else return buf;
162 } else {
163 buf[i] = cc;
164 }
165 }
166 buf[i] = '\0';
167 return buf;
168 }
169
170 static void
171 floatroundout(float f)
172 {
173 integer i;
174 i = (integer)(f<0 ? f-0.5 : f+0.5);
175 if (i-f < 0.001 && i-f > -0.001) {
176 numout((integer)i);
177 } else {
178 floatout(f);
179 }
180 }
181 /* end of addition */
182
183 void
184 outbangspecials(void) {
185 if (bangspecials) {
186 cmdout("TeXDict");
187 cmdout("begin");
188 cmdout("@defspecial\n");
189 dobs(bangspecials);
190 cmdout("\n@fedspecial");
191 cmdout("end");
192 }
193 }
194
195 /* We recommend that new specials be handled by the following general
196 * (and extensible) scheme, in which the user specifies one or more
197 * `key=value' pairs separated by spaces.
198 * The known keys are given in KeyTab; they take values
199 * of one of the following types:
200 *
201 * None: no value, just a keyword (in which case the = sign is omitted)
202 * String: the value should be "<string without double-quotes"
203 * or '<string without single-quotes'
204 * Integer: the value should be a decimal integer (%d format)
205 * Number: the value should be a decimal integer or real (%f format)
206 * Dimension: like Number, but will be multiplied by the scaledsize
207 * of the current font and converted to default PostScript units
208 * (Actually, strings are allowed in all cases; the delimiting quotes
209 * are simply stripped off if present.)
210 *
211 */
212
213 typedef enum {None, String, Integer, Number, Dimension} ValTyp;
214 typedef struct {
215 const char *Entry;
216 ValTyp Type;
217 } KeyDesc;
218
219 #define NKEYS (sizeof(KeyTab)/sizeof(KeyTab[0]))
220
221 KeyDesc KeyTab[] = {{"psfile", String}, /* j==0 in the routine below */
222 {"ifffile", String}, /* j==1 */
223 {"tekfile", String}, /* j==2 */
224 {"hsize", Number},
225 {"vsize", Number},
226 {"hoffset", Number},
227 {"voffset", Number},
228 {"hscale", Number},
229 {"vscale", Number},
230 {"angle", Number},
231 {"llx", Number},
232 {"lly", Number},
233 {"urx", Number},
234 {"ury", Number},
235 {"rwi", Number},
236 {"rhi", Number},
237 {"clip", None}};
238
239 #ifndef KPATHSEA
240 #define TOLOWER Tolower
241 #ifdef VMS
242 #ifndef __GNUC__ /* GNUC tolower is too simple */
243 #define Tolower tolower
244 #endif
245 #else
246 #ifdef VMCMS /* IBM: VM/CMS */
247 #define Tolower __tolower
248 #else
249 #ifdef MVSXA /* IBM: MVS/XA */
250 #define Tolower __tolower
251 #else
252 /*
253 * compare strings, ignore case
254 */
255 char
256 Tolower(register char c)
257 {
258 if ('A' <= c && c <= 'Z')
259 return(c+32);
260 else
261 return(c);
262 }
263 #endif
264 #endif /* IBM: VM/CMS */
265 #endif
266 #endif /* !KPATHSEA */
267 static int
268 IsSame(const char *a, const char *b)
269 {
270 for(; *a != '\0'; ) {
271 if( TOLOWER(*a) != TOLOWER(*b) )
272 return( 0 );
273 a++;
274 b++;
275 }
276 return( *b == '\0' );
277 }
278
279 char *KeyStr; /* Key and ... */
280 const char *ValStr; /* ... String values found */
281 long ValInt; /* Integer value found */
282 float ValNum; /* Number or Dimension value found */
283
284 static char *
285 GetKeyVal(char *str, int *tno) /* returns NULL if none found, else next scan point */
286 /* str : starting point for scan */
287 /* tno : table entry number of keyword, or -1 if keyword not found */
288 {
289 unsigned char *s;
290 register int i;
291 register char t;
292
293 for (s=str; *s <= ' ' && *s; s++); /* skip over blanks */
294 if (*s == '\0')
295 return (NULL);
296 KeyStr = s;
297 while (*s>' ' && *s!='=') s++;
298 if (0 != (t = *s))
299 *s++ = 0;
300
301 for(i=0; i<NKEYS; i++)
302 if( IsSame(KeyStr, KeyTab[i].Entry) )
303 goto found;
304 *tno = -1;
305 return (s);
306
307 found: *tno = i;
308 if (KeyTab[i].Type == None)
309 return (s);
310
311 if (t && t <= ' ') {
312 for (; *s <= ' ' && *s; s++); /* now look for the value part */
313 if ((t = *s)=='=')
314 s++;
315 }
316 ValStr = "";
317 if ( t == '=' ) {
318 while (*s <= ' ' && *s)
319 s++;
320 if (*s=='\'' || *s=='\"')
321 t = *s++; /* get string delimiter */
322 else t = ' ';
323 ValStr = s;
324 while (*s!=t && *s)
325 s++;
326 if (*s)
327 *s++ = 0;
328 }
329 switch (KeyTab[i].Type) {
330 case Integer:
331 if(sscanf(ValStr,"%ld",&ValInt)!=1) {
332 sprintf(errbuf,"Non-integer value (%.500s) given for keyword %.500s",
333 ValStr, KeyStr);
334 specerror(errbuf);
335 ValInt = 0;
336 }
337 break;
338 case Number:
339 case Dimension:
340 if(sscanf(ValStr,"%f",&ValNum)!=1) {
341 sprintf(errbuf,"Non-numeric value (%.500s) given for keyword %.500s",
342 ValStr, KeyStr);
343 specerror(errbuf);
344 ValNum = 0.0;
345 }
346 if (KeyTab[i].Type==Dimension) {
347 if (curfnt==NULL)
348 error("! No font selected");
349 ValNum = ValNum * ((double)curfnt->scaledsize) * conv * 72 / DPI;
350 }
351 break;
352 default: break;
353 }
354 return (s);
355 }
356
357 /*
358 * Now our routines. We get the number of bytes specified and place them
359 * into the string buffer, and then parse it. Numerous conventions are
360 * supported here for historical reasons.
361 *
362 * To support GNUplot's horribly long specials, we go ahead and malloc a
363 * new string buffer if necessary.
364 */
365
366 void
367 predospecial(integer numbytes, Boolean scanning)
368 {
369 register char *p = nextstring;
370 register int i = 0;
371 int j;
372 static int omega_specials = 0;
373
374 if (numbytes < 0 || numbytes > maxstring - nextstring) {
375 if (numbytes < 0 || numbytes > (INT_MAX - 1000) / 2 ) {
376 error("! Integer overflow in predospecial");
377 exit(1);
378 }
379 p = nextstring = mymalloc(1000 + 2 * numbytes);
380 maxstring = nextstring + 2 * numbytes + 700;
381 }
382 for (i=numbytes; i>0; i--)
383 #ifdef VMCMS /* IBM: VM/CMS */
384 *p++ = ascii2ebcdic[(char)dvibyte()];
385 #else
386 #ifdef MVSXA /* IBM: MVS/XA */
387 *p++ = ascii2ebcdic[(char)dvibyte()];
388 #else
389 *p++ = (char)dvibyte();
390 #endif /* IBM: VM/CMS */
391 #endif
392 if (pprescan)
393 return;
394 while (p[-1] <= ' ' && p > nextstring)
395 p--; /* trim trailing blanks */
396 if (p==nextstring) return; /* all blank is no-op */
397 *p = 0;
398 p = nextstring;
399 while (*p <= ' ')
400 p++;
401 #ifdef DEBUG
402 if (dd(D_SPECIAL))
403 fprintf_str(stderr, "Preprocessing special: %s\n", p);
404 #endif
405
406 /*
407 * We use strncmp() here to also pass things like landscape()
408 * or landscape: or such.
409 */
410
411 switch (*p) {
412 case 'o':
413 if (strncmp(p, "om:", 3)==0) { /* Omega specials ignored */
414 if (omega_specials==0) {
415 fprintf(stderr, "Omega specials are currently ignored\n");
416 omega_specials++;
417 }
418 return;
419 }
420 break;
421 case 'l':
422 if (strncmp(p, "landscape", 9)==0) {
423 if (hpapersize || vpapersize)
424 error(
425 "both landscape and papersize specified: ignoring landscape");
426 else
427 landscape = 1;
428 return;
429 }
430 break;
431 case 'p':
432 if (strncmp(p, "pos:", 4)==0) return; /* positional specials */
433 if (strncmp(p, "papersize", 9)==0) {
434 p += 9;
435 while (*p == '=' || *p == ' ')
436 p++;
437 if (lastpsizwins || hpapersize == 0 || vpapersize == 0) {
438 if (landscape) {
439 error(
440 "both landscape and papersize specified: ignoring landscape");
441 landscape = 0;
442 }
443 handlepapersize(p, &hpapersize, &vpapersize);
444 }
445 return;
446 }
447 break;
448 case 'x':
449 if (strncmp(p, "xtex:", 5)==0) return;
450 break;
451 case 's':
452 if (strncmp(p, "src:", 4)==0) return; /* source specials */
453 break;
454
455 case 'h':
456 if (strncmp(p, "header", 6)==0) {
457 char *q, *r, *pre = NULL, *post = NULL;
458 p += 6;
459 while ((*p <= ' ' || *p == '=' || *p == '(') && *p != 0)
460 p++;
461 if(*p == '{') {
462 p++;
463 while ((*p <= ' ' || *p == '=' || *p == '(') && *p != 0)
464 p++;
465 q = p;
466 while (*p != '}' && *p != 0)
467 p++;
468 r = p-1;
469 while ((*r <= ' ' || *r == ')') && r >= q)
470 r--;
471 if (*p != 0) p++;
472 r[1] = 0; /* q is the file name */
473 while ((*p <= ' ' || *p == '=' || *p == '(') && *p != 0)
474 p++;
475 if(strncmp(p, "pre", 3) == 0) {
476 int bracecount = 1, num_bytes = 0;
477 while(*p != '{' && *p != 0)
478 p++;
479 if (*p != 0) p++;
480 for(r = p; *r != 0; r++) {
481 if (*r == '{') bracecount++;
482 else if (*r == '}') bracecount--;
483 if (bracecount == 0) break;
484 num_bytes++;
485 }
486 pre = (char *)malloc(num_bytes+1);
487 r = pre;
488 for (j=0; j < num_bytes; j++)
489 *r++ = *p++;
490 *r = 0;
491 if (*p != 0) p++;
492 }
493 while ((*p <= ' ' || *p == '=' || *p == '(') && *p != 0)
494 p++;
495 if(strncmp(p, "post", 4) == 0) {
496 int bracecount = 1, num_bytes = 0;
497 while(*p != '{' && *p != 0)
498 p++;
499 if (*p != 0) p++;
500 for(r = p; *r != 0; r++) {
501 if (*r == '{') bracecount++;
502 else if (*r == '}') bracecount--;
503 if (bracecount == 0) break;
504 num_bytes++;
505 }
506 post = (char *)malloc(num_bytes+1);
507 r = post;
508 for (j=0; j < num_bytes; j++)
509 *r++ = *p++;
510 *r = 0;
511 }
512 if (strlen(q) > 0)
513 add_header_general(q, pre, post);
514 } else {
515 q = p; /* we will remove enclosing parentheses */
516 p = p + strlen(p) - 1;
517 while ((*p <= ' ' || *p == ')') && p >= q)
518 p--;
519 p[1] = 0;
520 if (p >= q)
521 add_header(q);
522 }
523 }
524 break;
525 /* IBM: color - added section here for color header and color history */
526 /* using strncmp in this fashion isn't perfect; if later someone wants
527 to introduce a verb like colorspace, well, just checking
528 strcmp(p, "color", 5) will not do. But we leave it alone for the moment.
529 --tgr */
530 case 'b':
531 if (strncmp(p, "background", 10) == 0) {
532 usescolor = 1;
533 p += 10;
534 while ( *p && *p <= ' ' ) p++;
535 background(p);
536 return;
537 }
538 break;
539 case 'c':
540 if (strncmp(p, "color", 5) == 0) {
541 usescolor = 1;
542 p += 5;
543 while ( *p && *p <= ' ' ) p++;
544 if (strncmp(p, "push", 4) == 0 ) {
545 p += 4;
546 while ( *p && *p <= ' ' ) p++;
547 pushcolor(p, 0);
548 } else if (strncmp(p, "pop", 3) == 0 ) {
549 popcolor(0);
550 } else {
551 resetcolorstack(p,0);
552 }
553 } /* IBM: color - end changes */
554 break;
555 case '!':
556 {
557 register struct bangspecial *q;
558 p++;
559 q = (struct bangspecial *)mymalloc((integer)(sizeof(struct bangspecial)));
560 q->actualstuff = mymalloc(strlen(p) + 1);
561 strcpy(q->actualstuff, p);
562 q->next = bangspecials;
563 bangspecials = q;
564 usesspecial = 1;
565 return;
566 }
567 break;
568 default:
569 break;
570 }
571 usesspecial = 1; /* now the special prolog will be sent */
572 if (scanning && *p != '"' && (p=GetKeyVal(p, &j)) != NULL && j==0
573 && *ValStr != '`') /* Don't bother to scan compressed files. */
574 scanfontcomments(ValStr);
575 }
576
577 /* Return 1 if S is readable along figpath, 0 if not. */
578 static int
579 maccess(char *s)
580 {
581 FILE *f = search(figpath, s, "r");
582 int found = (f != 0);
583 if (found)
584 (*close_file) (f);
585 return found;
586 }
587
588 const char *tasks[] = { 0, "iff2ps", "tek2ps" };
589
590 #define PSFILESIZ 511
591 static char psfile[PSFILESIZ];
592
593 void
594 dospecial(integer numbytes)
595 {
596 register char *p = nextstring;
597 register int i = 0;
598 int j, systemtype = 0;
599 register const char *q;
600 Boolean psfilewanted = 1;
601 const char *task = 0;
602 char cmdbuf[111];
603 #ifdef HPS
604 if (HPS_FLAG && PAGEUS_INTERUPPTUS) {
605 HREF_COUNT--;
606 start_new_box();
607 PAGEUS_INTERUPPTUS = 0;
608 }
609 if (HPS_FLAG && NEED_NEW_BOX) {
610 vertical_in_hps();
611 NEED_NEW_BOX = 0;
612 }
613 #endif
614 if (nextstring + numbytes > maxstring)
615 error("! out of string space in dospecial");
616 for (i=numbytes; i>0; i--)
617 #ifdef VMCMS /* IBM: VM/CMS */
618 *p++ = ascii2ebcdic[(char)dvibyte()];
619 #else
620 #ifdef MVSXA /* IBM: MVS/XA */
621 *p++ = ascii2ebcdic[(char)dvibyte()];
622 #else
623 *p++ = (char)dvibyte();
624 #endif /* IBM: VM/CMS */
625 #endif
626 while (p[-1] <= ' ' && p > nextstring)
627 p--; /* trim trailing blanks */
628 if (p==nextstring) return; /* all blank is no-op */
629 *p = 0;
630 p = nextstring;
631 while (*p <= ' ')
632 p++;
633 #ifdef DEBUG
634 if (dd(D_SPECIAL))
635 fprintf_str(stderr, "Processing special: %s\n", p);
636 #endif
637
638 switch (*p) {
639 case 'o':
640 if (strncmp(p, "om:", 3)==0) { /* Omega specials ignored */
641 return;
642 }
643 break;
644 case 'e':
645 if (strncmp(p, "em:", 3)==0) { /* emTeX specials in emspecial.c */
646 emspecial(p);
647 return;
648 }
649
650 /* added for dvi2ps special */
651 if (strncmp(p, "epsfile=", 8)==0) { /* epsf.sty for dvi2ps-j */
652 float llx, lly, urx, ury;
653 unsigned psfilelen = 0;
654
655 p += 8;
656 while (*p && !isspace((unsigned char)*p)) {
657 if (psfilelen < PSFILESIZ - 1) {
658 psfile[psfilelen] = *p;
659 psfilelen++;
660 p++;
661 } else {
662 psfile[psfilelen] = 0; /* should not strictly be necessary */
663 sprintf(errbuf,
664 "! epsfile=%.20s... argument longer than %d characters",
665 psfile, PSFILESIZ);
666 error(errbuf);
667 }
668 }
669 if (psfilelen == 0) {
670 error ("! epsfile= argument empty");
671 }
672 psfile[psfilelen] = 0;
673 p += strlen(psfile);
674 fgetboundingbox(psfile, &llx, &lly, &urx, &ury);
675 hvpos();
676 cmdout("@beginspecial");
677 floatroundout(llx);
678 cmdout("@llx");
679 floatroundout(lly);
680 cmdout("@lly");
681 floatroundout(urx);
682 cmdout("@urx");
683 floatroundout(ury);
684 cmdout("@ury");
685
686 while ((p = GetKeyVal(p, &j))) {
687 switch (j) {
688 case 3: /* hsize */
689 floatroundout(ValNum*10);
690 cmdout("@rwi");
691 break;
692 case 4: /* vsize */
693 floatroundout(ValNum*10);
694 cmdout("@rhi");
695 break;
696 case 7: /* hscale */
697 floatroundout((urx-llx)*ValNum*10);
698 cmdout("@rwi");
699 break;
700 case 8: /* vscale */
701 floatroundout((ury-lly)*ValNum*10);
702 cmdout("@rhi");
703 break;
704 default:
705 sprintf(errbuf, "Unknown keyword `%.500s' in \\special{epsfile=%.500s...} will be ignored\n",
706 KeyStr, psfile);
707 specerror(errbuf);
708 break;
709 }
710 }
711 cmdout("@setspecial");
712 numout((integer)0);
713 cmdout("lly");
714 cmdout("ury");
715 cmdout("sub");
716 cmdout("TR");
717 figcopyfile(psfile, 0);
718 cmdout("\n@endspecial");
719 return;
720 }
721 /* end addition */
722 break;
723 case 'p':
724 if (strncmp(p, "pos:", 4)==0) return; /* positional specials */
725 if (strncmp(p, "ps:", 3)==0) {
726 psflush(); /* now anything can happen. */
727 if (p[3]==':') {
728 if (strncmp(p+4, "[nobreak]", 9) == 0) {
729 hvpos();
730 outputstring(&p[13]);
731 } else if (strncmp(p+4, "[begin]", 7) == 0) {
732 hvpos();
733 trytobreakout(&p[11]);
734 } else if (strncmp(p+4, "[end]", 5) == 0)
735 trytobreakout(&p[9]);
736 else trytobreakout(&p[4]);
737 } else if (strncmp(p+3, " plotfile ", 10) == 0) {
738 char *sfp;
739 hvpos();
740 p += 13;
741 /*
742 * Fixed to allow popen input for plotfile
743 * TJD 10/20/91
744 */
745 while (*p == ' ') p++;
746 if (*p == '"') {
747 p++;
748 for (sfp = p; *sfp && *sfp != '"'; sfp++);
749 } else {
750 for (sfp = p; *sfp && *sfp != ' '; sfp++);
751 }
752 *sfp = '\0';
753 if (*p == '`')
754 figcopyfile(p+1, 1);
755 else
756 figcopyfile (p, 0);
757 /* End TJD changes */
758 } else {
759 hvpos();
760 trytobreakout(&p[3]);
761 psflush();
762 hvpos();
763 }
764 return;
765 }
766 if (strncmp(p, "papersize", 9) == 0)
767 return;
768 #ifdef TPIC
769 if (strncmp(p, "pn ", 3) == 0) {setPenSize(p+2); return;}
770 if (strncmp(p, "pa ", 3) == 0) {addPath(p+2); return;}
771 #endif
772
773 /* added for jdvi2kps special */
774 if (strncmp(p, "postscriptbox", 13)==0) { /* epsbox.sty for jdvi2kps */
775 float w, h;
776 float llx, lly, urx, ury;
777 if (strlen(p)-13-6 >= PSFILESIZ) { /* -6 for the braces */
778 /* We're not allowing as long a name as we could, since however
779 many characters the two {%fpt} arguments consume is not
780 taken into account. But parsing it all so we know the
781 character length is too much work for this obscure special. */
782 sprintf(errbuf,
783 "! postscriptbox{} arguments longer than %d characters",
784 PSFILESIZ);
785 error(errbuf);
786 }
787 if (sscanf(p+13, "{%fpt}{%fpt}{%[^}]}", &w, &h, psfile) != 3)
788 break;
789 fgetboundingbox(psfile, &llx, &lly, &urx, &ury);
790 hvpos();
791 cmdout("@beginspecial");
792 floatroundout(llx);
793 cmdout("@llx");
794 floatroundout(lly);
795 cmdout("@lly");
796 floatroundout(urx);
797 cmdout("@urx");
798 floatroundout(ury);
799 cmdout("@ury");
800 floatroundout(w*10*72/72.27);
801 cmdout("@rwi");
802 floatroundout(h*10*72/72.27);
803 cmdout("@rhi");
804 cmdout("@setspecial");
805 figcopyfile(psfile, 0);
806 cmdout("\n@endspecial");
807 return;
808 }
809 /* end addition */
810 break;
811 case 'l':
812 if (strncmp(p, "landscape", 9)==0) return;
813 break;
814 case '!':
815 return;
816 case 'h':
817 if (strncmp(p, "header", 6)==0) return;
818 #ifdef HPS
819 if (strncmp(p, "html:", 5)==0) {
820 if (! HPS_FLAG) return;
821 p += 5;
822 while (isspace((unsigned char)*p))
823 p++;
824 if (*p == '<') {
825 char *sp = p;
826 char *str;
827 int ii=0, len, lower_len;
828
829 while ((*p) && (*p != '>')) {
830 ii++;
831 p++;
832 }
833 str = (char *)mymalloc(ii+2);
834 strncpy(str,sp+1,ii-1);
835 str[ii-1] = 0;len=strlen(str);
836 if(len>6) lower_len=6; else lower_len=len;
837 for(ii=0;ii<lower_len;ii++) str[ii]=tolower((unsigned char)str[ii]);
838 do_html(str);
839 free(str);
840 } else
841 #ifdef KPATHSEA
842 if (!kpse_tex_hush ("special"))
843 #endif
844 {
845
846 sprintf(errbuf,"Error in html special\n");
847 error(errbuf);
848 return;
849 }
850 return;
851 }
852 #else
853 if (strncmp(p, "html:", 5)==0) return;
854 #endif
855 case 'w':
856 case 'W':
857 if (strncmp(p+1, "arning", 6) == 0) {
858 static int maxwarns = 50;
859 if (maxwarns > 0) {
860 error(p);
861 maxwarns--;
862 } else if (maxwarns == 0) {
863 error(". . . rest of warnings suppressed");
864 maxwarns--;
865 }
866 return;
867 }
868 #ifdef TPIC
869 if (strcmp(p, "wh") == 0) {whitenLast(); return;}
870 #endif
871 break;
872 case 'b':
873 if ( strncmp(p, "background", 10) == 0 )
874 return; /* already handled in prescan */
875 #ifdef TPIC
876 if (strcmp(p, "bk") == 0) {blackenLast(); return;}
877 #endif
878 break;
879 case 'c':
880 if (strncmp(p, "color", 5) == 0) {
881 p += 5;
882 while ( *p && *p <= ' ' ) p++;
883 if (strncmp(p, "push", 4) == 0 ) {
884 p += 4;
885 while ( *p && *p <= ' ' ) p++;
886 pushcolor(p,1);
887 } else if (strncmp(p, "pop", 3) == 0 ) {
888 popcolor(1);
889 } else {
890 resetcolorstack(p,1);
891 }
892 return;
893 } /* IBM: color - end changes*/
894 break;
895 case 'f':
896 #ifdef TPIC
897 if (strcmp(p, "fp") == 0) {flushPath(0); return;}
898 #endif
899 break;
900 case 'i':
901 #ifdef TPIC
902 if (strcmp(p, "ip") == 0) {flushPath(1); return;} /* tpic 2.0 */
903 if (strncmp(p, "ia ", 3) == 0) {arc(p+2, 1); return;} /* tpic 2.0 */
904 #endif
905 break;
906 case 'd':
907 #ifdef TPIC
908 if (strncmp(p, "da ", 3) == 0) {flushDashed(p+2, 0); return;}
909 if (strncmp(p, "dt ", 3) == 0) {flushDashed(p+2, 1); return;}
910 #endif
911 break;
912 case 's':
913 if (strncmp(p, "src:", 4) == 0) return; /* source specials */
914 #ifdef TPIC
915 if (strcmp(p, "sp") == 0) {flushSpline(p+2); return;} /* tpic 2.0 */
916 if (strncmp(p, "sp ", 3) == 0) {flushSpline(p+3); return;} /* tpic 2.0 */
917 if (strcmp(p, "sh") == 0) {shadeLast(p+2); return;} /* tpic 2.0 */
918 if (strncmp(p, "sh ", 3) == 0) {shadeLast(p+3); return;} /* tpic 2.0 */
919 #endif
920 break;
921 case 'a':
922 #ifdef TPIC
923 if (strncmp(p, "ar ", 3) == 0) {arc(p+2, 0); return;} /* tpic 2.0 */
924 #endif
925 break;
926 case 't':
927 #ifdef TPIC
928 if (strncmp(p, "tx ", 3) == 0) {SetShade(p+3); return;}
929 #endif
930 break;
931 case '"':
932 hvpos();
933 cmdout("@beginspecial");
934 cmdout("@setspecial");
935 trytobreakout(p+1);
936 cmdout("\n@endspecial");
937 return;
938 break;
939 default:
940 break;
941 }
942 /* At last we get to the key/value conventions */
943 psfile[0] = '\0';
944 hvpos();
945 cmdout("@beginspecial");
946
947 while( (p=GetKeyVal(p,&j)) != NULL )
948 switch (j) {
949 case -1: /* for compatibility with old conventions, we allow a file name
950 * to be given without the 'psfile=' keyword */
951 if (!psfile[0] && maccess(KeyStr)==1) { /* yes we can read it */
952 if (strlen(KeyStr) >= PSFILESIZ) {
953 sprintf(errbuf,
954 "! Bare filename (%.20s...) in \\special longer than %d characters",
955 KeyStr, PSFILESIZ);
956 }
957 strcpy(psfile,KeyStr);
958 } else {
959 if (strlen(KeyStr) < 40) {
960 sprintf(errbuf,
961 "Unknown keyword (%s) in \\special will be ignored",
962 KeyStr);
963 } else {
964 sprintf(errbuf,
965 "Unknown keyword (%.40s...) in \\special will be ignored",
966 KeyStr);
967 }
968 specerror(errbuf);
969 }
970 break;
971 case 0: case 1: case 2: /* psfile */
972 if (psfile[0]) {
973 sprintf(errbuf, "More than one \\special %s given; %.40s ignored",
974 "psfile", ValStr);
975 specerror(errbuf);
976 } else {
977 if (strlen(ValStr) >= PSFILESIZ) {
978 sprintf(errbuf,
979 "! PS filename (%.20s...) in \\special longer than %d characters",
980 ValStr, PSFILESIZ);
981 error(errbuf);
982 }
983 strcpy(psfile, ValStr);
984 }
985 task = tasks[j];
986 break;
987 default: /* most keywords are output as PostScript procedure calls */
988 if (KeyTab[j].Type == Integer)
989 numout((integer)ValInt);
990 else if (KeyTab[j].Type == String)
991 for (q=ValStr; *q; q++)
992 scout(*q);
993 else if (KeyTab[j].Type == None);
994 else { /* Number or Dimension */
995 ValInt = (integer)(ValNum<0? ValNum-0.5 : ValNum+0.5);
996 if (ValInt-ValNum < 0.001 && ValInt-ValNum > -0.001)
997 numout((integer)ValInt);
998 else {
999 snprintf(cmdbuf, sizeof(cmdbuf), "%f", ValNum);
1000 cmdout(cmdbuf);
1001 }
1002 }
1003 snprintf(cmdbuf, sizeof(cmdbuf), "@%s", KeyStr);
1004 cmdout(cmdbuf);
1005 }
1006
1007 cmdout("@setspecial");
1008
1009 if(psfile[0]) {
1010 if (task == 0) {
1011 systemtype = (psfile[0]=='`');
1012 figcopyfile(psfile+systemtype, systemtype);
1013 } else {
1014 fprintf (stderr, "dvips: iff2ps and tek2ps are not supported.\n");
1015 }
1016 } else if (psfilewanted
1017 #ifdef KPATHSEA
1018 && !kpse_tex_hush ("special")
1019 #endif
1020 )
1021 specerror("No \\special psfile was given; figure will be blank");
1022
1023 cmdout("@endspecial");
1024 }
1025
1026 /*
1027 * Handles specials encountered during bounding box calculations.
1028 * Currently we only deal with psfile's or PSfiles and only those
1029 * that do not use rotations.
1030 */
1031 static float rbbox[4];
1032 float *
1033 bbdospecial(int nbytes)
1034 {
1035 char *p = nextstring;
1036 int i, j;
1037 char seen[NKEYS];
1038 float valseen[NKEYS];
1039
1040 if (nbytes < 0 || nbytes > maxstring - nextstring) {
1041 if (nbytes < 0 || nbytes > (INT_MAX - 1000) / 2 ) {
1042 error("! Integer overflow in bbdospecial");
1043 exit(1);
1044 }
1045 p = nextstring = mymalloc(1000 + 2 * nbytes);
1046 maxstring = nextstring + 2 * nbytes + 700;
1047 }
1048 if (nextstring + nbytes > maxstring)
1049 error("! out of string space in bbdospecial");
1050 for (i=nbytes; i>0; i--)
1051 #ifdef VMCMS /* IBM: VM/CMS */
1052 *p++ = ascii2ebcdic[(char)dvibyte()];
1053 #else
1054 #ifdef MVSXA /* IBM: MVS/XA */
1055 *p++ = ascii2ebcdic[(char)dvibyte()];
1056 #else
1057 *p++ = (char)dvibyte();
1058 #endif /* IBM: VM/CMS */
1059 #endif
1060 while (p[-1] <= ' ' && p > nextstring)
1061 p--; /* trim trailing blanks */
1062 if (p==nextstring) return NULL; /* all blank is no-op */
1063 *p = 0;
1064 p = nextstring;
1065 while (*p && *p <= ' ')
1066 p++;
1067 if (strncmp(p, "psfile", 6)==0 || strncmp(p, "PSfile", 6)==0) {
1068 float originx = 0, originy = 0, hscale = 1, vscale = 1,
1069 hsize = 0, vsize = 0;
1070 for (j=0; j<NKEYS; j++)
1071 seen[j] = 0;
1072 j = 0;
1073 while ((p=GetKeyVal(p, &j))) {
1074 if (j >= 0 && j < NKEYS && KeyTab[j].Type == Number) {
1075 seen[j]++;
1076 valseen[j] = ValNum;
1077 }
1078 }
1079 /*
1080 * This code mimics what happens in @setspecial.
1081 */
1082 if (seen[3])
1083 hsize = valseen[3];
1084 if (seen[4])
1085 vsize = valseen[4];
1086 if (seen[5])
1087 originx = valseen[5];
1088 if (seen[6])
1089 originy = valseen[6];
1090 if (seen[7])
1091 hscale = valseen[7] / 100.0;
1092 if (seen[8])
1093 vscale = valseen[8] / 100.0;
1094 if (seen[10] && seen[12])
1095 hsize = valseen[12] - valseen[10];
1096 if (seen[11] && seen[13])
1097 vsize = valseen[13] - valseen[11];
1098 if (seen[14] || seen[15]) {
1099 if (seen[14] && seen[15] == 0) {
1100 hscale = vscale = valseen[14] / (10.0 * hsize);
1101 } else if (seen[15] && seen[14] == 0) {
1102 hscale = vscale = valseen[15] / (10.0 * vsize);
1103 } else {
1104 hscale = valseen[14] / (10.0 * hsize);
1105 vscale = valseen[15] / (10.0 * vsize);
1106 }
1107 }
1108 /* at this point, the bounding box in PostScript units relative to
1109 the current dvi point is
1110 originx originy originx+hsize*hscale originy+vsize*vscale
1111 We'll let the bbox routine handle the remaining math.
1112 */
1113 rbbox[0] = originx;
1114 rbbox[1] = originy;
1115 rbbox[2] = originx+hsize*hscale;
1116 rbbox[3] = originy+vsize*vscale;
1117 return rbbox;
1118 }
1119 return 0;
1120 }