Git/suuid/: New commits
[sunny256-utils.git] / gpath
blob1170d99c7b59a15ee77199f4869d73113d1cccc5
1 #!/usr/bin/env perl
3 #=======================================================================
4 # gpath
5 # File ID: acd2d3a2-4610-11e4-9bb4-c80aa9e67bbd
7 # Create a directed graph for Graphviz by reading plain text lines from
8 # files or stdin, each line is a node label. Adjacent lines are
9 # connected in the graph, an empty line splits the graph.
11 # Character set: UTF-8
12 # ©opyleft 2014– Øyvind A. Holm <sunny@sunbase.org>
13 # License: GNU General Public License version 2 or later, see end of
14 # file for legal stuff.
15 #=======================================================================
17 use strict;
18 use warnings;
19 use Getopt::Long;
21 local $| = 1;
23 our $Debug = 0;
25 our %Opt = (
27 'count' => 1,
28 'debug' => 0,
29 'help' => 0,
30 'verbose' => 0,
31 'version' => 0,
35 our $progname = $0;
36 $progname =~ s/^.*\/(.*?)$/$1/;
37 our $VERSION = '0.00';
39 Getopt::Long::Configure('bundling');
40 GetOptions(
42 'count|c=i' => \$Opt{'count'},
43 'debug' => \$Opt{'debug'},
44 'help|h' => \$Opt{'help'},
45 'verbose|v+' => \$Opt{'verbose'},
46 'version' => \$Opt{'version'},
48 ) || die("$progname: Option error. Use -h for help.\n");
50 $Opt{'debug'} && ($Debug = 1);
51 $Opt{'help'} && usage(0);
52 if ($Opt{'version'}) {
53 print_version();
54 exit(0);
56 $Opt{'count'} < 1 && die("$progname: -c/--count argument must be higher than zero\n");
58 exit(main(%Opt));
60 sub main {
61 # {{{
62 my %Opt = @_;
63 my $Retval = 0;
64 my ($curr, $prev) = ('', '');
65 my %used = ();
67 print("digraph g {\n");
68 while (my $line = <>) {
69 $line =~ s/"/\\"/g;
70 $line =~ s/^\s+//;
71 $line =~ s/\s+$//;
72 if ($line =~ /^(.+)$/) {
73 $curr = $1;
74 if (length($prev) && $prev ne $curr) {
75 $used{"$prev $curr"}++;
76 ($used{"$prev $curr"} == $Opt{'count'}) && (
77 print("\"$prev\" -> \"$curr\";\n")
80 $prev = $curr;
81 } else {
82 $prev = '';
85 print("}\n");
86 return($Retval);
87 # }}}
88 } # main()
90 sub print_version {
91 # Print program version {{{
92 print("$progname v$VERSION\n");
93 return;
94 # }}}
95 } # print_version()
97 sub usage {
98 # Send the help message to stdout {{{
99 my $Retval = shift;
101 if ($Opt{'verbose'}) {
102 print("\n");
103 print_version();
105 print(<<"END");
107 Usage: $progname [options] [file [files [...]]]
109 Create a directed graph for Graphviz by reading plain text lines from
110 files or stdin, each line is a node label. Adjacent lines are connected
111 in the graph, an empty line splits the graph.
113 Options:
115 -c X, --count X
116 Only display connections that have been repeated at least X times.
117 -h, --help
118 Show this help.
119 -v, --verbose
120 Increase level of verbosity. Can be repeated.
121 --version
122 Print version information.
123 --debug
124 Print debugging messages.
127 exit($Retval);
128 # }}}
129 } # usage()
131 sub msg {
132 # Print a status message to stderr based on verbosity level {{{
133 my ($verbose_level, $Txt) = @_;
135 if ($Opt{'verbose'} >= $verbose_level) {
136 print(STDERR "$progname: $Txt\n");
138 return;
139 # }}}
140 } # msg()
142 sub D {
143 # Print a debugging message {{{
144 $Debug || return;
145 my @call_info = caller;
146 chomp(my $Txt = shift);
147 my $File = $call_info[1];
148 $File =~ s#\\#/#g;
149 $File =~ s#^.*/(.*?)$#$1#;
150 print(STDERR "$File:$call_info[2] $$ $Txt\n");
151 return('');
152 # }}}
153 } # D()
155 __END__
157 # Plain Old Documentation (POD) {{{
159 =pod
161 =head1 NAME
165 =head1 SYNOPSIS
167 [options] [file [files [...]]]
169 =head1 DESCRIPTION
173 =head1 OPTIONS
175 =over 4
177 =item B<-h>, B<--help>
179 Print a brief help summary.
181 =item B<-v>, B<--verbose>
183 Increase level of verbosity. Can be repeated.
185 =item B<--version>
187 Print version information.
189 =item B<--debug>
191 Print debugging messages.
193 =back
195 =head1 BUGS
199 =head1 AUTHOR
201 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
203 =head1 COPYRIGHT
205 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
206 This is free software; see the file F<COPYING> for legalese stuff.
208 =head1 LICENCE
210 This program is free software: you can redistribute it and/or modify it
211 under the terms of the GNU General Public License as published by the
212 Free Software Foundation, either version 2 of the License, or (at your
213 option) any later version.
215 This program is distributed in the hope that it will be useful, but
216 WITHOUT ANY WARRANTY; without even the implied warranty of
217 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
218 See the GNU General Public License for more details.
220 You should have received a copy of the GNU General Public License along
221 with this program.
222 If not, see L<http://www.gnu.org/licenses/>.
224 =head1 SEE ALSO
226 =cut
228 # }}}
230 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :