2
* dpkg - main program for package management
3
* trigdeferred.l - parsing of triggers/Deferred
5
* Copyright © 2007 Canonical Ltd
6
* written by Ian Jackson <ian@chiark.greenend.org.uk>
8
* This is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as
10
* published by the Free Software Foundation; either version 2,
11
* or (at your option) any later version.
13
* This is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public
19
* License along with dpkg; if not, write to the Free Software
20
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
%option prefix="trigdef_yy"
24
/* Reset the name to the default value (instead of using "trigdeferred.c")
25
* so that automake (ylwrap) can find it. */
26
%option outfile="lex.yy.c"
40
#include <dpkg-i18n.h>
43
#include <sys/fcntl.h>
50
static struct varbuf fn, newfn;
56
[ \t\n] /* whitespace */
59
trigdef->trig_begin(trigdef_yytext);
63
<midline>[ \t] /* whitespace */
64
<midline>[-0-9a-z][-+.0-9a-z]* {
65
if (trigdef_yytext[0] == '-' && trigdef_yytext[1])
66
ohshit(_("invalid package name `%.250s' in triggers deferred "
67
"file `%.250s'"), trigdef_yytext, fn.buf);
68
trigdef->package(trigdef_yytext);
75
ohshit(_("truncated triggers deferred file `%.250s'"), fn.buf);
79
ohshit(_("syntax error in triggers deferred file `%.250s' at "
81
fn.buf, yytext, YY_START == midline ? " midline": "");
86
/*---------- Deferred file handling ----------*/
88
static int lock_fd = -1;
89
static FILE *old_deferred;
91
FILE *trig_new_deferred;
92
const struct trigdefmeths *trigdef;
95
constructfn(struct varbuf *vb, const char *admindir, const char *tail)
98
varbufaddstr(vb, admindir);
99
varbufaddstr(vb, "/" TRIGGERSDIR);
100
varbufaddstr(vb, tail);
105
trigdef_update_start(enum trigdef_updateflags uf, const char *admindir)
110
if (uf & tduf_write) {
111
constructfn(&fn, admindir, TRIGGERSLOCKFILE);
113
lock_fd = open(fn.buf, O_RDWR | O_CREAT | O_TRUNC, 0600);
115
if (!(errno == ENOENT && (uf & tduf_nolockok)))
116
ohshite(_("unable to open/create "
117
"triggers lockfile `%.250s'"),
123
lock_file(&lock_fd, fn.buf, _("unable to lock triggers area"),
126
/* Dummy for pop_cleanups. */
127
push_cleanup(NULL, 0, NULL, 0, 0);
130
constructfn(&fn, admindir, TRIGGERSDEFERREDFILE);
131
r = stat(fn.buf, &stab);
134
ohshite(_("unable to stat triggers deferred file `%.250s'"),
136
} else if (!stab.st_size) {
137
if (!(uf & tduf_writeifempty)) {
138
pop_cleanup(ehflag_normaltidy);
144
fclose(old_deferred);
145
old_deferred = fopen(fn.buf, "r");
148
ohshite(_("unable to open triggers deferred file `%.250s'"),
150
if (!(uf & tduf_writeifenoent)) {
151
pop_cleanup(ehflag_normaltidy);
156
if (uf & tduf_write) {
157
constructfn(&newfn, admindir, TRIGGERSDEFERREDFILE ".new");
158
if (trig_new_deferred)
159
fclose(trig_new_deferred);
160
trig_new_deferred = fopen(newfn.buf, "w");
161
if (!trig_new_deferred)
162
ohshite(_("unable to open/create new triggers deferred file `%.250s'"),
169
trigdef_yyrestart(old_deferred);
176
trigdef_process_done(void)
181
if (ferror(old_deferred))
182
ohshite(_("error reading triggers deferred file `%.250s'"),
184
fclose(old_deferred);
188
if (trig_new_deferred) {
189
if (ferror(trig_new_deferred))
190
ohshite(_("unable to write new triggers deferred "
191
"file `%.250s'"), newfn.buf);
192
r = fclose(trig_new_deferred);
193
trig_new_deferred = NULL;
195
ohshite(_("unable to close new triggers deferred "
196
"file `%.250s'"), newfn.buf);
198
if (rename(newfn.buf, fn.buf))
199
ohshite(_("unable to install new triggers deferred "
200
"file `%.250s'"), fn.buf);
204
pop_cleanup(ehflag_normaltidy);