Version 3 of RC

Updated 2008-09-03 23:23:32 by CMcC

For information on "rc files", see the RC file. This page describes a utility command for parsing application-specific RC files.


CMcC 2008Sep1 was looking for a way to read configuration into dicts and such.

I wanted something like subst to perform variable and functional substitution, but which guaranteed the resultant form would be list-like, if the input was. I also wanted the ability to intersperse comments in the configuration structure. What I really wanted was a tcl-like little-language for configuration.

The following proc ignores lines which are commented like tcl source, and substitutes [] and $-forms. Its output is a list of fully substituted tcl-like words. It may be nested.

Caveat: The little language isn't completely coherent and consistent, in that the characters ; and # always have special literal significance as commencing comments. I'm prepared to tolerate that inflexibility in exchange for speed and ease of parsing, but it may not suit you.

    proc rc {text} {
        set accum ""
        set result {}
        foreach line [split $text \n] {
            set line [string trim $line]
            if {$line eq ""} continue
            lassign [split $line {\#;}] line
            append accum " " [string trim $line]
            if {$accum ne "" && [info complete $accum]} {
                set pass [uplevel 1 list $accum]
                lappend result {*}$pass
                set accum ""
            }
        }
        return $result
    }

Lars H: Have you considered using an empty interpreter instead?

CMcC yes, and it may well be worth doing, however I want to be able to invoke a fairly wide range of functions and grab variable values from the config.

Additionally, it doesn't buy you much, as you must still feed the stuff a 'complete' line at a time, and presumably prepend a set to each line, or something.