]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: apt-sortpkgs.cc,v 1.5 2003/01/11 07:18:44 jgg Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | APT Sort Packages - Program to sort Package and Source files | |
7 | ||
8 | This program is quite simple, it just sorts the package files by | |
9 | package and sorts the fields inside by the internal APT sort order. | |
10 | Input is taken from a named file and sent to stdout. | |
11 | ||
12 | ##################################################################### */ | |
13 | /*}}}*/ | |
14 | // Include Files /*{{{*/ | |
15 | #include <config.h> | |
16 | ||
17 | #include <apt-pkg/tagfile.h> | |
18 | #include <apt-pkg/error.h> | |
19 | #include <apt-pkg/configuration.h> | |
20 | #include <apt-pkg/cmndline.h> | |
21 | #include <apt-pkg/init.h> | |
22 | #include <apt-pkg/strutl.h> | |
23 | #include <apt-pkg/fileutl.h> | |
24 | #include <apt-pkg/pkgsystem.h> | |
25 | ||
26 | #include <apt-private/private-cmndline.h> | |
27 | ||
28 | #include <vector> | |
29 | #include <algorithm> | |
30 | #include <stdio.h> | |
31 | #include <iostream> | |
32 | #include <string> | |
33 | #include <memory> | |
34 | ||
35 | #include <apti18n.h> | |
36 | /*}}}*/ | |
37 | ||
38 | using namespace std; | |
39 | ||
40 | struct PkgName /*{{{*/ | |
41 | { | |
42 | string Name; | |
43 | string Ver; | |
44 | string Arch; | |
45 | unsigned long Offset; | |
46 | unsigned long Length; | |
47 | ||
48 | inline int Compare3(const PkgName &x) const | |
49 | { | |
50 | int A = stringcasecmp(Name,x.Name); | |
51 | if (A == 0) | |
52 | { | |
53 | A = stringcasecmp(Ver,x.Ver); | |
54 | if (A == 0) | |
55 | A = stringcasecmp(Arch,x.Arch); | |
56 | } | |
57 | return A; | |
58 | } | |
59 | ||
60 | bool operator <(const PkgName &x) const {return Compare3(x) < 0;}; | |
61 | bool operator >(const PkgName &x) const {return Compare3(x) > 0;}; | |
62 | bool operator ==(const PkgName &x) const {return Compare3(x) == 0;}; | |
63 | }; | |
64 | /*}}}*/ | |
65 | // DoIt - Sort a single file /*{{{*/ | |
66 | // --------------------------------------------------------------------- | |
67 | /* */ | |
68 | static bool DoIt(string InFile) | |
69 | { | |
70 | FileFd Fd(InFile,FileFd::ReadOnly); | |
71 | pkgTagFile Tags(&Fd); | |
72 | if (_error->PendingError() == true) | |
73 | return false; | |
74 | ||
75 | // Parse. | |
76 | vector<PkgName> List; | |
77 | pkgTagSection Section; | |
78 | unsigned long Largest = 0; | |
79 | unsigned long Offset = Tags.Offset(); | |
80 | bool Source = _config->FindB("APT::SortPkgs::Source",false); | |
81 | while (Tags.Step(Section) == true) | |
82 | { | |
83 | PkgName Tmp; | |
84 | ||
85 | /* Fetch the name, auto-detecting if this is a source file or a | |
86 | package file */ | |
87 | Tmp.Name = Section.FindS("Package"); | |
88 | Tmp.Ver = Section.FindS("Version"); | |
89 | Tmp.Arch = Section.FindS("Architecture"); | |
90 | ||
91 | if (Tmp.Name.empty() == true) | |
92 | return _error->Error(_("Unknown package record!")); | |
93 | ||
94 | Tmp.Offset = Offset; | |
95 | Tmp.Length = Section.size(); | |
96 | if (Largest < Tmp.Length) | |
97 | Largest = Tmp.Length; | |
98 | ||
99 | List.push_back(Tmp); | |
100 | ||
101 | Offset = Tags.Offset(); | |
102 | } | |
103 | if (_error->PendingError() == true) | |
104 | return false; | |
105 | ||
106 | // Sort it | |
107 | sort(List.begin(),List.end()); | |
108 | ||
109 | const char **Order = TFRewritePackageOrder; | |
110 | if (Source == true) | |
111 | Order = TFRewriteSourceOrder; | |
112 | ||
113 | // Emit | |
114 | FileFd stdoutfd; | |
115 | stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false); | |
116 | auto const Buffer = std::unique_ptr<unsigned char[]>(new unsigned char[Largest+1]); | |
117 | for (vector<PkgName>::iterator I = List.begin(); I != List.end(); ++I) | |
118 | { | |
119 | // Read in the Record. | |
120 | if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer.get(),I->Length) == false) | |
121 | return false; | |
122 | ||
123 | Buffer[I->Length] = '\n'; | |
124 | if (Section.Scan((char *)Buffer.get(),I->Length+1) == false) | |
125 | return _error->Error("Internal error, failed to scan buffer"); | |
126 | ||
127 | // Sort the section | |
128 | if (Section.Write(stdoutfd, Order) == false || stdoutfd.Write("\n", 1) == false) | |
129 | return _error->Error("Internal error, failed to sort fields"); | |
130 | } | |
131 | return true; | |
132 | } | |
133 | /*}}}*/ | |
134 | // ShowHelp - Show the help text /*{{{*/ | |
135 | // --------------------------------------------------------------------- | |
136 | /* */ | |
137 | static bool ShowHelp(CommandLine &) | |
138 | { | |
139 | ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH); | |
140 | if (_config->FindB("version") == true) | |
141 | return true; | |
142 | ||
143 | cout << | |
144 | _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n" | |
145 | "\n" | |
146 | "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n" | |
147 | "to indicate what kind of file it is.\n" | |
148 | "\n" | |
149 | "Options:\n" | |
150 | " -h This help text\n" | |
151 | " -s Use source file sorting\n" | |
152 | " -c=? Read this configuration file\n" | |
153 | " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"); | |
154 | ||
155 | return true; | |
156 | } | |
157 | /*}}}*/ | |
158 | int main(int argc,const char *argv[]) /*{{{*/ | |
159 | { | |
160 | CommandLine::Args Args[] = { | |
161 | {'h',"help","help",0}, | |
162 | {'v',"version","version",0}, | |
163 | {'s',"source","APT::SortPkgs::Source",0}, | |
164 | {'c',"config-file",0,CommandLine::ConfigFile}, | |
165 | {'o',"option",0,CommandLine::ArbItem}, | |
166 | {0,0,0,0}}; | |
167 | ||
168 | // Set up gettext support | |
169 | setlocale(LC_ALL,""); | |
170 | textdomain(PACKAGE); | |
171 | ||
172 | // Parse the command line and initialize the package library | |
173 | CommandLine::Dispatch Cmds[] = {{NULL, NULL}}; | |
174 | CommandLine CmdL; | |
175 | ParseCommandLine(CmdL, Cmds, Args, &_config, &_system, argc, argv, ShowHelp); | |
176 | ||
177 | // Match the operation | |
178 | for (unsigned int I = 0; I != CmdL.FileSize(); I++) | |
179 | if (DoIt(CmdL.FileList[I]) == false) | |
180 | break; | |
181 | ||
182 | // Print any errors or warnings found during parsing | |
183 | if (_error->empty() == false) | |
184 | { | |
185 | bool Errors = _error->PendingError(); | |
186 | _error->DumpErrors(); | |
187 | return Errors == true?100:0; | |
188 | } | |
189 | ||
190 | return 0; | |
191 | } | |
192 | /*}}}*/ |