home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / unix / unix_mac.hqx / special.c < prev   
Encoding:
C/C++ Source or Header  |  1987-02-12  |  3.8 KB  |  185 lines

  1. /*
  2.     Routines for handling \special commands.  Currently only supports
  3.     inserting impress data from external files.  Bitmap stuff is not
  4.     complete.
  5.     
  6.     Allan Weber (Weber%Brand@USC-ECL.ARPA) - 9/12/86
  7. */
  8.  
  9. /* #define DEBUG */
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13.  
  14. extern char *malloc();
  15. extern int   errno;
  16. extern int ImHH, ImVV, hh, vv;
  17.  
  18. int S_bitmap();
  19. int S_impress();
  20. char *nb();
  21.  
  22. struct spec {
  23.     char *name;
  24.     int  (*func)();
  25. } spectbl[] = {
  26.     "bitmap",  S_bitmap,
  27.     "impress", S_impress,
  28.     "", NULL
  29. };
  30.  
  31. /* Perform a \special */
  32. DoSpecial (len)
  33. int len;            /* length of the \special string */
  34. {
  35.     struct spec *s;
  36.     char *p;
  37.     register char ch, *p1, *p2;
  38.     if (ImHH != hh || ImVV != vv)    /* flush out any pending moves */
  39.         ImSetPosition (hh, vv);
  40.     p1 = p = malloc(len + 1);
  41.     while (len--)            /* read the special string */
  42.         *p1++ = getc(stdin);
  43.     *p1 = '\0';
  44. #ifdef DEBUG
  45.     fprintf(stderr,"special: '%s'\n",p);
  46. #endif
  47.     p1 = nb(p);        /* skip leading blanks */    
  48.     if (*p1 == '\0')    /* if nothing left, return */
  49.         return;
  50.     p2 = p1;
  51.     while ((ch = *p2) != '\0' && !isspace(ch)) /* find end of 1st word */
  52.         p2++;
  53.     if (ch != '\0') {    /* more than one word? */
  54.         *p2++ = '\0';    /* mark end of first word */
  55.         p2 = nb(p2);    /* point to start of rest of string */
  56.     }
  57.     s = spectbl;
  58.     while (s->func != NULL) {
  59.         if (strcmp(p1,s->name) == 0) {
  60.             (s->func)(p1,p2);
  61.             return;
  62.         }
  63.         else
  64.             s++;
  65.     }
  66.     free(p);    
  67. }
  68.  
  69. /* 
  70. split - separate the words in string s, placing a '\0' after each one
  71. and setting the elements of tok to point to the beginning of each word.
  72. Only do this for up to max words.  Return the number of words found.
  73. */    
  74. static split(s,tok,max)
  75. register char *s;
  76. char *tok[];
  77. {
  78.     int i, n;
  79.     register char ch;
  80.     n = 0;
  81.     for (i = 0; i < max; i++) {
  82.         s = nb(s);            /* find word start */
  83.         if ((ch = *s) != '\0') {    /* did we find one? */
  84.             tok[i] = s;
  85.             n++;
  86.             while ((ch = *s) != '\0' && !isspace(ch))
  87.                 s++;        /* find end of word */
  88.             if (ch != '\0')        /* mark end of word */
  89.                 *s++ = '\0';
  90.         }
  91.         else
  92.             tok[i] = NULL;
  93.     }
  94.     return(n);
  95. }
  96.  
  97. char *nb(s)
  98. char *s;
  99. {
  100.     register char ch;
  101.     while ((ch = *s) != '\0' && isspace(ch))
  102.         s++;
  103.     return(s);
  104. }
  105.  
  106. /* --------------------------------------------------------------------- */
  107.  
  108. static S_bitmap(s1, s2)
  109. char *s1, *s2;
  110. {
  111.     int i, n, fd;
  112.     int rows, cols, wrows, wcols, wroff, wcoff;
  113.     char *p, *word[7];
  114. #ifdef DEBUG
  115.     fprintf(stderr,"in bitmap: s1='%s', s2='%s'\n",s1,s2);
  116. #endif
  117.     p = malloc(strlen(s2) + 1);
  118.     strcpy(p,s2);
  119.     n = split(p,word,7);
  120. #ifdef DEBUG
  121.     fprintf(stderr, "split into %d words\n",n);
  122.     for (i = 0; i < n; i++)
  123.         fprintf(stderr,"%d: '%s'\n",i,word[i]);
  124. #endif
  125. #ifndef DEBUG
  126.     if ((fd = open(word[0], 0)) == -1) {
  127.         error (1, errno, "can't open %s", s2);
  128.  
  129.     }
  130.     if (n < 7)        /* no window offset */
  131.         word[6] = word[5] = "0";
  132.     if (n < 5) {        /* full window */
  133.         word[3] = word[1];
  134.         word[4] = word[2];
  135.     }
  136.     if (n < 3) {        /* need at least the dimensions */
  137.         error(1, 0, "\"\\special bitmap\" not enough arguments");
  138.         free(p);
  139.         return;
  140.     }
  141.     rows = atoi(word[1]); cols = atoi(word[2]);
  142.     wrows = atoi(word[3]); wcols = atoi(word[4]);
  143.     wroff = atoi(word[5]); wcoff = atoi(word[6]);
  144.     close(fd);
  145. #endif
  146.     free(p);
  147. }
  148.  
  149. static S_impress(s1, s2)
  150. char *s1, *s2;
  151. {
  152.     FILE *fp;
  153.     int n, h, v, ch;
  154.     char *p, *word[3];
  155. #ifdef DEBUG
  156.     fprintf(stderr,"in impress: s1='%s', s2='%s'\n",s1,s2);
  157. #endif
  158. #ifndef DEBUG
  159.     p = malloc(strlen(s2) + 1);
  160.     strcpy(p,s2);
  161.     n = split(p,word,3);
  162.     if ((fp = fopen(word[0], "r")) == NULL) {
  163.         error (1, errno, "can't open %s", word[0]);
  164.     }
  165.     putchar(211);        /* push */
  166.     if (n == 3) {        /* absolute move specified */
  167.         if (sscanf(word[1],"%d", &h) == 1) {
  168.             putchar(135);
  169.             putchar((h >> 8) & 255);
  170.             putchar(h & 255);
  171.         }
  172.         if (sscanf(word[2],"%d", &v) == 1) {
  173.             putchar(137);
  174.             putchar((v >> 8) & 255);
  175.             putchar(v & 255);
  176.         }
  177.     }
  178.     while ((ch = getc(fp)) != EOF || !feof(fp))
  179.         putchar(ch);
  180.     putchar(212);        /* pop */
  181.     fclose(fp);
  182. #endif
  183. }
  184.  
  185.