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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* dselect - Debian package maintenance user interface
* methparse.cc - access method list parsing
*
* Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
* Copyright © 2008-2011, 2013-2015 Guillem Jover <guillem@debian.org>
*
* 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 of the License, 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <compat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <dpkg/i18n.h>
#include <dpkg/c-ctype.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/file.h>
#include "dselect.h"
#include "bindings.h"
#include "method.h"
int noptions = 0;
struct dselect_option *options = nullptr;
struct dselect_option *coption = nullptr;
struct method *methods = nullptr;
static void DPKG_ATTR_NORET
badmethod(varbuf &pathname, const char *why)
{
ohshit(_("syntax error in method options file '%s' -- %s"),
pathname.str(), why);
}
static void DPKG_ATTR_NORET
eofmethod(varbuf &pathname, FILE *f, const char *why)
{
if (ferror(f))
ohshite(_("error reading options file '%s'"),
pathname.str());
badmethod(pathname, why);
}
// TODO: Refactor to reduce nesting levels.
void
readmethods(const char *pathbase, dselect_option **optionspp, int *nread)
{
static const char *const methodprograms[] = {
METHODSETUPSCRIPT,
METHODUPDATESCRIPT,
METHODINSTALLSCRIPT,
nullptr
};
const char *const *ccpp;
int methodlen;
DIR *dir;
FILE *names;
struct dirent *dent;
struct varbuf vb;
method *meth;
dselect_option *opt;
varbuf path;
path += pathbase;
path += "/";
dir = opendir(path.str());
if (!dir) {
if (errno == ENOENT)
return;
ohshite(_("unable to read '%s' directory for reading methods"),
path.str());
}
debug(dbg_general, "readmethods('%s',...) directory open", pathbase);
while ((dent = readdir(dir)) != nullptr) {
int c = dent->d_name[0];
debug(dbg_general, "readmethods('%s',...) considering '%s' ...",
pathbase, dent->d_name);
if (c != '_' && !c_isalpha(c))
continue;
char *p = dent->d_name + 1;
while ((c = *p) != 0 && c_isalnum(c) && c != '_')
p++;
if (c)
continue;
methodlen = strlen(dent->d_name);
if (methodlen > IMETHODMAXLEN)
ohshit(_("method '%s' has name that is too long "
"(%d > %d characters)"),
dent->d_name, methodlen, IMETHODMAXLEN);
// FIXME: Add localized method support.
path.reset();
path += pathbase;
path += "/";
path += dent->d_name;
path += "/";
varbuf pathmeth;
for (ccpp = methodprograms; *ccpp; ccpp++) {
pathmeth = path;
pathmeth += *ccpp;
if (access(pathmeth.str(), R_OK | X_OK))
ohshite(_("unable to access method script '%s'"),
pathmeth.str());
}
debug(dbg_general,
" readmethods('%s',...) scripts ok", pathbase);
varbuf pathopts;
pathopts = path;
pathopts += METHODOPTIONSFILE;
names = fopen(pathopts.str(), "r");
if (!names)
ohshite(_("unable to read method options file '%s'"),
pathopts.str());
meth = new method;
meth->name = varbuf(dent->d_name);
meth->path = path;
meth->next = methods;
meth->prev = nullptr;
if (methods)
methods->prev = meth;
methods = meth;
debug(dbg_general,
" readmethods('%s',...) new method name='%s' path='%s'",
pathbase, meth->name.str(), meth->path.str());
while ((c = fgetc(names)) != EOF) {
if (c_isspace(c))
continue;
opt = new dselect_option;
opt->meth = meth;
vb.reset();
do {
if (!c_isdigit(c))
badmethod(pathopts, _("non-digit where digit wanted"));
vb += c;
c = fgetc(names);
if (c == EOF)
eofmethod(pathopts, names, _("end of file in index string"));
} while (!c_isspace(c));
if (vb.len() > OPTIONINDEXMAXLEN)
badmethod(pathopts, _("index string too long"));
opt->index = vb;
do {
if (c == '\n')
badmethod(pathopts, _("newline before option name start"));
c = fgetc(names);
if (c == EOF)
eofmethod(pathopts, names, _("end of file before option name start"));
} while (c_isspace(c));
vb.reset();
if (!c_isalpha(c) && c != '_')
badmethod(pathopts, _("nonalpha where option name start wanted"));
do {
if (!c_isalnum(c) && c != '_')
badmethod(pathopts, _("non-alphanum in option name"));
vb += c;
c = fgetc(names);
if (c == EOF)
eofmethod(pathopts, names, _("end of file in option name"));
} while (!c_isspace(c));
opt->name = vb;
do {
if (c == '\n')
badmethod(pathopts, _("newline before summary"));
c = fgetc(names);
if (c == EOF)
eofmethod(pathopts, names, _("end of file before summary"));
} while (c_isspace(c));
vb.reset();
do {
vb += c;
c = fgetc(names);
if (c == EOF)
eofmethod(pathopts, names, _("end of file in summary - missing newline"));
} while (c != '\n');
opt->summary = vb;
varbuf pathoptsdesc;
pathoptsdesc += pathopts;
pathoptsdesc += OPTIONSDESCPFX;
pathoptsdesc += opt->name;
dpkg_error err = DPKG_ERROR_INIT;
if (file_slurp(pathoptsdesc.str(), &opt->description, &err) < 0) {
if (err.syserrno != ENOENT)
dpkg_error_print(&err, _("cannot load option description file '%s'"),
pathoptsdesc.str());
}
debug(dbg_general,
" readmethods('%s',...) new option index='%s' name='%s'"
" summary='%.20s' len(description=%s)=%zu method name='%s'"
" path='%s'",
pathbase,
opt->index.str(),
opt->name.str(),
opt->summary.str(),
opt->description.len() ? "'...'" : "<none>",
opt->description.len() ? opt->description.len() : -1,
opt->meth->name.str(),
opt->meth->path.str());
dselect_option **optinsert = optionspp;
while (*optinsert &&
strcmp(opt->index.str(), (*optinsert)->index.str()) > 0)
optinsert = &(*optinsert)->next;
opt->next = *optinsert;
*optinsert = opt;
(*nread)++;
}
if (ferror(names))
ohshite(_("error during read of method options file '%s'"),
pathopts.str());
fclose(names);
}
closedir(dir);
debug(dbg_general, "readmethods('%s',...) done", pathbase);
}
static char *methoptfile = nullptr;
void
getcurrentopt()
{
char methoptbuf[IMETHODMAXLEN + 1 + IOPTIONMAXLEN + 2];
FILE *cmo;
int l;
char *p;
if (methoptfile == nullptr)
methoptfile = dpkg_db_get_path(CMETHOPTFILE);
coption = nullptr;
cmo = fopen(methoptfile, "r");
if (!cmo) {
if (errno == ENOENT)
return;
ohshite(_("unable to open current option file '%s'"),
methoptfile);
}
debug(dbg_general, "getcurrentopt() cmethopt open");
if (!fgets(methoptbuf, sizeof(methoptbuf), cmo)) {
fclose(cmo);
return;
}
if (fgetc(cmo) != EOF) {
fclose(cmo);
return;
}
if (!feof(cmo)) {
fclose(cmo);
return;
}
debug(dbg_general, "getcurrentopt() cmethopt eof");
fclose(cmo);
debug(dbg_general, "getcurrentopt() cmethopt read");
l = strlen(methoptbuf);
if (!l || methoptbuf[l - 1] != '\n')
return;
methoptbuf[--l] = 0;
debug(dbg_general, "getcurrentopt() cmethopt len and newline");
p = strchr(methoptbuf, ' ');
if (!p)
return;
debug(dbg_general, "getcurrentopt() cmethopt space");
*p++ = 0;
debug(dbg_general, "getcurrentopt() cmethopt meth name '%s'",
methoptbuf);
method *meth = methods;
while (meth && strcmp(methoptbuf, meth->name.str()))
meth = meth->next;
if (!meth)
return;
debug(dbg_general, "getcurrentopt() cmethopt meth found; opt '%s'", p);
dselect_option *opt = options;
while (opt && (opt->meth != meth || strcmp(p, opt->name.str())))
opt = opt->next;
if (!opt)
return;
debug(dbg_general, "getcurrentopt() cmethopt opt found");
coption = opt;
}
void
writecurrentopt()
{
struct atomic_file *file;
if (methoptfile == nullptr)
internerr("method options filename is nullptr");
file = atomic_file_new(methoptfile, ATOMIC_FILE_NORMAL);
atomic_file_open(file);
if (fprintf(file->fp, "%s %s\n", coption->meth->name.str(), coption->name.str()) == EOF)
ohshite(_("unable to write new option to '%s'"),
file->name_new);
atomic_file_close(file);
atomic_file_commit(file);
atomic_file_free(file);
}
|