Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 2 Jan 2003 23:01:54 +0100
From:      Paul Schenkeveld <fb-hackers@psconsult.nl>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: Reading rc.conf from C programs?
Message-ID:  <20030102230154.A8638@psconsult.nl>
In-Reply-To: <4967.1041540976@critter.freebsd.dk>; from phk@FreeBSD.ORG on Thu, Jan 02, 2003 at 09:56:16PM %2B0100
References:  <1041540674.3e14a642d30b5@webmail1.isg.siue.edu> <4967.1041540976@critter.freebsd.dk>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi all,

On Thu, Jan 02, 2003 at 09:56:16PM +0100, phk@FreeBSD.ORG wrote:
> In message <1041540674.3e14a642d30b5@webmail1.isg.siue.edu>, wgrim@siue.edu wri
> tes:
> >Well, perhaps I'm missing something here, but can't you just tokenize the items 
> >in rc.conf using strtok after opening up the file in your C program?
> 
> Only if you parse and evaluate the entire /bin/sh syntax.

So, I've come up with the following.  With 'env -' you can clear all
environment variables, with 'set -a' you can mark all shell variables
as exported and with '. /etc/rc.conf' or '. /etc/defaults/rc.conf' you
can suck in those variables into a shell.

Put this all together into:

  env - sh -ac '. /etc/defaults/rc.conf; /usr/bin/printenv'

and parse the output of that.

If you are worried about special characters (notably newlines) in the
output of printenv, you can grab my own version of printenv at

    ftp://ftp.psconsult.nl/pub/src/myprintenv/myprintenv.c

and put the binary in /usr/local/bin.

Then change the above command in:

  env - sh -ac '. /etc/defaults/rc.conf; /usr/local/bin/myprintenv -q'

Myprintenv -q puts double quotes around the value of each variable and
precedes the following characters by a backslash: "\$`

Parsing this output should not be that difficult and is unambiguous
with regards to special characters.  The only caveat is special characters
in variable names but '. /etc/defaults/rc.conf' will take care of that.

Hope this helps.

If people think that -q is a good option for printenv, I can come up
with a patch for the standard printenv.  Opinions?

-- 
Paul Schenkeveld, Consultant
PSconsult ICT Services BV

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030102230154.A8638>