home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / unix / hqxtobin.shr / mcvert.c < prev    next >
C/C++ Source or Header  |  1989-04-10  |  12KB  |  324 lines

  1. /* mcvert.c - version 1.0 - March 30, 1989
  2.  * Written by Doug Moore - Cornell University - moore@cs.cornell.edu - April '87
  3.  * Sun bug fixes, assorted stuff - Jim Sasaki, March '89
  4.  *
  5.  * This program may be freely distributed for non-profit purposes.  It may not
  6.  * be sold, by itself or as part of a collection of software.  It may be freely
  7.  * modified as long as no modified version is distributed.  Modifications of
  8.  * interest to all can be incorporated into the program by sending them to me
  9.  * for distribution.  Parts of the code can be used in other programs.  I am not
  10.  * responsible for any damage caused by this program.  I hope you enjoy it.
  11.  */
  12.  
  13. #include "mactypes.h"
  14.  
  15. #define HQX 0
  16. #define TEXT 1
  17. #define DATA 2
  18. #define RSRC 3
  19. #define FORWARDS 0
  20. #define BACKWARDS 1
  21.  
  22. FILE *verbose;
  23. char **hqxnames, **hqxnames_left;
  24. char *dir, *ext, *text_author;
  25. char *maxlines_str;
  26. int maxlines;
  27.  
  28. main(argc, argv)
  29. int argc;
  30. char **argv;
  31. {   char *flags, *getenv();
  32.     int direction, mode, unpit_flag;
  33.  
  34.     argv++;
  35.     argc--;
  36.     verbose = stderr;
  37.     direction = FORWARDS;
  38.     mode = HQX;
  39.     unpit_flag = 0;
  40.  
  41.     if ((text_author = getenv("MAC_EDITOR"))    == NULL)    text_author = "MACA";
  42.     if ((ext =         getenv("MAC_EXT"))       == NULL)    ext = ".bin";
  43.     if ((dir =         getenv("MAC_DLOAD_DIR")) == NULL)    dir = ".";
  44.     if ((maxlines_str = getenv("MAC_LINE_LIMIT")) == NULL)  maxlines = 2000;
  45.     else                                         maxlines = atoi(maxlines_str);
  46.     
  47.     /* Make command line arguments globally accessible */
  48.     hqxnames = (char **) calloc(argc+1, sizeof(char *));
  49.     hqxnames_left = hqxnames;
  50.     while (argc--)  *hqxnames_left++ = *argv++;
  51.     *hqxnames_left = "-";
  52.     hqxnames_left = hqxnames;
  53.  
  54.     while (strcmp(*hqxnames_left, "-")) {
  55.         if (hqxnames_left[0][0] == '-') {
  56.             flags = *hqxnames_left++;
  57.             while (*++flags)
  58.                 switch (*flags) {
  59.                 case 'x':
  60.                     mode = HQX;
  61.                     break;
  62.                 case 'u':
  63.                     mode = TEXT;
  64.                     break;
  65.                 case 'd':
  66.                     mode = DATA;
  67.                     break;
  68.                 case 'r':
  69.                     mode = RSRC;
  70.                     break;
  71.                 case 'D':
  72.                     direction = FORWARDS;
  73.                     break;
  74.                 case 'U':
  75.                     direction = BACKWARDS;
  76.                     break;
  77.                 case 'q':
  78.                     unpit_flag = 0;
  79.                     break;
  80.                 case 'p':
  81.                     unpit_flag = 1;
  82.                     break;
  83.                 case 's':
  84.                     verbose = fopen("/dev/null", "w");
  85.                     break;
  86.                 case 'v':
  87.                     verbose = stderr;
  88.                     break;
  89.                 default:
  90.                     error(
  91.                     "Usage: mcvert [ -[r|d|u|x] [D|U] [p|q] [s|v] ] filename...",
  92.                     NULL);
  93.                     }
  94.             }
  95.  
  96.         if (direction == BACKWARDS)
  97.             if (mode == HQX && unpit_flag) re_hqx();/* no re_pit() yet */
  98.             else if (mode == HQX) re_hqx();
  99.             else re_other(mode);
  100.         else
  101.             if (mode == HQX) un_hqx(unpit_flag);
  102.             else un_other(mode);
  103.         }
  104.     }
  105.  
  106. /* An array useful for CRC calculations that use 0x1021 as the "seed" */
  107. word magic[] = {
  108.     0x0000,  0x1021,  0x2042,  0x3063,  0x4084,  0x50a5,  0x60c6,  0x70e7,
  109.     0x8108,  0x9129,  0xa14a,  0xb16b,  0xc18c,  0xd1ad,  0xe1ce,  0xf1ef,
  110.     0x1231,  0x0210,  0x3273,  0x2252,  0x52b5,  0x4294,  0x72f7,  0x62d6,
  111.     0x9339,  0x8318,  0xb37b,  0xa35a,  0xd3bd,  0xc39c,  0xf3ff,  0xe3de,
  112.     0x2462,  0x3443,  0x0420,  0x1401,  0x64e6,  0x74c7,  0x44a4,  0x5485,
  113.     0xa56a,  0xb54b,  0x8528,  0x9509,  0xe5ee,  0xf5cf,  0xc5ac,  0xd58d,
  114.     0x3653,  0x2672,  0x1611,  0x0630,  0x76d7,  0x66f6,  0x5695,  0x46b4,
  115.     0xb75b,  0xa77a,  0x9719,  0x8738,  0xf7df,  0xe7fe,  0xd79d,  0xc7bc,
  116.     0x48c4,  0x58e5,  0x6886,  0x78a7,  0x0840,  0x1861,  0x2802,  0x3823,
  117.     0xc9cc,  0xd9ed,  0xe98e,  0xf9af,  0x8948,  0x9969,  0xa90a,  0xb92b,
  118.     0x5af5,  0x4ad4,  0x7ab7,  0x6a96,  0x1a71,  0x0a50,  0x3a33,  0x2a12,
  119.     0xdbfd,  0xcbdc,  0xfbbf,  0xeb9e,  0x9b79,  0x8b58,  0xbb3b,  0xab1a,
  120.     0x6ca6,  0x7c87,  0x4ce4,  0x5cc5,  0x2c22,  0x3c03,  0x0c60,  0x1c41,
  121.     0xedae,  0xfd8f,  0xcdec,  0xddcd,  0xad2a,  0xbd0b,  0x8d68,  0x9d49,
  122.     0x7e97,  0x6eb6,  0x5ed5,  0x4ef4,  0x3e13,  0x2e32,  0x1e51,  0x0e70,
  123.     0xff9f,  0xefbe,  0xdfdd,  0xcffc,  0xbf1b,  0xaf3a,  0x9f59,  0x8f78,
  124.     0x9188,  0x81a9,  0xb1ca,  0xa1eb,  0xd10c,  0xc12d,  0xf14e,  0xe16f,
  125.     0x1080,  0x00a1,  0x30c2,  0x20e3,  0x5004,  0x4025,  0x7046,  0x6067,
  126.     0x83b9,  0x9398,  0xa3fb,  0xb3da,  0xc33d,  0xd31c,  0xe37f,  0xf35e,
  127.     0x02b1,  0x1290,  0x22f3,  0x32d2,  0x4235,  0x5214,  0x6277,  0x7256,
  128.     0xb5ea,  0xa5cb,  0x95a8,  0x8589,  0xf56e,  0xe54f,  0xd52c,  0xc50d,
  129.     0x34e2,  0x24c3,  0x14a0,  0x0481,  0x7466,  0x6447,  0x5424,  0x4405,
  130.     0xa7db,  0xb7fa,  0x8799,  0x97b8,  0xe75f,  0xf77e,  0xc71d,  0xd73c,
  131.     0x26d3,  0x36f2,  0x0691,  0x16b0,  0x6657,  0x7676,  0x4615,  0x5634,
  132.     0xd94c,  0xc96d,  0xf90e,  0xe92f,  0x99c8,  0x89e9,  0xb98a,  0xa9ab,
  133.     0x5844,  0x4865,  0x7806,  0x6827,  0x18c0,  0x08e1,  0x3882,  0x28a3,
  134.     0xcb7d,  0xdb5c,  0xeb3f,  0xfb1e,  0x8bf9,  0x9bd8,  0xabbb,  0xbb9a,
  135.     0x4a75,  0x5a54,  0x6a37,  0x7a16,  0x0af1,  0x1ad0,  0x2ab3,  0x3a92,
  136.     0xfd2e,  0xed0f,  0xdd6c,  0xcd4d,  0xbdaa,  0xad8b,  0x9de8,  0x8dc9,
  137.     0x7c26,  0x6c07,  0x5c64,  0x4c45,  0x3ca2,  0x2c83,  0x1ce0,  0x0cc1,
  138.     0xef1f,  0xff3e,  0xcf5d,  0xdf7c,  0xaf9b,  0xbfba,  0x8fd9,  0x9ff8,
  139.     0x6e17,  0x7e36,  0x4e55,  0x5e74,  0x2e93,  0x3eb2,  0x0ed1,  0x1ef0
  140.     };
  141.  
  142. /* Report a fatal error */
  143. error(msg, name)
  144. char msg[], name[];
  145. {   fprintf(stderr, msg, name);
  146.     putc('\n', stderr);
  147.     exit(1);
  148.     }
  149.  
  150. /* replace illegal Unix characters in file name */
  151. /* make sure host file name doesn't get truncated beyond recognition */
  152. unixify(np)
  153. register byte *np;
  154. {   register ulong c;
  155.     c = strlen(np);
  156.     if (c > SYSNAMELEN - 4) c = SYSNAMELEN - 4;
  157.     np[c] = '\0';
  158.     np--;
  159.     while (c = *++np)
  160.         if (c <= ' ' || c == '/' || c > '~') *np = '_';
  161.     }
  162.  
  163. /* Convert Unix time (GMT since 1-1-1970) to Mac
  164.                                     time (local since 1-1-1904) */
  165. #define MACTIMEDIFF 0x7c25b080 /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
  166.  
  167. ulong time2mac(time)
  168. ulong time;
  169. {   struct timeb tp;
  170.     ftime(&tp);
  171.     return long2mac(time + MACTIMEDIFF
  172.                     - 60 * (tp.timezone - 60 * tp.dstflag));
  173.     }
  174.  
  175.  
  176. /* This procedure copies the input file to the output file, basically, although
  177.     in TEXT mode it changes LF's to CR's and in any mode it forges a Mac info 
  178.     header.  Author type for TEXT mode can come from the MAC_EDITOR environ-
  179.     ment variable if it is defined. */
  180.  
  181. un_other(mode)
  182. int mode;
  183. {   register ulong b;
  184.     register ulong nchars;
  185.     char txtfname[BINNAMELEN], binfname[BINNAMELEN];
  186.     FILE *txtfile, *binfile; 
  187.     char *suffix;
  188.     struct stat stbuf;
  189.     info_header info;
  190.     int extra_chars;
  191.     ulong dlen, rlen, mtim, ctim;
  192.  
  193.     if (mode == DATA) suffix = ".data";
  194.     else if (mode == RSRC) suffix = ".rsrc";
  195.     else suffix = ".text";
  196.  
  197.     while (hqxnames_left[0][0] != '-') {
  198.  
  199.         strcpy(txtfname, *hqxnames_left++);
  200.         if (!(txtfile = fopen(txtfname, "r"))) {
  201.             /* Maybe we are supposed to figure out the suffix ourselves? */
  202.             strcat(txtfname, suffix);
  203.             if (!(txtfile = fopen(txtfname, "r")))
  204.                 error("Cannot open %s", txtfname);
  205.             }
  206.  
  207.         if (stat(txtfname, &stbuf))
  208.             error("Cannot read %s", txtfname);
  209.  
  210.         /* stuff header data into the info header */
  211.         bzero(&info, sizeof(info_header));
  212.         info.nlen = strlen(txtfname);
  213.         info.nlen = (info.nlen > NAMELEN) ? NAMELEN : info.nlen;
  214.     info.name[info.nlen] = '\0';
  215.         strcpy(info.name, txtfname);           /* name */
  216.         mtim = time2mac(stbuf.st_mtime);
  217.         ctim = time2mac(stbuf.st_ctime);
  218.         bcopy(&mtim, info.mtim, 4);
  219.         bcopy(&ctim, info.ctim, 4);
  220.  
  221.         if (mode == RSRC) {
  222.             /* dlen is already zero */
  223.             rlen = long2mac(stbuf.st_size);
  224.             bcopy(&rlen, info.rlen, 4);
  225.             bcopy("APPL", info.type, 4);
  226.             bcopy("CCOM", info.auth, 4);
  227.             }
  228.         else {
  229.             dlen = long2mac(stbuf.st_size);
  230.             bcopy(&dlen, info.dlen, 4);
  231.             /* rlen is already zero */
  232.             bcopy("TEXT", info.type, 4);
  233.             if (mode == DATA) bcopy("????", info.auth, 4);
  234.             else bcopy(text_author, info.auth, 4);
  235.             }
  236.  
  237.         /* Create the .bin file and write the info to it */
  238.         sprintf(binfname, "%s/%s%s", dir, txtfname, ext);
  239.         if ((binfile = fopen(binfname, "w")) == NULL)
  240.             error("Cannot open %s", binfname);
  241.         fprintf(verbose,
  242.                 "Converting     %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  243.                 txtfname, info.type, info.auth);
  244.         fwrite(&info, sizeof(info), 1, binfile);
  245.  
  246.         nchars = stbuf.st_size;
  247.         extra_chars = 127 - (nchars+127) % 128;
  248.         if (mode == TEXT) while (nchars--) {
  249.             b = getc(txtfile);
  250.             if (b == LF) b = CR;
  251.             putc(b, binfile);
  252.             }
  253.         else while (nchars--) putc(getc(txtfile), binfile);
  254.  
  255.         while (extra_chars--) putc(0, binfile);
  256.         fclose(binfile);
  257.         fclose(txtfile);
  258.         }
  259.     }
  260.  
  261. /* This procedure copies the input file to the output file, basically, although
  262.     in TEXT mode it changes CR's to LF's and in any mode it skips over the Mac
  263.     info header. */
  264.  
  265. re_other(mode)
  266. int mode;
  267. {   register ulong b;
  268.     register ulong nchars;
  269.     char txtfname[BINNAMELEN], binfname[BINNAMELEN];
  270.     FILE *txtfile, *binfile; 
  271.     char *suffix;
  272.     info_header info;
  273.  
  274.     if (mode == DATA) suffix = ".data";
  275.     else if (mode == RSRC) suffix = ".rsrc";
  276.     else suffix = ".text";
  277.  
  278.     while (hqxnames_left[0][0] != '-') {
  279.  
  280.         strcpy(binfname, *hqxnames_left++);
  281.         if ((binfile = fopen(binfname, "r")) == NULL) {
  282.             /* Maybe we are supposed to figure out the suffix ourselves? */
  283.             strcat(binfname, ext);
  284.             if (!(binfile = fopen(binfname, "r")))
  285.                 error("Cannot open %s", binfname);
  286.             }
  287.  
  288.         /* Read the info from the .bin file, create the output file */
  289.         fread(&info, sizeof(info), 1, binfile);
  290.         strncpy(txtfname, info.name, info.nlen);
  291.     txtfname[info.nlen] = '\0';
  292.         fprintf(verbose,
  293.                 "Converting     %-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  294.                 txtfname, info.type, info.auth);
  295.         if ((txtfile = fopen(txtfname, "r")) == NULL) {
  296.             if ((txtfile = fopen(txtfname, "w")) == NULL)
  297.                 error("Cannot open %s", txtfname);
  298.             }
  299.         else {
  300.             fclose(txtfile);
  301.             strcat(txtfname, suffix);
  302.             if ((txtfile = fopen(txtfname, "w")) == NULL)
  303.                 error("Cannot open %s", txtfname);
  304.             }
  305.  
  306.         nchars = mac2long(* (ulong *) info.dlen);
  307.         if (mode == TEXT) while (nchars--) {
  308.             b = getc(binfile);
  309.             if (b == CR) b = LF;
  310.             putc(b, txtfile);
  311.             }
  312.         else if (mode == DATA) while (nchars--)
  313.             putc(getc(binfile), txtfile);
  314.         else {
  315.             while (nchars--) getc(binfile);
  316.             nchars = mac2long(* (ulong *) info.rlen);
  317.             while (nchars--) putc(getc(binfile), txtfile);
  318.             }
  319.  
  320.         fclose(binfile);
  321.         fclose(txtfile);
  322.         }
  323.     }
  324.