1 /*
2 ** print.c - Kernel.#p
3 **
4 ** See Copyright Notice in mruby.h
5 */
6 
7 #include <mruby.h>
8 #include <mruby/string.h>
9 #include <mruby/variable.h>
10 
11 #ifndef MRB_DISABLE_STDIO
12 static void
printstr(mrb_value obj,FILE * stream)13 printstr(mrb_value obj, FILE *stream)
14 {
15   if (mrb_string_p(obj)) {
16     fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stream);
17     putc('\n', stream);
18   }
19 }
20 #else
21 # define printstr(obj, stream) (void)0
22 #endif
23 
24 MRB_API void
mrb_p(mrb_state * mrb,mrb_value obj)25 mrb_p(mrb_state *mrb, mrb_value obj)
26 {
27   printstr(mrb_inspect(mrb, obj), stdout);
28 }
29 
30 MRB_API void
mrb_print_error(mrb_state * mrb)31 mrb_print_error(mrb_state *mrb)
32 {
33   mrb_print_backtrace(mrb);
34   printstr(mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0), stderr);
35 }
36 
37 MRB_API void
mrb_show_version(mrb_state * mrb)38 mrb_show_version(mrb_state *mrb)
39 {
40   printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_DESCRIPTION")), stdout);
41 }
42 
43 MRB_API void
mrb_show_copyright(mrb_state * mrb)44 mrb_show_copyright(mrb_state *mrb)
45 {
46   printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_COPYRIGHT")), stdout);
47 }
48