home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / jove4.9 / part01 / macvert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-25  |  3.1 KB  |  168 lines

  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    read(),
  15.         write();
  16.  
  17. int    mac_fd;
  18.  
  19. mac_io(fcn, ptr, nbytes)
  20. int    (*fcn)();
  21. char    *ptr;
  22. {
  23.     int    nio;
  24.  
  25.     if ((nio = (*fcn)(mac_fd, ptr, nbytes)) != nbytes)
  26.         fprintf(stderr, "[Macro %s error: %d got %d]",
  27.              (fcn == read) ? "read" : "write",
  28.              nbytes,
  29.              nio);
  30. }
  31.  
  32. #define NEWWAY    1
  33. #define OLDWAY    0
  34.  
  35. int    int_how = NEWWAY;
  36.  
  37. /* Formatting int's the old way or the new "improved" way? */
  38.  
  39. #if vax || pdp11
  40. long htonl(x)
  41. register long x;
  42. {
  43.     return(    (((x >>  0) & 0377) << 24) |
  44.         (((x >>  8) & 0377) << 16) |
  45.         (((x >> 16) & 0377) <<  8) |
  46.         (((x >> 24) & 0377) <<  0) );
  47. }
  48.  
  49. short htons(x)
  50. register short x;
  51. {
  52.     return(    (((x >>  0) & 0377) << 8) |
  53.         (((x >>  8) & 0377) << 0) );
  54. }
  55.  
  56. long ntohl(x)
  57. register long x;
  58. {
  59.     return(    (((x >>  0) & 0377) << 24) |
  60.         (((x >>  8) & 0377) << 16) |
  61.         (((x >> 16) & 0377) <<  8) |
  62.         (((x >> 24) & 0377) <<  0) );
  63. }
  64.  
  65. short ntohs(x)
  66. register short x;
  67. {
  68.     return(    (((x >>  0) & 0377) << 8) |
  69.         (((x >>  8) & 0377) << 0) );
  70. }
  71. #else
  72. long htonl(x)
  73. register long x;
  74. {
  75.     return(x);
  76. }
  77.  
  78. short htons(x)
  79. register short x;
  80. {
  81.     return(x);
  82. }
  83.  
  84. long ntohl(x)
  85. register long x;
  86. {
  87.     return(x);
  88. }
  89.  
  90. short ntohs(x)
  91. register short x;
  92. {
  93.     return(x);
  94. }
  95. #endif
  96.  
  97. int_fmt(i)
  98. {
  99.     if (int_how == NEWWAY)
  100.         return ntohl(i);
  101.     return i;
  102. }
  103.  
  104. read_and_write_macros(filein)
  105. char    *filein;
  106. {
  107.     int    namelen,
  108.         bodylen,
  109.         tmp;
  110.     char    macname[256],
  111.         macbuf[1024];
  112.  
  113.     if ((mac_fd = open(filein, 0)) == -1)
  114.         fprintf(stderr, "Cannot open %s\n", filein);
  115.  
  116.     while (read(mac_fd, (char *) &tmp, sizeof tmp) == (sizeof tmp)) {
  117. retry:        bodylen = int_fmt(tmp);
  118.         if (bodylen <= 0 || bodylen > 10000) {
  119.             if (int_how == NEWWAY) {
  120.                 int_how = OLDWAY;
  121.                 goto retry;
  122.             } else {
  123.                 fprintf(stderr, "I don't think \"%s\" is an old style JOVE macro file\n", filein);
  124.                 exit(1);
  125.             }
  126.         }
  127.         mac_io(read, (char *) &namelen, sizeof namelen);
  128.         namelen = int_fmt(namelen);
  129.         mac_io(read, macname, namelen);
  130.         mac_io(read, macbuf, bodylen);
  131.         output_new_definition(macname, macbuf, bodylen);
  132.     }        
  133. }
  134.  
  135. pr_putc(c)
  136. {
  137.     if (c == '\\' || c == '^')
  138.         putchar('\\');
  139.      else if (c < ' ' || c == '\177') {
  140.         putchar('^');
  141.         c = (c == '\177') ? '?' : (c + '@');
  142.     }
  143.     putchar(c);
  144. }
  145.  
  146. output_new_definition(name, body, bodylen)
  147. char    *name,
  148.     *body;
  149. {
  150.     int    i;
  151.  
  152.     fprintf(stdout, "define-macro %s ", name);
  153.     for (i = 0; i < bodylen; i++)
  154.         pr_putc(body[i]);
  155.     putchar('\n');
  156. }
  157.  
  158. main(argc, argv)
  159. char    *argv[];
  160. {
  161.     if (argc != 2) {
  162.         fprintf(stderr, "usage: macvert <old-style-macro-file>\n");
  163.         exit(1);
  164.     }
  165.  
  166.     read_and_write_macros(argv[1]);
  167. }
  168.