apt-cdrom.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cdrom.cc,v 1.28 1999/07/12 02:59:36 jgg Exp $
  4. /* ######################################################################
  5. APT CDROM - Tool for handling APT's CDROM database.
  6. Currently the only option is 'add' which will take the current CD
  7. in the drive and add it into the database.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <apt-pkg/cmndline.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/init.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/progress.h>
  16. #include <apt-pkg/cdromutl.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <config.h>
  19. #include "indexcopy.h"
  20. #include <iostream>
  21. #include <fstream>
  22. #include <vector>
  23. #include <algorithm>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <dirent.h>
  27. #include <unistd.h>
  28. #include <stdio.h>
  29. /*}}}*/
  30. // FindPackages - Find the package files on the CDROM /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* We look over the cdrom for package files. This is a recursive
  33. search that short circuits when it his a package file in the dir.
  34. This speeds it up greatly as the majority of the size is in the
  35. binary-* sub dirs. */
  36. bool FindPackages(string CD,vector<string> &List,vector<string> &SList,
  37. string &InfoDir,unsigned int Depth = 0)
  38. {
  39. static ino_t Inodes[9];
  40. if (Depth >= 7)
  41. return true;
  42. if (CD[CD.length()-1] != '/')
  43. CD += '/';
  44. if (chdir(CD.c_str()) != 0)
  45. return _error->Errno("chdir","Unable to change to %s",CD.c_str());
  46. // Look for a .disk subdirectory
  47. struct stat Buf;
  48. if (stat(".disk",&Buf) == 0)
  49. {
  50. if (InfoDir.empty() == true)
  51. InfoDir = CD + ".disk/";
  52. }
  53. /* Aha! We found some package files. We assume that everything under
  54. this dir is controlled by those package files so we don't look down
  55. anymore */
  56. if (stat("Packages",&Buf) == 0)
  57. {
  58. List.push_back(CD);
  59. // Continue down if thorough is given
  60. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  61. return true;
  62. }
  63. if (stat("Sources.gz",&Buf) == 0 || stat("Sources",&Buf) == 0)
  64. {
  65. SList.push_back(CD);
  66. // Continue down if thorough is given
  67. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  68. return true;
  69. }
  70. DIR *D = opendir(".");
  71. if (D == 0)
  72. return _error->Errno("opendir","Unable to read %s",CD.c_str());
  73. // Run over the directory
  74. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  75. {
  76. // Skip some files..
  77. if (strcmp(Dir->d_name,".") == 0 ||
  78. strcmp(Dir->d_name,"..") == 0 ||
  79. //strcmp(Dir->d_name,"source") == 0 ||
  80. strcmp(Dir->d_name,"experimental") == 0 ||
  81. strcmp(Dir->d_name,"binary-all") == 0)
  82. continue;
  83. // See if the name is a sub directory
  84. struct stat Buf;
  85. if (stat(Dir->d_name,&Buf) != 0)
  86. continue;
  87. if (S_ISDIR(Buf.st_mode) == 0)
  88. continue;
  89. unsigned int I;
  90. for (I = 0; I != Depth; I++)
  91. if (Inodes[I] == Buf.st_ino)
  92. break;
  93. if (I != Depth)
  94. continue;
  95. // Store the inodes weve seen
  96. Inodes[Depth] = Buf.st_ino;
  97. // Descend
  98. if (FindPackages(CD + Dir->d_name,List,SList,InfoDir,Depth+1) == false)
  99. break;
  100. if (chdir(CD.c_str()) != 0)
  101. return _error->Errno("chdir","Unable to change to ",CD.c_str());
  102. };
  103. closedir(D);
  104. return !_error->PendingError();
  105. }
  106. /*}}}*/
  107. // DropBinaryArch - Dump dirs with a string like /binary-<foo>/ /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* Here we drop everything that is not this machines arch */
  110. bool DropBinaryArch(vector<string> &List)
  111. {
  112. char S[300];
  113. sprintf(S,"/binary-%s/",_config->Find("Apt::Architecture").c_str());
  114. for (unsigned int I = 0; I < List.size(); I++)
  115. {
  116. const char *Str = List[I].c_str();
  117. const char *Res;
  118. if ((Res = strstr(Str,"/binary-")) == 0)
  119. continue;
  120. // Weird, remove it.
  121. if (strlen(Res) < strlen(S))
  122. {
  123. List.erase(List.begin() + I);
  124. I--;
  125. continue;
  126. }
  127. // See if it is our arch
  128. if (stringcmp(Res,Res + strlen(S),S) == 0)
  129. continue;
  130. // Erase it
  131. List.erase(List.begin() + I);
  132. I--;
  133. }
  134. return true;
  135. }
  136. /*}}}*/
  137. // Score - We compute a 'score' for a path /*{{{*/
  138. // ---------------------------------------------------------------------
  139. /* Paths are scored based on how close they come to what I consider
  140. normal. That is ones that have 'dist' 'stable' 'frozen' will score
  141. higher than ones without. */
  142. int Score(string Path)
  143. {
  144. int Res = 0;
  145. if (Path.find("stable/") != string::npos)
  146. Res += 2;
  147. if (Path.find("/binary-") != string::npos)
  148. Res += 2;
  149. if (Path.find("frozen/") != string::npos)
  150. Res += 2;
  151. if (Path.find("/dists/") != string::npos)
  152. Res += 4;
  153. if (Path.find("/main/") != string::npos)
  154. Res += 2;
  155. if (Path.find("/contrib/") != string::npos)
  156. Res += 2;
  157. if (Path.find("/non-free/") != string::npos)
  158. Res += 2;
  159. if (Path.find("/non-US/") != string::npos)
  160. Res += 2;
  161. if (Path.find("/source/") != string::npos)
  162. Res += 1;
  163. return Res;
  164. }
  165. /*}}}*/
  166. // DropRepeats - Drop repeated files resulting from symlinks /*{{{*/
  167. // ---------------------------------------------------------------------
  168. /* Here we go and stat every file that we found and strip dup inodes. */
  169. bool DropRepeats(vector<string> &List,const char *Name)
  170. {
  171. // Get a list of all the inodes
  172. ino_t *Inodes = new ino_t[List.size()];
  173. for (unsigned int I = 0; I != List.size(); I++)
  174. {
  175. struct stat Buf;
  176. if (stat((List[I] + Name).c_str(),&Buf) != 0 &&
  177. stat((List[I] + Name + ".gz").c_str(),&Buf) != 0)
  178. _error->Errno("stat","Failed to stat %s%s",List[I].c_str(),
  179. Name);
  180. Inodes[I] = Buf.st_ino;
  181. }
  182. if (_error->PendingError() == true)
  183. return false;
  184. // Look for dups
  185. for (unsigned int I = 0; I != List.size(); I++)
  186. {
  187. for (unsigned int J = I+1; J < List.size(); J++)
  188. {
  189. // No match
  190. if (Inodes[J] != Inodes[I])
  191. continue;
  192. // We score the two paths.. and erase one
  193. int ScoreA = Score(List[I]);
  194. int ScoreB = Score(List[J]);
  195. if (ScoreA < ScoreB)
  196. {
  197. List[I] = string();
  198. break;
  199. }
  200. List[J] = string();
  201. }
  202. }
  203. // Wipe erased entries
  204. for (unsigned int I = 0; I < List.size();)
  205. {
  206. if (List[I].empty() == false)
  207. I++;
  208. else
  209. List.erase(List.begin()+I);
  210. }
  211. return true;
  212. }
  213. /*}}}*/
  214. // ReduceSourceList - Takes the path list and reduces it /*{{{*/
  215. // ---------------------------------------------------------------------
  216. /* This takes the list of source list expressed entires and collects
  217. similar ones to form a single entry for each dist */
  218. bool ReduceSourcelist(string CD,vector<string> &List)
  219. {
  220. sort(List.begin(),List.end());
  221. // Collect similar entries
  222. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  223. {
  224. // Find a space..
  225. string::size_type Space = (*I).find(' ');
  226. if (Space == string::npos)
  227. continue;
  228. string::size_type SSpace = (*I).find(' ',Space + 1);
  229. if (SSpace == string::npos)
  230. continue;
  231. string Word1 = string(*I,Space,SSpace-Space);
  232. for (vector<string>::iterator J = List.begin(); J != I; J++)
  233. {
  234. // Find a space..
  235. string::size_type Space2 = (*J).find(' ');
  236. if (Space2 == string::npos)
  237. continue;
  238. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  239. if (SSpace2 == string::npos)
  240. continue;
  241. if (string(*J,Space2,SSpace2-Space2) != Word1)
  242. continue;
  243. *J += string(*I,SSpace);
  244. *I = string();
  245. }
  246. }
  247. // Wipe erased entries
  248. for (unsigned int I = 0; I < List.size();)
  249. {
  250. if (List[I].empty() == false)
  251. I++;
  252. else
  253. List.erase(List.begin()+I);
  254. }
  255. }
  256. /*}}}*/
  257. // WriteDatabase - Write the CDROM Database file /*{{{*/
  258. // ---------------------------------------------------------------------
  259. /* We rewrite the configuration class associated with the cdrom database. */
  260. bool WriteDatabase(Configuration &Cnf)
  261. {
  262. string DFile = _config->FindFile("Dir::State::cdroms");
  263. string NewFile = DFile + ".new";
  264. unlink(NewFile.c_str());
  265. ofstream Out(NewFile.c_str());
  266. if (!Out)
  267. return _error->Errno("ofstream::ofstream",
  268. "Failed to open %s.new",DFile.c_str());
  269. /* Write out all of the configuration directives by walking the
  270. configuration tree */
  271. const Configuration::Item *Top = Cnf.Tree(0);
  272. for (; Top != 0;)
  273. {
  274. // Print the config entry
  275. if (Top->Value.empty() == false)
  276. Out << Top->FullTag() + " \"" << Top->Value << "\";" << endl;
  277. if (Top->Child != 0)
  278. {
  279. Top = Top->Child;
  280. continue;
  281. }
  282. while (Top != 0 && Top->Next == 0)
  283. Top = Top->Parent;
  284. if (Top != 0)
  285. Top = Top->Next;
  286. }
  287. Out.close();
  288. rename(DFile.c_str(),string(DFile + '~').c_str());
  289. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  290. return _error->Errno("rename","Failed to rename %s.new to %s",
  291. DFile.c_str(),DFile.c_str());
  292. return true;
  293. }
  294. /*}}}*/
  295. // WriteSourceList - Write an updated sourcelist /*{{{*/
  296. // ---------------------------------------------------------------------
  297. /* This reads the old source list and copies it into the new one. It
  298. appends the new CDROM entires just after the first block of comments.
  299. This places them first in the file. It also removes any old entries
  300. that were the same. */
  301. bool WriteSourceList(string Name,vector<string> &List,bool Source)
  302. {
  303. if (List.size() == 0)
  304. return true;
  305. string File = _config->FindFile("Dir::Etc::sourcelist");
  306. // Open the stream for reading
  307. ifstream F(File.c_str(),ios::in | ios::nocreate);
  308. if (!F != 0)
  309. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  310. string NewFile = File + ".new";
  311. unlink(NewFile.c_str());
  312. ofstream Out(NewFile.c_str());
  313. if (!Out)
  314. return _error->Errno("ofstream::ofstream",
  315. "Failed to open %s.new",File.c_str());
  316. // Create a short uri without the path
  317. string ShortURI = "cdrom:" + Name + "/";
  318. const char *Type;
  319. if (Source == true)
  320. Type = "deb-src";
  321. else
  322. Type = "deb";
  323. char Buffer[300];
  324. int CurLine = 0;
  325. bool First = true;
  326. while (F.eof() == false)
  327. {
  328. F.getline(Buffer,sizeof(Buffer));
  329. CurLine++;
  330. _strtabexpand(Buffer,sizeof(Buffer));
  331. _strstrip(Buffer);
  332. // Comment or blank
  333. if (Buffer[0] == '#' || Buffer[0] == 0)
  334. {
  335. Out << Buffer << endl;
  336. continue;
  337. }
  338. if (First == true)
  339. {
  340. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  341. {
  342. string::size_type Space = (*I).find(' ');
  343. if (Space == string::npos)
  344. return _error->Error("Internal error");
  345. Out << Type << " \"cdrom:" << Name << "/" << string(*I,0,Space) <<
  346. "\" " << string(*I,Space+1) << endl;
  347. }
  348. }
  349. First = false;
  350. // Grok it
  351. string cType;
  352. string URI;
  353. char *C = Buffer;
  354. if (ParseQuoteWord(C,cType) == false ||
  355. ParseQuoteWord(C,URI) == false)
  356. {
  357. Out << Buffer << endl;
  358. continue;
  359. }
  360. // Emit lines like this one
  361. if (cType != Type || string(URI,0,ShortURI.length()) != ShortURI)
  362. {
  363. Out << Buffer << endl;
  364. continue;
  365. }
  366. }
  367. // Just in case the file was empty
  368. if (First == true)
  369. {
  370. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  371. {
  372. string::size_type Space = (*I).find(' ');
  373. if (Space == string::npos)
  374. return _error->Error("Internal error");
  375. Out << "deb \"cdrom:" << Name << "/" << string(*I,0,Space) <<
  376. "\" " << string(*I,Space+1) << endl;
  377. }
  378. }
  379. Out.close();
  380. rename(File.c_str(),string(File + '~').c_str());
  381. if (rename(NewFile.c_str(),File.c_str()) != 0)
  382. return _error->Errno("rename","Failed to rename %s.new to %s",
  383. File.c_str(),File.c_str());
  384. return true;
  385. }
  386. /*}}}*/
  387. // Prompt - Simple prompt /*{{{*/
  388. // ---------------------------------------------------------------------
  389. /* */
  390. void Prompt(const char *Text)
  391. {
  392. char C;
  393. cout << Text << ' ' << flush;
  394. read(STDIN_FILENO,&C,1);
  395. if (C != '\n')
  396. cout << endl;
  397. }
  398. /*}}}*/
  399. // PromptLine - Prompt for an input line /*{{{*/
  400. // ---------------------------------------------------------------------
  401. /* */
  402. string PromptLine(const char *Text)
  403. {
  404. cout << Text << ':' << endl;
  405. string Res;
  406. getline(cin,Res);
  407. return Res;
  408. }
  409. /*}}}*/
  410. // DoAdd - Add a new CDROM /*{{{*/
  411. // ---------------------------------------------------------------------
  412. /* This does the main add bit.. We show some status and things. The
  413. sequence is to mount/umount the CD, Ident it then scan it for package
  414. files and reduce that list. Then we copy over the package files and
  415. verify them. Then rewrite the database files */
  416. bool DoAdd(CommandLine &)
  417. {
  418. // Startup
  419. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  420. if (CDROM[0] == '.')
  421. CDROM= SafeGetCWD() + '/' + CDROM;
  422. cout << "Using CD-ROM mount point " << CDROM << endl;
  423. // Read the database
  424. Configuration Database;
  425. string DFile = _config->FindFile("Dir::State::cdroms");
  426. if (FileExists(DFile) == true)
  427. {
  428. if (ReadConfigFile(Database,DFile) == false)
  429. return _error->Error("Unable to read the cdrom database %s",
  430. DFile.c_str());
  431. }
  432. // Unmount the CD and get the user to put in the one they want
  433. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  434. {
  435. cout << "Unmounting CD-ROM" << endl;
  436. UnmountCdrom(CDROM);
  437. // Mount the new CDROM
  438. Prompt("Please insert a Disc in the drive and press enter");
  439. cout << "Mounting CD-ROM" << endl;
  440. if (MountCdrom(CDROM) == false)
  441. return _error->Error("Failed to mount the cdrom.");
  442. }
  443. // Hash the CD to get an ID
  444. cout << "Identifying.. " << flush;
  445. string ID;
  446. if (IdentCdrom(CDROM,ID) == false)
  447. {
  448. cout << endl;
  449. return false;
  450. }
  451. cout << '[' << ID << ']' << endl;
  452. cout << "Scanning Disc for index files.. " << flush;
  453. // Get the CD structure
  454. vector<string> List;
  455. vector<string> sList;
  456. string StartDir = SafeGetCWD();
  457. string InfoDir;
  458. if (FindPackages(CDROM,List,sList,InfoDir) == false)
  459. {
  460. cout << endl;
  461. return false;
  462. }
  463. chdir(StartDir.c_str());
  464. if (_config->FindB("Debug::aptcdrom",false) == true)
  465. {
  466. cout << "I found:" << endl;
  467. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  468. {
  469. cout << *I << endl;
  470. }
  471. }
  472. // Fix up the list
  473. DropBinaryArch(List);
  474. DropRepeats(List,"Packages");
  475. DropRepeats(sList,"Sources");
  476. cout << "Found " << List.size() << " package indexes and " << sList.size() <<
  477. " source indexes." << endl;
  478. if (List.size() == 0)
  479. return _error->Error("Unable to locate any package files, perhaps this is not a Debian Disc");
  480. // Check if the CD is in the database
  481. string Name;
  482. if (Database.Exists("CD::" + ID) == false ||
  483. _config->FindB("APT::CDROM::Rename",false) == true)
  484. {
  485. // Try to use the CDs label if at all possible
  486. if (InfoDir.empty() == false &&
  487. FileExists(InfoDir + "/info") == true)
  488. {
  489. ifstream F(string(InfoDir + "/info").c_str());
  490. if (!F == 0)
  491. getline(F,Name);
  492. if (Name.empty() == false)
  493. {
  494. cout << "Found label '" << Name << "'" << endl;
  495. Database.Set("CD::" + ID + "::Label",Name);
  496. }
  497. }
  498. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  499. Name.empty() == true)
  500. {
  501. cout << "Please provide a name for this Disc, such as 'Debian 2.1r1 Disk 1'";
  502. while (1)
  503. {
  504. Name = PromptLine("");
  505. if (Name.empty() == false &&
  506. Name.find('"') == string::npos &&
  507. Name.find(':') == string::npos &&
  508. Name.find('/') == string::npos)
  509. break;
  510. cout << "That is not a valid name, try again " << endl;
  511. }
  512. }
  513. }
  514. else
  515. Name = Database.Find("CD::" + ID);
  516. string::iterator J = Name.begin();
  517. for (; J != Name.end(); J++)
  518. if (*J == '/' || *J == '"' || *J == ':')
  519. *J = '_';
  520. Database.Set("CD::" + ID,Name);
  521. cout << "This Disc is called '" << Name << "'" << endl;
  522. // Copy the package files to the state directory
  523. PackageCopy Copy;
  524. SourceCopy SrcCopy;
  525. if (Copy.CopyPackages(CDROM,Name,List) == false ||
  526. SrcCopy.CopyPackages(CDROM,Name,sList) == false)
  527. return false;
  528. ReduceSourcelist(CDROM,List);
  529. ReduceSourcelist(CDROM,sList);
  530. // Write the database and sourcelist
  531. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  532. {
  533. if (WriteDatabase(Database) == false)
  534. return false;
  535. cout << "Writing new source list" << endl;
  536. if (WriteSourceList(Name,List,false) == false ||
  537. WriteSourceList(Name,sList,true) == false)
  538. return false;
  539. }
  540. // Print the sourcelist entries
  541. cout << "Source List entries for this Disc are:" << endl;
  542. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  543. {
  544. string::size_type Space = (*I).find(' ');
  545. if (Space == string::npos)
  546. return _error->Error("Internal error");
  547. cout << "deb \"cdrom:" << Name << "/" << string(*I,0,Space) <<
  548. "\" " << string(*I,Space+1) << endl;
  549. }
  550. for (vector<string>::iterator I = sList.begin(); I != sList.end(); I++)
  551. {
  552. string::size_type Space = (*I).find(' ');
  553. if (Space == string::npos)
  554. return _error->Error("Internal error");
  555. cout << "deb-src \"cdrom:" << Name << "/" << string(*I,0,Space) <<
  556. "\" " << string(*I,Space+1) << endl;
  557. }
  558. cout << "Repeat this process for the rest of the CDs in your set." << endl;
  559. return true;
  560. }
  561. /*}}}*/
  562. // ShowHelp - Show the help screen /*{{{*/
  563. // ---------------------------------------------------------------------
  564. /* */
  565. int ShowHelp()
  566. {
  567. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  568. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  569. if (_config->FindB("version") == true)
  570. return 100;
  571. cout << "Usage: apt-cdrom [options] command" << endl;
  572. cout << endl;
  573. cout << "apt-cdrom is a tool to add CDROM's to APT's source list. The " << endl;
  574. cout << "CDROM mount point and device information is taken from apt.conf" << endl;
  575. cout << "and /etc/fstab." << endl;
  576. cout << endl;
  577. cout << "Commands:" << endl;
  578. cout << " add - Add a CDROM" << endl;
  579. cout << endl;
  580. cout << "Options:" << endl;
  581. cout << " -h This help text" << endl;
  582. cout << " -d CD-ROM mount point" << endl;
  583. cout << " -r Rename a recognized CD-ROM" << endl;
  584. cout << " -m No mounting" << endl;
  585. cout << " -f Fast mode, don't check package files" << endl;
  586. cout << " -a Thorough scan mode" << endl;
  587. cout << " -c=? Read this configuration file" << endl;
  588. cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
  589. cout << "See fstab(5)" << endl;
  590. return 100;
  591. }
  592. /*}}}*/
  593. int main(int argc,const char *argv[])
  594. {
  595. CommandLine::Args Args[] = {
  596. {'h',"help","help",0},
  597. {'v',"version","version",0},
  598. {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
  599. {'r',"rename","APT::CDROM::Rename",0},
  600. {'m',"no-mount","APT::CDROM::NoMount",0},
  601. {'f',"fast","APT::CDROM::Fast",0},
  602. {'n',"just-print","APT::CDROM::NoAct",0},
  603. {'n',"recon","APT::CDROM::NoAct",0},
  604. {'n',"no-act","APT::CDROM::NoAct",0},
  605. {'a',"thorough","APT::CDROM::Thorough",0},
  606. {'c',"config-file",0,CommandLine::ConfigFile},
  607. {'o',"option",0,CommandLine::ArbItem},
  608. {0,0,0,0}};
  609. CommandLine::Dispatch Cmds[] = {
  610. {"add",&DoAdd},
  611. {0,0}};
  612. // Parse the command line and initialize the package library
  613. CommandLine CmdL(Args,_config);
  614. if (pkgInitialize(*_config) == false ||
  615. CmdL.Parse(argc,argv) == false)
  616. {
  617. _error->DumpErrors();
  618. return 100;
  619. }
  620. // See if the help should be shown
  621. if (_config->FindB("help") == true ||
  622. CmdL.FileSize() == 0)
  623. return ShowHelp();
  624. // Deal with stdout not being a tty
  625. if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
  626. _config->Set("quiet","1");
  627. // Match the operation
  628. CmdL.DispatchArg(Cmds);
  629. // Print any errors or warnings found during parsing
  630. if (_error->empty() == false)
  631. {
  632. bool Errors = _error->PendingError();
  633. _error->DumpErrors();
  634. return Errors == true?100:0;
  635. }
  636. return 0;
  637. }