FieldsCore.pm 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. # Copyright © 2007-2009 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. package Dpkg::Control::FieldsCore;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.00';
  19. use Exporter qw(import);
  20. use Dpkg::Gettext;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Control::Types;
  23. use Dpkg::Checksums;
  24. our @EXPORT = qw(field_capitalize field_is_official field_is_allowed_in
  25. field_transfer_single field_transfer_all
  26. field_list_src_dep field_list_pkg_dep field_get_dep_type
  27. field_get_sep_type
  28. field_ordered_list field_register
  29. field_insert_after field_insert_before
  30. FIELD_SEP_UNKNOWN FIELD_SEP_SPACE FIELD_SEP_COMMA
  31. FIELD_SEP_LINE);
  32. use constant {
  33. ALL_PKG => CTRL_INFO_PKG | CTRL_INDEX_PKG | CTRL_PKG_DEB | CTRL_FILE_STATUS,
  34. ALL_SRC => CTRL_INFO_SRC | CTRL_INDEX_SRC | CTRL_PKG_SRC,
  35. ALL_CHANGES => CTRL_FILE_CHANGES | CTRL_CHANGELOG,
  36. };
  37. use constant {
  38. FIELD_SEP_UNKNOWN => 0,
  39. FIELD_SEP_SPACE => 1,
  40. FIELD_SEP_COMMA => 2,
  41. FIELD_SEP_LINE => 4,
  42. };
  43. # The canonical list of fields
  44. # Note that fields used only in dpkg's available file are not listed
  45. # Deprecated fields of dpkg's status file are also not listed
  46. our %FIELDS = (
  47. 'Architecture' => {
  48. allowed => (ALL_PKG | ALL_SRC | CTRL_FILE_CHANGES) & (~CTRL_INFO_SRC),
  49. separator => FIELD_SEP_SPACE,
  50. },
  51. 'Binary' => {
  52. allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES,
  53. # XXX: This field values are separated either by space or comma
  54. # depending on the context.
  55. separator => FIELD_SEP_SPACE | FIELD_SEP_COMMA,
  56. },
  57. 'Binary-Only' => {
  58. allowed => ALL_CHANGES,
  59. },
  60. 'Breaks' => {
  61. allowed => ALL_PKG,
  62. separator => FIELD_SEP_COMMA,
  63. dependency => 'union',
  64. dep_order => 7,
  65. },
  66. 'Bugs' => {
  67. allowed => (ALL_PKG | CTRL_INFO_SRC | CTRL_FILE_VENDOR) & (~CTRL_INFO_PKG),
  68. },
  69. 'Build-Conflicts' => {
  70. allowed => ALL_SRC,
  71. separator => FIELD_SEP_COMMA,
  72. dependency => 'union',
  73. dep_order => 4,
  74. },
  75. 'Build-Conflicts-Arch' => {
  76. allowed => ALL_SRC,
  77. separator => FIELD_SEP_COMMA,
  78. dependency => 'union',
  79. dep_order => 5,
  80. },
  81. 'Build-Conflicts-Indep' => {
  82. allowed => ALL_SRC,
  83. separator => FIELD_SEP_COMMA,
  84. dependency => 'union',
  85. dep_order => 6,
  86. },
  87. 'Build-Depends' => {
  88. allowed => ALL_SRC,
  89. separator => FIELD_SEP_COMMA,
  90. dependency => 'normal',
  91. dep_order => 1,
  92. },
  93. 'Build-Depends-Arch' => {
  94. allowed => ALL_SRC,
  95. separator => FIELD_SEP_COMMA,
  96. dependency => 'normal',
  97. dep_order => 2,
  98. },
  99. 'Build-Depends-Indep' => {
  100. allowed => ALL_SRC,
  101. separator => FIELD_SEP_COMMA,
  102. dependency => 'normal',
  103. dep_order => 3,
  104. },
  105. 'Built-For-Profiles' => {
  106. allowed => ALL_PKG | CTRL_FILE_CHANGES,
  107. separator => FIELD_SEP_SPACE,
  108. },
  109. 'Built-Using' => {
  110. allowed => ALL_PKG,
  111. separator => FIELD_SEP_COMMA,
  112. dependency => 'union',
  113. dep_order => 10,
  114. },
  115. 'Changed-By' => {
  116. allowed => CTRL_FILE_CHANGES,
  117. },
  118. 'Changes' => {
  119. allowed => ALL_CHANGES,
  120. },
  121. 'Closes' => {
  122. allowed => ALL_CHANGES,
  123. separator => FIELD_SEP_SPACE,
  124. },
  125. 'Conffiles' => {
  126. allowed => CTRL_FILE_STATUS,
  127. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  128. },
  129. 'Config-Version' => {
  130. allowed => CTRL_FILE_STATUS,
  131. },
  132. 'Conflicts' => {
  133. allowed => ALL_PKG,
  134. separator => FIELD_SEP_COMMA,
  135. dependency => 'union',
  136. dep_order => 6,
  137. },
  138. 'Date' => {
  139. allowed => ALL_CHANGES,
  140. },
  141. 'Depends' => {
  142. allowed => ALL_PKG,
  143. separator => FIELD_SEP_COMMA,
  144. dependency => 'normal',
  145. dep_order => 2,
  146. },
  147. 'Description' => {
  148. allowed => ALL_PKG | CTRL_FILE_CHANGES,
  149. },
  150. 'Directory' => {
  151. allowed => CTRL_INDEX_SRC,
  152. },
  153. 'Distribution' => {
  154. allowed => ALL_CHANGES,
  155. },
  156. 'Enhances' => {
  157. allowed => ALL_PKG,
  158. separator => FIELD_SEP_COMMA,
  159. dependency => 'union',
  160. dep_order => 5,
  161. },
  162. 'Essential' => {
  163. allowed => ALL_PKG,
  164. },
  165. 'Filename' => {
  166. allowed => CTRL_INDEX_PKG,
  167. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  168. },
  169. 'Files' => {
  170. allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES,
  171. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  172. },
  173. 'Format' => {
  174. allowed => CTRL_PKG_SRC | CTRL_FILE_CHANGES,
  175. },
  176. 'Homepage' => {
  177. allowed => ALL_SRC | ALL_PKG,
  178. },
  179. 'Installed-Size' => {
  180. allowed => ALL_PKG & ~CTRL_INFO_PKG,
  181. },
  182. 'Installer-Menu-Item' => {
  183. allowed => ALL_PKG,
  184. },
  185. 'Kernel-Version' => {
  186. allowed => ALL_PKG,
  187. },
  188. 'Origin' => {
  189. allowed => (ALL_PKG | ALL_SRC) & (~CTRL_INFO_PKG),
  190. },
  191. 'Maintainer' => {
  192. allowed => CTRL_PKG_DEB | ALL_SRC | ALL_CHANGES,
  193. },
  194. 'Multi-Arch' => {
  195. allowed => ALL_PKG,
  196. },
  197. 'Package' => {
  198. allowed => ALL_PKG,
  199. },
  200. 'Package-List' => {
  201. allowed => ALL_SRC & ~CTRL_INFO_SRC,
  202. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  203. },
  204. 'Package-Type' => {
  205. allowed => ALL_PKG,
  206. },
  207. 'Parent' => {
  208. allowed => CTRL_FILE_VENDOR,
  209. },
  210. 'Pre-Depends' => {
  211. allowed => ALL_PKG,
  212. separator => FIELD_SEP_COMMA,
  213. dependency => 'normal',
  214. dep_order => 1,
  215. },
  216. 'Priority' => {
  217. allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
  218. },
  219. 'Provides' => {
  220. allowed => ALL_PKG,
  221. separator => FIELD_SEP_COMMA,
  222. dependency => 'union',
  223. dep_order => 9,
  224. },
  225. 'Recommends' => {
  226. allowed => ALL_PKG,
  227. separator => FIELD_SEP_COMMA,
  228. dependency => 'normal',
  229. dep_order => 3,
  230. },
  231. 'Replaces' => {
  232. allowed => ALL_PKG,
  233. separator => FIELD_SEP_COMMA,
  234. dependency => 'union',
  235. dep_order => 8,
  236. },
  237. 'Section' => {
  238. allowed => CTRL_INFO_SRC | CTRL_INDEX_SRC | ALL_PKG,
  239. },
  240. 'Size' => {
  241. allowed => CTRL_INDEX_PKG,
  242. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE,
  243. },
  244. 'Source' => {
  245. allowed => (ALL_PKG | ALL_SRC | ALL_CHANGES) &
  246. (~(CTRL_INDEX_SRC | CTRL_INFO_PKG)),
  247. },
  248. 'Standards-Version' => {
  249. allowed => ALL_SRC,
  250. },
  251. 'Status' => {
  252. allowed => CTRL_FILE_STATUS,
  253. separator => FIELD_SEP_SPACE,
  254. },
  255. 'Subarchitecture' => {
  256. allowed => ALL_PKG,
  257. },
  258. 'Suggests' => {
  259. allowed => ALL_PKG,
  260. separator => FIELD_SEP_COMMA,
  261. dependency => 'normal',
  262. dep_order => 4,
  263. },
  264. 'Tag' => {
  265. allowed => ALL_PKG,
  266. separator => FIELD_SEP_COMMA,
  267. },
  268. 'Task' => {
  269. allowed => ALL_PKG,
  270. },
  271. 'Testsuite' => {
  272. allowed => ALL_SRC,
  273. separator => FIELD_SEP_COMMA,
  274. },
  275. 'Triggers-Awaited' => {
  276. allowed => CTRL_FILE_STATUS,
  277. separator => FIELD_SEP_SPACE,
  278. },
  279. 'Triggers-Pending' => {
  280. allowed => CTRL_FILE_STATUS,
  281. separator => FIELD_SEP_SPACE,
  282. },
  283. 'Uploaders' => {
  284. allowed => ALL_SRC,
  285. separator => FIELD_SEP_COMMA,
  286. },
  287. 'Urgency' => {
  288. allowed => ALL_CHANGES,
  289. },
  290. 'Vcs-Browser' => {
  291. allowed => ALL_SRC,
  292. },
  293. 'Vcs-Arch' => {
  294. allowed => ALL_SRC,
  295. },
  296. 'Vcs-Bzr' => {
  297. allowed => ALL_SRC,
  298. },
  299. 'Vcs-Cvs' => {
  300. allowed => ALL_SRC,
  301. },
  302. 'Vcs-Darcs' => {
  303. allowed => ALL_SRC,
  304. },
  305. 'Vcs-Git' => {
  306. allowed => ALL_SRC,
  307. },
  308. 'Vcs-Hg' => {
  309. allowed => ALL_SRC,
  310. },
  311. 'Vcs-Mtn' => {
  312. allowed => ALL_SRC,
  313. },
  314. 'Vcs-Svn' => {
  315. allowed => ALL_SRC,
  316. },
  317. 'Vendor' => {
  318. allowed => CTRL_FILE_VENDOR,
  319. },
  320. 'Vendor-Url' => {
  321. allowed => CTRL_FILE_VENDOR,
  322. },
  323. 'Version' => {
  324. allowed => (ALL_PKG | ALL_SRC | ALL_CHANGES) &
  325. (~(CTRL_INFO_SRC | CTRL_INFO_PKG)),
  326. },
  327. );
  328. my @checksum_fields = map { &field_capitalize("Checksums-$_") } checksums_get_list();
  329. my @sum_fields = map { $_ eq 'md5' ? 'MD5sum' : &field_capitalize($_) }
  330. checksums_get_list();
  331. &field_register($_, CTRL_PKG_SRC | CTRL_FILE_CHANGES) foreach @checksum_fields;
  332. &field_register($_, CTRL_INDEX_PKG,
  333. separator => FIELD_SEP_LINE | FIELD_SEP_SPACE) foreach @sum_fields;
  334. our %FIELD_ORDER = (
  335. CTRL_PKG_DEB() => [
  336. qw(Package Package-Type Source Version Built-Using Kernel-Version
  337. Built-For-Profiles Architecture Subarchitecture
  338. Installer-Menu-Item Essential Origin Bugs
  339. Maintainer Installed-Size), &field_list_pkg_dep(),
  340. qw(Section Priority Multi-Arch Homepage Description Tag Task)
  341. ],
  342. CTRL_PKG_SRC() => [
  343. qw(Format Source Binary Architecture Version Origin Maintainer
  344. Uploaders Homepage Standards-Version Vcs-Browser
  345. Vcs-Arch Vcs-Bzr Vcs-Cvs Vcs-Darcs Vcs-Git Vcs-Hg Vcs-Mtn
  346. Vcs-Svn Testsuite), &field_list_src_dep(), qw(Package-List),
  347. @checksum_fields, qw(Files)
  348. ],
  349. CTRL_FILE_CHANGES() => [
  350. qw(Format Date Source Binary Binary-Only Built-For-Profiles Architecture
  351. Version Distribution Urgency Maintainer Changed-By Description
  352. Closes Changes),
  353. @checksum_fields, qw(Files)
  354. ],
  355. CTRL_CHANGELOG() => [
  356. qw(Source Binary-Only Version Distribution Urgency Maintainer
  357. Date Closes Changes)
  358. ],
  359. CTRL_FILE_STATUS() => [ # Same as fieldinfos in lib/dpkg/parse.c
  360. qw(Package Essential Status Priority Section Installed-Size Origin
  361. Maintainer Bugs Architecture Multi-Arch Source Version Config-Version
  362. Replaces Provides Depends Pre-Depends Recommends Suggests Breaks
  363. Conflicts Enhances Conffiles Description Triggers-Pending
  364. Triggers-Awaited)
  365. ],
  366. );
  367. # Order for CTRL_INDEX_PKG is derived from CTRL_PKG_DEB
  368. $FIELD_ORDER{CTRL_INDEX_PKG()} = [ @{$FIELD_ORDER{CTRL_PKG_DEB()}} ];
  369. &field_insert_before(CTRL_INDEX_PKG, 'Section', 'Filename', 'Size', @sum_fields);
  370. # Order for CTRL_INDEX_SRC is derived from CTRL_PKG_SRC
  371. $FIELD_ORDER{CTRL_INDEX_SRC()} = [ @{$FIELD_ORDER{CTRL_PKG_SRC()}} ];
  372. @{$FIELD_ORDER{CTRL_INDEX_SRC()}} = map { $_ eq 'Source' ? 'Package' : $_ }
  373. @{$FIELD_ORDER{CTRL_PKG_SRC()}};
  374. &field_insert_after(CTRL_INDEX_SRC, 'Version', 'Priority', 'Section');
  375. &field_insert_before(CTRL_INDEX_SRC, 'Checksums-Md5', 'Directory');
  376. =encoding utf8
  377. =head1 NAME
  378. Dpkg::Control::FieldsCore - manage (list of official) control fields
  379. =head1 DESCRIPTION
  380. The modules contains a list of fieldnames with associated meta-data explaining
  381. in which type of control information they are allowed. The types are the
  382. CTRL_* constants exported by Dpkg::Control.
  383. =head1 FUNCTIONS
  384. =over 4
  385. =item my $f = field_capitalize($field_name)
  386. Returns the field name properly capitalized. All characters are lowercase,
  387. except the first of each word (words are separated by a hyphen in field names).
  388. =cut
  389. sub field_capitalize($) {
  390. my $field = lc(shift);
  391. # Some special cases due to history
  392. return 'MD5sum' if $field eq 'md5sum';
  393. return uc($field) if checksums_is_supported($field);
  394. # Generic case
  395. return join '-', map { ucfirst } split /-/, $field;
  396. }
  397. =item field_is_official($fname)
  398. Returns true if the field is official and known.
  399. =cut
  400. sub field_is_official($) {
  401. return exists $FIELDS{field_capitalize($_[0])};
  402. }
  403. =item field_is_allowed_in($fname, @types)
  404. Returns true (1) if the field $fname is allowed in all the types listed in
  405. the list. Note that you can use type sets instead of individual types (ex:
  406. CTRL_FILE_CHANGES | CTRL_CHANGELOG).
  407. field_allowed_in(A|B, C) returns true only if the field is allowed in C
  408. and either A or B.
  409. Undef is returned for non-official fields.
  410. =cut
  411. sub field_is_allowed_in($@) {
  412. my ($field, @types) = @_;
  413. $field = field_capitalize($field);
  414. return unless field_is_official($field);
  415. return 0 if not scalar(@types);
  416. foreach my $type (@types) {
  417. next if $type == CTRL_UNKNOWN; # Always allowed
  418. return 0 unless $FIELDS{$field}{allowed} & $type;
  419. }
  420. return 1;
  421. }
  422. =item field_transfer_single($from, $to, $field)
  423. If appropriate, copy the value of the field named $field taken from the
  424. $from Dpkg::Control object to the $to Dpkg::Control object.
  425. Official fields are copied only if the field is allowed in both types of
  426. objects. Custom fields are treated in a specific manner. When the target
  427. is not among CTRL_PKG_SRC, CTRL_PKG_DEB or CTRL_FILE_CHANGES, then they
  428. are alway copied as is (the X- prefix is kept). Otherwise they are not
  429. copied except if the target object matches the target destination encoded
  430. in the field name. The initial X denoting custom fields can be followed by
  431. one or more letters among "S" (Source: corresponds to CTRL_PKG_SRC), "B"
  432. (Binary: corresponds to CTRL_PKG_DEB) or "C" (Changes: corresponds to
  433. CTRL_FILE_CHANGES).
  434. Returns undef if nothing has been copied or the name of the new field
  435. added to $to otherwise.
  436. =cut
  437. sub field_transfer_single($$;$) {
  438. my ($from, $to, $field) = @_;
  439. $field //= $_;
  440. my ($from_type, $to_type) = ($from->get_type(), $to->get_type());
  441. $field = field_capitalize($field);
  442. if (field_is_allowed_in($field, $from_type, $to_type)) {
  443. $to->{$field} = $from->{$field};
  444. return $field;
  445. } elsif ($field =~ /^X([SBC]*)-/i) {
  446. my $dest = $1;
  447. if (($dest =~ /B/i and $to_type == CTRL_PKG_DEB) or
  448. ($dest =~ /S/i and $to_type == CTRL_PKG_SRC) or
  449. ($dest =~ /C/i and $to_type == CTRL_FILE_CHANGES))
  450. {
  451. my $new = $field;
  452. $new =~ s/^X([SBC]*)-//i;
  453. $to->{$new} = $from->{$field};
  454. return $new;
  455. } elsif ($to_type != CTRL_PKG_DEB and
  456. $to_type != CTRL_PKG_SRC and
  457. $to_type != CTRL_FILE_CHANGES)
  458. {
  459. $to->{$field} = $from->{$field};
  460. return $field;
  461. }
  462. } elsif (not field_is_allowed_in($field, $from_type)) {
  463. warning(_g("unknown information field '%s' in input data in %s"),
  464. $field, $from->get_option('name') || _g('control information'));
  465. }
  466. return;
  467. }
  468. =item field_transfer_all($from, $to)
  469. Transfer all appropriate fields from $from to $to. Calls
  470. field_transfer_single() on all fields available in $from.
  471. Returns the list of fields that have been added to $to.
  472. =cut
  473. sub field_transfer_all($$) {
  474. my ($from, $to) = @_;
  475. my (@res, $res);
  476. foreach my $k (keys %$from) {
  477. $res = field_transfer_single($from, $to, $k);
  478. push @res, $res if $res and defined wantarray;
  479. }
  480. return @res;
  481. }
  482. =item field_ordered_list($type)
  483. Returns an ordered list of fields for a given type of control information.
  484. This list can be used to output the fields in a predictable order.
  485. The list might be empty for types where the order does not matter much.
  486. =cut
  487. sub field_ordered_list($) {
  488. my ($type) = @_;
  489. return @{$FIELD_ORDER{$type}} if exists $FIELD_ORDER{$type};
  490. return ();
  491. }
  492. =item field_list_src_dep()
  493. List of fields that contains dependencies-like information in a source
  494. Debian package.
  495. =cut
  496. sub field_list_src_dep() {
  497. my @list = sort {
  498. $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
  499. } grep {
  500. field_is_allowed_in($_, CTRL_PKG_SRC) and
  501. exists $FIELDS{$_}{dependency}
  502. } keys %FIELDS;
  503. return @list;
  504. }
  505. =item field_list_pkg_dep()
  506. List of fields that contains dependencies-like information in a binary
  507. Debian package. The fields that express real dependencies are sorted from
  508. the stronger to the weaker.
  509. =cut
  510. sub field_list_pkg_dep() {
  511. my @keys = keys %FIELDS;
  512. my @list = sort {
  513. $FIELDS{$a}{dep_order} <=> $FIELDS{$b}{dep_order}
  514. } grep {
  515. field_is_allowed_in($_, CTRL_PKG_DEB) and
  516. exists $FIELDS{$_}{dependency}
  517. } @keys;
  518. return @list;
  519. }
  520. =item field_get_dep_type($field)
  521. Return the type of the dependency expressed by the given field. Can
  522. either be "normal" for a real dependency field (Pre-Depends, Depends, ...)
  523. or "union" for other relation fields sharing the same syntax (Conflicts,
  524. Breaks, ...). Returns undef for fields which are not dependencies.
  525. =cut
  526. sub field_get_dep_type($) {
  527. my $field = field_capitalize($_[0]);
  528. return unless field_is_official($field);
  529. return $FIELDS{$field}{dependency} if exists $FIELDS{$field}{dependency};
  530. return;
  531. }
  532. =item field_get_sep_type($field)
  533. Return the type of the field value separator. Can be one of FIELD_SEP_UNKNOWN,
  534. FIELD_SEP_SPACE, FIELD_SEP_COMMA or FIELD_SEP_LINE.
  535. =cut
  536. sub field_get_sep_type($) {
  537. my $field = field_capitalize($_[0]);
  538. return $FIELDS{$field}{separator} if exists $FIELDS{$field}{separator};
  539. return FIELD_SEP_UNKNOWN;
  540. }
  541. =item field_register($field, $allowed_types, %opts)
  542. Register a new field as being allowed in control information of specified
  543. types. %opts is optional
  544. =cut
  545. sub field_register($$;@) {
  546. my ($field, $types, %opts) = @_;
  547. $field = field_capitalize($field);
  548. $FIELDS{$field} = {
  549. allowed => $types,
  550. %opts
  551. };
  552. }
  553. =item field_insert_after($type, $ref, @fields)
  554. Place field after another one ($ref) in output of control information of
  555. type $type.
  556. =cut
  557. sub field_insert_after($$@) {
  558. my ($type, $field, @fields) = @_;
  559. return 0 if not exists $FIELD_ORDER{$type};
  560. ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
  561. @{$FIELD_ORDER{$type}} = map {
  562. ($_ eq $field) ? ($_, @fields) : $_
  563. } @{$FIELD_ORDER{$type}};
  564. return 1;
  565. }
  566. =item field_insert_before($type, $ref, @fields)
  567. Place field before another one ($ref) in output of control information of
  568. type $type.
  569. =cut
  570. sub field_insert_before($$@) {
  571. my ($type, $field, @fields) = @_;
  572. return 0 if not exists $FIELD_ORDER{$type};
  573. ($field, @fields) = map { field_capitalize($_) } ($field, @fields);
  574. @{$FIELD_ORDER{$type}} = map {
  575. ($_ eq $field) ? (@fields, $_) : $_
  576. } @{$FIELD_ORDER{$type}};
  577. return 1;
  578. }
  579. =back
  580. =head1 AUTHOR
  581. Raphaël Hertzog <hertzog@debian.org>.
  582. =cut
  583. 1;