forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fallback.h
484 lines (377 loc) · 11.8 KB
/
fallback.h
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#ifndef FISH_FALLBACK_H
#define FISH_FALLBACK_H
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <wctype.h>
#include <wchar.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>
/** fish's internal versions of wcwidth and wcswidth, which can use an internal implementation if the system one is busted. */
int fish_wcwidth(wchar_t wc);
int fish_wcswidth(const wchar_t *str, size_t n);
#ifndef WCHAR_MAX
/**
This _should_ be defined by wchar.h, but e.g. OpenBSD doesn't.
*/
#define WCHAR_MAX INT_MAX
#endif
/**
Make sure __func__ is defined to some string. In C99, this should
be the currently compiled function. If we aren't using C99 or
later, older versions of GCC had __FUNCTION__.
*/
#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define __func__ __FUNCTION__
# else
# define __func__ "<unknown>"
# endif
#endif
/**
Under curses, tputs expects an int (*func)(char) as its last
parameter, but in ncurses, tputs expects a int (*func)(int) as its
last parameter. tputs_arg_t is defined to always be what tputs
expects. Hopefully.
*/
#ifdef NCURSES_VERSION
typedef int tputs_arg_t;
#else
typedef char tputs_arg_t;
#endif
#ifndef SIGIO
#define SIGIO SIGUSR1
#endif
#ifndef SIGWINCH
#define SIGWINCH SIGUSR2
#endif
#ifndef HAVE_WINSIZE
/**
Structure used to get the size of a terminal window
*/
struct winsize
{
/**
Number of rows
*/
unsigned short ws_row;
/**
Number of columns
*/
unsigned short ws_col;
}
;
#endif
#ifdef TPUTS_KLUDGE
/**
Linux on PPC seems to have a tputs implementation that sometimes
behaves strangely. This fallback seems to fix things.
*/
int tputs(const char *str, int affcnt, int (*fish_putc)(tputs_arg_t));
#endif
#ifdef TPARM_SOLARIS_KLUDGE
/**
Solaris tparm has a set fixed of paramters in it's curses implementation,
work around this here.
*/
#define tparm tparm_solaris_kludge
char *tparm_solaris_kludge(char *str, ...);
#endif
#ifndef HAVE_FWPRINTF
/**
Print formated string. Some operating systems (Like NetBSD) do not
have wide string formating functions. Therefore we implement our
own. Not at all complete. Supports wide and narrow characters,
strings and decimal numbers, position (%n), field width and
precision.
*/
int fwprintf(FILE *f, const wchar_t *format, ...);
/**
Print formated string. Some operating systems (Like NetBSD) do not
have wide string formating functions. Therefore we define our
own. Not at all complete. Supports wide and narrow characters,
strings and decimal numbers, position (%n), field width and
precision.
*/
int swprintf(wchar_t *str, size_t l, const wchar_t *format, ...);
/**
Print formated string. Some operating systems (Like NetBSD) do not
have wide string formating functions. Therefore we define our
own. Not at all complete. Supports wide and narrow characters,
strings and decimal numbers, position (%n), field width and
precision.
*/
int wprintf(const wchar_t *format, ...);
/**
Print formated string. Some operating systems (Like NetBSD) do not
have wide string formating functions. Therefore we define our
own. Not at all complete. Supports wide and narrow characters,
strings and decimal numbers, position (%n), field width and
precision.
*/
int vwprintf(const wchar_t *filter, va_list va);
/**
Print formated string. Some operating systems (Like NetBSD) do not
have wide string formating functions. Therefore we define our
own. Not at all complete. Supports wide and narrow characters,
strings and decimal numbers, position (%n), field width and
precision.
*/
int vfwprintf(FILE *f, const wchar_t *filter, va_list va);
/**
Print formated string. Some operating systems (Like NetBSD) do not
have wide string formating functions. Therefore we define our
own. Not at all complete. Supports wide and narrow characters,
strings and decimal numbers, position (%n), field width and
precision.
*/
int vswprintf(wchar_t *out, size_t n, const wchar_t *filter, va_list va);
#endif
#ifndef HAVE_FGETWC
/**
Fallback implementation of fgetwc
*/
wint_t fgetwc(FILE *stream);
/**
Fallback implementation of getwc
*/
wint_t getwc(FILE *stream);
#endif
#ifndef HAVE_FPUTWC
/**
Fallback implementation of fputwc
*/
wint_t fputwc(wchar_t wc, FILE *stream);
/**
Fallback implementation of putwc
*/
wint_t putwc(wchar_t wc, FILE *stream);
#endif
#ifndef HAVE_WCSTOK
/**
Fallback implementation of wcstok. Uses code borrowed from glibc.
*/
wchar_t *wcstok(wchar_t *wcs, const wchar_t *delim, wchar_t **ptr);
#endif
#ifndef HAVE_WCWIDTH
/**
Return the number of columns used by a character. This is a libc
function, but the prototype for this function is missing in some libc
implementations.
Fish has a fallback implementation in case the implementation is
missing altogether. In locales without a native wcwidth, Unicode
is probably so broken that it isn't worth trying to implement a
real wcwidth. Therefore, the fallback wcwidth assumes any printing
character takes up one column and anything else uses 0 columns.
*/
int wcwidth(wchar_t c);
#endif
/** On OS X, use weak linking for wcsdup and wcscasecmp. Weak linking allows you to call the function only if it exists at runtime. You can detect it by testing the function pointer against NULL. To avoid making the callers do that, redefine wcsdup to wcsdup_use_weak, and likewise with wcscasecmp. This lets us use the same binary on SnowLeopard (10.6) and Lion+ (10.7), even though these functions only exist on 10.7+.
On other platforms, use what's detected at build time.
*/
#if __APPLE__ && __DARWIN_C_LEVEL >= 200809L
wchar_t *wcsdup_use_weak(const wchar_t *);
int wcscasecmp_use_weak(const wchar_t *, const wchar_t *);
int wcsncasecmp_use_weak(const wchar_t *s1, const wchar_t *s2, size_t n);
#define wcsdup(a) wcsdup_use_weak((a))
#define wcscasecmp(a, b) wcscasecmp_use_weak((a), (b))
#define wcsncasecmp(a, b, c) wcsncasecmp_use_weak((a), (b), (c))
#else
#ifndef HAVE_WCSDUP
/**
Create a duplicate string. Wide string version of strdup. Will
automatically exit if out of memory.
*/
wchar_t *wcsdup(const wchar_t *in);
#endif
#ifndef HAVE_WCSCASECMP
/**
Case insensitive string compare function. Wide string version of
strcasecmp.
This implementation of wcscasecmp does not take into account
esoteric locales where uppercase and lowercase do not cleanly
transform between each other. Hopefully this should be fine since
fish only uses this function with one of the strings supplied by
fish and guaranteed to be a sane, english word. Using wcscasecmp on
a user-supplied string should be considered a bug.
*/
int wcscasecmp(const wchar_t *a, const wchar_t *b);
#endif
#endif //__APPLE__
#ifndef HAVE_WCSLEN
/**
Fallback for wclsen. Returns the length of the specified string.
*/
size_t wcslen(const wchar_t *in);
#endif
#ifndef HAVE_WCSNCASECMP
/**
Case insensitive string compare function. Wide string version of
strncasecmp.
This implementation of wcsncasecmp does not take into account
esoteric locales where uppercase and lowercase do not cleanly
transform between each other. Hopefully this should be fine since
fish only uses this function with one of the strings supplied by
fish and guaranteed to be a sane, english word. Using wcsncasecmp on
a user-supplied string should be considered a bug.
*/
int wcsncasecmp(const wchar_t *a, const wchar_t *b, size_t count);
/**
Returns a newly allocated wide character string wich is a copy of
the string in, but of length c or shorter. The returned string is
always null terminated, and the null is not included in the string
length.
*/
#endif
#ifndef HAVE_WCSNDUP
/**
Fallback for wcsndup function. Returns a copy of \c in, truncated
to a maximum length of \c c.
*/
wchar_t *wcsndup(const wchar_t *in, size_t c);
#endif
/**
Converts from wide char to digit in the specified base. If d is not
a valid digit in the specified base, return -1. This is a helper
function for wcstol, but it is useful itself, so it is exported.
*/
long convert_digit(wchar_t d, int base);
#ifndef HAVE_WCSTOL
/**
Fallback implementation. Convert a wide character string to a
number in the specified base. This functions is the wide character
string equivalent of strtol. For bases of 10 or lower, 0..9 are
used to represent numbers. For bases below 36, a-z and A-Z are used
to represent numbers higher than 9. Higher bases than 36 are not
supported.
*/
long wcstol(const wchar_t *nptr,
wchar_t **endptr,
int base);
#endif
#ifndef HAVE_WCSLCAT
/**
Appends src to string dst of size siz (unlike wcsncat, siz is the
full size of dst, not space left). At most siz-1 characters will be
copied. Always NUL terminates (unless siz <= wcslen(dst)). Returns
wcslen(src) + MIN(siz, wcslen(initial dst)). If retval >= siz,
truncation occurred.
This is the OpenBSD strlcat function, modified for wide characters,
and renamed to reflect this change.
*/
size_t wcslcat(wchar_t *dst, const wchar_t *src, size_t siz);
#endif
#ifndef HAVE_WCSLCPY
/**
Copy src to string dst of size siz. At most siz-1 characters will
be copied. Always NUL terminates (unless siz == 0). Returns
wcslen(src); if retval >= siz, truncation occurred.
This is the OpenBSD strlcpy function, modified for wide characters,
and renamed to reflect this change.
*/
size_t wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz);
#endif
/**
BSD del_curterm seems to do a double-free. We redefine it as a no-op
*/
#ifdef HAVE_BROKEN_DEL_CURTERM
#define fish_del_curterm(X) OK
#else
#define fish_del_curterm(X) del_curterm(X)
#endif
#ifndef HAVE_LRAND48_R
/**
Datastructure for the lrand48_r fallback implementation.
*/
struct drand48_data
{
/**
Seed value
*/
unsigned int seed;
}
;
/**
Fallback implementation of lrand48_r. Internally uses rand_r, so it is pretty weak.
*/
int lrand48_r(struct drand48_data *buffer, long int *result);
/**
Fallback implementation of srand48_r, the seed function for lrand48_r.
*/
int srand48_r(long int seedval, struct drand48_data *buffer);
#endif
#ifndef HAVE_FUTIMES
int futimes(int fd, const struct timeval *times);
#endif
/* autoconf may fail to detect gettext (645), so don't define a function call gettext or we'll get build errors */
/** Cover for gettext() */
char * fish_gettext(const char * msgid);
/** Cover for bindtextdomain() */
char * fish_bindtextdomain(const char * domainname, const char * dirname);
/** Cover for textdomain() */
char * fish_textdomain(const char * domainname);
/* Cover for dcgettext */
char * fish_dcgettext(const char * domainname, const char * msgid, int category);
#ifndef HAVE__NL_MSG_CAT_CNTR
/**
Some gettext implementation use have this variable, and by
increasing it, one can tell the system that the translations need
to be reloaded.
*/
extern int _nl_msg_cat_cntr;
#endif
#ifndef HAVE_KILLPG
/**
Send specified signal to specified process group.
*/
int killpg(int pgr, int sig);
#endif
#ifndef HAVE_WORKING_GETOPT_LONG
/**
Struct describing a long getopt option
*/
struct option
{
/**
Name of option
*/
const char *name;
/**
Flag
*/
int has_arg;
/**
Flag
*/
int *flag;
/**
Return value
*/
int val;
}
;
#ifndef no_argument
#define no_argument 0
#endif
#ifndef required_argument
#define required_argument 1
#endif
#ifndef optional_argument
#define optional_argument 2
#endif
int getopt_long(int argc,
char * const argv[],
const char *optstring,
const struct option *longopts,
int *longindex);
#endif
#ifndef HAVE_SYSCONF
#define _SC_ARG_MAX 1
long sysconf(int name);
#endif
#ifndef HAVE_NAN
double nan(char *tagp);
#endif
#endif