2 * ÀßÄê¥Õ¥¡¥¤¥ë¤Ê¤É¤Î¤¿¤á¤Î
3 * ÈÆÍѤΥե¡¥¤¥ëÆɤ߹þ¤ß¥â¥¸¥å¡¼¥ë
5 * Copyright (C) 2000-2006 TABATA Yusuke
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <anthy/conf.h>
29 #include <anthy/ruleparser.h>
30 #include <anthy/logger.h>
32 /* ʸˡ¥Õ¥¡¥¤¥ë¤Î¥Ñ¡¼¥¶ÍѤÎÄêµÁ */
33 #define MAX_TOKEN_LEN 256
34 /* ºÇÂç¤Î¥¤¥ó¥¯¥ë¡¼¥É¤Î¿¼¤µ */
35 #define MAX_INCLUDE_DEPTH 4
42 static const char *NL = "NL";
44 static struct parser_stat {
45 FILE *fp_stack[MAX_INCLUDE_DEPTH];
47 int cur_fpp;/* ¥¹¥¿¥Ã¥¯¤Î¥¤¥ó¥Ç¥Ã¥¯¥¹ */
55 char buf[MAX_TOKEN_LEN];
60 open_file_in_confdir(const char *fn)
71 (fn[0] == '.' && fn[1] == '/')) {
72 /* ÀäÂХѥ¹¤â¤·¤¯¤Ï¥«¥ì¥ó¥È¥Ç¥£¥ì¥¯¥È¥ê¤Ê¤Î¤Ç¤½¤Î¤Þ¤Þfopen */
73 return fopen(fn, "r");
76 dn = anthy_conf_get_str("ANTHYDIR");
80 dname_len = strlen(dn);
81 full = alloca(dname_len + strlen(fn) + 2);
82 sprintf(full, "%s/%s", dn, fn);
84 return fopen(full, "r");
87 /** ¥Ð¥Ã¥¯¥¹¥é¥Ã¥·¥å¤Ë¤è¤ë¥¨¥¹¥±¡¼¥×¤â½èÍý¤¹¤ëgetc
88 * ¥¨¥¹¥±¡¼¥×¤µ¤ì¤¿Ê¸»ú¤Ê¤é¤ÐÊÖ¤êÃͤÏ1¤Ë¤Ê¤ë¡£
95 int c2 = fgetc(g_ps.fp);
113 #define myisblank(c) ((c) == ' ' || (c) == '\t')
115 /* ¹Ô¤Ë°ìʸ»úÄɲ乤ë */
117 pushchar(struct line_stat *ls, int cc)
119 if (ls->buf_index == MAX_TOKEN_LEN - 1) {
120 ls->buf[MAX_TOKEN_LEN - 1] = 0;
122 ls->buf[ls->buf_index] = cc;
128 get_token_in(struct line_stat *ls)
132 if (ls->stat == PS_EOF) {
135 if (ls->stat == PS_RET) {
138 /* ¥È¡¼¥¯¥ó¤¬»Ï¤Þ¤ë¤Þ¤Ç¶õÇò¤òÆɤßÈô¤Ð¤¹ */
141 } while (cc > 0 && myisblank(cc) && esc == 0);
150 if (cc == '\"' && !esc) {
163 if (cc == '\n' && !esc) {
169 if (!in_quote && myisblank(cc)) {
172 if (in_quote && cc == '\"' && !esc) {
191 t = get_token_in(&ls);
199 g_ps.tokens = realloc(g_ps.tokens, sizeof(char *)*g_ps.nr_token);
200 g_ps.tokens[g_ps.nr_token-1] = strdup(t);
208 if (g_ps.nr_token != 2) {
209 anthy_log(0, "Syntax error in include directive.\n");
212 if (g_ps.cur_fpp > MAX_INCLUDE_DEPTH - 1) {
213 anthy_log(0, "Too deep include.\n");
216 fp = open_file_in_confdir(g_ps.tokens[1]);
218 anthy_log(0, "Failed to open %s.\n", g_ps.tokens[1]);
222 g_ps.fp_stack[g_ps.cur_fpp] = fp;
226 /* ¥¤¥ó¥¯¥ë¡¼¥É¤Î¥Í¥¹¥È¤ò²¼¤²¤ë */
232 g_ps.fp = g_ps.fp_stack[g_ps.cur_fpp];
246 /* EOFÅù¤Ç¤³¤ì°Ê¾åÆɤá¤ó */
247 if (g_ps.cur_fpp > 0) {
254 if (g_ps.nr_token < 1) {
257 if (!strcmp(g_ps.tokens[0], "\\include")) {
260 }else if (!strcmp(g_ps.tokens[0], "\\eof")) {
261 if (g_ps.cur_fpp > 0) {
269 if (g_ps.tokens[0][0] == '#'){
275 anthy_free_line(void)
278 if (g_ps.tokens) { /* ÉÔÀµ¤Ê¥á¥â¥ê¥¢¥¯¥»¥¹¤ÎËÉ»ß */
279 for (i = 0; i < g_ps.nr_token; i++) {
280 free(g_ps.tokens[i]);
289 anthy_open_file(const char *fn)
291 g_ps.fp_stack[0] = open_file_in_confdir(fn);
292 if (!g_ps.fp_stack[0]) {
295 /* ¥Ñ¡¼¥¶¤Î¾õÂÖ¤ò½é´ü²½¤¹¤ë */
297 g_ps.fp = g_ps.fp_stack[0];
299 g_ps.nr_token = 0; /* ½é´ü²½Ëº¤ì¤Î½¤Àµ */
300 g_ps.tokens = NULL; /* ½é´ü²½Ëº¤ì¤Î½¤Àµ */
305 anthy_close_file(void)
307 if (g_ps.fp != stdin) {
310 anthy_free_line(); /* ¸å»ÏËö˺¤ì¤ÎËÉ»ß */
314 anthy_read_line(char ***tokens, int *nr)
317 *tokens = g_ps.tokens;
326 anthy_get_line_number(void)
328 return g_ps.line_num;