1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>KLone: parser.c Source File</title>
<link href="kl.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div>
<div class="nav">
<a class="el" href="dir_000000.html">src</a> / <a class="el" href="dir_000013.html">libtrans</a></div>
<h1>parser.c</h1><a href="parser_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
00002 <span class="comment"> * Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com></span>
00003 <span class="comment"> * All rights reserved.</span>
00004 <span class="comment"> *</span>
00005 <span class="comment"> * This file is part of KLone, and as such it is subject to the license stated</span>
00006 <span class="comment"> * in the LICENSE file which you have received as part of this distribution.</span>
00007 <span class="comment"> *</span>
00008 <span class="comment"> * $Id: parser.c,v 1.11 2006/01/30 20:04:27 tho Exp $</span>
00009 <span class="comment"> */</span>
00010
00011 <span class="preprocessor">#include "klone_conf.h"</span>
00012 <span class="preprocessor">#include <stdio.h></span>
00013 <span class="preprocessor">#include <stdlib.h></span>
00014 <span class="preprocessor">#include <ctype.h></span>
00015 <span class="preprocessor">#include <<a class="code" href="klone_8h.html">klone/klone.h</a>></span>
00016 <span class="preprocessor">#include <<a class="code" href="translat_8h.html">klone/translat.h</a>></span>
00017 <span class="preprocessor">#include <<a class="code" href="parser_8h.html">klone/parser.h</a>></span>
00018
00019 <span class="comment">/* parser state */</span>
00020 <span class="keyword">enum</span> {
00021 S_START,
00022 S_IN_DOUBLE_QUOTE,
00023 S_IN_SINGLE_QUOTE,
00024 S_HTML,
00025 S_WAIT_PERC,
00026 S_START_CODE,
00027 S_CODE,
00028 S_WAIT_GT,
00029 <a class="code" href="parser_8c.html#a22a10">S_EAT_NEWLINE</a>
00030 };
00031
00032 <span class="keyword">enum</span> { <a class="code" href="parser_8c.html#a23a11">LF</a> = 0xA, <a class="code" href="parser_8c.html#a23a12">CR</a> = 0xD };
00033
00034 <span class="keyword">static</span> <span class="keywordtype">int</span> parser_on_block(<a class="code" href="structparser__s.html">parser_t</a> *p, <span class="keyword">const</span> <span class="keywordtype">char</span> *buf, size_t sz)
00035 {
00036 dbg_err_if (p == NULL);
00037 dbg_err_if (buf == NULL);
00038
00039 <span class="keywordflow">for</span>(;;)
00040 {
00041 <span class="keywordflow">switch</span>(p-><a class="code" href="structparser__s.html#o2">state</a>)
00042 {
00043 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a2">S_START</a>:
00044 <span class="comment">/* empty file */</span>
00045 <span class="keywordflow">return</span> 0;
00046 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a3">S_IN_DOUBLE_QUOTE</a>:
00047 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a4">S_IN_SINGLE_QUOTE</a>:
00048 <span class="keywordflow">if</span>(p-><a class="code" href="structparser__s.html#o2">state</a> != p-><a class="code" href="structparser__s.html#o3">prev_state</a>)
00049 {
00050 p-><a class="code" href="structparser__s.html#o2">state</a> = p-><a class="code" href="structparser__s.html#o3">prev_state</a>;
00051 <span class="keywordflow">continue</span>;
00052 } <span class="keywordflow">else</span>
00053 <span class="keywordflow">return</span> 0;
00054 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a5">S_HTML</a>:
00055 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a6">S_WAIT_PERC</a>:
00056 <span class="keywordflow">if</span>(sz && p-><a class="code" href="structparser__s.html#o9">cb_html</a>)
00057 dbg_err_if(p-><a class="code" href="structparser__s.html#o9">cb_html</a>(p, p-><a class="code" href="structparser__s.html#o7">cb_arg</a>, buf, sz));
00058 <span class="keywordflow">return</span> 0;
00059 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a7">S_START_CODE</a>:
00060 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a8">S_CODE</a>:
00061 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a9">S_WAIT_GT</a>:
00062 <span class="keywordflow">if</span>(sz && p-><a class="code" href="structparser__s.html#o8">cb_code</a>)
00063 dbg_err_if(p-><a class="code" href="structparser__s.html#o8">cb_code</a>(p, p-><a class="code" href="structparser__s.html#o4">cmd_code</a>, p-><a class="code" href="structparser__s.html#o7">cb_arg</a>, buf, sz));
00064 <span class="keywordflow">return</span> 0;
00065 }
00066 }
00067
00068 <span class="keywordflow">return</span> 0;
00069 err:
00070 <span class="keywordflow">return</span> ~0;
00071 }
00072
<a name="l00073"></a><a class="code" href="parser_8h.html#a5">00073</a> <span class="keywordtype">int</span> <a class="code" href="parser_8h.html#a5">parser_run</a>(<a class="code" href="structparser__s.html">parser_t</a> *p)
00074 {
00075 <span class="keyword">enum</span> { BUFSZ = 262144 }; <span class="comment">/* a big buffer is good to better zip *.kl1 */</span>
00076 <span class="preprocessor"> #define set_state( s ) \</span>
00077 <span class="preprocessor"> do { tmp = p->state; p->state = s; p->prev_state = tmp; } while(0)</span>
00078 <span class="preprocessor"></span><span class="preprocessor"> #define fetch_next_char() \</span>
00079 <span class="preprocessor"> do { prev = c; \</span>
00080 <span class="preprocessor"> dbg_err_if((rc = io_getc(p->in, &c)) < 0); \</span>
00081 <span class="preprocessor"> if(rc == 0) break; \</span>
00082 <span class="preprocessor"> if( (c == CR || c == LF) && prev != (c == CR ? LF : CR)) \</span>
00083 <span class="preprocessor"> p->line++; \</span>
00084 <span class="preprocessor"> } while(0)</span>
00085 <span class="preprocessor"></span> <span class="keywordtype">int</span> tmp;
00086 <span class="keywordtype">char</span> c = 0, prev;
00087 <span class="keywordtype">char</span> buf[BUFSZ];
00088 size_t idx = 0;
00089 ssize_t rc;
00090
00091 dbg_err_if (p == NULL);
00092
00093 buf[0] = 0;
00094 prev = 0;
00095
00096 dbg_err_if(p-><a class="code" href="structparser__s.html#o5">line</a> > 1);
00097
00098 <a class="code" href="parser_8c.html#a1">fetch_next_char</a>();
00099
00100 <span class="keywordflow">while</span>(rc > 0)
00101 {
00102 prev = c;
00103 <span class="keywordflow">switch</span>(p-><a class="code" href="structparser__s.html#o2">state</a>)
00104 {
00105 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a2">S_START</a>:
00106 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a5">S_HTML</a>);
00107 <span class="keywordflow">continue</span>;
00108 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a3">S_IN_DOUBLE_QUOTE</a>:
00109 <span class="keywordflow">if</span>(c == <span class="charliteral">'"'</span> && prev != <span class="charliteral">'\\'</span>)
00110 <a class="code" href="parser_8c.html#a0">set_state</a>(p-><a class="code" href="structparser__s.html#o3">prev_state</a>);
00111 <span class="keywordflow">break</span>;
00112 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a4">S_IN_SINGLE_QUOTE</a>:
00113 <span class="keywordflow">if</span>(c == <span class="charliteral">'\''</span> && prev != <span class="charliteral">'\\'</span>)
00114 <a class="code" href="parser_8c.html#a0">set_state</a>(p-><a class="code" href="structparser__s.html#o3">prev_state</a>);
00115 <span class="keywordflow">break</span>;
00116 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a5">S_HTML</a>:
00117 <span class="keywordflow">if</span>(c == <span class="charliteral">'<'</span>)
00118 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a6">S_WAIT_PERC</a>);
00119 <span class="keywordflow">break</span>;
00120 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a6">S_WAIT_PERC</a>:
00121 <span class="keywordflow">if</span>(c == <span class="charliteral">'%'</span>)
00122 {
00123 <span class="keywordflow">if</span>(idx && --idx) <span class="comment">/* erase < */</span>
00124 {
00125 buf[idx] = 0;
00126 dbg_err_if(parser_on_block(p, buf, idx));
00127 buf[0] = 0; idx = 0;
00128 }
00129 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a7">S_START_CODE</a>);
00130 p-><a class="code" href="structparser__s.html#o6">code_line</a> = p-><a class="code" href="structparser__s.html#o5">line</a>; <span class="comment">/* save start code line number */</span>
00131 <a class="code" href="parser_8c.html#a1">fetch_next_char</a>(); <span class="comment">/* get cmd char (!,@,etc.) */</span>
00132 <span class="keywordflow">continue</span>;
00133 } <span class="keywordflow">else</span> {
00134 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a5">S_HTML</a>);
00135 <span class="keywordflow">continue</span>;
00136 }
00137 <span class="keywordflow">break</span>;
00138 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a7">S_START_CODE</a>:
00139 <span class="keywordflow">if</span>(isspace(c))
00140 p-><a class="code" href="structparser__s.html#o4">cmd_code</a> = 0;
00141 <span class="keywordflow">else</span> {
00142 p-><a class="code" href="structparser__s.html#o4">cmd_code</a> = c;
00143 <a class="code" href="parser_8c.html#a1">fetch_next_char</a>();
00144 }
00145 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a8">S_CODE</a>);
00146 <span class="keywordflow">continue</span>;
00147 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a8">S_CODE</a>:
00148 <span class="keywordflow">if</span>(c == <span class="charliteral">'"'</span>)
00149 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a3">S_IN_DOUBLE_QUOTE</a>);
00150 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(c == <span class="charliteral">'\''</span>)
00151 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a4">S_IN_SINGLE_QUOTE</a>);
00152 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(c == <span class="charliteral">'%'</span>)
00153 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a9">S_WAIT_GT</a>);
00154 <span class="keywordflow">break</span>;
00155 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a9">S_WAIT_GT</a>:
00156 <span class="keywordflow">if</span>(c == <span class="charliteral">'>'</span>)
00157 {
00158 <span class="keywordflow">if</span>(idx && --idx) <span class="comment">/* erase % */</span>
00159 {
00160 buf[idx] = 0;
00161 dbg_err_if(parser_on_block(p, buf, idx));
00162 buf[0] = 0; idx = 0;
00163 }
00164 <a class="code" href="parser_8c.html#a1">fetch_next_char</a>();
00165 p-><a class="code" href="structparser__s.html#o4">cmd_code</a> = 0;
00166 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a5">S_HTML</a>);
00167 <span class="keywordflow">continue</span>;
00168 } <span class="keywordflow">else</span> {
00169 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a8">S_CODE</a>);
00170 <span class="keywordflow">continue</span>;
00171 }
00172 <span class="keywordflow">break</span>;
00173 <span class="keywordflow">case</span> <a class="code" href="parser_8c.html#a22a10">S_EAT_NEWLINE</a>:
00174 <span class="keywordflow">if</span>(c == <a class="code" href="parser_8c.html#a23a12">CR</a> || c == LF)
00175 {
00176 <a class="code" href="parser_8c.html#a1">fetch_next_char</a>();
00177 <span class="keywordflow">continue</span>; <span class="comment">/* eat it */</span>
00178 }
00179 <a class="code" href="parser_8c.html#a0">set_state</a>(<a class="code" href="parser_8c.html#a22a5">S_HTML</a>);
00180 <span class="keywordflow">continue</span>;
00181 <span class="keywordflow">default</span>:
00182 dbg_err_if(<span class="stringliteral">"unknown parser state"</span>);
00183 }
00184 buf[idx++] = c;
00185 <span class="keywordflow">if</span>(idx == BUFSZ - 1)
00186 {
00187 buf[idx] = 0;
00188 dbg_err_if(parser_on_block(p, buf, idx));
00189 buf[0] = 0; idx = 0;
00190 }
00191
00192 <a class="code" href="parser_8c.html#a1">fetch_next_char</a>();
00193 }
00194
00195 <span class="keywordflow">if</span>(idx)
00196 {
00197 buf[idx] = 0;
00198 dbg_err_if(parser_on_block(p, buf, idx));
00199 buf[0] = 0; idx = 0;
00200 }
00201
00202 <span class="keywordflow">return</span> 0;
00203 err:
00204 <span class="keywordflow">return</span> ~0;
00205 }
00206
<a name="l00207"></a><a class="code" href="parser_8h.html#a8">00207</a> <span class="keywordtype">void</span> <a class="code" href="parser_8h.html#a8">parser_set_cb_code</a>(<a class="code" href="structparser__s.html">parser_t</a> *p, parser_cb_code_t cb)
00208 {
00209 dbg_ifb (p == NULL) <span class="keywordflow">return</span>;
00210 p-><a class="code" href="structparser__s.html#o8">cb_code</a> = cb;
00211 }
00212
<a name="l00213"></a><a class="code" href="parser_8h.html#a9">00213</a> <span class="keywordtype">void</span> <a class="code" href="parser_8h.html#a9">parser_set_cb_html</a>(<a class="code" href="structparser__s.html">parser_t</a> *p, parser_cb_html_t cb)
00214 {
00215 dbg_ifb (p == NULL) <span class="keywordflow">return</span>;
00216 p-><a class="code" href="structparser__s.html#o9">cb_html</a> = cb;
00217 }
00218
<a name="l00219"></a><a class="code" href="parser_8h.html#a10">00219</a> <span class="keywordtype">void</span> <a class="code" href="parser_8h.html#a10">parser_set_cb_arg</a>(<a class="code" href="structparser__s.html">parser_t</a> *p, <span class="keywordtype">void</span> *opaque)
00220 {
00221 dbg_ifb (p == NULL) <span class="keywordflow">return</span>;
00222 p-><a class="code" href="structparser__s.html#o7">cb_arg</a> = opaque;
00223 }
00224
<a name="l00225"></a><a class="code" href="parser_8h.html#a7">00225</a> <span class="keywordtype">void</span> <a class="code" href="parser_8h.html#a7">parser_set_io</a>(<a class="code" href="structparser__s.html">parser_t</a> *p, <a class="code" href="structio__s.html">io_t</a> *in, <a class="code" href="structio__s.html">io_t</a> *out)
00226 {
00227 dbg_ifb (p == NULL) <span class="keywordflow">return</span>;
00228 p-><a class="code" href="structparser__s.html#o0">in</a> = in;
00229 p-><a class="code" href="structparser__s.html#o1">out</a> = out;
00230 }
00231
<a name="l00232"></a><a class="code" href="parser_8h.html#a4">00232</a> <span class="keywordtype">int</span> <a class="code" href="parser_8h.html#a4">parser_free</a>(<a class="code" href="structparser__s.html">parser_t</a> *t)
00233 {
00234 U_FREE(t);
00235 <span class="keywordflow">return</span> 0;
00236 }
00237
<a name="l00238"></a><a class="code" href="parser_8h.html#a6">00238</a> <span class="keywordtype">int</span> <a class="code" href="parser_8h.html#a6">parser_reset</a>(<a class="code" href="structparser__s.html">parser_t</a> *p)
00239 {
00240 dbg_return_if (p == NULL, ~0);
00241
00242 p-><a class="code" href="structparser__s.html#o5">line</a> = 1;
00243 p-><a class="code" href="structparser__s.html#o2">state</a> = p-><a class="code" href="structparser__s.html#o3">prev_state</a> = S_START;
00244 p-><a class="code" href="structparser__s.html#o4">cmd_code</a> = 0;
00245
00246 <span class="keywordflow">return</span> 0;
00247 }
00248
<a name="l00249"></a><a class="code" href="parser_8h.html#a3">00249</a> <span class="keywordtype">int</span> <a class="code" href="parser_8h.html#a3">parser_create</a>(<a class="code" href="structparser__s.html">parser_t</a> **pt)
00250 {
00251 <a class="code" href="structparser__s.html">parser_t</a> *p = NULL;
00252
00253 dbg_return_if (pt == NULL, ~0);
00254
00255 p = (<a class="code" href="structparser__s.html">parser_t</a>*)u_zalloc(<span class="keyword">sizeof</span>(<a class="code" href="structparser__s.html">parser_t</a>));
00256 dbg_err_if(p == NULL);
00257
00258 (void) <a class="code" href="parser_8h.html#a6">parser_reset</a>(p);
00259
00260 *pt = p;
00261
00262 <span class="keywordflow">return</span> 0;
00263 err:
00264 <span class="keywordflow">if</span>(p)
00265 <a class="code" href="parser_8h.html#a4">parser_free</a>(p);
00266 <span class="keywordflow">return</span> ~0;
00267 }
</pre></div><hr>
<div>
<div style="text-align:left">
<a href="http://www.koanlogic.com/kl/cont/gb/html/products.html">←Products</a>
</div>
<div style="text-align:center;">
© 2005-2006 - <a href="http://www.koanlogic.com">KoanLogic S.r.l.</a> - All rights reserved
</div>
</div>
</body>
</html>
|