Annotation of hatari/tools/hmsa/hmsa.c, revision 1.1.1.5

1.1       root        1: /*
1.1.1.5 ! root        2:   Hatari tool: MSA and ST disk image creator and converter - hmsa.c
1.1       root        3:  
                      4:   This file is distributed under the GNU Public License, version 2 or at
                      5:   your option any later version. Read the file gpl.txt for details.
                      6: */
                      7: 
                      8: #include <stdio.h>
                      9: #include <stdlib.h>
                     10: #include <stdarg.h>
                     11: #include <string.h>
                     12: 
                     13: #include "hmsa.h"
1.1.1.2   root       14: #include "main.h"      /* bool etc. */
1.1.1.5 ! root       15: #include "createBlankImage.h"
1.1       root       16: #include "file.h"
                     17: #include "msa.h"
                     18: 
1.1.1.3   root       19: /* prototypes for dummy log/alert functions below */
                     20: #include "dialog.h"
                     21: #include "log.h"
1.1       root       22: 
                     23: /**
1.1.1.5 ! root       24:  * Print suitable output prefix based on log level
        !            25:  */
        !            26: static void print_prefix(LOGTYPE nType)
        !            27: {
        !            28:        const char *sType;
        !            29:        switch (nType) {
        !            30:        case LOG_FATAL:
        !            31:        case LOG_ERROR:
        !            32:                sType = "ERROR: ";
        !            33:                break;
        !            34:        case LOG_WARN:
        !            35:                sType = "WARNING: ";
        !            36:                break;
        !            37:        default:
        !            38:                return;
        !            39:        }
        !            40:        fputs(sType, stdout);
        !            41: }
        !            42: 
        !            43: /**
        !            44:  * Output Hatari log string.
1.1       root       45:  */
1.1.1.3   root       46: extern void Log_Printf(LOGTYPE nType, const char *psFormat, ...)
1.1       root       47: {
                     48:        va_list argptr;
1.1.1.5 ! root       49:        print_prefix(nType);
        !            50:        va_start(argptr, psFormat);
        !            51:        vfprintf(stdout, psFormat, argptr);
        !            52:        va_end(argptr);
1.1       root       53: }
                     54: 
1.1.1.5 ! root       55: /**
        !            56:  * Output Hatari Alert dialog string.
        !            57:  */
        !            58: extern void Log_AlertDlg(LOGTYPE nType, const char *psFormat, ...)
        !            59: {
        !            60:        va_list argptr;
        !            61:        print_prefix(nType);
        !            62:        va_start(argptr, psFormat);
        !            63:        vfprintf(stdout, psFormat, argptr);
        !            64:        va_end(argptr);
        !            65:        /* Add a new-line if necessary: */
        !            66:        if (psFormat[strlen(psFormat)-1] != '\n')
        !            67:                fputs("\n", stdout);
        !            68: }
1.1       root       69: 
                     70: /**
1.1.1.5 ! root       71:  * Output Hatari Query dialog string.
1.1       root       72:  */
                     73: int DlgAlert_Query(const char *text)
                     74: {
                     75:        puts(text);
                     76:        return TRUE;
                     77: }
                     78: 
1.1.1.5 ! root       79: /**
        !            80:  * Create MSA or ST image of requested size.
        !            81:  * return error string or NULL for success.
        !            82:  */
        !            83: static const char* create_image(const char *filename, const char *sizeid)
        !            84: {
        !            85:        int tracks, sectors, sides;
        !            86:        tracks = 80;
        !            87:        
        !            88:        if (strcasecmp(sizeid, "ss") == 0) {
        !            89:                sides = 1;
        !            90:                sectors = 9;
        !            91:        } else if (strcasecmp(sizeid, "ds") == 0) {
        !            92:                sides = 2;
        !            93:                sectors = 9;
        !            94:        } else if (strcasecmp(sizeid, "hd") == 0) {
        !            95:                sides = 2;
        !            96:                sectors = 18;
        !            97:        } else if (strcasecmp(sizeid, "ed") == 0) {
        !            98:                sides = 2;
        !            99:                sectors = 36;
        !           100:        } else {
        !           101:                return "ERROR: given disk size isn't one of supported ones!\n";
        !           102:        }
        !           103:        if (CreateBlankImage_CreateFile(filename, tracks, sectors, sides)) {
        !           104:                return NULL;
        !           105:        }
        !           106:        return "ERROR: Disk creation failed.\n";
        !           107: }
        !           108: 
        !           109: /**
        !           110:  * Print program usage
        !           111:  */
        !           112: static void usage(const char *name)
        !           113: {
        !           114:                printf("\n\
        !           115: Hatari MSA (Magic Shadow Archiver) / ST disk image creator & converter v0.3.\n\
        !           116: \n\
        !           117: Usage:  %s FILENAME [DISK SIZE]\n\
        !           118: \n\
        !           119: If you give only one parameter - the file name of an existing MSA\n\
        !           120: or ST disk image, this image will be converted to the other disk image\n\
        !           121: format under a suitable new file name.  Disk image format is recognized\n\
        !           122: based on the file name extension (.msa or .st).\n\
        !           123: \n\
        !           124: If the given file doesn't exist and you give also a disk size\n\
        !           125: (SS, DS, HD, ED), an empty disk of the given size will be created.\n\
        !           126: \n\
        !           127: This software is distributed under the GNU Public License, version 2 or\n\
        !           128: at your option any later version. Please read the file gpl.txt for details.\n\
        !           129: \n",
        !           130:                       name);
        !           131: }
1.1       root      132: 
                    133: /**
1.1.1.5 ! root      134:  * Command line argument parsing and new disk creation
1.1       root      135:  */
                    136: int main(int argc, char *argv[])
                    137: {
1.1.1.5 ! root      138:        bool isMsa;
1.1       root      139:        int retval;
1.1.1.5 ! root      140:        long disksize;
        !           141:        unsigned char *diskbuf;
        !           142:        const char *srcfile, *srcdot;
        !           143:        char *dstfile, *dstdot;
        !           144: 
        !           145:        if(argc < 2 || argv[1][0] == '-') {
        !           146:                usage(argv[0]);
1.1       root      147:                return 0;
                    148:        }
                    149: 
1.1.1.5 ! root      150:        srcfile = argv[1];
        !           151:        srcdot = strchr(srcfile, '.');
        !           152:        if(srcdot == NULL) {
        !           153:                usage(argv[0]);
        !           154:                fprintf(stderr, "ERROR: extension missing for file name %s!\n", argv[1]);
1.1       root      155:                return -1;
                    156:        }
                    157: 
1.1.1.5 ! root      158:        if (strcasecmp(srcdot, ".msa") == 0) {
        !           159:                isMsa = true;
        !           160:        } else if (strcasecmp(srcdot, ".st") == 0) {
        !           161:                isMsa = false;
        !           162:        } else {
        !           163:                usage(argv[0]);
        !           164:                fprintf(stderr, "ERROR: unrecognized file name extension %s (not .msa or .st)!\n", srcdot);
1.1       root      165:                return -1;
                    166:        }
                    167: 
1.1.1.5 ! root      168:        if (!File_Exists(srcfile)) {
        !           169:                const char *errstr;
        !           170:                if (argc != 3) {
        !           171:                        usage(argv[0]);
        !           172:                        fprintf(stderr, "ERROR: disk size for the new disk image not given!\n");
1.1       root      173:                        return -1;
                    174:                }
1.1.1.5 ! root      175:                errstr = create_image(srcfile, argv[2]);
        !           176:                if (errstr) {
        !           177:                        usage(argv[0]);
        !           178:                        fputs(errstr, stderr);
1.1       root      179:                        return -1;
                    180:                }
1.1.1.5 ! root      181:                return 0;
        !           182:        }
1.1       root      183: 
1.1.1.5 ! root      184:        dstfile = malloc(strlen(srcfile) + 6);
        !           185:        if (dstfile == NULL) {
        !           186:                fprintf(stderr, "ERROR: No memory for new disk name!\n");
        !           187:                return -1;
        !           188:        }
1.1       root      189: 
1.1.1.5 ! root      190:        strcpy(dstfile, srcfile);
        !           191:        dstdot = strchr(dstfile, '.');
        !           192:        if (isMsa) {
        !           193:                /* Convert MSA to ST disk image */
        !           194:                strcpy(dstdot, ".st");
        !           195:        } else {
        !           196:                /* Convert ST to MSA disk image */
        !           197:                strcpy(dstdot, ".msa");
        !           198:        }
1.1       root      199: 
1.1.1.5 ! root      200:        if (File_Exists(dstfile)) {
        !           201:                fprintf(stderr, "ERROR: Destination disk image %s exists already!\n", dstfile);
        !           202:                free(dstfile);
        !           203:                return -1;
        !           204:        }
        !           205:        
        !           206:        if (isMsa) {
        !           207:                /* Read the source disk image */
        !           208:                diskbuf = MSA_ReadDisk(srcfile, &disksize);
        !           209:                if (!diskbuf || disksize < 512*8) {
        !           210:                        fprintf(stderr, "ERROR: could not read MSA disk %s!\n", srcfile);
        !           211:                        retval = -1;
        !           212:                } else {
        !           213:                        printf("Converting %s to %s (%li Bytes).\n", srcfile, dstfile, disksize);
        !           214:                        retval = File_Save(dstfile, diskbuf, disksize, FALSE);
        !           215:                }
        !           216:        } else {
        !           217:                /* Just read disk image directly into buffer */
        !           218:                disksize = 0;
        !           219:                diskbuf = File_Read(srcfile, &disksize, NULL);
        !           220:                if (!diskbuf || disksize < 512*8) {
        !           221:                        fprintf(stderr, "ERROR: could not read ST disk %s!\n", srcfile);
        !           222:                        retval = -1;
        !           223:                } else {
        !           224:                        printf("Converting %s to %s (%li Bytes).\n", srcfile, dstfile, disksize);
        !           225:                        retval = MSA_WriteDisk(dstfile, diskbuf, disksize);
        !           226:                }
1.1       root      227:        }
                    228: 
1.1.1.5 ! root      229:        if (diskbuf) {
        !           230:                free(diskbuf);
        !           231:        }
        !           232:        free(dstfile);
1.1       root      233: 
                    234:        return retval;
                    235: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.