forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.cpp
2344 lines (2036 loc) · 61.1 KB
/
common.cpp
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** \file common.c
Various functions, mostly string utilities, that are used by most
parts of fish.
*/
#include "config.h"
#include <unistd.h>
#ifdef HAVE_STROPTS_H
#include <stropts.h>
#endif
#ifdef HAVE_SIGINFO_H
#include <siginfo.h>
#endif
#include <stdlib.h>
#include <termios.h>
#include <wchar.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <wctype.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <locale.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <algorithm>
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#if HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#endif
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "common.h"
#include "expand.h"
#include "proc.h"
#include "wildcard.h"
#include "parser.h"
#include "complete.h"
#include "util.cpp"
#include "fallback.cpp"
struct termios shell_modes;
// Note we foolishly assume that pthread_t is just a primitive. But it might be a struct.
static pthread_t main_thread_id = 0;
static bool thread_assertions_configured_for_testing = false;
wchar_t ellipsis_char;
wchar_t omitted_newline_char;
char *profile=0;
const wchar_t *program_name;
int debug_level=1;
/**
This struct should be continually updated by signals as the term resizes, and as such always contain the correct current size.
*/
static struct winsize termsize;
static char *wcs2str_internal(const wchar_t *in, char *out);
void show_stackframe()
{
ASSERT_IS_NOT_FORKED_CHILD();
/* Hack to avoid showing backtraces in the tester */
if (program_name && ! wcscmp(program_name, L"(ignore)"))
return;
void *trace[32];
int i, trace_size = 0;
trace_size = backtrace(trace, 32);
char **messages = backtrace_symbols(trace, trace_size);
if (messages)
{
debug(0, L"Backtrace:");
for (i=0; i<trace_size; i++)
{
fwprintf(stderr, L"%s\n", messages[i]);
}
free(messages);
}
}
int fgetws2(wcstring *s, FILE *f)
{
int i=0;
wint_t c;
while (1)
{
errno=0;
c = getwc(f);
if (errno == EILSEQ || errno == EINTR)
{
continue;
}
switch (c)
{
/* End of line */
case WEOF:
case L'\n':
case L'\0':
return i;
/* Ignore carriage returns */
case L'\r':
break;
default:
i++;
s->push_back((wchar_t)c);
break;
}
}
}
/**
Converts the narrow character string \c in into its wide
equivalent, and return it
The string may contain embedded nulls.
This function encodes illegal character sequences in a reversible
way using the private use area.
*/
static wcstring str2wcs_internal(const char *in, const size_t in_len)
{
if (in_len == 0)
return wcstring();
assert(in != NULL);
wcstring result;
result.reserve(in_len);
mbstate_t state = {};
size_t in_pos = 0;
while (in_pos < in_len)
{
wchar_t wc = 0;
size_t ret = mbrtowc(&wc, &in[in_pos], in_len-in_pos, &state);
/* Determine whether to encode this characters with our crazy scheme */
bool use_encode_direct = false;
if (wc >= ENCODE_DIRECT_BASE && wc < ENCODE_DIRECT_BASE+256)
{
use_encode_direct = true;
}
else if (wc == INTERNAL_SEPARATOR)
{
use_encode_direct = true;
}
else if (ret == (size_t)(-2))
{
/* Incomplete sequence */
use_encode_direct = true;
}
else if (ret == (size_t)(-1))
{
/* Invalid data */
use_encode_direct = true;
}
else if (ret > in_len - in_pos)
{
/* Other error codes? Terrifying, should never happen */
use_encode_direct = true;
}
if (use_encode_direct)
{
wc = ENCODE_DIRECT_BASE + (unsigned char)in[in_pos];
result.push_back(wc);
in_pos++;
bzero(&state, sizeof state);
}
else if (ret == 0)
{
/* Embedded null byte! */
result.push_back(L'\0');
in_pos++;
bzero(&state, sizeof state);
}
else
{
/* Normal case */
result.push_back(wc);
in_pos += ret;
}
}
return result;
}
wcstring str2wcstring(const char *in, size_t len)
{
return str2wcs_internal(in, len);
}
wcstring str2wcstring(const char *in)
{
return str2wcs_internal(in, strlen(in));
}
wcstring str2wcstring(const std::string &in)
{
/* Handles embedded nulls! */
return str2wcs_internal(in.data(), in.size());
}
char *wcs2str(const wchar_t *in)
{
if (! in)
return NULL;
char *out;
size_t desired_size = MAX_UTF8_BYTES*wcslen(in)+1;
char local_buff[512];
if (desired_size <= sizeof local_buff / sizeof *local_buff)
{
// convert into local buff, then use strdup() so we don't waste malloc'd space
char *result = wcs2str_internal(in, local_buff);
if (result)
{
// It converted into the local buffer, so copy it
result = strdup(result);
if (! result)
{
DIE_MEM();
}
}
return result;
}
else
{
// here we fall into the bad case of allocating a buffer probably much larger than necessary
out = (char *)malloc(MAX_UTF8_BYTES*wcslen(in)+1);
if (!out)
{
DIE_MEM();
}
return wcs2str_internal(in, out);
}
return wcs2str_internal(in, out);
}
char *wcs2str(const wcstring &in)
{
return wcs2str(in.c_str());
}
/* This function is distinguished from wcs2str_internal in that it allows embedded null bytes */
std::string wcs2string(const wcstring &input)
{
std::string result;
result.reserve(input.size());
mbstate_t state;
memset(&state, 0, sizeof(state));
char converted[MB_LEN_MAX + 1];
for (size_t i=0; i < input.size(); i++)
{
wchar_t wc = input[i];
if (wc == INTERNAL_SEPARATOR)
{
}
else if ((wc >= ENCODE_DIRECT_BASE) &&
(wc < ENCODE_DIRECT_BASE+256))
{
result.push_back(wc - ENCODE_DIRECT_BASE);
}
else
{
bzero(converted, sizeof converted);
size_t len = wcrtomb(converted, wc, &state);
if (len == (size_t)(-1))
{
debug(1, L"Wide character %d has no narrow representation", wc);
memset(&state, 0, sizeof(state));
}
else
{
result.append(converted, len);
}
}
}
return result;
}
/**
Converts the wide character string \c in into it's narrow
equivalent, stored in \c out. \c out must have enough space to fit
the entire string.
This function decodes illegal character sequences in a reversible
way using the private use area.
*/
static char *wcs2str_internal(const wchar_t *in, char *out)
{
size_t res=0;
size_t in_pos=0;
size_t out_pos = 0;
mbstate_t state;
CHECK(in, 0);
CHECK(out, 0);
memset(&state, 0, sizeof(state));
while (in[in_pos])
{
if (in[in_pos] == INTERNAL_SEPARATOR)
{
}
else if ((in[in_pos] >= ENCODE_DIRECT_BASE) &&
(in[in_pos] < ENCODE_DIRECT_BASE+256))
{
out[out_pos++] = in[in_pos]- ENCODE_DIRECT_BASE;
}
else
{
res = wcrtomb(&out[out_pos], in[in_pos], &state);
if (res == (size_t)(-1))
{
debug(1, L"Wide character %d has no narrow representation", in[in_pos]);
memset(&state, 0, sizeof(state));
}
else
{
out_pos += res;
}
}
in_pos++;
}
out[out_pos] = 0;
return out;
}
char **wcsv2strv(const wchar_t * const *in)
{
size_t i, count = 0;
while (in[count] != 0)
count++;
char **res = (char **)malloc(sizeof(char *)*(count+1));
if (res == 0)
{
DIE_MEM();
}
for (i=0; i<count; i++)
{
res[i]=wcs2str(in[i]);
}
res[count]=0;
return res;
}
wcstring format_string(const wchar_t *format, ...)
{
va_list va;
va_start(va, format);
wcstring result = vformat_string(format, va);
va_end(va);
return result;
}
wcstring vformat_string(const wchar_t *format, va_list va_orig)
{
const int saved_err = errno;
/*
As far as I know, there is no way to check if a
vswprintf-call failed because of a badly formated string
option or because the supplied destination string was to
small. In GLIBC, errno seems to be set to EINVAL either way.
Because of this, on failiure we try to
increase the buffer size until the free space is
larger than max_size, at which point it will
conclude that the error was probably due to a badly
formated string option, and return an error. Make
sure to null terminate string before that, though.
*/
const size_t max_size = (128*1024*1024);
wchar_t static_buff[256];
size_t size = 0;
wchar_t *buff = NULL;
int status = -1;
while (status < 0)
{
/* Reallocate if necessary */
if (size == 0)
{
buff = static_buff;
size = sizeof static_buff;
}
else
{
size *= 2;
if (size >= max_size)
{
buff[0] = '\0';
break;
}
buff = (wchar_t *)realloc((buff == static_buff ? NULL : buff), size);
if (buff == NULL)
{
DIE_MEM();
}
}
/* Try printing */
va_list va;
va_copy(va, va_orig);
status = vswprintf(buff, size / sizeof(wchar_t), format, va);
va_end(va);
}
wcstring result = wcstring(buff);
if (buff != static_buff)
free(buff);
errno = saved_err;
return result;
}
void append_formatv(wcstring &str, const wchar_t *format, va_list ap)
{
/* Preserve errno across this call since it likes to stomp on it */
int err = errno;
str.append(vformat_string(format, ap));
errno = err;
}
void append_format(wcstring &str, const wchar_t *format, ...)
{
va_list va;
va_start(va, format);
append_formatv(str, format, va);
va_end(va);
}
wchar_t *wcsvarname(const wchar_t *str)
{
while (*str)
{
if ((!iswalnum(*str)) && (*str != L'_'))
{
return (wchar_t *)str;
}
str++;
}
return 0;
}
const wchar_t *wcsfuncname(const wchar_t *str)
{
return wcschr(str, L'/');
}
int wcsvarchr(wchar_t chr)
{
return iswalnum(chr) || chr == L'_';
}
/**
The glibc version of wcswidth seems to hang on some strings. fish uses this replacement.
*/
int my_wcswidth(const wchar_t *c)
{
return fish_wcswidth(c, wcslen(c));
}
wchar_t *quote_end(const wchar_t *pos)
{
wchar_t c = *pos;
while (1)
{
pos++;
if (!*pos)
return 0;
if (*pos == L'\\')
{
pos++;
if (!*pos)
return 0;
}
else
{
if (*pos == c)
{
return (wchar_t *)pos;
}
}
}
return 0;
}
wcstring wsetlocale(int category, const wchar_t *locale)
{
char *lang = locale ? wcs2str(locale) : NULL;
char *res = setlocale(category, lang);
free(lang);
/*
Use ellipsis if on known unicode system, otherwise use $
*/
char *ctype = setlocale(LC_CTYPE, NULL);
bool unicode = (strstr(ctype, ".UTF") || strstr(ctype, ".utf"));
ellipsis_char = unicode ? L'\x2026' : L'$';
// U+23CE is the "return" character
omitted_newline_char = unicode ? L'\x23CE' : L'~';
if (!res)
return wcstring();
else
return format_string(L"%s", res);
}
bool contains_internal(const wchar_t *a, ...)
{
const wchar_t *arg;
va_list va;
bool res = false;
CHECK(a, 0);
va_start(va, a);
while ((arg=va_arg(va, const wchar_t *))!= 0)
{
if (wcscmp(a,arg) == 0)
{
res = true;
break;
}
}
va_end(va);
return res;
}
/* wcstring variant of contains_internal. The first parameter is a wcstring, the rest are const wchar_t* */
__sentinel bool contains_internal(const wcstring &needle, ...)
{
const wchar_t *arg;
va_list va;
int res = 0;
va_start(va, needle);
while ((arg=va_arg(va, const wchar_t *))!= 0)
{
if (needle == arg)
{
res=1;
break;
}
}
va_end(va);
return res;
}
long read_blocked(int fd, void *buf, size_t count)
{
ssize_t res;
sigset_t chldset, oldset;
sigemptyset(&chldset);
sigaddset(&chldset, SIGCHLD);
VOMIT_ON_FAILURE(pthread_sigmask(SIG_BLOCK, &chldset, &oldset));
res = read(fd, buf, count);
VOMIT_ON_FAILURE(pthread_sigmask(SIG_SETMASK, &oldset, NULL));
return res;
}
ssize_t write_loop(int fd, const char *buff, size_t count)
{
size_t out_cum=0;
while (out_cum < count)
{
ssize_t out = write(fd, &buff[out_cum], count - out_cum);
if (out < 0)
{
if (errno != EAGAIN && errno != EINTR)
{
return -1;
}
}
else
{
out_cum += (size_t)out;
}
}
return (ssize_t)out_cum;
}
ssize_t read_loop(int fd, void *buff, size_t count)
{
ssize_t result;
do
{
result = read(fd, buff, count);
}
while (result < 0 && (errno == EAGAIN || errno == EINTR));
return result;
}
static bool should_debug(int level)
{
if (level > debug_level)
return false;
/* Hack to not print error messages in the tests */
if (program_name && ! wcscmp(program_name, L"(ignore)"))
return false;
return true;
}
static void debug_shared(const wcstring &msg)
{
const wcstring sb = wcstring(program_name) + L": " + msg;
wcstring sb2;
write_screen(sb, sb2);
fwprintf(stderr, L"%ls", sb2.c_str());
}
void debug(int level, const wchar_t *msg, ...)
{
if (! should_debug(level))
return;
int errno_old = errno;
va_list va;
va_start(va, msg);
wcstring local_msg = vformat_string(msg, va);
va_end(va);
debug_shared(local_msg);
errno = errno_old;
}
void debug(int level, const char *msg, ...)
{
if (! should_debug(level))
return;
int errno_old = errno;
char local_msg[512];
va_list va;
va_start(va, msg);
vsnprintf(local_msg, sizeof local_msg, msg, va);
va_end(va);
debug_shared(str2wcstring(local_msg));
errno = errno_old;
}
void debug_safe(int level, const char *msg, const char *param1, const char *param2, const char *param3, const char *param4, const char *param5, const char *param6, const char *param7, const char *param8, const char *param9, const char *param10, const char *param11, const char *param12)
{
const char * const params[] = {param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12};
if (! msg)
return;
/* Can't call printf, that may allocate memory Just call write() over and over. */
if (level > debug_level)
return;
int errno_old = errno;
size_t param_idx = 0;
const char *cursor = msg;
while (*cursor != '\0')
{
const char *end = strchr(cursor, '%');
if (end == NULL)
end = cursor + strlen(cursor);
write(STDERR_FILENO, cursor, end - cursor);
if (end[0] == '%' && end[1] == 's')
{
/* Handle a format string */
assert(param_idx < sizeof params / sizeof *params);
const char *format = params[param_idx++];
if (! format)
format = "(null)";
write(STDERR_FILENO, format, strlen(format));
cursor = end + 2;
}
else if (end[0] == '\0')
{
/* Must be at the end of the string */
cursor = end;
}
else
{
/* Some other format specifier, just skip it */
cursor = end + 1;
}
}
// We always append a newline
write(STDERR_FILENO, "\n", 1);
errno = errno_old;
}
void format_long_safe(char buff[128], long val)
{
if (val == 0)
{
strcpy(buff, "0");
}
else
{
/* Generate the string in reverse */
size_t idx = 0;
bool negative = (val < 0);
/* Note that we can't just negate val if it's negative, because it may be the most negative value. We do rely on round-towards-zero division though. */
while (val != 0)
{
long rem = val % 10;
buff[idx++] = '0' + (rem < 0 ? -rem : rem);
val /= 10;
}
if (negative)
buff[idx++] = '-';
buff[idx] = 0;
size_t left = 0, right = idx - 1;
while (left < right)
{
char tmp = buff[left];
buff[left++] = buff[right];
buff[right--] = tmp;
}
}
}
void format_long_safe(wchar_t buff[128], long val)
{
if (val == 0)
{
wcscpy(buff, L"0");
}
else
{
/* Generate the string in reverse */
size_t idx = 0;
bool negative = (val < 0);
while (val > 0)
{
long rem = val % 10;
/* Here we're assuming that wide character digits are contiguous - is that a correct assumption? */
buff[idx++] = L'0' + (wchar_t)(rem < 0 ? -rem : rem);
val /= 10;
}
if (negative)
buff[idx++] = L'-';
buff[idx] = 0;
size_t left = 0, right = idx - 1;
while (left < right)
{
wchar_t tmp = buff[left];
buff[left++] = buff[right];
buff[right--] = tmp;
}
}
}
void write_screen(const wcstring &msg, wcstring &buff)
{
const wchar_t *start, *pos;
int line_width = 0;
int tok_width = 0;
int screen_width = common_get_width();
if (screen_width)
{
start = pos = msg.c_str();
while (1)
{
int overflow = 0;
tok_width=0;
/*
Tokenize on whitespace, and also calculate the width of the token
*/
while (*pos && (!wcschr(L" \n\r\t", *pos)))
{
/*
Check is token is wider than one line.
If so we mark it as an overflow and break the token.
*/
if ((tok_width + fish_wcwidth(*pos)) > (screen_width-1))
{
overflow = 1;
break;
}
tok_width += fish_wcwidth(*pos);
pos++;
}
/*
If token is zero character long, we don't do anything
*/
if (pos == start)
{
start = pos = pos+1;
}
else if (overflow)
{
/*
In case of overflow, we print a newline, except if we already are at position 0
*/
wchar_t *token = wcsndup(start, pos-start);
if (line_width != 0)
buff.push_back(L'\n');
buff.append(format_string(L"%ls-\n", token));
free(token);
line_width=0;
}
else
{
/*
Print the token
*/
wchar_t *token = wcsndup(start, pos-start);
if ((line_width + (line_width!=0?1:0) + tok_width) > screen_width)
{
buff.push_back(L'\n');
line_width=0;
}
buff.append(format_string(L"%ls%ls", line_width?L" ":L"", token));
free(token);
line_width += (line_width!=0?1:0) + tok_width;
}
/*
Break on end of string
*/
if (!*pos)
{
break;
}
start=pos;
}
}
else
{
buff.append(msg);
}
buff.push_back(L'\n');
}
/**
Perform string escaping of a strinng by only quoting it. Assumes
the string has already been checked for characters that can not be
escaped this way.
*/
static wchar_t *escape_simple(const wchar_t *in)
{
wchar_t *out;
size_t len = wcslen(in);
out = (wchar_t *)malloc(sizeof(wchar_t)*(len+3));
if (!out)
DIE_MEM();
out[0] = L'\'';
wcscpy(&out[1], in);
out[len+1]=L'\'';
out[len+2]=0;
return out;
}
wchar_t *escape(const wchar_t *in_orig, escape_flags_t flags)
{
const wchar_t *in = in_orig;
bool escape_all = !!(flags & ESCAPE_ALL);
bool no_quoted = !!(flags & ESCAPE_NO_QUOTED);
bool no_tilde = !!(flags & ESCAPE_NO_TILDE);
wchar_t *out;
wchar_t *pos;
int need_escape=0;
int need_complex_escape=0;
if (!in)
{
debug(0, L"%s called with null input", __func__);
FATAL_EXIT();
}
if (!no_quoted && (wcslen(in) == 0))
{
out = wcsdup(L"''");
if (!out)
DIE_MEM();
return out;
}
out = (wchar_t *)malloc(sizeof(wchar_t)*(wcslen(in)*4 + 1));
pos = out;
if (!out)
DIE_MEM();
while (*in != 0)
{
if ((*in >= ENCODE_DIRECT_BASE) &&
(*in < ENCODE_DIRECT_BASE+256))
{
int val = *in - ENCODE_DIRECT_BASE;
int tmp;
*(pos++) = L'\\';
*(pos++) = L'X';
tmp = val/16;
*pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp;
tmp = val%16;
*pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp;
need_escape=need_complex_escape=1;
}
else
{
wchar_t c = *in;
switch (c)
{
case L'\t':
*(pos++) = L'\\';