1 /* $XConsortium: makestrs.c /main/10 1996/11/22 07:27:41 kaleb $ */
5 Copyright (c) 1991 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
30 /* Constructs string definitions */
34 #ifndef X_NOT_STDC_ENV
39 #if defined(macII) && !defined(__STDC__) /* stdlib.h fails to define these */
43 typedef struct _TableEnt {
44 struct _TableEnt* next;
50 typedef struct _Table {
53 TableEnt* tableentcurrent;
54 TableEnt** tableenttail;
59 typedef struct _File {
68 static File* file = NULL;
69 static File* filecurrent = NULL;
70 static File** filetail = &file;
71 static char* conststr;
72 static char* prefixstr = NULL;
73 static char* featurestr = NULL;
74 static char* ctmplstr = NULL;
75 static char* fileprotstr;
76 static char* externrefstr;
77 static char* externdefstr;
79 #define X_DEFAULT_ABI 0
80 #define X_ARRAYPER_ABI 1
82 #define X_INTEL_ABI_BC 3
84 #define X_FUNCTION_ABI 5
86 #define X_MAGIC_STRING "<<<STRING_TABLE_GOES_HERE>>>"
88 static void WriteHeaderProlog (f, phile)
95 (void) fprintf (f, "#ifdef %s\n", featurestr);
96 for (t = phile->table; t; t = t->next)
97 for (te = t->tableent; te; te = te->next) {
98 if (strcmp (te->left, "RAtom") == 0) {
100 "#ifndef %s%s\n#define %s%s \"%s\"\n#endif\n",
101 prefixstr, te->left, prefixstr, te->left, te->right);
104 "#define %s%s \"%s\"\n",
105 prefixstr, te->left, te->right);
108 (void) fprintf (f, "%s", "#else\n");
111 static void IntelABIWriteHeader (f, phile)
118 WriteHeaderProlog (f, phile);
120 for (t = phile->table; t; t = t->next) {
121 (void) fprintf (f, "%s %sConst char %s[];\n",
122 externrefstr, conststr ? conststr : fileprotstr, t->name);
123 for (te = t->tableent; te; te = te->next)
125 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
126 prefixstr, te->left, prefixstr, te->left, t->name, te->offset);
129 (void) fprintf (f, "#endif /* %s */\n", featurestr);
132 static void SPARCABIWriteHeader (f, phile)
139 for (t = phile->table; t; t = t->next)
140 for (te = t->tableent; te; te = te->next)
141 (void) fprintf (f, "#define %s%s \"%s\"\n",
142 prefixstr, te->left, te->right);
145 static void FunctionWriteHeader (f, phile)
152 WriteHeaderProlog (f, phile);
154 (void) fprintf (f, "%s %sConst char* %s();\n",
155 externrefstr, conststr ? conststr : fileprotstr,
158 for (t = phile->table; t; t = t->next)
159 for (te = t->tableent; te; te = te->next)
161 "#ifndef %s%s\n#define %s%s (%s(%d))\n#endif\n",
162 prefixstr, te->left, prefixstr, te->left, phile->table->name,
165 (void) fprintf (f, "#endif /* %s */\n", featurestr);
168 static void ArrayperWriteHeader (f, phile)
175 WriteHeaderProlog (f, phile);
177 for (t = phile->table; t; t = t->next)
178 for (te = t->tableent; te; te = te->next)
180 "#ifndef %s%s\n%s %sConst char %s%s[];\n#endif\n",
182 externrefstr, conststr ? conststr : fileprotstr,
183 prefixstr, te->left);
185 (void) fprintf (f, "#endif /* %s */\n", featurestr);
188 static void DefaultWriteHeader (f, phile)
195 WriteHeaderProlog (f, phile);
197 (void) fprintf (f, "%s %sConst char %s[];\n",
198 externrefstr, conststr ? conststr : fileprotstr,
201 for (t = phile->table; t; t = t->next)
202 for (te = t->tableent; te; te = te->next)
204 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
205 prefixstr, te->left, prefixstr, te->left, phile->table->name,
208 (void) fprintf (f, "#endif /* %s */\n", featurestr);
211 static void CopyTmplProlog (tmpl, f)
216 static char* magic_string = X_MAGIC_STRING;
217 int magic_string_len = strlen (magic_string);
219 while (fgets (buf, sizeof buf, tmpl)) {
220 if (strncmp (buf, magic_string, magic_string_len) == 0) {
223 (void) fputs (buf, f);
227 static void CopyTmplEpilog (tmpl, f)
233 while (fgets (buf, sizeof buf, tmpl))
234 (void) fputs (buf, f);
237 static char* abistring[] = {
238 "Default", "Array per string", "Intel", "Intel BC", "SPARC", "Function" };
240 static void WriteHeader (tagline, phile, abi)
249 static void (*headerproc[])() = {
250 DefaultWriteHeader, ArrayperWriteHeader,
251 IntelABIWriteHeader, IntelABIWriteHeader,
252 SPARCABIWriteHeader, FunctionWriteHeader };
254 if ((f = fopen (phile->name, "w+")) == NULL) exit (1);
256 if (phile->tmpl) CopyTmplProlog (phile->tmpl, f);
259 "%s\n%s\n/* %s ABI version -- Do not edit */\n",
260 "/* $XConsortium: makestrs.c /main/10 1996/11/22 07:27:41 kaleb $ */",
261 "/* This file is automatically generated. */",
264 if (tagline) (void) fprintf (f, "/* %s */\n\n", tagline);
266 /* do the right thing for Motif, i.e. avoid _XmXmStrDefs_h_ */
267 if (strcmp (prefixstr, "Xm") == 0) {
268 if ((fileprotstr = malloc (strlen (phile->name) + 3)) == NULL)
270 (void) sprintf (fileprotstr, "_%s_", phile->name);
272 if ((fileprotstr = malloc (strlen (phile->name) + strlen (prefixstr) + 3)) == NULL)
274 (void) sprintf (fileprotstr, "_%s%s_", prefixstr, phile->name);
277 for (tmp = fileprotstr; *tmp; tmp++) if (*tmp == '.') *tmp = '_';
279 (*headerproc[abi])(f, phile);
281 if (phile->tmpl) CopyTmplEpilog (phile->tmpl, f);
283 (void) free (fileprotstr);
284 (void) fclose (phile->tmpl);
288 static void WriteSourceLine (te, abi, fudge)
294 for (c = te->right; *c; c++) (void) printf ("'%c',", *c);
295 (void) printf ("%c", '0');
296 if (te->next || fudge) (void) printf ("%c", ',');
297 (void) printf ("%s", "\n");
300 static char* const_string = "%s %sConst char %s[] = {\n";
302 static void IntelABIWriteSource (abi)
307 for (phile = file; phile; phile = phile->next) {
311 for (t = phile->table; t; t = t->next) {
312 (void) printf (const_string, externdefstr,
313 conststr ? conststr : "", t->name);
314 for (te = t->tableent; te; te = te->next)
315 WriteSourceLine (te, abi, 0);
316 (void) printf ("%s\n\n", "};");
321 static void IntelABIBCWriteSource (abi)
326 for (phile = file; phile; phile = phile->next) {
330 (void) printf (const_string, externdefstr,
331 conststr ? conststr : "", phile->table->name);
333 for (t = phile->table; t; t = t->next)
334 for (te = t->tableent; te; te = te->next)
335 WriteSourceLine (te, abi, t->next ? 1 : 0);
336 (void) printf ("%s\n\n", "};");
338 if (phile->table->next) {
339 (void) printf (const_string, externdefstr,
340 conststr ? conststr : "", phile->table->next->name);
341 for (t = phile->table->next; t; t = t->next)
342 for (te = t->tableent; te; te = te->next)
343 WriteSourceLine (te, abi, 0);
344 (void) printf ("%s\n\n", "};");
349 static void FunctionWriteSource (abi)
354 for (phile = file; phile; phile = phile->next) {
358 (void) printf ("static %sConst char _%s[] = {\n",
359 conststr ? conststr : "", phile->table->name);
361 for (t = phile->table; t; t = t->next)
362 for (te = t->tableent; te; te = te->next)
363 WriteSourceLine (te, abi, t->next ? 1 : 0);
364 (void) printf ("%s\n\n", "};");
366 (void) printf ("%sConst char* %s(index)\n int index;\n{\n return &_%s[index];\n}\n\n",
367 conststr ? conststr : "",
368 phile->table->name, phile->table->name);
372 static void ArrayperWriteSource (abi)
376 static int done_atom;
378 for (phile = file; phile; phile = phile->next) {
382 for (t = phile->table; t; t = t->next)
383 for (te = t->tableent; te; te = te->next) {
384 if (strcmp (te->left, "RAtom") == 0) {
385 if (done_atom) return;
388 (void) printf ("%s %sConst char %s%s[] = \"%s\";\n",
389 externdefstr, conststr ? conststr : prefixstr,
390 te->left, te->right);
395 static void DefaultWriteSource (abi)
400 for (phile = file; phile; phile = phile->next) {
404 (void) printf (const_string, externdefstr, conststr ? conststr : "",
407 for (t = phile->table; t; t = t->next)
408 for (te = t->tableent; te; te = te->next)
409 WriteSourceLine (te, abi, t->next ? 1 : 0);
410 (void) printf ("%s\n\n", "};");
414 static void WriteSource(tagline, abi)
418 static void (*sourceproc[])() = {
419 DefaultWriteSource, ArrayperWriteSource,
420 IntelABIWriteSource, IntelABIBCWriteSource,
421 DefaultWriteSource, FunctionWriteSource };
426 tmpl = fopen (ctmplstr, "r");
428 if (tmpl) CopyTmplProlog (tmpl, stdout);
430 (void) fprintf (stderr, "Expected template %s, not found\n",
438 (void) printf ("%s\n%s\n/* %s ABI version -- Do not edit */\n",
439 "/* $XConsortium: makestrs.c /main/10 1996/11/22 07:27:41 kaleb $ */",
440 "/* This file is automatically generated. */",
443 if (tagline) (void) printf ("/* %s */\n\n", tagline);
445 (*sourceproc[abi])(abi);
447 if (tmpl) CopyTmplEpilog (tmpl, stdout);
450 static void DoLine(buf)
454 #define X_FILE_TOKEN 1
455 #define X_TABLE_TOKEN 2
456 #define X_PREFIX_TOKEN 3
457 #define X_FEATURE_TOKEN 4
458 #define X_EXTERNREF_TOKEN 5
459 #define X_EXTERNDEF_TOKEN 6
460 #define X_CTMPL_TOKEN 7
461 #define X_HTMPL_TOKEN 8
462 #define X_CONST_TOKEN 9
466 static char* file_str = "#file";
467 static char* table_str = "#table";
468 static char* prefix_str = "#prefix";
469 static char* feature_str = "#feature";
470 static char* externref_str = "#externref";
471 static char* externdef_str = "#externdef";
472 static char* ctmpl_str = "#ctmpl";
473 static char* htmpl_str = "#htmpl";
474 static char* const_str = "#const";
476 if (strncmp (buf, file_str, strlen (file_str)) == 0)
477 token = X_FILE_TOKEN;
478 else if (strncmp (buf, table_str, strlen (table_str)) == 0)
479 token = X_TABLE_TOKEN;
480 else if (strncmp (buf, prefix_str, strlen (prefix_str)) == 0)
481 token = X_PREFIX_TOKEN;
482 else if (strncmp (buf, feature_str, strlen (feature_str)) == 0)
483 token = X_FEATURE_TOKEN;
484 else if (strncmp (buf, externref_str, strlen (externref_str)) == 0)
485 token = X_EXTERNREF_TOKEN;
486 else if (strncmp (buf, externdef_str, strlen (externdef_str)) == 0)
487 token = X_EXTERNDEF_TOKEN;
488 else if (strncmp (buf, ctmpl_str, strlen (ctmpl_str)) == 0)
489 token = X_CTMPL_TOKEN;
490 else if (strncmp (buf, htmpl_str, strlen (htmpl_str)) == 0)
491 token = X_HTMPL_TOKEN;
492 else if (strncmp (buf, const_str, strlen (const_str)) == 0)
493 token = X_CONST_TOKEN;
502 if ((phile = (File*) malloc (sizeof(File))) == NULL)
504 if ((phile->name = malloc (strlen (buf + strlen (file_str)) + 1)) == NULL)
506 (void) strcpy (phile->name, buf + strlen (file_str) + 1);
508 phile->tablecurrent = NULL;
509 phile->tabletail = &phile->table;
514 filetail = &phile->next;
521 if ((table = (Table*) malloc (sizeof(Table))) == NULL)
523 if ((table->name = malloc (strlen (buf + strlen (table_str)) + 1)) == NULL)
525 (void) strcpy (table->name, buf + strlen (table_str) + 1);
526 table->tableent = NULL;
527 table->tableentcurrent = NULL;
528 table->tableenttail = &table->tableent;
532 *filecurrent->tabletail = table;
533 filecurrent->tabletail = &table->next;
534 filecurrent->tablecurrent = table;
538 if ((prefixstr = malloc (strlen (buf + strlen (prefix_str)) + 1)) == NULL)
540 (void) strcpy (prefixstr, buf + strlen (prefix_str) + 1);
542 case X_FEATURE_TOKEN:
543 if ((featurestr = malloc (strlen (buf + strlen (feature_str)) + 1)) == NULL)
545 (void) strcpy (featurestr, buf + strlen (feature_str) + 1);
547 case X_EXTERNREF_TOKEN:
548 if ((externrefstr = malloc (strlen (buf + strlen (externref_str)) + 1)) == NULL)
550 (void) strcpy (externrefstr, buf + strlen (externref_str) + 1);
552 case X_EXTERNDEF_TOKEN:
553 if ((externdefstr = malloc (strlen (buf + strlen (externdef_str)) + 1)) == NULL)
555 (void) strcpy (externdefstr, buf + strlen (externdef_str) + 1);
558 if ((ctmplstr = malloc (strlen (buf + strlen (ctmpl_str)) + 1)) == NULL)
560 (void) strcpy (ctmplstr, buf + strlen (ctmpl_str) + 1);
563 if ((filecurrent->tmpl = fopen (buf + strlen (htmpl_str) + 1, "r")) == NULL) {
564 (void) fprintf (stderr,
565 "Expected template %s, not found\n", htmpl_str);
570 if ((conststr = malloc (strlen (buf + strlen (const_str)) + 1)) == NULL)
572 (void) strcpy (conststr, buf + strlen (const_str) + 1);
582 if (right = index(buf, ' '))
587 strcpy (lbuf, prefixstr);
588 strcat (lbuf, right);
592 llen = len = strlen(buf) + 1;
593 rlen = strlen(right) + 1;
594 if (right != buf + 1) len += rlen;
595 if ((tableent = (TableEnt*)malloc(sizeof(TableEnt) + len)) == NULL)
597 tableent->left = (char *)(tableent + 1);
598 strcpy(tableent->left, buf);
600 tableent->right = tableent->left + llen;
601 strcpy(tableent->right, right);
603 tableent->right = tableent->left + 1;
605 tableent->next = NULL;
607 *filecurrent->tablecurrent->tableenttail = tableent;
608 filecurrent->tablecurrent->tableenttail = &tableent->next;
609 filecurrent->tablecurrent->tableentcurrent = tableent;
615 static void IntelABIIndexEntries (file)
621 for (t = file->table; t; t = t->next)
622 for (te = t->tableent; te; te = te->next) {
623 te->offset = t->offset;
624 t->offset += strlen (te->right);
629 static void DefaultIndexEntries (file)
636 for (t = file->table; t; t = t->next)
637 for (te = t->tableent; te; te = te->next) {
639 offset += strlen (te->right);
644 static void IndexEntries (file,abi)
653 IntelABIIndexEntries (file);
656 DefaultIndexEntries (file);
661 static char* DoComment (line)
669 /* assume that the first line with two '$' in it is the RCS tag line */
670 if ((tag = index (line, '$')) == NULL) return NULL;
671 if ((eol = index (tag + 1, '$')) == NULL) return NULL;
673 if ((ret = malloc (len)) == NULL)
675 (void) strncpy (ret, tag + 1, len - 1);
685 char* tagline = NULL;
698 for (i = 1; i < argc; i++) {
699 if (strcmp (argv[i], "-f") == 0) {
701 f = fopen (argv[i], "r");
705 if (strcmp (argv[i], "-sparcabi") == 0)
707 if (strcmp (argv[i], "-intelabi") == 0)
709 if (strcmp (argv[i], "-functionabi") == 0)
710 abi = X_FUNCTION_ABI;
711 if (strcmp (argv[i], "-earlyR6bc") == 0 && abi == X_INTEL_ABI)
712 abi = X_INTEL_ABI_BC;
713 if (strcmp (argv[i], "-arrayperabi") == 0)
714 abi = X_ARRAYPER_ABI;
716 if (strcmp (argv[i], "-defaultabi") == 0)
722 if (f == NULL) return 1;
723 while (fgets(buf, sizeof buf, f)) {
724 if (!buf[0] || buf[0] == '\n')
727 if (tagline) continue;
728 tagline = DoComment (buf);
731 if (buf[(len = strlen (buf) - 1)] == '\n') buf[len] = '\0';
734 for (phile = file; phile; phile = phile->next) {
735 if (abi != X_ARRAYPER_ABI) IndexEntries (phile, abi);
736 WriteHeader (tagline, phile, abi);
738 WriteSource(tagline, abi);