dpkg-vendor.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-vendor
  4. #
  5. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  6. # Copyright © 2009,2012 Guillem Jover <guillem@debian.org>
  7. #
  8. # This program 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.
  12. #
  13. # This program 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.
  17. #
  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/>.
  20. use strict;
  21. use warnings;
  22. use Dpkg ();
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Vendor qw(get_vendor_dir get_vendor_info get_current_vendor);
  26. textdomain('dpkg-dev');
  27. sub version {
  28. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  29. printf g_('
  30. This is free software; see the GNU General Public License version 2 or
  31. later for copying conditions. There is NO warranty.
  32. ');
  33. }
  34. sub usage {
  35. printf g_(
  36. 'Usage: %s [<option>...] [<command>]')
  37. . "\n\n" . g_(
  38. 'Commands:
  39. --is <vendor> returns true if current vendor is <vendor>.
  40. --derives-from <vendor> returns true if current vendor derives from <vendor>.
  41. --query <field> print the content of the vendor-specific field.
  42. --help show this help message.
  43. --version show the version.')
  44. . "\n\n" . g_(
  45. 'Options:
  46. --vendor <vendor> assume <vendor> is the current vendor.')
  47. . "\n", $Dpkg::PROGNAME;
  48. }
  49. my ($vendor, $param, $action);
  50. while (@ARGV) {
  51. $_ = shift(@ARGV);
  52. if (m/^--vendor$/) {
  53. $vendor = shift(@ARGV);
  54. usageerr(g_('%s needs a parameter'), $_) unless defined $vendor;
  55. } elsif (m/^--(is|derives-from|query)$/) {
  56. usageerr(g_('two commands specified: --%s and --%s'), $1, $action)
  57. if defined($action);
  58. $action = $1;
  59. $param = shift(@ARGV);
  60. usageerr(g_('%s needs a parameter'), $_) unless defined $param;
  61. } elsif (m/^-(?:\?|-help)$/) {
  62. usage();
  63. exit 0;
  64. } elsif (m/^--version$/) {
  65. version();
  66. exit 0;
  67. } else {
  68. usageerr(g_("unknown option '%s'"), $_);
  69. }
  70. }
  71. usageerr(g_('need an action option')) unless defined($action);
  72. # Uses $ENV{DEB_VENDOR} if set
  73. $vendor //= get_current_vendor();
  74. my $info = get_vendor_info($vendor);
  75. unless (defined($info)) {
  76. error(g_("vendor %s doesn't exist in %s"), $vendor || 'default',
  77. get_vendor_dir());
  78. }
  79. if ($action eq 'is') {
  80. exit(0) if lc($param) eq lc($info->{'Vendor'});
  81. exit(1);
  82. } elsif ($action eq 'derives-from') {
  83. exit(0) if lc($param) eq lc($info->{'Vendor'});
  84. while (defined($info) && exists $info->{'Parent'}) {
  85. $info = get_vendor_info($info->{'Parent'});
  86. exit(0) if lc($param) eq lc($info->{'Vendor'});
  87. }
  88. exit(1);
  89. } elsif ($action eq 'query') {
  90. if (exists $info->{$param}) {
  91. print $info->{$param} . "\n";
  92. exit(0);
  93. } else {
  94. exit(1);
  95. }
  96. }