Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 14 Apr 2004 16:35:02 -0600
From:      Brandon Erhart <berhart@ErhartGroup.COM>
To:        Craig Rodrigues <rodrigc@crodrigues.org>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: C code for parsing rc.conf?
Message-ID:  <6.0.2.0.2.20040414162810.01c8cb48@mx1.erhartgroup.com>
In-Reply-To: <20040414215601.GA3923@crodrigues.org>
References:  <20040414215601.GA3923@crodrigues.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Not that I know of, but it should be a breeze to write a simple parsing engine.
Just ignore all lines starting with a '#', and break at the '=' sign. The 
first part would be your variable name, the last part your value for it.
Then just display variables and their names, and maybe parse the variable 
names so you can assign meaningful help information to them.

I didn't compile this, not sure if it'll work, but it'll give you a good 
idea as to what your code may look like ..

int main(int argc, char **argv)
{
FILE *rc;
char buf[512];


if ( (rc=fopen("/etc/rc.conf", "r")) == NULL)
{
         perror("fopen()");

         exit(EXIT_FAILURE);
}

while (fgets(buf, sizeof(buf), rc) != NULL)
{
         char *eq_ptr, var_name[256], var_value[256];

         some_function_to_strip_trailing_and_pre_whitespace(buf); 
/* this function will just strip pre- and trailing whitespace from the line */

         if (!strlen(buf))   continue;    /* blank line */

         if (buf[0] == '#')  continue;   /* comment line */

         if ( (eq_ptr = index(buf, '=')) == NULL)   continue;   /* no equal 
sign */

         *eq_ptr = '\0';

         memset(var_name, 0, 256);
         memset(var_value, 0, 256);

         if (!strlen(buf) || !strlen(eq_ptr+1))   continue;    /* either 
the variable name or the value was empty */

         strncpy(var_name, buf, 255);
         strncpy(var_value, eq_ptr+1, 255);

         printf("%s=%s\n", var_name, var_value);
}

         exit(EXIT_SUCCESS);
}

At 03:56 PM 4/14/2004, you wrote:
>Hi,
>
>Is there a C library that comes with FreeBSD which
>can be used to parse, append to, and validate
>rc.conf?
>
>I'd like to customize some of the settings in /etc/rc.conf
>with my own GUI-based program.  It's not too hard
>to write something on my own, but I was wondering
>if a reusable library existed in FreeBSD 4.x or 5.x for doing this.
>
>Thanks.
>
>--
>Craig Rodrigues
>http://crodrigues.org
>rodrigc@crodrigues.org
>_______________________________________________
>freebsd-hackers@freebsd.org mailing list
>http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
>To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"



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