bug dans ctresolve

This commit is contained in:
Jephté Clain 2014-08-20 00:12:36 +04:00
parent 03797171f8
commit 1fe49ae1f9
2 changed files with 71 additions and 31 deletions

View File

@ -1 +1 @@
001001001
001002000

View File

@ -94,6 +94,7 @@ function enable_in_crontab() {
function ctnow() {
date +"%-M %-H %-d %-m %u"
}
__CTRESOLVE_CTNOW=""
# Analyser STDIN qui contient des lignes au format crontab, et afficher des
# commandes pour les lignes correspondant à la date courante.
# Les commandes sont de la forme "export var=value" pour la définition d'une
@ -106,9 +107,13 @@ function ctnow() {
# eval "$(ctresolve <crontab)"
function ctresolve() {
local -a ctnow
if [ -n "$__CTRESOLVE_CTNOW" ]; then
eval "ctnow=($__CTRESOLVE_CTNOW)"
else
eval "ctnow=($(ctnow))"
fi
filter_conf | awkrun -f ctnow[@] '
function ctmatch(ctval, ref, parts, part, i, j, start, end, step, ok) {
function ctmatch_one(ctval, ref, parts, part, i, j, start, end, step, ok) {
ok = 0
split(ctval, parts, /,/)
for(i = 1; i <= length(parts); i++) {
@ -130,28 +135,56 @@ function ctmatch(ctval, ref, parts, part, i, j, start, end, step, o
end = start
}
# tester si ça matche
#print "start=" start ", end=" end ", step=" step", ref=" ref #DEBUG
for (j = start; j <= end; j += step) {
if (j == ref) {
ok = 1
break
}
}
#print " step: is " ref " in [" start "-" end "/" step "] ? " (ok? "yes": "no") #DEBUG
if (ok) break
}
#print "final: is " ref " ~ " ctval " ? " (ok? "yes": "no") #DEBUG
return ok
}
function ctmatch_all(M, H, dom, mon, dow, refM, refH, refdom, refmon, refdow, Mok, Hok, domok, monok, dowok) {
gsub(/\*/, "0-59", M)
Mok = ctmatch_one(M, refM)
gsub(/\*/, "0-23", H)
Hok = ctmatch_one(H, refH)
gsub(/\*/, "1-31", dom)
domok = ctmatch_one(dom, refdom)
gsub(/\*/, "1-12", mon)
monok = ctmatch_one(mon, refmon)
gsub(/\*/, "1-7", dow)
dowok = ctmatch_one(dow, refdow)
return Mok && Hok && monok && (domok || dowok)
}
function print_cmd(cmd) {
print "__ctexec " quote_value(cmd)
}
BEGIN {
refM = ctnow[1]; refH = ctnow[2]; refdom = ctnow[3]; refmon = ctnow[4]; refdow = ctnow[5]
ctref = refM " " refH " " refdom " " refmon " " refdow
print "## now: " ctref
match_ctline = 0
# est-on dans une série de ctline?
ctline_run = 0
# dans une série de ctline, il y a-t-il au moins un match?
ctline_run_one_match = 0
# faut-il afficher les commandes indentées?
match_indented = 0
}
/^(export[ \t]+)?[a-zA-Z_][a-zA-Z0-9_]*[-+%#?]=/ {
ctline_run = 0; ctline_run_one_match = 0; match_indented = 0
# manipulation de variables de type PATH
match($0, /^(export[ \t]+)?([a-zA-Z_][a-zA-Z0-9_]*)([-+%#?])=(.*)$/, parts)
name = parts[2]
@ -170,12 +203,16 @@ BEGIN {
next
}
/^(export[ \t]+)?[a-zA-Z_][a-zA-Z0-9_]*=/ {
ctline_run = 0; ctline_run_one_match = 0; match_indented = 0
# spécification de variable
sub(/^export[ \t]+/, "", $0)
print "export " $0
next
}
/^\$.+/ {
ctline_run = 0; ctline_run_one_match = 0; match_indented = 0
# exécution de commande arbitraire
if ($0 ~ /^\$\{([ \t]*(#.*)?)?$/) {
# commande sur plusieurs lignes
@ -191,47 +228,50 @@ BEGIN {
}
next
}
/^[ \t]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+/ {
# ligne de planification
M = $1; H = $2; dom = $3; mon = $4; dow = $5
sub(/^[ ]*[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]*/, "", $0)
if (!match_ctline) match_indented = 0
/^[ \t]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+/ {
match_indented = 0
M = $1; H = $2; dom = $3; mon = $4; dow = $5
ctline = M " " H " " dom " " mon " " dow
gsub(/\*/, "0-59", M)
Mok = ctmatch(M, refM)
sub(/^[ \t]*[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]*/, "", $0)
cmd = $0
gsub(/\*/, "0-23", H)
Hok = ctmatch(H, refH)
match_cur = ctmatch_all(M, H, dom, mon, dow, refM, refH, refdom, refmon, refdow)
if (match_cur) print "## match: " ctline
gsub(/\*/, "1-31", dom)
domok = ctmatch(dom, refdom)
if (cmd == "") {
ctline_run = 1
if (match_cur) ctline_run_one_match = 1
if (ctline_run_one_match) match_indented = 1
gsub(/\*/, "1-12", mon)
monok = ctmatch(mon, refmon)
gsub(/\*/, "1-7", dow)
dowok = ctmatch(dow, refdow)
if (Mok && Hok && monok && (domok || dowok)) {
print "##matches: " ctline
match_ctline = 1
} else {
if (ctline_run && match_cur) ctline_run_one_match = 1
if (ctline_run && ctline_run_one_match) {
print_cmd(cmd)
match_indented = 1
}
if (match_ctline && $0 != "") {
print "__ctexec " quote_value($0)
match_ctline = 0
if (!ctline_run && match_cur) {
print_cmd(cmd)
match_indented = 1
}
ctline_run = 0
ctline_run_one_match = 0
}
next
}
/^[ \t]+/ {
ctline_run = 0; ctline_run_one_match = 0
if (match_indented) {
# il faut matcher les commandes indentees
sub(/^[ \t]+/, "", $0)
print "__ctexec " quote_value($0)
print_cmd($0)
}
next
}
{