Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-print / src / print.c
1 #include <mruby.h>
2 #include <mruby/string.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #if defined(_WIN32)
7 # include <windows.h>
8 # include <io.h>
9 #ifdef _MSC_VER
10 # define isatty(x) _isatty(x)
11 # define fileno(x) _fileno(x)
12 #endif
13 #endif
14
15 static void
16 printstr(mrb_state *mrb, mrb_value obj)
17 {
18   if (mrb_string_p(obj)) {
19 #if defined(_WIN32)
20     if (isatty(fileno(stdout))) {
21       DWORD written;
22       int mlen = (int)RSTRING_LEN(obj);
23       char* utf8 = RSTRING_PTR(obj);
24       int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, NULL, 0);
25       wchar_t* utf16 = (wchar_t*)mrb_malloc(mrb, (wlen+1) * sizeof(wchar_t));
26       if (MultiByteToWideChar(CP_UTF8, 0, utf8, mlen, utf16, wlen) > 0) {
27         utf16[wlen] = 0;
28         WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE),
29           utf16, wlen, &written, NULL);
30       }
31       mrb_free(mrb, utf16);
32     } else
33 #endif
34       fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
35     fflush(stdout);
36   }
37 }
38
39 /* 15.3.1.2.9  */
40 /* 15.3.1.3.34 */
41 mrb_value
42 mrb_printstr(mrb_state *mrb, mrb_value self)
43 {
44   mrb_value argv;
45
46   mrb_get_args(mrb, "o", &argv);
47   printstr(mrb, argv);
48
49   return argv;
50 }
51
52 void
53 mrb_mruby_print_gem_init(mrb_state* mrb)
54 {
55   struct RClass *krn;
56   krn = mrb->kernel_module;
57   mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, MRB_ARGS_REQ(1));
58 }
59
60 void
61 mrb_mruby_print_gem_final(mrb_state* mrb)
62 {
63 }