home *** CD-ROM | disk | FTP | other *** search
- /*
- ** $Id: config-server.rexx,v 1.1 1995/02/12 22:13:58 rkr Exp $
- **
- ** Config-server uses/maintains a configuration database.
- **
- ** See /doc/config-server.doc for more info.
- **
- ** BUGS
- ** When doing a REMATTR, we just lose track of the field, we don't
- ** really remove it. In theory, this shouldn't make a difference, but
- ** careless app's may mistakenly re-use "deleted" fields if they don't
- ** use the database properly (or if another app is editing the same
- ** fields concurrently).
- **
- ** I may or may not fix this one. Under normal circumstances, it
- ** won't matter, and you can "force" it by doing a SAVE then RELOAD,
- ** if you like. (SAVE only writes what is tracked, and RELOAD clears
- ** all tracked and untracked fields before re-reading the file.)
- **
- */
- pragma( 'stack', 4000 )
- port = 'config-server'
- config_file = 'config-server.config'
- if ~openport( port ) then /*** contrary to docs, returns 0/1, NOT an address! ***/
- do
- address command 'say Failed to open config-server port!'
- exit
- end
-
- call read_config_file( )
- null = '0000 0000'x
- eol = '0a'x
- done = 0
- do until done
- call waitpkt( port )
- do until null = packet
- packet = getpkt( port )
- if null ~= packet then
- do
- ret = ''
- err = 0
- command = getarg( packet )
- ucmd = upper( word( command, 1 ) )
- select
- when 'ADDATTR' == ucmd then
- do
- uparam = upper( word( command, 2 ) )
- select
- /*** TAG or VAL? ***/
- when 'TAG' == uparam then
- call add_config_line( subword( command, 3 ) )
- otherwise
- err = 10
- end
- end
-
- when 'GETATTR' == ucmd then
- do
- uparam = upper( word( command, 2 ) )
- select
- when 'APPS' = uparam then
- ret = app_list( )
-
- when 'TAG' = uparam then
- ret = get_tag( upper( subword( command, 3 ) ) )
-
- when 'TAGS' = uparam then
- do
- utag = upper( subword( command, 3 ) )
- parse var utag uapp ':' utag
- utag = uapp":TAG'S"
- ret = configs.utag
- end
-
- when 'VAL' = uparam then
- do
- ret = get_tag( upper( subword( command, 3 ) ) )
- if '*' ~= left( ret, 1 ) & '' ~= ret then
- do
- parse var ret encoding ':' ret
- uencoding = upper( encoding )
- select
- when 'CLIPLIST' = uencoding then
- ret = 'TEXT:'getclip( ret )
- when 'COMMAND' = uencoding then
- ret = 'TEXT:'command( ret )
- when 'ENV' = uencoding then
- ret = 'TEXT:'getenv( ret )
- when 'FILE' = uencoding then
- ret = 'TEXT:'getfile( ret )
- when 'HEX' = uencoding then
- ret = 'TEXT:'x2c( ret )
- when 'TEXT' = uencoding then
- ret = 'TEXT:'ret
- when 'VAR' = uencoding then
- ret = 'TEXT:'getvar( ret )
-
- otherwise
- ret = 'Unknown encoding:'encoding':'ret
- end
- end
- end
- otherwise
- err = 10
- end
- end
-
- when 'HELP' == ucmd then
- do
- ret = 'TEXT:Commands supported:'eol
- ret = ret' ADDATTR (has sub-commands):'eol
- ret = ret' TAG <app>:<enc>:<tag>:<val> Adds a config-entry for <app>.'eol
- ret = ret' GETATTR (has sub-commands):'eol
- ret = ret' APPS <app> Lists all <app>s for config-server.'eol
- ret = ret' TAG <app>:<tag> Gets the "raw" value for an <app>''s <tag>.'eol
- ret = ret' TAGS <app> Shows all <tag>s for an <app>.'eol
- ret = ret' VAL <app>:<tag> Gets the "real" value for an <app>''s <tag>.'eol
- ret = ret' HELP Displays this HELP list.'eol
- ret = ret' QUIT Halts execution of server.'eol
- ret = ret' RELOAD Reloads config. Aliases:'eol
- ret = ret' CLEAR'eol
- ret = ret' NEW'eol
- ret = ret' OPEN'eol
- ret = ret' REMATTR (has sub-commands):'eol
- ret = ret' APP Remove all <tags> for an <app>.'eol
- ret = ret' TAG Remove a <tag> for an <app>.'eol
- ret = ret' SAVE Saves configuration file.'eol
- end
-
- when 'QUIT' == ucmd then
- done = 1
-
- when 'RELOAD' == ucmd | 'NEW' == ucmd | 'CLEAR' == ucmd | 'OPEN' == ucmd then
- ret = read_config_file( )
-
- when 'REMATTR' == ucmd then
- do
- uparam = upper( word( command, 2 ) )
- select
- when 'APP' = uparam then
- do
- apps_txt = 'CONFIG-SERVER:APPLICATIONS'
- apps = configs.apps_txt
- rem_apps = upper( subword( command, 3 ) )
- ret = apps
- do while '' ~= rem_apps
- parse var rem_apps rem_app ':' rem_apps
- if '' ~= rem_app then
- do
- rem_app = ':'rem_app':'
- rem_loc = pos( rem_app, upper( apps ) )
- if 0 ~= rem_loc then
- do
- tail_len = length( apps ) -( length( rem_app ) + rem_loc - 1 )
- left = left( apps, rem_loc )
- right = right( apps, tail_len )
- ret = left || right
- configs.apps_txt = ret
- end
- end
- end
- end
- when 'TAG' = uparam then
- do
- utag = upper( subword( command, 3 ) )
- parse var utag uapp ':' utag
- if "'S" = right( utag, 2 ) then
- do
- rem_tags = left( utag, length( utag ) - 2 )
- tags_txt = uapp":TAG'S"
- tags = configs.tags_txt
- ret = tags
- do while '' ~= rem_tags
- parse var rem_tags rem_tag ':' rem_tags
- if '' ~= rem_tag then
- do
- rem_tag = ':'rem_tag':'
- rem_loc = pos( rem_tag, upper( tags ) )
- if 0 ~= rem_loc then
- do
- tail_len = length( tags ) -( length( rem_tag ) + rem_loc - 1 )
- left = left( tags, rem_loc )
- right = right( tags, tail_len )
- ret = left || right
- configs.tags_txt = ret
- end
- end
- end
- end
- else
- do
- parse var utag single_utag '.' tagnum
- max_tag_txt = uapp':'single_utag"'S"
- parse value configs.max_tag_txt with 'TEXT:'max_tag
- if '' = max_tag then
- max_tag = 0
- if ~datatype( tagnum, 'n' ) then
- tagnum = max_tag
- if tagnum <= max_tag then
- do
- utag = uapp':'single_utag
- do tag_i = tagnum while tag_i < max_tag
- tag_txt = utag'.' || (tag_i + 0)
- tag_nxt = utag'.' || (tag_i + 1)
- configs.tag_txt = configs.tag_nxt
- end
- max_tag = max_tag - 1
- configs.max_tag_txt = 'TEXT:'max_tag
- end
- ret = 'TEXT:'max_tag
- end
- end
- otherwise
- err = 10
- end
- end
-
- when 'SAVE' == ucmd then
- ret = save_config_file( )
-
- otherwise
- err = 10
- end
- call reply( packet, err, ret ) /*** UNDOCUMENTED 3rd param ***/
- end
- end
- end
- call closeport( port )
- exit 0
-
-
- read_config_file:
- ret = 0
- configs. = ''
- apps = 'CONFIG-SERVER:APPLICATIONS'
- configs.apps = 'TEXT:'
- if open( config_file, 'env:'config_file ) then
- do
- do until eof( config_file )
- line = readln( config_file )
- if '' ~= line then
- do
- parse var line app':'enc':'tag':'val
- if '' ~= tag then
- call add_config( app, enc, tag, val )
- end
- end
- ret = 1
- close( config_file )
- end
- return ret
-
-
- add_config_line:
- parse arg app ':' enc ':' tag ':' val
- return add_config( app, enc, tag, val )
-
-
- add_config:
- procedure expose configs.
- parse arg app, enc, tag, val
- /*** say 'app:' app; say 'enc:' enc; say 'tag:' tag; say 'val:' val ***/
- uapp = upper( app )
- utag = upper( tag )
- uenc = upper( enc )
- tags = uapp':'utag"'S"
- app_tags = uapp":TAG'S"
- apps = 'CONFIG-SERVER:APPLICATIONS'
-
- if 0 = pos( ':'uapp':', upper( configs.apps ) ) then
- configs.apps = configs.apps || app':'
-
- if '' = configs.app_tags then
- configs.app_tags = 'TEXT:'
- if 0 = pos( ':'utag':', upper( configs.app_tags ) ) then
- configs.app_tags = configs.app_tags || tag':'
- /*** say configs.tags ***/
- if '' = configs.tags then
- configs.tags = 'TEXT:'0
- /*** say configs.tags ***/
- parse value configs.tags with ':' tag_count
- /*** say tag_count ***/
- tag_count = tag_count + 1
- /*** say tag_count ***/
-
- configs.tags = 'TEXT:'tag_count
-
- tag = tag'.'tag_count
- utag = upper( tag )
-
- uid = uapp':'utag
- configs.uid = uenc':'val
- return 1
-
-
- save_config_file:
- procedure expose configs. config_file
- env_config = 'env:'config_file
- earc_config = 'envarc:'config_file
- ret = 0
- if open( env_config, env_config, 'w' ) then
- do
- if open( earc_config, earc_config, 'w' ) then
- do
- app_tags_txt = ':TAG''S'
- apps = app_list( )
- parse var apps 'TEXT:' apps
- do while '' ~= apps
- parse var apps app ':' apps
- uapp = upper( app )
- app_tags = app || app_tags_txt
- uapp_tags = upper( app_tags )
- tags = configs.uapp_tags
- utags = upper( tags )
- parse var utags 'TEXT:' tag_list
- do while '' ~= tag_list
- parse var tag_list tag ':' tag_list
- tags = uapp':'tag"'S"
- tag_count = configs.tags
- parse var tag_count 'TEXT:' tag_count
- do i = 1 for tag_count
- app_tag_i = uapp':'tag'.'i
- cfg = configs.app_tag_i
- parse var cfg enc ':' val
- call writeln( env_config, app':'enc':'tag':'val )
- call writeln( earc_config, app':'enc':'tag':'val )
- end
- end
- end
- call close( earc_config )
- ret = 1
- end
- call close( env_config )
- end
- return ret
-
-
- get_tag:
- procedure expose configs.
- parse arg utag
- parse var utag uapp ':' utag
- if '' = utag then
- utag = "TAG'S"
- else if 0 = pos( '.', utag ) & "'S" ~== right( utag, 2 ) then
- do
- tagnum = uapp':'utag"'S"
- tagnum = configs.tagnum
- parse var tagnum 'TEXT:'tagnum
- utag = utag'.'tagnum
- end
- utag = uapp':'utag
- return configs.utag
-
-
- app_list:
- procedure expose configs.
- apps = 'CONFIG-SERVER:APPLICATIONS'
- return configs.apps
-
-