Debian Codegraph @

vendor/mruby/mrbgems/mruby-errno/src/errno.c

raw
1 #include "mruby.h"
2 #include "mruby/array.h"
3 #include "mruby/class.h"
4 #include "mruby/hash.h"
5 #include "mruby/numeric.h"
6 #include "mruby/string.h"
7 #include "mruby/variable.h"
8 #include <errno.h>
9 #include <stdio.h>
10 #include <string.h>
11
12 #if MRUBY_RELEASE_NO < 10000
13 static struct RClass *
14 mrb_module_get(mrb_state *mrb, const char *name)
15 {
16 return mrb_class_get(mrb, name);
17 }
18 #endif
19
20 static mrb_value
21 mrb_sce_init(mrb_state *mrb, mrb_value self)
22 {
23 mrb_value c, e2c, m, str;
24 mrb_int n;
25 int argc, no_errno = 0;
26 char buf[20];
27
28 argc = mrb_get_args(mrb, "o|i", &m, &n);
29 if (argc == 1) {
30 if (mrb_type(m) == MRB_TT_FIXNUM) {
31 n = mrb_fixnum(m);
32 m = mrb_nil_value();
33 } else {
34 no_errno = 1;
35 }
36 }
37 if (!no_errno) {
38 e2c = mrb_const_get(mrb, mrb_obj_value(mrb_module_get(mrb, "Errno")), mrb_intern_lit(mrb, "Errno2class"));
39 c = mrb_hash_fetch(mrb, e2c, mrb_fixnum_value(n), mrb_nil_value());
40 if (!mrb_nil_p(c)) {
41 mrb_basic_ptr(self)->c = mrb_class_ptr(c);
42 str = mrb_str_new_cstr(mrb, strerror(n));
43 } else {
44 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "errno"), mrb_fixnum_value(n));
45 str = mrb_str_new_cstr(mrb, "Unknown error: ");
46 snprintf(buf, sizeof(buf), "%d", (int)n);
47 mrb_str_cat2(mrb, str, buf);
48 }
49 } else {
50 str = mrb_str_new_cstr(mrb, "unknown error");
51 }
52 if (!mrb_nil_p(m)) {
53 mrb_str_cat2(mrb, str, " - ");
54 mrb_str_append(mrb, str, m);
55 }
56 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "mesg"), str);
57 return self;
58 }
59
60 static mrb_value
61 mrb_sce_errno(mrb_state *mrb, mrb_value self)
62 {
63 struct RClass *c;
64 mrb_sym sym;
65
66 c = mrb_class(mrb, self);
67 sym = mrb_intern_lit(mrb, "Errno");
68 #if MRUBY_RELEASE_NO < 10000
69 if (mrb_const_defined_at(mrb, c, sym)) {
70 #else
71 if (mrb_const_defined_at(mrb, mrb_obj_value(c), sym)) {
72 #endif
73 return mrb_const_get(mrb, mrb_obj_value(c), sym);
74 } else {
75 sym = mrb_intern_lit(mrb, "errno");
76 return mrb_attr_get(mrb, self, sym);
77 }
78 }
79
80 static mrb_value
81 mrb_sce_to_s(mrb_state *mrb, mrb_value self)
82 {
83 return mrb_attr_get(mrb, self, mrb_intern_lit(mrb, "mesg"));
84 }
85
86 static mrb_value
87 mrb_sce_sys_fail(mrb_state *mrb, mrb_value cls)
88 {
89 struct RClass *cl, *sce;
90 mrb_value e, msg;
91 mrb_int no;
92 int argc;
93 char name[8];
94
95 sce = mrb_class_get(mrb, "SystemCallError");
96 argc = mrb_get_args(mrb, "i|S", &no, &msg);
97 if (argc == 1) {
98 e = mrb_funcall(mrb, mrb_obj_value(sce), "new", 1, mrb_fixnum_value(no));
99 } else {
100 e = mrb_funcall(mrb, mrb_obj_value(sce), "new", 2, msg, mrb_fixnum_value(no));
101 }
102 if (mrb_obj_class(mrb, e) == sce) {
103 snprintf(name, sizeof(name), "E%03ld", (long)no);
104 cl = mrb_define_class_under(mrb, mrb_module_get(mrb, "Errno"), name, sce);
105 mrb_define_const(mrb, cl, "Errno", mrb_fixnum_value(no));
106 mrb_basic_ptr(e)->c = cl;
107 }
108 mrb_exc_raise(mrb, e);
109 return mrb_nil_value(); /* NOTREACHED */
110 }
111
112 static mrb_value
113 mrb_exxx_init(mrb_state *mrb, mrb_value self)
114 {
115 mrb_value m, no, str;
116
117 no = mrb_const_get(mrb, mrb_obj_value(mrb_class(mrb, self)), mrb_intern_lit(mrb, "Errno"));
118 str = mrb_str_new_cstr(mrb, strerror(mrb_fixnum(no)));
119
120 m = mrb_nil_value();
121 mrb_get_args(mrb, "|S", &m);
122 if (!mrb_nil_p(m)) {
123 mrb_str_cat2(mrb, str, " - ");
124 mrb_str_append(mrb, str, m);
125 }
126 mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "mesg"), str);
127 return self;
128 }
129
130 void
131 mrb_mruby_errno_gem_init(mrb_state *mrb)
132 {
133 struct RClass *e, *eno, *sce, *ste;
134 mrb_value h, noerror;
135
136 ste = mrb_class_get(mrb, "StandardError");
137
138 sce = mrb_define_class(mrb, "SystemCallError", ste);
139 mrb_define_class_method(mrb, sce, "_sys_fail", mrb_sce_sys_fail, MRB_ARGS_REQ(1));
140 mrb_define_method(mrb, sce, "errno", mrb_sce_errno, MRB_ARGS_NONE());
141 mrb_define_method(mrb, sce, "to_s", mrb_sce_to_s, MRB_ARGS_NONE());
142 mrb_define_method(mrb, sce, "initialize", mrb_sce_init, MRB_ARGS_ARG(1, 1));
143
144 eno = mrb_define_module(mrb, "Errno");
145 h = mrb_hash_new(mrb);
146 mrb_define_const(mrb, eno, "Errno2class", h);
147
148 e = mrb_define_class_under(mrb, eno, "NOERROR", sce);
149 mrb_define_const(mrb, e, "Errno", mrb_fixnum_value(0));
150 mrb_define_method(mrb, e, "initialize", mrb_exxx_init, MRB_ARGS_OPT(1));
151 //mrb_define_method(mrb, e, "===", mrb_exxx_cmp, MRB_ARGS_REQ(1));
152 noerror = mrb_obj_value(e);
153
154 #define itsdefined(SYM) \
155 do { \
156 int ai = mrb_gc_arena_save(mrb); \
157 e = mrb_define_class_under(mrb, eno, #SYM, sce); \
158 mrb_define_const(mrb, e, "Errno", mrb_fixnum_value(SYM)); \
159 mrb_define_method(mrb, e, "initialize", mrb_exxx_init, MRB_ARGS_OPT(1)); \
160 mrb_hash_set(mrb, h, mrb_fixnum_value(SYM), mrb_obj_value(e)); \
161 mrb_gc_arena_restore(mrb, ai); \
162 } while (0)
163
164 #define itsnotdefined(SYM) \
165 do { \
166 mrb_define_const(mrb, eno, #SYM, noerror); \
167 } while (0)
168
169 #include "known_errors_def.cstub"
170 }
171
172 void
173 mrb_mruby_errno_gem_final(mrb_state *mrb)
174 {
175 }