/[cvs]/eggdrop1.8/src/tcl.c
ViewVC logotype

Contents of /eggdrop1.8/src/tcl.c

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


Revision 1.9.2.6 - (show annotations) (download) (as text)
Wed Jul 31 01:14:54 2013 UTC (5 years, 11 months ago) by thommey
Branch: gettext
Changes since 1.9.2.5: +4 -146 lines
File MIME type: text/x-chdr
apply two patches of guppy that were missing

1 /*
2 * tcl.c -- handles:
3 * the code for every command eggdrop adds to Tcl
4 * Tcl initialization
5 * getting and setting Tcl/eggdrop variables
6 *
7 * $Id: tcl.c,v 1.9.2.5 2012/06/19 14:27:06 thommey Exp $
8 */
9 /*
10 * Copyright (C) 1997 Robey Pointer
11 * Copyright (C) 1999 - 2010 Eggheads Development Team
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 #include <stdlib.h> /* getenv() */
29 #include <locale.h> /* setlocale() */
30 #include "main.h"
31
32 /* Used for read/write to internal strings */
33 typedef struct {
34 char *str; /* Pointer to actual string in eggdrop */
35 int max; /* max length (negative: read-only var
36 * when protect is on) (0: read-only ALWAYS) */
37 int flags; /* 1 = directory */
38 } strinfo;
39
40 typedef struct {
41 int *var;
42 int ro;
43 } intinfo;
44
45
46 extern time_t online_since;
47
48 extern char origbotname[], botuser[], motdfile[], admin[], userfile[],
49 firewall[], helpdir[], notify_new[], vhost[], moddir[],
50 tempdir[], owner[], network[], botnetnick[], bannerfile[],
51 egg_version[], natip[], configfile[], logfile_suffix[], log_ts[],
52 textdir[], pid_file[], listen_ip[];
53
54
55 extern int flood_telnet_thr, flood_telnet_time, shtime, share_greet,
56 require_p, keep_all_logs, allow_new_telnets, stealth_telnets,
57 use_telnet_banner, default_flags, conmask, switch_logfiles_at,
58 connect_timeout, firewallport, notify_users_at, flood_thr, tands,
59 ignore_time, reserved_port_min, reserved_port_max, max_logs,
60 max_logsize, dcc_total, raw_log, identtimeout, dcc_sanitycheck,
61 dupwait_timeout, egg_numver, share_unlinks, protect_telnet,
62 strict_host, resolve_timeout, default_uflags, userfile_perm,
63 cidr_support;
64
65 #ifdef IPV6
66 extern char vhost6[];
67 extern int pref_af;
68 #endif
69
70 #ifdef TLS
71 extern int tls_maxdepth, tls_vfybots, tls_vfyclients, tls_vfydcc, tls_auth;
72 extern char tls_capath[], tls_cafile[], tls_certfile[], tls_keyfile[],
73 tls_ciphers[];
74 #endif
75
76 extern struct dcc_t *dcc;
77 extern tcl_timer_t *timer, *utimer;
78
79 Tcl_Interp *interp;
80
81 int protect_readonly = 0; /* Enable read-only protection? */
82 char whois_fields[1025] = "";
83
84 int dcc_flood_thr = 3;
85 int use_invites = 0;
86 int use_exempts = 0;
87 int force_expire = 0;
88 int remote_boots = 2;
89 int allow_dk_cmds = 1;
90 int must_be_owner = 1;
91 int quiet_reject = 1;
92 int copy_to_tmp = 1;
93 int max_socks = 100;
94 int quick_logs = 0;
95 int par_telnet_flood = 1;
96 int quiet_save = 0;
97 int strtot = 0;
98 int handlen = HANDLEN;
99 int utftot = 0;
100 int clientdata_stuff = 0;
101
102 /* Prototypes for Tcl */
103 Tcl_Interp *Tcl_CreateInterp();
104
105 int expmem_tcl()
106 {
107 return strtot + utftot + clientdata_stuff;
108 }
109
110 static void botnet_change(char *new)
111 {
112 if (egg_strcasecmp(botnetnick, new)) {
113 /* Trying to change bot's nickname */
114 if (tands > 0) {
115 putlog(LOG_MISC, "*", _("* Tried to change my botnet nick, but I'm still "
116 "linked to a botnet."));
117 putlog(LOG_MISC, "*", _("* (Unlink and try again.)"));
118 return;
119 } else {
120 if (botnetnick[0])
121 putlog(LOG_MISC, "*", _("* IDENTITY CHANGE: %s -> %s"), botnetnick, new);
122 strcpy(botnetnick, new);
123 }
124 }
125 }
126
127
128 /*
129 * Vars, traces, misc
130 */
131
132 int init_misc();
133
134 /* Used for read/write to integer couplets */
135 typedef struct {
136 int *left; /* left side of couplet */
137 int *right; /* right side */
138 } coupletinfo;
139
140 /* FIXME: tcl_eggcouplet() should be redesigned so we can use
141 * TCL_TRACE_WRITES | TCL_TRACE_READS as the bit mask instead
142 * of 2 calls as is done in add_tcl_coups().
143 */
144 /* Read/write integer couplets (int1:int2) */
145 static char *tcl_eggcouplet(ClientData cdata, Tcl_Interp *irp,
146 EGG_CONST char *name1,
147 EGG_CONST char *name2, int flags)
148 {
149 char *s, s1[41];
150 coupletinfo *cp = (coupletinfo *) cdata;
151
152 if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
153 egg_snprintf(s1, sizeof s1, "%d:%d", *(cp->left), *(cp->right));
154 Tcl_SetVar2(interp, name1, name2, s1, TCL_GLOBAL_ONLY);
155 if (flags & TCL_TRACE_UNSETS)
156 Tcl_TraceVar(interp, name1,
157 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
158 tcl_eggcouplet, cdata);
159 } else { /* writes */
160 s = (char *) Tcl_GetVar2(interp, name1, name2, 0);
161 if (s != NULL) {
162 int nr1, nr2;
163
164 nr1 = nr2 = 0;
165
166 if (strlen(s) > 40)
167 s[40] = 0;
168
169 sscanf(s, "%d%*c%d", &nr1, &nr2);
170 *(cp->left) = nr1;
171 *(cp->right) = nr2;
172 }
173 }
174 return NULL;
175 }
176
177 /* Read or write normal integer.
178 */
179 static char *tcl_eggint(ClientData cdata, Tcl_Interp *irp,
180 EGG_CONST char *name1,
181 EGG_CONST char *name2, int flags)
182 {
183 char *s, s1[40];
184 long l;
185 intinfo *ii = (intinfo *) cdata;
186
187 if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
188 /* Special cases */
189 if ((int *) ii->var == &conmask)
190 strcpy(s1, masktype(conmask));
191 else if ((int *) ii->var == &default_flags) {
192 struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 };
193 fr.global = default_flags;
194
195 fr.udef_global = default_uflags;
196 build_flags(s1, &fr, 0);
197 } else if ((int *) ii->var == &userfile_perm) {
198 egg_snprintf(s1, sizeof s1, "0%o", userfile_perm);
199 } else
200 egg_snprintf(s1, sizeof s1, "%d", *(int *) ii->var);
201 Tcl_SetVar2(interp, name1, name2, s1, TCL_GLOBAL_ONLY);
202 if (flags & TCL_TRACE_UNSETS)
203 Tcl_TraceVar(interp, name1,
204 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
205 tcl_eggint, cdata);
206 return NULL;
207 } else { /* Writes */
208 s = (char *) Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
209 if (s != NULL) {
210 if ((int *) ii->var == &conmask) {
211 if (s[0])
212 conmask = logmodes(s);
213 else
214 conmask = LOG_MODES | LOG_MISC | LOG_CMDS;
215 } else if ((int *) ii->var == &default_flags) {
216 struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 };
217
218 break_down_flags(s, &fr, 0);
219 default_flags = sanity_check(fr.global); /* drummer */
220
221 default_uflags = fr.udef_global;
222 } else if ((int *) ii->var == &userfile_perm) {
223 int p = oatoi(s);
224
225 if (p <= 0)
226 return "invalid userfile permissions";
227 userfile_perm = p;
228 } else if ((ii->ro == 2) || ((ii->ro == 1) && protect_readonly))
229 return "read-only variable";
230 else {
231 if (Tcl_ExprLong(interp, s, &l) == TCL_ERROR)
232 return "variable must have integer value";
233 if ((int *) ii->var == &max_socks) {
234 if (l < threaddata()->MAXSOCKS)
235 return "you can't DECREASE max-socks below current usage";
236 max_socks = l;
237 } else if ((int *) ii->var == &max_logs) {
238 if (l < max_logs)
239 return "you can't DECREASE max-logs";
240 max_logs = l;
241 init_misc();
242 } else
243 *(ii->var) = (int) l;
244 }
245 }
246 return NULL;
247 }
248 }
249
250 /* Read/write normal string variable
251 */
252 static char *tcl_eggstr(ClientData cdata, Tcl_Interp *irp,
253 EGG_CONST char *name1,
254 EGG_CONST char *name2, int flags)
255 {
256 char *s;
257 strinfo *st = (strinfo *) cdata;
258
259 if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
260 if ((st->str == firewall) && (firewall[0])) {
261 char s1[127];
262
263 egg_snprintf(s1, sizeof s1, "%s:%d", firewall, firewallport);
264 Tcl_SetVar2(interp, name1, name2, s1, TCL_GLOBAL_ONLY);
265 } else
266 Tcl_SetVar2(interp, name1, name2, st->str, TCL_GLOBAL_ONLY);
267 if (flags & TCL_TRACE_UNSETS) {
268 Tcl_TraceVar(interp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
269 TCL_TRACE_UNSETS, tcl_eggstr, cdata);
270 if ((st->max <= 0) && (protect_readonly || (st->max == 0)))
271 return "read-only variable"; /* it won't return the error... */
272 }
273 return NULL;
274 } else { /* writes */
275 if ((st->max <= 0) && (protect_readonly || (st->max == 0))) {
276 Tcl_SetVar2(interp, name1, name2, st->str, TCL_GLOBAL_ONLY);
277 return "read-only variable";
278 }
279
280 s = (char *) Tcl_GetVar2(interp, name1, name2, 0);
281 if (s != NULL) {
282 if (strlen(s) > abs(st->max))
283 s[abs(st->max)] = 0;
284 if (st->str == botnetnick)
285 botnet_change(s);
286 else if (st->str == logfile_suffix)
287 logsuffix_change(s);
288 else if (st->str == firewall) {
289 splitc(firewall, s, ':');
290 if (!firewall[0])
291 strcpy(firewall, s);
292 else
293 firewallport = atoi(s);
294 } else
295 strcpy(st->str, s);
296 if ((st->flags) && (s[0])) {
297 if (st->str[strlen(st->str) - 1] != '/')
298 strcat(st->str, "/");
299 }
300 }
301 return NULL;
302 }
303 }
304
305 /* Add/remove tcl commands
306 */
307
308 void add_tcl_commands(tcl_cmds *table)
309 {
310 int i;
311
312 for (i = 0; table[i].name; i++)
313 Tcl_CreateCommand(interp, table[i].name, table[i].func, NULL, NULL);
314 }
315
316 void add_cd_tcl_cmds(cd_tcl_cmd *table)
317 {
318 while (table->name) {
319 Tcl_CreateCommand(interp, table->name, table->callback,
320 (ClientData) table->cdata, NULL);
321 table++;
322 }
323 }
324
325 void rem_tcl_commands(tcl_cmds *table)
326 {
327 int i;
328
329 for (i = 0; table[i].name; i++)
330 Tcl_DeleteCommand(interp, table[i].name);
331 }
332
333 void rem_cd_tcl_cmds(cd_tcl_cmd *table)
334 {
335 while (table->name) {
336 Tcl_DeleteCommand(interp, table->name);
337 table++;
338 }
339 }
340
341 void add_tcl_objcommands(tcl_cmds *table)
342 {
343 int i;
344
345 for (i = 0; table[i].name; i++)
346 Tcl_CreateObjCommand(interp, table[i].name, table[i].func, (ClientData) 0,
347 NULL);
348 }
349
350 /* Get the current tcl result string. */
351 const char *tcl_resultstring()
352 {
353 const char *result;
354 result = Tcl_GetStringResult(interp);
355 return result;
356 }
357
358 int tcl_resultempty() {
359 const char *result;
360 result = tcl_resultstring();
361 return (result && result[0]) ? 0 : 1;
362 }
363
364 /* Get the current tcl result as int. replaces atoi(interp->result) */
365 int tcl_resultint()
366 {
367 int result;
368 if (Tcl_GetIntFromObj(NULL, Tcl_GetObjResult(interp), &result) != TCL_OK)
369 result = 0;
370 return result;
371 }
372
373 static tcl_strings def_tcl_strings[] = {
374 {"botnet-nick", botnetnick, HANDLEN, 0},
375 {"userfile", userfile, 120, STR_PROTECT},
376 {"motd", motdfile, 120, STR_PROTECT},
377 {"admin", admin, 120, 0},
378 {"help-path", helpdir, 120, STR_DIR | STR_PROTECT},
379 {"temp-path", tempdir, 120, STR_DIR | STR_PROTECT},
380 {"text-path", textdir, 120, STR_DIR | STR_PROTECT},
381 #ifdef TLS
382 {"ssl-capath", tls_capath, 120, STR_DIR | STR_PROTECT},
383 {"ssl-cafile", tls_cafile, 120, STR_PROTECT},
384 {"ssl-ciphers", tls_ciphers, 120, STR_PROTECT},
385 {"ssl-privatekey", tls_keyfile, 120, STR_PROTECT},
386 {"ssl-certificate", tls_certfile, 120, STR_PROTECT},
387 #endif
388 #ifndef STATIC
389 {"mod-path", moddir, 120, STR_DIR | STR_PROTECT},
390 #endif
391 {"notify-newusers", notify_new, 120, 0},
392 {"owner", owner, 120, STR_PROTECT},
393 {"vhost4", vhost, 120, 0},
394 #ifdef IPV6
395 {"vhost6", vhost6, 120, 0},
396 #endif
397 {"listen-addr", listen_ip, 120, 0},
398 {"network", network, 40, 0},
399 {"whois-fields", whois_fields, 1024, 0},
400 {"nat-ip", natip, 120, 0},
401 {"username", botuser, 10, 0},
402 {"version", egg_version, 0, 0},
403 {"firewall", firewall, 120, 0},
404 {"config", configfile, 0, 0},
405 {"telnet-banner", bannerfile, 120, STR_PROTECT},
406 {"logfile-suffix", logfile_suffix, 20, 0},
407 {"timestamp-format",log_ts, 32, 0},
408 {"pidfile", pid_file, 120, STR_PROTECT},
409 {NULL, NULL, 0, 0}
410 };
411
412 static tcl_ints def_tcl_ints[] = {
413 {"ignore-time", &ignore_time, 0},
414 {"handlen", &handlen, 2},
415 #ifdef TLS
416 {"ssl-chain-depth", &tls_maxdepth, 0},
417 {"ssl-verify-dcc", &tls_vfydcc, 0},
418 {"ssl-verify-clients", &tls_vfyclients, 0},
419 {"ssl-verify-bots", &tls_vfybots, 0},
420 {"ssl-cert-auth", &tls_auth, 0},
421 #endif
422 {"dcc-flood-thr", &dcc_flood_thr, 0},
423 {"hourly-updates", &notify_users_at, 0},
424 {"switch-logfiles-at", &switch_logfiles_at, 0},
425 {"connect-timeout", &connect_timeout, 0},
426 {"reserved-port", &reserved_port_min, 0},
427 {"require-p", &require_p, 0},
428 {"keep-all-logs", &keep_all_logs, 0},
429 {"open-telnets", &allow_new_telnets, 0},
430 {"stealth-telnets", &stealth_telnets, 0},
431 {"use-telnet-banner", &use_telnet_banner, 0},
432 {"uptime", (int *) &online_since, 2},
433 {"console", &conmask, 0},
434 {"default-flags", &default_flags, 0},
435 {"numversion", &egg_numver, 2},
436 {"remote-boots", &remote_boots, 1},
437 {"max-socks", &max_socks, 0},
438 {"max-logs", &max_logs, 0},
439 {"max-logsize", &max_logsize, 0},
440 {"quick-logs", &quick_logs, 0},
441 {"raw-log", &raw_log, 1},
442 {"protect-telnet", &protect_telnet, 0},
443 {"dcc-sanitycheck", &dcc_sanitycheck, 0},
444 {"ident-timeout", &identtimeout, 0},
445 {"share-unlinks", &share_unlinks, 0},
446 {"log-time", &shtime, 0},
447 {"allow-dk-cmds", &allow_dk_cmds, 0},
448 {"resolve-timeout", &resolve_timeout, 0},
449 {"must-be-owner", &must_be_owner, 1},
450 {"paranoid-telnet-flood", &par_telnet_flood, 0},
451 {"use-exempts", &use_exempts, 0},
452 {"use-invites", &use_invites, 0},
453 {"quiet-save", &quiet_save, 0},
454 {"force-expire", &force_expire, 0},
455 {"dupwait-timeout", &dupwait_timeout, 0},
456 {"strict-host", &strict_host, 0},
457 {"userfile-perm", &userfile_perm, 0},
458 {"copy-to-tmp", &copy_to_tmp, 0},
459 {"quiet-reject", &quiet_reject, 0},
460 {"cidr-support", &cidr_support, 0},
461 #ifdef IPV6
462 {"prefer-ipv6", &pref_af, 0},
463 #endif
464 {NULL, NULL, 0}
465 };
466
467 static tcl_coups def_tcl_coups[] = {
468 {"telnet-flood", &flood_telnet_thr, &flood_telnet_time},
469 {"reserved-portrange", &reserved_port_min, &reserved_port_max},
470 {NULL, NULL, NULL}
471 };
472
473 /* Set up Tcl variables that will hook into eggdrop internal vars via
474 * trace callbacks.
475 */
476 static void init_traces()
477 {
478 add_tcl_coups(def_tcl_coups);
479 add_tcl_strings(def_tcl_strings);
480 add_tcl_ints(def_tcl_ints);
481 }
482
483 void kill_tcl()
484 {
485 rem_tcl_coups(def_tcl_coups);
486 rem_tcl_strings(def_tcl_strings);
487 rem_tcl_ints(def_tcl_ints);
488 kill_bind();
489 Tcl_DeleteInterp(interp);
490 }
491
492 extern tcl_cmds tcluser_cmds[], tcldcc_cmds[], tclmisc_cmds[],
493 tclmisc_objcmds[], tcldns_cmds[];
494 #ifdef TLS
495 extern tcl_cmds tcltls_cmds[];
496 #endif
497
498 #ifdef REPLACE_NOTIFIER
499 /* The tickle_*() functions replace the Tcl Notifier
500 * The tickle_*() functions can be called by Tcl threads
501 */
502 void tickle_SetTimer (TCL_CONST86 Tcl_Time *timePtr)
503 {
504 struct threaddata *td = threaddata();
505 /* we can block 1 second maximum, because we have SECONDLY events */
506 if (!timePtr || timePtr->sec > 1 || (timePtr->sec == 1 && timePtr->usec > 0)) {
507 td->blocktime.tv_sec = 1;
508 td->blocktime.tv_usec = 0;
509 } else {
510 td->blocktime.tv_sec = timePtr->sec;
511 td->blocktime.tv_usec = timePtr->usec;
512 }
513 }
514
515 int tickle_WaitForEvent (TCL_CONST86 Tcl_Time *timePtr)
516 {
517 struct threaddata *td = threaddata();
518
519 tickle_SetTimer(timePtr);
520 return (*td->mainloopfunc)(0);
521 }
522
523 void tickle_CreateFileHandler(int fd, int mask, Tcl_FileProc *proc, ClientData cd)
524 {
525 alloctclsock(fd, mask, proc, cd);
526 }
527
528 void tickle_DeleteFileHandler(int fd)
529 {
530 killtclsock(fd);
531 }
532
533 void tickle_FinalizeNotifier(ClientData cd)
534 {
535 struct threaddata *td = threaddata();
536 if (td->socklist)
537 nfree(td->socklist);
538 }
539
540 ClientData tickle_InitNotifier()
541 {
542 static int ismainthread = 1;
543 init_threaddata(ismainthread);
544 if (ismainthread)
545 ismainthread = 0;
546 return NULL;
547 }
548
549 int tclthreadmainloop(int zero)
550 {
551 int i;
552 i = sockread(NULL, NULL, threaddata()->socklist, threaddata()->MAXSOCKS, 1);
553 return (i == -5);
554 }
555
556 struct threaddata *threaddata()
557 {
558 static Tcl_ThreadDataKey tdkey;
559 struct threaddata *td = Tcl_GetThreadData(&tdkey, sizeof(struct threaddata));
560 return td;
561 }
562
563 #else /* REPLACE_NOTIFIER */
564
565 int tclthreadmainloop() { return 0; }
566
567 struct threaddata *threaddata()
568 {
569 static struct threaddata tsd;
570 return &tsd;
571 }
572
573 #endif /* REPLACE_NOTIFIER */
574
575 int init_threaddata(int mainthread)
576 {
577 struct threaddata *td = threaddata();
578 /* Nested evaluation (vwait/update) of the event loop only
579 * processes Tcl events (after/fileevent) for now. Using
580 * eggdrops mainloop() requires caution regarding reentrance.
581 * (check_tcl_* -> Tcl_Eval() -> mainloop() -> check_tcl_* etc.)
582 */
583 /* td->mainloopfunc = mainthread ? mainloop : tclthreadmainloop; */
584 td->mainloopfunc = tclthreadmainloop;
585 td->socklist = NULL;
586 td->mainthread = mainthread;
587 td->blocktime.tv_sec = 1;
588 td->blocktime.tv_usec = 0;
589 td->MAXSOCKS = 0;
590 increase_socks_max();
591 return 0;
592 }
593
594 /* Not going through Tcl's crazy main() system (what on earth was he
595 * smoking?!) so we gotta initialize the Tcl interpreter
596 */
597 void init_tcl(int argc, char **argv)
598 {
599 #ifdef REPLACE_NOTIFIER
600 Tcl_NotifierProcs notifierprocs;
601 #endif /* REPLACE_NOTIFIER */
602
603 const char *encoding;
604 int i, j;
605 char *langEnv, pver[1024] = "";
606
607 #ifdef REPLACE_NOTIFIER
608 egg_bzero(&notifierprocs, sizeof(notifierprocs));
609 notifierprocs.initNotifierProc = tickle_InitNotifier;
610 notifierprocs.createFileHandlerProc = tickle_CreateFileHandler;
611 notifierprocs.deleteFileHandlerProc = tickle_DeleteFileHandler;
612 notifierprocs.setTimerProc = tickle_SetTimer;
613 notifierprocs.waitForEventProc = tickle_WaitForEvent;
614 notifierprocs.finalizeNotifierProc = tickle_FinalizeNotifier;
615
616 Tcl_SetNotifier(&notifierprocs);
617 #endif /* REPLACE_NOTIFIER */
618
619 /* This must be done *BEFORE* Tcl_SetSystemEncoding(),
620 * or Tcl_SetSystemEncoding() will cause a segfault.
621 */
622 /* This is used for 'info nameofexecutable'.
623 * The filename in argv[0] must exist in a directory listed in
624 * the environment variable PATH for it to register anything.
625 */
626 Tcl_FindExecutable(argv[0]);
627
628 /* Initialize the interpreter */
629 interp = Tcl_CreateInterp();
630
631 #ifdef DEBUG_MEM
632 /* Initialize Tcl's memory debugging if we want it */
633 Tcl_InitMemory(interp);
634 #endif
635
636 /* Set Tcl variable tcl_interactive to 0 */
637 Tcl_SetVar(interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
638
639 /* Setup script library facility */
640 Tcl_Init(interp);
641 Tcl_SetServiceMode(TCL_SERVICE_ALL);
642
643 /* Code based on Tcl's TclpSetInitialEncodings() */
644 /* Determine the current encoding from the LC_* or LANG environment
645 * variables.
646 */
647 langEnv = getenv("LC_ALL");
648 if (langEnv == NULL || langEnv[0] == '\0') {
649 langEnv = getenv("LC_CTYPE");
650 }
651 if (langEnv == NULL || langEnv[0] == '\0') {
652 langEnv = getenv("LANG");
653 }
654 if (langEnv == NULL || langEnv[0] == '\0') {
655 langEnv = NULL;
656 }
657
658 encoding = NULL;
659 if (langEnv != NULL) {
660 for (i = 0; localeTable[i].lang != NULL; i++)
661 if (strcmp(localeTable[i].lang, langEnv) == 0) {
662 encoding = localeTable[i].encoding;
663 break;
664 }
665
666 /* There was no mapping in the locale table. If there is an
667 * encoding subfield, we can try to guess from that.
668 */
669 if (encoding == NULL) {
670 char *p;
671
672 for (p = langEnv; *p != '\0'; p++) {
673 if (*p == '.') {
674 p++;
675 break;
676 }
677 }
678 if (*p != '\0') {
679 Tcl_DString ds;
680
681 Tcl_DStringInit(&ds);
682 Tcl_DStringAppend(&ds, p, -1);
683
684 encoding = Tcl_DStringValue(&ds);
685 Tcl_UtfToLower(Tcl_DStringValue(&ds));
686 if (Tcl_SetSystemEncoding(NULL, encoding) == TCL_OK) {
687 Tcl_DStringFree(&ds);
688 goto resetPath;
689 }
690 Tcl_DStringFree(&ds);
691 encoding = NULL;
692 }
693 }
694 }
695
696 if (encoding == NULL) {
697 encoding = "iso8859-1";
698 }
699
700 Tcl_SetSystemEncoding(NULL, encoding);
701
702 resetPath:
703
704 /* Initialize the C library's locale subsystem. */
705 setlocale(LC_CTYPE, "");
706
707 /* In case the initial locale is not "C", ensure that the numeric
708 * processing is done in "C" locale regardless. */
709 setlocale(LC_NUMERIC, "C");
710
711 /* Keep the iso8859-1 encoding preloaded. The IO package uses it for
712 * gets on a binary channel. */
713 Tcl_GetEncoding(NULL, "iso8859-1");
714
715 /* Add eggdrop to Tcl's package list */
716 for (j = 0; j <= strlen(egg_version); j++) {
717 if ((egg_version[j] == ' ') || (egg_version[j] == '+'))
718 break;
719 pver[strlen(pver)] = egg_version[j];
720 }
721 Tcl_PkgProvide(interp, "eggdrop", pver);
722
723 /* Initialize binds and traces */
724 init_bind();
725 init_traces();
726
727 /* Add new commands */
728 add_tcl_commands(tcluser_cmds);
729 add_tcl_commands(tcldcc_cmds);
730 add_tcl_commands(tclmisc_cmds);
731 add_tcl_objcommands(tclmisc_objcmds);
732 add_tcl_commands(tcldns_cmds);
733 #ifdef TLS
734 add_tcl_commands(tcltls_cmds);
735 #endif
736 }
737
738 void do_tcl(char *whatzit, char *script)
739 {
740 int code;
741 char *result;
742 Tcl_DString dstr;
743
744 code = Tcl_Eval(interp, script);
745
746 /* properly convert string to system encoding. */
747 Tcl_DStringInit(&dstr);
748 Tcl_UtfToExternalDString(NULL, tcl_resultstring(), -1, &dstr);
749 result = Tcl_DStringValue(&dstr);
750
751 if (code != TCL_OK) {
752 putlog(LOG_MISC, "*", _("Tcl error in script for '%s':"), whatzit);
753 putlog(LOG_MISC, "*", "%s", result);
754 Tcl_BackgroundError(interp);
755 }
756
757 Tcl_DStringFree(&dstr);
758 }
759
760 /* Interpret tcl file fname.
761 *
762 * returns: 1 - if everything was okay
763 */
764 int readtclprog(char *fname)
765 {
766 int code;
767 EGG_CONST char *result;
768 Tcl_DString dstr;
769
770 if (!file_readable(fname))
771 return 0;
772
773 code = Tcl_EvalFile(interp, fname);
774 result = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
775
776 /* properly convert string to system encoding. */
777 Tcl_DStringInit(&dstr);
778 Tcl_UtfToExternalDString(NULL, result, -1, &dstr);
779 result = Tcl_DStringValue(&dstr);
780
781 if (code != TCL_OK) {
782 putlog(LOG_MISC, "*", _("Tcl error in file '%s':"), fname);
783 putlog(LOG_MISC, "*", "%s", result);
784 Tcl_BackgroundError(interp);
785 code = 0; /* JJM: refactored to remove premature return */
786 } else {
787 /* Refresh internal variables */
788 code = 1;
789 }
790
791 Tcl_DStringFree(&dstr);
792
793 return code;
794 }
795
796 void add_tcl_strings(tcl_strings *list)
797 {
798 int i;
799 strinfo *st;
800 int tmp;
801
802 for (i = 0; list[i].name; i++) {
803 st = nmalloc(sizeof *st);
804 strtot += sizeof(strinfo);
805 st->max = list[i].length - (list[i].flags & STR_DIR);
806 if (list[i].flags & STR_PROTECT)
807 st->max = -st->max;
808 st->str = list[i].buf;
809 st->flags = (list[i].flags & STR_DIR);
810 tmp = protect_readonly;
811 protect_readonly = 0;
812 tcl_eggstr((ClientData) st, interp, list[i].name, NULL, TCL_TRACE_WRITES);
813 protect_readonly = tmp;
814 tcl_eggstr((ClientData) st, interp, list[i].name, NULL, TCL_TRACE_READS);
815 Tcl_TraceVar(interp, list[i].name, TCL_TRACE_READS | TCL_TRACE_WRITES |
816 TCL_TRACE_UNSETS, tcl_eggstr, (ClientData) st);
817 }
818 }
819
820 void rem_tcl_strings(tcl_strings *list)
821 {
822 int i, f;
823 strinfo *st;
824
825 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
826 for (i = 0; list[i].name; i++) {
827 st = (strinfo *) Tcl_VarTraceInfo(interp, list[i].name, f, tcl_eggstr,
828 NULL);
829 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggstr, st);
830 if (st != NULL) {
831 strtot -= sizeof(strinfo);
832 nfree(st);
833 }
834 }
835 }
836
837 void add_tcl_ints(tcl_ints *list)
838 {
839 int i, tmp;
840 intinfo *ii;
841
842 for (i = 0; list[i].name; i++) {
843 ii = nmalloc(sizeof *ii);
844 strtot += sizeof(intinfo);
845 ii->var = list[i].val;
846 ii->ro = list[i].readonly;
847 tmp = protect_readonly;
848 protect_readonly = 0;
849 tcl_eggint((ClientData) ii, interp, list[i].name, NULL, TCL_TRACE_WRITES);
850 protect_readonly = tmp;
851 tcl_eggint((ClientData) ii, interp, list[i].name, NULL, TCL_TRACE_READS);
852 Tcl_TraceVar(interp, list[i].name,
853 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
854 tcl_eggint, (ClientData) ii);
855 }
856
857 }
858
859 void rem_tcl_ints(tcl_ints *list)
860 {
861 int i, f;
862 intinfo *ii;
863
864 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
865 for (i = 0; list[i].name; i++) {
866 ii = (intinfo *) Tcl_VarTraceInfo(interp, list[i].name, f, tcl_eggint,
867 NULL);
868 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggint, (ClientData) ii);
869 if (ii) {
870 strtot -= sizeof(intinfo);
871 nfree(ii);
872 }
873 }
874 }
875
876 /* Allocate couplet space for tracing couplets
877 */
878 void add_tcl_coups(tcl_coups *list)
879 {
880 coupletinfo *cp;
881 int i;
882
883 for (i = 0; list[i].name; i++) {
884 cp = nmalloc(sizeof *cp);
885 strtot += sizeof(coupletinfo);
886 cp->left = list[i].lptr;
887 cp->right = list[i].rptr;
888 tcl_eggcouplet((ClientData) cp, interp, list[i].name, NULL,
889 TCL_TRACE_WRITES);
890 tcl_eggcouplet((ClientData) cp, interp, list[i].name, NULL,
891 TCL_TRACE_READS);
892 Tcl_TraceVar(interp, list[i].name,
893 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
894 tcl_eggcouplet, (ClientData) cp);
895 }
896 }
897
898 void rem_tcl_coups(tcl_coups *list)
899 {
900 int i, f;
901 coupletinfo *cp;
902
903 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
904 for (i = 0; list[i].name; i++) {
905 cp = (coupletinfo *) Tcl_VarTraceInfo(interp, list[i].name, f,
906 tcl_eggcouplet, NULL);
907 strtot -= sizeof(coupletinfo);
908 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggcouplet, (ClientData) cp);
909 nfree(cp);
910 }
911 }
912
913 /* Check if the Tcl library supports threads
914 */
915 int tcl_threaded()
916 {
917 #ifdef HAVE_TCL_GETCURRENTTHREAD
918 if (Tcl_GetCurrentThread() != (Tcl_ThreadId)0)
919 return 1;
920 #endif
921
922 return 0;
923 }
924
925 /* Check if we need to fork before initializing Tcl
926 */
927 int fork_before_tcl()
928 {
929 #ifndef REPLACE_NOTIFIER
930 return tcl_threaded();
931 #endif
932 return 0;
933 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23