From owner-freebsd-hackers Thu Jan 2 14: 2: 0 2003 Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3563F37B401 for ; Thu, 2 Jan 2003 14:01:59 -0800 (PST) Received: from pop3.psconsult.nl (ps226.psconsult.nl [193.67.147.226]) by mx1.FreeBSD.org (Postfix) with ESMTP id 55E3F43E4A for ; Thu, 2 Jan 2003 14:01:56 -0800 (PST) (envelope-from paul@pop3.psconsult.nl) Received: (from paul@localhost) by pop3.psconsult.nl (8.9.2/8.9.2) id XAA09690 for freebsd-hackers@FreeBSD.ORG; Thu, 2 Jan 2003 23:01:54 +0100 (CET) (envelope-from paul) Date: Thu, 2 Jan 2003 23:01:54 +0100 From: Paul Schenkeveld To: freebsd-hackers@FreeBSD.ORG Subject: Re: Reading rc.conf from C programs? Message-ID: <20030102230154.A8638@psconsult.nl> References: <1041540674.3e14a642d30b5@webmail1.isg.siue.edu> <4967.1041540976@critter.freebsd.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: <4967.1041540976@critter.freebsd.dk>; from phk@FreeBSD.ORG on Thu, Jan 02, 2003 at 09:56:16PM +0100 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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