home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / hischgct.zip / resmgr.cmd < prev    next >
OS/2 REXX Batch file  |  1996-01-03  |  9KB  |  265 lines

  1. /* resmgr.cmd -- A "zip-like" .RES manager                    950531 */
  2. /* (c) Copyright Martin Lafaix 1995                                  */
  3.  
  4. say 'Operating System/2  Resource Manager'
  5. say 'Version 0.01.006 May 31 1995'
  6. say '(C) Copyright Martin Lafaix 1995'
  7. say 'All rights reserved.'
  8. say
  9.  
  10. arg param orgfile outfile x y
  11. tempneeded = 0; modified = 0; nl = '0d0a'x; rid = ''; rtype = ''
  12. typeName = '/POINTER /BITMAP /MENU /DIALOG /STRING /FONTDIR /FONT /ACCELTABLE /RCDATA',
  13.            '/MESSAGE /DLGINCLUDE /VKEYTBL /KEYTBL /CHARTBL /DISPLAYINFO /FKASHORT',
  14.            '/FKALONG /HELPTABLE /HELPSUBTABLE /FDDIR /FD'
  15.  
  16. call RxFuncAdd 'SysTempFileName', 'RexxUtil', 'SysTempFileName'
  17.  
  18. if param = '-H' | param = '' | y \= '' then
  19.    do
  20.       say 'Usage:  res <option> <.RES file> [id.type] [file]'
  21.       say '        -a              - Add specified resources (default)'
  22.       say '        -d              - Delete specified resources'
  23.       say '        -l              - List resources (short format)'
  24.       say '        -v              - List resources (long format)'
  25.       say '        -x              - Extract specified resources'
  26.       say '        -h              - Access Help'
  27.       say
  28.       say '        .RES file       = .EXE or .DLL filename'
  29.       say '        file            = Input or output file name'
  30.       say '        type            = Resource type or *'
  31.       say '        id              = Resource ID or *'
  32.       say
  33.       say 'Possible type value (with -d, -l, -v or -x):'
  34.       say
  35.       say '  Acceltable Bitmap   Chartbl Dialog  Displayinfo  Dlginclude Fd     Fddir'
  36.       say '  Fkalong    Fkashort Font    Fontdir Helpsubtable Helptable  Keytbl Menu'
  37.       say '  Messagetable        Pointer RCData  Stringtable  Vkeytbl'
  38.       say
  39.       say 'Environment variables:'
  40.       say '        TMP=temporary file path'
  41.       say '        TEMP=temporary file path'
  42.       if y \= '' then
  43.          exit 1
  44.       else
  45.          exit
  46.    end
  47.  
  48. if param \= '-A' & param \= '-D' & param \= '-L' & param \= '-X' & param \= '-V' then
  49.    call error 'Invalid option :' param, 2
  50.  
  51. if verify(orgfile,'*?<>:|"','M') > 0 then
  52.    call error 'Invalid file name :' orgfile, 1
  53.  
  54. if charin(orgfile,1,1) \= 'FF'x then
  55.    do
  56.       call stream orgfile, 'c', 'close'
  57.       tempneeded = 1
  58.       tempname = gettemp('RES?????.RES')
  59.       '@call rdc -r' orgfile tempname '>nul'
  60.       if rc \= 0 then
  61.          call error 'Invalid input file :' orgfile, 1
  62.       infile = tempname
  63.    end
  64. else
  65.    infile = orgfile
  66.  
  67. call initialize
  68.  
  69. call charin infile,1,0
  70. do while chars(infile) > 0
  71.    call skip 1
  72.    rt = readw()
  73.    call skip 1
  74.    id = readw()
  75.    opt = readw()
  76.    cb = readl()
  77.  
  78.    if (rtype = '*' | rt = rtype) & (rid = '*' | id = rid) then
  79.       select
  80.          when param = '-A' then call add
  81.          when param = '-D' then do; modified = 1; say '    'id'.'rt' ('cb' bytes)'; end
  82.          when param = '-L' then call shows
  83.          when param = '-V' then call showl
  84.          when param = '-X' then do; say '    'id'.'rt' ('cb' bytes)'; call extract; end
  85.       end  /* select */
  86.    else
  87.    if param = '-D' then
  88.       call extract
  89.  
  90.    call skip cb
  91. end /* do */
  92.  
  93. call terminate
  94.  
  95. exit
  96.  
  97. terminate: /* do option-dependant termination */
  98.    if param = '-D' | param = '-A' then
  99.       do
  100.          call stream infile, 'c', 'close'
  101.          call stream outfile, 'c', 'close'
  102.          if modified then
  103.             '@copy' outfile infile '>nul'
  104.          '@del /f' outfile
  105.       end
  106.    if tempneeded & modified then
  107.       '@call rc' infile orgfile '>nul'
  108.    if tempneeded then
  109.       do
  110.          call stream infile, 'c', 'close'
  111.          '@del /f' infile
  112.       end
  113.    return
  114.  
  115. initialize: /* do option-dependant initialisation */
  116.    select
  117.       when param = '-A' then
  118.          do
  119.             if x \= '' then
  120.                call error 'Too many arguments for -A :' x, 1
  121.             call getspec ''
  122.             oinfile = infile; infile = outfile; addedResources = '';
  123.             call charin infile,1,0
  124.             do while chars(infile) > 0
  125.                modified = 1
  126.                call skip 1
  127.                rt = readw()
  128.                call skip 1
  129.                id = readw()
  130.                opt = readw()
  131.                addedResources = addedResources id'.'rt
  132.                cb = readl()
  133.                call skip cb
  134.             end /* do */
  135.             call stream infile, 'c', 'close'
  136.             outfile = gettemp('RES?????.TMP')
  137.             '@copy' infile outfile '>nul'
  138.             infile = oinfile
  139.          end
  140.       when param = '-L' | param = '-V' then
  141.          do
  142.             call getspec outfile
  143.             if param = '-L' then
  144.                do
  145.                   say 'Res.ID   Resource type   Res. size'
  146.                   say '----------------------------------'
  147.                end
  148.             else
  149.                do
  150.                   say 'Res.ID   Resource type       Res. size   Res. flags'
  151.                   say '------------------------------------------------------------'
  152.                end
  153.          end
  154.       when param = '-X' then
  155.          do
  156.             if x = '' then
  157.                call getspec ''
  158.             else
  159.                do
  160.                   call getspec outfile
  161.                   outfile = x
  162.                end
  163.             if verify(outfile,'*?<>:|"','M') > 0 then
  164.                call error 'Invalid file name :' outfile, 1
  165.             if stream(outfile,'c','query exists') \= '' then
  166.                '@del' outfile
  167.             say 'Extracting...'
  168.          end
  169.       when param = '-D' then
  170.          do
  171.             call getspec outfile
  172.             outfile = gettemp('RES?????.TMP')
  173.             say 'Removing...'
  174.          end
  175.    otherwise
  176.    end  /* select */
  177.    return
  178.  
  179. add:     /* add specified resource to outfile if not present */
  180.    if wordpos(id'.'rt,addedResources) > 0 then
  181.       return
  182. extract: /* extract specified resource */
  183.    call emit 'FF'x||d2w(rt)'FF'x||d2w(id)d2w(opt)d2l(cb)
  184.    call emit charin(infile,,cb)
  185.    cb = 0
  186.    return
  187.  
  188. shows:   /* display specified resource info (short format) */
  189.    call charout ,right(id,6)'   '
  190.    if rt < 22 then
  191.       call charout ,left(substr(word(typeName,rt),2),15)
  192.    else
  193.       call charout ,left(rt,15)
  194.    call charout ,right(cb,10)nl
  195.    return
  196.  
  197. showl:   /* display specified resource info (long format) */
  198.    call charout ,right(id,6)'   '
  199.    if rt < 22 then
  200.       call charout ,left(substr(word(typeName,rt),2) '('rt')',19)
  201.    else
  202.       call charout ,left(rt,19)
  203.    call charout ,right(cb,10)'   'option()nl
  204.    return
  205.  
  206. option:  /* convert flags to option string */
  207.    if bit(opt,10) then r = 'PRELOAD'; else r = 'LOADONCALL'
  208.    if bit(opt,12) then r = r' MOVEABLE'
  209.    if bit(opt, 4) then r = r' DISCARDABLE'
  210.    if \ (bit(opt,4) | bit(opt,12)) then r = r' FIXED'
  211.    if r = 'LOADONCALL MOVEABLE DISCARDABLE' then r = ''
  212.    return r
  213.  
  214. getspec: /* get resources specs as described in arg(1) */
  215.    procedure expose rid rtype typeName
  216.    if arg(1) \= '' then
  217.       parse value arg(1) with rid '.' rtype
  218.    parse value rid rtype '* *' with rid rtype .
  219.    if wordpos('/'rtype, typeName) > 0 then
  220.       rtype = wordpos('/'rtype, typeName)
  221.    return
  222.  
  223. gettemp: /* get a temp file name following arg(1) specs */
  224.    procedure
  225.    tempdir = value('TMP',,'OS2ENVIRONMENT')
  226.    if tempdir = '' then tempdir = value('TEMP',,'OS2ENVIRONMENT')
  227.    if tempdir = '' then tempdir = directory()
  228.    if tempdir \= '' & right(tempdir,1) \= '\' then tempdir = tempdir||'\'
  229.    return SysTempFileName(tempdir||arg(1))
  230.  
  231. emit:    /* write data to output file */
  232.    return charout(outfile,arg(1))
  233.  
  234. readw:   /* read one word from infile */
  235.    return w2d(charin(infile,,2))
  236.  
  237. readl:   /* read one long from infile */
  238.    return l2d(charin(infile,,4))
  239.  
  240. skip:    /* skip arg(1) chars */
  241.    return charin(infile,,arg(1))
  242.  
  243. bit:     /* return bit arg(2) of arg(1) */
  244.    return substr(x2b(d2x(arg(1),4)), arg(2),1)
  245.  
  246. w2d:     /* littleendian word to decimal */
  247.    w = c2x(arg(1))
  248.    return x2d(substr(w,3,2)substr(w,1,2))
  249.  
  250. d2w:     /* decimal to littleendian word */
  251.    w = d2x(arg(1),4)
  252.    return x2c(substr(w,3,2)substr(w,1,2))
  253.  
  254. l2d:     /* littleendian long to decimal */
  255.    l = c2x(arg(1))
  256.    return x2d(substr(l,7,2)substr(l,5,2)substr(l,3,2)substr(l,1,2))
  257.  
  258. d2l:     /* decimal to littleindian long */
  259.    l = d2x(arg(1),8)
  260.    return x2c(substr(l,7,2)substr(l,5,2)substr(l,3,2)substr(l,1,2))
  261.  
  262. error:   /* display arg(1) and exit with code arg(2) */
  263.    say arg(1)
  264.    exit arg(2)
  265.