xref: /original-bsd/old/roff/nroff_term/mktab.c (revision f955cb91)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)mktab.c	5.2 (Berkeley) 04/18/91";
16 #endif /* not lint */
17 
18 /*
19  * mktab.c
20  *
21  * Function:	Build nroff terminal tables in a compiler-independent way.
22  * Usage:	cc -Itroff mktab.c tabnnn.c -o mktab; mktab > tabnnn
23  * Date:	Sat Feb 25 00:10:06 MST 1989
24  * Author:	Donn Seeley
25  * Remarks:
26  *	Traditional nroff terminal table construction works by compiling
27  *	a C file into a binary that is read directly into nroff as it runs.
28  *	If your C compiler or your relocation format differ from what nroff
29  *	expects, you lose.  This program, when linked with a terminal table
30  *	object file, fakes up an 'object' file that looks enough like the
31  *	traditional one to fool nroff.
32  */
33 
34 #include <stdio.h>
35 #include "tw.h"
36 
37 main()
38 {
39 	static struct fake_exec {
40 		int bogus[8];	/* bogus[2] == a_data */
41 	} fe;
42 	register int *bip;
43 	register char **tip;
44 	register int offset = sizeof t;
45 	int buf[sizeof t / sizeof (int)];
46 	char *malloc();
47 	int twbase = (int *) &t.twinit - &t.bset;
48 
49 	/*
50 	 * Copy the integers at the start of the table.
51 	 */
52 	bcopy((char *) &t, (char *) buf, twbase * sizeof (int));
53 
54 	/*
55 	 * Replace the character pointers in the copy with integer offsets.
56 	 * This duplicates the effect of relocation offsets.
57 	 * Take care to count the possibly null control bytes in the codetab
58 	 *	section.
59 	 */
60 	for (bip = &buf[twbase], tip = &t.twinit;
61 	     tip < &t.codetab[0];
62 	     ++bip, ++tip)
63 		if (*tip) {
64 			*bip = offset;
65 			offset += strlen(*tip) + 1;
66 		} else
67 			*bip = 0;
68 	for (; tip < &t.zzz; ++bip, ++tip)
69 		if (*tip) {
70 			*bip = offset;
71 			offset += strlen(*tip + 1) + 2;
72 		} else
73 			*bip = 0;
74 
75 	/*
76 	 * Patch in a fake data segment size field.
77 	 */
78 	fe.bogus[2] = offset;
79 
80 	/*
81 	 * Dump the header and the table.
82 	 */
83 	(void) fwrite((char *) &fe, sizeof fe, 1, stdout);
84 	(void) fwrite((char *) buf, sizeof t, 1, stdout);
85 
86 	/*
87 	 * Dump the strings.
88 	 */
89 	for (tip = &t.twinit; tip < &t.codetab[0]; ++tip)
90 		if (*tip) {
91 			fputs(*tip, stdout);
92 			putchar('\0');
93 		}
94 	for (tip = &t.codetab[0]; tip < &t.zzz; ++tip)
95 		if (*tip) {
96 			putchar(**tip);
97 			fputs(*tip + 1, stdout);
98 			putchar('\0');
99 		}
100 
101 	return 0;
102 }
103