File: slcommon.c

package info (click to toggle)
slang2 2.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,580 kB
  • ctags: 10,851
  • sloc: ansic: 96,830; sh: 3,373; makefile: 1,033; pascal: 143
file content (371 lines) | stat: -rw-r--r-- 8,037 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* This file contains library-wide symbols that are always needed when one
 * links to the library.
 */
/*
Copyright (C) 2004-2016 John E. Davis

This file is part of the S-Lang Library.

The S-Lang Library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The S-Lang Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "slinclud.h"

#include <errno.h>
#include "slang.h"
#include "_slang.h"

#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif

#ifdef HAVE_LANGINFO_H
# include <langinfo.h>
#endif

#if defined(__WIN32__)
# include <windows.h>
#endif

#define SLSYSWRAP_REALLOC realloc
#define SLSYSWRAP_MALLOC malloc
#ifdef SLSYSWRAP
# include <slsyswrap.h>
#endif

int SLang_Version = SLANG_VERSION;
SLFUTURE_CONST char *SLang_Version_String = SLANG_VERSION_STRING;

int _pSLinterp_UTF8_Mode = 0;
int _pSLtt_UTF8_Mode = 0;
int _pSLutf8_mode = 0;

#ifndef HAVE_LOCALE_H
# define setlocale(x,y) (NULL)
# define LC_ALL 0
#endif

int SLutf8_is_utf8_mode (void)
{
   return _pSLutf8_mode;
}

int SLinterp_utf8_enable (int mode)
{
   if (mode == -1)
     mode = _pSLutf8_mode;

   return _pSLinterp_UTF8_Mode = mode;
}

int SLinterp_is_utf8_mode (void)
{
   return _pSLinterp_UTF8_Mode;
}

static int utf8_enable (int mode)
{
   char *locale;

   if (mode != -1)
     return (mode != 0);

#if defined(__WIN32__)
   if (GetConsoleOutputCP () == 65001)
     return 1;
#endif

   (void) setlocale (LC_ALL, "");

#ifdef HAVE_NL_LANGINFO_CODESET
   locale = nl_langinfo (CODESET);
# ifdef SLSYSWRAP
     {
	char *env;
	if ((NULL != (env = getenv ("SLTEST_NO_LANGINFO")))
	    && (atoi (env) != 0))
	  locale = NULL;		       /* skip this block of code */
     }
# endif
   if ((locale != NULL) && (*locale))
     {
	if ((0 == strcmp (locale, "UTF-8"))
	    || (0 == strcmp (locale, "utf-8"))
	    || (0 == strcmp (locale, "utf8"))
	    || (0 == strcmp (locale, "UTF8")))
	  return 1;

	return 0;
     }
#endif

   locale = setlocale (LC_ALL, "");

   if (((locale == NULL) || (*locale == 0))
       && ((NULL == (locale = getenv ("LC_ALL"))) || (*locale == 0))
       && ((NULL == (locale = getenv ("LC_CTYPE"))) || (*locale == 0))
       && ((NULL == (locale = getenv ("LANG"))) || (*locale == 0)))
     return 0;

   /* setlocale man page says the return value is something like:
    *   language[_territory][.codeset][@modifier][+special][,...
    * Here, we want the codeset, if present.
    */

   while (*locale && (*locale != '.') && (*locale != '@')
	  && (*locale != '+') && (*locale != ','))
     locale++;

   if (*locale == '.')
     {
	locale++;
	if (0 == strncmp (locale, "UTF-8", 5))
	  locale += 5;
	else if (0 == strncmp (locale, "utf8", 4))
	  locale += 4;
	else
	  return 0;

	if ((*locale == 0) || (*locale == '@')
	    || (*locale == '+') || (*locale == ','))
	  return 1;
     }

   return 0;
}

/* Returns the value of _pSLutf8_mode */
int SLutf8_enable (int mode)
{
   char *cjk;

   mode = utf8_enable (mode);
   _pSLutf8_mode = mode;
   _pSLtt_UTF8_Mode = mode;
   _pSLinterp_UTF8_Mode = mode;
   if (mode && (NULL != (cjk = getenv ("WCWIDTH_CJK_LEGACY"))))
     {
	if ((*cjk == 0) || (0==strcmp(cjk,"yes")))
	  (void) SLwchar_set_wcwidth_flags (SLWCWIDTH_CJK_LEGACY);
     }
   return mode;
}

/* Mallocing 0 bytes leads to undefined behavior.  Some C libraries will
 * return NULL, and others return non-NULL.  Here, if 0 bytes is requested,
 * and the library malloc function returns NULL, then malloc will be retried
 * using 1 byte.
 */
SLFUTURE_VOID *SLmalloc (SLstrlen_Type len)
{
   SLFUTURE_VOID *p;

   if (NULL != (p = (SLFUTURE_VOID *)SLSYSWRAP_MALLOC(len)))
     return p;

   if (len || (NULL == (p = (SLFUTURE_VOID *)SLSYSWRAP_MALLOC(1))))
     SLang_set_error (SL_MALLOC_ERROR);

   return p;
}

void SLfree (SLFUTURE_VOID *p)
{
   if (p != NULL) free (p);
}

SLFUTURE_VOID *SLrealloc (SLFUTURE_VOID *p, SLstrlen_Type len)
{
   if (len == 0)
     {
	SLfree (p);
	return NULL;
     }

   if (p == NULL) p = SLmalloc (len);
   else
     {
	p = (SLFUTURE_VOID *) SLSYSWRAP_REALLOC(p, len);
	if (p == NULL)
	  SLang_set_error (SL_MALLOC_ERROR);
     }
   return p;
}

SLFUTURE_VOID *_SLrecalloc (SLFUTURE_VOID *p, SLstrlen_Type nelems, SLstrlen_Type len)
{
   unsigned int nlen = nelems * len;

   if (nelems && (nlen/nelems != len))
     {
	SLang_set_error (SL_Malloc_Error);
	return NULL;
     }
   return SLrealloc (p, nlen);
}

SLFUTURE_VOID *_SLcalloc (SLstrlen_Type nelems, SLstrlen_Type len)
{
   SLstrlen_Type nlen = nelems * len;

   if (nelems && (nlen/nelems != len))
     {
	SLang_set_error (SL_Malloc_Error);
	return NULL;
     }
   return SLmalloc (nlen);
}

SLFUTURE_VOID *SLcalloc (SLstrlen_Type nelems, SLstrlen_Type len)
{
   SLFUTURE_VOID *p = _SLcalloc (nelems, len);
   if (p != NULL) memset (p, 0, len*nelems);
   return p;
}

int _pSLsecure_issetugid (void)
{
#ifdef HAVE_ISSETUGID
   return (1 == issetugid ());
#else
# if defined(HAVE_GETUID) && defined(HAVE_GETEUID) && defined(HAVE_GETGID) && defined(HAVE_GETEUID)
   static int enable_secure;
   if (enable_secure == 0)
     {
	if ((getuid () != geteuid ())
	    || (getgid () != getegid ()))
	  enable_secure = 1;
	else
	  enable_secure = -1;
     }
   return (enable_secure == 1);
# else
   return 0;
# endif
#endif
}

/* Like getenv, except if running as setuid or setgid, returns NULL */
char *_pSLsecure_getenv (SLCONST char *s)
{
   if (_pSLsecure_issetugid ())
     return NULL;
   return getenv (s);
}

typedef struct Interrupt_Hook_Type
{
   int (*func)(VOID_STAR);
   VOID_STAR client_data;
   struct Interrupt_Hook_Type *next;
}
Interrupt_Hook_Type;

static Interrupt_Hook_Type *Interrupt_Hooks = NULL;

static Interrupt_Hook_Type *
  find_interrupt_hook (int (*func)(VOID_STAR), VOID_STAR cd,
		       Interrupt_Hook_Type **prevp)
{
   Interrupt_Hook_Type *h = Interrupt_Hooks;
   Interrupt_Hook_Type *prev = NULL;
   while (h != NULL)
     {
	if ((h->func == func) && (h->client_data == cd))
	  {
	     if (prevp != NULL)
	       *prevp = prev;
	     return h;
	  }
	prev = h;
	h = h->next;
     }
   return NULL;
}

int SLang_add_interrupt_hook (int (*func)(VOID_STAR), VOID_STAR cd)
{
   Interrupt_Hook_Type *h;

   if (NULL != find_interrupt_hook (func, cd, NULL))
     return 0;

   if (NULL == (h = (Interrupt_Hook_Type *)SLmalloc (sizeof (Interrupt_Hook_Type))))
     return -1;

   h->func = func;
   h->client_data = cd;
   h->next = Interrupt_Hooks;
   Interrupt_Hooks = h;
   return 0;
}

void SLang_remove_interrupt_hook (int (*func)(VOID_STAR), VOID_STAR cd)
{
   Interrupt_Hook_Type *h, *hprev;

   if (NULL == (h = find_interrupt_hook (func, cd, &hprev)))
     return;

   if (hprev == NULL)
     Interrupt_Hooks = h->next;
   else
     hprev->next = h->next;

   SLfree ((char *) h);
}

int SLang_handle_interrupt (void)
{
   Interrupt_Hook_Type *h;
   int status = 0;
   int e = errno;
   int se = _pSLerrno_errno;

   h = Interrupt_Hooks;
   while (h != NULL)
     {
	if (-1 == (*h->func)(h->client_data))
	  status = -1;

	h = h->next;
     }
   errno = e;
   _pSLerrno_errno = se;
   return status;
}

int _pSLerrno_errno;

int SLerrno_set_errno (int sys_errno)
{
   _pSLerrno_errno = sys_errno;
   return 0;
}

#if defined(__WIN32__) && defined(SLANG_DLL)
# include <windows.h>

/* where is the prototype for DllMain? */
BOOL WINAPI DllMain(HANDLE hInstance,DWORD dwReason,LPVOID lpParam);

BOOL WINAPI DllMain(HANDLE hInstance,DWORD dwReason,LPVOID lpParam)
{
   return 1;
}
#endif