2 * libdpkg - Debian packaging suite library routines
3 * fields.c - parsing of all the different fields, when reading in
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2001 Wichert Akkerman
7 * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
8 * Copyright © 2009 Canonical Ltd.
9 * Copyright © 2011 Linaro Limited
10 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
12 * This is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32 #include <dpkg/i18n.h>
33 #include <dpkg/c-ctype.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/dpkg-db.h>
36 #include <dpkg/arch.h>
37 #include <dpkg/string.h>
38 #include <dpkg/path.h>
39 #include <dpkg/parsedump.h>
40 #include <dpkg/pkg-spec.h>
41 #include <dpkg/triglib.h>
44 * Flags to parse a name associated to a value.
47 /** Expect no more words (default). */
49 /** Expect another word after the parsed name. */
51 /** Do not fail if there is no name with an associated value found. */
52 PARSE_NV_FALLBACK
= 2,
56 * Parses a name and returns its associated value.
58 * Gets a pointer to the string to parse in @a strp, and modifies the pointer
59 * to the string to point to the end of the parsed text. If no value is found
60 * for the name and @a flags is set to #PARSE_NV_FALLBACK then @a strp is set
61 * to NULL and returns -1, otherwise a parse error is emitted.
64 parse_nv(struct parsedb_state
*ps
, enum parse_nv_mode parse_mode
,
65 const char **strp
, const struct namevalue
*nv_head
)
67 const char *str_start
= *strp
, *str_end
;
68 const struct namevalue
*nv
;
71 dpkg_error_destroy(&ps
->err
);
73 if (str_start
[0] == '\0')
74 return dpkg_put_error(&ps
->err
, _("is missing a value"));
76 nv
= namevalue_find_by_name(nv_head
, str_start
);
78 /* We got no match, skip further string validation. */
79 if (parse_mode
!= PARSE_NV_FALLBACK
)
80 return dpkg_put_error(&ps
->err
, _("has invalid value '%.50s'"), str_start
);
85 str_end
= str_start
+ nv
->length
;
86 while (c_isspace(str_end
[0]))
90 if (parse_mode
!= PARSE_NV_NEXT
&& str_is_set(str_end
))
91 return dpkg_put_error(&ps
->err
, _("has trailing junk"));
100 f_name(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
101 struct parsedb_state
*ps
,
102 const char *value
, const struct fieldinfo
*fip
)
106 e
= pkg_name_is_illegal(value
);
108 parse_error(ps
, _("invalid package name in '%s' field: %s"), fip
->name
, e
);
109 /* We use the new name, as pkg_hash_find_set() may have done a tolower for us. */
110 pkg
->set
->name
= pkg_hash_find_set(value
)->name
;
114 f_archives(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
115 struct parsedb_state
*ps
,
116 const char *value
, const struct fieldinfo
*fip
)
118 struct archivedetails
*fdp
, **fdpp
;
123 parse_error(ps
, _("empty archive details '%s' field"), fip
->name
);
124 if (!(ps
->flags
& pdb_recordavailable
))
126 _("archive details '%s' field not allowed in status file"),
128 allowextend
= !pkg
->archives
;
129 fdpp
= &pkg
->archives
;
130 cpos
= nfstrsave(value
);
133 while (*space
&& !c_isspace(*space
))
141 _("too many values in archive details '%s' field "
142 "(compared to others)"), fip
->name
);
143 fdp
= nfmalloc(sizeof(*fdp
));
145 fdp
->name
= fdp
->msdosname
= fdp
->size
= fdp
->md5sum
= NULL
;
148 STRUCTFIELD(fdp
, fip
->integer
, const char *) = cpos
;
150 while (*space
&& c_isspace(*space
))
156 _("too few values in archive details '%s' field "
157 "(compared to others)"), fip
->name
);
161 f_charfield(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
162 struct parsedb_state
*ps
,
163 const char *value
, const struct fieldinfo
*fip
)
166 STRUCTFIELD(pkgbin
, fip
->integer
, char *) = nfstrsave(value
);
170 f_boolean(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
171 struct parsedb_state
*ps
,
172 const char *value
, const struct fieldinfo
*fip
)
179 boolean
= parse_nv(ps
, PARSE_NV_LAST
, &value
, booleaninfos
);
180 if (dpkg_has_error(&ps
->err
))
181 parse_error(ps
, _("boolean (yes/no) '%s' field: %s"),
182 fip
->name
, ps
->err
.str
);
184 STRUCTFIELD(pkgbin
, fip
->integer
, bool) = boolean
;
188 f_multiarch(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
189 struct parsedb_state
*ps
,
190 const char *value
, const struct fieldinfo
*fip
)
197 multiarch
= parse_nv(ps
, PARSE_NV_LAST
, &value
, multiarchinfos
);
198 if (dpkg_has_error(&ps
->err
))
199 parse_error(ps
, _("quadstate (foreign/allowed/same/no) '%s' field: %s"),
200 fip
->name
, ps
->err
.str
);
201 STRUCTFIELD(pkgbin
, fip
->integer
, int) = multiarch
;
205 f_architecture(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
206 struct parsedb_state
*ps
,
207 const char *value
, const struct fieldinfo
*fip
)
209 pkgbin
->arch
= dpkg_arch_find(value
);
210 if (pkgbin
->arch
->type
== DPKG_ARCH_ILLEGAL
)
211 parse_warn(ps
, _("'%s' is not a valid architecture name in '%s' field: %s"),
212 value
, fip
->name
, dpkg_arch_name_is_illegal(value
));
216 f_section(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
217 struct parsedb_state
*ps
,
218 const char *value
, const struct fieldinfo
*fip
)
221 pkg
->section
= nfstrsave(value
);
225 f_priority(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
226 struct parsedb_state
*ps
,
227 const char *value
, const struct fieldinfo
*fip
)
229 const char *str
= value
;
234 priority
= parse_nv(ps
, PARSE_NV_FALLBACK
, &str
, priorityinfos
);
235 if (dpkg_has_error(&ps
->err
))
236 parse_error(ps
, _("word in '%s' field: %s"), fip
->name
, ps
->err
.str
);
239 pkg
->priority
= PKG_PRIO_OTHER
;
240 pkg
->otherpriority
= nfstrsave(value
);
242 pkg
->priority
= priority
;
247 f_obs_class(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
248 struct parsedb_state
*ps
,
249 const char *value
, const struct fieldinfo
*fip
)
251 parse_warn(ps
, _("obsolete '%s' field used"), fip
->name
);
252 f_priority(pkg
, pkgbin
, ps
, value
, fip
);
256 f_status(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
257 struct parsedb_state
*ps
,
258 const char *value
, const struct fieldinfo
*fip
)
260 if (ps
->flags
& pdb_rejectstatus
)
262 _("value for '%s' field not allowed in this context"),
264 if (ps
->flags
& pdb_recordavailable
)
267 pkg
->want
= parse_nv(ps
, PARSE_NV_NEXT
, &value
, wantinfos
);
268 if (dpkg_has_error(&ps
->err
))
269 parse_error(ps
, _("first (want) word in '%s' field: %s"),
270 fip
->name
, ps
->err
.str
);
271 pkg
->eflag
= parse_nv(ps
, PARSE_NV_NEXT
, &value
, eflaginfos
);
272 if (dpkg_has_error(&ps
->err
))
273 parse_error(ps
, _("second (error) word in '%s' field: %s"),
274 fip
->name
, ps
->err
.str
);
275 pkg
->status
= parse_nv(ps
, PARSE_NV_LAST
, &value
, statusinfos
);
276 if (dpkg_has_error(&ps
->err
))
277 parse_error(ps
, _("third (status) word in '%s' field: %s"),
278 fip
->name
, ps
->err
.str
);
282 f_version(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
283 struct parsedb_state
*ps
,
284 const char *value
, const struct fieldinfo
*fip
)
286 if (parse_db_version(ps
, &pkgbin
->version
, value
) < 0)
287 parse_problem(ps
, _("'%s' field value '%.250s'"), fip
->name
, value
);
291 f_obs_revision(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
292 struct parsedb_state
*ps
,
293 const char *value
, const struct fieldinfo
*fip
)
297 parse_warn(ps
, _("obsolete '%s' field used"), fip
->name
);
299 if (str_is_set(pkgbin
->version
.revision
)) {
300 newversion
= nfmalloc(strlen(pkgbin
->version
.version
) +
301 strlen(pkgbin
->version
.revision
) + 2);
302 sprintf(newversion
, "%s-%s", pkgbin
->version
.version
,
303 pkgbin
->version
.revision
);
304 pkgbin
->version
.version
= newversion
;
306 pkgbin
->version
.revision
= nfstrsave(value
);
310 f_configversion(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
311 struct parsedb_state
*ps
,
312 const char *value
, const struct fieldinfo
*fip
)
314 if (ps
->flags
& pdb_rejectstatus
)
316 _("value for '%s' field not allowed in this context"),
318 if (ps
->flags
& pdb_recordavailable
)
321 if (parse_db_version(ps
, &pkg
->configversion
, value
) < 0)
322 parse_problem(ps
, _("'%s' field value '%.250s'"), fip
->name
, value
);
326 * The code in f_conffiles ensures that value[-1] == ' ', which is helpful.
328 static void conffvalue_lastword(const char *value
, const char *from
,
330 const char **word_start_r
, int *word_len_r
,
331 const char **new_from_r
,
332 struct parsedb_state
*ps
)
336 if (from
<= value
+1) goto malformed
;
337 for (lastspc
= from
-1; *lastspc
!= ' '; lastspc
--);
338 if (lastspc
<= value
+1 || lastspc
>= endent
-1) goto malformed
;
340 *new_from_r
= lastspc
;
341 *word_start_r
= lastspc
+ 1;
342 *word_len_r
= (int)(from
- *word_start_r
);
347 _("value for '%s' field has malformed line '%.*s'"),
348 "Conffiles", (int)min(endent
- value
, 250), value
);
352 f_conffiles(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
353 struct parsedb_state
*ps
,
354 const char *value
, const struct fieldinfo
*fip
)
356 static const char obsolete_str
[]= "obsolete";
357 static const char remove_on_upgrade_str
[] = "remove-on-upgrade";
358 struct conffile
**lastp
;
360 lastp
= &pkgbin
->conffiles
;
362 struct conffile
*newlink
;
363 const char *endent
, *endfn
, *hashstart
;
365 int c
, namelen
, hashlen
;
366 int flags
= CONFFILE_NONE
;
369 if (c
== '\n') continue;
372 _("value for '%s' field has line starting with non-space '%c'"),
374 for (endent
= value
; (c
= *endent
) != '\0' && c
!= '\n'; endent
++) ;
375 conffvalue_lastword(value
, endent
, endent
,
376 &hashstart
, &hashlen
, &endfn
,
378 if (hashlen
== sizeof(remove_on_upgrade_str
) - 1 &&
379 memcmp(hashstart
, remove_on_upgrade_str
, hashlen
) == 0) {
380 flags
|= CONFFILE_REMOVE_ON_UPGRADE
;
381 conffvalue_lastword(value
, endfn
, endent
, &hashstart
, &hashlen
, &endfn
,
385 if (hashlen
== sizeof(obsolete_str
) - 1 &&
386 memcmp(hashstart
, obsolete_str
, hashlen
) == 0) {
387 flags
|= CONFFILE_OBSOLETE
;
388 conffvalue_lastword(value
, endfn
, endent
,
389 &hashstart
, &hashlen
, &endfn
,
393 newlink
= nfmalloc(sizeof(*newlink
));
394 value
= path_skip_slash_dotslash(value
);
395 namelen
= (int)(endfn
-value
);
398 _("root or empty directory listed as a conffile in '%s' field"),
400 newptr
= nfmalloc(namelen
+2);
402 memcpy(newptr
+1,value
,namelen
);
403 newptr
[namelen
+1] = '\0';
404 newlink
->name
= newptr
;
405 newptr
= nfmalloc(hashlen
+1);
406 memcpy(newptr
, hashstart
, hashlen
);
407 newptr
[hashlen
] = '\0';
408 newlink
->hash
= newptr
;
409 newlink
->flags
= flags
;
412 lastp
= &newlink
->next
;
418 f_dependency(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
419 struct parsedb_state
*ps
,
420 const char *value
, const struct fieldinfo
*fip
)
422 const char *p
, *emsg
;
423 static struct varbuf depname
, version
;
425 struct dependency
**ldypp
;
427 /* Empty fields are ignored. */
432 ldypp
= &pkgbin
->depends
;
434 ldypp
= &(*ldypp
)->next
;
436 /* Loop creating new struct dependency's. */
438 struct deppossi
**ldopp
;
439 struct dependency
*dyp
;
441 dyp
= nfmalloc(sizeof(*dyp
));
442 /* Set this to NULL for now, as we don't know what our real
443 * struct pkginfo address (in the database) is going to be yet. */
445 dyp
->next
= NULL
; *ldypp
= dyp
; ldypp
= &dyp
->next
;
446 dyp
->list
= NULL
; ldopp
= &dyp
->list
;
447 dyp
->type
= fip
->integer
;
449 /* Loop creating new struct deppossi's. */
451 const char *depnamestart
;
453 struct deppossi
*dop
;
456 /* Skip over package name characters. */
457 while (*p
&& !c_isspace(*p
) && *p
!= ':' && *p
!= '(' && *p
!= ',' &&
460 depnamelength
= p
- depnamestart
;
461 if (depnamelength
== 0)
463 _("'%s' field, missing package name, or garbage where "
464 "package name expected"), fip
->name
);
466 varbuf_set_buf(&depname
, depnamestart
, depnamelength
);
468 emsg
= pkg_name_is_illegal(depname
.buf
);
471 _("'%s' field, invalid package name '%.255s': %s"),
472 fip
->name
, depname
.buf
, emsg
);
473 dop
= nfmalloc(sizeof(*dop
));
475 dop
->ed
= pkg_hash_find_set(depname
.buf
);
476 dop
->next
= NULL
; *ldopp
= dop
; ldopp
= &dop
->next
;
478 /* Don't link this (which is after all only ‘new_pkg’ from
479 * the main parsing loop in parsedb) into the depended on
480 * packages' lists yet. This will be done later when we
481 * install this (in parse.c). For the moment we do the
482 * ‘forward’ links in deppossi (‘ed’) only, and the ‘backward’
483 * links from the depended on packages to dop are left undone. */
484 dop
->rev_next
= NULL
;
485 dop
->rev_prev
= NULL
;
487 dop
->cyclebreak
= false;
489 /* See if we have an architecture qualifier. */
491 static struct varbuf arch
;
492 const char *archstart
;
496 while (*p
&& !c_isspace(*p
) && *p
!= '(' && *p
!= ',' && *p
!= '|')
498 archlength
= p
- archstart
;
500 parse_error(ps
, _("'%s' field, missing architecture name, or garbage "
501 "where architecture name expected"), fip
->name
);
503 varbuf_set_buf(&arch
, archstart
, archlength
);
505 dop
->arch_is_implicit
= false;
506 dop
->arch
= dpkg_arch_find(arch
.buf
);
508 if (dop
->arch
->type
== DPKG_ARCH_ILLEGAL
)
509 emsg
= dpkg_arch_name_is_illegal(arch
.buf
);
511 parse_error(ps
, _("'%s' field, reference to '%.255s': "
512 "invalid architecture name '%.255s': %s"),
513 fip
->name
, depname
.buf
, arch
.buf
, emsg
);
514 } else if (fip
->integer
== dep_conflicts
|| fip
->integer
== dep_breaks
||
515 fip
->integer
== dep_replaces
) {
516 /* Conflicts/Breaks/Replaces get an implicit "any" arch qualifier. */
517 dop
->arch_is_implicit
= true;
518 dop
->arch
= dpkg_arch_get(DPKG_ARCH_WILDCARD
);
520 /* Otherwise use the pkgbin architecture, which will be assigned to
521 * later on by parse.c, once we can guarantee we have parsed it from
522 * the control stanza. */
523 dop
->arch_is_implicit
= true;
527 /* Skip whitespace after package name. */
528 while (c_isspace(*p
))
531 /* See if we have a versioned relation. */
534 const char *versionstart
;
538 while (c_isspace(*p
))
541 if (c1
== '<' || c1
== '>') {
545 dop
->verrel
= (c1
== '<') ? DPKG_RELATION_LT
: DPKG_RELATION_GT
;
547 dop
->verrel
|= DPKG_RELATION_EQ
;
549 } else if (c2
== c1
) {
550 /* Either ‘<<’ or ‘>>’. */
552 } else if (c2
== '<' || c2
== '>') {
554 _("'%s' field, reference to '%.255s':\n"
555 " bad version relationship %c%c"),
556 fip
->name
, depname
.buf
, c1
, c2
);
559 _("'%s' field, reference to '%.255s':\n"
560 " '%c' is obsolete, use '%c=' or '%c%c' instead"),
561 fip
->name
, depname
.buf
, c1
, c1
, c1
, c1
);
562 dop
->verrel
|= DPKG_RELATION_EQ
;
564 } else if (c1
== '=') {
565 dop
->verrel
= DPKG_RELATION_EQ
;
569 _("'%s' field, reference to '%.255s':\n"
570 " implicit exact match on version number, "
571 "suggest using '=' instead"),
572 fip
->name
, depname
.buf
);
573 dop
->verrel
= DPKG_RELATION_EQ
;
575 if ((dop
->verrel
!= DPKG_RELATION_EQ
) && (fip
->integer
== dep_provides
))
576 parse_lax_problem(ps
, pdb_lax_stanza_parser
,
577 _("only exact versions may be used for '%s' field"),
580 if (!c_isspace(*p
) && !c_isalnum(*p
)) {
582 _("'%s' field, reference to '%.255s':\n"
583 " version value starts with non-alphanumeric, "
584 "suggest adding a space"),
585 fip
->name
, depname
.buf
);
587 /* Skip spaces between the relation and the version. */
588 while (c_isspace(*p
))
592 while (*p
&& *p
!= ')' && *p
!= '(') {
597 versionlength
= p
- versionstart
;
598 while (c_isspace(*p
))
602 _("'%s' field, reference to '%.255s': "
603 "version unterminated"), fip
->name
, depname
.buf
);
606 _("'%s' field, reference to '%.255s': "
607 "version contains '%c' instead of '%c'"),
608 fip
->name
, depname
.buf
, *p
, ')');
609 varbuf_set_buf(&version
, versionstart
, versionlength
);
610 if (parse_db_version(ps
, &dop
->version
, version
.buf
) < 0)
612 _("'%s' field, reference to '%.255s': version '%s'"),
613 fip
->name
, depname
.buf
, version
.buf
);
615 while (c_isspace(*p
))
618 dop
->verrel
= DPKG_RELATION_NONE
;
619 dpkg_version_blank(&dop
->version
);
621 if (!*p
|| *p
== ',') break;
624 _("'%s' field, syntax error after reference to package '%.255s'"),
625 fip
->name
, dop
->ed
->name
);
626 if (fip
->integer
== dep_conflicts
||
627 fip
->integer
== dep_breaks
||
628 fip
->integer
== dep_provides
||
629 fip
->integer
== dep_replaces
)
630 parse_error(ps
, _("alternatives ('|') not allowed in '%s' field"),
633 while (c_isspace(*p
))
638 while (c_isspace(*p
))
644 f_obs_dependency(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
645 struct parsedb_state
*ps
,
646 const char *value
, const struct fieldinfo
*fip
)
648 parse_warn(ps
, _("obsolete '%s' field used"), fip
->name
);
649 f_dependency(pkg
, pkgbin
, ps
, value
, fip
);
653 scan_word(const char **valp
)
655 static struct varbuf word
;
656 const char *p
, *start
, *end
;
672 if (*p
&& !c_iswhite(*p
)) {
680 varbuf_set_buf(&word
, start
, end
- start
);
688 f_trigpend(struct pkginfo
*pend
, struct pkgbin
*pkgbin
,
689 struct parsedb_state
*ps
,
690 const char *value
, const struct fieldinfo
*fip
)
694 if (ps
->flags
& pdb_rejectstatus
)
696 _("value for '%s' field not allowed in this context"),
699 while ((word
= scan_word(&value
))) {
702 emsg
= trig_name_is_illegal(word
);
705 _("illegal pending trigger name '%.255s': %s"), word
, emsg
);
707 if (!trig_note_pend_core(pend
, nfstrsave(word
)))
709 _("duplicate pending trigger '%.255s'"), word
);
714 f_trigaw(struct pkginfo
*aw
, struct pkgbin
*pkgbin
,
715 struct parsedb_state
*ps
,
716 const char *value
, const struct fieldinfo
*fip
)
720 if (ps
->flags
& pdb_rejectstatus
)
722 _("value for '%s' field not allowed in this context"),
725 while ((word
= scan_word(&value
))) {
726 struct pkginfo
*pend
;
727 struct dpkg_error err
;
729 pend
= pkg_spec_parse_pkg(word
, &err
);
732 _("illegal package name in awaited trigger '%.255s': %s"),
735 if (!trig_note_aw(pend
, aw
))
737 _("duplicate awaited trigger package '%.255s'"), word
);
739 trig_awaited_pend_enqueue(pend
);