home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / disk / misc / TransADF.lha / TransADF / Source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  7.5 KB  |  317 lines

  1. /* main.c - Entry point plus startup and shutdown routines
  2. ** Copyright (C) 1997,1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. /*----------------*/
  20. /* Main procedure */
  21. /*----------------*/
  22.  
  23. /* COMPILE_LITE takes precedence over COMPILE_RT */
  24. #ifdef COMPILE_LITE
  25. #  ifdef COMPILE_RT
  26. #    undef COMPILE_RT
  27. #  endif /* COMPILE_RT */
  28. #endif /* COMPILE_LITE */
  29.  
  30. #include <exec/types.h>
  31. #include <exec/io.h>
  32. #ifdef COMPILE_RT
  33. #  include <exec/libraries.h>
  34. #endif /* COMPILE_RT */
  35. #include <dos/dos.h>
  36. #include <dos/dosextens.h>
  37. #include <clib/exec_protos.h>
  38. #include <clib/dos_protos.h>
  39.  
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43.  
  44. #include "main.h"
  45. #include "args.h"
  46. #include "mem_chunks.h"
  47. #include "device.h"
  48. #include "read_disk.h"
  49. #include "write_disk.h"
  50. #ifndef COMPILE_LITE
  51. #  include "defl_disk.h"
  52. #  include "infl_disk.h"
  53. #endif /* COMPILE_LITE */
  54. #include "util.h"
  55. #include "errors.h"
  56. #include "version.h"
  57.  
  58.  
  59. /*---------------------------------*/
  60. /* Global constants and structures */
  61. /*---------------------------------*/
  62.  
  63. const char breakText[]   = "***Break";
  64.  
  65. /*----------------------------*/
  66. /* Standatd Input/Ouput/Error */
  67. /*----------------------------*/
  68.  
  69. BPTR StdIn, StdOut, StdErr;
  70. STRPTR ProgName;
  71.  
  72. /*
  73. ** Set handles for DOS Library StdIO functions, as well as
  74. ** find our command name.
  75. */
  76. void setStdIO (void)
  77. {
  78.   struct Process *Proc = (struct Process *)FindTask (NULL);
  79.   
  80.   StdIn  = Proc->pr_CIS;  /* Quicker than Input() or Output() */
  81.   StdOut = Proc->pr_COS;
  82.   StdErr = Proc->pr_CES;
  83.   if (!StdErr)
  84.   {
  85.     /* Use the file handle from the file number of stdio FILE stderr */
  86.     StdErr = (BPTR) fdtofh (fileno (stderr));
  87.     if (!StdErr) StdErr = StdOut;
  88.   }
  89. }
  90.  
  91.  
  92. /*-------------------------------------------*/
  93. /* Global variables and associated functions */
  94. /*-------------------------------------------*/
  95.  
  96. #ifdef COMPILE_RT
  97. /* z.library base pointer */
  98. struct Library *ZBase;
  99. #endif /* COMPILE_RT */
  100.  
  101. /* Device information. */
  102. struct DeviceInfo *devInfo;
  103.  
  104. /* files */
  105. BPTR   ADFile;
  106. STRPTR DOSDev;
  107.  
  108. /* io */
  109. struct IOExtTD *diskReq;
  110.  
  111.  
  112. /*
  113. ** Initialise all globals to their default values.
  114. */
  115. void initGlobals (void)
  116. {
  117. #ifdef COMPILE_RT
  118.   ZBase = NULL;
  119. #endif /* COMPILE_RT */
  120.   
  121.   setStdIO();
  122.   
  123.   /* Iniitalise our memory list */
  124.   initMemChunkList();
  125.   
  126.   devInfo = NULL;
  127.   ADFile  = NULL;
  128.   DOSDev  = NULL;
  129.   diskReq = NULL;
  130. }
  131.  
  132.  
  133. /*
  134. ** Exit cleanly.
  135. */
  136. void cleanExit (ULONG rc, LONG rc2)
  137. {
  138.   /* Free all allocated memory */
  139.   deleteMemChunkList();
  140.   
  141.   closeDev (diskReq);
  142.   
  143.   if (DOSDev) Inhibit (DOSDev,FALSE);
  144.   if (ADFile) Close (ADFile);
  145.   
  146.   /* Turn the cursor on */
  147.   FPuts (StdOut, "\033[1 p"); 
  148.   Flush (StdOut);
  149.   
  150. #ifdef COMPILE_RT
  151.   if (ZBase) CloseLibrary (ZBase);
  152. #endif /* COMPILE_RT */
  153.   
  154.   SetIoErr (rc2);
  155.   exit (rc);
  156. }
  157.  
  158.  
  159. /*---------------*/
  160. /* Main function */
  161. /*---------------*/
  162. int main (int argc, char *argv[])
  163. {
  164.   struct ADF_Packet adfPkt;
  165.   struct TA_Args *taArgs;
  166.   
  167.   STRPTR ADFileName;
  168.   ULONG  startTrack, endTrack;
  169.   ULONG  fileType = 0;
  170.   
  171.   BOOL   WriteDisk;
  172.   LONG   ADFOpenMode = MODE_NEWFILE;  /* Default is to write to file */
  173.   LONG   DOSError;
  174.  
  175. #ifndef COMPILE_LITE
  176.   STRPTR origName;
  177.   ULONG  cLevel;
  178. #endif /* COMPILE_LITE */
  179.   
  180.  
  181.   /* Initialise any global data */
  182.   initGlobals();
  183.   ProgName = FilePart (argv[0]);
  184.  
  185.   /* Collect arguments */
  186.   taArgs = getArgs ();
  187.   if (!taArgs) outputUsage ();
  188.  
  189. #ifdef COMPILE_RT
  190.   /* Open z.library */
  191.   ZBase = OpenLibrary ("z.library",0);
  192.   if (!ZBase)
  193.   {
  194.     FPrintf (StdErr, "%s: Couldn't open z.library.\n", ProgName);
  195.     cleanExit (RETURN_FAIL, NULL);
  196.   }
  197. #endif /* COMPILE_RT */
  198.   
  199.   /* Collect information about the given DOS device */
  200.   devInfo = getDeviceInfo (taArgs->Drive);
  201.   if (!devInfo)
  202.   {
  203.     FPrintf (StdErr, "%s: Error - Don't know how to talk to %s.\n",
  204.                      ProgName, taArgs->Drive);
  205.     cleanExit (RETURN_FAIL, NULL);
  206.   }
  207.   
  208.   /* Analyse arguments */
  209.   ADFileName = taArgs->File;
  210.   startTrack = taArgs->Start;
  211.   endTrack   = taArgs->End;
  212.   WriteDisk  = taArgs->WriteDisk;
  213.   
  214.   /* Make the tracks correct */
  215.   if (startTrack == 0) startTrack = devInfo->lowTrack;
  216.   else                 startTrack = startTrack * devInfo->numHeads;
  217.   if (endTrack == -1)  endTrack = devInfo->highTrack;
  218.   else                 endTrack = ((endTrack + 1) * devInfo->numHeads) - 1;
  219.   
  220. #ifndef COMPILE_LITE
  221.   if      (taArgs->ZLib)  fileType = FT_ZLIB;  
  222.   else if (taArgs->GZip)  fileType = FT_GZIP;  
  223.   else if (taArgs->PKZip) 
  224.   {
  225.     if (taArgs->PKZAdd)   fileType = FT_PKZIP_ADD;
  226.     else                  fileType = FT_PKZIP;
  227.   }
  228.   origName = taArgs->OrigName;  
  229.   cLevel = taArgs->Level;
  230. #endif /* COMPILE_LITE */
  231.   
  232.   if ((startTrack < 0) || (startTrack > endTrack))
  233.   {
  234.     FPrintf (StdErr, "%s: Invalid Start/End track.\n", ProgName);
  235.     cleanExit (RETURN_FAIL, NULL);
  236.   }
  237.   
  238.   /* Inhibit DOS access to the drive we'll be operating on */  
  239.   DOSDev = taArgs->Drive;
  240.   if ( !Inhibit (DOSDev, DOSTRUE) )
  241.   {
  242.     DOSError = IoErr();
  243.     FPrintf (StdErr, "%s: Error - Couldn't inhibit DOS access to %s.\n",
  244.                      ProgName, DOSDev);
  245.     DOSDev = NULL;
  246.     cleanExit (RETURN_FAIL, DOSError);
  247.   }
  248.   
  249.   /* Open the Amiga Disk File */
  250.   if (WriteDisk) ADFOpenMode = MODE_OLDFILE;  /* Reading from file */
  251.   else if (fileType == FT_PKZIP_ADD) ADFOpenMode = MODE_READWRITE;
  252.   ADFile = Open (ADFileName, ADFOpenMode);
  253.   if (!ADFile)
  254.   {
  255.     DOSError = IoErr();
  256.     FPrintf (StdErr, "%s: Couldn't open %s for %s - ", ProgName, ADFileName,
  257.                      (WriteDisk ? "input" : "output"));
  258.     reportDOSError (DOSError);
  259.     cleanExit (RETURN_FAIL, DOSError);
  260.   }
  261.   
  262.   /* Attemp to open device */
  263.   diskReq = openDev (devInfo->deviceName, devInfo->deviceUnit);
  264.   if (!diskReq)
  265.     /* A message has already been printed */
  266.     cleanExit (RETURN_FAIL, NULL);
  267.   
  268.   /* The device in now open and ready for use */
  269.   
  270.   /* Turn off the cursor */
  271.   FPuts (StdOut, "\033[0 p"); Flush (StdOut);
  272.   
  273.   /* Load packet */
  274.   adfPkt.devInfo    = devInfo;
  275.   adfPkt.diskReq    = diskReq;
  276.   adfPkt.ADFile     = ADFile;
  277.   adfPkt.ADFileName = ADFileName;
  278.   adfPkt.startTrack = startTrack;
  279.   adfPkt.endTrack   = endTrack;
  280.   adfPkt.verify     = taArgs->Verify;
  281.   adfPkt.format     = taArgs->Format;
  282.     
  283.   if (WriteDisk)
  284.   {
  285.     /* We're reading from the file and writing to the disk */
  286. #ifndef COMPILE_LITE
  287.     fileType = getFileType (ADFile);
  288.     
  289.     if ((fileType == FT_ZLIB) ||
  290.         (fileType == FT_GZIP) ||
  291.         (fileType == FT_PKZIP))
  292.       inflDisk (&adfPkt, origName, fileType);
  293.     else
  294. #endif /* COMPILE_LITE */
  295.       writeDisk (&adfPkt);
  296.   }
  297.   else
  298.   {
  299.     /* We're reading from the disk and writing to the file */
  300. #ifndef COMPILE_LITE
  301.     if (fileType)
  302.       deflDisk (&adfPkt, cLevel, origName, fileType);
  303.     else
  304. #endif /* COMPILE_LITE */
  305.       readDisk (&adfPkt);
  306.   }
  307.   
  308.   
  309.   FPuts (StdOut, "\nDone.\n");
  310.   
  311.   /* Let's get outa here! */
  312.   cleanExit (RETURN_OK, NULL);
  313.   
  314.   /* Keep the compiler happy */
  315.   return 0;
  316. }
  317.