home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / sosutl11.zip / extract.cmd < prev    next >
OS/2 REXX Batch file  |  1993-08-20  |  9KB  |  272 lines

  1. /* -------
  2.  * Extract (C) SuperOscar Softwares, Tommi Nieminen 1991-93.
  3.  * -------
  4.  * Extracts files from archives. Usage:
  5.  *
  6.  *      [D:\] extract ARCHIVE [ FILE ... ] [ /jlp? /t DIR ]
  7.  *
  8.  * NOTE: if /l or /p switch is used, no other switch (well, I allowed
  9.  * /? because help is always needed...) can be used at the same time
  10.  * due to the semantics of these switches: if you're /Listing contents,
  11.  * it makes no sense /Piping files, or /Junking them, or /Targeting
  12.  * them to another directory, etc.
  13.  *
  14.  * 16-Aug-1993  v1.0: first PD version, based on private version 2.8.
  15.  *              Supported archivers: Zip (.zip), Lh2 (.lzh), Zoo (.zoo),
  16.  *              gzip (.gz), Compress (.Z), tar (.tar), UNARJ (.arj).
  17.  */
  18.  
  19. "@echo off"
  20.  
  21. Parse Arg name files "/"switches
  22.  
  23.   /* Program name and version for messages */
  24. program_name = "Extract v1.0"
  25.  
  26.   /* Symbolic constants */
  27. TRUE = 1
  28. FALSE = 0
  29.  
  30.   /* Defaults */
  31. contents = FALSE        /* Display just contents? */
  32. paths = TRUE            /* Extract directory structure? */
  33. pipe_output = FALSE     /* Pipe output to CON? */
  34. target_dir = "."        /* Target directory */
  35.  
  36.   /* Load SysGetKey() function */
  37. Call RxFuncAdd "SysGetKey", "RexxUtil", "SysGetKey"
  38.  
  39. Do i = 1 To Length(switches)
  40.     ch = Translate(SubStr(switches, i, 1))
  41.     Select
  42.           /* Ignore separators */
  43.         When ch == " " | ch == "/" Then
  44.             Iterate
  45.  
  46.           /* /j: junk directories */
  47.         When ch == "J" Then
  48.             paths = FALSE
  49.  
  50.           /* /l: look at contents */
  51.         When ch == "L" Then Do
  52.             If Verify(switches, "Ll?") <> 0 Then
  53.                 Call Error "/l can't be used together with /j, /p or /t!"
  54.             contents = TRUE
  55.         End
  56.  
  57.           /* /p: pipe output to standard output */
  58.         When ch == "P" Then Do
  59.             If Verify(switches, "Pp?") <> 0 Then
  60.                 Call Error "/p can't be used together with /j, /l or /t!"
  61.             pipe_output = TRUE
  62.         End
  63.  
  64.           /* /t: give target directory */
  65.         When ch == "T" Then Do
  66.             end = SubStr(switches, i + 1)
  67.  
  68.               /* Are there switches yet? */
  69.             If Pos("/", end) == 0 Then Do
  70.                 target_dir = Strip(end)
  71.                   /* Switches exhausted */
  72.                 Leave
  73.             End
  74.             Else Do
  75.                 target_dir = Strip(Left(end, Pos("/", end) - 1))
  76.                   /* Move pointer to the next switch char */
  77.                 i = Pos("/", end)
  78.             End
  79.         End
  80.  
  81.           /* /?: help request */
  82.         When ch == "?" Then
  83.             Call Help
  84.  
  85.         Otherwise
  86.             Call Error "unknown switch '"ch"'"
  87.     End
  88. End
  89.  
  90.   /* No parameters */
  91. If name == "" Then
  92.     Call Error "no parameters (use /? to get help)"
  93.  
  94.   /* Get full path name for archive file; this is needed if you specify
  95.    * a target directory in a different drive with /t switch and don't
  96.    * use full path name yourself.
  97.    */
  98. archive = Stream(name, "c", "query exists")
  99. If archive == "" Then
  100.     Call Error "can't find archive file '"name"'"
  101.  
  102.   /* Pick out an archiver, and compose a parameter string for it */
  103. ext = Translate(SubStr(archive, LastPos(".", archive)))
  104. Select
  105.     When ext == ".ARJ" Then Do
  106.         If files <> "" Then Do
  107.             If Confirm("UNARJ always extracts all files!") == "N" Then
  108.                 Exit 1
  109.             files = ""
  110.         End
  111.         If contents == FALSE & paths == FALSE Then
  112.             prg = "unarj e"
  113.         Else If contents == FALSE & paths == TRUE Then
  114.             prg = "unarj x"
  115.         Else
  116.             prg = "unarj l"
  117.         If pipe_output = TRUE Then Do
  118.             If Confirm("can't pipe .ARJ to stdout, /p has to be ignored!") == "N" Then
  119.                 Exit 1
  120.         End
  121.     End
  122.  
  123.     When ext == ".GZ" Then Do
  124.         If contents == FALSE Then
  125.             prg = "gzip -d"
  126.         Else
  127.             prg = "gzip -l"
  128.         If paths == FALSE Then
  129.             If Confirm("gzip always extracts paths, /j has to be ignored!") == "N" Then
  130.                 Exit 1
  131.         If pipe_output == TRUE Then
  132.             prg = prg || "c"
  133.     End
  134.  
  135.     When ext == ".LZH" Then Do
  136.         If contents == FALSE Then
  137.             prg = "lh2 x"
  138.         Else
  139.             prg = "lh2 l"
  140.         If paths == TRUE Then
  141.             files = files || " /s"
  142.         If pipe_output == TRUE Then
  143.             If Confirm("can't pipe .LZH to stdout, /p has to be ignored!") == "N" Then
  144.                 Exit 1
  145.     End
  146.  
  147.     When ext == ".TAR" Then Do
  148.         If contents == FALSE Then
  149.             prg = "tar -xv"
  150.         Else
  151.             prg = "tar -t"
  152.         If paths == FALSE Then
  153.             If Confirm("tar always extracts paths, /j has to be ignored!") == "N" Then
  154.                 Exit 1
  155.         If pipe_output == TRUE Then
  156.             prg = prg || "O"
  157.         prg = prg || "f"
  158.     End
  159.  
  160.     When ext == ".Z" Then Do
  161.         If contents == TRUE Then
  162.             Call Error "can't display contents of Compress archive ('.Z')"
  163.         prg = "compress -d"
  164.         If pipe_output == TRUE
  165.             Then prg = prg || "c"
  166.     End
  167.  
  168.     When ext == ".ZIP" Then Do
  169.         If contents == FALSE Then
  170.             prg = "unzip"
  171.         Else
  172.             prg = "unzip -l"
  173.         If paths == FALSE Then
  174.             prg = prg || " -j"
  175.         If pipe_output == TRUE Then
  176.             prg = prg || " -p"
  177.     End
  178.  
  179.     When ext == ".ZOO" Then Do
  180.         If contents == FALSE Then
  181.             prg = "zoo x"
  182.         Else
  183.             prg = "zoo la"
  184.         If target_dir <> "." Then
  185.             prg = prg || "."
  186.         If paths == FALSE Then
  187.             prg = prg || ":"
  188.         If pipe_output == TRUE Then
  189.             prg = prg || "pq"
  190.     End
  191.  
  192.     Otherwise
  193.         Call Error "unknown archive extension '"ext"'"
  194. End
  195.  
  196.   /* Translate backslashes to slashes in path names if archiver is any
  197.    * other than lh2 (all others use slashes internally).
  198.    * NOTE: you have to use backslashes on the command line, or else
  199.    * file name parameters will be misinterpreted to contain switches.
  200.    */
  201. If ext <> ".LZH" Then
  202.     files = Translate(files, "/", "\")
  203.  
  204.   /* If we are extracting to another directory than the default "."... */
  205. If target_dir <> "." Then Do
  206.     curr = Directory()
  207.     new = Directory(target_dir)
  208.     If new == "" Then Do
  209.         msg = "Can't find '" || target_dir || "', extracting to current directory"
  210.         If Confirm(msg) == "N" Then
  211.             Exit 1
  212.         target_dir = "."
  213.     End
  214. End
  215.  
  216.   /* Run chosen program */
  217. prg archive files
  218.  
  219.   /* Restore previous directory if it was changed */
  220. If target_dir <> "." Then
  221.     Call Directory curr
  222.  
  223.   /* Nice exit */
  224. Exit 0
  225.  
  226.   /* Ask for confirmation. Return "Y" or "N". */
  227. Confirm: Procedure Expose program_name
  228.     Parse Arg wrnmsg
  229.  
  230.     Say program_name":" wrnmsg
  231.     Call CharOut CON, "Continue (y/n)? "
  232.     Do Until Pos(ans, "YyNn") > 0
  233.         ans = SysGetKey("NoEcho")
  234.     End
  235.     Say Translate(ans)
  236. Return Translate(ans)
  237.  
  238.   /* Exit with an error message */
  239. Error: Procedure Expose program_name
  240.     Parse Arg errmsg
  241.  
  242.     Say program_name":" errmsg
  243. Exit 1
  244.  
  245. Help: Procedure Expose program_name
  246.     Say program_name": (C) SuperOscar Softwares, Tommi Nieminen 1991-93."
  247.     Say
  248.     Say "Usage: [D:\] extract ARCHIVE [ FILE ... ] [ /jlp? /t DIR ]"
  249.     Say
  250.     Say "Extracts files or displays archive contents from archives made in"
  251.     Say "several archivers. Currently known archivers are the following:"
  252.     Say
  253.     Say "  ==========================================================="
  254.     Say "    Ext    Archivers                          Archiver used"
  255.     Say "  ==========================================================="
  256.     Say "   .arj    ARJ                                UNARJ 2.41"
  257.     Say "   .gz     gzip                               GNU gzip 1.2.2"
  258.     Say "   .lzh    OS/2:    Lh2, C-LHarc              Lh2 2.14"
  259.     Say "           MS-DOS:  LHArc, LHA"
  260.     Say "   .tar    tar                                GNU tar 1.10"
  261.     Say "   .Z      Compress                           Compress v 4.2"
  262.     Say "   .zip    OS/2:    PKZip2, Zip               UnZip v5.0"
  263.     Say "           MS-DOS:  PKZip"
  264.     Say "   .zoo    Zoo                                Zoo 2.1"
  265.     Say
  266.     Say "Switches:          /j      don't extract directories"
  267.     Say "                   /l      display contents only"
  268.     Say "                   /p      extract to standard output (pipe)"
  269.     Say "                   /t      target directory"
  270.     Say "                   /?      display this help page"
  271. Exit 0
  272.