gendev.c 9.8 KB

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