1 | package LATMOS::Accounts::Utils; |
---|
2 | use 5.010000; |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use Exporter (); |
---|
6 | use vars qw(@ISA @EXPORT_OK @EXPORT); |
---|
7 | use utf8; |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | use File::Temp qw(tempfile); |
---|
10 | |
---|
11 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
12 | |
---|
13 | @ISA = qw(Exporter); |
---|
14 | @EXPORT = qw(to_ascii exec_command); |
---|
15 | @EXPORT_OK = qw(to_ascii exec_command); |
---|
16 | |
---|
17 | sub to_ascii { |
---|
18 | my ($text) = @_; |
---|
19 | return $text unless(defined($text)); |
---|
20 | utf8::decode($text) unless(utf8::is_utf8($text)); |
---|
21 | $text =~ s/Å/oe/g; |
---|
22 | $text =~ s/Ê/ae/g; |
---|
23 | $text =~ tr {uà âÀÃÃÃçéÚêëÃÃÃÃïîÃÃÞöÎÃÃÌûÃÃ} |
---|
24 | {uaaaAAAceeeeEEEEiiIIoooOOuuUU}; |
---|
25 | $text =~ s/([^[:ascii:]])/_/g; |
---|
26 | $text |
---|
27 | } |
---|
28 | |
---|
29 | sub exec_command { |
---|
30 | my ($command, $env) = @_; |
---|
31 | |
---|
32 | my @exec = ref $command |
---|
33 | ? (@$command) |
---|
34 | : ($command); |
---|
35 | la_log(LA_DEBUG, 'running command `%s\'', join(' ', @exec)); |
---|
36 | |
---|
37 | my $pid = fork; |
---|
38 | if (!defined($pid)) { |
---|
39 | la_log(LA_ERR, "Can't launch script: cannot fork"); |
---|
40 | } elsif ($pid) { |
---|
41 | # Father |
---|
42 | waitpid($pid, 0); |
---|
43 | if (my $exitstatus = $?) { |
---|
44 | la_log(LA_ERR, 'command %s exit with status %d', |
---|
45 | join(' ', @exec), $exitstatus); |
---|
46 | return; |
---|
47 | } else { |
---|
48 | return 1; |
---|
49 | } |
---|
50 | } else { |
---|
51 | # Child |
---|
52 | foreach (keys %{ $env || {} }) { |
---|
53 | $ENV{"LA_$_"} = $env->{$_}; |
---|
54 | } |
---|
55 | exec(@exec); |
---|
56 | exit($!); |
---|
57 | } |
---|
58 | 1 |
---|
59 | } |
---|
60 | |
---|
61 | sub parse_obj_file { |
---|
62 | my ($handle) = @_; |
---|
63 | |
---|
64 | my %attributes; |
---|
65 | while (my $line = <$handle>) { |
---|
66 | $line =~ /^#/ and next; |
---|
67 | chomp($line); |
---|
68 | my ($attr, $value) = $line =~ /^\s*(\S+):\s*(.*)\s*$/ or |
---|
69 | die "Malformed input file\n"; |
---|
70 | $value =~ s/\s*$//; |
---|
71 | $value =~ s/\\n/\n/g; |
---|
72 | if ($attributes{$attr}) { |
---|
73 | if (ref $attributes{$attr}) { |
---|
74 | push(@{ $attributes{$attr} }, $value); |
---|
75 | } else { |
---|
76 | my $temp = $attributes{$attr}; |
---|
77 | $attributes{$attr} = [ $temp, $value ]; |
---|
78 | } |
---|
79 | } else { |
---|
80 | $attributes{$attr} = $value || undef; |
---|
81 | $attr eq 'exported' && !defined $attributes{$attr} and $attributes{$attr} = 1; |
---|
82 | } |
---|
83 | } |
---|
84 | %attributes |
---|
85 | } |
---|
86 | |
---|
87 | sub dump_read_temp_file { |
---|
88 | my ($writecb, $readcb) = @_; |
---|
89 | |
---|
90 | my ($fh, $filename) = tempfile(CLEANUP => 0); |
---|
91 | $writecb->($fh) or return; |
---|
92 | $fh = undef; # closing file |
---|
93 | my $res; |
---|
94 | |
---|
95 | my @stat = stat($filename); |
---|
96 | while (1) { |
---|
97 | my $cmd = ($ENV{EDITOR} || 'vi') . " $filename"; |
---|
98 | warn "Running $cmd\n"; |
---|
99 | if (system($cmd) == -1 ) { |
---|
100 | warn "Cannot run editor $!\n"; |
---|
101 | last; |
---|
102 | } |
---|
103 | if ((stat($filename))[9] == $stat[9]) { |
---|
104 | warn "No change existing\n"; |
---|
105 | last; |
---|
106 | } |
---|
107 | |
---|
108 | open($fh, '<', $filename) or return; |
---|
109 | $res = $readcb->($fh); |
---|
110 | $fh = undef; # closing again |
---|
111 | $res < 2 and last; |
---|
112 | } |
---|
113 | unlink($filename); |
---|
114 | $res; |
---|
115 | } |
---|
116 | |
---|
117 | sub check_oid_validity { |
---|
118 | my ($name) = @_; |
---|
119 | return "leadind space" if ($name =~ /^\s/); |
---|
120 | return "trailing space" if ($name =~ /\s$/); |
---|
121 | return "containing space" if ($name =~ /\s/); |
---|
122 | |
---|
123 | return; |
---|
124 | } |
---|
125 | |
---|
126 | sub check_ug_validity { |
---|
127 | my ($name) = @_; |
---|
128 | return "first caractere must be a-z" |
---|
129 | if ($name !~ /^[a-z]/); |
---|
130 | return "must contain only a-z,0-9" |
---|
131 | if ($name !~ /^[a-z,0-9]+$/); |
---|
132 | |
---|
133 | return check_oid_validity($name); |
---|
134 | } |
---|
135 | |
---|
136 | 1; |
---|