From: Ian Jackson Date: Wed, 4 Sep 2013 20:40:23 +0000 (+0100) Subject: New features: X-Git-Tag: debian/4.2.1__iwj2~16 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=commitdiff_plain;h=44d9110194da21397c5b41c7595daf67708a20a3 New features: * random-word: -F option for using only first lines of source file. --- diff --git a/debian/changelog b/debian/changelog index 0fa99b3..e0dfcd2 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,5 +1,8 @@ chiark-utils (4.2.1~~iwj1) unstable; urgency=low + New features: + * random-word: -F option for using only first lines of source file. + Code cleanups: * random-word: Some perl-mode emacs formatting glitch workarounds. * random-word: use strict; -w. diff --git a/scripts/random-word b/scripts/random-word index 427e4d0..649af15 100755 --- a/scripts/random-word +++ b/scripts/random-word @@ -25,6 +25,7 @@ use POSIX; our $want= 1; our $filename= "/usr/share/dict/words"; our @randfile= ("/dev/urandom", "/dev/random"); +our $filemaxlen; sub fail ($) { die "random-word: $_[0]\n"; } open D, ">/dev/null" or fail("open /dev/null: $!"); @@ -37,6 +38,8 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) { $want= $1; } elsif (m/^\-f/ && length > 2) { $filename= $'; #'; + } elsif (m/^\-F(\d+)$/) { + $filemaxlen= $1; } elsif (m/^\-r/ && length > 2) { @randfile= ($'); #'); } elsif (m/^\-D$/) { @@ -66,7 +69,16 @@ $r->autoflush(0); our $w= new IO::File $filename, 'r'; $w or fail("cannot open $filename: $!"); debug("open $filename ok"); -our @words= <$w>; +our @words; +if (defined $filemaxlen) { + while (@words < $filemaxlen) { + my $l = <$w>; + last unless defined $l; + push @words, $l; + } +} else { + @words= <$w>; +} $w->error and fail("cannot read $filename: $!"); debug("read $filename ok");