Thread: RE: [Sablevm-developer] Re: Config File Parsing
Brought to you by:
egagnon
From: Brent F. <bre...@xp...> - 2000-12-05 22:06:11
|
I've got a stupid C question: Is it possible to modify the char* vector "argv" passed from the command line? Based on our earlier e-mail, I would anticipate the following: 1. User starts sablevm with one or two command line options. 2. SableVM opens and parses the /etc/sablevm file. (This works) 3. SableVM must then insert options from the /etc/sablevm file into the argv that gets passed to the "popt" library for parsing. How does this work? I tried a realloc on the argv vector, but that caused a segfault. In fact, more generally, how do you do something like this: int main() { char* args[3] = { "--foo", "--bar", "--baz" }; int argc = 3; do_something(&argc, args); } void do_something(int* argc, char** argv) { /* Add some new fields */ char* data = realloc(*args, 1000); /* I think this actually just macs args[0] = a buffer of size 1000. It doesn't grow the whole block. */ data[4] = "--more_stuff"; /* I think this ... } The problem I'm having is with conceptualizing pointer-to-pointer versus array-of-pointers. How can I achieve the desired behavior of basically "growing" an array of pointers? I know how I would achieve this under C++, but I'm unclear as to the proper behavior under C. Any help *greatly* appreciated. Thanks, -Brent |
From: Brent F. <bre...@xp...> - 2000-12-07 19:04:02
|
> A very simple program could read options from a configuration > file, strip leading/trailing space characters (at the line > beginning/end, and around the first "=" character) then add > a "--" prefix, then store the result in an array of strings, > then simply add to this array the strings of argv[]. As > usual, empty lines (after stripping) and comment lines > (starting with #) would be ignored. Line continuation > support would be nice to have, too;-) > I've got this pretty much coded up. One thing I'm curious about is this: Let's say I have a master config file in e.g., /etc/sablevmrc: boot-class-path = /usr/share/sable/classes boot-library-path = /usr/lib/sable And I have my own config file in $HOME/.sablevmrc: boot-class-path = /usr/src/sablepath/classes boot-library-path = /usr/src/sablepath/lib And I write my command line: #>sablevm --boot-class-path=/usr/src/sable2/classes -- HelloWorld I believe that the command line should take precedence over the $HOME sable config file, and the $HOME config file over the system config file. If so, I think I'll have to take a pass through the files first, tossing out duplicates per the above rules of precedence. What do you think? -Brent |
From: Etienne M. G. <eg...@j-...> - 2000-12-07 19:13:25
|
Brent Fulgham wrote: > If so, I think I'll have to take a pass through the > files first, tossing out duplicates per the above rules > of precedence. > > What do you think? If you put everything in back-precdence order in the agrv-style array, then everything should work fine. Popt is very handy there; it assures us that we get the latest definition for each one. I like popt; does it show? :o) Etienne -- ---------------------------------------------------------------------- Etienne M. Gagnon, M.Sc. e-mail: eg...@j-... Author of SableCC: http://www.sable.mcgill.ca/sablecc/ and SableVM: http://www.sablevm.org/ ---------------------------------------------------------------------- |
From: Brent F. <bre...@xp...> - 2000-12-07 19:16:31
|
> If you put everything in back-precdence order in the agrv-style array, > then everything should work fine. > > Popt is very handy there; it assures us that we get the latest > definition for each one. I like popt; does it show? > > :o) > Great! I'll try to check something in tomorrow or so. (And my JNI book finally arrived...) -Brent |
From: Etienne M. G. <eg...@j-...> - 2000-12-07 19:21:15
|
Brent Fulgham wrote: > Great! I'll try to check something in tomorrow or so. Super! Have you made a 'cvs update' lately? I have finally resolved the problem I was having to cleanly define PKGDATADIR and SYSCONFDIR in configure.in. I also made minor modifications in sablevm.c which may affect you. > (And my JNI book finally arrived...) Lots of interesting reading ahead! Thanks! Etienne -- ---------------------------------------------------------------------- Etienne M. Gagnon, M.Sc. e-mail: eg...@j-... Author of SableCC: http://www.sable.mcgill.ca/sablecc/ and SableVM: http://www.sablevm.org/ ---------------------------------------------------------------------- |
From: Brent F. <bfu...@de...> - 2000-12-08 06:49:46
|
I've committed changes that implement a configuration file parsing system. It starts at the SYSCONFDIR (which is set by autoconf), then checks the user's $HOME directory, and finally it evaluates the command-line parameters. The parameters observed the following rules of precedence: 1. Command-line parameters trump everything. 2. $HOME directory configuration parameters are second highest. 3. SYSCONFDIR config parametres have lowest priority. I've tested it on my system, so I'm pretty sure it doesn't break anything. But of course, please let me know if you discover otherwise! ;-) Thanks, -Brent |
From: Etienne M. G. <eg...@j-...> - 2000-12-09 05:42:28
|
Hi Brent! Sorry for taking some time to reply. I was partly in bed, fighting a popular Winter virus:-( [By the way, it is quite cold here these days: the maximum today was -15 celcius!] Thanks a lot for your contribution. I have looked at it quickly, and it seems mostly good. By the way, I forgot to tell you: to indent your code, simply type 'make indent' in the root directory, and it will indent all your code according to the GNU standard. On Debian, make sure to 'apt-get install indent' first. There are a few points, though [I know, I am very picky;-)]: 1- You use hard coded array lenghts, this can cause all sorts of problems. (Ever heard about buffer overflow exploits?) 2- You are using a non ISO/POSIX function `getline'. I have fixed configure.in so that this breaks the build, which is a good thing, if we want SableVM to remain portable. So, if you could help me fixing these two things, it would be great. [No more MAXPATHLENGTH, or 12 (!!!) constants], and replace the call to getline() by something equivalent that only uses ISO/POSIX library calls. [Preference going to pure ISO library]. I have, here, "The Annotated ANSI C Standard" ISBN 0-07-881952 which contains the ANSI/ISO 9899-1990 standard text and annotations. You can through the annotations out of the window, but the standard text is a wonderful source of information for writing portable code. But be warned, it is a little dry to read (it seems written by lawyers). You might get away with simpler texts, but you should definitely have some reference book for writing portable code. In last resort, 'man 3 functionname' also contains a "CONFORMING TO" section. Try 'man 3 getc'. OK. Thanks a lot. Etienne cvs Brent Fulgham wrote: > > I've committed changes that implement a configuration file > parsing system. It starts at the SYSCONFDIR (which is set > by autoconf), then checks the user's $HOME directory, and > finally it evaluates the command-line parameters. > > The parameters observed the following rules of precedence: > > 1. Command-line parameters trump everything. > 2. $HOME directory configuration parameters are second highest. > 3. SYSCONFDIR config parametres have lowest priority. > > I've tested it on my system, so I'm pretty sure it doesn't > break anything. But of course, please let me know if you > discover otherwise! ;-) > > Thanks, > > -Brent > _______________________________________________ > Sablevm-developer mailing list > Sab...@li... > http://lists.sourceforge.net/mailman/listinfo/sablevm-developer -- ---------------------------------------------------------------------- Etienne M. Gagnon, M.Sc. e-mail: eg...@j-... Author of SableCC: http://www.sable.mcgill.ca/sablecc/ and SableVM: http://www.sablevm.org/ ---------------------------------------------------------------------- |
From: Etienne M. G. <eg...@j-...> - 2000-12-09 06:04:33
|
Hi again. > 2- You are using a non ISO/POSIX function `getline'. I have fixed > configure.in so that this breaks the build, which is a good thing, if we > want SableVM to remain portable. In fact, for the error to show, you need to do the following: $ cvs update $ export CFLAGS='-Werror -O0 -ggdb' $ autoreconf $ make clean $ make I always keep the CFLAGS set as shown above during development: you get a nicer debugger behavior, and you don't forget any warning;-) I usually use DDD (apt-get install ddd). Quite nice; allows you to browse your data structures. Etienne -- ---------------------------------------------------------------------- Etienne M. Gagnon, M.Sc. e-mail: eg...@j-... Author of SableCC: http://www.sable.mcgill.ca/sablecc/ and SableVM: http://www.sablevm.org/ ---------------------------------------------------------------------- |
From: Brent F. <bfu...@de...> - 2000-12-09 07:24:42
|
On Sat, Dec 09, 2000 at 12:42:16AM -0500, Etienne M. Gagnon wrote: > Hi Brent! > > Sorry for taking some time to reply. I was partly in bed, fighting a > popular Winter virus:-( [By the way, it is quite cold here these days: > the maximum today was -15 celcius!] > Brrr! I hate to be cruel, but here in Ventura California the low has been about 45 F (evening), and around 65 F (Daylight). (I'm wearing shorts right now.) > Thanks a lot for your contribution. I have looked at it quickly, and it > seems mostly good. > Great! > By the way, I forgot to tell you: to indent your code, simply type > 'make indent' in the root directory, and it will indent all your code > according to the GNU standard. On Debian, make sure to 'apt-get install > indent' first. > Already had it, but I have never worked it into the build system like this before. What a great idea! My indent settings are different from your in my editor, so it's nice to be able to get things in synch. > There are a few points, though [I know, I am very picky;-)]: > 1- You use hard coded array lenghts, this can cause all sorts of > problems. (Ever heard about buffer overflow exploits?) Yes! That's why I use strnlen/strncpy everywhere. But you are quite correct. I've modified the code to have no limits on the size of the array or the length of the strings. I have tested it with a file of 18 options, but there may be some nastiness I've missed. Let's all test it out... :-) > 2- You are using a non ISO/POSIX function `getline'. I have fixed > configure.in so that this breaks the build, which is a good thing, if we > want SableVM to remain portable. > Ack. I did think about this, but I read in the GNU C Library manual that getline is recommended since it is safer, because it does a realloc on the string buffer if it's not long enough. However, it's not portable so I've reworked the code to use "fgets", which should be ANSI compliant according to my "System V" programming guide. Please let me know if this still causes trouble. > > So, if you could help me fixing these two things, it would be great. > [No more MAXPATHLENGTH, or 12 (!!!) constants], and replace the call to > getline() by something equivalent that only uses ISO/POSIX library > calls. [Preference going to pure ISO library]. > Done! -Brent |
From: Etienne M. G. <eg...@j-...> - 2000-12-11 23:28:54
|
Brent Fulgham wrote: > Yes! That's why I use strnlen/strncpy everywhere. But you are > quite correct. I've modified the code to have no limits on the > size of the array or the length of the strings. Good! > I have tested it with a file of 18 options, but there may be some > nastiness I've missed. Let's all test it out... :-) I have made a few nasty tests, an submitted 2 bug reports. (Finally testing this SF feature;-). > Ack. I did think about this, but I read in the GNU C Library manual > that getline is recommended since it is safer, because it does a > realloc on the string buffer if it's not long enough. However, it's > not portable so I've reworked the code to use "fgets", which should > be ANSI compliant according to my "System V" programming guide. > Please let me know if this still causes trouble. The man pages are quite correct, as far as I've experienced so far. > Done! Thanks! Tell me if you need more than "technician" privilege to bugs. Etienne -- ---------------------------------------------------------------------- Etienne M. Gagnon, M.Sc. e-mail: eg...@j-... Author of SableCC: http://www.sable.mcgill.ca/sablecc/ and SableVM: http://www.sablevm.org/ ---------------------------------------------------------------------- |
From: Etienne M. G. <eg...@j-...> - 2000-12-05 22:36:52
|
Brent Fulgham wrote: > I've got a stupid C question: > > Is it possible to modify the char* vector "argv" passed from > the command line? No. You have to allocate a new array. > Based on our earlier e-mail, I would anticipate the following: > > 1. User starts sablevm with one or two command line options. > 2. SableVM opens and parses the /etc/sablevm file. (This works) The location of this file should be chosen by "configure". The existence of this file is optional. We should also consider allowing for user specific configuration file (typically in $HOME/.sablevm), but this can wait until the system-wide one works. I am taking care of that. You can assume that there is a SYSCONFDIR (#defined in config.h) that will contain this path (typically: "/usr/local/etc"). > 3. SableVM must then insert options from the /etc/sablevm file > into the argv that gets passed to the "popt" library for > parsing. No. You simply create an array big enough to contain all options. > > How does this work? I tried a realloc on the argv vector, but > that caused a segfault. realloc won't work for argv. Etienne -- ---------------------------------------------------------------------- Etienne M. Gagnon, M.Sc. e-mail: eg...@j-... Author of SableCC: http://www.sable.mcgill.ca/sablecc/ and SableVM: http://www.sablevm.org/ ---------------------------------------------------------------------- |
From: Etienne M. G. <eg...@j-...> - 2000-12-05 22:51:29
|
"Etienne M. Gagnon" wrote: > .... You can assume that there is a SYSCONFDIR > (#defined in config.h) that will contain this path (typically: > "/usr/local/etc"). This is now done. Simply type "cvs update" in your root sablevm directory. > realloc won't work for argv. So what you must do is: ... char **all_opts; ... all_opts = malloc (sizeof (char *) * (argc + config_opts_count)); ... then, ... context = poptGetContext ("sablevm", argc + config_opts_count, (const char **) all_opts, options, 0); ... -- ---------------------------------------------------------------------- Etienne M. Gagnon, M.Sc. e-mail: eg...@j-... Author of SableCC: http://www.sable.mcgill.ca/sablecc/ and SableVM: http://www.sablevm.org/ ---------------------------------------------------------------------- |