home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / nistime2.zip / nistime2.cmd < prev    next >
OS/2 REXX Batch file  |  2001-11-02  |  6KB  |  194 lines

  1. /* REXX */
  2. /* nistime2.cmd -- get current time from NIST and set OS/2 clock */
  3.  
  4. versionstring = "Version 0.2d   02-nov-2001"
  5.  
  6. /* Author:
  7.       Pieter Bras
  8.       Cambridge, MA
  9.       pbras@pobox.com
  10.  
  11.    Copyright (C) 2001 Pieter Bras
  12.    You may redistribute this program provided this copyright notice is
  13.    preserved in full.
  14.  
  15. Usage:
  16.  
  17.    nistime2 options listfile
  18.  
  19.    "listfile" is the name of a file in the current directory containing
  20.    the various NIST time servers, in the order that they should be tried.
  21.  
  22.    If "listfile" is missing then the program looks for a file named
  23.    nist-srv.lst in the current directory.
  24.  
  25.    If there is also no file named nist-srv.lst then only the NISTIME default
  26.    NIST time server is tried.
  27.  
  28. Options (introduced with the '-' character):
  29.  
  30.    -? or  ?    display help, then exit
  31.    -h or -H    same as -?
  32.  
  33.    -s0   don't display any messages, and pass -s0 to nistime.exe
  34.  
  35.    all other options are passed through to nistime.exe
  36. */
  37.  
  38. /* uncomment the following line for debugging */
  39. signal on novalue
  40.  
  41. /* load OS/2 RexxUtil functions (not needed) */
  42. /* if rxFuncQuery( 'SysLoadFuncs') = 1 then do
  43.    call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  44.    call SysLoadFuncs
  45.    end
  46. */
  47.  
  48. /* define non-printing ascii codes */
  49. HTAB  = '09'X
  50. SPACE = '20'X
  51.  
  52. msglvl = 1
  53. n_opts = ""
  54. parse arg commandline
  55.  
  56. /* get name of file containing list of NIST servers */
  57. defaultlist = "nist-srv.lst"        /* default file from NIST */
  58. serverlist = checkopts( strip( commandline))
  59. if serverlist = "" then
  60.    serverlist = defaultlist
  61. else if \FileExists( serverlist) then do
  62.    call message "Cannot find file:" serverlist "-- using:" defaultlist
  63.    serverlist = defaultlist
  64.    end
  65.  
  66. SrvAddr.0 = 0                       /* number of servers */
  67. rv = BuildServerTable( serverlist)
  68. if SrvAddr.0 = 0 then do            /* if table empty, set up default server */
  69.    call message "Server list empty or not found, using default server..."
  70.    SrvAddr.1 = ''
  71.    SrvName.1 = '(default server)'
  72.    SrvNote.1 = ''
  73.    SrvLoc.1  = ''
  74.    SrvAddr.0 = 1
  75.    end
  76. rv = GetSetTime()
  77. exit rv
  78.  
  79. /* Try all NIST servers until one of them returns the correct time. */
  80. /* return codes from nistime.exe (ver 0.2c or later):
  81.       0        success: clock not adjusted
  82.       1        success: clock was adjusted
  83.       100-199  unrecoverable error
  84.       200-299  error, possibly recoverable with different choice of server
  85.       300-...  error, possibly recoverable with currently selected server
  86. */
  87. GetSetTime:
  88.    do i = 1 to SrvAddr.0
  89.       if pos( '3', SrvNote.i) > 0 then
  90.          iterate                       /* test server, do not use */
  91.       call message ''
  92.       call message "Trying:" SrvName.i
  93.       rv = nistime( SrvAddr.i)
  94.       if rv < 200 then                 /* done: success or unrecoverable err */
  95.          leave
  96.       if rv < 300 then                 /* try another server */
  97.          iterate
  98.       call message "Trying again..."   /* try current server one more time */
  99.       rv = nistime( SrvAddr.i)
  100.       if rv < 200 then                 /* done: success or unrecoverable err */
  101.          leave
  102.       end /* do i = ... */
  103.    if rv >= 100 then do
  104.       call message ''
  105.       call message "Unsuccessful: NISTIME error" rv
  106.       end
  107.    return rv
  108.  
  109. /* Call nistime.exe program to set clock. Returns nistime.exe return code. */
  110. nistime:
  111.    /* address CMD */ "@nistime.exe -S" n_opts arg( 1)
  112.    return RC
  113.  
  114. /* Build table of NIST time servers. Returns: 0=success, 1=failure */
  115. BuildServerTable:
  116.    parse arg srvlist
  117.    if \FileExists( srvlist) then
  118.       return 1
  119.    if \FileOpen( srvlist) then
  120.       return 1
  121.    i = SrvAddr.0
  122. /* scan until a line starting with '$' is found */
  123.    do while lines( srvlist)
  124.       if left( linein( srvlist), 1) = '$' then
  125.          leave
  126.       end /* do while */
  127. /* parse the lines until another line starting with '$' is found */
  128.    do while lines( srvlist)
  129.       server = translate( linein( srvlist), SPACE, HTAB)
  130.       if left( server, 1) = '$' then
  131.          leave
  132.       i = i + 1
  133.       parse var server SrvName.i SrvAddr.i SrvNote.i SrvLoc.i .
  134.       end /* do while */
  135.    SrvAddr.0 = i
  136.    rv = FileClose( srvlist)
  137.    return 0
  138.  
  139. /* rv = FileExists( fileName) */ /* TRUE if exists, FALSE otherwise */
  140. FileExists:
  141.    rv = stream( arg( 1), 'C', 'QUERY EXISTS')
  142.    if rv = '' then
  143.       return 0
  144.    else
  145.       return 1
  146.  
  147. /* rv = FileOpen( fileName, fileMode) */  /* TRUE/FALSE if success/error */
  148. FileOpen:
  149.    rv = stream( arg( 1), 'C', 'OPEN' arg( 2))
  150.    return abbrev( 'READY:', rv)
  151.  
  152. /* rv = FileClose( fileName) */           /* TRUE/FALSE if success/error */
  153. FileClose:
  154.    rv = stream( arg( 1), 'C', 'CLOSE')
  155.    return abbrev( 'READY:', rv)
  156.  
  157. message:
  158.    if msglvl = 1 then
  159.       say arg( 1)
  160.    return
  161.  
  162. checkopts:
  163.    parse arg optlist
  164.    if left( optlist, 1) = '?' then
  165.       signal usage
  166.    do while left( optlist, 1) = '-'
  167.       parse var optlist switch optlist
  168.       if left( switch, 2) = '-?' | left( translate( switch), 2) = '-H' then
  169.          signal usage
  170.       if left( switch, 3) = '-m0' then
  171.          msglvl = 0
  172.       n_opts = n_opts switch              /* pass to nistime.exe */
  173.       end
  174.    return strip( optlist)
  175.  
  176. usage:
  177.    say 'NISTIME2  ' versionstring
  178.    say ''
  179.    say 'Usage:  nistime2 [options] [serverlist]'
  180.    say ''
  181.    say '   where "serverlist" (optional) is a preferred list of NIST servers;'
  182.    say '   if not found, program will look for a file named "nist-srv.lst".'
  183.    say ''
  184.    say 'Options:'
  185.    say ''
  186.    say '   -? or  ?    display this help message'
  187.    say '   -h or -H    same as -?'
  188.    say "   -s0         don't display messages (also passed to nistime.exe)"
  189.    say ''
  190.    say '   all other options are passed through to nistime.exe'
  191.    exit 0
  192.  
  193. /* <eof> */
  194.