system_messages.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // Produce System Messages Manual
  7. //
  8. // This tool reads all the message files given on the command line.
  9. // It pulls all the messages and description out, sorts them by
  10. // message ID, and writes them out as a single (formatted) file.
  11. //
  12. // Invocation:
  13. // The code is invoked using the command line:
  14. //
  15. // system_messages [-o <output-file>] <files>
  16. //
  17. // If no output file is specified, output is written to stdout.
  18. // The produced format is docbook XML.
  19. #include <algorithm>
  20. #include <fstream>
  21. #include <iostream>
  22. #include <map>
  23. #include <string>
  24. #include <vector>
  25. #include <stdlib.h>
  26. #include <cstring>
  27. #include <boost/lexical_cast.hpp>
  28. typedef std::vector<std::string> LinesType;
  29. /// @brief dictionary values
  30. struct Details {
  31. std::string text;
  32. LinesType description;
  33. std::string sname;
  34. std::string filename;
  35. };
  36. /// @brief Main dictionary holding all the messages.
  37. /// The messages are accumulated here before being printed in
  38. /// alphabetical order.
  39. typedef std::map<const std::string, Details> DictionaryType;
  40. DictionaryType dictionary;
  41. /// @brief The structure of the output page
  42. //
  43. /// header
  44. /// section header
  45. /// message
  46. /// separator
  47. /// message
  48. /// separator
  49. /// :
  50. /// separator
  51. /// message
  52. /// section trailer
  53. /// separator
  54. /// section header
  55. /// :
  56. /// section trailer
  57. /// trailer
  58. //
  59. /// (Indentation is not relevant - it has only been added to the above
  60. /// illustration to make the structure clearer.) The text of these section is:
  61. /// @name Constants for the output page
  62. //@{
  63. /// @brief File header
  64. /// this is output before anything else.
  65. const std::string FILE_HEADER =
  66. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
  67. <!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.2//EN\"\n\
  68. \"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\" [\n\
  69. <!ENTITY mdash \"&#x2014;\" >\n\
  70. <!ENTITY % version SYSTEM \"version.ent\">\n\
  71. %version;\n\
  72. ]>\n\
  73. <!--\n\
  74. This XML document is generated using the system_messages tool\n\
  75. based on the .mes message files.\n\
  76. \n\
  77. Do not edit this file.\n\
  78. -->\n\
  79. <book>\n\
  80. <?xml-stylesheet href=\"kea-guide.css\" type=\"text/css\"?>\n\
  81. \n\
  82. <bookinfo>\n\
  83. <title>Kea Messages Manual</title>\n\
  84. \n\
  85. <copyright>\n\
  86. <year>2011-2015</year><holder>Internet Systems Consortium, Inc.</holder>\n\
  87. </copyright>\n\
  88. \n\
  89. <abstract>\n\
  90. <para>\n\
  91. This is the messages manual for Kea version &__VERSION__;.\n\
  92. The most up-to-date version of this document, along with\n\
  93. other documents for Kea, can be found at\n\
  94. <ulink url=\"http://kea.isc.org/docs\"/>.\n\
  95. </para>\n\
  96. </abstract>\n\
  97. \n\
  98. <releaseinfo>This is the messages manual for Kea version\n\
  99. &__VERSION__;.</releaseinfo>\n\
  100. </bookinfo>\n\
  101. \n\
  102. <chapter id=\"intro\">\n\
  103. <title>Introduction</title>\n\
  104. <para>\n\
  105. This document lists each message that can be logged by the\n\
  106. programs in the Kea package. Each entry in this manual\n\
  107. is of the form:\n\
  108. <screen>IDENTIFICATION message-text</screen>\n\
  109. ... where \"IDENTIFICATION\" is the message identification included\n\
  110. in each message logged and \"message-text\" is the accompanying\n\
  111. message text. The \"message-text\" may include placeholders of the\n\
  112. form \"%1\", \"%2\" etc.; these parameters are replaced by relevant\n\
  113. values when the message is logged.\n\
  114. </para>\n\
  115. <para>\n\
  116. Each entry is also accompanied by a description giving more\n\
  117. information about the circumstances that result in the message\n\
  118. being logged.\n\
  119. </para>\n\
  120. <para>\n\
  121. For information on configuring and using Kea logging,\n\
  122. refer to the <ulink url=\"kea-guide.html\">Kea Guide</ulink>.\n\
  123. </para>\n\
  124. </chapter>\n\
  125. \n\
  126. <chapter id=\"messages\">\n\
  127. <title>Kea Log Messages</title>\n";
  128. /// @brief Section header
  129. /// This is output one for each module. $M substitution token is the name.
  130. const std::string SECTION_HEADER = " <section id=\"$M\">\n\
  131. <title>$M Module</title>\n\
  132. <para>\n\
  133. <variablelist>\n";
  134. /// @brief message ID
  135. /// This is output once for each message. The string contains
  136. /// substitution tokens: $I is replaced by the message identification,
  137. /// $T by the message text, and $D by the message description.
  138. const std::string ID_MESSAGE =
  139. "<varlistentry id=\"$I\">\n\
  140. <term>$I $T</term>\n\
  141. <listitem><para>\n\
  142. $D</para></listitem>\n\
  143. </varlistentry>";
  144. /// @brief Blank line
  145. /// A description may contain blank lines intended to separate
  146. /// paragraphs. If so, each blank line is replaced by the following.
  147. const std::string BLANK = "</para><para>";
  148. /// @brief Separator
  149. /// The separator is copied to the output verbatim after each message except
  150. /// the last.
  151. const std::string SEPARATOR = "";
  152. /// @brief Section trailer
  153. /// The trailer is copied to the output verbatim after the last message.
  154. const std::string SECTION_TRAILER =
  155. " </variablelist>\n\
  156. </para>\n\
  157. </section>";
  158. /// @brief File trailer
  159. /// The trailier is copied to the output verbatim after the last section.
  160. const std::string FILE_TRAILER =
  161. " </chapter>\n\
  162. </book>";
  163. //@}
  164. /// @name Utility routines
  165. //@{
  166. /// @brief Report an error and exit
  167. void reportError(const std::string& filename, const std::string& what)
  168. {
  169. std::cerr << "*** ERROR in " << filename << "\n";
  170. std::cerr << "*** REASON: " << what << "\n";
  171. std::cerr << "*** System message generator terminating" << "\n";
  172. exit(1);
  173. }
  174. /// @brief Replace tag
  175. /// Replaces the '<' and '>' in text about to be inserted into the template
  176. /// sections above with &lt; and &gt; to avoid problems with message text
  177. /// being interpreted as XML text.
  178. std::string replaceTag(const std::string& src)
  179. {
  180. std::string result;
  181. for (std::string::const_iterator it = src.begin(); it != src.end(); ++it) {
  182. if (*it == '<') {
  183. result.append("&lt;");
  184. } else if (*it == '>') {
  185. result.append("&gt;");
  186. } else {
  187. result.push_back(*it);
  188. }
  189. }
  190. return (result);
  191. }
  192. /// @brief Replace shell
  193. /// Replace $c in a string (or with other words performs macro expansion
  194. /// with '$' for introducing a macro followed by a character selecting
  195. /// a specific macro.
  196. ///
  197. /// @param src source string
  198. /// @param c character selecting a macro when it follows '$'
  199. /// @param val value which
  200. ///
  201. /// @return the source string where all occurrences of $c were
  202. /// replaced by val
  203. std::string replaceShell(const std::string& src, char c,
  204. const std::string& val)
  205. {
  206. std::string result;
  207. bool shell = false;
  208. for (std::string::const_iterator it = src.begin(); it != src.end(); ++it) {
  209. if (shell) {
  210. if (*it == c) {
  211. result.append(val);
  212. } else {
  213. result.push_back('$');
  214. result.push_back(*it);
  215. }
  216. shell = false;
  217. } else if (*it == '$') {
  218. shell = true;
  219. } else {
  220. result.push_back(*it);
  221. }
  222. }
  223. return (result);
  224. }
  225. /// @brief Replace blank lines
  226. /// Replaces blank lines in an array with the contents of the 'blank' section.
  227. LinesType replaceBlankLines(const LinesType& lines)
  228. {
  229. LinesType result;
  230. for (LinesType::const_iterator l = lines.begin(); l != lines.end(); ++l) {
  231. if (l->empty()) {
  232. result.push_back(BLANK);
  233. } else {
  234. result.push_back(*l);
  235. }
  236. }
  237. return (result);
  238. }
  239. //@}
  240. /// @name Printing functions
  241. //@{
  242. /// @brief Print file header
  243. void printHeader() {
  244. std::cout << FILE_HEADER << "\n";
  245. }
  246. /// @brief Print separator
  247. void printSeparator() {
  248. std::cout << SEPARATOR << "\n";
  249. }
  250. /// @brief Print section header
  251. void printSectionHeader(const std::string& sname)
  252. {
  253. // In the section name, replace "<" and ">" with XML-safe versions and
  254. // substitute into the data.
  255. std::cout << replaceShell(SECTION_HEADER, 'M', replaceTag(sname));
  256. }
  257. /// @brief print message id
  258. void printMessage(const std::string& msgid)
  259. {
  260. // In the message ID, replace "<" and ">" with XML-safe versions and
  261. // substitute into the data.
  262. const std::string m0 = ID_MESSAGE;
  263. const std::string m1 = replaceShell(m0, 'I', replaceTag(msgid));
  264. // Do the same for the message text.
  265. std::string m2 = replaceShell(m1, 'T',
  266. replaceTag(dictionary[msgid].text));
  267. // Do the same for the description then replace blank lines with the
  268. // specified separator. (We do this in that order to avoid replacing
  269. // the "<" and ">" in the XML tags in the separator.)
  270. LinesType desc0 = dictionary[msgid].description;
  271. LinesType desc1;
  272. for (LinesType::iterator l = desc0.begin(); l != desc0.end(); ++l) {
  273. desc1.push_back(replaceTag(*l));
  274. }
  275. LinesType desc2 = replaceBlankLines(desc1);
  276. // Join the lines together to form a single string and insert into
  277. // current text.
  278. std::string m3;
  279. for (LinesType::iterator l = desc2.begin(); l != desc2.end(); ++l) {
  280. m3.append(*l);
  281. m3.push_back('\n');
  282. }
  283. std::cout << replaceShell(m2, 'D', m3) << "\n";
  284. }
  285. /// @brief print section trailer
  286. void printSectionTrailer() {
  287. std::cout << SECTION_TRAILER << "\n";
  288. }
  289. /// @brief print file trailer
  290. void printTrailer() {
  291. std::cout << FILE_TRAILER << "\n";
  292. }
  293. //@}
  294. /// @brief Removes leading and trailing empty lines.
  295. ///
  296. /// A list of strings is passed as argument, some of which may be empty.
  297. /// This function removes from the start and end of list a contiguous
  298. /// sequence of empty lines and returns the result. Embedded sequence of
  299. /// empty lines are not touched.
  300. ///
  301. /// @param lines List of strings to be modified.
  302. ///
  303. /// @return Input list of strings with leading/trailing blank line
  304. /// sequences removed.
  305. LinesType removeEmptyLeadingTrailing(const LinesType& lines)
  306. {
  307. LinesType retlines = lines;
  308. // Dispose of degenerate case of empty array
  309. if (retlines.empty()) {
  310. return (retlines);
  311. }
  312. // Search for first non-blank line
  313. for (;;) {
  314. LinesType::iterator start = retlines.begin();
  315. if (start == retlines.end()) {
  316. return (retlines);
  317. }
  318. if (start->empty()) {
  319. retlines.erase(start);
  320. } else {
  321. break;
  322. }
  323. }
  324. // Search for last non-blank line
  325. for (;;) {
  326. LinesType::reverse_iterator finish = retlines.rbegin();
  327. if (finish == retlines.rend()) {
  328. return (retlines);
  329. }
  330. if (finish->empty()) {
  331. retlines.erase(retlines.end() - 1);
  332. } else {
  333. break;
  334. }
  335. }
  336. return (retlines);
  337. }
  338. /// @brief Add the current message ID and associated information to the global
  339. /// dictionary.
  340. /// If a message with that ID already exists, loop appending suffixes
  341. /// of the form "(n)" to it until one is found that doesn't.
  342. ///
  343. /// @param msgid Message ID
  344. /// @param msgtext Message text
  345. /// @param desc Message description
  346. /// @param filename File from which the message came. Currently this is
  347. /// not used, but a future enhancement may wish to
  348. /// include the name of the message file in the
  349. /// messages manual.
  350. void addToDictionary(const std::string& msgid,
  351. const std::string& msgtext,
  352. const LinesType& desc,
  353. const std::string& filename)
  354. {
  355. // If the ID is in the dictionary, append a "(n)" to the name - this will
  356. // flag that there are multiple instances. (However, this is an error -
  357. // each ID should be unique in the code.)
  358. std::string key = msgid;
  359. if (dictionary.count(key) > 0) {
  360. int i = 1;
  361. std::string s = boost::lexical_cast<std::string>(i);
  362. key = msgid + " (" + s + ")";
  363. while (dictionary.count(key) > 0) {
  364. i = i + 1;
  365. s = boost::lexical_cast<std::string>(i);
  366. key = msgid + " (" + s + ")";
  367. }
  368. }
  369. // Remove leading and trailing blank lines in the description, then
  370. // add everything into a subdictionary which is then added to the main
  371. // one.
  372. Details details;
  373. details.text = msgtext;
  374. details.description = removeEmptyLeadingTrailing(desc);
  375. size_t underscore = msgid.find_first_of('_');
  376. details.sname = msgid.substr(0, underscore);
  377. details.filename = filename;
  378. dictionary.insert(std::pair<const std::string, Details>(key, details));
  379. }
  380. /// @brief Processes file content.
  381. /// Messages and descriptions are identified and added to a dictionary
  382. /// (keyed by message ID). If the key already exists, a numeric
  383. /// suffix is added to it.
  384. /// The format of .mes files is fully described in src/lib/log/logging.dox
  385. ///
  386. /// @param filename Name of the message file being processed
  387. /// @param lines Lines read from the file
  388. void processFileContent(const std::string& filename,
  389. const LinesType& lines)
  390. {
  391. std::string prefix; // Last prefix encountered
  392. std::string msgid; // Last message ID encountered
  393. std::string msgtext; // Text of the message
  394. LinesType description; // Description
  395. for (LinesType::const_iterator l = lines.begin(); l != lines.end(); ++l) {
  396. if (l->empty()) {
  397. description.push_back(*l);
  398. } else if (l->at(0) == '$') {
  399. // Starts with "$". Ignore anything other than $PREFIX
  400. char* line = new char [l->size() + 1];
  401. std::strcpy(line, l->c_str());
  402. char* word0 = strtok(line, " \t\r\n\t\v");
  403. if (strcasecmp(word0, "$PREFIX") == 0) {
  404. char* word1 = strtok(NULL, " \t\r\n\t\v");
  405. prefix = word1;
  406. }
  407. delete[] line;
  408. } else if (l->at(0) == '%') {
  409. // Start of a message. Add the message we were processing to the
  410. // dictionary and clear everything apart from the file name.
  411. if (!msgid.empty()) {
  412. addToDictionary(msgid, msgtext, description, filename);
  413. }
  414. msgid.clear();
  415. msgtext.clear();
  416. description.clear();
  417. // Start of a message
  418. char* line = new char [l->size() + 1];
  419. std::strcpy(line, l->c_str());
  420. // Remove "%" and trim leading spaces
  421. size_t start = l->find_first_not_of(" \t\r\n\t\v", 1);
  422. if (start == std::string::npos) {
  423. reportError(filename, "Line with single % found");
  424. continue;
  425. }
  426. // Split into words. The first word is the message ID
  427. char* word0 = strtok(line + start, " \t\r\n\t\v");
  428. msgid = prefix;
  429. msgid.append(word0);
  430. std::transform(msgid.begin(), msgid.end(),
  431. msgid.begin(), toupper);
  432. char* word1 = strtok(NULL, " \t\r\n\t\v");
  433. start = word1 - line;
  434. size_t finish = l->find_last_not_of(" \t\r\n\t\v");
  435. msgtext = l->substr(start, finish + 1 - start);
  436. } else {
  437. // Part of a description, so add to the current description array
  438. description.push_back(*l);
  439. }
  440. }
  441. // All done, add the last message to the global dictionaty.
  442. if (!msgid.empty()) {
  443. addToDictionary(msgid, msgtext, description, filename);
  444. }
  445. }
  446. /// @brief Process a file
  447. /// Read it in and strip out all comments and and directives. Leading
  448. /// and trailing blank lines in the file are removed and the remainder
  449. /// passed for message processing.
  450. ///
  451. /// @param filename Name of the message file to process
  452. void processFile(const std::string& filename)
  453. {
  454. std::ifstream cin;
  455. cin.open(filename.c_str(), std::ios::in);
  456. if (!cin.is_open()) {
  457. reportError(filename, "open for read failure");
  458. }
  459. LinesType lines0;
  460. while (!cin.eof()) {
  461. std::string line;
  462. getline(cin, line);
  463. lines0.push_back(line);
  464. }
  465. cin.close();
  466. // Trim leading and trailing spaces from each line, and remove comments.
  467. LinesType lines1;
  468. for (LinesType::iterator l = lines0.begin(); l != lines0.end(); ++l) {
  469. std::string line = *l;
  470. // Empty lines have no spaces so are processed
  471. if (line.empty()) {
  472. lines1.push_back(line);
  473. continue;
  474. }
  475. // Trim leading spaces
  476. size_t start = line.find_first_not_of(" \t\r\n\t\v");
  477. if (start != 0) {
  478. line.erase(0, start);
  479. }
  480. // Done?
  481. if (line.empty()) {
  482. lines1.push_back(line);
  483. continue;
  484. }
  485. // Trim trailing spaces
  486. size_t finish = line.find_last_not_of(" \t\r\n\t\v");
  487. if ((finish != std::string::npos) &&
  488. (finish + 1 != line.size())) {
  489. line.erase(finish + 1);
  490. }
  491. // Done
  492. if (line.empty()) {
  493. lines1.push_back(line);
  494. continue;
  495. }
  496. // Skip comments
  497. if (line[0] != '#') {
  498. lines1.push_back(line);
  499. }
  500. }
  501. // Remove leading/trailing empty line sequences from the result
  502. LinesType lines2 = removeEmptyLeadingTrailing(lines1);
  503. // Interpret content
  504. processFileContent(filename, lines2);
  505. }
  506. /// @brief Usage error routine
  507. void usage(char* progname)
  508. {
  509. std::cerr << "Usage: " << progname <<
  510. " [--help | options] files\n";
  511. std::cerr << " options: --output file: " <<
  512. "output file name (default to stdout)\n";
  513. }
  514. /// @brief Main (entry point)
  515. int main(int argc, char* argv[])
  516. {
  517. char* progname = argv[0];
  518. std::ofstream fout;
  519. while (argc > 1) {
  520. --argc;
  521. ++argv;
  522. if (strcmp(argv[0], "--help") == 0) {
  523. usage(progname);
  524. exit(0);
  525. }
  526. // Redirect output if specified (errors are written to stderr)
  527. if ((strcmp(argv[0], "-o") == 0) ||
  528. (strcmp(argv[0], "--output") == 0)) {
  529. --argc;
  530. ++argv;
  531. if (argc == 0) {
  532. usage(progname);
  533. exit(-1);
  534. }
  535. fout.open(argv[0], std::ofstream::out | std::ofstream::trunc);
  536. if (!fout.is_open()) {
  537. reportError(argv[0], "open for write failure");
  538. }
  539. std::cout.rdbuf(fout.rdbuf());
  540. --argc;
  541. ++argv;
  542. break;
  543. }
  544. }
  545. if (argc == 0) {
  546. usage(progname);
  547. exit(-1);
  548. }
  549. for (int i = 0; i < argc; ++i) {
  550. processFile(argv[i]);
  551. }
  552. // Now just print out everything we've read (in alphabetical order).
  553. bool first = true;
  554. std::string sname;
  555. printHeader();
  556. for (DictionaryType::iterator it = dictionary.begin();
  557. it != dictionary.end();
  558. ++it) {
  559. if (sname.compare(it->second.sname) != 0) {
  560. if (!sname.empty()) {
  561. printSectionTrailer();
  562. printSeparator();
  563. }
  564. sname = it->second.sname;
  565. printSectionHeader(sname);
  566. first = true;
  567. }
  568. if (!first) {
  569. printSeparator();
  570. }
  571. first = false;
  572. printMessage(it->first);
  573. }
  574. if (!sname.empty()) {
  575. printSectionTrailer();
  576. }
  577. printTrailer();
  578. exit(0);
  579. }