Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libevent / dist / test / regress.gen.c
blob9da8b2ec049a243de34c0dc31d158c8b596cd22d
1 /* $NetBSD$ */
2 /*
3 * Automatically generated from ./regress.rpc
4 * by event_rpcgen.py/0.1. DO NOT EDIT THIS FILE.
5 */
7 #include <sys/types.h>
8 #include <sys/time.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <event.h>
15 #include "./regress.gen.h"
17 void event_err(int eval, const char *fmt, ...);
18 void event_warn(const char *fmt, ...);
19 void event_errx(int eval, const char *fmt, ...);
20 void event_warnx(const char *fmt, ...);
24 * Implementation of msg
27 static struct msg_access_ __msg_base = {
28 msg_from_name_assign,
29 msg_from_name_get,
30 msg_to_name_assign,
31 msg_to_name_get,
32 msg_attack_assign,
33 msg_attack_get,
34 msg_run_assign,
35 msg_run_get,
36 msg_run_add,
39 struct msg *
40 msg_new(void)
42 struct msg *tmp;
43 if ((tmp = malloc(sizeof(struct msg))) == NULL) {
44 event_warn("%s: malloc", __func__);
45 return (NULL);
47 tmp->base = &__msg_base;
49 tmp->from_name_data = NULL;
50 tmp->from_name_set = 0;
52 tmp->to_name_data = NULL;
53 tmp->to_name_set = 0;
55 tmp->attack_data = NULL;
56 tmp->attack_set = 0;
58 tmp->run_data = NULL;
59 tmp->run_length = 0;
60 tmp->run_num_allocated = 0;
61 tmp->run_set = 0;
63 return (tmp);
69 struct run *
70 msg_run_add(struct msg *msg)
72 if (++msg->run_length >= msg->run_num_allocated) {
73 int tobe_allocated = msg->run_num_allocated;
74 struct run ** new_data = NULL;
75 tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
76 new_data = (struct run **) realloc(msg->run_data,
77 tobe_allocated * sizeof(struct run *));
78 if (new_data == NULL)
79 goto error;
80 msg->run_data = new_data;
81 msg->run_num_allocated = tobe_allocated;
83 msg->run_data[msg->run_length - 1] = run_new();
84 if (msg->run_data[msg->run_length - 1] == NULL)
85 goto error;
86 msg->run_set = 1;
87 return (msg->run_data[msg->run_length - 1]);
88 error:
89 --msg->run_length;
90 return (NULL);
94 int
95 msg_from_name_assign(struct msg *msg,
96 const char * value)
98 if (msg->from_name_data != NULL)
99 free(msg->from_name_data);
100 if ((msg->from_name_data = strdup(value)) == NULL)
101 return (-1);
102 msg->from_name_set = 1;
103 return (0);
107 msg_to_name_assign(struct msg *msg,
108 const char * value)
110 if (msg->to_name_data != NULL)
111 free(msg->to_name_data);
112 if ((msg->to_name_data = strdup(value)) == NULL)
113 return (-1);
114 msg->to_name_set = 1;
115 return (0);
119 msg_attack_assign(struct msg *msg,
120 const struct kill* value)
122 struct evbuffer *tmp = NULL;
123 if (msg->attack_set) {
124 kill_clear(msg->attack_data);
125 msg->attack_set = 0;
126 } else {
127 msg->attack_data = kill_new();
128 if (msg->attack_data == NULL) {
129 event_warn("%s: kill_new()", __func__);
130 goto error;
133 if ((tmp = evbuffer_new()) == NULL) {
134 event_warn("%s: evbuffer_new()", __func__);
135 goto error;
137 kill_marshal(tmp, value);
138 if (kill_unmarshal(msg->attack_data, tmp) == -1) {
139 event_warnx("%s: kill_unmarshal", __func__);
140 goto error;
142 msg->attack_set = 1;
143 evbuffer_free(tmp);
144 return (0);
145 error:
146 if (tmp != NULL)
147 evbuffer_free(tmp);
148 if (msg->attack_data != NULL) {
149 kill_free(msg->attack_data);
150 msg->attack_data = NULL;
152 return (-1);
156 msg_run_assign(struct msg *msg, int off,
157 const struct run * value)
159 struct evbuffer *tmp = NULL;
160 if (!msg->run_set || off < 0 || off >= msg->run_length)
161 return (-1);
162 run_clear(msg->run_data[off]);
163 if ((tmp = evbuffer_new()) == NULL) {
164 event_warn("%s: evbuffer_new()", __func__);
165 goto error;
167 run_marshal(tmp, value);
168 if (run_unmarshal(msg->run_data[off], tmp) == -1) {
169 event_warnx("%s: run_unmarshal", __func__);
170 goto error;
172 evbuffer_free(tmp);
173 return (0);
174 error:
175 if (tmp != NULL)
176 evbuffer_free(tmp);
177 run_clear(msg->run_data[off]);
178 return (-1);
182 msg_from_name_get(struct msg *msg, char * *value)
184 if (msg->from_name_set != 1)
185 return (-1);
186 *value = msg->from_name_data;
187 return (0);
191 msg_to_name_get(struct msg *msg, char * *value)
193 if (msg->to_name_set != 1)
194 return (-1);
195 *value = msg->to_name_data;
196 return (0);
200 msg_attack_get(struct msg *msg, struct kill* *value)
202 if (msg->attack_set != 1) {
203 msg->attack_data = kill_new();
204 if (msg->attack_data == NULL)
205 return (-1);
206 msg->attack_set = 1;
208 *value = msg->attack_data;
209 return (0);
213 msg_run_get(struct msg *msg, int offset,
214 struct run * *value)
216 if (!msg->run_set || offset < 0 || offset >= msg->run_length)
217 return (-1);
218 *value = msg->run_data[offset];
219 return (0);
222 void
223 msg_clear(struct msg *tmp)
225 if (tmp->from_name_set == 1) {
226 free (tmp->from_name_data);
227 tmp->from_name_data = NULL;
228 tmp->from_name_set = 0;
230 if (tmp->to_name_set == 1) {
231 free (tmp->to_name_data);
232 tmp->to_name_data = NULL;
233 tmp->to_name_set = 0;
235 if (tmp->attack_set == 1) {
236 kill_free(tmp->attack_data);
237 tmp->attack_data = NULL;
238 tmp->attack_set = 0;
240 if (tmp->run_set == 1) {
241 int i;
242 for (i = 0; i < tmp->run_length; ++i) {
243 run_free(tmp->run_data[i]);
245 free(tmp->run_data);
246 tmp->run_data = NULL;
247 tmp->run_set = 0;
248 tmp->run_length = 0;
249 tmp->run_num_allocated = 0;
253 void
254 msg_free(struct msg *tmp)
256 if (tmp->from_name_data != NULL)
257 free (tmp->from_name_data);
258 if (tmp->to_name_data != NULL)
259 free (tmp->to_name_data);
260 if (tmp->attack_data != NULL)
261 kill_free(tmp->attack_data);
262 if (tmp->run_data != NULL) {
263 int i;
264 for (i = 0; i < tmp->run_length; ++i) {
265 run_free(tmp->run_data[i]);
266 tmp->run_data[i] = NULL;
268 free(tmp->run_data);
269 tmp->run_data = NULL;
270 tmp->run_length = 0;
271 tmp->run_num_allocated = 0;
273 free(tmp);
276 void
277 msg_marshal(struct evbuffer *evbuf, const struct msg *tmp){
278 evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
279 evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
280 if (tmp->attack_set) {
281 evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
284 int i;
285 for (i = 0; i < tmp->run_length; ++i) {
286 evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
292 msg_unmarshal(struct msg *tmp, struct evbuffer *evbuf)
294 uint32_t tag;
295 while (EVBUFFER_LENGTH(evbuf) > 0) {
296 if (evtag_peek(evbuf, &tag) == -1)
297 return (-1);
298 switch (tag) {
300 case MSG_FROM_NAME:
302 if (tmp->from_name_set)
303 return (-1);
304 if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
305 event_warnx("%s: failed to unmarshal from_name", __func__);
306 return (-1);
308 tmp->from_name_set = 1;
309 break;
311 case MSG_TO_NAME:
313 if (tmp->to_name_set)
314 return (-1);
315 if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
316 event_warnx("%s: failed to unmarshal to_name", __func__);
317 return (-1);
319 tmp->to_name_set = 1;
320 break;
322 case MSG_ATTACK:
324 if (tmp->attack_set)
325 return (-1);
326 tmp->attack_data = kill_new();
327 if (tmp->attack_data == NULL)
328 return (-1);
329 if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) {
330 event_warnx("%s: failed to unmarshal attack", __func__);
331 return (-1);
333 tmp->attack_set = 1;
334 break;
336 case MSG_RUN:
338 if (msg_run_add(tmp) == NULL)
339 return (-1);
340 if (evtag_unmarshal_run(evbuf, MSG_RUN,
341 tmp->run_data[tmp->run_length - 1]) == -1) {
342 --tmp->run_length;
343 event_warnx("%s: failed to unmarshal run", __func__);
344 return (-1);
346 tmp->run_set = 1;
347 break;
349 default:
350 return -1;
354 if (msg_complete(tmp) == -1)
355 return (-1);
356 return (0);
360 msg_complete(struct msg *msg)
362 if (!msg->from_name_set)
363 return (-1);
364 if (!msg->to_name_set)
365 return (-1);
366 if (msg->attack_set && kill_complete(msg->attack_data) == -1)
367 return (-1);
369 int i;
370 for (i = 0; i < msg->run_length; ++i) {
371 if (run_complete(msg->run_data[i]) == -1)
372 return (-1);
375 return (0);
379 evtag_unmarshal_msg(struct evbuffer *evbuf, uint32_t need_tag, struct msg *msg)
381 uint32_t tag;
382 int res = -1;
384 struct evbuffer *tmp = evbuffer_new();
386 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
387 goto error;
389 if (msg_unmarshal(msg, tmp) == -1)
390 goto error;
392 res = 0;
394 error:
395 evbuffer_free(tmp);
396 return (res);
399 void
400 evtag_marshal_msg(struct evbuffer *evbuf, uint32_t tag, const struct msg *msg)
402 struct evbuffer *_buf = evbuffer_new();
403 assert(_buf != NULL);
404 evbuffer_drain(_buf, -1);
405 msg_marshal(_buf, msg);
406 evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
407 evbuffer_free(_buf);
411 * Implementation of kill
414 static struct kill_access_ __kill_base = {
415 kill_weapon_assign,
416 kill_weapon_get,
417 kill_action_assign,
418 kill_action_get,
419 kill_how_often_assign,
420 kill_how_often_get,
423 struct kill *
424 kill_new(void)
426 struct kill *tmp;
427 if ((tmp = malloc(sizeof(struct kill))) == NULL) {
428 event_warn("%s: malloc", __func__);
429 return (NULL);
431 tmp->base = &__kill_base;
433 tmp->weapon_data = NULL;
434 tmp->weapon_set = 0;
436 tmp->action_data = NULL;
437 tmp->action_set = 0;
439 tmp->how_often_data = 0;
440 tmp->how_often_set = 0;
442 return (tmp);
449 kill_weapon_assign(struct kill *msg,
450 const char * value)
452 if (msg->weapon_data != NULL)
453 free(msg->weapon_data);
454 if ((msg->weapon_data = strdup(value)) == NULL)
455 return (-1);
456 msg->weapon_set = 1;
457 return (0);
461 kill_action_assign(struct kill *msg,
462 const char * value)
464 if (msg->action_data != NULL)
465 free(msg->action_data);
466 if ((msg->action_data = strdup(value)) == NULL)
467 return (-1);
468 msg->action_set = 1;
469 return (0);
473 kill_how_often_assign(struct kill *msg, const uint32_t value)
475 msg->how_often_set = 1;
476 msg->how_often_data = value;
477 return (0);
481 kill_weapon_get(struct kill *msg, char * *value)
483 if (msg->weapon_set != 1)
484 return (-1);
485 *value = msg->weapon_data;
486 return (0);
490 kill_action_get(struct kill *msg, char * *value)
492 if (msg->action_set != 1)
493 return (-1);
494 *value = msg->action_data;
495 return (0);
499 kill_how_often_get(struct kill *msg, uint32_t *value)
501 if (msg->how_often_set != 1)
502 return (-1);
503 *value = msg->how_often_data;
504 return (0);
507 void
508 kill_clear(struct kill *tmp)
510 if (tmp->weapon_set == 1) {
511 free (tmp->weapon_data);
512 tmp->weapon_data = NULL;
513 tmp->weapon_set = 0;
515 if (tmp->action_set == 1) {
516 free (tmp->action_data);
517 tmp->action_data = NULL;
518 tmp->action_set = 0;
520 tmp->how_often_set = 0;
523 void
524 kill_free(struct kill *tmp)
526 if (tmp->weapon_data != NULL)
527 free (tmp->weapon_data);
528 if (tmp->action_data != NULL)
529 free (tmp->action_data);
530 free(tmp);
533 void
534 kill_marshal(struct evbuffer *evbuf, const struct kill *tmp){
535 evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
536 evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
537 if (tmp->how_often_set) {
538 evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data);
543 kill_unmarshal(struct kill *tmp, struct evbuffer *evbuf)
545 uint32_t tag;
546 while (EVBUFFER_LENGTH(evbuf) > 0) {
547 if (evtag_peek(evbuf, &tag) == -1)
548 return (-1);
549 switch (tag) {
551 case KILL_WEAPON:
553 if (tmp->weapon_set)
554 return (-1);
555 if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
556 event_warnx("%s: failed to unmarshal weapon", __func__);
557 return (-1);
559 tmp->weapon_set = 1;
560 break;
562 case KILL_ACTION:
564 if (tmp->action_set)
565 return (-1);
566 if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
567 event_warnx("%s: failed to unmarshal action", __func__);
568 return (-1);
570 tmp->action_set = 1;
571 break;
573 case KILL_HOW_OFTEN:
575 if (tmp->how_often_set)
576 return (-1);
577 if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data) == -1) {
578 event_warnx("%s: failed to unmarshal how_often", __func__);
579 return (-1);
581 tmp->how_often_set = 1;
582 break;
584 default:
585 return -1;
589 if (kill_complete(tmp) == -1)
590 return (-1);
591 return (0);
595 kill_complete(struct kill *msg)
597 if (!msg->weapon_set)
598 return (-1);
599 if (!msg->action_set)
600 return (-1);
601 return (0);
605 evtag_unmarshal_kill(struct evbuffer *evbuf, uint32_t need_tag, struct kill *msg)
607 uint32_t tag;
608 int res = -1;
610 struct evbuffer *tmp = evbuffer_new();
612 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
613 goto error;
615 if (kill_unmarshal(msg, tmp) == -1)
616 goto error;
618 res = 0;
620 error:
621 evbuffer_free(tmp);
622 return (res);
625 void
626 evtag_marshal_kill(struct evbuffer *evbuf, uint32_t tag, const struct kill *msg)
628 struct evbuffer *_buf = evbuffer_new();
629 assert(_buf != NULL);
630 evbuffer_drain(_buf, -1);
631 kill_marshal(_buf, msg);
632 evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
633 evbuffer_free(_buf);
637 * Implementation of run
640 static struct run_access_ __run_base = {
641 run_how_assign,
642 run_how_get,
643 run_some_bytes_assign,
644 run_some_bytes_get,
645 run_fixed_bytes_assign,
646 run_fixed_bytes_get,
649 struct run *
650 run_new(void)
652 struct run *tmp;
653 if ((tmp = malloc(sizeof(struct run))) == NULL) {
654 event_warn("%s: malloc", __func__);
655 return (NULL);
657 tmp->base = &__run_base;
659 tmp->how_data = NULL;
660 tmp->how_set = 0;
662 tmp->some_bytes_data = NULL;
663 tmp->some_bytes_length = 0;
664 tmp->some_bytes_set = 0;
666 memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
667 tmp->fixed_bytes_set = 0;
669 return (tmp);
676 run_how_assign(struct run *msg,
677 const char * value)
679 if (msg->how_data != NULL)
680 free(msg->how_data);
681 if ((msg->how_data = strdup(value)) == NULL)
682 return (-1);
683 msg->how_set = 1;
684 return (0);
688 run_some_bytes_assign(struct run *msg, const uint8_t * value, uint32_t len)
690 if (msg->some_bytes_data != NULL)
691 free (msg->some_bytes_data);
692 msg->some_bytes_data = malloc(len);
693 if (msg->some_bytes_data == NULL)
694 return (-1);
695 msg->some_bytes_set = 1;
696 msg->some_bytes_length = len;
697 memcpy(msg->some_bytes_data, value, len);
698 return (0);
702 run_fixed_bytes_assign(struct run *msg, const uint8_t *value)
704 msg->fixed_bytes_set = 1;
705 memcpy(msg->fixed_bytes_data, value, 24);
706 return (0);
710 run_how_get(struct run *msg, char * *value)
712 if (msg->how_set != 1)
713 return (-1);
714 *value = msg->how_data;
715 return (0);
719 run_some_bytes_get(struct run *msg, uint8_t * *value, uint32_t *plen)
721 if (msg->some_bytes_set != 1)
722 return (-1);
723 *value = msg->some_bytes_data;
724 *plen = msg->some_bytes_length;
725 return (0);
729 run_fixed_bytes_get(struct run *msg, uint8_t **value)
731 if (msg->fixed_bytes_set != 1)
732 return (-1);
733 *value = msg->fixed_bytes_data;
734 return (0);
737 void
738 run_clear(struct run *tmp)
740 if (tmp->how_set == 1) {
741 free (tmp->how_data);
742 tmp->how_data = NULL;
743 tmp->how_set = 0;
745 if (tmp->some_bytes_set == 1) {
746 free (tmp->some_bytes_data);
747 tmp->some_bytes_data = NULL;
748 tmp->some_bytes_length = 0;
749 tmp->some_bytes_set = 0;
751 tmp->fixed_bytes_set = 0;
752 memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
755 void
756 run_free(struct run *tmp)
758 if (tmp->how_data != NULL)
759 free (tmp->how_data);
760 if (tmp->some_bytes_data != NULL)
761 free (tmp->some_bytes_data);
762 free(tmp);
765 void
766 run_marshal(struct evbuffer *evbuf, const struct run *tmp){
767 evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
768 if (tmp->some_bytes_set) {
769 evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
771 evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, sizeof(tmp->fixed_bytes_data));
775 run_unmarshal(struct run *tmp, struct evbuffer *evbuf)
777 uint32_t tag;
778 while (EVBUFFER_LENGTH(evbuf) > 0) {
779 if (evtag_peek(evbuf, &tag) == -1)
780 return (-1);
781 switch (tag) {
783 case RUN_HOW:
785 if (tmp->how_set)
786 return (-1);
787 if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
788 event_warnx("%s: failed to unmarshal how", __func__);
789 return (-1);
791 tmp->how_set = 1;
792 break;
794 case RUN_SOME_BYTES:
796 if (tmp->some_bytes_set)
797 return (-1);
798 if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
799 return (-1);
800 if (tmp->some_bytes_length > EVBUFFER_LENGTH(evbuf))
801 return (-1);
802 if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
803 return (-1);
804 if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
805 event_warnx("%s: failed to unmarshal some_bytes", __func__);
806 return (-1);
808 tmp->some_bytes_set = 1;
809 break;
811 case RUN_FIXED_BYTES:
813 if (tmp->fixed_bytes_set)
814 return (-1);
815 if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, sizeof(tmp->fixed_bytes_data)) == -1) {
816 event_warnx("%s: failed to unmarshal fixed_bytes", __func__);
817 return (-1);
819 tmp->fixed_bytes_set = 1;
820 break;
822 default:
823 return -1;
827 if (run_complete(tmp) == -1)
828 return (-1);
829 return (0);
833 run_complete(struct run *msg)
835 if (!msg->how_set)
836 return (-1);
837 if (!msg->fixed_bytes_set)
838 return (-1);
839 return (0);
843 evtag_unmarshal_run(struct evbuffer *evbuf, uint32_t need_tag, struct run *msg)
845 uint32_t tag;
846 int res = -1;
848 struct evbuffer *tmp = evbuffer_new();
850 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
851 goto error;
853 if (run_unmarshal(msg, tmp) == -1)
854 goto error;
856 res = 0;
858 error:
859 evbuffer_free(tmp);
860 return (res);
863 void
864 evtag_marshal_run(struct evbuffer *evbuf, uint32_t tag, const struct run *msg)
866 struct evbuffer *_buf = evbuffer_new();
867 assert(_buf != NULL);
868 evbuffer_drain(_buf, -1);
869 run_marshal(_buf, msg);
870 evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
871 evbuffer_free(_buf);