Logo Search packages:      
Sourcecode: cbmconvert version File versions  Download package

int main ( int  argc,
char **  argv 
)

The main program

Parameters:
argc number of command-line arguments
argv contents of the command-line arguments
Returns:
0 on success, nonzero on error

Definition at line 296 of file main.c.

References Always, ArchiveC2N, archiveFilename, ArchiveLynx, ArNoSpace, ArOK, changeDisks, CloseImage(), currentFilename, deleteArchive(), DirEntFindOrCreate, DirEntOnlyCreate, Errors, Everything, Im1541, Im1571, Im1581, imageType(), ImNoSpace, ImOK, Image::name, Never, newArchive(), OpenImage(), RdNoSpace, RdOK, read_file_t(), ReadARC, ReadArkive, ReadC2N, ReadCpmImage, ReadImage, ReadLynx, ReadNative, ReadPC64, ReadT64, Sometimes, verbosityLevel, Warnings, Write9660, writeArchiveFunc, WriteCpmImage, writeFile(), writeFunc, WriteImage, writeImageFunc, writeLog(), WriteNative, and WritePC64.

{
  read_file_t* readFunc = ReadNative;
  FILE* file;
  char* prog = *argv; /* name of the program */
  int retval = 0; /* return status */

  /* process the option flags */
  for (argv++; argc > 1 && **argv == '-'; argv++, argc--) {
    char* opts = *argv;

    if (!strcmp (opts, "--")) { /* disable processing further options */
      argv++;
      argc--;
      break;
    }

    while (*++opts) /* process all flags */
      switch (*opts) {
      case 'v':
      switch (opts[1]) {
      case 'v':
      case '2':
        verbosityLevel = Everything;
        break;
      case 'w':
      case '1':
        verbosityLevel = Warnings;
        break;
      case '0':
        verbosityLevel = Errors;
        break;
      default:
        goto Usage;
      }
      opts++;
      break;

      case 'i':
      switch (opts[1]) {
      case '0':
        changeDisks = Never;
        break;
      case '1':
        changeDisks = Sometimes;
        break;
      case '2':
        changeDisks = Always;
        break;
      default:
        goto Usage;
      }
      opts++;
      break;

      case 'n':
      readFunc = ReadNative;
      break;
      case 'p':
      readFunc = ReadPC64;
      break;
      case 'a':
      readFunc = ReadARC;
      break;
      case 'k':
      readFunc = ReadArkive;
      break;
      case 'l':
      readFunc = ReadLynx;
      break;
      case 't':
      readFunc = ReadT64;
      break;
      case 'c':
      readFunc = ReadC2N;
      break;
      case 'd':
      readFunc = ReadImage;
      break;
      case 'm':
      readFunc = ReadCpmImage;
      break;
      case 'I':
      writeFunc = Write9660;
      break;
      case 'P':
      writeFunc = WritePC64;
      break;
      case 'N':
      writeFunc = WriteNative;
      break;
      case 'L':
      if (image || archive || argc <= 2)
        goto Usage;

      archive = newArchive();
      writeArchiveFunc = ArchiveLynx;
      archiveFilename = *++argv;argc--;
      break;
      case 'C':
      if (image || archive || argc <= 2)
        goto Usage;

      archive = newArchive();
      writeArchiveFunc = ArchiveC2N;
      archiveFilename = *++argv;argc--;
      break;
      case 'M':
      case 'D':
      if (archive)
        goto Usage;

      if (argc > 2) {
        enum ImageType im = Im1541;

        switch (opts[1]) {
        case '4':
          im = Im1541;
          break;
        case '7':
          im = Im1571;
          break;
        case '8':
          im = Im1581;
          break;
        default:
          goto Usage;
        }

        writeFunc = 0;
        writeImageFunc = *opts == 'M' ? WriteCpmImage : WriteImage;

        opts++;
        argc--;
        argv++;

        {
          enum DirEntOpts dopts = DirEntOnlyCreate;
          if (opts[1] == 'o') {
            dopts = DirEntFindOrCreate;
            opts++;
          }

          if (OpenImage (*argv, &image, im, dopts) != ImOK) {
            fprintf (stderr, "Could not open the %s%s image '%s'.\n",
                   writeImageFunc == WriteCpmImage ? "CP/M " : "",
                   imageType (im), *argv);
            return 2;
          }
        }
      }
      else
        goto Usage;

      break;

      default:
      goto Usage;
      }
  }

  if (argc < 2) {
  Usage:
    fprintf (stderr,
           "cbmconvert 2.1.2 - Commodore archive converter\n"
           "Usage: %s [options] file(s)\n", prog);

    fputs ("Options: -I: Create ISO 9660 compliant file names.\n"
         "         -P: Output files in PC64 format.\n"
         "         -N: Output files in native format.\n"
         "         -L archive.lnx: Output files in Lynx format.\n"
         "         -C archive.c2n: Output files in Commodore C2N format.\n"
         "         -D4 imagefile: Write to a 1541 disk image.\n"
         "         -D4o imagefile: Ditto, overwriting existing files.\n"
         "         -D7[o] imagefile: Write to a 1571 disk image.\n"
         "         -D8[o] imagefile: Write to a 1581 disk image.\n"
         "         -M4[o] imagefile: Write to a 1541 CP/M disk image.\n"
         "         -M7[o] imagefile: Write to a 1571 CP/M disk image.\n"
         "         -M8[o] imagefile: Write to a 1581 CP/M disk image.\n"
         "\n"
         "         -i2: Switch disk images on out of space or duplicate file name.\n"
         "         -i1: Switch disk images on out of space.\n"
         "         -i0: Never switch disk images.\n"
         "\n"
         "         -n: input files in native format.\n"
         "         -p: input files in PC64 format.\n"
         "         -a: input files in ARC/SDA format.\n"
         "         -k: input files in Arkive format.\n"
         "         -l: input files in Lynx format.\n"
         "         -t: input files in T64 format.\n"
         "         -c: input files in Commodore C2N format.\n"
         "         -d: input files in disk image format.\n"
         "         -m: input files in C128 CP/M disk image format.\n"
         "\n"
         "         -v2: Verbose mode.  Display all messages.\n"
         "         -v1: Display warnings in addition to errors.\n"
         "         -v0: Display error messages only.\n"
         "         --: Stop processing any further options.\n",
         stderr);

    return 1;
  }

  /* Process the files. */

  for (; --argc; argv++) {
    enum RdStatus status;
    currentFilename = *argv;

    if (!(file = fopen (currentFilename, "rb"))) {
      fprintf (stderr, "fopen '%s': %s\n", currentFilename, strerror(errno));
      retval = 2;
      continue;
    }

    status = (*readFunc) (file, *argv, writeFile, writeLog);
    fclose (file);

    switch (status) {
    case RdOK:
      writeLog (Everything, 0, "Archive extracted.");
      break;

    case RdNoSpace:
      writeLog (Errors, 0, "out of space.");
      if (image || archive)
      goto write;
      else
      return 3;

    default:
      writeLog (Errors, 0, "unexpected error.");
      retval = 4;
      if (image || archive)
      goto write;
      else
      return retval;
    }
  }

write:
  if (image) {
    switch (CloseImage (image)) {
    case ImOK:
      writeLog (Everything, 0, "Wrote image file \"%s\"", image->name);
      break;

    case ImNoSpace:
      writeLog (Errors, 0, "Out of space while writing image file \"%s\"!",
            image->name);
      return 3;

    default:
      writeLog (Errors, 0, "Unexpected error while writing image \"%s\"!",
            image->name);
      return 4;
    }

    free (image);
    image = 0;
  }

  if (archive) {
    switch ((*writeArchiveFunc) (archive, archiveFilename)) {
    case ArOK:
      writeLog (Everything, 0, "Wrote archive file \"%s\"",
            archiveFilename);
      break;

    case ArNoSpace:
      writeLog (Everything, 0,
            "Out of space while writing archive file \"%s\"!",
            archiveFilename);
      return 3;

    default:
      writeLog (Everything, 0,
            "Unexpected error while writing image \"%s\"!",
            archiveFilename);
      return 4;
    }

    deleteArchive (archive);
    archive = 0;
  }

  if (verbosityLevel == Everything)
    fprintf (stderr, "%s: all done\n", prog);

  return retval;
}


Generated by  Doxygen 1.6.0   Back to index