home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW cawf 4.0.9 / device.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  9.4 KB  |  249 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    device.c -- cawf(1) output device support functions
  3.  */
  4.  
  5. /*
  6.  *    Copyright (c) 1991 Purdue University Research Foundation,
  7.  *    West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *    Written by Victor A. Abell <abe@cc.purdue.edu>,  Purdue    University
  10.  *    Computing Center.  Not derived from licensed software; derived from
  11.  *    awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *    Permission is granted to anyone to use this software for any
  14.  *    purpose on any computer system, and to alter it and redistribute
  15.  *    it freely, subject to the following restrictions:
  16.  *
  17.  *    1. The author is not responsible for any consequences of use of
  18.  *       this software, even if they arise from flaws in it.
  19.  *
  20.  *    2. The origin of this software must not be misrepresented, either
  21.  *       by explicit claim or by omission.  Credits must appear in the
  22.  *       documentation.
  23.  *
  24.  *    3. Altered versions must be plainly marked as such, and must not
  25.  *       be misrepresented as being the original software.  Credits must
  26.  *       appear in the documentation.
  27.  *
  28.  *    4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32. #include <ctype.h>
  33.  
  34. _PROTOTYPE(static unsigned char *Convstr,(char *s, int *len));
  35. _PROTOTYPE(static int Convfont,(char *nm, char *s, char **fn,
  36.     unsigned char **fi));
  37.  
  38. #if    defined(__EMX__)
  39. #define    strcasecmp    stricmp
  40. #else    /* !defined(__EMX__) */
  41. # if    !defined(UNIX)
  42. #define    strcasecmp    strcmpi
  43. # endif    /* !defined(UNIX) */
  44. #endif    /* defined(__EMX__) */
  45.  
  46.  
  47.  
  48. /*
  49.  * Convstr(s, len) - convert a string
  50.  */
  51.  
  52. static unsigned char *
  53. Convstr(s, len)
  54.     char *s;            /* input string */
  55.     int *len;            /* length of result */
  56. {
  57.     int c;                /* character assembly */
  58.     unsigned char *cp;        /* temporary character pointer */
  59.     char *em;            /* error message */
  60.     int i;                /* temporary index */
  61.     int l;                /* length */
  62.     unsigned char *r;        /* result string */
  63. /*
  64.  * Make space for the result.
  65.  */
  66.     if ((r = (unsigned char *)malloc(strlen((char *)s) + 1)) == NULL) {
  67.         (void) fprintf(stderr, "%s: out of string space at %s\n",
  68.             Pname, s);
  69.         return(NULL);
  70.     }
  71. /*
  72.  *  NULL) {
  73.             (void) fprintf(stderr, "%s: no space for %s name\n",
  74.                 Pname, DEVCONFIG);
  75.             return(1);
  76.         }
  77.         (void) sprintf(s, "%s%s%s", p,
  78.  
  79. #if    defined(macintosh)
  80.             "",
  81. #else    /* !defined(macintosh) */
  82.             "/",
  83. #endif    /* defined(macintosh) */
  84.  
  85.             DEVCONFIG);
  86.  
  87.         p = s;
  88.     }
  89. /*
  90.  * Open the configuration file.
  91.  */
  92.  
  93. #if    defined(UNIX)
  94.     if ((fs = fopen(p, "r")) == NULL)
  95. #else    /* !defined(UNIX) */
  96.     if ((fs = fopen(p, "rt")) == NULL)
  97. #endif    /* defined(UNIX) */
  98.  
  99.     {
  100.         (void) fprintf(stderr, "%s: can't open config file: %s\n",
  101.             Pname, p);
  102.         return(1);
  103.     }
  104.     *line = ' ';
  105. /*
  106.  * Look for a device definition line -- a line that begins with a name.
  107.  */
  108.     while ( ! feof(fs)) {
  109.         if (*line == '\t' || *line == '#' || *line == ' ') {
  110.             (void) fgets(line, MAXLINE, fs);
  111.             continue;
  112.         }
  113.         if ((s = strrchr(line, '\n')) != NULL)
  114.             *s = '\0';
  115.         else
  116.             line[MAXLINE-1] = '\0';
  117.     /*
  118.      * Match device name.
  119.      */
  120.         if (strcmp(Device, line) != 0) {
  121.             (void) fgets(line, MAXLINE, fs);
  122.             continue;
  123.         }
  124.         fd = 1;
  125.     /*
  126.      * Read the parameter lines for the device.
  127.      */
  128.         while (fgets(line, MAXLINE, fs) != NULL) {
  129.             if (*line == ' ') {
  130.                 for (i = 1; line[i] == ' '; i++)
  131.                     ;
  132.             } else if (*line == '\t')
  133.                 i = 1;
  134.             else
  135.                 break;
  136.  
  137. #if    defined(__STDC__)
  138.             if ( ! isalpha(line[i])
  139. #else    /* !defined(__STDC__) */
  140.             if ( ! isascii(line[i]) || ! isalpha(line[i])
  141. #endif    /* defined(__STDC__) */
  142.  
  143.             ||   line[i+1] != '=')
  144.                 break;
  145.             if ((s = strrchr(line, '\n')) != NULL)
  146.                 *s = '\0';
  147.             else
  148.                 line[MAXLINE-1] = '\0';
  149.             switch (line[i]) {
  150.         /*
  151.          * \tb=<bolding_string>
  152.          */
  153.             case 'b':
  154.                 if (Fstr.b != NULL) {
  155.                     (void) fprintf(stderr,
  156.                     "%s: dup bold for %s in %s: %s\n",
  157.                     Pname, Device, p, line);
  158.                     (void) free(Fstr.b);
  159.                     Fstr.b = NULL;
  160.                 }
  161.                 if ((Fstr.b = Convstr(&line[i+2], &Fstr.bl))
  162.                 == NULL)
  163.                     err++;
  164.                 break;
  165.         /*
  166.          * \ti=<italicization_string>
  167.          */
  168.             case 'i':
  169.                 if (Fstr.it != NULL) {
  170.                     (void) fprintf(stderr,
  171.                     "%s: dup italic for %s in %s: %s\n",
  172.                     Pname, Device, p, line);
  173.                     (void) free(Fstr.it);
  174.                     Fstr.it = NULL;
  175.                 }
  176.                 if ((Fstr.it = Convstr(&line[i+2], &Fstr.itl))
  177.                 == NULL)
  178.                     err++;
  179.                 break;
  180.         /*
  181.          * \tr=<return_to_Roman_string>
  182.          */
  183.             case 'r':
  184.                 if (Fstr.r != NULL) {
  185.                     (void) fprintf(stderr,
  186.                     "%s: dup roman for %s in %s: %s\n",
  187.                     Pname, Device, p, line);
  188.                     (void) free(Fstr.r);
  189.                     Fstr.r = NULL;
  190.                 }
  191.                 if ((Fstr.r = Convstr(&line[i+2], &Fstr.rl))
  192.                 == NULL)
  193.                     err++;
  194.                 break;
  195.         /*
  196.          * \tf=<font_name>=<font_initialization_string>
  197.          */
  198.             case 'f':
  199.                 if ( ! Devfont || Fstr.i)
  200.                     break;
  201.                 if ((i = Convfont(Device, &line[i+2], &fn, &fi))
  202.                 < 0)
  203.                     err++;
  204.                 else if (fn && strcmp(Devfont, fn) == 0) {
  205.                     Fstr.i = fi;
  206.                     Fstr.il = i;
  207.                     fi = NULL;
  208.                 }
  209.                 if (fn) {
  210.                     (void) free(fn);
  211.                     fn = NULL;
  212.                 }
  213.                 if (fi) {
  214.                     (void) free((char *)fi);
  215.                     fi = NULL;
  216.                 }
  217.                 break;
  218.         /*
  219.          * ????
  220.          */
  221.             default:
  222.                 (void) fprintf(stderr,
  223.                     "%s: unknown device %s line: %s\n",
  224.                     Pname, Device, line);
  225.                 err++;
  226.             }
  227.         }
  228.         break;
  229.     }
  230.     (void) fclose(fs);
  231.     if (err)
  232.         return(1);
  233. /*
  234.  * See if the device stanza was located and the font exists.
  235.  */
  236.     if ( ! fd) {
  237.         (void) fprintf(stderr, "%s: can't find device %s in %s\n",
  238.             Pname, Device, p);
  239.         return(1);
  240.     }
  241.     if (Devfont && ! Fstr.i) {
  242.         (void) fprintf(stderr,
  243.             "%s: font %s for device %s not found in %s\n",
  244.             Pname, Devfont, Device, p);
  245.         return(1);
  246.     }
  247.     return(0);
  248. }
  249.