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 libraries 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
57 typedef struct _TableEnt {
58 struct _TableEnt* next;
64 typedef struct _Table {
67 TableEnt* tableentcurrent;
68 TableEnt** tableenttail;
73 typedef struct _File {
82 static File* file = NULL;
83 static File* filecurrent = NULL;
84 static File** filetail = &file;
85 static char* conststr;
86 static char* prefixstr = NULL;
87 static char* featurestr = NULL;
88 static char* ctmplstr = NULL;
89 static char* fileprotstr;
90 static char* externrefstr;
91 static char* externdefstr;
93 #define X_DEFAULT_ABI 0
94 #define X_ARRAYPER_ABI 1
96 #define X_INTEL_ABI_BC 3
98 #define X_FUNCTION_ABI 5
100 #define X_MAGIC_STRING "<<<STRING_TABLE_GOES_HERE>>>"
102 static void WriteHeaderProlog (f, phile)
109 (void) fprintf (f, "#ifdef %s\n", featurestr);
110 for (t = phile->table; t; t = t->next)
111 for (te = t->tableent; te; te = te->next) {
112 if (strcmp (te->left, "RAtom") == 0) {
114 "#ifndef %s%s\n#define %s%s \"%s\"\n#endif\n",
115 prefixstr, te->left, prefixstr, te->left, te->right);
118 "#define %s%s \"%s\"\n",
119 prefixstr, te->left, te->right);
122 (void) fprintf (f, "%s", "#else\n");
125 static void IntelABIWriteHeader (f, phile)
132 WriteHeaderProlog (f, phile);
134 for (t = phile->table; t; t = t->next) {
135 (void) fprintf (f, "%s %sConst char %s[];\n",
136 externrefstr, conststr ? conststr : fileprotstr, t->name);
137 for (te = t->tableent; te; te = te->next)
139 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
140 prefixstr, te->left, prefixstr, te->left, t->name, te->offset);
143 (void) fprintf (f, "#endif /* %s */\n", featurestr);
146 static void SPARCABIWriteHeader (f, phile)
153 for (t = phile->table; t; t = t->next)
154 for (te = t->tableent; te; te = te->next)
155 (void) fprintf (f, "#define %s%s \"%s\"\n",
156 prefixstr, te->left, te->right);
159 static void FunctionWriteHeader (f, phile)
166 WriteHeaderProlog (f, phile);
168 (void) fprintf (f, "%s %sConst char* %s();\n",
169 externrefstr, conststr ? conststr : fileprotstr,
172 for (t = phile->table; t; t = t->next)
173 for (te = t->tableent; te; te = te->next)
175 "#ifndef %s%s\n#define %s%s (%s(%d))\n#endif\n",
176 prefixstr, te->left, prefixstr, te->left, phile->table->name,
179 (void) fprintf (f, "#endif /* %s */\n", featurestr);
182 static void ArrayperWriteHeader (f, phile)
189 WriteHeaderProlog (f, phile);
191 for (t = phile->table; t; t = t->next)
192 for (te = t->tableent; te; te = te->next)
194 "#ifndef %s%s\n%s %sConst char %s%s[];\n#endif\n",
196 externrefstr, conststr ? conststr : fileprotstr,
197 prefixstr, te->left);
199 (void) fprintf (f, "#endif /* %s */\n", featurestr);
202 static void DefaultWriteHeader (f, phile)
209 WriteHeaderProlog (f, phile);
211 (void) fprintf (f, "%s %sConst char %s[];\n",
212 externrefstr, conststr ? conststr : fileprotstr,
215 for (t = phile->table; t; t = t->next)
216 for (te = t->tableent; te; te = te->next)
218 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
219 prefixstr, te->left, prefixstr, te->left, phile->table->name,
222 (void) fprintf (f, "#endif /* %s */\n", featurestr);
225 static void CopyTmplProlog (tmpl, f)
230 static char* magic_string = X_MAGIC_STRING;
231 int magic_string_len = strlen (magic_string);
233 while (fgets (buf, sizeof buf, tmpl)) {
234 if (strncmp (buf, magic_string, magic_string_len) == 0) {
237 (void) fputs (buf, f);
241 static void CopyTmplEpilog (tmpl, f)
247 while (fgets (buf, sizeof buf, tmpl))
248 (void) fputs (buf, f);
251 static char* abistring[] = {
252 "Default", "Array per string", "Intel", "Intel BC", "SPARC", "Function" };
254 static void WriteHeader (tagline, phile, abi)
263 static void (*headerproc[])() = {
264 DefaultWriteHeader, ArrayperWriteHeader,
265 IntelABIWriteHeader, IntelABIWriteHeader,
266 SPARCABIWriteHeader, FunctionWriteHeader };
268 if ((f = fopen (phile->name, "w+")) == NULL) exit (1);
270 if (phile->tmpl) CopyTmplProlog (phile->tmpl, f);
273 "%s\n%s\n/* %s ABI version -- Do not edit */\n",
274 "/* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */",
275 "/* This file is automatically generated. */",
278 if (tagline) (void) fprintf (f, "/* %s */\n\n", tagline);
280 /* do the right thing for Motif, i.e. avoid _XmXmStrDefs_h_ */
281 if (strcmp (prefixstr, "Xm") == 0) {
282 if ((fileprotstr = malloc (strlen (phile->name) + 3)) == NULL)
284 (void) snprintf (fileprotstr, strlen (phile->name) + 3,
285 "_%s_", phile->name);
287 if ((fileprotstr = malloc (strlen (phile->name) + strlen (prefixstr) + 3)) == NULL)
289 (void) snprintf (fileprotstr,
290 strlen (phile->name) + strlen (prefixstr) + 3,
291 "_%s%s_", prefixstr, phile->name);
294 for (tmp = fileprotstr; *tmp; tmp++) if (*tmp == '.') *tmp = '_';
296 (*headerproc[abi])(f, phile);
299 CopyTmplEpilog (phile->tmpl, f);
300 (void) fclose (phile->tmpl);
303 (void) free (fileprotstr);
307 static void WriteSourceLine (te, abi, fudge)
314 for (c = te->right; *c; c++) (void) printf ("'%c',", *c);
315 (void) printf ("%c", '0');
316 if (te->next || fudge) (void) printf ("%c", ',');
317 (void) printf ("%s", "\n");
320 static char* const_string = "%s %sConst char %s[] = {\n";
322 static void IntelABIWriteSource (abi)
327 for (phile = file; phile; phile = phile->next) {
331 for (t = phile->table; t; t = t->next) {
332 (void) printf (const_string, externdefstr,
333 conststr ? conststr : "", t->name);
334 for (te = t->tableent; te; te = te->next)
335 WriteSourceLine (te, abi, 0);
336 (void) printf ("%s\n\n", "};");
341 static void IntelABIBCWriteSource (abi)
346 for (phile = file; phile; phile = phile->next) {
350 (void) printf (const_string, externdefstr,
351 conststr ? conststr : "", phile->table->name);
353 for (t = phile->table; t; t = t->next)
354 for (te = t->tableent; te; te = te->next)
355 WriteSourceLine (te, abi, t->next ? 1 : 0);
356 (void) printf ("%s\n\n", "};");
358 if (phile->table->next) {
359 (void) printf (const_string, externdefstr,
360 conststr ? conststr : "", phile->table->next->name);
361 for (t = phile->table->next; t; t = t->next)
362 for (te = t->tableent; te; te = te->next)
363 WriteSourceLine (te, abi, 0);
364 (void) printf ("%s\n\n", "};");
369 static void FunctionWriteSource (abi)
374 for (phile = file; phile; phile = phile->next) {
378 (void) printf ("static %sConst char _%s[] = {\n",
379 conststr ? conststr : "", phile->table->name);
381 for (t = phile->table; t; t = t->next)
382 for (te = t->tableent; te; te = te->next)
383 WriteSourceLine (te, abi, t->next ? 1 : 0);
384 (void) printf ("%s\n\n", "};");
386 (void) printf ("%sConst char* %s(index)\n int index;\n{\n return &_%s[index];\n}\n\n",
387 conststr ? conststr : "",
388 phile->table->name, phile->table->name);
392 static void ArrayperWriteSource (abi)
396 static int done_atom;
398 for (phile = file; phile; phile = phile->next) {
402 for (t = phile->table; t; t = t->next)
403 for (te = t->tableent; te; te = te->next) {
404 if (strcmp (te->left, "RAtom") == 0) {
405 if (done_atom) return;
408 (void) printf ("%s %sConst char %s%s[] = \"%s\";\n",
409 externdefstr, conststr ? conststr : prefixstr,
410 prefixstr, te->left, te->right);
415 static void DefaultWriteSource (abi)
420 for (phile = file; phile; phile = phile->next) {
424 (void) printf (const_string, externdefstr, conststr ? conststr : "",
427 for (t = phile->table; t; t = t->next)
428 for (te = t->tableent; te; te = te->next)
429 WriteSourceLine (te, abi, t->next ? 1 : 0);
430 (void) printf ("%s\n\n", "};");
434 static void WriteSource(tagline, abi)
438 static void (*sourceproc[])() = {
439 DefaultWriteSource, ArrayperWriteSource,
440 IntelABIWriteSource, IntelABIBCWriteSource,
441 DefaultWriteSource, FunctionWriteSource };
446 tmpl = fopen (ctmplstr, "r");
448 if (tmpl) CopyTmplProlog (tmpl, stdout);
450 (void) fprintf (stderr, "Expected template %s, not found\n",
458 (void) printf ("%s\n%s\n/* %s ABI version -- Do not edit */\n",
459 "/* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */",
460 "/* This file is automatically generated. */",
463 if (tagline) (void) printf ("/* %s */\n\n", tagline);
465 (*sourceproc[abi])(abi);
468 CopyTmplEpilog (tmpl, stdout);
473 static void DoLine(buf)
477 #define X_FILE_TOKEN 1
478 #define X_TABLE_TOKEN 2
479 #define X_PREFIX_TOKEN 3
480 #define X_FEATURE_TOKEN 4
481 #define X_EXTERNREF_TOKEN 5
482 #define X_EXTERNDEF_TOKEN 6
483 #define X_CTMPL_TOKEN 7
484 #define X_HTMPL_TOKEN 8
485 #define X_CONST_TOKEN 9
489 static char* file_str = "#file";
490 static char* table_str = "#table";
491 static char* prefix_str = "#prefix";
492 static char* feature_str = "#feature";
493 static char* externref_str = "#externref";
494 static char* externdef_str = "#externdef";
495 static char* ctmpl_str = "#ctmpl";
496 static char* htmpl_str = "#htmpl";
497 static char* const_str = "#const";
499 if (strncmp (buf, file_str, strlen (file_str)) == 0)
500 token = X_FILE_TOKEN;
501 else if (strncmp (buf, table_str, strlen (table_str)) == 0)
502 token = X_TABLE_TOKEN;
503 else if (strncmp (buf, prefix_str, strlen (prefix_str)) == 0)
504 token = X_PREFIX_TOKEN;
505 else if (strncmp (buf, feature_str, strlen (feature_str)) == 0)
506 token = X_FEATURE_TOKEN;
507 else if (strncmp (buf, externref_str, strlen (externref_str)) == 0)
508 token = X_EXTERNREF_TOKEN;
509 else if (strncmp (buf, externdef_str, strlen (externdef_str)) == 0)
510 token = X_EXTERNDEF_TOKEN;
511 else if (strncmp (buf, ctmpl_str, strlen (ctmpl_str)) == 0)
512 token = X_CTMPL_TOKEN;
513 else if (strncmp (buf, htmpl_str, strlen (htmpl_str)) == 0)
514 token = X_HTMPL_TOKEN;
515 else if (strncmp (buf, const_str, strlen (const_str)) == 0)
516 token = X_CONST_TOKEN;
525 if ((phile = (File*) malloc (sizeof(File))) == NULL)
527 if ((phile->name = malloc (strlen (buf + strlen (file_str)) + 1)) == NULL)
529 (void) strncpy (phile->name, buf + strlen (file_str) + 1,
530 strlen (buf + strlen (file_str)) + 1);
532 phile->tablecurrent = NULL;
533 phile->tabletail = &phile->table;
538 filetail = &phile->next;
545 if ((table = (Table*) malloc (sizeof(Table))) == NULL)
547 if ((table->name = malloc (strlen (buf + strlen (table_str)) + 1)) == NULL)
549 (void) strncpy (table->name, buf + strlen (table_str) + 1,
550 strlen (buf + strlen (table_str)) + 1);
551 table->tableent = NULL;
552 table->tableentcurrent = NULL;
553 table->tableenttail = &table->tableent;
557 *filecurrent->tabletail = table;
558 filecurrent->tabletail = &table->next;
559 filecurrent->tablecurrent = table;
563 if ((prefixstr = malloc (strlen (buf + strlen (prefix_str)) + 1)) == NULL)
565 (void) strncpy (prefixstr, buf + strlen (prefix_str) + 1,
566 strlen (buf + strlen (prefix_str)) + 1);
568 case X_FEATURE_TOKEN:
569 if ((featurestr = malloc (strlen (buf + strlen (feature_str)) + 1)) == NULL)
571 (void) strncpy (featurestr, buf + strlen (feature_str) + 1,
572 strlen (buf + strlen (feature_str)) + 1);
574 case X_EXTERNREF_TOKEN:
575 if ((externrefstr = malloc (strlen (buf + strlen (externref_str)) + 1)) == NULL)
577 (void) strncpy (externrefstr, buf + strlen (externref_str) + 1,
578 strlen (buf + strlen (externref_str)) + 1);
580 case X_EXTERNDEF_TOKEN:
581 if ((externdefstr = malloc (strlen (buf + strlen (externdef_str)) + 1)) == NULL)
583 (void) strncpy (externdefstr, buf + strlen (externdef_str) + 1,
584 strlen (buf + strlen (externdef_str)) + 1);
587 if ((ctmplstr = malloc (strlen (buf + strlen (ctmpl_str)) + 1)) == NULL)
589 (void) strncpy (ctmplstr, buf + strlen (ctmpl_str) + 1,
590 strlen (buf + strlen (ctmpl_str)) + 1);
593 if ((filecurrent->tmpl = fopen (buf + strlen (htmpl_str) + 1, "r")) == NULL) {
594 (void) fprintf (stderr,
595 "Expected template %s, not found\n", htmpl_str);
600 if ((conststr = malloc (strlen (buf + strlen (const_str)) + 1)) == NULL)
602 (void) strncpy (conststr, buf + strlen (const_str) + 1,
603 strlen (buf + strlen (const_str)) + 1);
613 if ((right = index(buf, ' ')))
618 snprintf(lbuf, sizeof(lbuf), "%s%s", prefixstr, right);
622 llen = len = strlen(buf) + 1;
623 rlen = strlen(right) + 1;
624 if (right != buf + 1) len += rlen;
625 if ((tableent = (TableEnt*)malloc(sizeof(TableEnt) + len)) == NULL)
627 tableent->left = (char *)(tableent + 1);
628 strncpy(tableent->left, buf, llen);
630 tableent->right = tableent->left + llen;
631 strncpy(tableent->right, right, rlen);
633 tableent->right = tableent->left + 1;
635 tableent->next = NULL;
637 *filecurrent->tablecurrent->tableenttail = tableent;
638 filecurrent->tablecurrent->tableenttail = &tableent->next;
639 filecurrent->tablecurrent->tableentcurrent = tableent;
645 static void IntelABIIndexEntries (file)
651 for (t = file->table; t; t = t->next)
652 for (te = t->tableent; te; te = te->next) {
653 te->offset = t->offset;
654 t->offset += strlen (te->right);
659 static void DefaultIndexEntries (file)
666 for (t = file->table; t; t = t->next)
667 for (te = t->tableent; te; te = te->next) {
669 offset += strlen (te->right);
674 static void IndexEntries (file,abi)
683 IntelABIIndexEntries (file);
686 DefaultIndexEntries (file);
691 static char* DoComment (line)
699 /* assume that the first line with two '$' in it is the RCS tag line */
700 if ((tag = index (line, '$')) == NULL) return NULL;
701 if ((eol = index (tag + 1, '$')) == NULL) return NULL;
703 if ((ret = malloc (len)) == NULL)
705 (void) strncpy (ret, tag + 1, len - 1);
715 char* tagline = NULL;
728 for (i = 1; i < argc; i++) {
729 if (strcmp (argv[i], "-f") == 0) {
731 f = fopen (argv[i], "r");
735 if (strcmp (argv[i], "-sparcabi") == 0)
737 if (strcmp (argv[i], "-intelabi") == 0)
739 if (strcmp (argv[i], "-functionabi") == 0)
740 abi = X_FUNCTION_ABI;
741 if (strcmp (argv[i], "-earlyR6bc") == 0 && abi == X_INTEL_ABI)
742 abi = X_INTEL_ABI_BC;
743 if (strcmp (argv[i], "-arrayperabi") == 0)
744 abi = X_ARRAYPER_ABI;
746 if (strcmp (argv[i], "-defaultabi") == 0)
752 if (f == NULL) return 1;
753 while (fgets(buf, sizeof buf, f)) {
754 if (!buf[0] || buf[0] == '\n')
757 if (tagline) continue;
758 tagline = DoComment (buf);
761 if (buf[(len = strlen (buf) - 1)] == '\n') buf[len] = '\0';
764 for (phile = file; phile; phile = phile->next) {
765 if (abi != X_ARRAYPER_ABI) IndexEntries (phile, abi);
766 WriteHeader (tagline, phile, abi);
768 WriteSource(tagline, abi);