message_reader.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <cassert>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <iostream>
  18. #include <iostream>
  19. #include <fstream>
  20. #include <log/message_exception.h>
  21. #include <log/messagedef.h>
  22. #include <log/message_reader.h>
  23. #include <util/strutil.h>
  24. using namespace std;
  25. namespace {
  26. const char DIRECTIVE_FLAG = '$'; // Starts each directive
  27. const char MESSAGE_FLAG = '%'; // Starts each message
  28. }
  29. namespace isc {
  30. namespace log {
  31. // Read the file.
  32. void
  33. MessageReader::readFile(const string& file, MessageReader::Mode mode) {
  34. // Ensure the non-added collection is empty: we could be re-using this
  35. // object.
  36. not_added_.clear();
  37. // Open the file.
  38. ifstream infile(file.c_str());
  39. if (infile.fail()) {
  40. throw MessageException(MSG_OPENIN, file, strerror(errno));
  41. }
  42. // Loop round reading it. As we process the file one line at a time,
  43. // keep a track of line number of aid diagnosis of problems.
  44. string line;
  45. getline(infile, line);
  46. lineno_ = 0;
  47. while (infile.good()) {
  48. ++lineno_;
  49. processLine(line, mode);
  50. getline(infile, line);
  51. }
  52. // Why did the loop terminate?
  53. if (!infile.eof()) {
  54. throw MessageException(MSG_READERR, file, strerror(errno));
  55. }
  56. infile.close();
  57. }
  58. // Parse a line of the file.
  59. void
  60. MessageReader::processLine(const string& line, MessageReader::Mode mode) {
  61. // Get rid of leading and trailing spaces
  62. string text = isc::util::str::trim(line);
  63. if (text.empty()) {
  64. ; // Ignore blank lines
  65. } else if (text[0] == DIRECTIVE_FLAG) {
  66. parseDirective(text); // Process directives
  67. } else if (text[0] == MESSAGE_FLAG) {
  68. parseMessage(text, mode); // Process message definition line
  69. } else {
  70. ; // Other lines are extended message
  71. // description so are ignored
  72. }
  73. }
  74. // Process directive
  75. void
  76. MessageReader::parseDirective(const std::string& text) {
  77. // Break into tokens
  78. vector<string> tokens = isc::util::str::tokens(text);
  79. // Uppercase directive and branch on valid ones
  80. isc::util::str::uppercase(tokens[0]);
  81. if (tokens[0] == string("$PREFIX")) {
  82. parsePrefix(tokens);
  83. } else if (tokens[0] == string("$NAMESPACE")) {
  84. parseNamespace(tokens);
  85. } else {
  86. // Unrecognised directive
  87. throw MessageException(MSG_UNRECDIR, tokens[0], lineno_);
  88. }
  89. }
  90. // Process $PREFIX
  91. void
  92. MessageReader::parsePrefix(const vector<string>& tokens) {
  93. // Should not get here unless there is something in the tokens array.
  94. assert(tokens.size() > 0);
  95. // Process $PREFIX. With no arguments, the prefix is set to the empty
  96. // string. One argument sets the prefix to the to its value and more than
  97. // one argument is invalid.
  98. if (tokens.size() == 1) {
  99. prefix_ = "";
  100. } else if (tokens.size() == 2) {
  101. prefix_ = tokens[1];
  102. // Token is potentially valid providing it only contains alphabetic
  103. // and numeric characters (and underscores) and does not start with a
  104. // digit.
  105. if (invalidSymbol(prefix_)) {
  106. throw MessageException(MSG_PRFINVARG, prefix_, lineno_);
  107. }
  108. } else {
  109. // Too many arguments
  110. throw MessageException(MSG_PRFEXTRARG, lineno_);
  111. }
  112. }
  113. // Check if string is an invalid C++ symbol. It is valid if comprises only
  114. // alphanumeric characters and underscores, and does not start with a digit.
  115. // (Owing to the logic of the rest of the code, we check for its invalidity,
  116. // not its validity.)
  117. bool
  118. MessageReader::invalidSymbol(const string& symbol) {
  119. static const string valid_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  120. "abcdefghijklmnopqrstuvwxyz"
  121. "0123456789_";
  122. return ( symbol.empty() ||
  123. (symbol.find_first_not_of(valid_chars) != string::npos) ||
  124. (std::isdigit(symbol[0])));
  125. }
  126. // Process $NAMESPACE. A lot of the processing is similar to that of $PREFIX,
  127. // except that only limited checks will be done on the namespace (to avoid a
  128. // lot of parsing and separating out of the namespace components.) Also, unlike
  129. // $PREFIX, there can only be one $NAMESPACE in a file.
  130. void
  131. MessageReader::parseNamespace(const vector<string>& tokens) {
  132. // Check argument count
  133. if (tokens.size() < 2) {
  134. throw MessageException(MSG_NSNOARG, lineno_);
  135. } else if (tokens.size() > 2) {
  136. throw MessageException(MSG_NSEXTRARG, lineno_);
  137. }
  138. // Token is potentially valid providing it only contains alphabetic
  139. // and numeric characters (and underscores and colons). As noted above,
  140. // we won't be exhaustive - after all, and code containing the resultant
  141. // namespace will have to be compiled, and the compiler will catch errors.
  142. static const string valid_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  143. "abcdefghijklmnopqrstuvwxyz"
  144. "0123456789_:";
  145. if (tokens[1].find_first_not_of(valid_chars) != string::npos) {
  146. throw MessageException(MSG_NSINVARG, tokens[1], lineno_);
  147. }
  148. // All OK - unless the namespace has already been set.
  149. if (ns_.size() != 0) {
  150. throw MessageException(MSG_DUPLNS, lineno_);
  151. }
  152. // Prefix has not been set, so set it and return success.
  153. ns_ = tokens[1];
  154. }
  155. // Process message. By the time this method is called, the line has been
  156. // stripped of leading and trailing spaces. The first character of the string
  157. // is the message introducer, so we can get rid of that. The remainder is
  158. // a line defining a message.
  159. //
  160. // The first token on the line, when concatenated to the prefix and converted to
  161. // upper-case, is the message ID. The first of the line from the next token
  162. // on is the message text.
  163. void
  164. MessageReader::parseMessage(const std::string& text, MessageReader::Mode mode) {
  165. static string delimiters("\t\n "); // Delimiters
  166. // The line passed should be at least one character long and start with the
  167. // message introducer (else we should not have got here).
  168. assert((text.size() >= 1) && (text[0] == MESSAGE_FLAG));
  169. // A line comprising just the message introducer is not valid.
  170. if (text.size() == 1) {
  171. throw MessageException(MSG_NOMSGID, text, lineno_);
  172. }
  173. // Strip off the introducer and any leading space after that.
  174. string message_line = isc::util::str::trim(text.substr(1));
  175. // Look for the first delimiter.
  176. size_t first_delim = message_line.find_first_of(delimiters);
  177. if (first_delim == string::npos) {
  178. // Just a single token in the line - this is not valid
  179. throw MessageException(MSG_NOMSGTXT, message_line, lineno_);
  180. }
  181. // Extract the first token into the message ID, preceding it with the
  182. // current prefix, then convert to upper-case. If the prefix is not set,
  183. // perform the valid character check now - the string will become a C++
  184. // symbol so we may as well identify problems early.
  185. string ident = prefix_ + message_line.substr(0, first_delim);
  186. if (prefix_.empty()) {
  187. if (invalidSymbol(ident)) {
  188. throw MessageException(MSG_INVMSGID, ident, lineno_);
  189. }
  190. }
  191. isc::util::str::uppercase(ident);
  192. // Locate the start of the message text
  193. size_t first_text = message_line.find_first_not_of(delimiters, first_delim);
  194. if (first_text == string::npos) {
  195. // ?? This happens if there are trailing delimiters, which should not
  196. // occur as we have stripped trailing spaces off the line. Just treat
  197. // this as a single-token error for simplicity's sake.
  198. throw MessageException(MSG_NOMSGTXT, message_line, lineno_);
  199. }
  200. // Add the result to the dictionary and to the non-added list if the add to
  201. // the dictionary fails.
  202. bool added;
  203. if (mode == ADD) {
  204. added = dictionary_->add(ident, message_line.substr(first_text));
  205. }
  206. else {
  207. added = dictionary_->replace(ident, message_line.substr(first_text));
  208. }
  209. if (!added) {
  210. not_added_.push_back(ident);
  211. }
  212. }
  213. } // namespace log
  214. } // namespace isc