1: Okay, this is a bit old, but I found it, and it was fun to do ;)) <BR>
   2: SUN needed the configuration of our system, I could do it in C<BR>
   3: but didn't feel like cutting and pasting everything:<BR>
   4: <CODE>
   5: #!/usr/bin/perl -w
   6: use strict;
   7: 
   8: my (@Variables,@Code);
   9: 
  10: foreach (`man sysconf`)
  11: {
  12:  s/^\s+//;
  13:  my $Var=(split /\s+/,$_,2)[0] or next;
  14:  next unless ($Var=~/^_[A-Z_]+$/o);
  15:  my $Cvar=lc $Var;
  16:  push @Variables,"long int $Cvar=sysconf($Var);";
  17:  push @Code,"printf(\"$Var set to: \%d\\n\",$Cvar);";
  18: };
  19: local $"="\n ";
  20: 
  21: print << "PROGRAM";
  22: #include <stdio.h>
  23: #include <unistd.h>
  24: 
  25: int main (argc,argv)
  26: int argc;
  27: char *argv[];
  28: {
  29:  @Variables
  30:  @Code
  31: }
  32: PROGRAM
  33: </CODE>
  34: <BR>
  35: Presto!!! redirect to getconf.c, compile, run, be happy....<BR>
  36: <BR>
  37: I have to add here that this code is NOT <BR>
  38: very portable... In this case it didn't need to be... <BR>
  39: It was short and worked ([jeroenes] posted an update here, and<BR>
  40: I did some updating myself, so this should have made it a bit<BR>
  41: more portable (for what I don't know though ;))<BR>
  42: <BR>
  43: You MAY run into gcc compiling errors, this is a result from labels in<BR>
  44: the man page not being in your include file<BR>
  45: delete those items ;))<BR>
  46: <BR>
  47: GreetZ!, <BR><UL>ChOas</UL><BR>

Replies are listed 'Best First'.
Re: Get your system configuration from a man page.
by jeroenes (Priest) on Jan 08, 2001 at 16:23 UTC
    Cool. Needed some changes to get it working, though.
    s/^\s+//; my $Var=(split /\s+/,$_,2)[0] or next; next unless ($Var=~/_/o);
    I think leading whitespace stuff broke your script on my system. So I deleted it. The or next is needed to prevent the compiler from whining about undefined variables. Couldn't get my gcc to eat the .c, though. Has something to do with a whole mess of backspaces.

    Update: Got it now (at last). By coincedence, all references to the macros are without preceding _, and otherwise they appear just before end-of-sentence ;-) (aka: '.'). So now I have the following code, which compiles if I manually delete one entry that appears to miss in the lib somehow.

    ... s/^\s+//; my $Var=(split /\s+/,$_,2)[0] or next; $Var =~ s/.\010//g; next unless ($Var=~/^_/o); next if ($Var =~ /\./); my $Cvar=lc $Var; ....

    Cheers,

    Jeroen
    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

Re: Get your system configuration from a man page.
by jeroenes (Priest) on Feb 05, 2001 at 15:45 UTC
    It can even be done without that evil C! Today, I stumbled across POSIX, and decided to play with it a little.
    use POSIX; for (grep{ /^_\w/; } keys %::) { eval( "print \"$_ is set to \".$_.\"\n\";"); }
    is all you need.

    Cheers,

    Jeroen
    "We are not alone"(FZ)

Re: Get your system configuration from a man page.
by petral (Curate) on Jan 24, 2001 at 01:06 UTC
    Just for completeness:
    next unless ($Var=~/^_[A-Z][A-Z_128]+$/);
    catches the following:
    _SC_2_C_BIN _POSIX2_C_BIND _SC_2_C_DEV _POSIX2_C_DEV _SC_2_C_VERSION _POSIX2_C_VERSION _SC_2_CHAR_TERM _POSIX2_CHAR_TERM _SC_2_FORT_DEV _POSIX2_FORT_DEV _SC_2_FORT_RUN _POSIX2_FORT_RUN _SC_2_LOCALEDEF _POSIX2_LOCALEDEF _SC_2_SW_DEV _POSIX2_SW_DEV _SC_2_UPE _POSIX2_UPE _SC_2_VERSION _POSIX2_VERSION _SC_XOPEN_ENH_I18N _XOPEN_ENH_I18N


    p