po: Update German programs translation
[dpkg.git] / src / trigger / main.c
blobcf2e2c46a57a5f2bb4c546846c699f4848ef8111
1 /*
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/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <sys/types.h>
27 #include <fcntl.h>
28 #ifdef HAVE_LOCALE_H
29 #include <locale.h>
30 #endif
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <stdio.h>
36 #include <dpkg/i18n.h>
37 #include <dpkg/dpkg.h>
38 #include <dpkg/dpkg-db.h>
39 #include <dpkg/debug.h>
40 #include <dpkg/options.h>
41 #include <dpkg/trigdeferred.h>
42 #include <dpkg/triglib.h>
43 #include <dpkg/pkg-spec.h>
45 static const char printforhelp[] = N_(
46 "Type dpkg-trigger --help for help about this utility.");
48 static int
49 printversion(const char *const *argv)
51 printf(_("Debian %s package trigger utility version %s.\n"),
52 dpkg_get_progname(), PACKAGE_RELEASE);
54 printf(_(
55 "This is free software; see the GNU General Public License version 2 or\n"
56 "later for copying conditions. There is NO warranty.\n"));
58 m_output(stdout, _("<standard output>"));
60 return 0;
63 static int
64 usage(const char *const *argv)
66 printf(_(
67 "Usage: %s [<option>...] <trigger-name>\n"
68 " %s [<option>...] <command>\n"
69 "\n"), dpkg_get_progname(), dpkg_get_progname());
71 printf(_(
72 "Commands:\n"
73 " --check-supported Check if the running dpkg supports triggers.\n"
74 "\n"));
76 printf(_(
77 " -?, --help Show this help message.\n"
78 " --version Show the version.\n"
79 "\n"));
81 printf(_(
82 "Options:\n"
83 " --admindir=<directory> Use <directory> instead of %s.\n"
84 " --root=<directory> Use <directory> instead of %s.\n"
85 " --by-package=<package> Override trigger awaiter (normally set\n"
86 " by dpkg).\n"
87 " --await Package needs to await the processing.\n"
88 " --no-await No package needs to await the processing.\n"
89 " --no-act Just test - don't actually change anything.\n"
90 "\n"), ADMINDIR, "/");
92 m_output(stdout, _("<standard output>"));
94 return 0;
97 static int opt_act = 1;
98 static int opt_await = 1;
99 static const char *opt_bypackage;
101 static const char *activate;
102 static bool done_trig, ctrig;
104 static void
105 yespackage(const char *awname)
107 trigdef_update_printf(" %s", awname);
110 static const char *
111 parse_awaiter_package(void)
113 struct dpkg_error err = DPKG_ERROR_INIT;
114 struct pkginfo *pkg;
116 if (!opt_await)
117 opt_bypackage = "-";
119 if (opt_bypackage == NULL) {
120 const char *pkgname, *archname;
122 pkgname = getenv("DPKG_MAINTSCRIPT_PACKAGE");
123 archname = getenv("DPKG_MAINTSCRIPT_ARCH");
124 if (pkgname == NULL || archname == NULL)
125 badusage(_("must be called from a maintainer script"
126 " (or with a --by-package option)"));
128 pkg = pkg_spec_find_pkg(pkgname, archname, &err);
129 } else if (strcmp(opt_bypackage, "-") == 0) {
130 pkg = NULL;
131 } else {
132 pkg = pkg_spec_parse_pkg(opt_bypackage, &err);
135 /* Normalize the bypackage name if there was no error. */
136 if (pkg)
137 opt_bypackage = pkg_name(pkg, pnaw_same);
139 return err.str;
142 static void
143 tdm_add_trig_begin(const char *trig)
145 ctrig = strcmp(trig, activate) == 0;
146 trigdef_update_printf("%s", trig);
147 if (!ctrig || done_trig)
148 return;
149 yespackage(opt_bypackage);
150 done_trig = true;
153 static void
154 tdm_add_package(const char *awname)
156 if (ctrig && strcmp(awname, opt_bypackage) == 0)
157 return;
158 yespackage(awname);
161 static void
162 tdm_add_trig_end(void)
164 trigdef_update_printf("\n");
167 static const struct trigdefmeths tdm_add = {
168 .trig_begin = tdm_add_trig_begin,
169 .package = tdm_add_package,
170 .trig_end = tdm_add_trig_end,
173 static int
174 do_trigger(const char *const *argv)
176 const char *badname;
177 enum trigdef_update_flags tduf;
178 enum trigdef_update_status tdus;
180 if (!*argv || argv[1])
181 badusage(_("takes one argument, the trigger name"));
183 badname = parse_awaiter_package();
184 if (badname)
185 badusage(_("illegal awaited package name '%.250s': %.250s"),
186 opt_bypackage, badname);
188 activate = argv[0];
189 badname = trig_name_is_illegal(activate);
190 if (badname)
191 badusage(_("invalid trigger name '%.250s': %.250s"),
192 activate, badname);
194 trigdef_set_methods(&tdm_add);
196 tduf = TDUF_NO_LOCK_OK;
197 if (opt_act)
198 tduf |= TDUF_WRITE | TDUF_WRITE_IF_EMPTY;
199 tdus = trigdef_update_start(tduf);
200 if (tdus >= 0) {
201 trigdef_parse();
202 if (!done_trig)
203 trigdef_update_printf("%s %s\n", activate, opt_bypackage);
204 trigdef_process_done();
207 return 0;
210 static int
211 do_check(const char *const *argv)
213 enum trigdef_update_status uf;
215 if (*argv)
216 badusage(_("--%s takes no arguments"), "check-supported");
218 uf = trigdef_update_start(TDUF_NO_LOCK_OK);
219 switch (uf) {
220 case TDUS_ERROR_NO_DIR:
221 notice(_("triggers data directory not yet created"));
222 return 1;
223 case TDUS_ERROR_NO_DEFERRED:
224 notice(_("trigger records not yet in existence"));
225 return 1;
226 case TDUS_OK:
227 case TDUS_ERROR_EMPTY_DEFERRED:
228 return 0;
229 default:
230 internerr("unknown trigdef_update_start return value '%d'", uf);
234 static const struct cmdinfo cmdinfo_trigger =
235 ACTION("trigger", 0, 0, do_trigger);
237 static const struct cmdinfo cmdinfos[] = {
238 ACTION("check-supported", 0, 0, do_check),
239 ACTION("help", '?', 0, usage),
240 ACTION("version", 0, 0, printversion),
242 { "admindir", 0, 1, NULL, NULL, set_admindir, 0 },
243 { "root", 0, 1, NULL, NULL, set_root, 0 },
244 { "by-package", 'f', 1, NULL, &opt_bypackage },
245 { "await", 0, 0, &opt_await, NULL, NULL, 1 },
246 { "no-await", 0, 0, &opt_await, NULL, NULL, 0 },
247 { "no-act", 0, 0, &opt_act, NULL, NULL, 0 },
248 { NULL }
252 main(int argc, const char *const *argv)
254 int ret;
256 dpkg_locales_init(PACKAGE);
257 dpkg_program_init("dpkg-trigger");
258 dpkg_options_parse(&argv, cmdinfos, printforhelp);
260 debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
262 if (!cipaction)
263 setaction(&cmdinfo_trigger, NULL);
265 ret = cipaction->action(argv);
267 dpkg_program_done();
268 dpkg_locales_done();
270 return ret;