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

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

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


Revision 1.15 - (show annotations) (download) (as text)
Sat Jul 14 12:37:08 2001 UTC (18 years ago) by poptix
Branch: MAIN
CVS Tags: eggdrop1_6_6
Changes since 1.14: +2 -2 lines
File MIME type: text/x-chdr

Cosmetic TCL fixes, plus TCL threaded info, primarily ITE's patch.

1 /*
2 * language.c -- handles:
3 * language support code
4 *
5 * $Id: language.c,v 1.14 2001/06/30 06:29:55 guppy Exp $
6 */
7 /*
8 * Copyright (C) 1997 Robey Pointer
9 * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
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
71 typedef struct lang_st {
72 struct lang_st *next;
73 char *lang;
74 char *section;
75 } lang_sec;
76
77 typedef struct lang_pr {
78 struct lang_pr *next;
79 char *lang;
80 } lang_pri;
81
82 typedef struct lang_t {
83 int idx;
84 char *text;
85 struct lang_t *next;
86 } lang_tab;
87
88 static lang_tab *langtab[64];
89 static lang_sec *langsection = NULL;
90 static lang_pri *langpriority = NULL;
91
92 static int del_lang(char *);
93 static int add_message(int, char *);
94 static void recheck_lang_sections(void);
95 static void read_lang(char *);
96 void add_lang_section(char *);
97 int del_lang_section(char *);
98 int exist_lang_section(char *);
99 static char *get_specific_langfile(char *, lang_sec *);
100 static char *get_langfile(lang_sec *);
101 static int split_lang(char *, char **, char **);
102 int cmd_loadlanguage(struct userrec *, int, char *);
103
104
105 /* Add a new preferred language to the list of languages. Newly added
106 * languages get the highest priority.
107 */
108 void add_lang(char *lang)
109 {
110 lang_pri *lp = langpriority, *lpo = NULL;
111
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 */
142 static int del_lang(char *lang)
143 {
144 lang_pri *lp = langpriority, *lpo = NULL;
145
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;
199 char *langfile;
200
201 for (ls = langsection; ls && ls->section; ls = ls->next) {
202 langfile = get_langfile(ls);
203 /* Found a language with a more preferred language? */
204 if (langfile) {
205 read_lang(langfile);
206 nfree(langfile);
207 }
208 }
209 }
210
211 /* Parse a language file
212 */
213 static void read_lang(char *langfile)
214 {
215 FILE *FLANG;
216 char lbuf[512];
217 char *ltext = NULL;
218 char *ctmp, *ctmp1;
219 int lidx;
220 int lline = 0;
221 int lskip;
222 int ltexts = 0;
223 int ladd = 0, lupdate = 0;
224
225 FLANG = fopen(langfile, "r");
226 if (FLANG == NULL) {
227 putlog(LOG_MISC, "*", "LANG: unexpected: reading from file %s failed.",
228 langfile);
229 return;
230 }
231
232 lskip = 0;
233 while (fgets(lbuf, 511, FLANG)) {
234 lline++;
235 if (lbuf[0] != '#' || lskip) {
236 ltext = nrealloc(ltext, 512);
237 if (sscanf(lbuf, "%s", ltext) != EOF) {
238 #ifdef LIBSAFE_HACKS
239 if (sscanf(lbuf, "0x%x,%500c", &lidx, ltext) != 1) {
240 #else
241 if (sscanf(lbuf, "0x%x,%500c", &lidx, ltext) != 2) {
242 #endif
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 */
263 ctmp = ltext;
264 ctmp1 = ltext;
265 while (*ctmp1) {
266 if ((*ctmp1 == '\\') && (*(ctmp1 + 1) == 'n')) {
267 *ctmp = '\n';
268 ctmp1++;
269 } else if ((*ctmp1 == '\\') && (*(ctmp1 + 1) == 't')) {
270 *ctmp = '\t';
271 ctmp1++;
272 } else
273 *ctmp = *ctmp1;
274 ctmp++;
275 ctmp1++;
276 }
277 *ctmp = '\0';
278 if (add_message(lidx, ltext)) {
279 lupdate++;
280 } else
281 ladd++;
282 }
283 } else {
284 ctmp = strchr(lbuf, '\n');
285 if (lskip && (strlen(lbuf) == 1 || *(ctmp - 1) != '\\'))
286 lskip = 0;
287 }
288 }
289 nfree(ltext);
290 fclose(FLANG);
291
292 debug3("LANG: %d messages of %d lines loaded from %s", ltexts, lline,
293 langfile);
294 debug2("LANG: %d adds, %d updates to message table", ladd, lupdate);
295 }
296
297 /* Returns 1 if the section exists, otherwise 0.
298 */
299 int exist_lang_section(char *section)
300 {
301 lang_sec *ls;
302
303 for (ls = langsection; ls; ls = ls->next)
304 if (!strcmp(section, ls->section))
305 return 1;
306 return 0;
307 }
308
309 /* Add a new language section. e.g. section "core"
310 * Load an apropriate language file for the specified section.
311 */
312 void add_lang_section(char *section)
313 {
314 char *langfile = NULL;
315 lang_sec *ls, *ols = NULL;
316 int ok = 0;
317
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
337 /* Always load base language */
338 langfile = get_specific_langfile(BASELANG, ls);
339 if (langfile) {
340 read_lang(langfile);
341 nfree(langfile);
342 ok = 1;
343 }
344 /* Now overwrite base language with a more preferred one */
345 langfile = get_langfile(ls);
346 if (!langfile) {
347 if (!ok)
348 putlog(LOG_MISC, "*", "LANG: No lang files found for section %s.",
349 section);
350 return;
351 }
352 read_lang(langfile);
353 nfree(langfile);
354 }
355
356 int del_lang_section(char *section)
357 {
358 lang_sec *ls, *ols;
359
360 for (ls = langsection, ols = NULL; ls; ols = ls, ls = ls->next)
361 if (ls->section && !strcmp(ls->section, section)) {
362 if (ols)
363 ols->next = ls->next;
364 else
365 langsection = ls->next;
366 nfree(ls->section);
367 if (ls->lang)
368 nfree(ls->lang);
369 nfree(ls);
370 debug1("LANG: Section unloaded: %s", section);
371 return 1;
372 }
373 return 0;
374 }
375
376 static char *get_specific_langfile(char *language, lang_sec *sec)
377 {
378 char *ldir = getenv("EGG_LANGDIR");
379 char *langfile;
380 FILE *sfile = NULL;
381
382 if (!ldir)
383 ldir = LANGDIR;
384 langfile = nmalloc(strlen(ldir) + strlen(sec->section) + strlen(language)+8);
385 sprintf(langfile, "%s/%s.%s.lang", ldir, sec->section, language);
386 sfile = fopen(langfile, "r");
387 if (sfile) {
388 fclose(sfile);
389 /* Save language used for this section */
390 sec->lang = nrealloc(sec->lang, strlen(language) + 1);
391 strcpy(sec->lang, language);
392 return langfile;
393 }
394 nfree(langfile);
395 return NULL;
396 }
397
398 /* Searches for available language files and returns the file with the
399 * most preferred language.
400 */
401 static char *get_langfile(lang_sec *sec)
402 {
403 char *langfile;
404 lang_pri *lp;
405
406 for (lp = langpriority; lp; lp = lp->next) {
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 }
414 /* We did not find any files, clear the language field */
415 if (sec->lang)
416 nfree(sec->lang);
417 sec->lang = NULL;
418 return NULL;
419 }
420
421 /* Split up a string /path/<section>.<language>.lang into the
422 * needed information for the new language system.
423 * Only needed for compability functions.
424 */
425 static int split_lang(char *par, char **lang, char **section)
426 {
427 char *p;
428
429 p = strrchr(par, '/');
430 /* path attached? */
431 if (p)
432 *section = p + 1;
433 else
434 *section = par;
435 p = strchr(*section, '.');
436 if (p)
437 p[0] = 0;
438 else
439 return 0;
440 *lang = p + 1;
441 p = strstr(*lang, ".lang");
442 if (p)
443 p[0] = 0;
444 return 1;
445 }
446
447 /* Compability function to allow users/modules to use the old command.
448 */
449 int cmd_loadlanguage(struct userrec *u, int idx, char *par)
450 {
451 char *section, *lang, *buf;
452
453 dprintf(idx, "Note: This command is obsoleted by +lang.\n");
454 if (!par || !par[0]) {
455 dprintf(idx, "Usage: language <section>.<language>\n");
456 return 0;
457 }
458 if (idx != DP_LOG)
459 putlog(LOG_CMDS, "*", "#%s# language %s", dcc[idx].nick, par);
460 buf = nmalloc(strlen(par)+1);
461 strcpy(buf, par);
462 if (!split_lang(buf, &lang, &section)) {
463 nfree(buf);
464 dprintf(idx, "Invalid parameter %s.\n", par);
465 return 0;
466 }
467 add_lang(lang);
468 add_lang_section(section);
469 nfree(buf);
470 recheck_lang_sections();
471 return 0;
472 }
473
474 static int cmd_plslang(struct userrec *u, int idx, char *par)
475 {
476 if (!par || !par[0]) {
477 dprintf(idx, "Usage: +lang <language>\n");
478 return 0;
479 }
480 putlog(LOG_CMDS, "*", "#%s# +lang %s", dcc[idx].nick, par);
481 add_lang(par);
482 recheck_lang_sections();
483 return 0;
484 }
485
486 static int cmd_mnslang(struct userrec *u, int idx, char *par)
487 {
488 if (!par || !par[0]) {
489 dprintf(idx, "Usage: -lang <language>\n");
490 return 0;
491 }
492 putlog(LOG_CMDS, "*", "#%s# -lang %s", dcc[idx].nick, par);
493 if (!del_lang(par))
494 dprintf(idx, "Language %s not found.\n", par);
495 else
496 recheck_lang_sections();
497 return 0;
498 }
499
500 static int cmd_plslsec(struct userrec *u, int idx, char *par)
501 {
502 if (!par || !par[0]) {
503 dprintf(idx, "Usage: +lsec <section>\n");
504 return 0;
505 }
506 putlog(LOG_CMDS, "*", "#%s# +lsec %s", dcc[idx].nick, par);
507 add_lang_section(par);
508 return 0;
509 }
510
511 static int cmd_mnslsec(struct userrec *u, int idx, char *par)
512 {
513 if (!par || !par[0]) {
514 dprintf(idx, "Usage: -lsec <section>\n");
515 return 0;
516 }
517 putlog(LOG_CMDS, "*", "#%s# -lsec %s", dcc[idx].nick, par);
518 if (!del_lang_section(par))
519 dprintf(idx, "Section %s not found.\n", par);
520 return 0;
521 }
522
523 static int cmd_relang(struct userrec *u, int idx, char *par)
524 {
525 dprintf(idx, "Rechecking language sections...\n");
526 recheck_lang_sections();
527 return 0;
528 }
529
530 static int cmd_languagedump(struct userrec *u, int idx, char *par)
531 {
532 lang_tab *l;
533 char ltext2[512];
534 int idx2, i;
535
536 putlog(LOG_CMDS, "*", "#%s# ldump %s", dcc[idx].nick, par);
537 if (par[0]) {
538 /* atoi (hence strtol) don't work right here for hex */
539 if (strlen(par) > 2 && par[0] == '0' && par[1] == 'x')
540 sscanf(par, "%x", &idx2);
541 else
542 idx2 = (int) strtol(par, (char **) NULL, 10);
543 strcpy(ltext2, get_language(idx2));
544 dprintf(idx, "0x%x: %s\n", idx2, ltext2);
545 return 0;
546 }
547 dprintf(idx, " LANGIDX TEXT\n");
548 for (i = 0; i < 64; i++)
549 for (l = langtab[i]; l; l = l->next)
550 dprintf(idx, "0x%x %s\n", l->idx, l->text);
551 return 0;
552 }
553
554 static char text[512];
555 char *get_language(int idx)
556 {
557 lang_tab *l;
558
559 if (!idx)
560 return "MSG-0-";
561 for (l = langtab[idx & 63]; l; l = l->next)
562 if (idx == l->idx)
563 return l->text;
564 egg_snprintf(text, sizeof text, "MSG%03X", idx);
565 return text;
566 }
567
568 int expmem_language()
569 {
570 lang_tab *l;
571 lang_sec *ls;
572 lang_pri *lp;
573 int i, size = 0;
574
575 for (i = 0; i < 64; i++)
576 for (l = langtab[i]; l; l = l->next) {
577 size += sizeof(lang_tab);
578 size += (strlen(l->text) + 1);
579 }
580 for (ls = langsection; ls; ls = ls->next) {
581 size += sizeof(lang_sec);
582 if (ls->section)
583 size += strlen(ls->section)+1;
584 if (ls->lang)
585 size += strlen(ls->lang)+1;
586 }
587 for (lp = langpriority; lp; lp = lp->next) {
588 size += sizeof(lang_pri);
589 if (lp->lang)
590 size += strlen(lp->lang)+1;
591 }
592 return size;
593 }
594
595 /* A report on the module status - only for debugging purposes
596 */
597 static int cmd_languagestatus(struct userrec *u, int idx, char *par)
598 {
599 int ltexts = 0;
600 register int i, c, maxdepth = 0, used = 0, empty = 0;
601 lang_tab *l;
602 lang_sec *ls = langsection;
603 lang_pri *lp = langpriority;
604
605 putlog(LOG_CMDS, "*", "#%s# lstat %s", dcc[idx].nick, par);
606 for (i = 0; i < 64; i++) {
607 c = 0;
608 for (l = langtab[i]; l; l = l->next)
609 c++;
610 if (c > maxdepth)
611 maxdepth = c;
612 if (c)
613 used++;
614 else
615 empty++;
616 ltexts += c;
617 }
618 dprintf(idx, "Language code report:\n");
619 dprintf(idx, " Table size : %d bytes\n", expmem_language());
620 dprintf(idx, " Text messages: %d\n", ltexts);
621 dprintf(idx, " %d used, %d unused, maxdepth %d, avg %f\n",
622 used, empty, maxdepth, (float) ltexts / 64.0);
623 if (lp) {
624 int c = 0;
625
626 dprintf(idx, " Supported languages:");
627 for (; lp; lp = lp->next) {
628 dprintf(idx, "%s %s", c ? "," : "", lp->lang);
629 c = 1;
630 }
631 dprintf(idx, "\n");
632 }
633 if (ls) {
634 dprintf(idx, "\n SECTION LANG\n");
635 dprintf(idx, " ==============================\n");
636 for (; ls; ls = ls->next)
637 dprintf(idx, " %-20s %s\n", ls->section,
638 ls->lang ? ls->lang : "<none>");
639 }
640 return 0;
641 }
642
643 /* Compability function to allow scripts to use the old command.
644 */
645 static int tcl_language STDVAR
646 {
647 char *lang, *section, *buf;
648
649 putlog(LOG_MISC, "*", "TCL cmd 'language' is obsolete. Use 'addlang' instead.");
650 BADARGS(2, 2, " language");
651
652 buf = nmalloc(strlen(argv[1])+1);
653 strcpy(buf, argv[1]);
654 if (!split_lang(buf, &lang, &section)) {
655 Tcl_AppendResult(irp, "Invalid parameter", NULL);
656 nfree(buf);
657 return TCL_ERROR;
658 }
659 add_lang(lang);
660 add_lang_section(section);
661 nfree(buf);
662 recheck_lang_sections();
663 return TCL_OK;
664 }
665
666 static int tcl_plslang STDVAR
667 {
668 BADARGS(2, 2, " language");
669
670 add_lang(argv[1]);
671 recheck_lang_sections();
672
673 return TCL_OK;
674 }
675
676 static int tcl_mnslang STDVAR
677 {
678 BADARGS(2, 2, " language");
679
680 if (!del_lang(argv[1])) {
681 Tcl_AppendResult(irp, "Language not found.", NULL);
682 return TCL_ERROR;
683 }
684 recheck_lang_sections();
685
686 return TCL_OK;
687 }
688
689 static int tcl_addlangsection STDVAR
690 {
691 BADARGS(2, 2, " section");
692
693 add_lang_section(argv[1]);
694 return TCL_OK;
695 }
696
697 static int tcl_dellangsection STDVAR
698 {
699 BADARGS(2, 2, " section");
700
701 if (!del_lang_section(argv[1])) {
702 Tcl_AppendResult(irp, "Section not found", NULL);
703 return TCL_ERROR;
704 }
705 return TCL_OK;
706 }
707
708 static int tcl_relang STDVAR
709 {
710 recheck_lang_sections();
711 return TCL_OK;
712 }
713
714 static cmd_t langdcc[] =
715 {
716 {"language", "n", cmd_loadlanguage, NULL},
717 {"+lang", "n", cmd_plslang, NULL},
718 {"-lang", "n", cmd_mnslang, NULL},
719 {"+lsec", "n", cmd_plslsec, NULL},
720 {"-lsec", "n", cmd_mnslsec, NULL},
721 {"ldump", "n", cmd_languagedump, NULL},
722 {"lstat", "n", cmd_languagestatus, NULL},
723 {"relang", "n", cmd_relang, NULL},
724 {NULL, NULL, NULL, NULL}
725 };
726
727 static tcl_cmds langtcls[] =
728 {
729 {"language", tcl_language},
730 {"addlang", tcl_plslang},
731 {"dellang", tcl_mnslang},
732 {"addlangsection", tcl_addlangsection},
733 {"dellangsection", tcl_dellangsection},
734 {"relang", tcl_relang},
735 {NULL, NULL}
736 };
737
738 void init_language(int flag)
739 {
740 int i;
741 char *deflang;
742
743 if (flag) {
744 for (i = 0; i < 32; i++)
745 langtab[i] = 0;
746 /* The default language is always BASELANG as language files are
747 * gauranteed to exist in that language.
748 */
749 add_lang(BASELANG);
750 /* Let the user choose a different, preferred language */
751 deflang = getenv("EGG_LANG");
752 if (deflang)
753 add_lang(deflang);
754 add_lang_section("core");
755 } else {
756 add_tcl_commands(langtcls);
757 add_builtins(H_dcc, langdcc);
758 }
759 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23