/[cvs]/eggdrop1.4/src/language.c
ViewVC logotype

Contents of /eggdrop1.4/src/language.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (show annotations) (download) (as text) (vendor branch)
Wed Jun 23 19:51:29 1999 UTC (22 years, 4 months ago) by segfault
Branch: eggdev
CVS Tags: start
Changes since 1.1: +0 -0 lines
File MIME type: text/x-chdr
Eggdrop 1.3.28 CVS Code

1 /*
2 * language.c - language support code.
3 */
4
5 /*
6 * DOES:
7 * Nothing <- typical BB code :)
8 *
9 * WILL DO:
10 * Upon loading:
11 * o default loads core.english.lang, if possible.
12 * Commands:
13 * DCC .language <language>
14 * DCC .ldump
15 *
16 * FILE FORMAT: language.lang
17 * <textidx>,<text>
18 * TEXT MESSAGE USAGE:
19 * get_language(<textidx> [,<PARMS>])
20 *
21 * ADDING LANGUAGES:
22 * o Copy an existing language.lang to a new .lang
23 * file and modify as needed. Use %1...%n, where
24 * necessary, for plug-in insertions of parameters
25 * (see language.lang).
26 * o Ensure <language>.lang is in the lang directory.
27 * o .language <language>
28 *
29 */
30
31 #include "main.h"
32
33 extern struct dcc_t *dcc;
34
35 static int langloaded = 0;
36
37 typedef struct lang_t {
38 int idx;
39 char *text;
40 struct lang_t *next;
41 } lang_tab;
42
43 static lang_tab *langtab[64];
44
45 static int add_message(int lidx, char *ltext)
46 {
47 lang_tab *l = langtab[lidx & 63];
48
49 while (l) {
50 if (l->idx && (l->idx == lidx)) {
51 nfree(l->text);
52 l->text = nmalloc(strlen(ltext) + 1);
53 strcpy(l->text, ltext);
54 return 1;
55 }
56 if (!l->next)
57 break;
58 l = l->next;
59 }
60 if (l) {
61 l->next = nmalloc(sizeof(lang_tab));
62 l = l->next;
63 } else
64 l = langtab[lidx & 63] = nmalloc(sizeof(lang_tab));
65 l->idx = lidx;
66 l->text = nmalloc(strlen(ltext) + 1);
67 strcpy(l->text, ltext);
68 l->next = 0;
69 return 0;
70 }
71
72 int cmd_loadlanguage(struct userrec *u, int idx, char *par)
73 {
74 FILE *FLANG;
75 char langfile[100];
76 char *lread;
77 int lidx;
78 char ltext[512];
79 char lbuf[512];
80 int lline = 0;
81 int lskip;
82 int ltexts = 0;
83 int ladd = 0, lupdate = 0;
84 char *ctmp, *ctmp1;
85
86 context;
87 langloaded = 0;
88 if (!par || !par[0]) {
89 dprintf(idx, "Usage: language <language>\n");
90 return 0;
91 }
92 if (idx != DP_LOG)
93 putlog(LOG_CMDS, "*", "#%s# language %s", dcc[idx].nick, par);
94 if (par[0] == '.' && par[0] == '/')
95 strcpy(langfile, par);
96 else
97 sprintf(langfile, "./language/%s.lang", par);
98
99 FLANG = fopen(langfile, "r");
100 if (FLANG == NULL) {
101 dprintf(idx, "Can't load language module: %s\n", langfile);
102 return 0;
103 }
104 lskip = 0;
105 while ((lread = fgets(lbuf, 511, FLANG))) {
106 lline++;
107 if (lbuf[0] != '#' || lskip) {
108 if (sscanf(lbuf, "%s", ltext) != EOF) {
109 if (sscanf(lbuf, "0x%x,%500c", &lidx, ltext) != 2) {
110 putlog(LOG_MISC, "*", "Malformed text line in %s at %d.",
111 langfile, lline);
112 } else {
113 ltexts++;
114 ctmp = strchr(ltext, '\n');
115 *ctmp = 0;
116 while (ltext[strlen(ltext) - 1] == '\\') {
117 ltext[strlen(ltext) - 1] = 0;
118 if (fgets(lbuf, 511, FLANG)) {
119 lline++;
120 ctmp = strchr(lbuf, '\n');
121 *ctmp = 0;
122 if (strlen(lbuf) + strlen(ltext) > 511) {
123 putlog(LOG_MISC, "*", "Language: Message 0x%lx in %s at line %d too long.",
124 lidx, langfile, lline);
125 lskip = 1;
126 } else
127 strcpy(strchr(ltext, 0), lbuf);
128 }
129 }
130 }
131 /* We gotta fix \n's here as, being arguments to sprintf(), */
132 /* they won't get translated */
133 ctmp = ltext;
134 ctmp1 = ltext;
135 while (*ctmp1) {
136 if (*ctmp1 == '\\' && *(ctmp1 + 1) == 'n') {
137 *ctmp = '\n';
138 ctmp1++;
139 } else
140 *ctmp = *ctmp1;
141 ctmp++;
142 ctmp1++;
143 }
144 *ctmp = '\0';
145 if (add_message(lidx, ltext)) {
146 lupdate++;
147 } else
148 ladd++;
149 }
150 } else {
151 ctmp = strchr(lbuf, '\n');
152 if (lskip && (strlen(lbuf) == 1 || *(ctmp - 1) != '\\'))
153 lskip = 0;
154 }
155 }
156 fclose(FLANG);
157 putlog(LOG_MISC, "*", "LANG: %d messages of %d lines loaded from %s",
158 ltexts, lline, langfile);
159 putlog(LOG_MISC, "*", "LANG: %d adds, %d updates to message table",
160 ladd, lupdate);
161 langloaded = 1;
162 return 0;
163 }
164
165 static int cmd_languagedump(struct userrec *u, int idx, char *par)
166 {
167 lang_tab *l;
168 char ltext2[512];
169 int idx2, i;
170
171 context;
172 putlog(LOG_CMDS, "*", "#%s# ldump %s", dcc[idx].nick, par);
173 if (par[0]) {
174 /* atoi (hence strtol) don't work right here for hex */
175 if (strlen(par) > 2 && par[0] == '0' && par[1] == 'x')
176 sscanf(par, "%x", &idx2);
177 else
178 idx2 = (int) strtol(par, (char **) NULL, 10);
179 strcpy(ltext2, get_language(idx2));
180 dprintf(idx, "0x%x: %s\n", idx2, ltext2);
181 return 0;
182 }
183 dprintf(idx, " LANGIDX TEXT\n");
184 for (i = 0; i < 64; i++)
185 for (l = langtab[i]; l; l = l->next)
186 dprintf(idx, "0x%x %s\n", l->idx, l->text);
187 return 0;
188 }
189
190 static char text[512];
191 char *get_language(int idx)
192 {
193 lang_tab *l = langtab[idx & 63];
194
195 if (!idx)
196 return "MSG-0-";
197 while (l) {
198 if (idx == l->idx)
199 return l->text;
200 l = l->next;
201 }
202 sprintf(text, "MSG%03X", idx);
203 return text;
204 }
205
206 int expmem_language()
207 {
208 lang_tab *l;
209 int i, size = 0;
210
211 for (i = 0; i < 64; i++)
212 for (l = langtab[i]; l; l = l->next) {
213 size += sizeof(lang_tab);
214 size += (strlen(l->text) + 1);
215 }
216 return size;
217 }
218
219 /* a report on the module status - not sure if we need this now :/ */
220 static int cmd_languagestatus(struct userrec *u, int idx, char *par)
221 {
222 int ltexts = 0;
223 int maxdepth = 0, used = 0, empty = 0, i, c;
224 lang_tab *l;
225
226 for (i = 0; i < 64; i++) {
227 c = 0;
228 for (l = langtab[i]; l; l = l->next)
229 c++;
230 if (c > maxdepth)
231 maxdepth = c;
232 if (c)
233 used++;
234 else
235 empty++;
236 ltexts += c;
237 }
238 context;
239 dprintf(idx, "language code report:\n");
240 dprintf(idx, " Table size: %d bytes\n", expmem_language());
241 dprintf(idx, " Text messages: %d\n", ltexts);
242 dprintf(idx, " %d used, %d unused, maxdepth %d, avg %f\n",
243 used, empty, maxdepth, (float) ltexts / 64.0);
244 return 0;
245 }
246
247 static int tcl_language STDVAR
248 {
249 BADARGS(2, 2, " language");
250 (void) cmd_loadlanguage(0, DP_LOG, argv[1]);
251 if (!langloaded) {
252 Tcl_AppendResult(irp, "Load failed.", NULL);
253 return TCL_ERROR;
254 }
255 return TCL_OK;
256 }
257
258 static cmd_t langdcc[] =
259 {
260 {"language", "n", cmd_loadlanguage, NULL},
261 {"ldump", "n", cmd_languagedump, NULL},
262 {"lstat", "n", cmd_languagestatus, NULL},
263 };
264
265 static tcl_cmds langtcls[] =
266 {
267 {"language", tcl_language},
268 {0, 0}
269 };
270
271 void init_language(char *default_lang)
272 {
273 int i;
274
275 context;
276 if (default_lang) {
277 for (i = 0; i < 32; i++)
278 langtab[i] = 0;
279 cmd_loadlanguage(0, DP_LOG, default_lang); /* and robey said super-dprintf
280 * was silly :) */
281 } else {
282 add_tcl_commands(langtcls);
283 add_builtins(H_dcc, langdcc, 3);
284 }
285 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23