File: Util.pm

package info (click to toggle)
wml 2.32.0~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 1,812 kB
  • sloc: perl: 6,963; ansic: 747; yacc: 154; makefile: 107; sh: 25
file content (257 lines) | stat: -rw-r--r-- 6,581 bytes parent folder | download | duplicates (3)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
##
##  WML -- Website META Language
##
##  Copyright (c) 1996-2001 Ralf S. Engelschall.
##  Copyright (c) 1999-2001 Denis Barbier.
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to
##
##      Free Software Foundation, Inc.
##      59 Temple Place - Suite 330
##      Boston, MA  02111-1307, USA
##
##  Notice, that ``free software'' addresses the fact that this program
##  is __distributed__ under the term of the GNU General Public License
##  and because of this, it can be redistributed and modified under the
##  conditions of this license, but the software remains __copyrighted__
##  by the author. Don't intermix this with the general meaning of
##  Public Domain software or such a derivated distribution label.
##
##  The author reserves the right to distribute following releases of
##  this program under different conditions or license agreements.
##

package TheWML::Frontends::Wml::Util;

use strict;
use warnings;
use Cwd ();

use parent 'Exporter';

our @EXPORT_OK = qw/ _my_cwd canon_path canonize_path ctime
    error expandrange gmt_ctime gmt_isotime
    isotime make_shell_safe quotearg split_argv time_record usage user_record /;

sub expandrange
{
    my ( $s, $e ) = @_;
    return join '', ( $s .. $e );
}

sub _my_cwd
{
    my $cwd = Cwd::cwd;
    $cwd =~ s#/\z##;
    return $cwd;
}

sub ctime
{
    my ($time) = @_;
    return scalar( localtime($time) );
}

sub isotime
{
    my ($time) = @_;

    my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
        localtime($time);
    return sprintf(
        "%04d-%02d-%02d %02d:%02d:%02d",
        $year + 1900,
        $mon + 1, $mday, $hour, $min, $sec
    );
}

sub gmt_ctime
{
    my ($time) = @_;
    return scalar( gmtime($time) );
}

sub gmt_isotime
{
    my ($time) = @_;

    my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
        gmtime($time);
    return sprintf(
        "%04d-%02d-%02d %02d:%02d:%02d",
        $year + 1900,
        $mon + 1, $mday, $hour, $min, $sec
    );
}

sub time_record
{
    my ($time) = @_;
    return +{
        ctime       => ctime($time),
        gmt_ctime   => gmt_ctime($time),
        gmt_isotime => gmt_isotime($time),
        isotime     => isotime($time),
        time        => $time,
    };
}

sub make_shell_safe
{
    my ($str) = @_;

    $$str =~ s|[\'\$\`\"]||g;    # make safe for shell interpolation

    return;
}

sub user_record
{
    my ($uid) = @_;

    my @pwinfo   = getpwuid($uid);
    my $username = $pwinfo[0] || 'UNKNOWN-USERNAME';
    make_shell_safe( \$username );
    my $realname = $pwinfo[6] || 'UNKNOWN-REALNAME';
    $realname =~ s|^([^\,]+)\,.*$|$1|;
    make_shell_safe( \$realname );

    return { username => $username, realname => $realname };
}

sub usage
{
    my ($progname) = @_;

    print STDERR <<"EOF";
Usage: $progname [options] [inputfile]

Input Options:
  -I, --include=PATH      adds an include directory
  -i, --includefile=PATH  pre-include a particular include file
  -D, --define=NAME[=STR] define a variable
  -D, --define=NAME~PATH  define an auto-adjusted path variable
  -n, --noshebang         no shebang-line parsing (usually used by WMk)
  -r, --norcfile          no .wmlrc files are read
  -c, --nocd              read .wmlrc files without changing to input file directory

Output Options:
  -O, --optimize=NUM      specify the output optimization level
  -o, --outputfile=PATH   specify the output file(s)
  -P, --prolog=PATH       specify one or more prolog filters
  -E, --epilog=PATH       specify one or more epilog filters
  -t, --settime           sets mtime of outputfile(s) to mtime+1 of inputfile

Processing Options:
  -M, --depend[=OPTIONS]  dump dependencies as gcc does
  -p, --pass=STR          specify which passed should be run
  -W, --passoption[=PASS,OPTIONS]
                          set options for a specific pass
  -s, --safe              don't use precompile/inline hacks to speedup processing
  -v, --verbose[=NUM]     verbose mode
  -q, --quiet             quiet mode

Giving Feedback:
  -V, --version[=NUM]     display version and build information
  -h, --help              display this usage summary

EOF
    exit(1);
}

sub error
{
    my ($str) = @_;
    print STDERR "** WML:Error: $str\n";
    exit(1);
}

#   escape options if not quoted but
#   when shell metachars exists
sub quotearg
{
    my ($arg) = @_;
    if ( not( $arg =~ /\A'/ and $arg =~ /'\z/ ) )
    {
        $arg =~ s#([\$"`])#\\$1#gs;
    }
    return $arg;
}

#   helper function to split argument line
#   the same way Bourne-Shell does:
#   #1: foo=bar quux   => "foo=bar", "quux"
#   #2: "foo=bar quux" => "foo=bar quux"
#   #3: foo="bar quux" => "foo=bar quux"     <-- !!
sub split_argv
{
    my ($str) = @_;
    my @argv;
    my $r    = '';
    my $prev = '';

SPLIT_ARGV: while (1)
    {
        $prev = $str;
        if (   $str =~ s#\A"([^"\\]*(?:\\.[^"\\]*)*)"#$r .= $1;''#e
            or $str =~ s#\A'([^'\\]*(?:\\.[^'\\]*)*)'#$r .= $1;''#e
            or $str =~ s#\A([^\s"']+)#$r .= $1;''#e )
        {
            next SPLIT_ARGV;
        }
        if ( $str =~ /\A[\s\n]/ or $str eq '' )
        {
            if ( $r ne '' )
            {
                push( @argv, $r );
                $r = '';
            }
            $str =~ s#\A[\s\n]+##;
            last if ( $str eq '' );
        }
        if ( $str eq $prev )
        {
            #    breaks an infinite loop
            print STDERR "** WML:Error: options can not be correctly parsed\n";
            exit(1);
        }
    }
    return @argv;
}

sub canon_path
{
    my ($path) = @_;

    my $pathL = '';
    while ( $path ne $pathL )
    {
        $pathL = $path;
        $path =~ s|//|/|g;
        $path =~ s|/\./|/|g;
        $path =~ s|/\.$|/|g;
        $path =~ s|^\./(.)|$1|g;
        $path =~ s|([^/.][^/.]*)/\.\.||;
    }
    return $path;
}

sub canonize_path
{
    my ( $path, $reldir ) = @_;
    $$path = canon_path( ( $$path eq '.' ) ? "$reldir" : "$reldir/$$path" );
    return;
}

1;