1 /* This file contains library-wide symbols that are always needed when one
2 * links to the library.
5 Copyright (C) 2004, 2005, 2006 John E. Davis
7 This file is part of the S-Lang Library.
9 The S-Lang Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
14 The S-Lang Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this library; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
34 #ifdef HAVE_LANGINFO_H
35 # include <langinfo.h>
38 #define DEBUG_MALLOC 0
41 # define SLREALLOC_FUN SLdebug_realloc
42 # define SLMALLOC_FUN SLdebug_malloc
43 # define SLFREE_FUN SLdebug_free
45 # define SLREALLOC_FUN SLREALLOC
46 # define SLMALLOC_FUN SLMALLOC
47 # define SLFREE_FUN SLFREE
51 int SLang_Version
= SLANG_VERSION
;
52 char *SLang_Version_String
= SLANG_VERSION_STRING
;
54 int _pSLinterp_UTF8_Mode
= 0;
55 int _pSLtt_UTF8_Mode
= 0;
56 int _pSLutf8_mode
= 0;
59 # define setlocale(x,y) (NULL)
63 int SLutf8_is_utf8_mode (void)
68 int SLinterp_utf8_enable (int mode
)
73 return _pSLinterp_UTF8_Mode
= mode
;
76 int SLinterp_is_utf8_mode (void)
78 return _pSLinterp_UTF8_Mode
;
81 static int utf8_enable (int mode
)
88 (void) setlocale (LC_ALL
, "");
90 #ifdef HAVE_NL_LANGINFO_CODESET
91 locale
= nl_langinfo (CODESET
);
92 if ((locale
!= NULL
) && (*locale
))
94 if ((0 == strcmp (locale
, "UTF-8"))
95 || (0 == strcmp (locale
, "utf-8"))
96 || (0 == strcmp (locale
, "utf8"))
97 || (0 == strcmp (locale
, "UTF8")))
104 locale
= setlocale (LC_ALL
, "");
106 if (((locale
== NULL
) || (*locale
== 0))
107 && ((NULL
== (locale
= getenv ("LC_ALL"))) || (*locale
== 0))
108 && ((NULL
== (locale
= getenv ("LC_CTYPE"))) || (*locale
== 0))
109 && ((NULL
== (locale
= getenv ("LANG"))) || (*locale
== 0)))
112 /* setlocale man page says the return value is something like:
113 * language[_territory][.codeset][@modifier][+special][,...
114 * Here, we want the codeset, if present.
117 while (*locale
&& (*locale
!= '.') && (*locale
!= '@')
118 && (*locale
!= '+') && (*locale
!= ','))
124 if (0 == strncmp (locale
, "UTF-8", 5))
126 else if (0 == strncmp (locale
, "utf8", 4))
131 if ((*locale
== 0) || (*locale
== '@')
132 || (*locale
== '+') || (*locale
== ','))
140 /* Returns the value of _pSLutf8_mode */
141 int SLutf8_enable (int mode
)
143 mode
= utf8_enable (mode
);
144 _pSLutf8_mode
= mode
;
145 _pSLtt_UTF8_Mode
= mode
;
146 _pSLinterp_UTF8_Mode
= mode
;
150 #ifndef MIDNIGHT_COMMANDER_CODE
152 char *SLmalloc (unsigned int len
)
156 p
= (char *) SLMALLOC_FUN (len
);
158 SLang_set_error (SL_MALLOC_ERROR
);
163 void SLfree (char *p
)
165 if (p
!= NULL
) SLFREE_FUN (p
);
168 char *SLrealloc (char *p
, unsigned int len
)
176 if (p
== NULL
) p
= SLmalloc (len
);
179 p
= (char *)SLREALLOC_FUN (p
, len
);
181 SLang_set_error (SL_MALLOC_ERROR
);
186 #endif /* !MIDNIGHT_COMMANDER_CODE */
188 char *SLcalloc (unsigned int nelems
, unsigned int len
)
194 if (p
!= NULL
) memset (p
, 0, len
);
198 #if 0 /* was: !defined(HAVE_ISSETUGID) && defined(__GLIBC__) && (__GLIBC__ >= 2) */
199 extern int __libc_enable_secure
;
200 # define HAVE___LIBC_ENABLE_SECURE 1
203 int _pSLsecure_issetugid (void)
205 #ifdef HAVE_ISSETUGID
206 return (1 == issetugid ());
208 # if defined HAVE___LIBC_ENABLE_SECURE && 0
209 return __libc_enable_secure
;
211 # if defined(HAVE_GETUID) && defined(HAVE_GETEUID) && defined(HAVE_GETGID) && defined(HAVE_GETEUID)
212 static int enable_secure
;
213 if (enable_secure
== 0)
215 if ((getuid () != geteuid ())
216 || (getgid () != getegid ()))
221 return (enable_secure
== 1);
229 /* Like getenv, except if running as setuid or setgid, returns NULL */
230 char *_pSLsecure_getenv (char *s
)
232 if (_pSLsecure_issetugid ())
238 typedef struct Interrupt_Hook_Type
240 int (*func
)(VOID_STAR
);
241 VOID_STAR client_data
;
242 struct Interrupt_Hook_Type
*next
;
246 static Interrupt_Hook_Type
*Interrupt_Hooks
= NULL
;
248 static Interrupt_Hook_Type
*
249 find_interrupt_hook (int (*func
)(VOID_STAR
), VOID_STAR cd
,
250 Interrupt_Hook_Type
**prevp
)
252 Interrupt_Hook_Type
*h
= Interrupt_Hooks
;
253 Interrupt_Hook_Type
*prev
= NULL
;
256 if ((h
->func
== func
) && (h
->client_data
== cd
))
267 int SLang_add_interrupt_hook (int (*func
)(VOID_STAR
), VOID_STAR cd
)
269 Interrupt_Hook_Type
*h
;
271 if (NULL
!= find_interrupt_hook (func
, cd
, NULL
))
274 if (NULL
== (h
= (Interrupt_Hook_Type
*)SLmalloc (sizeof (Interrupt_Hook_Type
))))
279 h
->next
= Interrupt_Hooks
;
284 void SLang_remove_interrupt_hook (int (*func
)(VOID_STAR
), VOID_STAR cd
)
286 Interrupt_Hook_Type
*h
, *hprev
;
288 if (NULL
== (h
= find_interrupt_hook (func
, cd
, &hprev
)))
292 Interrupt_Hooks
= h
->next
;
294 hprev
->next
= h
->next
;
299 int SLang_handle_interrupt (void)
301 Interrupt_Hook_Type
*h
;
307 if (-1 == (*h
->func
)(h
->client_data
))
315 #if defined(__WIN32__) && defined(SLANG_DLL)
316 # include <windows.h>
318 BOOL WINAPI
DllMain(HANDLE hInstance
,DWORD dwReason
,LPVOID lpParam
)