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 #if defined(vax) || defined(pdp11) 46 47 static long 48 ntohl(x) 49 register long x; 50 { 51 return (((x >> 0) & 0377) << 24) | 52 (((x >> 8) & 0377) << 16) | 53 (((x >> 16) & 0377) << 8) | 54 (((x >> 24) & 0377) << 0); 55 } 56 57 #else 58 59 static long 60 ntohl(x) 61 register long x; 62 { 63 return x; 64 } 65 66 #endif 67 68 static int 69 int_fmt(i) 70 int i; 71 { 72 if (int_how == NEWWAY) { 73 /* Note: using ntohl() for an int is not portable! */ 74 return (int) ntohl((long) i); 75 } 76 return i; 77 } 78 79 static void 80 read_and_write_macros(filein) 81 char *filein; 82 { 83 int namelen, 84 bodylen, 85 tmp; 86 char macname[256], 87 macbuf[1024]; 88 89 if ((mac_fd = open(filein, 0)) == -1) 90 fprintf(stderr, "Cannot open %s\n", filein); 91 92 while (read(mac_fd, (char *) &tmp, sizeof tmp) == (sizeof tmp)) { 93 retry: 94 bodylen = int_fmt(tmp); 95 if (bodylen <= 0 || bodylen > 10000) { 96 if (int_how == NEWWAY) { 97 int_how = OLDWAY; 98 goto retry; 99 } else { 100 fprintf(stderr, "I don't think \"%s\" is an old style JOVE macro file\n", filein); 101 exit(1); 102 } 103 } 104 mac_io(read, (char *) &namelen, (int) (sizeof namelen)); 105 namelen = int_fmt(namelen); 106 mac_io(read, macname, namelen); 107 mac_io(read, macbuf, bodylen); 108 output_new_definition(macname, macbuf, bodylen); 109 } 110 } 111 112 static void 113 pr_putc(c) 114 int c; 115 { 116 if (c == '\\' || c == '^') 117 putchar('\\'); 118 else if (c < ' ' || c == '\177') { 119 putchar('^'); 120 c = (c == '\177') ? '?' : (c + '@'); 121 } 122 putchar(c); 123 } 124 125 static void 126 output_new_definition(name, body, bodylen) 127 char *name, 128 *body; 129 int bodylen; 130 { 131 int i; 132 133 fprintf(stdout, "define-macro %s ", name); 134 for (i = 0; i < bodylen; i++) 135 pr_putc(body[i]); 136 putchar('\n'); 137 } 138 139 int 140 main(argc, argv) 141 int argc; 142 char *argv[]; 143 { 144 if (argc != 2) { 145 fprintf(stderr, "usage: macvert <old-style-macro-file>\n"); 146 exit(1); 147 } 148 149 read_and_write_macros(argv[1]); 150 return 0; 151 } 152