ct.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
  9. Copyright (c) 2002 Fred L. Drake, Jr. <[email protected]>
  10. Copyright (c) 2016-2017 Sebastian Pipping <[email protected]>
  11. Licensed under the MIT license:
  12. Permission is hereby granted, free of charge, to any person obtaining
  13. a copy of this software and associated documentation files (the
  14. "Software"), to deal in the Software without restriction, including
  15. without limitation the rights to use, copy, modify, merge, publish,
  16. distribute, sublicense, and/or sell copies of the Software, and to permit
  17. persons to whom the Software is furnished to do so, subject to the
  18. following conditions:
  19. The above copyright notice and this permission notice shall be included
  20. in all copies or substantial portions of the Software.
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  24. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  25. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  26. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  27. USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. #define CHARSET_MAX 41
  30. static const char *
  31. getTok(const char **pp) {
  32. enum { inAtom, inString, init, inComment };
  33. int state = init;
  34. const char *tokStart = 0;
  35. for (;;) {
  36. switch (**pp) {
  37. case '\0':
  38. return 0;
  39. case ' ':
  40. case '\r':
  41. case '\t':
  42. case '\n':
  43. if (state == inAtom)
  44. return tokStart;
  45. break;
  46. case '(':
  47. if (state == inAtom)
  48. return tokStart;
  49. if (state != inString)
  50. state++;
  51. break;
  52. case ')':
  53. if (state > init)
  54. --state;
  55. else if (state != inString)
  56. return 0;
  57. break;
  58. case ';':
  59. case '/':
  60. case '=':
  61. if (state == inAtom)
  62. return tokStart;
  63. if (state == init)
  64. return (*pp)++;
  65. break;
  66. case '\\':
  67. ++*pp;
  68. if (**pp == '\0')
  69. return 0;
  70. break;
  71. case '"':
  72. switch (state) {
  73. case inString:
  74. ++*pp;
  75. return tokStart;
  76. case inAtom:
  77. return tokStart;
  78. case init:
  79. tokStart = *pp;
  80. state = inString;
  81. break;
  82. }
  83. break;
  84. default:
  85. if (state == init) {
  86. tokStart = *pp;
  87. state = inAtom;
  88. }
  89. break;
  90. }
  91. ++*pp;
  92. }
  93. /* not reached */
  94. }
  95. /* key must be lowercase ASCII */
  96. static int
  97. matchkey(const char *start, const char *end, const char *key) {
  98. if (! start)
  99. return 0;
  100. for (; start != end; start++, key++)
  101. if (*start != *key && *start != 'A' + (*key - 'a'))
  102. return 0;
  103. return *key == '\0';
  104. }
  105. void
  106. getXMLCharset(const char *buf, char *charset) {
  107. const char *next, *p;
  108. charset[0] = '\0';
  109. next = buf;
  110. p = getTok(&next);
  111. if (matchkey(p, next, "text"))
  112. strcpy(charset, "us-ascii");
  113. else if (! matchkey(p, next, "application"))
  114. return;
  115. p = getTok(&next);
  116. if (! p || *p != '/')
  117. return;
  118. p = getTok(&next);
  119. if (matchkey(p, next, "xml"))
  120. isXml = 1;
  121. p = getTok(&next);
  122. while (p) {
  123. if (*p == ';') {
  124. p = getTok(&next);
  125. if (matchkey(p, next, "charset")) {
  126. p = getTok(&next);
  127. if (p && *p == '=') {
  128. p = getTok(&next);
  129. if (p) {
  130. char *s = charset;
  131. if (*p == '"') {
  132. while (++p != next - 1) {
  133. if (*p == '\\')
  134. ++p;
  135. if (s == charset + CHARSET_MAX - 1) {
  136. charset[0] = '\0';
  137. break;
  138. }
  139. *s++ = *p;
  140. }
  141. *s++ = '\0';
  142. } else {
  143. if (next - p > CHARSET_MAX - 1)
  144. break;
  145. while (p != next)
  146. *s++ = *p++;
  147. *s = 0;
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. } else
  154. p = getTok(&next);
  155. }
  156. }
  157. int
  158. main(int argc, char **argv) {
  159. char buf[CHARSET_MAX];
  160. getXMLCharset(argv[1], buf);
  161. printf("charset = \"%s\"\n", buf);
  162. return 0;
  163. }