1 /***************************************************************************
2  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
3  * is provided to you without charge, and with no warranty.  You may give  *
4  * away copies of JOVE, including sources, provided that this notice is    *
5  * included in all the files.                                              *
6  ***************************************************************************/
7 
8 /* Macvert converts old style macro files to the new style.  The old
9    style macros were binary files, the new ones are text files suitable
10    for loading with the "source" command of JOVE. */
11 
12 #include <stdio.h>
13 
14 extern int
15 	read(),
16 	write();
17 
18 static void
19 	output_new_definition();
20 
21 static int	mac_fd;
22 
23 static void
24 mac_io(fcn, ptr, nbytes)
25 int	(*fcn)();
26 char	*ptr;
27 int	nbytes;
28 {
29 	int	nio;
30 
31 	if ((nio = (*fcn)(mac_fd, ptr, nbytes)) != nbytes)
32 		fprintf(stderr, "[Macro %s error: %d got %d]",
33 			 (fcn == read) ? "read" : "write",
34 			 nbytes,
35 			 nio);
36 }
37 
38 #define NEWWAY	1
39 #define OLDWAY	0
40 
41 static int	int_how = NEWWAY;
42 
43 /* Formatting int's the old way or the new "improved" way? */
44 
45 #ifdef notdef
46 #if	defined(vax) || defined(pdp11)
47 
48 static long
ntohl(x)49 ntohl(x)
50 register long x;
51 {
52 	return	(((x >>  0) & 0377) << 24) |
53 		(((x >>  8) & 0377) << 16) |
54 		(((x >> 16) & 0377) <<  8) |
55 		(((x >> 24) & 0377) <<  0);
56 }
57 
58 #else
59 
60 static long
ntohl(x)61 ntohl(x)
62 register long x;
63 {
64 	return x;
65 }
66 
67 #endif
68 #endif
69 
70 static int
int_fmt(i)71 int_fmt(i)
72 int	i;
73 {
74 	if (int_how == NEWWAY) {
75 		/* Note: using ntohl() for an int is not portable! */
76 		return (int) ntohl((long) i);
77 	}
78 	return i;
79 }
80 
81 static void
read_and_write_macros(filein)82 read_and_write_macros(filein)
83 char	*filein;
84 {
85 	int	namelen,
86 		bodylen,
87 		tmp;
88 	char	macname[256],
89 		macbuf[1024];
90 
91 	if ((mac_fd = open(filein, 0)) == -1)
92 		fprintf(stderr, "Cannot open %s\n", filein);
93 
94 	while (read(mac_fd, (char *) &tmp, sizeof tmp) == (sizeof tmp)) {
95 retry:
96 		bodylen = int_fmt(tmp);
97 		if (bodylen <= 0 || bodylen > 10000) {
98 			if (int_how == NEWWAY) {
99 				int_how = OLDWAY;
100 				goto retry;
101 			} else {
102 				fprintf(stderr, "I don't think \"%s\" is an old style JOVE macro file\n", filein);
103 				exit(1);
104 			}
105 		}
106 		mac_io(read, (char *) &namelen, (int) (sizeof namelen));
107 		namelen = int_fmt(namelen);
108 		mac_io(read, macname, namelen);
109 		mac_io(read, macbuf, bodylen);
110 		output_new_definition(macname, macbuf, bodylen);
111 	}
112 }
113 
114 static void
pr_putc(c)115 pr_putc(c)
116 int	c;
117 {
118 	if (c == '\\' || c == '^')
119 		putchar('\\');
120 	 else if (c < ' ' || c == '\177') {
121 		putchar('^');
122 		c = (c == '\177') ? '?' : (c + '@');
123 	}
124 	putchar(c);
125 }
126 
127 static void
output_new_definition(name,body,bodylen)128 output_new_definition(name, body, bodylen)
129 char	*name,
130 	*body;
131 int	bodylen;
132 {
133 	int	i;
134 
135 	fprintf(stdout, "define-macro %s ", name);
136 	for (i = 0; i < bodylen; i++)
137 		pr_putc(body[i]);
138 	putchar('\n');
139 }
140 
141 int
main(argc,argv)142 main(argc, argv)
143 int	argc;
144 char	*argv[];
145 {
146 	if (argc != 2) {
147 		fprintf(stderr, "usage: macvert <old-style-macro-file>\n");
148 		exit(1);
149 	}
150 
151 	read_and_write_macros(argv[1]);
152 	return 0;
153 }
154