Debian Codegraph @

src/modules/frontend/corba/corba.c

raw
1 /*
2 * cdebconf frontend, corba client
3 */
4
5 #include "common.h"
6 #include "template.h"
7 #include "question.h"
8 #include "frontend.h"
9 #include "database.h"
10 #include "strutl.h"
11
12 #include <stdlib.h>
13 #include <ctype.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <termios.h>
18 #include <unistd.h>
19 #include <sys/ioctl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22
23 #include <ORBitservices/CosNaming.h>
24
25 #include "dcf-common.c" /* Yeah, stupid and wrong. :P */
26 #include "dcf-stubs.c"
27
28 #define EXCEPTION(ev) \
29 if ((ev)->_major != CORBA_NO_EXCEPTION) { \
30 if ((ev)->_major == CORBA_USER_EXCEPTION) { \
31 CORBA_char *buffer; \
32 buffer = CORBA_exception_id(ev); \
33 if (strcmp (buffer, "Debconf_Frontend_Failure")) { \
34 fprintf(stderr, "dcf_corba: user exception: %s\n", \
35 ((Debconf_Failure *) CORBA_exception_value(ev))->reason); \
36 return DC_NOTOK; \
37 } else { \
38 fprintf(stderr, "dcf_corba: unknown exception raised!!\n"); \
39 return DC_NOTOK; \
40 } \
41 } else { \
42 fprintf(stderr, "dcf_corba: System exception: %s\n", \
43 CORBA_exception_id(ev)); \
44 return DC_NOTOK; \
45 } \
46 }
47
48 #define UIDATA(f) ((struct uidata *)(f)->data)
49
50 /* Private variables */
51 struct uidata {
52 CORBA_Environment *ev;
53 CORBA_Object *serv;
54 };
55
56 static int corba_boolean(struct frontend *f, struct question *q) {
57 struct uidata *uid = UIDATA(f);
58 CORBA_Environment *ev = uid->ev;
59
60 CORBA_boolean ans = Debconf_Frontend_Boolean(*uid->serv, "yes/no", ev);
61 EXCEPTION(ev);
62
63 return DC_OK;
64 }
65
66 static int corba_multiselect(struct frontend *f, struct question *q) {
67 struct uidata *uid = UIDATA(f);
68 CORBA_Environment *ev = uid->ev;
69
70 CORBA_boolean ans = Debconf_Frontend_Multiselect(*uid->serv, "yes/no", ev);
71 EXCEPTION(ev);
72
73 return DC_OK;
74 }
75
76 static int corba_note(struct frontend *f, struct question *q) {
77 struct uidata *uid = UIDATA(f);
78 CORBA_Environment *ev = uid->ev;
79
80 Debconf_Frontend_Note(*uid->serv, "yes/no", ev);
81 EXCEPTION(ev);
82
83 return DC_OK;
84 }
85
86 static int corba_password(struct frontend *f, struct question *q) {
87 struct uidata *uid = UIDATA(f);
88 CORBA_Environment *ev = uid->ev;
89
90 CORBA_boolean ans = Debconf_Frontend_Password(*uid->serv, "yes/no", ev);
91 EXCEPTION(ev);
92
93 return DC_OK;
94 }
95
96 static int corba_select(struct frontend *f, struct question *q) {
97 struct uidata *uid = UIDATA(f);
98 CORBA_Environment *ev = uid->ev;
99
100 CORBA_boolean ans = Debconf_Frontend_Select(*uid->serv, "yes/no", ev);
101 EXCEPTION(ev);
102
103 return DC_OK;
104 }
105
106 static int corba_string(struct frontend *f, struct question *q) {
107 struct uidata *uid = UIDATA(f);
108 CORBA_Environment *ev = uid->ev;
109
110 CORBA_boolean ans = Debconf_Frontend_String(*uid->serv, "yes/no", ev);
111 EXCEPTION(ev);
112
113 return DC_OK;
114 }
115
116 static int corba_text(struct frontend *f, struct question *q) {
117 struct uidata *uid = UIDATA(f);
118 CORBA_Environment *ev = uid->ev;
119
120 CORBA_char *ans = Debconf_Frontend_Text(*uid->serv, "Enter some text: ", ev);
121 EXCEPTION(ev);
122
123 question_setvalue(q, (char *) ans);
124 CORBA_free(ans);
125 return DC_OK;
126 }
127
128 /******************************************************************************/
129
130 static struct question_handlers {
131 const char *type;
132 int (*handler)(struct frontend *f, struct question *q);
133 } question_handlers[] = {
134 { "boolean", corba_boolean },
135 { "multiselect", corba_multiselect },
136 { "note", corba_note },
137 { "password", corba_password },
138 { "select", corba_select },
139 { "string", corba_string },
140 { "text", corba_text },
141 { "error", corba_note },
142 };
143
144 int corba_initialize(struct frontend *f, struct configuration *conf)
145 {
146 struct uidata *uid;
147 CORBA_ORB orb;
148 CORBA_Environment *ev;
149 CORBA_Object *serv;
150 CosNaming_NamingContext root;
151 CosNaming_NameComponent name_component[2] = {{"Debconf", "subcontext"},
152 {"Frontend", "server"}};
153 CosNaming_Name name = {2, 2, name_component, CORBA_FALSE};
154 gchar *dummy_argv[2];
155 gint dummy_argc;
156
157 dummy_argc = 1;
158 dummy_argv[0] = argv[0];
159 dummy_argv[1] = 0;
160
161 uid = (struct uidata *) malloc(sizeof(struct uidata));
162 if (! uid)
163 return DC_NOTOK;
164 memset(uid, 0, sizeof(struct uidata));
165 ev = uid->ev = (CORBA_Environment *) malloc(sizeof(CORBA_Environment));
166 if (! uid->ev)
167 return DC_NOTOK;
168 serv = uid->serv = (CORBA_Object *) malloc(sizeof(CORBA_Object));
169 if (! uid->serv)
170 return DC_NOTOK;
171
172 f->interactive = 1;
173
174 CORBA_exception_init(ev);
175 orb = CORBA_ORB_init(&dummy_argc, dummy_argv, "orbit-local-orb", ev);
176 if (ev->_major != CORBA_NO_EXCEPTION) {
177 fprintf(stderr, "Error: unable to initialise the ORB: %s\n",
178 CORBA_exception_id(ev));
179 CORBA_exception_free(ev);
180 free(ev);
181 free(serv);
182 free(uid);
183 return DC_NOTOK;
184 }
185
186 root = CORBA_ORB_resolve_initial_service(orb, "NameService", ev);
187 if (ev->_major != CORBA_NO_EXCEPTION) {
188 fprintf(stderr, "Error: could not get name service: %s\n",
189 CORBA_exception_id(&ev));
190 exit(1);
191 }
192
193 *serv = CosNaming_NamingContext_resolve(root, &name, ev);
194 if (ev->_major != CORBA_NO_EXCEPTION) {
195 fprintf(stderr, "Error: could resolve object: %s\n",
196 CORBA_exception_id(&ev));
197 exit(1);
198 }
199
200 f->data = uid;
201
202 /* Or shall I put it in uidata to free it later? */
203 CORBA_free (root);
204 return DC_OK;
205 }
206
207 static int corba_shutdown(struct frontend *f) {
208 struct uidata *uid = UIDATA(f);
209 CORBA_Object_release(&uid->serv, uid->ev);
210 CORBA_exception_free(uid->ev);
211 free(uid->ev);
212 free(uid->serv);
213 free(uid);
214
215 return DC_OK;
216 }
217
218 static int corba_go(struct frontend *f) {
219 struct uidata *uid = UIDATA(f);
220 struct question *q = f->questions;
221 int i;
222 int ret;
223
224 printf("%s\n\n", f->title);
225
226 for (; q != 0; q = q->next) {
227 for (i = 0; i < DIM(question_handlers); i++)
228 if (strcmp(q->template->type, question_handlers[i].type) == 0) {
229 corba_displaydesc(f, q);
230 ret = question_handlers[i].handler(f, q);
231 if (ret == DC_OK)
232 frontend_qdb_set(f->qdb, q, 0);
233 else
234 return ret;
235 break;
236 }
237 if (i == DIM(question_handlers))
238 return DC_NOTIMPL;
239 }
240
241 return DC_OK;
242 }
243
244 struct frontend_module debconf_frontend_module = {
245 initialize: corba_initialize,
246 shutdown: corba_shutdown,
247 go: corba_go,
248 };