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) sprintf (fileprotstr, "_%s_", phile->name);
289 if ((fileprotstr = malloc (strlen (phile->name) + strlen (prefixstr) + 3)) == NULL)
291 (void) sprintf (fileprotstr, "_%s%s_", prefixstr, phile->name);
294 for (tmp = fileprotstr; *tmp; tmp++) if (*tmp == '.') *tmp = '_';
296 (*headerproc[abi])(f, phile);
298 if (phile->tmpl) CopyTmplEpilog (phile->tmpl, f);
300 (void) free (fileprotstr);
301 (void) fclose (phile->tmpl);
305 static void WriteSourceLine (te, abi, fudge)
311 for (c = te->right; *c; c++) (void) printf ("'%c',", *c);
312 (void) printf ("%c", '0');
313 if (te->next || fudge) (void) printf ("%c", ',');
314 (void) printf ("%s", "\n");
317 static char* const_string = "%s %sConst char %s[] = {\n";
319 static void IntelABIWriteSource (abi)
324 for (phile = file; phile; phile = phile->next) {
328 for (t = phile->table; t; t = t->next) {
329 (void) printf (const_string, externdefstr,
330 conststr ? conststr : "", t->name);
331 for (te = t->tableent; te; te = te->next)
332 WriteSourceLine (te, abi, 0);
333 (void) printf ("%s\n\n", "};");
338 static void IntelABIBCWriteSource (abi)
343 for (phile = file; phile; phile = phile->next) {
347 (void) printf (const_string, externdefstr,
348 conststr ? conststr : "", phile->table->name);
350 for (t = phile->table; t; t = t->next)
351 for (te = t->tableent; te; te = te->next)
352 WriteSourceLine (te, abi, t->next ? 1 : 0);
353 (void) printf ("%s\n\n", "};");
355 if (phile->table->next) {
356 (void) printf (const_string, externdefstr,
357 conststr ? conststr : "", phile->table->next->name);
358 for (t = phile->table->next; t; t = t->next)
359 for (te = t->tableent; te; te = te->next)
360 WriteSourceLine (te, abi, 0);
361 (void) printf ("%s\n\n", "};");
366 static void FunctionWriteSource (abi)
371 for (phile = file; phile; phile = phile->next) {
375 (void) printf ("static %sConst char _%s[] = {\n",
376 conststr ? conststr : "", phile->table->name);
378 for (t = phile->table; t; t = t->next)
379 for (te = t->tableent; te; te = te->next)
380 WriteSourceLine (te, abi, t->next ? 1 : 0);
381 (void) printf ("%s\n\n", "};");
383 (void) printf ("%sConst char* %s(index)\n int index;\n{\n return &_%s[index];\n}\n\n",
384 conststr ? conststr : "",
385 phile->table->name, phile->table->name);
389 static void ArrayperWriteSource (abi)
393 static int done_atom;
395 for (phile = file; phile; phile = phile->next) {
399 for (t = phile->table; t; t = t->next)
400 for (te = t->tableent; te; te = te->next) {
401 if (strcmp (te->left, "RAtom") == 0) {
402 if (done_atom) return;
405 (void) printf ("%s %sConst char %s%s[] = \"%s\";\n",
406 externdefstr, conststr ? conststr : prefixstr,
407 te->left, te->right);
412 static void DefaultWriteSource (abi)
417 for (phile = file; phile; phile = phile->next) {
421 (void) printf (const_string, externdefstr, conststr ? conststr : "",
424 for (t = phile->table; t; t = t->next)
425 for (te = t->tableent; te; te = te->next)
426 WriteSourceLine (te, abi, t->next ? 1 : 0);
427 (void) printf ("%s\n\n", "};");
431 static void WriteSource(tagline, abi)
435 static void (*sourceproc[])() = {
436 DefaultWriteSource, ArrayperWriteSource,
437 IntelABIWriteSource, IntelABIBCWriteSource,
438 DefaultWriteSource, FunctionWriteSource };
443 tmpl = fopen (ctmplstr, "r");
445 if (tmpl) CopyTmplProlog (tmpl, stdout);
447 (void) fprintf (stderr, "Expected template %s, not found\n",
455 (void) printf ("%s\n%s\n/* %s ABI version -- Do not edit */\n",
456 "/* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */",
457 "/* This file is automatically generated. */",
460 if (tagline) (void) printf ("/* %s */\n\n", tagline);
462 (*sourceproc[abi])(abi);
464 if (tmpl) CopyTmplEpilog (tmpl, stdout);
467 static void DoLine(buf)
471 #define X_FILE_TOKEN 1
472 #define X_TABLE_TOKEN 2
473 #define X_PREFIX_TOKEN 3
474 #define X_FEATURE_TOKEN 4
475 #define X_EXTERNREF_TOKEN 5
476 #define X_EXTERNDEF_TOKEN 6
477 #define X_CTMPL_TOKEN 7
478 #define X_HTMPL_TOKEN 8
479 #define X_CONST_TOKEN 9
483 static char* file_str = "#file";
484 static char* table_str = "#table";
485 static char* prefix_str = "#prefix";
486 static char* feature_str = "#feature";
487 static char* externref_str = "#externref";
488 static char* externdef_str = "#externdef";
489 static char* ctmpl_str = "#ctmpl";
490 static char* htmpl_str = "#htmpl";
491 static char* const_str = "#const";
493 if (strncmp (buf, file_str, strlen (file_str)) == 0)
494 token = X_FILE_TOKEN;
495 else if (strncmp (buf, table_str, strlen (table_str)) == 0)
496 token = X_TABLE_TOKEN;
497 else if (strncmp (buf, prefix_str, strlen (prefix_str)) == 0)
498 token = X_PREFIX_TOKEN;
499 else if (strncmp (buf, feature_str, strlen (feature_str)) == 0)
500 token = X_FEATURE_TOKEN;
501 else if (strncmp (buf, externref_str, strlen (externref_str)) == 0)
502 token = X_EXTERNREF_TOKEN;
503 else if (strncmp (buf, externdef_str, strlen (externdef_str)) == 0)
504 token = X_EXTERNDEF_TOKEN;
505 else if (strncmp (buf, ctmpl_str, strlen (ctmpl_str)) == 0)
506 token = X_CTMPL_TOKEN;
507 else if (strncmp (buf, htmpl_str, strlen (htmpl_str)) == 0)
508 token = X_HTMPL_TOKEN;
509 else if (strncmp (buf, const_str, strlen (const_str)) == 0)
510 token = X_CONST_TOKEN;
519 if ((phile = (File*) malloc (sizeof(File))) == NULL)
521 if ((phile->name = malloc (strlen (buf + strlen (file_str)) + 1)) == NULL)
523 (void) strcpy (phile->name, buf + strlen (file_str) + 1);
525 phile->tablecurrent = NULL;
526 phile->tabletail = &phile->table;
531 filetail = &phile->next;
538 if ((table = (Table*) malloc (sizeof(Table))) == NULL)
540 if ((table->name = malloc (strlen (buf + strlen (table_str)) + 1)) == NULL)
542 (void) strcpy (table->name, buf + strlen (table_str) + 1);
543 table->tableent = NULL;
544 table->tableentcurrent = NULL;
545 table->tableenttail = &table->tableent;
549 *filecurrent->tabletail = table;
550 filecurrent->tabletail = &table->next;
551 filecurrent->tablecurrent = table;
555 if ((prefixstr = malloc (strlen (buf + strlen (prefix_str)) + 1)) == NULL)
557 (void) strcpy (prefixstr, buf + strlen (prefix_str) + 1);
559 case X_FEATURE_TOKEN:
560 if ((featurestr = malloc (strlen (buf + strlen (feature_str)) + 1)) == NULL)
562 (void) strcpy (featurestr, buf + strlen (feature_str) + 1);
564 case X_EXTERNREF_TOKEN:
565 if ((externrefstr = malloc (strlen (buf + strlen (externref_str)) + 1)) == NULL)
567 (void) strcpy (externrefstr, buf + strlen (externref_str) + 1);
569 case X_EXTERNDEF_TOKEN:
570 if ((externdefstr = malloc (strlen (buf + strlen (externdef_str)) + 1)) == NULL)
572 (void) strcpy (externdefstr, buf + strlen (externdef_str) + 1);
575 if ((ctmplstr = malloc (strlen (buf + strlen (ctmpl_str)) + 1)) == NULL)
577 (void) strcpy (ctmplstr, buf + strlen (ctmpl_str) + 1);
580 if ((filecurrent->tmpl = fopen (buf + strlen (htmpl_str) + 1, "r")) == NULL) {
581 (void) fprintf (stderr,
582 "Expected template %s, not found\n", htmpl_str);
587 if ((conststr = malloc (strlen (buf + strlen (const_str)) + 1)) == NULL)
589 (void) strcpy (conststr, buf + strlen (const_str) + 1);
599 if (right = index(buf, ' '))
604 strcpy (lbuf, prefixstr);
605 strcat (lbuf, right);
609 llen = len = strlen(buf) + 1;
610 rlen = strlen(right) + 1;
611 if (right != buf + 1) len += rlen;
612 if ((tableent = (TableEnt*)malloc(sizeof(TableEnt) + len)) == NULL)
614 tableent->left = (char *)(tableent + 1);
615 strcpy(tableent->left, buf);
617 tableent->right = tableent->left + llen;
618 strcpy(tableent->right, right);
620 tableent->right = tableent->left + 1;
622 tableent->next = NULL;
624 *filecurrent->tablecurrent->tableenttail = tableent;
625 filecurrent->tablecurrent->tableenttail = &tableent->next;
626 filecurrent->tablecurrent->tableentcurrent = tableent;
632 static void IntelABIIndexEntries (file)
638 for (t = file->table; t; t = t->next)
639 for (te = t->tableent; te; te = te->next) {
640 te->offset = t->offset;
641 t->offset += strlen (te->right);
646 static void DefaultIndexEntries (file)
653 for (t = file->table; t; t = t->next)
654 for (te = t->tableent; te; te = te->next) {
656 offset += strlen (te->right);
661 static void IndexEntries (file,abi)
670 IntelABIIndexEntries (file);
673 DefaultIndexEntries (file);
678 static char* DoComment (line)
686 /* assume that the first line with two '$' in it is the RCS tag line */
687 if ((tag = index (line, '$')) == NULL) return NULL;
688 if ((eol = index (tag + 1, '$')) == NULL) return NULL;
690 if ((ret = malloc (len)) == NULL)
692 (void) strncpy (ret, tag + 1, len - 1);
702 char* tagline = NULL;
715 for (i = 1; i < argc; i++) {
716 if (strcmp (argv[i], "-f") == 0) {
718 f = fopen (argv[i], "r");
722 if (strcmp (argv[i], "-sparcabi") == 0)
724 if (strcmp (argv[i], "-intelabi") == 0)
726 if (strcmp (argv[i], "-functionabi") == 0)
727 abi = X_FUNCTION_ABI;
728 if (strcmp (argv[i], "-earlyR6bc") == 0 && abi == X_INTEL_ABI)
729 abi = X_INTEL_ABI_BC;
730 if (strcmp (argv[i], "-arrayperabi") == 0)
731 abi = X_ARRAYPER_ABI;
733 if (strcmp (argv[i], "-defaultabi") == 0)
739 if (f == NULL) return 1;
740 while (fgets(buf, sizeof buf, f)) {
741 if (!buf[0] || buf[0] == '\n')
744 if (tagline) continue;
745 tagline = DoComment (buf);
748 if (buf[(len = strlen (buf) - 1)] == '\n') buf[len] = '\0';
751 for (phile = file; phile; phile = phile->next) {
752 if (abi != X_ARRAYPER_ABI) IndexEntries (phile, abi);
753 WriteHeader (tagline, phile, abi);
755 WriteSource(tagline, abi);