slcommon.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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
  6. This file is part of the S-Lang Library.
  7. The S-Lang Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. The S-Lang Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  18. USA.
  19. */
  20. #define _GNU_SOURCE
  21. #include "slinclud.h"
  22. #include "slang.h"
  23. #include "_slang.h"
  24. #ifdef HAVE_LOCALE_H
  25. # include <locale.h>
  26. #endif
  27. #ifdef HAVE_LANGINFO_H
  28. # include <langinfo.h>
  29. #endif
  30. #define DEBUG_MALLOC 0
  31. #if DEBUG_MALLOC
  32. # define SLREALLOC_FUN SLdebug_realloc
  33. # define SLMALLOC_FUN SLdebug_malloc
  34. # define SLFREE_FUN SLdebug_free
  35. #else
  36. # define SLREALLOC_FUN SLREALLOC
  37. # define SLMALLOC_FUN SLMALLOC
  38. # define SLFREE_FUN SLFREE
  39. #endif
  40. int SLang_Version = SLANG_VERSION;
  41. char *SLang_Version_String = SLANG_VERSION_STRING;
  42. int _pSLinterp_UTF8_Mode = 0;
  43. int _pSLtt_UTF8_Mode = 0;
  44. int _pSLutf8_mode = 0;
  45. #ifndef HAVE_LOCALE_H
  46. # define setlocale(x,y) (NULL)
  47. # define LC_ALL 0
  48. #endif
  49. int SLutf8_is_utf8_mode (void)
  50. {
  51. return _pSLutf8_mode;
  52. }
  53. int SLinterp_utf8_enable (int mode)
  54. {
  55. if (mode == -1)
  56. mode = _pSLutf8_mode;
  57. return _pSLinterp_UTF8_Mode = mode;
  58. }
  59. int SLinterp_is_utf8_mode (void)
  60. {
  61. return _pSLinterp_UTF8_Mode;
  62. }
  63. static int utf8_enable (int mode)
  64. {
  65. char *locale;
  66. if (mode != -1)
  67. return (mode != 0);
  68. (void) setlocale (LC_ALL, "");
  69. #ifdef HAVE_NL_LANGINFO_CODESET
  70. locale = nl_langinfo (CODESET);
  71. if ((locale != NULL) && (*locale))
  72. {
  73. if ((0 == strcmp (locale, "UTF-8"))
  74. || (0 == strcmp (locale, "utf-8"))
  75. || (0 == strcmp (locale, "utf8"))
  76. || (0 == strcmp (locale, "UTF8")))
  77. return 1;
  78. return 0;
  79. }
  80. #endif
  81. locale = setlocale (LC_ALL, "");
  82. if (((locale == NULL) || (*locale == 0))
  83. && ((NULL == (locale = getenv ("LC_ALL"))) || (*locale == 0))
  84. && ((NULL == (locale = getenv ("LC_CTYPE"))) || (*locale == 0))
  85. && ((NULL == (locale = getenv ("LANG"))) || (*locale == 0)))
  86. return 0;
  87. /* setlocale man page says the return value is something like:
  88. * language[_territory][.codeset][@modifier][+special][,...
  89. * Here, we want the codeset, if present.
  90. */
  91. while (*locale && (*locale != '.') && (*locale != '@')
  92. && (*locale != '+') && (*locale != ','))
  93. locale++;
  94. if (*locale == '.')
  95. {
  96. locale++;
  97. if (0 == strncmp (locale, "UTF-8", 5))
  98. locale += 5;
  99. else if (0 == strncmp (locale, "utf8", 4))
  100. locale += 4;
  101. else
  102. return 0;
  103. if ((*locale == 0) || (*locale == '@')
  104. || (*locale == '+') || (*locale == ','))
  105. return 1;
  106. }
  107. return 0;
  108. }
  109. /* Returns the value of _pSLutf8_mode */
  110. int SLutf8_enable (int mode)
  111. {
  112. mode = utf8_enable (mode);
  113. _pSLutf8_mode = mode;
  114. _pSLtt_UTF8_Mode = mode;
  115. _pSLinterp_UTF8_Mode = mode;
  116. return mode;
  117. }
  118. #ifndef MIDNIGHT_COMMANDER_CODE
  119. char *SLmalloc (unsigned int len)
  120. {
  121. char *p;
  122. p = (char *) SLMALLOC_FUN (len);
  123. if (p == NULL)
  124. SLang_set_error (SL_MALLOC_ERROR);
  125. return p;
  126. }
  127. void SLfree (char *p)
  128. {
  129. if (p != NULL) SLFREE_FUN (p);
  130. }
  131. char *SLrealloc (char *p, unsigned int len)
  132. {
  133. if (len == 0)
  134. {
  135. SLfree (p);
  136. return NULL;
  137. }
  138. if (p == NULL) p = SLmalloc (len);
  139. else
  140. {
  141. p = (char *)SLREALLOC_FUN (p, len);
  142. if (p == NULL)
  143. SLang_set_error (SL_MALLOC_ERROR);
  144. }
  145. return p;
  146. }
  147. #endif /* !MIDNIGHT_COMMANDER_CODE */
  148. char *SLcalloc (unsigned int nelems, unsigned int len)
  149. {
  150. char *p;
  151. len = nelems * len;
  152. p = SLmalloc (len);
  153. if (p != NULL) memset (p, 0, len);
  154. return p;
  155. }
  156. #if 0 /* was: !defined(HAVE_ISSETUGID) && defined(__GLIBC__) && (__GLIBC__ >= 2) */
  157. extern int __libc_enable_secure;
  158. # define HAVE___LIBC_ENABLE_SECURE 1
  159. #endif
  160. int _pSLsecure_issetugid (void)
  161. {
  162. #ifdef HAVE_ISSETUGID
  163. return (1 == issetugid ());
  164. #else
  165. # if defined HAVE___LIBC_ENABLE_SECURE && 0
  166. return __libc_enable_secure;
  167. # else
  168. # if defined(HAVE_GETUID) && defined(HAVE_GETEUID) && defined(HAVE_GETGID) && defined(HAVE_GETEUID)
  169. static int enable_secure;
  170. if (enable_secure == 0)
  171. {
  172. if ((getuid () != geteuid ())
  173. || (getgid () != getegid ()))
  174. enable_secure = 1;
  175. else
  176. enable_secure = -1;
  177. }
  178. return (enable_secure == 1);
  179. # else
  180. return 0;
  181. # endif
  182. # endif
  183. #endif
  184. }
  185. /* Like getenv, except if running as setuid or setgid, returns NULL */
  186. char *_pSLsecure_getenv (char *s)
  187. {
  188. if (_pSLsecure_issetugid ())
  189. return NULL;
  190. return getenv (s);
  191. }
  192. typedef struct Interrupt_Hook_Type
  193. {
  194. int (*func)(VOID_STAR);
  195. VOID_STAR client_data;
  196. struct Interrupt_Hook_Type *next;
  197. }
  198. Interrupt_Hook_Type;
  199. static Interrupt_Hook_Type *Interrupt_Hooks = NULL;
  200. static Interrupt_Hook_Type *
  201. find_interrupt_hook (int (*func)(VOID_STAR), VOID_STAR cd,
  202. Interrupt_Hook_Type **prevp)
  203. {
  204. Interrupt_Hook_Type *h = Interrupt_Hooks;
  205. Interrupt_Hook_Type *prev = NULL;
  206. while (h != NULL)
  207. {
  208. if ((h->func == func) && (h->client_data == cd))
  209. {
  210. if (prevp != NULL)
  211. *prevp = prev;
  212. return h;
  213. }
  214. h = h->next;
  215. }
  216. return NULL;
  217. }
  218. int SLang_add_interrupt_hook (int (*func)(VOID_STAR), VOID_STAR cd)
  219. {
  220. Interrupt_Hook_Type *h;
  221. if (NULL != find_interrupt_hook (func, cd, NULL))
  222. return 0;
  223. if (NULL == (h = (Interrupt_Hook_Type *)SLmalloc (sizeof (Interrupt_Hook_Type))))
  224. return -1;
  225. h->func = func;
  226. h->client_data = cd;
  227. h->next = Interrupt_Hooks;
  228. Interrupt_Hooks = h;
  229. return 0;
  230. }
  231. void SLang_remove_interrupt_hook (int (*func)(VOID_STAR), VOID_STAR cd)
  232. {
  233. Interrupt_Hook_Type *h, *hprev;
  234. if (NULL == (h = find_interrupt_hook (func, cd, &hprev)))
  235. return;
  236. if (hprev == NULL)
  237. Interrupt_Hooks = h->next;
  238. else
  239. hprev->next = h->next;
  240. SLfree ((char *) h);
  241. }
  242. int SLang_handle_interrupt (void)
  243. {
  244. Interrupt_Hook_Type *h;
  245. int status = 0;
  246. h = Interrupt_Hooks;
  247. while (h != NULL)
  248. {
  249. if (-1 == (*h->func)(h->client_data))
  250. status = -1;
  251. h = h->next;
  252. }
  253. return status;
  254. }
  255. #if defined(__WIN32__) && defined(SLANG_DLL)
  256. # include <windows.h>
  257. BOOL WINAPI DllMain(HANDLE hInstance,DWORD dwReason,LPVOID lpParam)
  258. {
  259. return 1;
  260. }
  261. #endif