2 * Anthy¤ÎÀßÄê¤Î¥Ç¡¼¥¿¥Ù¡¼¥¹
3 * conf_init¤ÇÀßÄꤵ¤ì¤ëÊÑ¿ô¤Èconf_override¤ÇÀßÄꤵ¤ì¤ë
6 * Copyright (C) 2000-2007 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
26 #include <sys/types.h>
31 #include <anthy/alloc.h>
32 #include <anthy/conf.h>
33 #include <anthy/logger.h>
47 static struct val_ent *ent_list;
48 /** ½é´ü²½ºÑ¤ß¥Õ¥é¥° */
49 static int confIsInit;
50 static allocator val_ent_ator;
55 struct val_ent *v = p;
62 /** ÊÑ¿ô̾¤ËÂбþ¤¹¤ëval_ent¤ò¼èÆÀ¤¹¤ë */
63 static struct val_ent *
64 find_val_ent(const char *v)
67 for (e = ent_list; e; e = e->next) {
68 if(!strcmp(v, e->var)) {
72 e = malloc(sizeof(struct val_ent));
83 /** ${ÊÑ¿ô̾}¤Î·Á¤ÎÊÑ¿ô¤ÎÃͤò¼èÆÀ¤¹¤ë
86 get_subst(const char *s)
88 if (s[0] == '$' && s[1] == '{' &&
91 char *var = strdup(&s[2]);
92 char *k = strchr(var, '}');
94 val = find_val_ent(var);
96 if (!val || !val->val) {
112 ensure_buffer(struct expand_buf *eb, int count)
114 int required = count - (eb->size - eb->len) + 16;
116 eb->size += required;
117 eb->buf = realloc(eb->buf, eb->size);
118 eb->cur = &eb->buf[eb->len];
123 expand_string(const char *s)
125 struct expand_buf eb;
128 eb.buf = malloc(eb.size);
133 const char *subst = get_subst(s);
135 int len = strlen(subst);
136 ensure_buffer(&eb, len + 1);
138 strcat(eb.buf, subst);
151 ensure_buffer(&eb, 256);
155 res = strdup(eb.buf);
161 add_val(const char *var, const char *val)
164 e = find_val_ent(var);
166 free((void *)e->val);
168 e->val = expand_string(val);
177 fn = anthy_conf_get_str("CONFFILE");
180 anthy_log(0, "Failed to open %s\n", fn);
183 while(fgets(buf, 1024, fp)) {
185 char var[1024], val[1024];
186 if (sscanf(buf, "%s %s", var, val) == 2){
195 anthy_do_conf_override(const char *var, const char *val)
197 if (!strcmp(var,"CONFFILE")) {
199 anthy_do_conf_init();
201 anthy_do_conf_init();
206 /* ¥æ¥Ë¡¼¥¯¤Ê¥»¥Ã¥·¥ç¥óID¤ò³ÎÊݤ¹¤ë */
207 #define SID_FORMAT "%s-%08x-%05d" /* HOST-TIME-PID */
208 #define MAX_SID_LEN (MAX_HOSTNAME+8+5+2)
209 #define MAX_HOSTNAME 64
212 alloc_session_id(void)
216 char hn[MAX_HOSTNAME];
217 char sid[MAX_SID_LEN];
220 gethostname(hn, MAX_HOSTNAME);
221 hn[MAX_HOSTNAME-1] = '\0';
222 sprintf(sid, SID_FORMAT, hn, (unsigned int)t & 0xffffffff, pid & 0xffff);
223 add_val("SESSION-ID", sid);
227 anthy_do_conf_init(void)
233 val_ent_ator = anthy_create_allocator(sizeof(struct val_ent), val_ent_dtor);
234 /*¥Ç¥Õ¥©¥ë¥È¤ÎÃͤòÀßÄꤹ¤ë¡£*/
235 add_val("VERSION", VERSION);
236 fn = anthy_conf_get_str("CONFFILE");
238 add_val("CONFFILE", CONF_DIR"/anthy-conf");
240 pw = getpwuid(getuid());
241 add_val("HOME", pw->pw_dir);
249 anthy_conf_free(void)
251 struct val_ent *e, *next;
252 for (e = ent_list; e; e = next) {
253 free((char *)e->var);
254 free((char *)e->val);
263 anthy_conf_get_str(const char *var)
266 e = find_val_ent(var);