home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / cmdpk164.zip / query.cmd < prev    next >
OS/2 REXX Batch file  |  1998-01-04  |  15KB  |  484 lines

  1. /* query.cmd - compute connect time                            971209 */
  2. /* (C) Copyright Martin Lafaix 1995-1997.  All rights reserved.       */
  3.  
  4. /* Recognized command line args
  5.  
  6.    query Date
  7.    query Time
  8.    query [timeframe] [DETAILED] CONNECT TIME [FOR ...]
  9.    query [timeframe] [LAST] CONNECT MESSAGE [FOR ...]
  10.    query [timeframe] MAIL [FOR name] [FROM ...] [IN foldername] [WHERE condition]
  11.    query DISK [unit]
  12.    query PICTure DIMension FOR ...
  13.    query OS [Version]
  14.    query [component] Version
  15.    query SYStem (DEVices|DRIVERS|IRQs|MEMory)
  16.    query PROCess ID
  17.    query DIRector(y|ies) STACK
  18.  
  19.    timeframe  == [THIS|LAST] (YEAR|MONTH|monthname)['S]
  20.    monthname  == JANuary | FEBruary | MARch | APRil | MAY | JUNe | JULy |
  21.                  AUGust | SEPtember | OCTober | NOVember | DECember
  22.    foldername == name | "name" | * | foldername, foldername
  23.    condition  == element operator value [(OR|AND) condition]
  24.    element    == FROM | TO | SUBJECT | SENDER [['S] DOMAIN]
  25.    operator   == CONTAINS | = | == | > | < | \= | <>
  26.    unit       == * | letter[:] | unit unit
  27.    component  == REXX | JAVA | OS
  28.  
  29.    examples
  30.  
  31.    query connect time
  32.    query this year connect time for user1
  33.    query detailed november connect time for user1 user2
  34.    query last connect message
  35.    query mail for user1 in in-basket where subject contains "PAGING"
  36.    query disk
  37.    query disk e f
  38.    query os version
  39. */
  40.  
  41. if RxFuncQuery("SysLoadFuncs") then
  42.    do
  43.    call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  44.    call SysLoadFuncs
  45.    end
  46.  
  47. arg user_arg
  48. parse arg request
  49.  
  50. select
  51.    when abbrev('DATE', user_arg, 1) then
  52.       say date()
  53.    when abbrev('TIME', user_arg, 1) then
  54.       say time()
  55.    when wordpos('CONNECT',user_arg) > 0 then
  56.       say connect()
  57.    when wordpos('MAIL', user_arg) > 0 then
  58.       say mail()
  59.    when wordpos('DISK', user_arg) > 0 then
  60.       say disk()
  61.    when abbrev('PICTURE', word(user_arg,1), 4) then
  62.       say picture()
  63.    when abbrev('SYSTEM',word(user_arg,1),3) & words(user_arg) = 2 then
  64.       select
  65.          when word(user_arg,2) = 'DRIVERS' then
  66.             say drivers()
  67.          when abbrev('DEVICES',word(user_arg,2),3) then
  68.             '@rmview /hw'
  69.          when abbrev('MEMORY',word(user_arg,2),3) then
  70.             say memory()
  71.          when abbrev('IRQS',word(user_arg,2),3) then
  72.             say irqs()
  73.          when word(user_arg,1) = 'OS' then
  74.             if abbrev('VERSION',word(user_arg,2),1) then
  75.                '@ver /r'
  76.             else
  77.                '@ver'
  78.       otherwise
  79.          say 'Unknown request:' request
  80.       end
  81.    when abbrev('VERSION',word(user_arg,2),1) & words(user_arg) = 2 then
  82.       say version()
  83.    when abbrev('PROCESS',word(user_arg,1),4) & word(user_arg,2) = 'ID' & words(user_arg) = 2 then
  84.       say DosGetInfoBlocks('PID TID')
  85.    when wordpos('STACK', user_arg) > 0 then
  86.       say dirstack()
  87.    when value('HELP.COMMAND',,'OS2ENVIRONMENT') \= '' & wordpos(user_arg,value('HELP.SWITCHES',,'OS2ENVIRONMENT')) > 0 then
  88.       'call %HELP.COMMAND% QUERY' user_arg
  89. otherwise
  90.    say 'Unknown request:' request
  91. end  /* select */
  92.  
  93. exit
  94.  
  95. connect:
  96.    disconnect = 'Déconnexion après' /* 'Disconnected after' */
  97.    monthname = '\JANUARY  \FEBRUARY \MARCH    \APRIL    \MAY      \JUNE     '||,
  98.                '\JULY     \AUGUST   \SEPTEMBER\OCTOBER  \NOVEMBER \DECEMBER'
  99.    detailed = wordpos('DETAILED',user_arg) > 0
  100.    msg = wordpos('MESSAGE',user_arg)+wordpos('MESSAGES',user_arg) > 0
  101.    this = wordpos('THIS',user_arg) > 0
  102.    last = wordpos('LAST',user_arg) > 0
  103.    user = wordpos('FOR',user_arg) > 0
  104.    mm = wordpos('MONTH',user_arg)+wordpos('MONTH''S',user_arg) > 0
  105.    yy = wordpos('YEAR',user_arg)+wordpos('YEAR''S',user_arg) > 0
  106.    m = 0
  107.    do i = 1 to words(user_arg)
  108.       w = pos('\'word(user_arg,i),monthname)
  109.       if w > 0 then
  110.          if abbrev(substr(monthname,w+1,9),word(user_arg,i),3) = 1 then
  111.             if m \= 0 then
  112.                return 'Incorrect request:' request
  113.             else
  114.                m = 1 + w % 10
  115.    end /* do */
  116.  
  117.    count = \user
  118.    if user then
  119.       userlist = substr(request,wordindex(user_arg,wordpos('FOR',user_arg))+4)
  120.  
  121.    if this & last | mm & yy | mm & (m > 0) then
  122.       return 'Incorrect request:' request
  123.  
  124.    log = value('ETC',,'OS2ENVIRONMENT')'\Connect.log'
  125.    time = 0
  126.    parse value date('S') with year 5 month 7 .
  127.    if last & mm then month = month-1
  128.    if last & yy then year = year-1
  129.    if yy then month = ''
  130.    if m \= 0 then month = m
  131.    yymm = year'/'right(month,2,0)
  132.  
  133.    message = ''
  134.    curLine = 1
  135.    do while lines(log) \= 0
  136.       line = linein(log); curLine = curLine+1
  137.       if substr(line,21,1) = '─' then beginning = curLine
  138.       if count & msg & curLine > beginning+2 & substr(line,21,length(disconnect)) <> disconnect then
  139.          if last & curLine = beginning+3 then
  140.             message = line
  141.          else
  142.             message = message || copies('0d0a'x,1+(curLine = beginning+3)) || line
  143.       else
  144.       if word(line,5) = 'dialed' then do
  145.          if user then
  146.             count = wordpos(word(line,4),userlist) > 0
  147.          dialed = left(line,length(yymm))
  148.          end
  149.       else
  150.       if count & substr(line,21,length(disconnect)) = disconnect then
  151.          select
  152.             when yymm = dialed & left(line,length(yymm)) = yymm then
  153.                time = time + seconds(word(line,5), line)
  154.             when dialed < yymm & left(line,length(yymm)) = yymm then
  155.                time = time + seconds(substr(line,12,8), line)
  156.             when yymm = dialed then
  157.                time = time + seconds(word(line,5), line) - seconds(substr(line,12,8), line)
  158.          otherwise
  159.          end  /* select */
  160.    end
  161.  
  162.    call stream log, 'c', 'close'
  163.  
  164.    if msg then
  165.       return message
  166.    else
  167.    if yy then
  168.       return year 'Yearly connect time' (time % 3600)':' || right((time // 3600) % 60,2,0) || ':' || right(time // 60,2,0)
  169.    else
  170.       return year'/'right(month,2,0) 'Monthly connect time' (time % 3600)':' || right((time // 3600) % 60,2,0) || ':' || right(time // 60,2,0)
  171.  
  172. seconds:
  173.    parse value arg(1) with h ':' m ':' s
  174.    if detailed then say word(arg(2),1) arg(1)
  175.    return max(35, s + 60 * m + 3600 * h)
  176.  
  177. mail:
  178.    NUMERIC DIGITS 10
  179.  
  180.    MAIL.checkfor = wordpos('FOR', user_arg) \= 0
  181.    MAIL.checkfrom = wordpos('FROM', user_arg) \= 0
  182.    MAIL.checkin = wordpos('IN', user_arg) \= 0
  183.    MAIL.checkwhere = wordpos('WHERE', user_arg) \= 0
  184.  
  185.    umpath = SysSearchPath('PATH', 'umail.exe')
  186.    if umpath = '' then
  187.       return 'Ultimedia Mail/Lite not found!'
  188.    umpath = left(umpath,lastpos('\', umpath))'MailStor'
  189.    olddir = directory()
  190.  
  191.    if MAIL.checkfor then do
  192.       parse var user_arg . ' FOR ' user .
  193.       umpath = umpath'\'user
  194.       end
  195.    else do
  196.       call SysFileTree umpath'\*', 'users', 'DO'
  197.       if users.0 \= 1 then
  198.          return 'More than one mailbox available!  Please specify one.'
  199.       umpath = users.1
  200.       end
  201.  
  202.    if directory(umpath) = '' then
  203.       return 'Mailbox not found!'
  204.  
  205.    if MAIL.checkfrom then do
  206.       parse var user_arg . ' FROM ' who ' WHERE ' .
  207.       parse var who who ' IN ' .
  208.       MAIL.from_who = '00'x||translate(space(strip(who),0),'00'x,',')'00'x
  209.       end
  210.  
  211.    if MAIL.checkin then do
  212.       parse var user_arg . ' IN ' in ' WHERE ' .
  213.       parse var in in ' FROM ' .
  214.       MAIL.in_what = '00'x||translate(space(strip(in),0),'00'x,',')'00'x
  215.       end
  216.  
  217.    if MAIL.checkwhere then do
  218.       parse var user_arg . ' WHERE ' where
  219.       MAIL.where_what = where
  220.       end
  221.  
  222.    call explore
  223.  
  224.    call directory olddir
  225.  
  226.    return 'Done!'
  227.  
  228. explore:
  229.    procedure expose MAIL.
  230.  
  231.    infile = directory()'\UMAIL.NDX'
  232.    call stream infile,'c','open read'
  233.    do while chars(infile) > 0
  234.       call charin infile,,6
  235.       size = x2d(c2x(charin(infile,,2)))
  236.       data = charin(infile,,size)
  237.       type = x2d(c2x(substr(data,9,2)))
  238.       select
  239.          when type = 0    then nop /* call charout ,'Letter ' */
  240.          when type = 1    then nop /* call charout ,'Folder ' */
  241.          when type = 16   then nop /* call charout ,'In-Box ' */
  242.          when type = 3840 then call charout ,'A-Book '
  243.       otherwise
  244.          call charout ,'['type'] '
  245.       end  /* select */
  246.       call display
  247.    end /* do */
  248.    call stream infile,'c','close'
  249.  
  250.    return
  251.  
  252. display:
  253.    procedure expose data type MAIL.
  254.    select
  255.       when type = 0 & MAIL.checkfrom & substr(data,12,1) = '04'x then nop
  256.       when type = 0 then do
  257.          parse value substr(data,88) with header '00'x file1 '00'x from '00'x to '00'x subject '00'x sender '00'x domain '00'x
  258.          if (\ MAIL.checkfrom | pos('00'x||space(translate(sender),0)'00'x, MAIL.from_who) > 0) &,
  259.             (\ MAIL.checkwhere | inwhere()) then
  260.             say ' 'sender ':' subject
  261.          end
  262.       when type = 1 | type = 16 then do
  263.          parse value substr(data,88) with header '00'x name '00'x fsname '00'x .
  264.          if \ MAIL.checkin | pos('00'x||space(translate(name),0)'00'x, MAIL.in_what) \= 0 then do
  265.             say '['name']'
  266.             call directory fsname
  267.             call explore
  268.             call directory '..'
  269.             end
  270.          end
  271.    otherwise
  272.       say  substr(data,88)
  273.    end  /* select */
  274.    return
  275.  
  276. inwhere:
  277.    return 1
  278.  
  279. disk:
  280.    if translate(word(request, 1)) \= 'DISK' then
  281.       return 'Incorrect request:' request
  282.    drv = subword(request, 2)
  283.  
  284.    drives = ''
  285.    if drv = '*' then
  286.       drives = SysDriveMap()
  287.    else
  288.    if drv = '' then
  289.       drives = filespec('d', directory())
  290.    else
  291.    do i = 1 to words(drv)
  292.       unit = word(drv, i)
  293.       if length(unit) > 2 | datatype(left(unit, 1), 'M') \= 1 | (length(unit) = 2 & right(unit, 1) \= ':') then
  294.          return 'Incorrect request:' request
  295.       drives = drives left(unit':', 2)
  296.    end /* do */
  297.  
  298.    str = ''
  299.    do i = 1 to words(drives)
  300.       parse value SysDriveInfo(word(drives,i)) with drive free max label
  301.       if drive = '' then iterate
  302.       used = max-free
  303.       if max = 0 then
  304.          capacity = 100
  305.       else
  306.          capacity = used/max*100
  307.       str = str || '0d0a'x drive 'disk usage:' used % 1024 || 'k ('strip(format(capacity,3,0)'%')')'
  308.    end
  309.    return substr(str,3)
  310.  
  311. picture:
  312.    /* code for GIF and JPEG adapted from E-Zine 2-9 and 2-10 */
  313.    if abbrev('DIMENSION', word(user_arg,2), 3) = 0 then
  314.       return 'Incorrect request:' request
  315.    if word(user_arg,3) \= 'FOR' then
  316.       return 'Incorrect request:' request
  317.    str = ''
  318.    parse var user_arg . "FOR" list
  319.    do i = 1 to words(list)
  320.       f = word(list,i)
  321.       if right(f,4) = '.GIF' then
  322.          str = str||gifsize(f)'0d0a'x
  323.       else
  324.       if right(f,4) = '.JPG' | right(f,5) = '.JPEG' then
  325.          str = str||jpgsize(f)'0d0a'x
  326.       else
  327.          say 'Unsupported format:' word(list,i)
  328.    end /* do */
  329.    if right(str,2) = '0d0a'x then
  330.       return left(str,length(str)-2)
  331.    else
  332.       return str
  333.  
  334. gifsize:
  335.    call charin f,1,6
  336.    width = c2d(reverse(charin(f,,2)))
  337.    height = c2d(reverse(charin(f,,2)))
  338.    call stream f, 'c', 'close'
  339.    return width'x'height
  340.  
  341. jpgsize:
  342.    type = ''
  343.    if c2x(charin(f,1,2)) \= "FFD8" then
  344.       return 'Incorrect JPEG format'
  345.    nextseg=3
  346.    height="HEIGHT"
  347.    do while type \= "D9" & nextseg \= -1 & height = "HEIGHT"
  348.       nextseg = readsegment(nextseg)
  349.    end /* do */
  350.    call stream f, 'c', 'close'
  351.    return width'x'height
  352.  
  353. readsegment:
  354.    arg SegPos
  355.  
  356.    if c2x(charin(f, SegPos)) \= "FF" then
  357.       return -1
  358.    type = c2x(charin(f))
  359.    if type = "01" | type >= "D0" & type <= "D9" then
  360.       res = SegPos+2
  361.    else
  362.       res = SegPos+2+c2d(charin(f,,2))
  363.  
  364.    if type ="C0" | type = "C2" then do
  365.       call charin f
  366.       height = c2d(charin(f,,2))
  367.       width = c2d(charin(f,,2))
  368.       end
  369.  
  370.    return res
  371.  
  372. drivers:
  373.    str = ''
  374.    localqueue = rxqueue('create')
  375.    oldqueue = rxqueue('set', localqueue)
  376.  
  377.    '@rmview /d /r | find "Driver:" | rxqueue' localqueue
  378.  
  379.    do while (queued() > 0)
  380.       parse pull line
  381.       if line \= '' then
  382.          str = str||substr(line, 9)'0d0a'x
  383.    end /* do */
  384.  
  385.    call rxqueue 'delete', localqueue
  386.    call rxqueue 'set', oldqueue
  387.  
  388.    return str
  389.  
  390. version:
  391.    if wordpos('JAVA', user_arg) > 0 then do
  392.       str = ''
  393.       localqueue = rxqueue('create')
  394.       oldqueue = rxqueue('set', localqueue)
  395.  
  396.       'java -version 2>&1 | rxqueue' localqueue
  397.  
  398.       do while (queued() > 0)
  399.          parse pull line
  400.          if line \= '' then
  401.             str = str||line'0d0a'x
  402.       end /* do */
  403.  
  404.       call rxqueue 'delete', localqueue
  405.       call rxqueue 'set', oldqueue
  406.  
  407.       if right(str,2) = '0d0a'x then
  408.          str = left(str,length(str)-2)
  409.  
  410.       return str
  411.       end
  412.    else
  413.    if wordpos('REXX', user_arg) > 0 then do
  414.       parse version v
  415.       return v
  416.       end
  417.    else
  418.       return 'Incorrect request:' request
  419.  
  420. dirstack:
  421.    str = ''
  422.    if words(user_arg) = 2 & (abbrev('DIRECTORY',word(user_arg,1),3) | abbrev('DIRECTORIES',word(user_arg,1),3)) then do
  423.       envvar = 'DIRSTACK.'translate(DosGetInfoBlocks(), '.', ' ')
  424.       qname = value(envvar,,'OS2ENVIRONMENT')
  425.       if qname = '' then
  426.          return 'Directory stack empty!'
  427.       oldq = RxQueue('Set', qname)
  428.       i = 0
  429.       do while queued() \= 0
  430.          i = i + 1
  431.          pull stack.i
  432.       end /* do */
  433.       stack.0 = i;
  434.       do i = stack.0 to 1 by -1
  435.          push stack.i
  436.          str = str||stack.i'0d0a'x
  437.       end /* do */
  438.  
  439.       if right(str,2) = '0d0a'x then
  440.          return left(str,length(str)-2)
  441.       else
  442.          return str
  443.       end
  444.    return 'Incorrect request:' request
  445.  
  446. memory:
  447.    signal on syntax name memoryerror
  448.    if RxFuncQuery("rxuinit") then
  449.       do
  450.       call RxFuncAdd 'rxuinit','rxu','rxuinit'
  451.       call rxuinit
  452.       end
  453.  
  454.    dosrc = RxQuerySysInfo(info.)
  455.  
  456.    str = "  Physical memory on board: " format(info.17/1024, 8) "KB"||"0a0d"x
  457.    str = str||"  Locked by OS/2:           " format(info.18/1024, 8) "KB"||"0a0d"x
  458.    str = str||"  Allocatable memory:       " format(info.19/1024, 8) "KB"||"0a0d"x
  459.    str = str||"  Memory page size:         " format(info.10, 8) "bytes"||"0a0d"x
  460.  
  461.    return str
  462.  
  463. memoryerror:
  464.    say 'RXU is required for:' request
  465.    exit
  466.  
  467. irqs:
  468.    str = ''
  469.    localqueue = rxqueue('create')
  470.    oldqueue = rxqueue('set', localqueue)
  471.  
  472.    '@rmview /irq | rxqueue' localqueue
  473.  
  474.    do while (queued() > 0)
  475.       parse pull line
  476.       if word(line,1) = 'IRQ' then
  477.          str = str||strip(line)'0d0a'x
  478.    end /* do */
  479.  
  480.    call rxqueue 'delete', localqueue
  481.    call rxqueue 'set', oldqueue
  482.  
  483.    return str
  484.