separated out history section names to history.h
[midnight-commander.git] / slang / slcommon.c
blob2ccd6131bdccba97b8a348d86763abf37b871bf2
1 /* This file contains library-wide symbols that are always needed when one
2 * links to the library.
3 */
4 /*
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,
22 USA.
24 #define _GNU_SOURCE
25 #include "slinclud.h"
27 #include "slang.h"
28 #include "_slang.h"
30 #ifdef HAVE_LOCALE_H
31 # include <locale.h>
32 #endif
34 #ifdef HAVE_LANGINFO_H
35 # include <langinfo.h>
36 #endif
38 #define DEBUG_MALLOC 0
40 #if DEBUG_MALLOC
41 # define SLREALLOC_FUN SLdebug_realloc
42 # define SLMALLOC_FUN SLdebug_malloc
43 # define SLFREE_FUN SLdebug_free
44 #else
45 # define SLREALLOC_FUN SLREALLOC
46 # define SLMALLOC_FUN SLMALLOC
47 # define SLFREE_FUN SLFREE
48 #endif
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;
58 #ifndef HAVE_LOCALE_H
59 # define setlocale(x,y) (NULL)
60 # define LC_ALL 0
61 #endif
63 int SLutf8_is_utf8_mode (void)
65 return _pSLutf8_mode;
68 int SLinterp_utf8_enable (int mode)
70 if (mode == -1)
71 mode = _pSLutf8_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)
83 char *locale;
85 if (mode != -1)
86 return (mode != 0);
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")))
98 return 1;
100 return 0;
102 #endif
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)))
110 return 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 != ','))
119 locale++;
121 if (*locale == '.')
123 locale++;
124 if (0 == strncmp (locale, "UTF-8", 5))
125 locale += 5;
126 else if (0 == strncmp (locale, "utf8", 4))
127 locale += 4;
128 else
129 return 0;
131 if ((*locale == 0) || (*locale == '@')
132 || (*locale == '+') || (*locale == ','))
133 return 1;
136 return 0;
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;
147 return mode;
150 #ifndef MIDNIGHT_COMMANDER_CODE
152 char *SLmalloc (unsigned int len)
154 char *p;
156 p = (char *) SLMALLOC_FUN (len);
157 if (p == NULL)
158 SLang_set_error (SL_MALLOC_ERROR);
160 return p;
163 void SLfree (char *p)
165 if (p != NULL) SLFREE_FUN (p);
168 char *SLrealloc (char *p, unsigned int len)
170 if (len == 0)
172 SLfree (p);
173 return NULL;
176 if (p == NULL) p = SLmalloc (len);
177 else
179 p = (char *)SLREALLOC_FUN (p, len);
180 if (p == NULL)
181 SLang_set_error (SL_MALLOC_ERROR);
183 return p;
186 #endif /* !MIDNIGHT_COMMANDER_CODE */
188 char *SLcalloc (unsigned int nelems, unsigned int len)
190 char *p;
192 len = nelems * len;
193 p = SLmalloc (len);
194 if (p != NULL) memset (p, 0, len);
195 return p;
198 #if 0 /* was: !defined(HAVE_ISSETUGID) && defined(__GLIBC__) && (__GLIBC__ >= 2) */
199 extern int __libc_enable_secure;
200 # define HAVE___LIBC_ENABLE_SECURE 1
201 #endif
203 int _pSLsecure_issetugid (void)
205 #ifdef HAVE_ISSETUGID
206 return (1 == issetugid ());
207 #else
208 # if defined HAVE___LIBC_ENABLE_SECURE && 0
209 return __libc_enable_secure;
210 # else
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 ()))
217 enable_secure = 1;
218 else
219 enable_secure = -1;
221 return (enable_secure == 1);
222 # else
223 return 0;
224 # endif
225 # endif
226 #endif
229 /* Like getenv, except if running as setuid or setgid, returns NULL */
230 char *_pSLsecure_getenv (char *s)
232 if (_pSLsecure_issetugid ())
233 return NULL;
234 return getenv (s);
238 typedef struct Interrupt_Hook_Type
240 int (*func)(VOID_STAR);
241 VOID_STAR client_data;
242 struct Interrupt_Hook_Type *next;
244 Interrupt_Hook_Type;
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;
254 while (h != NULL)
256 if ((h->func == func) && (h->client_data == cd))
258 if (prevp != NULL)
259 *prevp = prev;
260 return h;
262 h = h->next;
264 return NULL;
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))
272 return 0;
274 if (NULL == (h = (Interrupt_Hook_Type *)SLmalloc (sizeof (Interrupt_Hook_Type))))
275 return -1;
277 h->func = func;
278 h->client_data = cd;
279 h->next = Interrupt_Hooks;
280 Interrupt_Hooks = h;
281 return 0;
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)))
289 return;
291 if (hprev == NULL)
292 Interrupt_Hooks = h->next;
293 else
294 hprev->next = h->next;
296 SLfree ((char *) h);
299 int SLang_handle_interrupt (void)
301 Interrupt_Hook_Type *h;
302 int status = 0;
304 h = Interrupt_Hooks;
305 while (h != NULL)
307 if (-1 == (*h->func)(h->client_data))
308 status = -1;
310 h = h->next;
312 return status;
315 #if defined(__WIN32__) && defined(SLANG_DLL)
316 # include <windows.h>
318 BOOL WINAPI DllMain(HANDLE hInstance,DWORD dwReason,LPVOID lpParam)
320 return 1;
322 #endif