annotate sort-header @ 2:616efa22d193 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/text_processing/text_processing commit 369e40078146d00608d52205bb8cee66ae735b76-dirty
author bgruening
date Tue, 30 Jun 2015 17:47:23 -0400
parents 5314e5d6f040
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
1 #!/usr/bin/env perl
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
2 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
3 ## Sort-header - wrapper for GNU sort with header-line support
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
4 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
5 ## Copyright(C) A. Gordon
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
6 ## license AGPLv3+
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
7 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
8 use strict;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
9 use warnings;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
10 use Data::Dumper;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
11 use IO::Handle;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
12 use Getopt::Long qw(:config bundling no_ignore_case_always);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
13
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
14 ## Forward declarations
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
15 sub add_standard_sort_param(@);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
16 sub add_standard_sort_param_value(@);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
17 sub forbidden_sort_param(@);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
18 sub show_help();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
19 sub show_version();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
20 sub show_examples();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
21 sub parse_commandline_options();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
22 sub reassign_input_output();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
23 sub process_header_lines();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
24 sub run_sort();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
25 sub read_line_non_buffered();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
26
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
27
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
28 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
29 ## Runtime options
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
30 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
31 my $PROGRAM="sort-header";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
32 my $VERSION=0.4;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
33
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
34 my $check_only=undef;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
35 my $input_file=undef;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
36 my $output_file=undef;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
37 my $field_separator=undef;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
38 my $header_lines =1 ;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
39 my $debug=undef;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
40 my $sort_exit_code=1; #by default, assume some error
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
41
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
42 my @sort_options;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
43
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
44 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
45 ## Program Start
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
46 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
47 parse_commandline_options();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
48 reassign_input_output();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
49 process_header_lines();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
50 run_sort();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
51 exit($sort_exit_code);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
52 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
53 ## Program End
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
54 ##
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
55
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
56 sub show_examples()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
57 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
58 print<<EOF;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
59 Sorting a file with a header line:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
60
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
61 \$ cat input.txt
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
62 Fruit Color Price
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
63 Banana Yellow 4.1
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
64 Avocado Green 8.0
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
65 Apple Red 3.0
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
66 Melon Green 6.1
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
67
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
68 # By default, 'sort-header' assumes 1 header line
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
69 # (no need to use --header in this case).
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
70
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
71 \$ sort-header -k3,3nr input.txt
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
72 Fruit Color Price
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
73 Avocado Green 8.0
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
74 Melon Green 6.1
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
75 Banana Yellow 4.1
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
76 Apple Red 3.0
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
77
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
78 EOF
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
79 exit(0);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
80 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
81
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
82 sub show_help()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
83 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
84 print<<EOF;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
85 ${PROGRAM}: Wrapper for GNU sort, allowing sorting files with header lines.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
86
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
87 Usage: $PROGRAM [HEADER-OPTIONS] [GNU sort Options] [INPUT-FILE]
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
88
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
89 HEADER-OPTIONS: the following options are supported by '${PROGRAM}':
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
90
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
91 --header N = Treat the first N lines as header lines.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
92 These line will NOT be sorted. They will be passed
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
93 directly to the output file. (default: 1)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
94
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
95 --version = Print ${PROGRAM}'s version.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
96
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
97 --debugheader = Print debug messages (relating to ${PROGRAM}'s operation).
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
98
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
99 --help = Show this help screen.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
100
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
101 --examples = Show usage examples.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
102
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
103 GNU sort options:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
104 Most of the standard GNU sort options are supported and passed to GNU sort.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
105 The following options can not be used with '${PROGRAM}':
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
106
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
107 -m --merge => ${PROGRAM} can only sort one file, not merge multiple files.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
108 -c -C --check => Currently not supported
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
109 --files0-from => Currently not supported
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
110 -z --zero-terminated => Currently not supported
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
111
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
112 INPUT-FILE:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
113 If INPUT-FILE is not specified, $PROGRAM will use STDIN (just like GNU sort).
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
114
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
115 EOF
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
116 exit(0);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
117 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
118
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
119 sub show_version()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
120 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
121 print<<EOF;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
122 $PROGRAM $VERSION
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
123 Copyright (C) 2010 A. Gordon (gordon\@cshl.edu)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
124 License AGPLv3+: Affero GPL version 3 or later (http://www.gnu.org/licenses/agpl.html)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
125
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
126 To see the GNU's sort version, run:
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
127 sort --version
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
128 EOF
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
129 exit(0);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
130 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
131
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
132 sub parse_commandline_options()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
133 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
134 my $rc = GetOptions(
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
135 "ignore-leading-blanks|b" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
136 "dictionary-order|d" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
137 "ignore-case|f" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
138 "general-numeric-sort|g" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
139 "ignore-nonprinting|i" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
140 "month-sort|M" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
141 "human-numeric-sort|h" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
142 "numeric-sort|n" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
143 "random-source=s" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
144 "random-sort|R" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
145 "reverse|r" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
146 "sort=s" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
147 "version-sort|V" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
148
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
149 "check|c" => \&forbidden_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
150 "C" => \&forbidden_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
151 "compress-program=s" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
152 "debug" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
153
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
154 "files0-from=s" => \&forbidden_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
155
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
156 "key|k=s" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
157 "merge|m" => \&forbidden_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
158 "batch-size=i" => \&forbidden_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
159
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
160 "parallel=i" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
161
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
162 "output|o=s" => \$output_file,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
163
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
164 "stable|s" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
165 "buffer-size|S=s" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
166
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
167 "field-separator|t=s" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
168 "temporary-directory|T=s" => \&add_standard_sort_param_value,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
169 "unique|u" => \&add_standard_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
170
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
171 "zero-terminated|z" => \&forbidden_sort_param,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
172
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
173 "help" => \&show_help,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
174 "version" => \&show_version,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
175 "examples" => \&show_examples,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
176
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
177 "header=i" => \$header_lines,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
178 "debugheader" => \$debug,
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
179 );
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
180
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
181 exit 1 unless $rc;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
182
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
183 my @INPUT_FILES = @ARGV;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
184
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
185 die "$PROGRAM: error: invalid number of header lines ($header_lines)\n" unless $header_lines>=0;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
186 die "$PROGRAM: error: Multiple input files specified. This program can sort only a signle file.\n" if (scalar(@INPUT_FILES)>1);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
187 $input_file = shift @INPUT_FILES if scalar(@INPUT_FILES)==1;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
188
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
189 if ($debug) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
190 warn "$PROGRAM: number of header lines = $header_lines\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
191 warn "$PROGRAM: PASS-to-Sort options:\n", Dumper(\@sort_options), "\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
192 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
193 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
194
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
195 sub reassign_input_output()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
196 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
197 if ($output_file) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
198 warn "$PROGRAM: Re-assigning STDOUT to '$output_file'\n" if $debug;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
199 open OUTPUT, '>', $output_file or die "$PROGRAM: Error: failed to create output file '$output_file': $!\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
200 STDOUT->fdopen(\*OUTPUT, 'w') or die "$PROGRAM: Error: failed to reassign STDOUT to '$output_file': $!\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
201 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
202
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
203
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
204 if ($input_file) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
205 warn "$PROGRAM: Re-assigning STDIN to '$input_file'\n" if $debug;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
206 open INPUT, '<', $input_file or die "$PROGRAM: Error: failed to open input file '$input_file': $!\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
207 STDIN->fdopen(\*INPUT, 'r') or die "$PROGRAM: Error: failed to reassign STDIN to '$input_file': $!\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
208 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
209 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
210
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
211 sub process_header_lines()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
212 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
213 warn "$PROGRAM: Reading $header_lines header lines...\n" if $debug;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
214 for (my $i=0; $i<$header_lines; $i++) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
215 my $line = read_line_non_buffered();
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
216 exit unless defined $line;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
217 print $line;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
218 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
219 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
220
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
221 sub run_sort()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
222 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
223 warn "$PROGRAM: Running GNU sort...\n" if $debug;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
224 system('sort', @sort_options);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
225 if ($? == -1) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
226 die "$PROGRAM: Error: failed to execute 'sort': $!\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
227 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
228 elsif ($? & 127) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
229 my $signal = ($? & 127);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
230 kill 2, $$ if $signal == 2; ##if sort was interrupted (CTRL-C) - just pass it on and commit suicide
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
231 die "$PROGRAM: Error: 'sort' child-process died with signal $signal\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
232 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
233 else {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
234 $sort_exit_code = ($? >> 8);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
235 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
236 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
237
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
238
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
239 sub add_standard_sort_param(@)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
240 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
241 my ($obj)= @_;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
242 add_standard_sort_param_value($obj, undef);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
243 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
244
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
245 sub add_standard_sort_param_value(@)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
246 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
247 my ($obj,$value)= @_;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
248
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
249 my $option = "" . $obj ; #stringify the optino object, get the option name.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
250
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
251 if (length($option)==1) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
252 $option = "-" . $option ;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
253 } else {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
254 $option = "--" . $option ;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
255 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
256 push @sort_options, $option ;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
257 push @sort_options, $value if $value;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
258 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
259
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
260 sub forbidden_sort_param(@)
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
261 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
262 my ($obj,$value)= @_;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
263 my $option = "" . $obj ; #stringify the optino object, get the option name.
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
264
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
265 die "$PROGRAM: Error: option '$option' can not be used with this program. If you must use it, run GNU sort directly. see --help for more details.\n";
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
266 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
267
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
268 sub read_line_non_buffered()
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
269 {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
270 my $line = '';
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
271 while ( 1 ) {
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
272 my $c;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
273 my $rc = sysread STDIN, $c, 1;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
274 die "$PROGRAM: STDIN Read error: $!" unless defined $rc;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
275 return $line if $rc==0 && $line;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
276 return undef if $rc==0 && (!$line);
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
277 $line .= $c ;
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
278 return $line if ( $c eq "\n");
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
279 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
280 }
5314e5d6f040 Imported from capsule None
bgruening
parents:
diff changeset
281