1 /* 2 * Copyright 2011, Oliver Tappe <zooey@hirschkaefer.de> 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <ctype.h> 8 #include <errno.h> 9 #include <getopt.h> 10 #include <stdio.h> 11 #include <stdlib.h> 12 #include <string.h> 13 14 #include <package/hpkg/PackageInfoAttributeValue.h> 15 #include <package/hpkg/RepositoryContentHandler.h> 16 #include <package/hpkg/RepositoryReader.h> 17 #include <package/hpkg/StandardErrorOutput.h> 18 #include <package/PackageInfo.h> 19 #include <package/RepositoryInfo.h> 20 21 #include "package_repo.h" 22 23 24 using namespace BPackageKit::BHPKG; 25 using namespace BPackageKit; 26 27 struct RepositoryContentListHandler : BRepositoryContentHandler { 28 RepositoryContentListHandler(bool verbose) 29 : 30 fLevel(0), 31 fVerbose(verbose) 32 { 33 } 34 35 virtual status_t HandleEntry(BPackageEntry* entry) 36 { 37 return B_OK; 38 } 39 40 virtual status_t HandleEntryAttribute(BPackageEntry* entry, 41 BPackageEntryAttribute* attribute) 42 { 43 return B_OK; 44 } 45 46 virtual status_t HandleEntryDone(BPackageEntry* entry) 47 { 48 return B_OK; 49 } 50 51 virtual status_t HandlePackageAttribute( 52 const BPackageInfoAttributeValue& value) 53 { 54 switch (value.attributeID) { 55 case B_PACKAGE_INFO_NAME: 56 if (fVerbose) { 57 printf("package-attributes:\n"); 58 printf("\tname: %s\n", value.string); 59 } else 60 printf("package: %s", value.string); 61 break; 62 63 case B_PACKAGE_INFO_SUMMARY: 64 if (fVerbose) 65 printf("\tsummary: %s\n", value.string); 66 break; 67 68 case B_PACKAGE_INFO_DESCRIPTION: 69 if (fVerbose) 70 printf("\tdescription: %s\n", value.string); 71 break; 72 73 case B_PACKAGE_INFO_VENDOR: 74 if (fVerbose) 75 printf("\tvendor: %s\n", value.string); 76 break; 77 78 case B_PACKAGE_INFO_PACKAGER: 79 if (fVerbose) 80 printf("\tpackager: %s\n", value.string); 81 break; 82 83 case B_PACKAGE_INFO_FLAGS: 84 if (value.unsignedInt == 0 || !fVerbose) 85 break; 86 printf("\tflags:\n"); 87 if ((value.unsignedInt & B_PACKAGE_FLAG_APPROVE_LICENSE) != 0) 88 printf("\t\tapprove_license\n"); 89 if ((value.unsignedInt & B_PACKAGE_FLAG_SYSTEM_PACKAGE) != 0) 90 printf("\t\tsystem_package\n"); 91 break; 92 93 case B_PACKAGE_INFO_ARCHITECTURE: 94 if (fVerbose) { 95 printf("\tarchitecture: %s\n", 96 BPackageInfo::kArchitectureNames[value.unsignedInt]); 97 } 98 break; 99 100 case B_PACKAGE_INFO_VERSION: 101 if (!fVerbose) 102 printf("("); 103 _PrintPackageVersion(value.version); 104 if (!fVerbose) 105 printf(")\n"); 106 break; 107 108 case B_PACKAGE_INFO_COPYRIGHTS: 109 if (fVerbose) 110 printf("\tcopyright: %s\n", value.string); 111 break; 112 113 case B_PACKAGE_INFO_LICENSES: 114 if (fVerbose) 115 printf("\tlicense: %s\n", value.string); 116 break; 117 118 case B_PACKAGE_INFO_URLS: 119 if (fVerbose) 120 printf("\tURL: %s\n", value.string); 121 break; 122 123 case B_PACKAGE_INFO_SOURCE_URLS: 124 if (fVerbose) 125 printf("\tsource URL: %s\n", value.string); 126 break; 127 128 case B_PACKAGE_INFO_PROVIDES: 129 if (!fVerbose) 130 break; 131 printf("\tprovides: %s", value.resolvable.name); 132 if (value.resolvable.haveVersion) { 133 printf(" = "); 134 _PrintPackageVersion(value.resolvable.version); 135 } 136 printf("\n"); 137 break; 138 139 case B_PACKAGE_INFO_REQUIRES: 140 if (!fVerbose) 141 break; 142 printf("\trequires: %s", value.resolvableExpression.name); 143 if (value.resolvableExpression.haveOpAndVersion) { 144 printf(" %s ", BPackageResolvableExpression::kOperatorNames[ 145 value.resolvableExpression.op]); 146 _PrintPackageVersion(value.resolvableExpression.version); 147 } 148 printf("\n"); 149 break; 150 151 case B_PACKAGE_INFO_SUPPLEMENTS: 152 if (!fVerbose) 153 break; 154 printf("\tsupplements: %s", value.resolvableExpression.name); 155 if (value.resolvableExpression.haveOpAndVersion) { 156 printf(" %s ", BPackageResolvableExpression::kOperatorNames[ 157 value.resolvableExpression.op]); 158 _PrintPackageVersion(value.resolvableExpression.version); 159 } 160 printf("\n"); 161 break; 162 163 case B_PACKAGE_INFO_CONFLICTS: 164 if (!fVerbose) 165 break; 166 printf("\tconflicts: %s", value.resolvableExpression.name); 167 if (value.resolvableExpression.haveOpAndVersion) { 168 printf(" %s ", BPackageResolvableExpression::kOperatorNames[ 169 value.resolvableExpression.op]); 170 _PrintPackageVersion(value.resolvableExpression.version); 171 } 172 printf("\n"); 173 break; 174 175 case B_PACKAGE_INFO_FRESHENS: 176 if (!fVerbose) 177 break; 178 printf("\tfreshens: %s", value.resolvableExpression.name); 179 if (value.resolvableExpression.haveOpAndVersion) { 180 printf(" %s ", BPackageResolvableExpression::kOperatorNames[ 181 value.resolvableExpression.op]); 182 _PrintPackageVersion(value.resolvableExpression.version); 183 } 184 printf("\n"); 185 break; 186 187 case B_PACKAGE_INFO_REPLACES: 188 if (!fVerbose) 189 break; 190 printf("\treplaces: %s\n", value.string); 191 break; 192 193 case B_PACKAGE_INFO_CHECKSUM: 194 printf("\tchecksum: %s\n", value.string); 195 break; 196 197 default: 198 printf( 199 "*** Invalid package attribute section: unexpected " 200 "package attribute id %d encountered\n", value.attributeID); 201 return B_BAD_DATA; 202 } 203 204 return B_OK; 205 } 206 207 virtual status_t HandleRepositoryInfo(const BRepositoryInfo& repositoryInfo) 208 { 209 printf("repository-info:\n"); 210 printf("\tname: %s\n", repositoryInfo.Name().String()); 211 printf("\tsummary: %s\n", repositoryInfo.Summary().String()); 212 printf("\turl: %s\n", repositoryInfo.OriginalBaseURL().String()); 213 printf("\tvendor: %s\n", repositoryInfo.Vendor().String()); 214 printf("\tpriority: %u\n", repositoryInfo.Priority()); 215 printf("\tarchitecture: %s\n", 216 BPackageInfo::kArchitectureNames[repositoryInfo.Architecture()]); 217 const BStringList licenseNames = repositoryInfo.LicenseNames(); 218 if (!licenseNames.IsEmpty()) { 219 printf("\tlicenses:\n"); 220 for (int i = 0; i < licenseNames.CountStrings(); ++i) 221 printf("\t\t%s\n", licenseNames.StringAt(i).String()); 222 } 223 224 return B_OK; 225 } 226 227 virtual void HandleErrorOccurred() 228 { 229 } 230 231 private: 232 static void _PrintPackageVersion(const BPackageVersionData& version) 233 { 234 printf("%s", BPackageVersion(version).ToString().String()); 235 } 236 237 private: 238 int fLevel; 239 bool fVerbose; 240 }; 241 242 243 int 244 command_list(int argc, const char* const* argv) 245 { 246 bool verbose = false; 247 248 while (true) { 249 static struct option sLongOptions[] = { 250 { "help", no_argument, 0, 'h' }, 251 { "verbose", no_argument, 0, 'v' }, 252 { 0, 0, 0, 0 } 253 }; 254 255 opterr = 0; // don't print errors 256 int c = getopt_long(argc, (char**)argv, "+hv", sLongOptions, NULL); 257 if (c == -1) 258 break; 259 260 switch (c) { 261 case 'h': 262 print_usage_and_exit(false); 263 break; 264 265 case 'v': 266 verbose = true; 267 break; 268 269 default: 270 print_usage_and_exit(true); 271 break; 272 } 273 } 274 275 // One argument should remain -- the repository file name. 276 if (optind + 1 != argc) 277 print_usage_and_exit(true); 278 279 const char* repositoryFileName = argv[optind++]; 280 281 // open repository 282 BStandardErrorOutput errorOutput; 283 BRepositoryReader repositoryReader(&errorOutput); 284 status_t error = repositoryReader.Init(repositoryFileName); 285 if (error != B_OK) 286 return 1; 287 288 // list 289 RepositoryContentListHandler handler(verbose); 290 error = repositoryReader.ParseContent(&handler); 291 if (error != B_OK) 292 return 1; 293 294 return 0; 295 } 296