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
|
/*
* dpkg - main program for package management
* trigdeferred.l - parsing of triggers/Deferred
*
* Copyright (C) 2007 Canonical Ltd
* written by Ian Jackson <ian@chiark.greenend.org.uk>
*
* This 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,
* or (at your option) any later version.
*
* This 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 dpkg; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
%option prefix="trigdef_yy"
/* Reset the name to the default value (instead of using "trigdeferred.c")
* so that automake (ylwrap) can find it. */
%option outfile="lex.yy.c"
%option noyywrap
%option batch
%option nodefault
%option perf-report
%option warn
%option nounput
%x midline
%{
#include <config.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <dpkg.h>
#include <dpkg-db.h>
static struct varbuf fn, newfn;
%}
%%
[ \t\n] /* whitespace */
#.*\n /* comments */
[\x21-\x7e]+ {
trigdef->trig_begin(trigdef_yytext);
BEGIN(midline);
}
<midline>[ \t] /* whitespace */
<midline>[-0-9a-z][-+.0-9a-z]* {
if (trigdef_yytext[0] == '-' && trigdef_yytext[1])
ohshit(_("invalid package name `%.250s' in triggers deferred "
"file `%.250s'"), trigdef_yytext, fn.buf);
trigdef->package(trigdef_yytext);
}
<midline>\n|#.*\n {
trigdef->trig_end();
BEGIN(0);
}
<midline><<EOF>> {
ohshit(_("truncated triggers deferred file `%.250s'"), fn.buf);
}
<*>. {
ohshit(_("syntax error in triggers deferred file `%.250s' at "
"character `%s'%s"),
fn.buf, yytext, YY_START == midline ? " midline": "");
}
%%
/*---------- Deferred file handling ----------*/
static int lock_fd = -1;
static FILE *old_deferred;
FILE *trig_new_deferred;
const struct trigdefmeths *trigdef;
static void
constructfn(struct varbuf *vb, const char *admindir, const char *tail)
{
varbufreset(vb);
varbufaddstr(vb, admindir);
varbufaddstr(vb, "/" TRIGGERSDIR);
varbufaddstr(vb, tail);
varbufaddc(vb, 0);
}
int
trigdef_update_start(enum trigdef_updateflags uf, const char *admindir)
{
struct stat stab;
int r;
if (uf & tduf_write) {
constructfn(&fn, admindir, TRIGGERSLOCKFILE);
if (lock_fd == -1) {
lock_fd = open(fn.buf, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (lock_fd == -1) {
if (!(errno == ENOENT && (uf & tduf_nolockok)))
ohshite(_("unable to open/create "
"triggers lockfile `%.250s'"),
fn.buf);
return -1;
}
}
lock_file(&lock_fd, fn.buf, _("unable to lock triggers area"),
NULL);
} else {
/* Dummy for pop_cleanups. */
push_cleanup(NULL, 0, NULL, 0, 0);
}
constructfn(&fn, admindir, TRIGGERSDEFERREDFILE);
r = stat(fn.buf, &stab);
if (r) {
if (errno != ENOENT)
ohshite(_("unable to stat triggers deferred file `%.250s'"),
fn.buf);
} else if (!stab.st_size) {
if (!(uf & tduf_writeifempty)) {
pop_cleanup(ehflag_normaltidy);
return -2;
}
}
if (old_deferred)
fclose(old_deferred);
old_deferred = fopen(fn.buf, "r");
if (!old_deferred) {
if (errno != ENOENT)
ohshite(_("unable to open triggers deferred file `%.250s'"),
fn.buf);
if (!(uf & tduf_writeifenoent)) {
pop_cleanup(ehflag_normaltidy);
return -3;
}
}
if (uf & tduf_write) {
constructfn(&newfn, admindir, TRIGGERSDEFERREDFILE ".new");
if (trig_new_deferred)
fclose(trig_new_deferred);
trig_new_deferred = fopen(newfn.buf, "w");
if (!trig_new_deferred)
ohshite(_("unable to open/create new triggers deferred file `%.250s'"),
newfn.buf);
}
if (!old_deferred)
return 1;
trigdef_yyrestart(old_deferred);
BEGIN(0);
return 2;
}
void
trigdef_process_done(void)
{
int r;
if (old_deferred) {
if (ferror(old_deferred))
ohshite(_("error reading triggers deferred file `%.250s'"),
fn.buf);
fclose(old_deferred);
old_deferred = NULL;
}
if (trig_new_deferred) {
if (ferror(trig_new_deferred))
ohshite(_("unable to write new triggers deferred "
"file `%.250s'"), newfn.buf);
r = fclose(trig_new_deferred);
trig_new_deferred = NULL;
if (r)
ohshite(_("unable to close new triggers deferred "
"file `%.250s'"), newfn.buf);
if (rename(newfn.buf, fn.buf))
ohshite(_("unable to install new triggers deferred "
"file `%.250s'"), fn.buf);
}
/* Unlock. */
pop_cleanup(ehflag_normaltidy);
}
|