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