/[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.14 - (show annotations) (download) (as text)
Mon May 29 02:29:09 2000 UTC (19 years, 1 month ago) by guppy
Branch: MAIN
CVS Tags: eggdrop10404, HEAD
Changes since 1.13: +32 -23 lines
File MIME type: text/x-chdr
gup5_14.diff (moves alot of language messages to debug messages)

1 /*
2 * language.c -- handles:
3 * language support code
4 *
5 * $Id: language.c,v 1.13 2000/01/08 21:23:14 per Exp $
6 */
7 /*
8 * Copyright (C) 1997 Robey Pointer
9 * Copyright (C) 1999, 2000 Eggheads
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 /*
27 * DOES:
28 * Nothing <- typical BB code :)
29 *
30 * ENVIRONMENT VARIABLES:
31 * EGG_LANG - language to use (default: "english")
32 * EGG_LANGDIR - directory with all lang files
33 * (default: "./language")
34 * WILL DO:
35 * Upon loading:
36 * o default loads section core, if possible.
37 * Commands:
38 * DCC .+lang <language>
39 * DCC .-lang <language>
40 * DCC .+lsec <section>
41 * DCC .-lsec <section>
42 * DCC .relang
43 * DCC .ldump
44 * DCC .lstat
45 *
46 * FILE FORMAT: language.lang
47 * <textidx>,<text>
48 * TEXT MESSAGE USAGE:
49 * get_language(<textidx> [,<PARMS>])
50 *
51 * ADDING LANGUAGES:
52 * o Copy an existing <section>.<oldlanguage>.lang to a
53 * new .lang file and modify as needed.
54 * Use %s or %d where necessary, for plug-in
55 * insertions of parameters (see core.english.lang).
56 * o Ensure <section>.<newlanguage>.lang is in the lang
57 * directory.
58 * o .+lang <newlanguage>
59 * ADDING SECTIONS:
60 * o Create a <newsection>.english.lang file.
61 * o Add add_lang_section("<newsection>"); to your module
62 * startup function.
63 *
64 */
65
66 #include "main.h"
67
68 extern struct dcc_t *dcc;
69
70 typedef struct lang_st {
71 struct lang_st *next;
72 char *lang;
73 char *section;
74 } lang_sec;
75
76 typedef struct lang_pr {
77 struct lang_pr *next;
78 char *lang;
79 } lang_pri;
80
81 typedef struct lang_t {
82 int idx;
83 char *text;
84 struct lang_t *next;
85 } lang_tab;
86
87 static lang_tab *langtab[64];
88 static lang_sec *langsection = NULL;
89 static lang_pri *langpriority = NULL;
90
91 static int del_lang(char *);
92 static int add_message(int, char *);
93 static void recheck_lang_sections(void);
94 static void read_lang(char *);
95 void add_lang_section(char *);
96 int del_lang_section(char *);
97 int exist_lang_section(char *);
98 static char *get_specific_langfile(char *, lang_sec *);
99 static char *get_langfile(lang_sec *);
100 static int split_lang(char *, char **, char **);
101 int cmd_loadlanguage(struct userrec *, int, char *);
102
103
104 /* add a new preferred language to the list of languages. Newly added
105 * languages get the highest priority.
106 */
107 void add_lang(char *lang)
108 {
109 lang_pri *lp = langpriority, *lpo = NULL;
110
111 Context;
112 while (lp) {
113 /* the language already exists, moving to the beginning */
114 if (!strcmp(lang, lp->lang)) {
115 /* already at the front? */
116 if (!lpo)
117 return;
118 lpo->next = lp->next;
119 lp->next = lpo;
120 langpriority = lp;
121 return;
122 }
123 lpo = lp;
124 lp = lp->next;
125 }
126
127 /* no existing entry, create a new one */
128 lp = nmalloc(sizeof(lang_pri));
129 lp->lang = nmalloc(strlen(lang) + 1);
130 strcpy(lp->lang, lang);
131 lp->next = NULL;
132
133 /* if we have other entries, point to the beginning of the old list */
134 if (langpriority)
135 lp->next = langpriority;
136 langpriority = lp;
137 debug1("LANG: Language loaded: %s", lang);
138 }
139
140 /* remove a language from the list of preferred languages. */
141 static int del_lang(char *lang)
142 {
143 lang_pri *lp = langpriority, *lpo = NULL;
144
145 Context;
146 while (lp) {
147 /* found the language? */
148 if (!strcmp(lang, lp->lang)) {
149 if (lpo)
150 lpo->next = lp->next;
151 else
152 langpriority = lp->next;
153 if (lp->lang)
154 nfree(lp->lang);
155 nfree(lp);
156 debug1("LANG: Language unloaded: %s", lang);
157 return 1;
158 }
159 lpo = lp;
160 lp = lp->next;
161 }
162 /* language not found */
163 return 0;
164 }
165
166 static int add_message(int lidx, char *ltext)
167 {
168 lang_tab *l = langtab[lidx & 63];
169
170 while (l) {
171 if (l->idx && (l->idx == lidx)) {
172 nfree(l->text);
173 l->text = nmalloc(strlen(ltext) + 1);
174 strcpy(l->text, ltext);
175 return 1;
176 }
177 if (!l->next)
178 break;
179 l = l->next;
180 }
181 if (l) {
182 l->next = nmalloc(sizeof(lang_tab));
183 l = l->next;
184 } else
185 l = langtab[lidx & 63] = nmalloc(sizeof(lang_tab));
186 l->idx = lidx;
187 l->text = nmalloc(strlen(ltext) + 1);
188 strcpy(l->text, ltext);
189 l->next = 0;
190 return 0;
191 }
192
193 /* recheck all sections and check if any language files are available
194 * which match the preferred language(s) more closely
195 */
196 static void recheck_lang_sections(void)
197 {
198 lang_sec *ls = langsection;
199 char *langfile;
200
201 Context;
202 while (ls) {
203 if (ls->section) {
204 langfile = get_langfile(ls);
205 /* found a language with a more preferred language? */
206 if (langfile) {
207 read_lang(langfile);
208 nfree(langfile);
209 }
210 }
211 ls = ls->next;
212 }
213 }
214
215 /* parse a language file */
216 static void read_lang(char *langfile)
217 {
218 FILE *FLANG;
219 char lbuf[512];
220 char *ltext = NULL;
221 char *ctmp, *ctmp1;
222 int lidx;
223 int lline = 0;
224 int lskip;
225 int ltexts = 0;
226 int ladd = 0, lupdate = 0;
227
228 Context;
229 FLANG = fopen(langfile, "r");
230 if (FLANG == NULL) {
231 putlog(LOG_MISC, "*", "LANG: unexpected: reading from file %s failed.",
232 langfile);
233 return;
234 }
235
236 lskip = 0;
237 while (fgets(lbuf, 511, FLANG)) {
238 lline++;
239 if (lbuf[0] != '#' || lskip) {
240 ltext = nrealloc(ltext, 512);
241 if (sscanf(lbuf, "%s", ltext) != EOF) {
242 if (sscanf(lbuf, "0x%x,%500c", &lidx, ltext) != 2) {
243 putlog(LOG_MISC, "*", "Malformed text line in %s at %d.",
244 langfile, lline);
245 } else {
246 ltexts++;
247 ctmp = strchr(ltext, '\n');
248 *ctmp = 0;
249 while (ltext[strlen(ltext) - 1] == '\\') {
250 ltext[strlen(ltext) - 1] = 0;
251 if (fgets(lbuf, 511, FLANG)) {
252 lline++;
253 ctmp = strchr(lbuf, '\n');
254 *ctmp = 0;
255 ltext = nrealloc(ltext, strlen(lbuf) + strlen(ltext) + 1);
256 strcpy(strchr(ltext, 0), lbuf);
257 }
258 }
259 }
260 /* We gotta fix \n's here as, being arguments to sprintf(), */
261 /* they won't get translated */
262 ctmp = ltext;
263 ctmp1 = ltext;
264 while (*ctmp1) {
265 if ((*ctmp1 == '\\') && (*(ctmp1 + 1) == 'n')) {
266 *ctmp = '\n';
267 ctmp1++;
268 } else if ((*ctmp1 == '\\') && (*(ctmp1 + 1) == 't')) {
269 *ctmp = '\t';
270 ctmp1++;
271 } else
272 *ctmp = *ctmp1;
273 ctmp++;
274 ctmp1++;
275 }
276 *ctmp = '\0';
277 if (add_message(lidx, ltext)) {
278 lupdate++;
279 } else
280 ladd++;
281 }
282 } else {
283 ctmp = strchr(lbuf, '\n');
284 if (lskip && (strlen(lbuf) == 1 || *(ctmp - 1) != '\\'))
285 lskip = 0;
286 }
287 }
288 nfree(ltext);
289 fclose(FLANG);
290
291 debug3("LANG: %d messages of %d lines loaded from %s", ltexts, lline,
292 langfile);
293 debug2("LANG: %d adds, %d updates to message table", ladd, lupdate);
294 }
295
296 /* Returns 1 if the section exists, otherwise 0.
297 */
298 int exist_lang_section(char *section)
299 {
300 lang_sec *ls;
301
302 for (ls = langsection; ls; ls = ls->next)
303 if (!strcmp(section, ls->section))
304 return 1;
305 return 0;
306 }
307
308 /* Add a new language section. e.g. section "core"
309 * Load an apropriate language file for the specified section.
310 */
311 void add_lang_section(char *section)
312 {
313 char *langfile = NULL;
314 lang_sec *ls, *ols = NULL;
315 int ok = 0;
316
317 Context;
318 for (ls = langsection; ls; ols = ls, ls = ls->next)
319 /* already know of that section? */
320 if (!strcmp(section, ls->section))
321 return;
322
323 /* create new section entry */
324 ls = nmalloc(sizeof(lang_sec));
325 ls->section = nmalloc(strlen(section) + 1);
326 strcpy(ls->section, section);
327 ls->lang = NULL;
328 ls->next = NULL;
329
330 /* connect to existing list of sections */
331 if (ols)
332 ols->next = ls;
333 else
334 langsection = ls;
335 debug1("LANG: Section loaded: %s", section);
336 /* Always load base language */
337 langfile = get_specific_langfile(BASELANG, ls);
338 if (langfile) {
339 read_lang(langfile);
340 nfree(langfile);
341 ok = 1;
342 }
343 /* Now overwrite base language with a more preferred one */
344 langfile = get_langfile(ls);
345 if (!langfile) {
346 if (!ok)
347 putlog(LOG_MISC, "*", "LANG: No lang files found for section %s.",
348 section);
349 return;
350 }
351 read_lang(langfile);
352 nfree(langfile);
353 }
354
355 int del_lang_section(char *section)
356 {
357 lang_sec *ls, *ols = NULL;
358
359 for (ls = langsection, ols = NULL; ls; ols = ls, ls = ls->next)
360 if (ls->section && !strcmp(ls->section, section)) {
361 if (ols)
362 ols->next = ls->next;
363 else
364 langsection = ls->next;
365 nfree(ls->section);
366 if (ls->lang)
367 nfree(ls->lang);
368 nfree(ls);
369 debug1("LANG: Section unloaded: %s", section);
370 return 1;
371 }
372 return 0;
373 }
374
375 static char *get_specific_langfile(char *language, lang_sec *sec)
376 {
377 char *ldir = getenv("EGG_LANGDIR");
378 char *langfile;
379 FILE *sfile = NULL;
380
381 if (!ldir)
382 ldir = LANGDIR;
383 langfile = nmalloc(strlen(ldir) + strlen(sec->section) + strlen(language)+8);
384 sprintf(langfile, "%s/%s.%s.lang", ldir, sec->section, language);
385 sfile = fopen(langfile, "r");
386 if (sfile) {
387 fclose(sfile);
388 /* save language used for this section */
389 sec->lang = nrealloc(sec->lang, strlen(language) + 1);
390 strcpy(sec->lang, language);
391 return langfile;
392 }
393 nfree(langfile);
394 return NULL;
395 }
396
397 /* Searches for available language files and returns the file with the
398 * most preferred language.
399 */
400 static char *get_langfile(lang_sec *sec)
401 {
402 char *langfile;
403 lang_pri *lp = langpriority;
404
405 Context;
406 while (lp) {
407 /* there is no need to reload the same language */
408 if (sec->lang && !strcmp(sec->lang, lp->lang))
409 return NULL;
410 langfile = get_specific_langfile(lp->lang, sec);
411 if (langfile)
412 return langfile;
413 lp = lp->next;
414 }
415 /* we did not find any files, clear the language field */
416 if (sec->lang)
417 nfree(sec->lang);
418 sec->lang = NULL;
419 return NULL;
420 }
421
422 /* Split up a string /path/<section>.<language>.lang into the
423 * needed information for the new language system.
424 * Only needed for compability functions.
425 */
426 static int split_lang(char *par, char **lang, char **section)
427 {
428 char *p;
429
430 Context;
431 p = strrchr(par, '/');
432 /* path attached? */
433 if (p)
434 *section = p + 1;
435 else
436 *section = par;
437 p = strchr(*section, '.');
438 if (p)
439 p[0] = 0;
440 else
441 return 0;
442 *lang = p + 1;
443 p = strstr(*lang, ".lang");
444 if (p)
445 p[0] = 0;
446 return 1;
447 }
448
449 /* compability function to allow users/modules to use the old command. */
450 int cmd_loadlanguage(struct userrec *u, int idx, char *par)
451 {
452 char *section, *lang, *buf;
453
454 Context;
455 dprintf(idx, "Note: This command is obsoleted by +lang.\n");
456 if (!par || !par[0]) {
457 dprintf(idx, "Usage: language <section>.<language>\n");
458 return 0;
459 }
460 if (idx != DP_LOG)
461 putlog(LOG_CMDS, "*", "#%s# language %s", dcc[idx].nick, par);
462 buf = nmalloc(strlen(par)+1);
463 strcpy(buf, par);
464 if (!split_lang(buf, &lang, &section)) {
465 nfree(buf);
466 dprintf(idx, "Invalid parameter %s.\n", par);
467 return 0;
468 }
469 add_lang(lang);
470 add_lang_section(section);
471 nfree(buf);
472 recheck_lang_sections();
473 return 0;
474 }
475
476 static int cmd_plslang(struct userrec *u, int idx, char *par)
477 {
478 Context;
479 if (!par || !par[0]) {
480 dprintf(idx, "Usage: +lang <language>\n");
481 return 0;
482 }
483 putlog(LOG_CMDS, "*", "#%s# +lang %s", dcc[idx].nick, par);
484 add_lang(par);
485 recheck_lang_sections();
486 return 0;
487 }
488
489 static int cmd_mnslang(struct userrec *u, int idx, char *par)
490 {
491 Context;
492 if (!par || !par[0]) {
493 dprintf(idx, "Usage: -lang <language>\n");
494 return 0;
495 }
496 putlog(LOG_CMDS, "*", "#%s# -lang %s", dcc[idx].nick, par);
497 if (!del_lang(par))
498 dprintf(idx, "Language %s not found.\n", par);
499 else
500 recheck_lang_sections();
501 return 0;
502 }
503
504 static int cmd_plslsec(struct userrec *u, int idx, char *par)
505 {
506 Context;
507 if (!par || !par[0]) {
508 dprintf(idx, "Usage: +lsec <section>\n");
509 return 0;
510 }
511 putlog(LOG_CMDS, "*", "#%s# +lsec %s", dcc[idx].nick, par);
512 add_lang_section(par);
513 return 0;
514 }
515
516 static int cmd_mnslsec(struct userrec *u, int idx, char *par)
517 {
518 Context;
519 if (!par || !par[0]) {
520 dprintf(idx, "Usage: -lsec <section>\n");
521 return 0;
522 }
523 putlog(LOG_CMDS, "*", "#%s# -lsec %s", dcc[idx].nick, par);
524 if (!del_lang_section(par))
525 dprintf(idx, "Section %s not found.\n", par);
526 return 0;
527 }
528
529 static int cmd_relang(struct userrec *u, int idx, char *par)
530 {
531 dprintf(idx, "Rechecking language sections...\n");
532 recheck_lang_sections();
533 return 0;
534 }
535
536 static int cmd_languagedump(struct userrec *u, int idx, char *par)
537 {
538 lang_tab *l;
539 char ltext2[512];
540 int idx2, i;
541
542 Context;
543 putlog(LOG_CMDS, "*", "#%s# ldump %s", dcc[idx].nick, par);
544 if (par[0]) {
545 /* atoi (hence strtol) don't work right here for hex */
546 if (strlen(par) > 2 && par[0] == '0' && par[1] == 'x')
547 sscanf(par, "%x", &idx2);
548 else
549 idx2 = (int) strtol(par, (char **) NULL, 10);
550 strcpy(ltext2, get_language(idx2));
551 dprintf(idx, "0x%x: %s\n", idx2, ltext2);
552 return 0;
553 }
554 dprintf(idx, " LANGIDX TEXT\n");
555 for (i = 0; i < 64; i++)
556 for (l = langtab[i]; l; l = l->next)
557 dprintf(idx, "0x%x %s\n", l->idx, l->text);
558 return 0;
559 }
560
561 static char text[512];
562 char *get_language(int idx)
563 {
564 lang_tab *l = langtab[idx & 63];
565
566 if (!idx)
567 return "MSG-0-";
568 while (l) {
569 if (idx == l->idx)
570 return l->text;
571 l = l->next;
572 }
573 sprintf(text, "MSG%03X", idx);
574 return text;
575 }
576
577 int expmem_language()
578 {
579 lang_tab *l;
580 lang_sec *ls = langsection;
581 lang_pri *lp = langpriority;
582 int i, size = 0;
583
584 Context;
585 for (i = 0; i < 64; i++)
586 for (l = langtab[i]; l; l = l->next) {
587 size += sizeof(lang_tab);
588 size += (strlen(l->text) + 1);
589 }
590 while (ls) {
591 size += sizeof(lang_sec);
592 if (ls->section)
593 size += strlen(ls->section)+1;
594 if (ls->lang)
595 size += strlen(ls->lang)+1;
596 ls = ls->next;
597 }
598 while (lp) {
599 size += sizeof(lang_pri);
600 if (lp->lang)
601 size += strlen(lp->lang)+1;
602 lp = lp->next;
603 }
604 return size;
605 }
606
607 /* a report on the module status - not sure if we need this now :/ */
608 static int cmd_languagestatus(struct userrec *u, int idx, char *par)
609 {
610 int ltexts = 0;
611 register int i, c, maxdepth = 0, used = 0, empty = 0;
612 lang_tab *l;
613 lang_sec *ls = langsection;
614 lang_pri *lp = langpriority;
615
616 Context;
617 putlog(LOG_CMDS, "*", "#%s# lstat %s", dcc[idx].nick, par);
618 for (i = 0; i < 64; i++) {
619 c = 0;
620 for (l = langtab[i]; l; l = l->next)
621 c++;
622 if (c > maxdepth)
623 maxdepth = c;
624 if (c)
625 used++;
626 else
627 empty++;
628 ltexts += c;
629 }
630 Context;
631 dprintf(idx, "Language code report:\n");
632 dprintf(idx, " Table size : %d bytes\n", expmem_language());
633 dprintf(idx, " Text messages: %d\n", ltexts);
634 dprintf(idx, " %d used, %d unused, maxdepth %d, avg %f\n",
635 used, empty, maxdepth, (float) ltexts / 64.0);
636 if (lp) {
637 int c = 0;
638
639 dprintf(idx, " Supported languages:");
640 while (lp) {
641 dprintf(idx, "%s %s", c ? "," : "", lp->lang);
642 c = 1;
643 lp = lp->next;
644 }
645 dprintf(idx, "\n");
646 }
647 if (ls) {
648 dprintf(idx, "\n SECTION LANG\n");
649 dprintf(idx, " ==============================\n");
650
651 while (ls) {
652 dprintf(idx, " %-20s %s\n", ls->section,
653 ls->lang ? ls->lang : "<none>");
654 ls = ls->next;
655 }
656 }
657 return 0;
658 }
659
660 /* compability function to allow scripts to use the old command. */
661 static int tcl_language STDVAR
662 {
663 char *lang, *section, *buf;
664
665 putlog(LOG_MISC, "*", "Tcl cmd 'language' is obsolete. Use 'addlang' instead.");
666 BADARGS(2, 2, " language");
667
668 buf = nmalloc(strlen(argv[1])+1);
669 strcpy(buf, argv[1]);
670 if (!split_lang(buf, &lang, &section)) {
671 Tcl_AppendResult(irp, "Invalid parameter", NULL);
672 nfree(buf);
673 return TCL_ERROR;
674 }
675 add_lang(lang);
676 add_lang_section(section);
677 nfree(buf);
678 recheck_lang_sections();
679 return TCL_OK;
680 }
681
682 static int tcl_plslang STDVAR
683 {
684 BADARGS(2, 2, " language");
685
686 add_lang(argv[1]);
687 recheck_lang_sections();
688
689 return TCL_OK;
690 }
691
692 static int tcl_mnslang STDVAR
693 {
694 BADARGS(2, 2, " language");
695
696 if (!del_lang(argv[1])) {
697 Tcl_AppendResult(irp, "Language not found.", NULL);
698 return TCL_ERROR;
699 }
700 recheck_lang_sections();
701
702 return TCL_OK;
703 }
704
705 static int tcl_addlangsection STDVAR
706 {
707 BADARGS(2, 2, " section");
708
709 add_lang_section(argv[1]);
710 return TCL_OK;
711 }
712
713 static int tcl_dellangsection STDVAR
714 {
715 BADARGS(2, 2, " section");
716
717 if (!del_lang_section(argv[1])) {
718 Tcl_AppendResult(irp, "Section not found", NULL);
719 return TCL_ERROR;
720 }
721 return TCL_OK;
722 }
723
724 static int tcl_relang STDVAR
725 {
726 recheck_lang_sections();
727 return TCL_OK;
728 }
729
730 static cmd_t langdcc[] =
731 {
732 {"language", "n", cmd_loadlanguage, NULL},
733 {"+lang", "n", cmd_plslang, NULL},
734 {"-lang", "n", cmd_mnslang, NULL},
735 {"+lsec", "n", cmd_plslsec, NULL},
736 {"-lsec", "n", cmd_mnslsec, NULL},
737 {"ldump", "n", cmd_languagedump, NULL},
738 {"lstat", "n", cmd_languagestatus, NULL},
739 {"relang", "n", cmd_relang, NULL},
740 {0, 0, 0, 0}
741 };
742
743 static tcl_cmds langtcls[] =
744 {
745 {"language", tcl_language},
746 {"addlang", tcl_plslang},
747 {"dellang", tcl_mnslang},
748 {"addlangsection", tcl_addlangsection},
749 {"dellangsection", tcl_dellangsection},
750 {"relang", tcl_relang},
751 {0, 0}
752 };
753
754 void init_language(int flag)
755 {
756 int i;
757 char *deflang;
758
759 Context;
760 if (flag) {
761 for (i = 0; i < 32; i++)
762 langtab[i] = 0;
763 /* The default language is always "english" as language files are
764 * gauranteed to exist in english. */
765 add_lang("english");
766 /* Let the user choose a different, preferred language */
767 deflang = getenv("EGG_LANG");
768 if (deflang)
769 add_lang(deflang);
770 add_lang_section("core");
771 } else {
772 add_tcl_commands(langtcls);
773 add_builtins(H_dcc, langdcc);
774 }
775 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23