]>
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 <apt-pkg/tagfile.h> | |
16 | #include <apt-pkg/error.h> | |
17 | #include <apt-pkg/configuration.h> | |
18 | #include <apt-pkg/cmndline.h> | |
19 | #include <apt-pkg/init.h> | |
20 | #include <apt-pkg/strutl.h> | |
21 | ||
22 | #include <config.h> | |
23 | #include <apti18n.h> | |
24 | ||
25 | #include <vector> | |
26 | #include <algorithm> | |
27 | ||
28 | #include <locale.h> | |
29 | #include <unistd.h> | |
30 | /*}}}*/ | |
31 | ||
32 | using namespace std; | |
33 | ||
34 | struct PkgName | |
35 | { | |
36 | string Name; | |
37 | string Ver; | |
38 | string Arch; | |
39 | unsigned long Offset; | |
40 | unsigned long Length; | |
41 | ||
42 | inline int Compare3(const PkgName &x) const | |
43 | { | |
44 | int A = stringcasecmp(Name,x.Name); | |
45 | if (A == 0) | |
46 | { | |
47 | A = stringcasecmp(Ver,x.Ver); | |
48 | if (A == 0) | |
49 | A = stringcasecmp(Arch,x.Arch); | |
50 | } | |
51 | return A; | |
52 | } | |
53 | ||
54 | bool operator <(const PkgName &x) const {return Compare3(x) < 0;}; | |
55 | bool operator >(const PkgName &x) const {return Compare3(x) > 0;}; | |
56 | bool operator ==(const PkgName &x) const {return Compare3(x) == 0;}; | |
57 | }; | |
58 | ||
59 | // DoIt - Sort a single file /*{{{*/ | |
60 | // --------------------------------------------------------------------- | |
61 | /* */ | |
62 | bool DoIt(string InFile) | |
63 | { | |
64 | FileFd Fd(InFile,FileFd::ReadOnly); | |
65 | pkgTagFile Tags(&Fd); | |
66 | if (_error->PendingError() == true) | |
67 | return false; | |
68 | ||
69 | // Parse. | |
70 | vector<PkgName> List; | |
71 | pkgTagSection Section; | |
72 | unsigned long Largest = 0; | |
73 | unsigned long Offset = Tags.Offset(); | |
74 | bool Source = _config->FindB("APT::SortPkgs::Source",false); | |
75 | while (Tags.Step(Section) == true) | |
76 | { | |
77 | PkgName Tmp; | |
78 | ||
79 | /* Fetch the name, auto-detecting if this is a source file or a | |
80 | package file */ | |
81 | Tmp.Name = Section.FindS("Package"); | |
82 | Tmp.Ver = Section.FindS("Version"); | |
83 | Tmp.Arch = Section.FindS("Architecture"); | |
84 | ||
85 | if (Tmp.Name.empty() == true) | |
86 | return _error->Error(_("Unknown package record!")); | |
87 | ||
88 | Tmp.Offset = Offset; | |
89 | Tmp.Length = Section.size(); | |
90 | if (Largest < Tmp.Length) | |
91 | Largest = Tmp.Length; | |
92 | ||
93 | List.push_back(Tmp); | |
94 | ||
95 | Offset = Tags.Offset(); | |
96 | } | |
97 | if (_error->PendingError() == true) | |
98 | return false; | |
99 | ||
100 | // Sort it | |
101 | sort(List.begin(),List.end()); | |
102 | ||
103 | const char **Order = TFRewritePackageOrder; | |
104 | if (Source == true) | |
105 | Order = TFRewriteSourceOrder; | |
106 | ||
107 | // Emit | |
108 | unsigned char *Buffer = new unsigned char[Largest+1]; | |
109 | for (vector<PkgName>::iterator I = List.begin(); I != List.end(); I++) | |
110 | { | |
111 | // Read in the Record. | |
112 | if (Fd.Seek(I->Offset) == false || Fd.Read(Buffer,I->Length) == false) | |
113 | { | |
114 | delete [] Buffer; | |
115 | return false; | |
116 | } | |
117 | ||
118 | Buffer[I->Length] = '\n'; | |
119 | if (Section.Scan((char *)Buffer,I->Length+1) == false) | |
120 | { | |
121 | delete [] Buffer; | |
122 | return _error->Error("Internal error, failed to scan buffer"); | |
123 | } | |
124 | ||
125 | // Sort the section | |
126 | if (TFRewrite(stdout,Section,Order,0) == false) | |
127 | { | |
128 | delete [] Buffer; | |
129 | return _error->Error("Internal error, failed to sort fields"); | |
130 | } | |
131 | ||
132 | fputc('\n',stdout); | |
133 | } | |
134 | ||
135 | delete [] Buffer; | |
136 | return true; | |
137 | } | |
138 | /*}}}*/ | |
139 | // ShowHelp - Show the help text /*{{{*/ | |
140 | // --------------------------------------------------------------------- | |
141 | /* */ | |
142 | int ShowHelp() | |
143 | { | |
144 | ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION, | |
145 | COMMON_ARCH,__DATE__,__TIME__); | |
146 | if (_config->FindB("version") == true) | |
147 | return 0; | |
148 | ||
149 | cout << | |
150 | _("Usage: apt-sortpkgs [options] file1 [file2 ...]\n" | |
151 | "\n" | |
152 | "apt-sortpkgs is a simple tool to sort package files. The -s option is used\n" | |
153 | "to indicate what kind of file it is.\n" | |
154 | "\n" | |
155 | "Options:\n" | |
156 | " -h This help text\n" | |
157 | " -s Use source file sorting\n" | |
158 | " -c=? Read this configuration file\n" | |
159 | " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"); | |
160 | ||
161 | return 0; | |
162 | } | |
163 | /*}}}*/ | |
164 | ||
165 | int main(int argc,const char *argv[]) | |
166 | { | |
167 | CommandLine::Args Args[] = { | |
168 | {'h',"help","help",0}, | |
169 | {'v',"version","version",0}, | |
170 | {'s',"source","APT::SortPkgs::Source",0}, | |
171 | {'c',"config-file",0,CommandLine::ConfigFile}, | |
172 | {'o',"option",0,CommandLine::ArbItem}, | |
173 | {0,0,0,0}}; | |
174 | ||
175 | // Set up gettext support | |
176 | setlocale(LC_ALL,""); | |
177 | textdomain(PACKAGE); | |
178 | ||
179 | // Parse the command line and initialize the package library | |
180 | CommandLine CmdL(Args,_config); | |
181 | if (pkgInitConfig(*_config) == false || | |
182 | CmdL.Parse(argc,argv) == false || | |
183 | pkgInitSystem(*_config,_system) == false) | |
184 | { | |
185 | _error->DumpErrors(); | |
186 | return 100; | |
187 | } | |
188 | ||
189 | // See if the help should be shown | |
190 | if (_config->FindB("help") == true || | |
191 | CmdL.FileSize() == 0) | |
192 | return ShowHelp(); | |
193 | ||
194 | // Match the operation | |
195 | for (unsigned int I = 0; I != CmdL.FileSize(); I++) | |
196 | if (DoIt(CmdL.FileList[I]) == false) | |
197 | break; | |
198 | ||
199 | // Print any errors or warnings found during parsing | |
200 | if (_error->empty() == false) | |
201 | { | |
202 | bool Errors = _error->PendingError(); | |
203 | _error->DumpErrors(); | |
204 | return Errors == true?100:0; | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } |