2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */
27 Copyright (c) 1991, 1998 The Open Group
31 The above copyright notice and this permission notice shall be included in
32 all copies or substantial portions of the Software.
34 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
38 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 Except as contained in this notice, the name of The Open Group shall not be
42 used in advertising or otherwise to promote the sale, use or other dealings
43 in this Software without prior written authorization from The Open Group.
47 /* Constructs string definitions */
51 #ifndef X_NOT_STDC_ENV
56 #if defined(macII) && !defined(__STDC__) /* stdlib.h fails to define these */
60 typedef struct _TableEnt {
61 struct _TableEnt* next;
67 typedef struct _Table {
70 TableEnt* tableentcurrent;
71 TableEnt** tableenttail;
76 typedef struct _File {
85 static File* file = NULL;
86 static File* filecurrent = NULL;
87 static File** filetail = &file;
88 static char* conststr;
89 static char* prefixstr = NULL;
90 static char* featurestr = NULL;
91 static char* ctmplstr = NULL;
92 static char* fileprotstr;
93 static char* externrefstr;
94 static char* externdefstr;
96 #define X_DEFAULT_ABI 0
97 #define X_ARRAYPER_ABI 1
99 #define X_INTEL_ABI_BC 3
100 #define X_SPARC_ABI 4
101 #define X_FUNCTION_ABI 5
103 #define X_MAGIC_STRING "<<<STRING_TABLE_GOES_HERE>>>"
105 static void WriteHeaderProlog (f, phile)
112 (void) fprintf (f, "#ifdef %s\n", featurestr);
113 for (t = phile->table; t; t = t->next)
114 for (te = t->tableent; te; te = te->next) {
115 if (strcmp (te->left, "RAtom") == 0) {
117 "#ifndef %s%s\n#define %s%s \"%s\"\n#endif\n",
118 prefixstr, te->left, prefixstr, te->left, te->right);
121 "#define %s%s \"%s\"\n",
122 prefixstr, te->left, te->right);
125 (void) fprintf (f, "%s", "#else\n");
128 static void IntelABIWriteHeader (f, phile)
135 WriteHeaderProlog (f, phile);
137 for (t = phile->table; t; t = t->next) {
138 (void) fprintf (f, "%s %sConst char %s[];\n",
139 externrefstr, conststr ? conststr : fileprotstr, t->name);
140 for (te = t->tableent; te; te = te->next)
142 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
143 prefixstr, te->left, prefixstr, te->left, t->name, te->offset);
146 (void) fprintf (f, "#endif /* %s */\n", featurestr);
149 static void SPARCABIWriteHeader (f, phile)
156 for (t = phile->table; t; t = t->next)
157 for (te = t->tableent; te; te = te->next)
158 (void) fprintf (f, "#define %s%s \"%s\"\n",
159 prefixstr, te->left, te->right);
162 static void FunctionWriteHeader (f, phile)
169 WriteHeaderProlog (f, phile);
171 (void) fprintf (f, "%s %sConst char* %s();\n",
172 externrefstr, conststr ? conststr : fileprotstr,
175 for (t = phile->table; t; t = t->next)
176 for (te = t->tableent; te; te = te->next)
178 "#ifndef %s%s\n#define %s%s (%s(%d))\n#endif\n",
179 prefixstr, te->left, prefixstr, te->left, phile->table->name,
182 (void) fprintf (f, "#endif /* %s */\n", featurestr);
185 static void ArrayperWriteHeader (f, phile)
192 WriteHeaderProlog (f, phile);
194 for (t = phile->table; t; t = t->next)
195 for (te = t->tableent; te; te = te->next)
197 "#ifndef %s%s\n%s %sConst char %s%s[];\n#endif\n",
199 externrefstr, conststr ? conststr : fileprotstr,
200 prefixstr, te->left);
202 (void) fprintf (f, "#endif /* %s */\n", featurestr);
205 static void DefaultWriteHeader (f, phile)
212 WriteHeaderProlog (f, phile);
214 (void) fprintf (f, "%s %sConst char %s[];\n",
215 externrefstr, conststr ? conststr : fileprotstr,
218 for (t = phile->table; t; t = t->next)
219 for (te = t->tableent; te; te = te->next)
221 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
222 prefixstr, te->left, prefixstr, te->left, phile->table->name,
225 (void) fprintf (f, "#endif /* %s */\n", featurestr);
228 static void CopyTmplProlog (tmpl, f)
233 static char* magic_string = X_MAGIC_STRING;
234 int magic_string_len = strlen (magic_string);
236 while (fgets (buf, sizeof buf, tmpl)) {
237 if (strncmp (buf, magic_string, magic_string_len) == 0) {
240 (void) fputs (buf, f);
244 static void CopyTmplEpilog (tmpl, f)
250 while (fgets (buf, sizeof buf, tmpl))
251 (void) fputs (buf, f);
254 static char* abistring[] = {
255 "Default", "Array per string", "Intel", "Intel BC", "SPARC", "Function" };
257 static void WriteHeader (tagline, phile, abi)
266 static void (*headerproc[])() = {
267 DefaultWriteHeader, ArrayperWriteHeader,
268 IntelABIWriteHeader, IntelABIWriteHeader,
269 SPARCABIWriteHeader, FunctionWriteHeader };
271 if ((f = fopen (phile->name, "w+")) == NULL) exit (1);
273 if (phile->tmpl) CopyTmplProlog (phile->tmpl, f);
276 "%s\n%s\n/* %s ABI version -- Do not edit */\n",
277 "/* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */",
278 "/* This file is automatically generated. */",
281 if (tagline) (void) fprintf (f, "/* %s */\n\n", tagline);
283 /* do the right thing for Motif, i.e. avoid _XmXmStrDefs_h_ */
284 if (strcmp (prefixstr, "Xm") == 0) {
285 if ((fileprotstr = malloc (strlen (phile->name) + 3)) == NULL)
287 (void) snprintf (fileprotstr, strlen (phile->name) + 3,
288 "_%s_", phile->name);
290 if ((fileprotstr = malloc (strlen (phile->name) + strlen (prefixstr) + 3)) == NULL)
292 (void) snprintf (fileprotstr,
293 strlen (phile->name) + strlen (prefixstr) + 3,
294 "_%s%s_", prefixstr, phile->name);
297 for (tmp = fileprotstr; *tmp; tmp++) if (*tmp == '.') *tmp = '_';
299 (*headerproc[abi])(f, phile);
301 if (phile->tmpl) CopyTmplEpilog (phile->tmpl, f);
303 (void) free (fileprotstr);
304 (void) fclose (phile->tmpl);
308 static void WriteSourceLine (te, abi, fudge)
315 for (c = te->right; *c; c++) (void) printf ("'%c',", *c);
316 (void) printf ("%c", '0');
317 if (te->next || fudge) (void) printf ("%c", ',');
318 (void) printf ("%s", "\n");
321 static char* const_string = "%s %sConst char %s[] = {\n";
323 static void IntelABIWriteSource (abi)
328 for (phile = file; phile; phile = phile->next) {
332 for (t = phile->table; t; t = t->next) {
333 (void) printf (const_string, externdefstr,
334 conststr ? conststr : "", t->name);
335 for (te = t->tableent; te; te = te->next)
336 WriteSourceLine (te, abi, 0);
337 (void) printf ("%s\n\n", "};");
342 static void IntelABIBCWriteSource (abi)
347 for (phile = file; phile; phile = phile->next) {
351 (void) printf (const_string, externdefstr,
352 conststr ? conststr : "", phile->table->name);
354 for (t = phile->table; t; t = t->next)
355 for (te = t->tableent; te; te = te->next)
356 WriteSourceLine (te, abi, t->next ? 1 : 0);
357 (void) printf ("%s\n\n", "};");
359 if (phile->table->next) {
360 (void) printf (const_string, externdefstr,
361 conststr ? conststr : "", phile->table->next->name);
362 for (t = phile->table->next; t; t = t->next)
363 for (te = t->tableent; te; te = te->next)
364 WriteSourceLine (te, abi, 0);
365 (void) printf ("%s\n\n", "};");
370 static void FunctionWriteSource (abi)
375 for (phile = file; phile; phile = phile->next) {
379 (void) printf ("static %sConst char _%s[] = {\n",
380 conststr ? conststr : "", phile->table->name);
382 for (t = phile->table; t; t = t->next)
383 for (te = t->tableent; te; te = te->next)
384 WriteSourceLine (te, abi, t->next ? 1 : 0);
385 (void) printf ("%s\n\n", "};");
387 (void) printf ("%sConst char* %s(index)\n int index;\n{\n return &_%s[index];\n}\n\n",
388 conststr ? conststr : "",
389 phile->table->name, phile->table->name);
393 static void ArrayperWriteSource (abi)
397 static int done_atom;
399 for (phile = file; phile; phile = phile->next) {
403 for (t = phile->table; t; t = t->next)
404 for (te = t->tableent; te; te = te->next) {
405 if (strcmp (te->left, "RAtom") == 0) {
406 if (done_atom) return;
409 (void) printf ("%s %sConst char %s%s[] = \"%s\";\n",
410 externdefstr, conststr ? conststr : prefixstr,
411 prefixstr, te->left, te->right);
416 static void DefaultWriteSource (abi)
421 for (phile = file; phile; phile = phile->next) {
425 (void) printf (const_string, externdefstr, conststr ? conststr : "",
428 for (t = phile->table; t; t = t->next)
429 for (te = t->tableent; te; te = te->next)
430 WriteSourceLine (te, abi, t->next ? 1 : 0);
431 (void) printf ("%s\n\n", "};");
435 static void WriteSource(tagline, abi)
439 static void (*sourceproc[])() = {
440 DefaultWriteSource, ArrayperWriteSource,
441 IntelABIWriteSource, IntelABIBCWriteSource,
442 DefaultWriteSource, FunctionWriteSource };
447 tmpl = fopen (ctmplstr, "r");
449 if (tmpl) CopyTmplProlog (tmpl, stdout);
451 (void) fprintf (stderr, "Expected template %s, not found\n",
459 (void) printf ("%s\n%s\n/* %s ABI version -- Do not edit */\n",
460 "/* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */",
461 "/* This file is automatically generated. */",
464 if (tagline) (void) printf ("/* %s */\n\n", tagline);
466 (*sourceproc[abi])(abi);
468 if (tmpl) CopyTmplEpilog (tmpl, stdout);
471 static void DoLine(buf)
475 #define X_FILE_TOKEN 1
476 #define X_TABLE_TOKEN 2
477 #define X_PREFIX_TOKEN 3
478 #define X_FEATURE_TOKEN 4
479 #define X_EXTERNREF_TOKEN 5
480 #define X_EXTERNDEF_TOKEN 6
481 #define X_CTMPL_TOKEN 7
482 #define X_HTMPL_TOKEN 8
483 #define X_CONST_TOKEN 9
487 static char* file_str = "#file";
488 static char* table_str = "#table";
489 static char* prefix_str = "#prefix";
490 static char* feature_str = "#feature";
491 static char* externref_str = "#externref";
492 static char* externdef_str = "#externdef";
493 static char* ctmpl_str = "#ctmpl";
494 static char* htmpl_str = "#htmpl";
495 static char* const_str = "#const";
497 if (strncmp (buf, file_str, strlen (file_str)) == 0)
498 token = X_FILE_TOKEN;
499 else if (strncmp (buf, table_str, strlen (table_str)) == 0)
500 token = X_TABLE_TOKEN;
501 else if (strncmp (buf, prefix_str, strlen (prefix_str)) == 0)
502 token = X_PREFIX_TOKEN;
503 else if (strncmp (buf, feature_str, strlen (feature_str)) == 0)
504 token = X_FEATURE_TOKEN;
505 else if (strncmp (buf, externref_str, strlen (externref_str)) == 0)
506 token = X_EXTERNREF_TOKEN;
507 else if (strncmp (buf, externdef_str, strlen (externdef_str)) == 0)
508 token = X_EXTERNDEF_TOKEN;
509 else if (strncmp (buf, ctmpl_str, strlen (ctmpl_str)) == 0)
510 token = X_CTMPL_TOKEN;
511 else if (strncmp (buf, htmpl_str, strlen (htmpl_str)) == 0)
512 token = X_HTMPL_TOKEN;
513 else if (strncmp (buf, const_str, strlen (const_str)) == 0)
514 token = X_CONST_TOKEN;
523 if ((phile = (File*) malloc (sizeof(File))) == NULL)
525 if ((phile->name = malloc (strlen (buf + strlen (file_str)) + 1)) == NULL)
527 (void) strncpy (phile->name, buf + strlen (file_str) + 1,
528 strlen (buf + strlen (file_str)) + 1);
530 phile->tablecurrent = NULL;
531 phile->tabletail = &phile->table;
536 filetail = &phile->next;
543 if ((table = (Table*) malloc (sizeof(Table))) == NULL)
545 if ((table->name = malloc (strlen (buf + strlen (table_str)) + 1)) == NULL)
547 (void) strncpy (table->name, buf + strlen (table_str) + 1,
548 strlen (buf + strlen (table_str)) + 1);
549 table->tableent = NULL;
550 table->tableentcurrent = NULL;
551 table->tableenttail = &table->tableent;
555 *filecurrent->tabletail = table;
556 filecurrent->tabletail = &table->next;
557 filecurrent->tablecurrent = table;
561 if ((prefixstr = malloc (strlen (buf + strlen (prefix_str)) + 1)) == NULL)
563 (void) strncpy (prefixstr, buf + strlen (prefix_str) + 1,
564 strlen (buf + strlen (prefix_str)) + 1);
566 case X_FEATURE_TOKEN:
567 if ((featurestr = malloc (strlen (buf + strlen (feature_str)) + 1)) == NULL)
569 (void) strncpy (featurestr, buf + strlen (feature_str) + 1,
570 strlen (buf + strlen (feature_str)) + 1);
572 case X_EXTERNREF_TOKEN:
573 if ((externrefstr = malloc (strlen (buf + strlen (externref_str)) + 1)) == NULL)
575 (void) strncpy (externrefstr, buf + strlen (externref_str) + 1,
576 strlen (buf + strlen (externref_str)) + 1);
578 case X_EXTERNDEF_TOKEN:
579 if ((externdefstr = malloc (strlen (buf + strlen (externdef_str)) + 1)) == NULL)
581 (void) strncpy (externdefstr, buf + strlen (externdef_str) + 1,
582 strlen (buf + strlen (externdef_str)) + 1);
585 if ((ctmplstr = malloc (strlen (buf + strlen (ctmpl_str)) + 1)) == NULL)
587 (void) strncpy (ctmplstr, buf + strlen (ctmpl_str) + 1,
588 strlen (buf + strlen (ctmpl_str)) + 1);
591 if ((filecurrent->tmpl = fopen (buf + strlen (htmpl_str) + 1, "r")) == NULL) {
592 (void) fprintf (stderr,
593 "Expected template %s, not found\n", htmpl_str);
598 if ((conststr = malloc (strlen (buf + strlen (const_str)) + 1)) == NULL)
600 (void) strncpy (conststr, buf + strlen (const_str) + 1,
601 strlen (buf + strlen (const_str)) + 1);
611 if ((right = index(buf, ' ')))
616 strncpy (lbuf, prefixstr, 1024);
617 strncat (lbuf, right, strlen(right));
621 llen = len = strlen(buf) + 1;
622 rlen = strlen(right) + 1;
623 if (right != buf + 1) len += rlen;
624 if ((tableent = (TableEnt*)malloc(sizeof(TableEnt) + len)) == NULL)
626 tableent->left = (char *)(tableent + 1);
627 strncpy(tableent->left, buf, llen);
629 tableent->right = tableent->left + llen;
630 strncpy(tableent->right, right, rlen);
632 tableent->right = tableent->left + 1;
634 tableent->next = NULL;
636 *filecurrent->tablecurrent->tableenttail = tableent;
637 filecurrent->tablecurrent->tableenttail = &tableent->next;
638 filecurrent->tablecurrent->tableentcurrent = tableent;
644 static void IntelABIIndexEntries (file)
650 for (t = file->table; t; t = t->next)
651 for (te = t->tableent; te; te = te->next) {
652 te->offset = t->offset;
653 t->offset += strlen (te->right);
658 static void DefaultIndexEntries (file)
665 for (t = file->table; t; t = t->next)
666 for (te = t->tableent; te; te = te->next) {
668 offset += strlen (te->right);
673 static void IndexEntries (file,abi)
682 IntelABIIndexEntries (file);
685 DefaultIndexEntries (file);
690 static char* DoComment (line)
698 /* assume that the first line with two '$' in it is the RCS tag line */
699 if ((tag = index (line, '$')) == NULL) return NULL;
700 if ((eol = index (tag + 1, '$')) == NULL) return NULL;
702 if ((ret = malloc (len)) == NULL)
704 (void) strncpy (ret, tag + 1, len - 1);
714 char* tagline = NULL;
727 for (i = 1; i < argc; i++) {
728 if (strcmp (argv[i], "-f") == 0) {
730 f = fopen (argv[i], "r");
734 if (strcmp (argv[i], "-sparcabi") == 0)
736 if (strcmp (argv[i], "-intelabi") == 0)
738 if (strcmp (argv[i], "-functionabi") == 0)
739 abi = X_FUNCTION_ABI;
740 if (strcmp (argv[i], "-earlyR6bc") == 0 && abi == X_INTEL_ABI)
741 abi = X_INTEL_ABI_BC;
742 if (strcmp (argv[i], "-arrayperabi") == 0)
743 abi = X_ARRAYPER_ABI;
745 if (strcmp (argv[i], "-defaultabi") == 0)
751 if (f == NULL) return 1;
752 while (fgets(buf, sizeof buf, f)) {
753 if (!buf[0] || buf[0] == '\n')
756 if (tagline) continue;
757 tagline = DoComment (buf);
760 if (buf[(len = strlen (buf) - 1)] == '\n') buf[len] = '\0';
763 for (phile = file; phile; phile = phile->next) {
764 if (abi != X_ARRAYPER_ABI) IndexEntries (phile, abi);
765 WriteHeader (tagline, phile, abi);
767 WriteSource(tagline, abi);