Article 6529 of comp.lang.perl: Xref: feenix.metronet.com comp.lang.perl:6529 Newsgroups: comp.lang.perl Path: feenix.metronet.com!news.ecn.bgu.edu!usenet.ins.cwru.edu!howland.reston.ans.net!europa.eng.gtefsd.com!uunet!boulder!wraeththu.cs.colorado.edu!tchrist From: Tom Christiansen Subject: Re: Dynamically creating formats Message-ID: Originator: tchrist@wraeththu.cs.colorado.edu Sender: news@Colorado.EDU (USENET News System) Reply-To: tchrist@cs.colorado.edu (Tom Christiansen) Organization: University of Colorado, Boulder References: <9310051321.AA14685@eng.xyplex.com> Date: Thu, 7 Oct 1993 16:31:34 GMT Lines: 132 From the keyboard of craig@chs.xyplex.com (Craig H. Smith): : :Before I bang my head against this problem for awhile, I'd better :check if someone has done this, or even if it is possible (which :knowing perl it is :). : :I'm trying to use tricklet-3.1 along with perl to create a generic :SNMP table output tool. The problem is that the table format varies, :and therefore cannot be hard coded into a format statement to be used :with the write statement. What I would like to so is *build* the :format statement at run time as the SNMP daemon get-nexts through the :table. : :Any ideas? : :Craig #!/usr/local/bin/perl # # tgent -- retrieve a termcap entry, formatted for # this window size, or the -w width if given # read from /etc/termcap, or -f file, or stdin if isn't_atty # tchrist@cs.colorado.edu 9/17/93 ############################################ # CONFIGURATION SECTION $DEF_WIDTH = 80; $DEF_FILE = '/etc/termcap'; ############################################# main: { &parse_args; &getwinsz; &open_input; &run_filter; &gen_format; &find_entries; exit; } ############################################# sub usage { die "usage: $0 [-f tcapfile] [-w width] entry ...\n"; } ############################################# sub parse_args { require 'getopts.pl'; &Getopts("df:w:") || &usage; &usage unless @ARGV; @wanted{@ARGV} = (1) x @ARGV; } ############################################# sub getwinsz { $cols = 0; if ($opt_w) { $cols = $opt_w; return; } if (-t STDOUT) { ($rows, $cols) = split(' ', `stty size`); # still might be 0? } $cols = $DEF_WIDTH unless $cols; } ############################################# sub open_input { $FILE = do { if ($opt_f) { $opt_f } elsif (-f STDIN) { "<&STDIN" } else { $DEF_FILE } }; open FILE || die "can't open $FILE: $!"; } ############################################# sub gen_format { $cols -= 3; $format = "format STDOUT = \n"; $format .= '^' . '<' x $cols . "\n"; $format .= '$entry' . "\n"; $format .= "\t^" . "<" x ($cols-8) . "~~\n"; $format .= '$entry' . "\n"; $format .= ".\n"; print STDERR $format if $opt_d; eval $format; die "bad format:\n$format\n$@" if $@; $: = ":"; } ############################################# sub find_entries { LINE: while () { next LINE if /^#/; chop; if ( /\\$/ ) { chop; $_ .= ; redo LINE; } s/:\s*:/:/g; $entry = $_; NAME: for (split(/\|/, (split(/:/,$_,2))[0])) { if ($wanted{$_}) { write; next LINE; } } # for name } # while line } ############################################# sub run_filter { if ($pid = open(CHILD, "|-")) { open(STDOUT, ">&CHILD") || die "can't dup child to stdout: $!"; $| = 1; return; } die "can't fork: $!" unless defined $pid; $| = 1; $\ = "\n"; $saveline = ''; while () { chop; s/\s+$//; s/^\t([^:])/\t:$1/; print /^\S/ ? $saveline : "$saveline\\" if $saveline; $saveline = $_; } print $saveline if $saveline; exit; } __END__ -- Tom Christiansen tchrist@cs.colorado.edu Consultant Boulder Colorado 303-444-3212 .