home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #3 / amigamamagazinepolishissue1998.iso / bazy / config_server / rexx / config-server.rexx
OS/2 REXX Batch file  |  1995-02-12  |  15KB  |  360 lines

  1. /*
  2. **  $Id: config-server.rexx,v 1.1 1995/02/12 22:13:58 rkr Exp $
  3. **
  4. **  Config-server uses/maintains a configuration database.
  5. **
  6. **  See /doc/config-server.doc for more info.
  7. **
  8. **  BUGS
  9. **      When doing a REMATTR, we just lose track of the field, we don't
  10. **      really remove it.  In theory, this shouldn't make a difference, but
  11. **      careless app's may mistakenly re-use "deleted" fields if they don't
  12. **      use the database properly (or if another app is editing the same
  13. **      fields concurrently).
  14. **
  15. **      I may or may not fix this one.  Under normal circumstances, it
  16. **      won't matter, and you can "force" it by doing a SAVE then RELOAD,
  17. **      if you like.  (SAVE only writes what is tracked, and RELOAD clears
  18. **      all tracked and untracked fields before re-reading the file.)
  19. **
  20. */
  21.     pragma( 'stack', 4000 )
  22.     port = 'config-server'
  23.     config_file = 'config-server.config'
  24.     if ~openport( port ) then   /*** contrary to docs, returns 0/1, NOT an address! ***/
  25.     do
  26.         address command 'say Failed to open config-server port!'
  27.         exit
  28.     end
  29.  
  30.     call read_config_file( )
  31.     null = '0000 0000'x
  32.     eol = '0a'x
  33.     done = 0
  34.     do until done
  35.         call waitpkt( port )
  36.         do until null = packet
  37.             packet = getpkt( port )
  38.             if null ~= packet then
  39.             do
  40.                 ret = ''
  41.                 err = 0
  42.                 command = getarg( packet )
  43.                 ucmd = upper( word( command, 1 ) )
  44.                 select
  45.                     when 'ADDATTR' == ucmd then
  46.                     do
  47.                         uparam = upper( word( command, 2 ) )
  48.                         select
  49.                             /*** TAG or VAL? ***/
  50.                             when 'TAG' == uparam then
  51.                                 call add_config_line( subword( command, 3 ) )
  52.                             otherwise
  53.                                 err = 10
  54.                         end
  55.                     end
  56.  
  57.                     when 'GETATTR' == ucmd then
  58.                     do
  59.                         uparam = upper( word( command, 2 ) )
  60.                         select
  61.                             when 'APPS' = uparam then
  62.                                 ret = app_list( )
  63.  
  64.                             when 'TAG' = uparam then
  65.                                 ret = get_tag( upper( subword( command, 3 ) ) )
  66.  
  67.                             when 'TAGS' = uparam then
  68.                             do
  69.                                 utag = upper( subword( command, 3 ) )
  70.                                 parse var utag uapp ':' utag
  71.                                 utag = uapp":TAG'S"
  72.                                 ret = configs.utag
  73.                             end
  74.  
  75.                             when 'VAL' = uparam then
  76.                             do
  77.                                 ret = get_tag( upper( subword( command, 3 ) ) )
  78.                                 if '*' ~= left( ret, 1 ) & '' ~= ret then
  79.                                 do
  80.                                     parse var ret encoding ':' ret
  81.                                     uencoding = upper( encoding )
  82.                                     select
  83.                                         when 'CLIPLIST' = uencoding then
  84.                                             ret = 'TEXT:'getclip( ret )
  85.                                         when 'COMMAND' = uencoding then
  86.                                             ret = 'TEXT:'command( ret )
  87.                                         when 'ENV' = uencoding then
  88.                                             ret = 'TEXT:'getenv( ret )
  89.                                         when 'FILE' = uencoding then
  90.                                             ret = 'TEXT:'getfile( ret )
  91.                                         when 'HEX' = uencoding then
  92.                                             ret = 'TEXT:'x2c( ret )
  93.                                         when 'TEXT' = uencoding then
  94.                                             ret = 'TEXT:'ret
  95.                                         when 'VAR' = uencoding then
  96.                                             ret = 'TEXT:'getvar( ret )
  97.  
  98.                                         otherwise
  99.                                             ret = 'Unknown encoding:'encoding':'ret
  100.                                     end
  101.                                 end
  102.                             end
  103.                             otherwise
  104.                                 err = 10
  105.                         end
  106.                     end
  107.  
  108.                     when 'HELP' == ucmd then
  109.                     do
  110.                         ret =    'TEXT:Commands supported:'eol
  111.                         ret = ret'  ADDATTR                        (has sub-commands):'eol
  112.                         ret = ret'    TAG  <app>:<enc>:<tag>:<val>   Adds a config-entry for <app>.'eol
  113.                         ret = ret'  GETATTR                        (has sub-commands):'eol
  114.                         ret = ret'    APPS <app>                     Lists all <app>s for config-server.'eol
  115.                         ret = ret'    TAG  <app>:<tag>               Gets the "raw" value for an <app>''s <tag>.'eol
  116.                         ret = ret'    TAGS <app>                     Shows all <tag>s for an <app>.'eol
  117.                         ret = ret'    VAL  <app>:<tag>               Gets the "real" value for an <app>''s <tag>.'eol
  118.                         ret = ret'  HELP                           Displays this HELP list.'eol
  119.                         ret = ret'  QUIT                           Halts execution of server.'eol
  120.                         ret = ret'  RELOAD                         Reloads config.  Aliases:'eol
  121.                         ret = ret'    CLEAR'eol
  122.                         ret = ret'    NEW'eol
  123.                         ret = ret'    OPEN'eol
  124.                         ret = ret'  REMATTR                        (has sub-commands):'eol
  125.                         ret = ret'    APP                            Remove all <tags> for an <app>.'eol
  126.                         ret = ret'    TAG                            Remove a <tag> for an <app>.'eol
  127.                         ret = ret'  SAVE                           Saves configuration file.'eol
  128.                     end
  129.  
  130.                     when 'QUIT' == ucmd then
  131.                         done = 1
  132.  
  133.                     when 'RELOAD' == ucmd | 'NEW' == ucmd | 'CLEAR' == ucmd | 'OPEN' == ucmd then
  134.                         ret = read_config_file( )
  135.  
  136.                     when 'REMATTR' == ucmd then
  137.                     do
  138.                         uparam = upper( word( command, 2 ) )
  139.                         select
  140.                             when 'APP' = uparam then
  141.                             do
  142.                                 apps_txt = 'CONFIG-SERVER:APPLICATIONS'
  143.                                 apps = configs.apps_txt
  144.                                 rem_apps = upper( subword( command, 3 ) )
  145.                                 ret = apps
  146.                                 do while '' ~= rem_apps
  147.                                     parse var rem_apps rem_app ':' rem_apps
  148.                                     if '' ~= rem_app then
  149.                                     do
  150.                                         rem_app = ':'rem_app':'
  151.                                         rem_loc = pos( rem_app, upper( apps ) )
  152.                                         if 0 ~= rem_loc then
  153.                                         do
  154.                                             tail_len = length( apps ) -( length( rem_app ) + rem_loc - 1 )
  155.                                             left = left( apps, rem_loc )
  156.                                             right = right( apps, tail_len )
  157.                                             ret = left || right
  158.                                             configs.apps_txt = ret
  159.                                         end
  160.                                     end
  161.                                 end
  162.                             end
  163.                             when 'TAG' = uparam then
  164.                             do
  165.                                 utag = upper( subword( command, 3 ) )
  166.                                 parse var utag uapp ':' utag
  167.                                 if "'S" = right( utag, 2 ) then
  168.                                 do
  169.                                     rem_tags = left( utag, length( utag ) - 2 )
  170.                                     tags_txt = uapp":TAG'S"
  171.                                     tags = configs.tags_txt
  172.                                     ret = tags
  173.                                     do while '' ~= rem_tags
  174.                                         parse var rem_tags rem_tag ':' rem_tags
  175.                                         if '' ~= rem_tag then
  176.                                         do
  177.                                             rem_tag = ':'rem_tag':'
  178.                                             rem_loc = pos( rem_tag, upper( tags ) )
  179.                                             if 0 ~= rem_loc then
  180.                                             do
  181.                                                 tail_len = length( tags ) -( length( rem_tag ) + rem_loc - 1 )
  182.                                                 left = left( tags, rem_loc )
  183.                                                 right = right( tags, tail_len )
  184.                                                 ret = left || right
  185.                                                 configs.tags_txt = ret
  186.                                             end
  187.                                         end
  188.                                     end
  189.                                 end
  190.                                 else
  191.                                 do
  192.                                     parse var utag single_utag '.' tagnum
  193.                                     max_tag_txt = uapp':'single_utag"'S"
  194.                                     parse value configs.max_tag_txt with 'TEXT:'max_tag
  195.                                     if '' = max_tag then
  196.                                         max_tag = 0
  197.                                     if ~datatype( tagnum, 'n' ) then
  198.                                         tagnum = max_tag
  199.                                     if tagnum <= max_tag then
  200.                                     do
  201.                                         utag = uapp':'single_utag
  202.                                         do tag_i = tagnum while tag_i < max_tag
  203.                                             tag_txt = utag'.' || (tag_i + 0)
  204.                                             tag_nxt = utag'.' || (tag_i + 1)
  205.                                             configs.tag_txt = configs.tag_nxt
  206.                                         end
  207.                                         max_tag = max_tag - 1
  208.                                         configs.max_tag_txt = 'TEXT:'max_tag
  209.                                     end
  210.                                     ret = 'TEXT:'max_tag
  211.                                 end
  212.                             end
  213.                             otherwise
  214.                                 err = 10
  215.                         end
  216.                     end
  217.  
  218.                     when 'SAVE' == ucmd then
  219.                         ret = save_config_file( )
  220.  
  221.                     otherwise
  222.                         err = 10
  223.                 end
  224.                 call reply( packet, err, ret )  /*** UNDOCUMENTED 3rd param ***/
  225.             end
  226.         end
  227.     end
  228.     call closeport( port )
  229. exit 0
  230.  
  231.  
  232. read_config_file:
  233.     ret = 0
  234.     configs. = ''
  235.     apps = 'CONFIG-SERVER:APPLICATIONS'
  236.     configs.apps = 'TEXT:'
  237.     if open( config_file, 'env:'config_file ) then
  238.     do
  239.         do until eof( config_file )
  240.             line = readln( config_file )
  241.             if '' ~= line then
  242.             do
  243.                 parse var line app':'enc':'tag':'val
  244.                 if '' ~= tag then
  245.                     call add_config( app, enc, tag, val )
  246.             end
  247.         end
  248.         ret = 1
  249.         close( config_file )
  250.     end
  251. return ret
  252.  
  253.  
  254. add_config_line:
  255. parse arg app ':' enc ':' tag ':' val
  256. return add_config( app, enc, tag, val )
  257.  
  258.  
  259. add_config:
  260. procedure expose configs.
  261. parse arg app, enc, tag, val
  262.     /*** say 'app:' app; say 'enc:' enc; say 'tag:' tag; say 'val:' val ***/
  263.     uapp = upper( app )
  264.     utag = upper( tag )
  265.     uenc = upper( enc )
  266.     tags = uapp':'utag"'S"
  267.     app_tags = uapp":TAG'S"
  268.     apps = 'CONFIG-SERVER:APPLICATIONS'
  269.  
  270.     if 0 = pos( ':'uapp':', upper( configs.apps ) ) then
  271.         configs.apps = configs.apps || app':'
  272.  
  273.     if '' = configs.app_tags then
  274.         configs.app_tags = 'TEXT:'
  275.     if 0 = pos( ':'utag':', upper( configs.app_tags ) ) then
  276.         configs.app_tags = configs.app_tags || tag':'
  277.     /*** say configs.tags ***/
  278.     if '' = configs.tags then
  279.         configs.tags = 'TEXT:'0
  280.     /*** say configs.tags ***/
  281.     parse value configs.tags with ':' tag_count
  282.     /*** say tag_count ***/
  283.     tag_count = tag_count + 1
  284.     /*** say tag_count ***/
  285.  
  286.     configs.tags = 'TEXT:'tag_count
  287.  
  288.     tag = tag'.'tag_count
  289.     utag = upper( tag )
  290.  
  291.     uid = uapp':'utag
  292.     configs.uid = uenc':'val
  293. return 1
  294.  
  295.  
  296. save_config_file:
  297. procedure expose configs. config_file
  298.     env_config = 'env:'config_file
  299.     earc_config = 'envarc:'config_file
  300.     ret = 0
  301.     if open( env_config, env_config, 'w' ) then
  302.     do
  303.         if open( earc_config, earc_config, 'w' ) then
  304.         do
  305.             app_tags_txt = ':TAG''S'
  306.             apps = app_list( )
  307.             parse var apps 'TEXT:' apps
  308.             do while '' ~= apps
  309.                 parse var apps app ':' apps
  310.                 uapp = upper( app )
  311.                 app_tags = app || app_tags_txt
  312.                 uapp_tags = upper( app_tags )
  313.                 tags = configs.uapp_tags
  314.                 utags = upper( tags )
  315.                 parse var utags 'TEXT:' tag_list
  316.                 do while '' ~= tag_list
  317.                     parse var tag_list tag ':' tag_list
  318.                     tags = uapp':'tag"'S"
  319.                     tag_count = configs.tags
  320.                     parse var tag_count 'TEXT:' tag_count
  321.                     do i = 1 for tag_count
  322.                         app_tag_i = uapp':'tag'.'i
  323.                         cfg = configs.app_tag_i
  324.                         parse var cfg enc ':' val
  325.                         call writeln( env_config, app':'enc':'tag':'val )
  326.                         call writeln( earc_config, app':'enc':'tag':'val )
  327.                     end
  328.                 end
  329.             end
  330.             call close( earc_config )
  331.             ret = 1
  332.         end
  333.         call close( env_config )
  334.     end
  335. return ret
  336.  
  337.  
  338. get_tag:
  339. procedure expose configs.
  340. parse arg utag
  341.     parse var utag uapp ':' utag
  342.     if '' = utag then
  343.         utag = "TAG'S"
  344.     else if 0 = pos( '.', utag ) & "'S" ~== right( utag, 2 ) then
  345.     do
  346.         tagnum = uapp':'utag"'S"
  347.         tagnum = configs.tagnum
  348.         parse var tagnum 'TEXT:'tagnum
  349.         utag = utag'.'tagnum
  350.     end
  351.     utag = uapp':'utag
  352. return configs.utag
  353.  
  354.  
  355. app_list:
  356. procedure expose configs.
  357.     apps = 'CONFIG-SERVER:APPLICATIONS'
  358. return configs.apps
  359.  
  360.