/[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.13 - (show annotations) (download) (as text)
Wed Jul 31 01:09:33 2013 UTC (6 years, 2 months ago) by thommey
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +3 -146 lines
File MIME type: text/x-chdr
Error occurred while calculating annotation data.
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.12 2012/06/19 14:27:17 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 s = (char *) Tcl_GetVar2(interp, name1, name2, 0);
280 if (s != NULL) {
281 if (strlen(s) > abs(st->max))
282 s[abs(st->max)] = 0;
283 if (st->str == botnetnick)
284 botnet_change(s);
285 else if (st->str == logfile_suffix)
286 logsuffix_change(s);
287 else if (st->str == firewall) {
288 splitc(firewall, s, ':');
289 if (!firewall[0])
290 strcpy(firewall, s);
291 else
292 firewallport = atoi(s);
293 } else
294 strcpy(st->str, s);
295 if ((st->flags) && (s[0])) {
296 if (st->str[strlen(st->str) - 1] != '/')
297 strcat(st->str, "/");
298 }
299 }
300 return NULL;
301 }
302 }
303
304 /* Add/remove tcl commands
305 */
306
307 void add_tcl_commands(tcl_cmds *table)
308 {
309 int i;
310
311 for (i = 0; table[i].name; i++)
312 Tcl_CreateCommand(interp, table[i].name, table[i].func, NULL, NULL);
313 }
314
315 void add_cd_tcl_cmds(cd_tcl_cmd *table)
316 {
317 while (table->name) {
318 Tcl_CreateCommand(interp, table->name, table->callback,
319 (ClientData) table->cdata, NULL);
320 table++;
321 }
322 }
323
324 void rem_tcl_commands(tcl_cmds *table)
325 {
326 int i;
327
328 for (i = 0; table[i].name; i++)
329 Tcl_DeleteCommand(interp, table[i].name);
330 }
331
332 void rem_cd_tcl_cmds(cd_tcl_cmd *table)
333 {
334 while (table->name) {
335 Tcl_DeleteCommand(interp, table->name);
336 table++;
337 }
338 }
339
340 void add_tcl_objcommands(tcl_cmds *table)
341 {
342 int i;
343
344 for (i = 0; table[i].name; i++)
345 Tcl_CreateObjCommand(interp, table[i].name, table[i].func, (ClientData) 0,
346 NULL);
347 }
348
349 /* Get the current tcl result string. */
350 const char *tcl_resultstring()
351 {
352 const char *result;
353 result = Tcl_GetStringResult(interp);
354 return result;
355 }
356
357 int tcl_resultempty() {
358 const char *result;
359 result = tcl_resultstring();
360 return (result && result[0]) ? 0 : 1;
361 }
362
363 /* Get the current tcl result as int. replaces atoi(interp->result) */
364 int tcl_resultint()
365 {
366 int result;
367 if (Tcl_GetIntFromObj(NULL, Tcl_GetObjResult(interp), &result) != TCL_OK)
368 result = 0;
369 return result;
370 }
371
372 static tcl_strings def_tcl_strings[] = {
373 {"botnet-nick", botnetnick, HANDLEN, 0},
374 {"userfile", userfile, 120, STR_PROTECT},
375 {"motd", motdfile, 120, STR_PROTECT},
376 {"admin", admin, 120, 0},
377 {"help-path", helpdir, 120, STR_DIR | STR_PROTECT},
378 {"temp-path", tempdir, 120, STR_DIR | STR_PROTECT},
379 {"text-path", textdir, 120, STR_DIR | STR_PROTECT},
380 #ifdef TLS
381 {"ssl-capath", tls_capath, 120, STR_DIR | STR_PROTECT},
382 {"ssl-cafile", tls_cafile, 120, STR_PROTECT},
383 {"ssl-ciphers", tls_ciphers, 120, STR_PROTECT},
384 {"ssl-privatekey", tls_keyfile, 120, STR_PROTECT},
385 {"ssl-certificate", tls_certfile, 120, STR_PROTECT},
386 #endif
387 #ifndef STATIC
388 {"mod-path", moddir, 120, STR_DIR | STR_PROTECT},
389 #endif
390 {"notify-newusers", notify_new, 120, 0},
391 {"owner", owner, 120, STR_PROTECT},
392 {"vhost4", vhost, 120, 0},
393 #ifdef IPV6
394 {"vhost6", vhost6, 120, 0},
395 #endif
396 {"listen-addr", listen_ip, 120, 0},
397 {"network", network, 40, 0},
398 {"whois-fields", whois_fields, 1024, 0},
399 {"nat-ip", natip, 120, 0},
400 {"username", botuser, 10, 0},
401 {"version", egg_version, 0, 0},
402 {"firewall", firewall, 120, 0},
403 {"config", configfile, 0, 0},
404 {"telnet-banner", bannerfile, 120, STR_PROTECT},
405 {"logfile-suffix", logfile_suffix, 20, 0},
406 {"timestamp-format",log_ts, 32, 0},
407 {"pidfile", pid_file, 120, STR_PROTECT},
408 {NULL, NULL, 0, 0}
409 };
410
411 static tcl_ints def_tcl_ints[] = {
412 {"ignore-time", &ignore_time, 0},
413 {"handlen", &handlen, 2},
414 #ifdef TLS
415 {"ssl-chain-depth", &tls_maxdepth, 0},
416 {"ssl-verify-dcc", &tls_vfydcc, 0},
417 {"ssl-verify-clients", &tls_vfyclients, 0},
418 {"ssl-verify-bots", &tls_vfybots, 0},
419 {"ssl-cert-auth", &tls_auth, 0},
420 #endif
421 {"dcc-flood-thr", &dcc_flood_thr, 0},
422 {"hourly-updates", &notify_users_at, 0},
423 {"switch-logfiles-at", &switch_logfiles_at, 0},
424 {"connect-timeout", &connect_timeout, 0},
425 {"reserved-port", &reserved_port_min, 0},
426 {"require-p", &require_p, 0},
427 {"keep-all-logs", &keep_all_logs, 0},
428 {"open-telnets", &allow_new_telnets, 0},
429 {"stealth-telnets", &stealth_telnets, 0},
430 {"use-telnet-banner", &use_telnet_banner, 0},
431 {"uptime", (int *) &online_since, 2},
432 {"console", &conmask, 0},
433 {"default-flags", &default_flags, 0},
434 {"numversion", &egg_numver, 2},
435 {"remote-boots", &remote_boots, 1},
436 {"max-socks", &max_socks, 0},
437 {"max-logs", &max_logs, 0},
438 {"max-logsize", &max_logsize, 0},
439 {"quick-logs", &quick_logs, 0},
440 {"raw-log", &raw_log, 1},
441 {"protect-telnet", &protect_telnet, 0},
442 {"dcc-sanitycheck", &dcc_sanitycheck, 0},
443 {"ident-timeout", &identtimeout, 0},
444 {"share-unlinks", &share_unlinks, 0},
445 {"log-time", &shtime, 0},
446 {"allow-dk-cmds", &allow_dk_cmds, 0},
447 {"resolve-timeout", &resolve_timeout, 0},
448 {"must-be-owner", &must_be_owner, 1},
449 {"paranoid-telnet-flood", &par_telnet_flood, 0},
450 {"use-exempts", &use_exempts, 0},
451 {"use-invites", &use_invites, 0},
452 {"quiet-save", &quiet_save, 0},
453 {"force-expire", &force_expire, 0},
454 {"dupwait-timeout", &dupwait_timeout, 0},
455 {"strict-host", &strict_host, 0},
456 {"userfile-perm", &userfile_perm, 0},
457 {"copy-to-tmp", &copy_to_tmp, 0},
458 {"quiet-reject", &quiet_reject, 0},
459 {"cidr-support", &cidr_support, 0},
460 #ifdef IPV6
461 {"prefer-ipv6", &pref_af, 0},
462 #endif
463 {NULL, NULL, 0}
464 };
465
466 static tcl_coups def_tcl_coups[] = {
467 {"telnet-flood", &flood_telnet_thr, &flood_telnet_time},
468 {"reserved-portrange", &reserved_port_min, &reserved_port_max},
469 {NULL, NULL, NULL}
470 };
471
472 /* Set up Tcl variables that will hook into eggdrop internal vars via
473 * trace callbacks.
474 */
475 static void init_traces()
476 {
477 add_tcl_coups(def_tcl_coups);
478 add_tcl_strings(def_tcl_strings);
479 add_tcl_ints(def_tcl_ints);
480 }
481
482 void kill_tcl()
483 {
484 rem_tcl_coups(def_tcl_coups);
485 rem_tcl_strings(def_tcl_strings);
486 rem_tcl_ints(def_tcl_ints);
487 kill_bind();
488 Tcl_DeleteInterp(interp);
489 }
490
491 extern tcl_cmds tcluser_cmds[], tcldcc_cmds[], tclmisc_cmds[],
492 tclmisc_objcmds[], tcldns_cmds[];
493 #ifdef TLS
494 extern tcl_cmds tcltls_cmds[];
495 #endif
496
497 #ifdef REPLACE_NOTIFIER
498 /* The tickle_*() functions replace the Tcl Notifier
499 * The tickle_*() functions can be called by Tcl threads
500 */
501 void tickle_SetTimer (TCL_CONST86 Tcl_Time *timePtr)
502 {
503 struct threaddata *td = threaddata();
504 /* we can block 1 second maximum, because we have SECONDLY events */
505 if (!timePtr || timePtr->sec > 1 || (timePtr->sec == 1 && timePtr->usec > 0)) {
506 td->blocktime.tv_sec = 1;
507 td->blocktime.tv_usec = 0;
508 } else {
509 td->blocktime.tv_sec = timePtr->sec;
510 td->blocktime.tv_usec = timePtr->usec;
511 }
512 }
513
514 int tickle_WaitForEvent (TCL_CONST86 Tcl_Time *timePtr)
515 {
516 struct threaddata *td = threaddata();
517
518 tickle_SetTimer(timePtr);
519 return (*td->mainloopfunc)(0);
520 }
521
522 void tickle_CreateFileHandler(int fd, int mask, Tcl_FileProc *proc, ClientData cd)
523 {
524 alloctclsock(fd, mask, proc, cd);
525 }
526
527 void tickle_DeleteFileHandler(int fd)
528 {
529 killtclsock(fd);
530 }
531
532 void tickle_FinalizeNotifier(ClientData cd)
533 {
534 struct threaddata *td = threaddata();
535 if (td->socklist)
536 nfree(td->socklist);
537 }
538
539 ClientData tickle_InitNotifier()
540 {
541 static int ismainthread = 1;
542 init_threaddata(ismainthread);
543 if (ismainthread)
544 ismainthread = 0;
545 return NULL;
546 }
547
548 int tclthreadmainloop(int zero)
549 {
550 int i;
551 i = sockread(NULL, NULL, threaddata()->socklist, threaddata()->MAXSOCKS, 1);
552 return (i == -5);
553 }
554
555 struct threaddata *threaddata()
556 {
557 static Tcl_ThreadDataKey tdkey;
558 struct threaddata *td = Tcl_GetThreadData(&tdkey, sizeof(struct threaddata));
559 return td;
560 }
561
562 #else /* REPLACE_NOTIFIER */
563
564 int tclthreadmainloop() { return 0; }
565
566 struct threaddata *threaddata()
567 {
568 static struct threaddata tsd;
569 return &tsd;
570 }
571
572 #endif /* REPLACE_NOTIFIER */
573
574 int init_threaddata(int mainthread)
575 {
576 struct threaddata *td = threaddata();
577 /* Nested evaluation (vwait/update) of the event loop only
578 * processes Tcl events (after/fileevent) for now. Using
579 * eggdrops mainloop() requires caution regarding reentrance.
580 * (check_tcl_* -> Tcl_Eval() -> mainloop() -> check_tcl_* etc.)
581 */
582 /* td->mainloopfunc = mainthread ? mainloop : tclthreadmainloop; */
583 td->mainloopfunc = tclthreadmainloop;
584 td->socklist = NULL;
585 td->mainthread = mainthread;
586 td->blocktime.tv_sec = 1;
587 td->blocktime.tv_usec = 0;
588 td->MAXSOCKS = 0;
589 increase_socks_max();
590 return 0;
591 }
592
593 /* Not going through Tcl's crazy main() system (what on earth was he
594 * smoking?!) so we gotta initialize the Tcl interpreter
595 */
596 void init_tcl(int argc, char **argv)
597 {
598 #ifdef REPLACE_NOTIFIER
599 Tcl_NotifierProcs notifierprocs;
600 #endif /* REPLACE_NOTIFIER */
601
602 const char *encoding;
603 int i, j;
604 char *langEnv, pver[1024] = "";
605
606 #ifdef REPLACE_NOTIFIER
607 egg_bzero(&notifierprocs, sizeof(notifierprocs));
608 notifierprocs.initNotifierProc = tickle_InitNotifier;
609 notifierprocs.createFileHandlerProc = tickle_CreateFileHandler;
610 notifierprocs.deleteFileHandlerProc = tickle_DeleteFileHandler;
611 notifierprocs.setTimerProc = tickle_SetTimer;
612 notifierprocs.waitForEventProc = tickle_WaitForEvent;
613 notifierprocs.finalizeNotifierProc = tickle_FinalizeNotifier;
614
615 Tcl_SetNotifier(&notifierprocs);
616 #endif /* REPLACE_NOTIFIER */
617
618 /* This must be done *BEFORE* Tcl_SetSystemEncoding(),
619 * or Tcl_SetSystemEncoding() will cause a segfault.
620 */
621 /* This is used for 'info nameofexecutable'.
622 * The filename in argv[0] must exist in a directory listed in
623 * the environment variable PATH for it to register anything.
624 */
625 Tcl_FindExecutable(argv[0]);
626
627 /* Initialize the interpreter */
628 interp = Tcl_CreateInterp();
629
630 #ifdef DEBUG_MEM
631 /* Initialize Tcl's memory debugging if we want it */
632 Tcl_InitMemory(interp);
633 #endif
634
635 /* Set Tcl variable tcl_interactive to 0 */
636 Tcl_SetVar(interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
637
638 /* Setup script library facility */
639 Tcl_Init(interp);
640 Tcl_SetServiceMode(TCL_SERVICE_ALL);
641
642 /* Code based on Tcl's TclpSetInitialEncodings() */
643 /* Determine the current encoding from the LC_* or LANG environment
644 * variables.
645 */
646 langEnv = getenv("LC_ALL");
647 if (langEnv == NULL || langEnv[0] == '\0') {
648 langEnv = getenv("LC_CTYPE");
649 }
650 if (langEnv == NULL || langEnv[0] == '\0') {
651 langEnv = getenv("LANG");
652 }
653 if (langEnv == NULL || langEnv[0] == '\0') {
654 langEnv = NULL;
655 }
656
657 encoding = NULL;
658 if (langEnv != NULL) {
659 for (i = 0; localeTable[i].lang != NULL; i++)
660 if (strcmp(localeTable[i].lang, langEnv) == 0) {
661 encoding = localeTable[i].encoding;
662 break;
663 }
664
665 /* There was no mapping in the locale table. If there is an
666 * encoding subfield, we can try to guess from that.
667 */
668 if (encoding == NULL) {
669 char *p;
670
671 for (p = langEnv; *p != '\0'; p++) {
672 if (*p == '.') {
673 p++;
674 break;
675 }
676 }
677 if (*p != '\0') {
678 Tcl_DString ds;
679
680 Tcl_DStringInit(&ds);
681 Tcl_DStringAppend(&ds, p, -1);
682
683 encoding = Tcl_DStringValue(&ds);
684 Tcl_UtfToLower(Tcl_DStringValue(&ds));
685 if (Tcl_SetSystemEncoding(NULL, encoding) == TCL_OK) {
686 Tcl_DStringFree(&ds);
687 goto resetPath;
688 }
689 Tcl_DStringFree(&ds);
690 encoding = NULL;
691 }
692 }
693 }
694
695 if (encoding == NULL) {
696 encoding = "iso8859-1";
697 }
698
699 Tcl_SetSystemEncoding(NULL, encoding);
700
701 resetPath:
702
703 /* Initialize the C library's locale subsystem. */
704 setlocale(LC_CTYPE, "");
705
706 /* In case the initial locale is not "C", ensure that the numeric
707 * processing is done in "C" locale regardless. */
708 setlocale(LC_NUMERIC, "C");
709
710 /* Keep the iso8859-1 encoding preloaded. The IO package uses it for
711 * gets on a binary channel. */
712 Tcl_GetEncoding(NULL, "iso8859-1");
713
714 /* Add eggdrop to Tcl's package list */
715 for (j = 0; j <= strlen(egg_version); j++) {
716 if ((egg_version[j] == ' ') || (egg_version[j] == '+'))
717 break;
718 pver[strlen(pver)] = egg_version[j];
719 }
720 Tcl_PkgProvide(interp, "eggdrop", pver);
721
722 /* Initialize binds and traces */
723 init_bind();
724 init_traces();
725
726 /* Add new commands */
727 add_tcl_commands(tcluser_cmds);
728 add_tcl_commands(tcldcc_cmds);
729 add_tcl_commands(tclmisc_cmds);
730 add_tcl_objcommands(tclmisc_objcmds);
731 add_tcl_commands(tcldns_cmds);
732 #ifdef TLS
733 add_tcl_commands(tcltls_cmds);
734 #endif
735 }
736
737 void do_tcl(char *whatzit, char *script)
738 {
739 int code;
740 char *result;
741 Tcl_DString dstr;
742
743 code = Tcl_Eval(interp, script);
744
745 /* properly convert string to system encoding. */
746 Tcl_DStringInit(&dstr);
747 Tcl_UtfToExternalDString(NULL, tcl_resultstring(), -1, &dstr);
748 result = Tcl_DStringValue(&dstr);
749
750 if (code != TCL_OK) {
751 putlog(LOG_MISC, "*", "Tcl error in script for '%s':", whatzit);
752 putlog(LOG_MISC, "*", "%s", result);
753 Tcl_BackgroundError(interp);
754 }
755
756 Tcl_DStringFree(&dstr);
757 }
758
759 /* Interpret tcl file fname.
760 *
761 * returns: 1 - if everything was okay
762 */
763 int readtclprog(char *fname)
764 {
765 int code;
766 EGG_CONST char *result;
767 Tcl_DString dstr;
768
769 if (!file_readable(fname))
770 return 0;
771
772 code = Tcl_EvalFile(interp, fname);
773 result = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
774
775 /* properly convert string to system encoding. */
776 Tcl_DStringInit(&dstr);
777 Tcl_UtfToExternalDString(NULL, result, -1, &dstr);
778 result = Tcl_DStringValue(&dstr);
779
780 if (code != TCL_OK) {
781 putlog(LOG_MISC, "*", "Tcl error in file '%s':", fname);
782 putlog(LOG_MISC, "*", "%s", result);
783 Tcl_BackgroundError(interp);
784 code = 0; /* JJM: refactored to remove premature return */
785 } else {
786 /* Refresh internal variables */
787 code = 1;
788 }
789
790 Tcl_DStringFree(&dstr);
791
792 return code;
793 }
794
795 void add_tcl_strings(tcl_strings *list)
796 {
797 int i;
798 strinfo *st;
799 int tmp;
800
801 for (i = 0; list[i].name; i++) {
802 st = nmalloc(sizeof *st);
803 strtot += sizeof(strinfo);
804 st->max = list[i].length - (list[i].flags & STR_DIR);
805 if (list[i].flags & STR_PROTECT)
806 st->max = -st->max;
807 st->str = list[i].buf;
808 st->flags = (list[i].flags & STR_DIR);
809 tmp = protect_readonly;
810 protect_readonly = 0;
811 tcl_eggstr((ClientData) st, interp, list[i].name, NULL, TCL_TRACE_WRITES);
812 protect_readonly = tmp;
813 tcl_eggstr((ClientData) st, interp, list[i].name, NULL, TCL_TRACE_READS);
814 Tcl_TraceVar(interp, list[i].name, TCL_TRACE_READS | TCL_TRACE_WRITES |
815 TCL_TRACE_UNSETS, tcl_eggstr, (ClientData) st);
816 }
817 }
818
819 void rem_tcl_strings(tcl_strings *list)
820 {
821 int i, f;
822 strinfo *st;
823
824 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
825 for (i = 0; list[i].name; i++) {
826 st = (strinfo *) Tcl_VarTraceInfo(interp, list[i].name, f, tcl_eggstr,
827 NULL);
828 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggstr, st);
829 if (st != NULL) {
830 strtot -= sizeof(strinfo);
831 nfree(st);
832 }
833 }
834 }
835
836 void add_tcl_ints(tcl_ints *list)
837 {
838 int i, tmp;
839 intinfo *ii;
840
841 for (i = 0; list[i].name; i++) {
842 ii = nmalloc(sizeof *ii);
843 strtot += sizeof(intinfo);
844 ii->var = list[i].val;
845 ii->ro = list[i].readonly;
846 tmp = protect_readonly;
847 protect_readonly = 0;
848 tcl_eggint((ClientData) ii, interp, list[i].name, NULL, TCL_TRACE_WRITES);
849 protect_readonly = tmp;
850 tcl_eggint((ClientData) ii, interp, list[i].name, NULL, TCL_TRACE_READS);
851 Tcl_TraceVar(interp, list[i].name,
852 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
853 tcl_eggint, (ClientData) ii);
854 }
855
856 }
857
858 void rem_tcl_ints(tcl_ints *list)
859 {
860 int i, f;
861 intinfo *ii;
862
863 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
864 for (i = 0; list[i].name; i++) {
865 ii = (intinfo *) Tcl_VarTraceInfo(interp, list[i].name, f, tcl_eggint,
866 NULL);
867 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggint, (ClientData) ii);
868 if (ii) {
869 strtot -= sizeof(intinfo);
870 nfree(ii);
871 }
872 }
873 }
874
875 /* Allocate couplet space for tracing couplets
876 */
877 void add_tcl_coups(tcl_coups *list)
878 {
879 coupletinfo *cp;
880 int i;
881
882 for (i = 0; list[i].name; i++) {
883 cp = nmalloc(sizeof *cp);
884 strtot += sizeof(coupletinfo);
885 cp->left = list[i].lptr;
886 cp->right = list[i].rptr;
887 tcl_eggcouplet((ClientData) cp, interp, list[i].name, NULL,
888 TCL_TRACE_WRITES);
889 tcl_eggcouplet((ClientData) cp, interp, list[i].name, NULL,
890 TCL_TRACE_READS);
891 Tcl_TraceVar(interp, list[i].name,
892 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
893 tcl_eggcouplet, (ClientData) cp);
894 }
895 }
896
897 void rem_tcl_coups(tcl_coups *list)
898 {
899 int i, f;
900 coupletinfo *cp;
901
902 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
903 for (i = 0; list[i].name; i++) {
904 cp = (coupletinfo *) Tcl_VarTraceInfo(interp, list[i].name, f,
905 tcl_eggcouplet, NULL);
906 strtot -= sizeof(coupletinfo);
907 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggcouplet, (ClientData) cp);
908 nfree(cp);
909 }
910 }
911
912 /* Check if the Tcl library supports threads
913 */
914 int tcl_threaded()
915 {
916 #ifdef HAVE_TCL_GETCURRENTTHREAD
917 if (Tcl_GetCurrentThread() != (Tcl_ThreadId)0)
918 return 1;
919 #endif
920
921 return 0;
922 }
923
924 /* Check if we need to fork before initializing Tcl
925 */
926 int fork_before_tcl()
927 {
928 #ifndef REPLACE_NOTIFIER
929 return tcl_threaded();
930 #endif
931 return 0;
932 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23