2 * dpkg-trigger - trigger management utility
4 * Copyright © 2007 Canonical Ltd.
5 * Written by Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but 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 License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 #include <sys/types.h>
36 #include <dpkg/i18n.h>
37 #include <dpkg/dpkg.h>
38 #include <dpkg/dpkg-db.h>
39 #include <dpkg/options.h>
40 #include <dpkg/trigdeferred.h>
41 #include <dpkg/triglib.h>
42 #include <dpkg/pkg-spec.h>
44 static const char printforhelp[] = N_(
45 "Type dpkg-trigger --help for help about this utility.");
47 static void DPKG_ATTR_NORET
48 printversion(const struct cmdinfo *ci, const char *value)
50 printf(_("Debian %s package trigger utility version %s.\n"),
51 dpkg_get_progname(), PACKAGE_RELEASE);
54 "This is free software; see the GNU General Public License version 2 or\n"
55 "later for copying conditions. There is NO warranty.\n"));
57 m_output(stdout, _("<standard output>"));
62 static void DPKG_ATTR_NORET
63 usage(const struct cmdinfo *ci, const char *value)
66 "Usage: %s [<options> ...] <trigger-name>\n"
67 " %s [<options> ...] <command>\n"
68 "\n"), dpkg_get_progname(), dpkg_get_progname());
72 " --check-supported Check if the running dpkg supports triggers.\n"
76 " -?, --help Show this help message.\n"
77 " --version Show the version.\n"
82 " --admindir=<directory> Use <directory> instead of %s.\n"
83 " --by-package=<package> Override trigger awaiter (normally set\n"
85 " --await Package needs to await the processing.\n"
86 " --no-await No package needs to await the processing.\n"
87 " --no-act Just test - don't actually change anything.\n"
90 m_output(stdout, _("<standard output>"));
95 static const char *admindir;
96 static int f_noact, f_check;
97 static int f_await = 1;
99 static const char *bypackage, *activate;
100 static bool done_trig, ctrig;
103 yespackage(const char *awname)
105 trigdef_update_printf(" %s", awname);
109 parse_awaiter_package(void)
111 struct dpkg_error err = DPKG_ERROR_INIT;
117 if (bypackage == NULL) {
118 const char *pkgname, *archname;
120 pkgname = getenv("DPKG_MAINTSCRIPT_PACKAGE");
121 archname = getenv("DPKG_MAINTSCRIPT_ARCH");
122 if (pkgname == NULL || archname == NULL)
123 badusage(_("must be called from a maintainer script"
124 " (or with a --by-package option)"));
126 pkg = pkg_spec_find_pkg(pkgname, archname, &err);
127 } else if (strcmp(bypackage, "-") == 0) {
130 pkg = pkg_spec_parse_pkg(bypackage, &err);
133 /* Normalize the bypackage name if there was no error. */
135 bypackage = pkg_name(pkg, pnaw_nonambig);
141 tdm_add_trig_begin(const char *trig)
143 ctrig = strcmp(trig, activate) == 0;
144 trigdef_update_printf("%s", trig);
145 if (!ctrig || done_trig)
147 yespackage(bypackage);
152 tdm_add_package(const char *awname)
154 if (ctrig && strcmp(awname, bypackage) == 0)
160 tdm_add_trig_end(void)
162 trigdef_update_printf("\n");
165 static const struct trigdefmeths tdm_add = {
166 .trig_begin = tdm_add_trig_begin,
167 .package = tdm_add_package,
168 .trig_end = tdm_add_trig_end,
174 enum trigdef_update_status uf;
176 uf = trigdef_update_start(TDUF_NO_LOCK_OK);
178 case TDUS_ERROR_NO_DIR:
179 notice(_("triggers data directory not yet created"));
181 case TDUS_ERROR_NO_DEFERRED:
182 notice(_("trigger records not yet in existence"));
185 case TDUS_ERROR_EMPTY_DEFERRED:
188 internerr("unknown trigdef_update_start return value '%d'", uf);
192 static const struct cmdinfo cmdinfos[] = {
193 { "admindir", 0, 1, NULL, &admindir },
194 { "by-package", 'f', 1, NULL, &bypackage },
195 { "await", 0, 0, &f_await, NULL, NULL, 1 },
196 { "no-await", 0, 0, &f_await, NULL, NULL, 0 },
197 { "no-act", 0, 0, &f_noact, NULL, NULL, 1 },
198 { "check-supported", 0, 0, &f_check, NULL, NULL, 1 },
199 { "help", '?', 0, NULL, NULL, usage },
200 { "version", 0, 0, NULL, NULL, printversion },
205 main(int argc, const char *const *argv)
208 enum trigdef_update_flags tduf;
209 enum trigdef_update_status tdus;
211 dpkg_locales_init(PACKAGE);
212 dpkg_program_init("dpkg-trigger");
213 dpkg_options_parse(&argv, cmdinfos, printforhelp);
215 admindir = dpkg_db_set_dir(admindir);
219 badusage(_("--%s takes no arguments"),
224 if (!*argv || argv[1])
225 badusage(_("takes one argument, the trigger name"));
227 badname = parse_awaiter_package();
229 badusage(_("illegal awaited package name '%.250s': %.250s"),
233 badname = trig_name_is_illegal(activate);
235 badusage(_("invalid trigger name '%.250s': %.250s"),
238 trigdef_set_methods(&tdm_add);
240 tduf = TDUF_NO_LOCK_OK;
242 tduf |= TDUF_WRITE | TDUF_WRITE_IF_EMPTY;
243 tdus = trigdef_update_start(tduf);
247 trigdef_update_printf("%s %s\n", activate, bypackage);
248 trigdef_process_done();