home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / sosutl12.zip / bin / extract.cmd < prev    next >
OS/2 REXX Batch file  |  1993-10-18  |  11KB  |  345 lines

  1. /* -------
  2.  * Extract (C) SuperOscar Softwares, Tommi Nieminen 1991-93.
  3.  * -------
  4.  * Extracts files from archives. Note: this script only forms the
  5.  * command line necessary to run the archiver but doesn't REPLACE it.
  6.  * You'll still need all the archivers you are likely to encounter.
  7.  *
  8.  * If you do not want to keep your archivers in PATH, you can modify
  9.  * the value of 'arcdir' variable in line 48 (cf. comments there).
  10.  *
  11.  * Usage:
  12.  *     [D:\] extract ARCHIVE [ FILE ... ] [ /jlp? /t DIR ]
  13.  *
  14.  * Switches:
  15.  *     /j      Don't extract directories (junk paths)
  16.  *     /l      Display archive listing only
  17.  *     /p      Extract to standard output (pipe)
  18.  *     /t      Set target directory
  19.  *     /?      Display help page and quit
  20.  *
  21.  * Semantic ambiguities are not allowed in the command line, ie. /l and
  22.  * /p cannot be used in connection with any other switch (/l output
  23.  * always goes to standard output, it's no use /Piping it). However, /?
  24.  * overrides all the other switches.
  25.  *
  26.  * 16-Aug-1993  v1.0: first PD version, based on private version 2.8.
  27.  *              Supported archivers: Zip (.zip), Lh2 (.lzh), Zoo (.zoo),
  28.  *              gzip (.gz), Compress (.Z), tar (.tar), UNARJ (.arj).
  29.  * 5-Sep-1993   v1.0a: archiver location stated in `arcdir' variable;
  30.  *              this makes it unnecessary to keep the programs in PATH.
  31.  * 27-Sep-1993  v1.0b: added support for HPACK and ARC.
  32.  * 3-Oct-1993   v1.0c: help page simplified.
  33.  * 18-Oct-1993  v1.1: different internal structure (the code should be a
  34.  *              bit easier to read now).
  35.  */
  36.  
  37. "@echo off"
  38.  
  39. Parse Arg name files "/"switches
  40.  
  41.   /* Program name and version for messages */
  42. prgname = "Extract v1.1"
  43.  
  44.   /* Directory where archivers are kept. The value must have a trailing
  45.    * backslash (eg. "C:\Bin\Arc\"). If arcdir == "", Extract does a
  46.    * normal PATH search.
  47.    */
  48. arcdir = ""
  49.  
  50.   /* Symbolic constants */
  51. TRUE = 1
  52. FALSE = 0
  53.  
  54.   /* Commands */
  55. EXTRACT = 0             /* Extract file */
  56. PIPE = 1                /* Pipe file, ie. extract to stdout */
  57. LIST = 2                /* View archive directory */
  58.  
  59.   /* Defaults */
  60. command = EXTRACT       /* Default command */
  61. paths = TRUE            /* Extract directory structure? */
  62. target_dir = "."        /* Current directory */
  63.  
  64.   /* Warning messages */
  65. WRN_paths = "Archiver doesn't support /j. Directories will be created."
  66. WRN_pipes = "Archiver doesn't support /p. Extracting to disk."
  67.  
  68.   /* Load SysGetKey() function */
  69. Call RxFuncAdd "SysGetKey", "RexxUtil", "SysGetKey"
  70.  
  71. Do i = 1 To Length(switches)
  72.     ch = Translate(SubStr(switches, i, 1))
  73.     Select
  74.           /* Ignore separators */
  75.         When ch == " " | ch == "/" Then
  76.             Iterate
  77.  
  78.           /* /j: junk directories */
  79.         When ch == "J" Then
  80.             paths = FALSE
  81.  
  82.           /* /l: look at contents */
  83.         When ch == "L" Then Do
  84.             If Verify(switches, "Ll?") <> 0 Then
  85.                 Call Error "/l can't be used in this connection"
  86.             command = LIST
  87.         End
  88.  
  89.           /* /p: pipe output to standard output */
  90.         When ch == "P" Then Do
  91.             If Verify(switches, "Pp?") <> 0 Then
  92.                 Call Error "/p can't be used in this connection"
  93.             command = PIPE
  94.         End
  95.  
  96.           /* /t: give target directory */
  97.         When ch == "T" Then Do
  98.             end = SubStr(switches, i + 1)
  99.  
  100.               /* Are there still switches? */
  101.             If Pos("/", end) == 0 Then Do
  102.                 target_dir = Strip(end)
  103.                   /* Switches exhausted */
  104.                 Leave
  105.             End
  106.             Else Do
  107.                 target_dir = Strip(Left(end, Pos("/", end) - 1))
  108.                   /* Move pointer to the next switch char */
  109.                 i = Pos("/", end)
  110.             End
  111.         End
  112.  
  113.           /* /?: help request */
  114.         When ch == "?" Then
  115.             Call Help
  116.  
  117.         Otherwise
  118.             Call Error "unknown switch '"ch"'"
  119.     End
  120. End
  121.  
  122.   /* No parameters */
  123. If name == "" Then Call Error "no parameters (use /? to get help)"
  124.  
  125.   /* Get full path name for archive file.  This is necessary if the user
  126.    * specified a target directory for extracting in a different drive
  127.    * but didn't use full path name for the archive.
  128.    */
  129. archive = Stream(name, "c", "query exists")
  130. If archive == "" Then
  131.     Call Error "can't find archive file '"name"'"
  132.  
  133.   /* Pick out an archiver, and compose a parameter string for it */
  134. ext = Translate(SubStr(archive, LastPos(".", archive)))
  135. Select
  136.       /* ARC */
  137.     When ext == ".ARC" Then
  138.         Select
  139.             When command == PIPE Then
  140.                 prg = "arc p"
  141.             When command == LIST Then
  142.                 prg = "arc l"
  143.             Otherwise Do
  144.                 If paths == FALSE Then
  145.                     If confirm(WRN_paths) == "N" Then Exit 1
  146.                 prg = "arc x"
  147.             End
  148.         End
  149.  
  150.       /* ARJ */
  151.     When ext == ".ARJ" Then Do
  152.         Select
  153.             When command == PIPE Then Do
  154.                 If Confirm(WRN_pipes) == "N" Then Exit 1
  155.                 prg = "unarj x"
  156.             End
  157.             When command == LIST Then
  158.                 prg = "unarj l"
  159.             Otherwise
  160.                 If paths == FALSE Then
  161.                     prg = "unarj e"
  162.                 Else
  163.                     prg = "unarj x"
  164.         End
  165.         If Confirm(WRN_filemask) == "N" Then
  166.             Exit 1
  167.         files = ""
  168.     End
  169.  
  170.       /* GNU gzip */
  171.     When ext == ".GZ" Then
  172.         Select
  173.             When command == PIPE Then
  174.                 prg = "gzip -dc"
  175.             When command == LIST Then
  176.                 prg = "gzip -l"
  177.             Otherwise Do
  178.                 If paths == FALSE Then
  179.                     If Confirm(WRN_paths) == "N" Then Exit 1
  180.                 prg = "gzip -d"
  181.             End
  182.         End
  183.  
  184.       /* HPACK */
  185.     When ext == ".HPK" Then
  186.         Select
  187.             When command == PIPE Then
  188.                 prg = "hpack p"
  189.             When command == LIST Then
  190.                 prg = "hpack v"
  191.             Otherwise Do
  192.                 If paths == FALSE Then
  193.                     If Confirm(WRN_paths) == "N" Then Exit 1
  194.                 prg = "hpack x"
  195.             End
  196.         End
  197.  
  198.       /* Lh/2, C-LHarc, LHArc, LHA */
  199.     When ext == ".LZH" Then
  200.         Select
  201.             When command == PIPE Then Do
  202.                 If Confirm(WRN_pipes) == "N" Then Exit 1
  203.                 prg = "lh2 x"
  204.             End
  205.             When command == LIST Then
  206.                 prg = "lh2 l"
  207.             Otherwise Do
  208.                 prg = "lh2 x"
  209.                 If paths == TRUE Then files = files || " /s"
  210.             End
  211.         End
  212.  
  213.       /* tar */
  214.     When ext == ".TAR" Then
  215.         Select
  216.             When command == PIPE Then
  217.                 prg = "tar -xvOf"
  218.             When command == LIST Then
  219.                 prg = "tar -tf"
  220.             Otherwise Do
  221.                 If paths == FALSE Then
  222.                     If Confirm(WRN_paths) == "N" Then Exit 1
  223.                 prg = "tar -xvf"
  224.             End
  225.         End
  226.  
  227.       /* Compress */
  228.     When ext == ".Z" Then
  229.         Select
  230.             When command == PIPE Then
  231.                 prg = "compress -dc"
  232.             When command == LIST Then
  233.                 Call Error "can't display contents of Compress archive ('.Z')"
  234.             Otherwise Do
  235.                 If paths == FALSE Then
  236.                     If Confirm(WRN_paths) == "N" Then Exit 1
  237.                 prg = "compress -d"
  238.             End
  239.         End
  240.  
  241.       /* Zip, PKZip2, PKZip */
  242.     When ext == ".ZIP" Then
  243.         Select
  244.             When command == PIPE Then
  245.                 prg = "unzip -p"
  246.             When command == LIST Then
  247.                 prg = "unzip -l"
  248.             Otherwise Do
  249.                 prg = "unzip -x"
  250.                 If paths == FALSE Then
  251.                     prg = prg || "j"
  252.             End
  253.         End
  254.  
  255.       /* Zoo */
  256.     When ext == ".ZOO" Then
  257.         Select
  258.             When command == PIPE Then
  259.                 prg = "zoo xpq"
  260.             When command == LIST Then
  261.                 prg = "zoo la"
  262.             Otherwise Do
  263.                 prg = "zoo x"
  264.                 If target_dir <> "." Then prg = prg || "."
  265.                 If paths == FALSE Then prg = prg || ":"
  266.             End
  267.         End
  268.  
  269.     Otherwise
  270.         Call Error "unknown archive extension '"ext"'"
  271. End
  272.  
  273.   /* Translate backslashes to slashes in path names if archiver is any
  274.    * other than Lh/2 (all others use slashes internally).
  275.    * NOTE: on the command line you have to use backslashes, or else the
  276.    * file name parameters will be misinterpreted to contain switches.
  277.    */
  278. If ext <> ".LZH" Then
  279.     files = Translate(files, "/", "\")
  280.  
  281.   /* If we are extracting to another directory than the current one,
  282.    * we have to change to the target directory--this is the general
  283.    * method; some archivers might have a capability to do this on their
  284.    * own.
  285.    */
  286. If target_dir <> "." Then Do
  287.     curr = Directory()
  288.     new = Directory(target_dir)
  289.     If new == "" Then Do
  290.         If Confirm("'"target_dir"' not found. Extracting to current directory.") == "N" Then
  291.             Exit 1
  292.         target_dir = "."
  293.     End
  294. End
  295.  
  296.   /* Execute */
  297. arcdir""prg archive files
  298.  
  299.   /* Restore previous directory if it was changed */
  300. If target_dir <> "." Then Call Directory curr
  301.  
  302.   /* Nice exit */
  303. Exit 0
  304.  
  305.   /* Ask for confirmation. Return "Y" or "N". */
  306. Confirm: Procedure Expose prgname
  307.     Parse Arg msg
  308.  
  309.     Say prgname":" msg
  310.     Call CharOut CON, "Continue (y/n)? "
  311.     Do Until Pos(ans, "YyNn") > 0
  312.         ans = SysGetKey("NoEcho")
  313.     End
  314.     Say Translate(ans)
  315. Return Translate(ans)
  316.  
  317.   /* Exit with an error message */
  318. Error:
  319.     Parse Arg errmsg
  320.  
  321.     Say prgname":" errmsg
  322. Exit 1
  323.  
  324. Help: Procedure Expose prgname
  325.     Say prgname "(C) SuperOscar Softwares, Tommi Nieminen 1991-93."
  326.     Say
  327.     Say "    [D:\] extract ARCHIVE [ FILE ... ] [ /jlp? /t DIR ]"
  328.     Say
  329.     Say "Extracts files or displays archive contents from archives made in"
  330.     Say "several archivers. Currently known archivers are the following:"
  331.     Say
  332.     Say "    .arc   ARC                     .tar    GNU tar"
  333.     Say "    .arj   ARJ                     .Z      Compress"
  334.     Say "    .gz    gzip                    .zip    Zip (PKZip etc.)"
  335.     Say "    .hpk   HPACK                   .zoo    Zoo"
  336.     Say "    .lzh   Lh/2 (LHArc etc.)"
  337.     Say
  338.     Say "Switches:"
  339.     Say "    /j      don't extract directories"
  340.     Say "    /l      display contents only"
  341.     Say "    /p      extract to standard output (pipe)"
  342.     Say "    /t      target directory"
  343.     Say "    /?      display this help page"
  344. Exit 0
  345.