home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / EMULATOR / UNIX / CAIN2 / HEXCOM.C < prev    next >
C/C++ Source or Header  |  2000-06-30  |  2KB  |  118 lines

  1. /*
  2. hexcom.c
  3. makes a com file from a hex file.  Uses standard I/O
  4.  
  5. Sample input file:
  6.  
  7. :100100003EFF87D200003E0187DA0000AFC2000048
  8. :100110003CCA00003DFA00003DF200003E7F3CE298
  9. :100120000000E6FFEA00001132010E09CD0500C310
  10. :0701300000004F4B0D0A24F3
  11. :0000000000
  12.  
  13. */
  14.  
  15. #include    <stdio.h>
  16. #include    <string.h>
  17. #include    <stdlib.h>
  18. #include    <stdarg.h>
  19.  
  20. static char    *progname;
  21.  
  22. static void    err_exit(const char *s, ...)
  23. {
  24.     va_list    ap;
  25.  
  26.     fprintf(stderr, "%s: ", progname);
  27.     va_start(ap, s);
  28.     vfprintf(stderr, s, ap);
  29.     va_end(ap);
  30.     exit(1);
  31. }
  32.  
  33. static int    gethex(const char *p)
  34. {
  35.     int        r, c;
  36.  
  37.     if (!(c = *p++))
  38.         return(-1);
  39.  
  40.     if (c >= 'A')
  41.         c += 9;
  42.  
  43.     r = (c & 0x0f) << 4;
  44.  
  45.     if (!(c = *p))
  46.         return(-1);
  47.  
  48.     if (c >= 'A')
  49.         c += 9;
  50.  
  51.     return(r + (c & 0x0f));
  52. }
  53.  
  54. static void    hexcom(FILE *in_fp, FILE *out_fp)
  55. {
  56.     char    *p;
  57.     int        count, b;
  58.  
  59.     while ((p = getline(in_fp, 0)) != NULL)
  60.     {
  61.         if (*p++ != ':')
  62.             err_exit("Line doesn't start with colon");
  63.  
  64.         if ((count = gethex(p)) == -1)
  65.             err_exit("Invalid byte count in line");
  66.  
  67.         p += 8;
  68.  
  69.         while (count--)
  70.         {
  71.             if ((b = gethex(p)) == -1)
  72.                 err_exit("invalid byte in line");
  73.  
  74.             p += 2;
  75.             putc(b, out_fp);
  76.         }
  77.     }
  78. }
  79.  
  80. int        main(int argc, char **argv)
  81. {
  82.     FILE    *in_fp, *out_fp;
  83.     char    buf[256], *p, *q;
  84.     int        k;
  85.  
  86.     if ((progname = strrchr(argv[0], '/')) == NULL)
  87.         progname = argv[0];
  88.     else
  89.         progname++;
  90.  
  91.     if (argc == 1)
  92.     {
  93.         hexcom(stdin, stdout);
  94.         return(0);
  95.     }
  96.  
  97.     for (k = 1; k < argc; k++)
  98.     {
  99.         strcpy(buf, argv[k]);
  100.  
  101.         if ((p = strrchr(buf, '.')) == NULL ||
  102.             ((q = strrchr(buf, '/')) != NULL && p < q))
  103.                 strcat(buf, ".hex");
  104.  
  105.         if ((in_fp = fopen(buf, "rt")) == NULL)
  106.             err_exit("Can't open %s: %s", buf, strerror(errno));
  107.  
  108.         strcpy(strrchr(buf, '.'), ".com");
  109.  
  110.         if ((out_fp = fopen(buf, "wb")) == NULL)
  111.             err_exit("Can't open %s: %s", buf, strerror(errno));
  112.  
  113.         hexcom(in_fp, out_fp);
  114.     }
  115.  
  116.     return(0);
  117. }
  118.