-
Notifications
You must be signed in to change notification settings - Fork 4
/
dbk
executable file
·494 lines (400 loc) · 14.1 KB
/
dbk
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/usr/bin/env perl
#=======================================================================
# dbk
# File ID: c1080bc6-f742-11dd-acb1-000475e441b9
# Ny versjon av DBK-systemet.
#
# Character set: UTF-8
# ©opyleft 1999– Øyvind A. Holm <sunny@sunbase.org>
# License: GNU General Public License version 2 or later, see end of
# file for legal stuff.
#=======================================================================
use strict;
use warnings;
use Getopt::Long;
# use diagnostics;
use Time::Local;
use Fcntl ':flock';
$| = 1;
our $Debug = 0;
D("\$#ARGV = $#ARGV\n");
our %Opt = (
'batch' => 0,
'debug' => 0,
'force' => 0,
'help' => 0,
'insert-file' => "",
'now' => 0,
'verbose' => 0,
'version' => 0,
'zone' => 0,
);
our $progname = $0;
$progname =~ s/^.*\/(.*?)$/$1/;
our $VERSION = "0.00";
Getopt::Long::Configure("bundling");
GetOptions(
"batch|b" => \$Opt{'batch'},
"debug" => \$Opt{'debug'},
"force|f" => \$Opt{'force'},
"help|h" => \$Opt{'help'},
"insert-file|i=s" => \$Opt{'insert-file'},
"now|t" => \$Opt{'now'},
"verbose|v+" => \$Opt{'verbose'},
"version" => \$Opt{'version'},
"zone|z" => \$Opt{'zone'},
) || die("$progname: Option error. Use -h for help.\n");
$Opt{'debug'} && ($Debug = 1);
$Opt{'help'} && usage(0);
if ($Opt{'version'}) {
print_version();
exit(0);
}
my $Editor = "joe";
my $dbk_dir = "$ENV{HOME}/dbk";
my $Viewer = "lynx -assume_charset=UTF-8 -homepage=file://$dbk_dir";
my $dbk_ext = ".html";
my $dbk_lockext = ".lock";
my $dir_mode = 0770;
my $curr_time = time;
my $time_not_found = 0;
my @day_name = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
D("$0: \$Opt{'now'} = $Opt{'now'}\n");
my ($Sec, $Min, $Hour, $Day, $Mon, $Year, $Wday, $Yday, $is_dst) = localtime($curr_time);
$Year += 1900;
$Mon += 1;
D("\$curr_time: $Year-$Mon-$Day $Hour:$Min:$Sec, \$Wday = $Wday, \$Yday = $Yday, \$is_dst = $is_dst\n");
# Parameterparsing og initialisering av datovariabler {{{
my $arg_count;
my $time_str = ""; # Strengen som det vil bli leita etter og som blir satt inn.
foreach (@ARGV) {
/^\d\d:/ && (splice(@ARGV, $arg_count, 1), $time_str = $_, $Opt{'now'} = 1);
$arg_count++;
}
$Year = $ARGV[2] if $#ARGV >= 2;
$Mon = sprintf("%02u", $ARGV[1]) if $#ARGV >= 1;
$Day = sprintf("%02u", $ARGV[0]) if $#ARGV >= 0;
# Når vi nærmer oss 2030 begynner dette å bli en fixme. :)
if ($Year < 30) {
$Year += 2000;
} elsif ($Year < 100) {
$Year += 1900;
}
$Mon =~ s/^(\d)$/0$1/;
$Day =~ s/^(\d)$/0$1/;
$Hour =~ s/^(\d)$/0$1/;
$Min =~ s/^(\d)$/0$1/;
$Sec =~ s/^(\d)$/0$1/;
die("$0: $Day: Feil format på datoen\n") unless ($Day =~ /^\d\d$/);
die("$0: $Mon: Feil format på måneden\n") unless ($Mon =~ /^\d\d$/);
die("$0: $Year: Feil format på året\n") unless ($Year =~ /^\d\d\d\d$/);
my $time_utc = timegm($Sec, $Min, $Hour, $Day, $Mon-1, $Year);
my $time_sec = timelocal($Sec, $Min, $Hour, $Day, $Mon-1, $Year);
my @time_arr = localtime($time_sec);
my $week_day = $time_arr[6];
my $utc_diff = ($time_utc-$time_sec)/3600;
D("Etter ARGV: $Year-$Mon-$Day\n");
D("\$dbk_dir = $dbk_dir\n");
D("\$Opt{'insert-file'} = \"$Opt{'insert-file'}\"\n");
# }}}
# Sjekk at alle katalogene er på plass {{{
my $year_dir = "$dbk_dir/$Year";
my $dbk_file = "$year_dir/${Year}${Mon}${Day}${dbk_ext}";
my $lock_dir = "$year_dir/${Year}${Mon}${Day}${dbk_lockext}";
umask(0);
unless (-d $dbk_dir) {
mkdir($dbk_dir, $dir_mode) || die("$0: $dbk_dir: Kan ikke lage directoryen: $!\n");
}
unless (-d $year_dir) {
mkdir($year_dir, $dir_mode) || die("$0: $year_dir: Kan ikke lage directoryen: $!\n");
}
D("\$dbk_file = $dbk_file\n");
if ($Opt{'force'}) {
rmdir($lock_dir) || warn("$0: $lock_dir: Kan ikke fjerne gammel lockdir med makt: $!\n");
}
# }}}
# Locking {{{
my $print_ok = 0;
until (mkdir($lock_dir, 0777)) {
$print_ok || print(STDERR "Venter på at lockdir $lock_dir skal forsvinne ($!)..");
print(".");
sleep(1);
$print_ok = 1;
}
$print_ok && print("OK\n");
# }}}
open(CheckFP, ">$year_dir/.check") || die("$0: $year_dir/.check: Klarte ikke å lage fila: $!\n");
close(CheckFP);
unless (-f $dbk_file) {
open(FP, ">$dbk_file") || die("$0: $dbk_file: Kan ikke åpne fila for skriving: $!\n");
print(FP "<h1>$day_name[$week_day] $Year-$Mon-$Day</h1>\n") || die("$0: $dbk_file: Kan ikke skrive header til fila: $!\n");
close(FP);
}
my $min_size = 0; # Minimum size of file to prevent deletion
if ($Opt{'now'}) {
# Legg til klokkeslettet akkurat nå i fila og gå inn i editoren {{{
my $utc_str = sprintf("%s%02u00", $utc_diff < 0 ? "-" : "+", $utc_diff);
$utc_str = "" unless $Opt{'zone'};
if (length($time_str)) {
$time_str .= $utc_str;
} else {
$time_str = "$Hour:$Min:$Sec$utc_str";
}
open(dbkFP, "+<$dbk_file") || die("$0: $dbk_file: Kan ikke åpne fila for lesing+skriving+flock: $!\n");
flock(dbkFP, LOCK_EX) || die("$0: $dbk_file: Klarte ikke flock(): $!\n");
my $which_line = find_line($time_str);
if ($time_not_found) {
seek(dbkFP, 0, 2) || die("$0: $dbk_file: Klarte ikke å seeke til slutten for å legge til LF: $!\n");
print(dbkFP "\n") || warn("$0: $dbk_file: Feil under skriving til fila: $!\n");
$which_line++;
}
length($Opt{'insert-file'}) ? insert_file($Opt{'insert-file'}, $which_line) : insert_time($time_str, $which_line);
close(dbkFP);
$which_line++;
system("$Editor +$which_line $dbk_file") unless (length($Opt{'insert-file'}) || length($Opt{'batch'}));
$min_size = 49;
# }}}
} else {
$min_size = 25;
}
unless ($Opt{'batch'}) {
system("$Viewer $dbk_file");
system("clear");
}
my @stat_info = stat($dbk_file);
my $file_size = $stat_info[7];
if ($file_size < $min_size) {
print("Ingenting ble skrevet, så jeg sletter driten.\n");
unlink($dbk_file) || warn("$0: $dbk_file: Klarte ikke å slette fila: $!\n");
rmdir($year_dir);
}
rmdir($lock_dir) || die("$0: $lock_dir: Klarte ikke å slette lockdir: $!\n");
exit(0);
#### SUBRUTINER ####
sub find_line {
# {{{
my $srch_time = shift;
my $last_line = 0;
my $Found = 0;
seek(dbkFP, 0, 0) || die("$dbk_file: Klarte ikke å gå til begynnelsen av fila: $!\n");
while (<dbkFP>) {
if (/^<p><b>(\d\d:\d\d:\d\d)/i || /^<p><b>(\d\d:\d\d)/i) {
my $found_time = $1;
return($.) if ($srch_time lt $found_time);
}
}
$time_not_found = 1;
return($. + 1);
# }}}
} # find_line()
sub insert_time {
# {{{
my ($time_str, $line_num) = @_;
seek(dbkFP, 0, 0) || die("$dbk_file: Klarte ikke å gå til begynnelsen av fila i insert_time(): $!\n");
my @Linjer = <dbkFP>;
splice(@Linjer, $line_num-1, 0, sprintf("<p><b>$time_str</b> -\n%s", $time_not_found ? "" : "\n"));
seek(dbkFP, 0, 0) || die("$0: $dbk_file: Klarte ikke å seeke til starten i insert_time(): $!\n");
truncate(dbkFP, 0) || die("$0: $dbk_file: Klarte ikke å trunce fila til 0: $!\n");
foreach (@Linjer) {
print(dbkFP $_) || warn("$0: $dbk_file: Å neiiii, katastrofe! Feil under skriving til fila: $!\n");
}
# }}}
} # insert_time()
sub is_utf8 {
# Sjekker om en tekst inneholder ulovlige UTF-8-sekvenser og
# returnerer i så fall 0
# {{{
my $Str = shift;
my $Retval = 1;
# Henta fra "find_inv_utf8,v 1.1 2001/09/07 08:54:31 sunny" og
# modifisert litt. FIXME: Klager ikke på overlange sekvenser og tegn
# i UTF-16-surrogatområdet
$Str =~ s/([\xFC-\xFD][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF])//g;
$Str =~ s/([\xF8-\xFB][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF])//g;
$Str =~ s/([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])//g;
$Str =~ s/([\xE0-\xEF][\x80-\xBF][\x80-\xBF])//g;
$Str =~ s/([\xC0-\xDF][\x80-\xBF])//g;
$Retval = 0 if ($Str =~ /[\x80-\xFD]/);
D("is_utf8() returnerer $Retval.\n");
return $Retval;
# }}}
} # is_utf8()
sub insert_file {
# {{{
my ($file_name, $line_num) = @_;
local *LocFP;
seek(dbkFP, 0, 0) || die("$0: $dbk_file: Klarte ikke å gå til begynnelsen av fila i insert_file(): $!\n");
my @Linjer = <dbkFP>;
my @InsLinj = "<p><b>$time_str</b> -\n";
open(LocFP, "<$file_name") || die("$0: $file_name: Kan ikke åpne fila for lesing: $!\n");
push(@InsLinj, <LocFP>);
push(@InsLinj, "\n") unless $time_not_found; # Denne likte jeg egentlig fantastisk dårlig, men hvem bruker vel å ha to LF’er på slutten av fila?
for (@InsLinj) {
D("\$_ = \"$_\"\n");
if (!is_utf8($_)) {
warn("$0: Ugyldig UTF-8 funnet, behandler teksten som ISO-8859-1.\n");
my @Ny = ();
for my $Curr (@InsLinj) {
D("FØR : \$Curr = \"$Curr\"\n");
$Curr =~ s/([\x80-\xFF])/widechar(ord($1))/ge;
D("ETTER: \$Curr = \"$Curr\"\n");
push(@Ny, $Curr);
}
@InsLinj = @Ny;
last;
}
}
splice(@Linjer, $line_num-1, 0, @InsLinj);
close(LocFP);
seek(dbkFP, 0, 0) || die("$0: $dbk_file: Klarte ikke å seeke til starten i insert_file(): $!\n");
truncate(dbkFP, 0) || die("$0: $dbk_file: Klarte ikke å trunce fila til 0: $!\n");
D("scalar(\@Linjer): \"" . scalar(@Linjer) . "\".\n");
foreach my $Curr (@Linjer) {
$Curr =~ s/&#(\d{1,10});/widechar($1)/ge;
$Curr =~ s/&#x([0-9a-f]{1,8});/widechar(hex($1))/gei;
print(dbkFP $Curr) || warn("$0: $dbk_file: Knallkrise. Feil under skriving til fila: $!\n");
}
# }}}
} # insert_file()
sub print_version {
# Print program version {{{
print("$progname v$VERSION\n");
# }}}
} # print_version()
sub usage {
# Send the help message to stdout {{{
my $Retval = shift;
if ($Opt{'verbose'}) {
print("\n");
print_version();
}
print(<<END);
Syntax: $progname [valg] [dag [måned [år]]] [HH:MM]
Options:
-b, --batch
Batch mode, ikke start editor eller viewer.
-f, --force
Force, start dbk selv om lockdir eksisterer.
-h, --help
Show this help.
-i x, --insert-file x
Sett inn filen x istedenfor å bruke tastatur. Forventer -t eller
klokkeslett.
-t, --now
Legg til klokkeslettet akkurat nå i fila og gå inn i editoren
-v, --verbose
Increase level of verbosity. Can be repeated.
--version
Print version information.
-z, --zone
Legg til tidssone til klokkeslett
--debug
Print debugging messages.
Klokkeslettet kan spesifiseres hvor som helst, men må starte med
formatet "HH:" der HH er to siffer.
END
exit($Retval);
# }}}
} # usage()
sub msg {
# Print a status message to stderr based on verbosity level {{{
my ($verbose_level, $Txt) = @_;
if ($Opt{'verbose'} >= $verbose_level) {
print(STDERR "$progname: $Txt\n");
}
# }}}
} # msg()
sub D {
# Print a debugging message {{{
$Debug || return;
my @call_info = caller;
chomp(my $Txt = shift);
my $File = $call_info[1];
$File =~ s#\\#/#g;
$File =~ s#^.*/(.*?)$#$1#;
print(STDERR "$File:$call_info[2] $$ $Txt\n");
return("");
# }}}
} # D()
sub widechar {
# Konverterer en tegnverdi til UTF-8 {{{
# Henta fra "h2u,v 1.5 2002/11/20 00:09:40 sunny".
my $Val = shift;
my $allow_invalid = 0;
if ($Val < 0x80) {
return sprintf("%c", $Val);
} elsif ($Val < 0x800) {
return sprintf("%c%c", 0xC0 | ($Val >> 6),
0x80 | ($Val & 0x3F));
} elsif ($Val < 0x10000) {
unless ($allow_invalid) {
if (($Val >= 0xD800 && $Val <= 0xDFFF) || ($Val eq 0xFFFE) || ($Val eq 0xFFFF)) {
$Val = 0xFFFD;
}
}
return sprintf("%c%c%c", 0xE0 | ($Val >> 12),
0x80 | (($Val >> 6) & 0x3F),
0x80 | ($Val & 0x3F));
} elsif ($Val < 0x200000) {
return sprintf("%c%c%c%c", 0xF0 | ($Val >> 18),
0x80 | (($Val >> 12) & 0x3F),
0x80 | (($Val >> 6) & 0x3F),
0x80 | ($Val & 0x3F));
} elsif ($Val < 0x4000000) {
return sprintf("%c%c%c%c%c", 0xF8 | ($Val >> 24),
0x80 | (($Val >> 18) & 0x3F),
0x80 | (($Val >> 12) & 0x3F),
0x80 | (($Val >> 6) & 0x3F),
0x80 | ( $Val & 0x3F));
} elsif ($Val < 0x80000000) {
return sprintf("%c%c%c%c%c%c", 0xFC | ($Val >> 30),
0x80 | (($Val >> 24) & 0x3F),
0x80 | (($Val >> 18) & 0x3F),
0x80 | (($Val >> 12) & 0x3F),
0x80 | (($Val >> 6) & 0x3F),
0x80 | ( $Val & 0x3F));
} else {
return widechar(0xFFFD);
}
# }}}
} # widechar()
__END__
# Plain Old Documentation (POD) {{{
=pod
=head1 NAME
=head1 SYNOPSIS
[options] [file [files [...]]]
=head1 DESCRIPTION
=head1 OPTIONS
=over 4
=item B<-h>, B<--help>
Print a brief help summary.
=item B<-v>, B<--verbose>
Increase level of verbosity. Can be repeated.
=item B<--version>
Print version information.
=item B<--debug>
Print debugging messages.
=back
=head1 BUGS
=head1 AUTHOR
Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
=head1 COPYRIGHT
Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
This is free software; see the file F<COPYING> for legalese stuff.
=head1 LICENCE
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, see L<http://www.gnu.org/licenses/>.
=head1 SEE ALSO
=cut
# }}}
# vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :