home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gendev.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.8 KB  |  364 lines

  1. /* Copyright (C) 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gendev.c,v 1.2 2000/09/19 19:00:23 lpd Exp $ */
  20. /* Generate .dev configuration files */
  21. #include "stdpre.h"
  22. #include <assert.h>
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>        /* for calloc */
  26. #include <string.h>
  27.  
  28. /*
  29.  * This program generates .dev configuration files.
  30.  *
  31.  * Usage:
  32.  *      gendev [-Z] [-n [<name_prefix> | -]] [-C [<dir> | -]]
  33.  *        (-d <devfile> | -m <modfile> | -a <modfile>)
  34.  *        (-<category> | <item>)*
  35.  *
  36.  * The name_prefix is for device[2], font, init, and iodev resources.
  37.  * ****** DOESN'T HANDLE -replace YET ******
  38.  * ****** DOESN'T MERGE device AND device2 ******
  39.  */
  40.  
  41. #define DEFAULT_NAME_PREFIX "gs_"
  42. #define INITIAL_CATEGORY "obj"
  43.  
  44. typedef struct config_s {
  45.     /* Set once */
  46.     FILE *out;
  47.     bool debug;
  48.     const char *name_prefix;
  49.     const char *file_prefix;
  50.     ulong file_id;        /* for uniq_last detection */
  51.     /* Updated dynamically */
  52. #define ITEM_ID_BITS 5
  53.     uint item_id;
  54.     bool any_scan_items;
  55.     bool in_res_scan;
  56.     bool in_category;        /* in_category implies in_res_scan */
  57.     const char *current_category;
  58. } config;
  59. static const config init_config =
  60. {
  61.     0,                /* out */
  62.     0,                /* debug */
  63.     DEFAULT_NAME_PREFIX,    /* name_prefix */
  64.     "",                /* file_prefix */
  65.     0,                /* file_id */
  66.     0,                /* item_id */
  67.     0,                /* any_scan_items */
  68.     0,                /* in_res_scan */
  69.     0,                /* in_category */
  70.     ""                /* current_category */
  71. };
  72.  
  73. static const char *indent_INCLUDED = "\t\t\t\t";
  74. static const char *indent_RES_SCAN = "   ";
  75. static const char *indent_category = "\t";
  76. static const char *indent_SEEN = "\t    ";
  77. static const char *indent_include = "   ";
  78. static const char *indent_scan_item = "\t";
  79. static const char *indent_item = "";
  80.  
  81. /* Forward definitions */
  82. void add_entry(P4(config *, const char *, const char *, bool));
  83.  
  84. main(int argc, char *argv[])
  85. {
  86.     config conf;
  87.     int i, j;
  88.     bool dev, append;
  89.     const char *category = INITIAL_CATEGORY;
  90.     const char *fnarg;
  91.     FILE *out;
  92.     long pos;
  93.  
  94.     conf = init_config;
  95.     /* Process command line arguments. */
  96.     for (i = 1; i < argc; i++) {
  97.     const char *arg = argv[i];
  98.  
  99.     if (*arg != '-') {
  100.         fprintf(stderr, "-d|m|a must precede non-switches.\n", arg);
  101.         exit(1);
  102.     }
  103.     switch (arg[1]) {
  104.         case 'C':        /* change directory, by analogy with make */
  105.         conf.file_prefix =
  106.             (argv[i + 1][0] == '-' ? "" : argv[i + 1]);
  107.         ++i;
  108.         continue;
  109.         case 'n':
  110.         conf.name_prefix =
  111.             (argv[i + 1][0] == '-' ? "" : argv[i + 1]);
  112.         ++i;
  113.         continue;
  114.         case 'a':
  115.         dev = false, append = true;
  116.         break;
  117.         case 'd':
  118.         dev = true, append = false;
  119.         break;
  120.         case 'm':
  121.         dev = false, append = false;
  122.         break;
  123.         case 'Z':
  124.         conf.debug = true;
  125.         continue;
  126.         default:
  127.         fprintf(stderr, "Unknown switch %s.\n", argv[i]);
  128.         exit(1);
  129.     }
  130.     break;
  131.     }
  132.     if (i == argc - 1) {
  133.     fprintf(stderr, "No output file name given, last argument is %s.\n",
  134.         argv[i]);
  135.     exit(1);
  136.     }
  137.     /* Must be the output file. */
  138.     fnarg = argv[++i];
  139.     {
  140.     char fname[100];
  141.  
  142.     strcpy(fname, fnarg);
  143.     strcat(fname, ".dev");
  144.     out = fopen(fname, (append ? "a" : "w"));
  145.     if (out == 0) {
  146.         fprintf(stderr, "Can't open %s for output.\n", fname);
  147.         exit(1);
  148.     }
  149.     if (!append)
  150.         fprintf(out,
  151.             "/*\n * File %s created automatically by gendev.\n */\n",
  152.             fname);
  153.     }
  154.     conf.out = out;
  155.     pos = ftell(out);
  156.     /* We need a separate _INCLUDED flag for each batch of definitions. */
  157.     fprintf(out, "\n#%sifndef %s_%ld_INCLUDED\n",
  158.         indent_INCLUDED, fnarg, pos);
  159.     fprintf(out, "#%s  define %s_%ld_INCLUDED\n",
  160.         indent_INCLUDED, fnarg, pos);
  161.     /* Create a "unique" hash for the output file. */
  162.     for (j = 0; fnarg[j] != 0; ++j)
  163.     conf.file_id = conf.file_id * 41 + fnarg[j];
  164.     conf.item_id <<= ITEM_ID_BITS;
  165.     /* Add the real entries. */
  166.     if (dev)
  167.     add_entry(&conf, "dev", fnarg, false);
  168.     for (j = i + 1; j < argc; ++j) {
  169.     const char *arg = argv[j];
  170.  
  171.     if (arg[0] == '-')
  172.         category = arg + 1;
  173.     else
  174.         add_entry(&conf, category, arg, false);
  175.     }
  176.     if (conf.in_category)
  177.     fprintf(out, "#%sendif /* -%s */\n",
  178.         indent_category, conf.current_category);
  179.     /* Add the scanning entries, if any. */
  180.     if (conf.any_scan_items) {
  181.     if (conf.in_res_scan)
  182.         fprintf(out, "#%selse /* RES_SCAN */\n", indent_RES_SCAN);
  183.     else
  184.         fprintf(out, "#%sifdef RES_SCAN\n", indent_RES_SCAN);
  185.     conf.in_res_scan = true;
  186.     category = INITIAL_CATEGORY;
  187.     conf.item_id = 0;
  188.     for (j = i + 1; j < argc; ++j) {
  189.         const char *arg = argv[j];
  190.  
  191.         if (arg[0] == '-')
  192.         category = arg + 1;
  193.         else
  194.         add_entry(&conf, category, arg, true);
  195.     }
  196.     }
  197.     if (conf.in_res_scan)
  198.     fprintf(out, "#%sendif /* RES_SCAN */\n", indent_RES_SCAN);
  199.     fprintf(out, "#%sendif /* !%s_%ld_INCLUDED */\n",
  200.         indent_INCLUDED, fnarg, pos);
  201.     fclose(out);
  202.     exit(0);
  203. }
  204.  
  205. /* Add an entry to the output. */
  206. typedef enum {
  207.     uniq_none = 0,
  208.     uniq_first,
  209.     uniq_last
  210. } uniq_mode;
  211. void
  212. write_item(config * pconf, const char *str, const char *category,
  213.        const char *item, uniq_mode mode)
  214. {
  215.     FILE *out = pconf->out;
  216.     char cati[80];
  217.  
  218.     if (!pconf->in_res_scan) {
  219.     fprintf(out, "#%sifndef RES_SCAN\n", indent_RES_SCAN);
  220.     pconf->in_res_scan = true;
  221.     }
  222.     if (strcmp(pconf->current_category, category)) {
  223.     const char *paren = strchr(str, '(');
  224.  
  225.     if (pconf->in_category)
  226.         fprintf(out, "#%sendif /* -%s */\n",
  227.             indent_category, pconf->current_category);
  228.     fprintf(out, "#%sifdef ", indent_category);
  229.     fwrite(str, sizeof(char), paren - str, out);
  230.  
  231.     fprintf(out, "\n");
  232.     pconf->current_category = category;
  233.     pconf->in_category = true;
  234.     }
  235.     sprintf(cati, "%s_%s_", category, item);
  236.     switch (mode) {
  237.     case uniq_none:
  238.         fprintf(out, "%s%s\n", indent_item, str);
  239.         break;
  240.     case uniq_first:
  241.         fprintf(out, "#%sifndef %sSEEN\n", indent_SEEN, cati);
  242.         fprintf(out, "#%s  define %sSEEN\n", indent_SEEN, cati);
  243.         write_item(pconf, str, category, item, uniq_none);
  244.         fprintf(out, "#%sendif\n", indent_SEEN, cati);
  245.         break;
  246.     case uniq_last:
  247.         fprintf(out, "#%sif %sSEEN == %lu\n", indent_SEEN, cati,
  248.             pconf->file_id + pconf->item_id++);
  249.         write_item(pconf, str, category, item, uniq_none);
  250.         fprintf(out, "#%sendif\n", indent_SEEN, cati);
  251.         pconf->any_scan_items = true;
  252.         break;
  253.     }
  254. }
  255. void
  256. write_scan_item(config * pconf, const char *str, const char *category,
  257.         const char *item, uniq_mode mode)
  258. {
  259.     FILE *out = pconf->out;
  260.     char cati[80];
  261.  
  262.     sprintf(cati, "%s_%s_", category, item);
  263.     switch (mode) {
  264.     case uniq_none:
  265.         break;
  266.     case uniq_first:
  267.         break;
  268.     case uniq_last:
  269.         fprintf(out, "#%sundef %sSEEN\n", indent_scan_item, cati);
  270.         fprintf(out, "#%s  define %sSEEN %lu\n", indent_scan_item, cati,
  271.             pconf->file_id + pconf->item_id++);
  272.     }
  273. }
  274. void
  275. add_entry(config * pconf, const char *category, const char *item, bool scan)
  276. {
  277.     char str[80];
  278.     uniq_mode mode = uniq_first;
  279.  
  280.     if (pconf->debug && !scan)
  281.     printf("Adding %s %s;\n", category, item);
  282.     str[0] = 0;
  283.     switch (category[0]) {    /* just an accelerator */
  284. #define is_cat(str) !strcmp(category, str)
  285.     case 'd':
  286.         if (is_cat("dev"))
  287.         sprintf(str, "device_(%s%s_device)\n",
  288.             pconf->name_prefix, item);
  289.         else if (is_cat("dev2"))
  290.         sprintf(str, "device2_(%s%s_device)\n",
  291.             pconf->name_prefix, item);
  292.         break;
  293.     case 'e':
  294.         if (is_cat("emulator"))
  295.         sprintf(str, "emulator_(\"%s\",%d)",
  296.             item, strlen(item));
  297.         break;
  298.     case 'f':
  299.         if (is_cat("font"))
  300.         sprintf(str, "font_(\"0.font_%s\",%sf_%s,zf_%s)",
  301.             item, pconf->name_prefix, item, item);
  302.         break;
  303.     case 'i':
  304.         if (is_cat("include")) {
  305.         int len = strlen(item);
  306.  
  307.         if (scan)
  308.             return;
  309.         if (strcmp(pconf->current_category, category)) {
  310.             if (pconf->in_category) {
  311.             fprintf(pconf->out, "#%sendif /* -%s */\n",
  312.                 indent_category, pconf->current_category);
  313.             pconf->in_category = false;
  314.             }
  315.             pconf->current_category = category;
  316.         }
  317.         if (pconf->in_res_scan) {
  318.             fprintf(pconf->out, "#%sendif /* RES_SCAN */\n",
  319.                 indent_RES_SCAN);
  320.             pconf->in_res_scan = false;
  321.         }
  322.         if (len < 5 || strcmp(item + len - 4, ".dev"))
  323.             fprintf(pconf->out, "#%sinclude \"%s.dev\"\n",
  324.                 indent_include, item);
  325.         else
  326.             fprintf(pconf->out, "#%sinclude \"%s\"\n",
  327.                 indent_include, item);
  328.         return;
  329.         } else if (is_cat("init"))
  330.         sprintf(str, "init_(%s%s_init)", pconf->name_prefix, item);
  331.         else if (is_cat("iodev"))
  332.         sprintf(str, "io_device_(%siodev_%s)", pconf->name_prefix, item);
  333.         break;
  334.     case 'l':
  335.         if (is_cat("lib")) {
  336.         sprintf(str, "lib_(%s)", item);
  337.         mode = uniq_last;
  338.         }
  339.         break;
  340.     case 'o':
  341.         if (is_cat("obj"))
  342.         sprintf(str, "obj_(%s%s)", pconf->file_prefix, item);
  343.         else if (is_cat("oper"))
  344.         sprintf(str, "oper_(%s_op_defs)", item);
  345.         break;
  346.     case 'p':
  347.         if (is_cat("ps"))
  348.         sprintf(str, "psfile_(\"%s.ps\",%d)",
  349.             item, strlen(item) + 3);
  350.         break;
  351. #undef is_cat
  352.     default:
  353.         ;
  354.     }
  355.     if (str[0] == 0) {
  356.     fprintf(stderr, "Unknown category %s.\n", category);
  357.     exit(1);
  358.     }
  359.     if (scan)
  360.     write_scan_item(pconf, str, category, item, mode);
  361.     else
  362.     write_item(pconf, str, category, item, mode);
  363. }
  364.