1 /* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */
5 Copyright (c) 1991, 1998 The Open Group
9 The above copyright notice and this permission notice shall be included in
10 all copies or substantial portions of the Software.
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
16 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 Except as contained in this notice, the name of The Open Group shall not be
20 used in advertising or otherwise to promote the sale, use or other dealings
21 in this Software without prior written authorization from The Open Group.
25 /* Constructs string definitions */
29 #ifndef X_NOT_STDC_ENV
34 #if defined(macII) && !defined(__STDC__) /* stdlib.h fails to define these */
38 typedef struct _TableEnt {
39 struct _TableEnt* next;
45 typedef struct _Table {
48 TableEnt* tableentcurrent;
49 TableEnt** tableenttail;
54 typedef struct _File {
63 static File* file = NULL;
64 static File* filecurrent = NULL;
65 static File** filetail = &file;
66 static char* conststr;
67 static char* prefixstr = NULL;
68 static char* featurestr = NULL;
69 static char* ctmplstr = NULL;
70 static char* fileprotstr;
71 static char* externrefstr;
72 static char* externdefstr;
74 #define X_DEFAULT_ABI 0
75 #define X_ARRAYPER_ABI 1
77 #define X_INTEL_ABI_BC 3
79 #define X_FUNCTION_ABI 5
81 #define X_MAGIC_STRING "<<<STRING_TABLE_GOES_HERE>>>"
83 static void WriteHeaderProlog (f, phile)
90 (void) fprintf (f, "#ifdef %s\n", featurestr);
91 for (t = phile->table; t; t = t->next)
92 for (te = t->tableent; te; te = te->next) {
93 if (strcmp (te->left, "RAtom") == 0) {
95 "#ifndef %s%s\n#define %s%s \"%s\"\n#endif\n",
96 prefixstr, te->left, prefixstr, te->left, te->right);
99 "#define %s%s \"%s\"\n",
100 prefixstr, te->left, te->right);
103 (void) fprintf (f, "%s", "#else\n");
106 static void IntelABIWriteHeader (f, phile)
113 WriteHeaderProlog (f, phile);
115 for (t = phile->table; t; t = t->next) {
116 (void) fprintf (f, "%s %sConst char %s[];\n",
117 externrefstr, conststr ? conststr : fileprotstr, t->name);
118 for (te = t->tableent; te; te = te->next)
120 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
121 prefixstr, te->left, prefixstr, te->left, t->name, te->offset);
124 (void) fprintf (f, "#endif /* %s */\n", featurestr);
127 static void SPARCABIWriteHeader (f, phile)
134 for (t = phile->table; t; t = t->next)
135 for (te = t->tableent; te; te = te->next)
136 (void) fprintf (f, "#define %s%s \"%s\"\n",
137 prefixstr, te->left, te->right);
140 static void FunctionWriteHeader (f, phile)
147 WriteHeaderProlog (f, phile);
149 (void) fprintf (f, "%s %sConst char* %s();\n",
150 externrefstr, conststr ? conststr : fileprotstr,
153 for (t = phile->table; t; t = t->next)
154 for (te = t->tableent; te; te = te->next)
156 "#ifndef %s%s\n#define %s%s (%s(%d))\n#endif\n",
157 prefixstr, te->left, prefixstr, te->left, phile->table->name,
160 (void) fprintf (f, "#endif /* %s */\n", featurestr);
163 static void ArrayperWriteHeader (f, phile)
170 WriteHeaderProlog (f, phile);
172 for (t = phile->table; t; t = t->next)
173 for (te = t->tableent; te; te = te->next)
175 "#ifndef %s%s\n%s %sConst char %s%s[];\n#endif\n",
177 externrefstr, conststr ? conststr : fileprotstr,
178 prefixstr, te->left);
180 (void) fprintf (f, "#endif /* %s */\n", featurestr);
183 static void DefaultWriteHeader (f, phile)
190 WriteHeaderProlog (f, phile);
192 (void) fprintf (f, "%s %sConst char %s[];\n",
193 externrefstr, conststr ? conststr : fileprotstr,
196 for (t = phile->table; t; t = t->next)
197 for (te = t->tableent; te; te = te->next)
199 "#ifndef %s%s\n#define %s%s ((char*)&%s[%d])\n#endif\n",
200 prefixstr, te->left, prefixstr, te->left, phile->table->name,
203 (void) fprintf (f, "#endif /* %s */\n", featurestr);
206 static void CopyTmplProlog (tmpl, f)
211 static char* magic_string = X_MAGIC_STRING;
212 int magic_string_len = strlen (magic_string);
214 while (fgets (buf, sizeof buf, tmpl)) {
215 if (strncmp (buf, magic_string, magic_string_len) == 0) {
218 (void) fputs (buf, f);
222 static void CopyTmplEpilog (tmpl, f)
228 while (fgets (buf, sizeof buf, tmpl))
229 (void) fputs (buf, f);
232 static char* abistring[] = {
233 "Default", "Array per string", "Intel", "Intel BC", "SPARC", "Function" };
235 static void WriteHeader (tagline, phile, abi)
244 static void (*headerproc[])() = {
245 DefaultWriteHeader, ArrayperWriteHeader,
246 IntelABIWriteHeader, IntelABIWriteHeader,
247 SPARCABIWriteHeader, FunctionWriteHeader };
249 if ((f = fopen (phile->name, "w+")) == NULL) exit (1);
251 if (phile->tmpl) CopyTmplProlog (phile->tmpl, f);
254 "%s\n%s\n/* %s ABI version -- Do not edit */\n",
255 "/* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */",
256 "/* This file is automatically generated. */",
259 if (tagline) (void) fprintf (f, "/* %s */\n\n", tagline);
261 /* do the right thing for Motif, i.e. avoid _XmXmStrDefs_h_ */
262 if (strcmp (prefixstr, "Xm") == 0) {
263 if ((fileprotstr = malloc (strlen (phile->name) + 3)) == NULL)
265 (void) sprintf (fileprotstr, "_%s_", phile->name);
267 if ((fileprotstr = malloc (strlen (phile->name) + strlen (prefixstr) + 3)) == NULL)
269 (void) sprintf (fileprotstr, "_%s%s_", prefixstr, phile->name);
272 for (tmp = fileprotstr; *tmp; tmp++) if (*tmp == '.') *tmp = '_';
274 (*headerproc[abi])(f, phile);
276 if (phile->tmpl) CopyTmplEpilog (phile->tmpl, f);
278 (void) free (fileprotstr);
279 (void) fclose (phile->tmpl);
283 static void WriteSourceLine (te, abi, fudge)
289 for (c = te->right; *c; c++) (void) printf ("'%c',", *c);
290 (void) printf ("%c", '0');
291 if (te->next || fudge) (void) printf ("%c", ',');
292 (void) printf ("%s", "\n");
295 static char* const_string = "%s %sConst char %s[] = {\n";
297 static void IntelABIWriteSource (abi)
302 for (phile = file; phile; phile = phile->next) {
306 for (t = phile->table; t; t = t->next) {
307 (void) printf (const_string, externdefstr,
308 conststr ? conststr : "", t->name);
309 for (te = t->tableent; te; te = te->next)
310 WriteSourceLine (te, abi, 0);
311 (void) printf ("%s\n\n", "};");
316 static void IntelABIBCWriteSource (abi)
321 for (phile = file; phile; phile = phile->next) {
325 (void) printf (const_string, externdefstr,
326 conststr ? conststr : "", phile->table->name);
328 for (t = phile->table; t; t = t->next)
329 for (te = t->tableent; te; te = te->next)
330 WriteSourceLine (te, abi, t->next ? 1 : 0);
331 (void) printf ("%s\n\n", "};");
333 if (phile->table->next) {
334 (void) printf (const_string, externdefstr,
335 conststr ? conststr : "", phile->table->next->name);
336 for (t = phile->table->next; t; t = t->next)
337 for (te = t->tableent; te; te = te->next)
338 WriteSourceLine (te, abi, 0);
339 (void) printf ("%s\n\n", "};");
344 static void FunctionWriteSource (abi)
349 for (phile = file; phile; phile = phile->next) {
353 (void) printf ("static %sConst char _%s[] = {\n",
354 conststr ? conststr : "", phile->table->name);
356 for (t = phile->table; t; t = t->next)
357 for (te = t->tableent; te; te = te->next)
358 WriteSourceLine (te, abi, t->next ? 1 : 0);
359 (void) printf ("%s\n\n", "};");
361 (void) printf ("%sConst char* %s(index)\n int index;\n{\n return &_%s[index];\n}\n\n",
362 conststr ? conststr : "",
363 phile->table->name, phile->table->name);
367 static void ArrayperWriteSource (abi)
371 static int done_atom;
373 for (phile = file; phile; phile = phile->next) {
377 for (t = phile->table; t; t = t->next)
378 for (te = t->tableent; te; te = te->next) {
379 if (strcmp (te->left, "RAtom") == 0) {
380 if (done_atom) return;
383 (void) printf ("%s %sConst char %s%s[] = \"%s\";\n",
384 externdefstr, conststr ? conststr : prefixstr,
385 te->left, te->right);
390 static void DefaultWriteSource (abi)
395 for (phile = file; phile; phile = phile->next) {
399 (void) printf (const_string, externdefstr, conststr ? conststr : "",
402 for (t = phile->table; t; t = t->next)
403 for (te = t->tableent; te; te = te->next)
404 WriteSourceLine (te, abi, t->next ? 1 : 0);
405 (void) printf ("%s\n\n", "};");
409 static void WriteSource(tagline, abi)
413 static void (*sourceproc[])() = {
414 DefaultWriteSource, ArrayperWriteSource,
415 IntelABIWriteSource, IntelABIBCWriteSource,
416 DefaultWriteSource, FunctionWriteSource };
421 tmpl = fopen (ctmplstr, "r");
423 if (tmpl) CopyTmplProlog (tmpl, stdout);
425 (void) fprintf (stderr, "Expected template %s, not found\n",
433 (void) printf ("%s\n%s\n/* %s ABI version -- Do not edit */\n",
434 "/* $TOG: makestrs.c /main/11 1998/02/06 11:24:15 kaleb $ */",
435 "/* This file is automatically generated. */",
438 if (tagline) (void) printf ("/* %s */\n\n", tagline);
440 (*sourceproc[abi])(abi);
442 if (tmpl) CopyTmplEpilog (tmpl, stdout);
445 static void DoLine(buf)
449 #define X_FILE_TOKEN 1
450 #define X_TABLE_TOKEN 2
451 #define X_PREFIX_TOKEN 3
452 #define X_FEATURE_TOKEN 4
453 #define X_EXTERNREF_TOKEN 5
454 #define X_EXTERNDEF_TOKEN 6
455 #define X_CTMPL_TOKEN 7
456 #define X_HTMPL_TOKEN 8
457 #define X_CONST_TOKEN 9
461 static char* file_str = "#file";
462 static char* table_str = "#table";
463 static char* prefix_str = "#prefix";
464 static char* feature_str = "#feature";
465 static char* externref_str = "#externref";
466 static char* externdef_str = "#externdef";
467 static char* ctmpl_str = "#ctmpl";
468 static char* htmpl_str = "#htmpl";
469 static char* const_str = "#const";
471 if (strncmp (buf, file_str, strlen (file_str)) == 0)
472 token = X_FILE_TOKEN;
473 else if (strncmp (buf, table_str, strlen (table_str)) == 0)
474 token = X_TABLE_TOKEN;
475 else if (strncmp (buf, prefix_str, strlen (prefix_str)) == 0)
476 token = X_PREFIX_TOKEN;
477 else if (strncmp (buf, feature_str, strlen (feature_str)) == 0)
478 token = X_FEATURE_TOKEN;
479 else if (strncmp (buf, externref_str, strlen (externref_str)) == 0)
480 token = X_EXTERNREF_TOKEN;
481 else if (strncmp (buf, externdef_str, strlen (externdef_str)) == 0)
482 token = X_EXTERNDEF_TOKEN;
483 else if (strncmp (buf, ctmpl_str, strlen (ctmpl_str)) == 0)
484 token = X_CTMPL_TOKEN;
485 else if (strncmp (buf, htmpl_str, strlen (htmpl_str)) == 0)
486 token = X_HTMPL_TOKEN;
487 else if (strncmp (buf, const_str, strlen (const_str)) == 0)
488 token = X_CONST_TOKEN;
497 if ((phile = (File*) malloc (sizeof(File))) == NULL)
499 if ((phile->name = malloc (strlen (buf + strlen (file_str)) + 1)) == NULL)
501 (void) strcpy (phile->name, buf + strlen (file_str) + 1);
503 phile->tablecurrent = NULL;
504 phile->tabletail = &phile->table;
509 filetail = &phile->next;
516 if ((table = (Table*) malloc (sizeof(Table))) == NULL)
518 if ((table->name = malloc (strlen (buf + strlen (table_str)) + 1)) == NULL)
520 (void) strcpy (table->name, buf + strlen (table_str) + 1);
521 table->tableent = NULL;
522 table->tableentcurrent = NULL;
523 table->tableenttail = &table->tableent;
527 *filecurrent->tabletail = table;
528 filecurrent->tabletail = &table->next;
529 filecurrent->tablecurrent = table;
533 if ((prefixstr = malloc (strlen (buf + strlen (prefix_str)) + 1)) == NULL)
535 (void) strcpy (prefixstr, buf + strlen (prefix_str) + 1);
537 case X_FEATURE_TOKEN:
538 if ((featurestr = malloc (strlen (buf + strlen (feature_str)) + 1)) == NULL)
540 (void) strcpy (featurestr, buf + strlen (feature_str) + 1);
542 case X_EXTERNREF_TOKEN:
543 if ((externrefstr = malloc (strlen (buf + strlen (externref_str)) + 1)) == NULL)
545 (void) strcpy (externrefstr, buf + strlen (externref_str) + 1);
547 case X_EXTERNDEF_TOKEN:
548 if ((externdefstr = malloc (strlen (buf + strlen (externdef_str)) + 1)) == NULL)
550 (void) strcpy (externdefstr, buf + strlen (externdef_str) + 1);
553 if ((ctmplstr = malloc (strlen (buf + strlen (ctmpl_str)) + 1)) == NULL)
555 (void) strcpy (ctmplstr, buf + strlen (ctmpl_str) + 1);
558 if ((filecurrent->tmpl = fopen (buf + strlen (htmpl_str) + 1, "r")) == NULL) {
559 (void) fprintf (stderr,
560 "Expected template %s, not found\n", htmpl_str);
565 if ((conststr = malloc (strlen (buf + strlen (const_str)) + 1)) == NULL)
567 (void) strcpy (conststr, buf + strlen (const_str) + 1);
577 if (right = index(buf, ' '))
582 strcpy (lbuf, prefixstr);
583 strcat (lbuf, right);
587 llen = len = strlen(buf) + 1;
588 rlen = strlen(right) + 1;
589 if (right != buf + 1) len += rlen;
590 if ((tableent = (TableEnt*)malloc(sizeof(TableEnt) + len)) == NULL)
592 tableent->left = (char *)(tableent + 1);
593 strcpy(tableent->left, buf);
595 tableent->right = tableent->left + llen;
596 strcpy(tableent->right, right);
598 tableent->right = tableent->left + 1;
600 tableent->next = NULL;
602 *filecurrent->tablecurrent->tableenttail = tableent;
603 filecurrent->tablecurrent->tableenttail = &tableent->next;
604 filecurrent->tablecurrent->tableentcurrent = tableent;
610 static void IntelABIIndexEntries (file)
616 for (t = file->table; t; t = t->next)
617 for (te = t->tableent; te; te = te->next) {
618 te->offset = t->offset;
619 t->offset += strlen (te->right);
624 static void DefaultIndexEntries (file)
631 for (t = file->table; t; t = t->next)
632 for (te = t->tableent; te; te = te->next) {
634 offset += strlen (te->right);
639 static void IndexEntries (file,abi)
648 IntelABIIndexEntries (file);
651 DefaultIndexEntries (file);
656 static char* DoComment (line)
664 /* assume that the first line with two '$' in it is the RCS tag line */
665 if ((tag = index (line, '$')) == NULL) return NULL;
666 if ((eol = index (tag + 1, '$')) == NULL) return NULL;
668 if ((ret = malloc (len)) == NULL)
670 (void) strncpy (ret, tag + 1, len - 1);
680 char* tagline = NULL;
693 for (i = 1; i < argc; i++) {
694 if (strcmp (argv[i], "-f") == 0) {
696 f = fopen (argv[i], "r");
700 if (strcmp (argv[i], "-sparcabi") == 0)
702 if (strcmp (argv[i], "-intelabi") == 0)
704 if (strcmp (argv[i], "-functionabi") == 0)
705 abi = X_FUNCTION_ABI;
706 if (strcmp (argv[i], "-earlyR6bc") == 0 && abi == X_INTEL_ABI)
707 abi = X_INTEL_ABI_BC;
708 if (strcmp (argv[i], "-arrayperabi") == 0)
709 abi = X_ARRAYPER_ABI;
711 if (strcmp (argv[i], "-defaultabi") == 0)
717 if (f == NULL) return 1;
718 while (fgets(buf, sizeof buf, f)) {
719 if (!buf[0] || buf[0] == '\n')
722 if (tagline) continue;
723 tagline = DoComment (buf);
726 if (buf[(len = strlen (buf) - 1)] == '\n') buf[len] = '\0';
729 for (phile = file; phile; phile = phile->next) {
730 if (abi != X_ARRAYPER_ABI) IndexEntries (phile, abi);
731 WriteHeader (tagline, phile, abi);
733 WriteSource(tagline, abi);