-
Notifications
You must be signed in to change notification settings - Fork 33
/
fstab_beautifier.pl
executable file
·107 lines (86 loc) · 3.57 KB
/
fstab_beautifier.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 21 March 2014
# https://trizenx.blogspot.com
# Realign the columns of a space-delimited file (with support for comments and empty lines)
use 5.010;
use strict;
use warnings;
sub fstab_beautifier {
my ($fh, $code) = @_;
my @data;
while (defined(my $line = <$fh>)) {
if ($line =~ /^#/) { # it's a comment
push @data, {comment => $line};
}
elsif (not $line =~ /\S/) { # it's an empty line
push @data, {empty => ""};
}
else { # hopefully, it's a line with columns
push @data, {fields => [split(' ', $line)]};
}
}
# Indicate the EOF (this is used to flush the buffer)
push @data, {eof => 1};
# Store the columns and the width of each column
my @buffer;
my @widths;
for (my $i = 0 ; $i <= $#data ; $i++) {
my $line = $data[$i];
if (exists $line->{fields}) { # it's a line with columns
# Collect the maximum width of each column
while (my ($i, $item) = each @{$line->{fields}}) {
if ((my $len = length($item)) > ($widths[$i] //= 0)) {
$widths[$i] = $len;
}
}
# Store the line in the buffer
# and continue looping to the next line
push @buffer, $line->{fields};
next;
}
elsif (exists $line->{comment}) { # it's a comment
$code->(unpack("A*", $line->{comment}));
}
if (@buffer) { # buffer is not empty
# Create the format for 'sprintf'
my $format = join("\t", map { "%-${_}s" } splice(@widths));
# For each line of the buffer, format it and send it further
while (defined(my $line = shift @buffer)) {
$code->(unpack("A*", sprintf($format, @{$line})));
}
}
if (exists $line->{empty}) { # empty line
$code->($line->{empty});
}
}
}
my $fh = @ARGV
? do {
open my $fh, '<', $ARGV[0]
or die "Can't open file `$ARGV[0]' for reading: $!";
$fh;
}
: \*DATA;
# Call the function with a FileHandle and CODE
fstab_beautifier($fh, sub { say $_[0] });
__END__
# My system partitions
/dev/sda7 swap swap defaults 0 0
/dev/sda1 / ext3 defaults 1 1
/dev/sda2 /home ext3 defaults 1 2
# My /mnt partitions
/dev/sr0 /mnt/dvd_sr0 auto noauto,user,ro 0 0
/dev/sr1 /mnt/dvd_sr1 auto noauto,user,ro 0 0
/dev/fd0 /mnt/floppy auto rw,noauto,user,sync 0 0
/dev/sdd4 /mnt/zip vfat rw,noauto,user,sync 0 0
/dev/sde1 /mnt/usb auto rw,noauto,user,sync 0 0
# My /home/vtel57/ partitions
/dev/sda8 /home/vtel57/vtel57_archives ext2 defaults 0 2
/dev/sdc1 /home/vtel57/vtel57_backups ext2 defaults 0 2
/dev/sdc7 /home/vtel57/vtel57_common vfat rw,gid=users,uid=vtel57 0 0
# My /dev partitions
devpts /dev/pts devpts gid=5,mode=620 0 0
proc /proc proc defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0