14 #include "./regress.gen.h"
16 void event_err(
int eval,
const char *
fmt, ...);
17 void event_warn(
const char *
fmt, ...);
18 void event_errx(
int eval,
const char *
fmt, ...);
19 void event_warnx(
const char *
fmt, ...);
42 if ((tmp = malloc(
sizeof(
struct msg))) == NULL) {
43 event_warn(
"%s: malloc", __func__);
46 tmp->base = &__msg_base;
48 tmp->from_name_data = NULL;
49 tmp->from_name_set = 0;
51 tmp->to_name_data = NULL;
54 tmp->attack_data = NULL;
59 tmp->run_num_allocated = 0;
69 msg_run_add(
struct msg *
msg)
71 if (++msg->run_length >= msg->run_num_allocated) {
72 int tobe_allocated = msg->run_num_allocated;
73 struct run ** new_data = NULL;
74 tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
75 new_data = (
struct run **) realloc(msg->run_data,
76 tobe_allocated *
sizeof(
struct run *));
79 msg->run_data = new_data;
80 msg->run_num_allocated = tobe_allocated;
82 msg->run_data[msg->run_length - 1] = run_new();
83 if (msg->run_data[msg->run_length - 1] == NULL)
86 return (msg->run_data[msg->run_length - 1]);
94 msg_from_name_assign(
struct msg *msg,
97 if (msg->from_name_data != NULL)
98 free(msg->from_name_data);
99 if ((msg->from_name_data = strdup(value)) == NULL)
101 msg->from_name_set = 1;
106 msg_to_name_assign(
struct msg *msg,
109 if (msg->to_name_data != NULL)
110 free(msg->to_name_data);
111 if ((msg->to_name_data = strdup(value)) == NULL)
113 msg->to_name_set = 1;
118 msg_attack_assign(
struct msg *msg,
119 const struct kill* value)
122 if (msg->attack_set) {
123 kill_clear(msg->attack_data);
126 msg->attack_data = kill_new();
127 if (msg->attack_data == NULL) {
128 event_warn(
"%s: kill_new()", __func__);
132 if ((tmp = evbuffer_new()) == NULL) {
133 event_warn(
"%s: evbuffer_new()", __func__);
136 kill_marshal(tmp, value);
137 if (kill_unmarshal(msg->attack_data, tmp) == -1) {
138 event_warnx(
"%s: kill_unmarshal", __func__);
147 if (msg->attack_data != NULL) {
148 kill_free(msg->attack_data);
149 msg->attack_data = NULL;
155 msg_run_assign(
struct msg *msg,
int off,
156 const struct run * value)
159 if (!msg->run_set || off < 0 || off >= msg->run_length)
161 run_clear(msg->run_data[off]);
162 if ((tmp = evbuffer_new()) == NULL) {
163 event_warn(
"%s: evbuffer_new()", __func__);
166 run_marshal(tmp, value);
167 if (run_unmarshal(msg->run_data[off], tmp) == -1) {
168 event_warnx(
"%s: run_unmarshal", __func__);
176 run_clear(msg->run_data[off]);
181 msg_from_name_get(
struct msg *msg,
char * *value)
183 if (msg->from_name_set != 1)
185 *value = msg->from_name_data;
190 msg_to_name_get(
struct msg *msg,
char * *value)
192 if (msg->to_name_set != 1)
194 *value = msg->to_name_data;
199 msg_attack_get(
struct msg *msg,
struct kill* *value)
201 if (msg->attack_set != 1) {
202 msg->attack_data = kill_new();
203 if (msg->attack_data == NULL)
207 *value = msg->attack_data;
212 msg_run_get(
struct msg *msg,
int offset,
215 if (!msg->run_set || offset < 0 || offset >= msg->run_length)
217 *value = msg->run_data[
offset];
222 msg_clear(
struct msg *tmp)
224 if (tmp->from_name_set == 1) {
225 free (tmp->from_name_data);
226 tmp->from_name_data = NULL;
227 tmp->from_name_set = 0;
229 if (tmp->to_name_set == 1) {
230 free (tmp->to_name_data);
231 tmp->to_name_data = NULL;
232 tmp->to_name_set = 0;
234 if (tmp->attack_set == 1) {
235 kill_free(tmp->attack_data);
236 tmp->attack_data = NULL;
239 if (tmp->run_set == 1) {
241 for (i = 0; i < tmp->run_length; ++
i) {
242 run_free(tmp->run_data[i]);
245 tmp->run_data = NULL;
248 tmp->run_num_allocated = 0;
253 msg_free(
struct msg *tmp)
255 if (tmp->from_name_data != NULL)
256 free (tmp->from_name_data);
257 if (tmp->to_name_data != NULL)
258 free (tmp->to_name_data);
259 if (tmp->attack_data != NULL)
260 kill_free(tmp->attack_data);
261 if (tmp->run_data != NULL) {
263 for (i = 0; i < tmp->run_length; ++
i) {
264 run_free(tmp->run_data[i]);
265 tmp->run_data[
i] = NULL;
268 tmp->run_data = NULL;
270 tmp->run_num_allocated = 0;
276 msg_marshal(
struct evbuffer *evbuf,
const struct msg *tmp){
277 evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
278 evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
279 if (tmp->attack_set) {
280 evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
284 for (i = 0; i < tmp->run_length; ++
i) {
285 evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
291 msg_unmarshal(
struct msg *tmp,
struct evbuffer *evbuf)
294 while (EVBUFFER_LENGTH(evbuf) > 0) {
295 if (evtag_peek(evbuf, &tag) == -1)
301 if (tmp->from_name_set)
303 if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
304 event_warnx(
"%s: failed to unmarshal from_name", __func__);
307 tmp->from_name_set = 1;
312 if (tmp->to_name_set)
314 if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
315 event_warnx(
"%s: failed to unmarshal to_name", __func__);
318 tmp->to_name_set = 1;
325 tmp->attack_data = kill_new();
326 if (tmp->attack_data == NULL)
328 if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) {
329 event_warnx(
"%s: failed to unmarshal attack", __func__);
337 if (msg_run_add(tmp) == NULL)
339 if (evtag_unmarshal_run(evbuf, MSG_RUN,
340 tmp->run_data[tmp->run_length - 1]) == -1) {
342 event_warnx(
"%s: failed to unmarshal run", __func__);
353 if (msg_complete(tmp) == -1)
359 msg_complete(
struct msg *msg)
361 if (!msg->from_name_set)
363 if (!msg->to_name_set)
365 if (msg->attack_set && kill_complete(msg->attack_data) == -1)
369 for (i = 0; i < msg->run_length; ++
i) {
370 if (run_complete(msg->run_data[i]) == -1)
378 evtag_unmarshal_msg(
struct evbuffer *evbuf, uint32_t need_tag,
struct msg *msg)
383 struct evbuffer *tmp = evbuffer_new();
385 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
388 if (msg_unmarshal(msg, tmp) == -1)
399 evtag_marshal_msg(
struct evbuffer *evbuf, uint32_t tag,
const struct msg *msg)
401 struct evbuffer *_buf = evbuffer_new();
402 assert(_buf != NULL);
403 evbuffer_drain(_buf, -1);
404 msg_marshal(_buf, msg);
405 evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
418 kill_how_often_assign,
426 if ((tmp = malloc(
sizeof(
struct kill))) == NULL) {
427 event_warn(
"%s: malloc", __func__);
430 tmp->base = &__kill_base;
432 tmp->weapon_data = NULL;
435 tmp->action_data = NULL;
438 tmp->how_often_data = 0;
439 tmp->how_often_set = 0;
448 kill_weapon_assign(
struct kill *msg,
451 if (msg->weapon_data != NULL)
452 free(msg->weapon_data);
453 if ((msg->weapon_data = strdup(value)) == NULL)
460 kill_action_assign(
struct kill *msg,
463 if (msg->action_data != NULL)
464 free(msg->action_data);
465 if ((msg->action_data = strdup(value)) == NULL)
472 kill_how_often_assign(
struct kill *msg,
const uint32_t value)
474 msg->how_often_set = 1;
475 msg->how_often_data = value;
480 kill_weapon_get(
struct kill *msg,
char * *value)
482 if (msg->weapon_set != 1)
484 *value = msg->weapon_data;
489 kill_action_get(
struct kill *msg,
char * *value)
491 if (msg->action_set != 1)
493 *value = msg->action_data;
498 kill_how_often_get(
struct kill *msg, uint32_t *value)
500 if (msg->how_often_set != 1)
502 *value = msg->how_often_data;
507 kill_clear(
struct kill *tmp)
509 if (tmp->weapon_set == 1) {
510 free (tmp->weapon_data);
511 tmp->weapon_data = NULL;
514 if (tmp->action_set == 1) {
515 free (tmp->action_data);
516 tmp->action_data = NULL;
519 tmp->how_often_set = 0;
523 kill_free(
struct kill *tmp)
525 if (tmp->weapon_data != NULL)
526 free (tmp->weapon_data);
527 if (tmp->action_data != NULL)
528 free (tmp->action_data);
533 kill_marshal(
struct evbuffer *evbuf,
const struct kill *tmp){
534 evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
535 evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
536 if (tmp->how_often_set) {
537 evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data);
542 kill_unmarshal(
struct kill *tmp,
struct evbuffer *evbuf)
545 while (EVBUFFER_LENGTH(evbuf) > 0) {
546 if (evtag_peek(evbuf, &tag) == -1)
554 if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
555 event_warnx(
"%s: failed to unmarshal weapon", __func__);
565 if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
566 event_warnx(
"%s: failed to unmarshal action", __func__);
574 if (tmp->how_often_set)
576 if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data) == -1) {
577 event_warnx(
"%s: failed to unmarshal how_often", __func__);
580 tmp->how_often_set = 1;
588 if (kill_complete(tmp) == -1)
594 kill_complete(
struct kill *msg)
596 if (!msg->weapon_set)
598 if (!msg->action_set)
604 evtag_unmarshal_kill(
struct evbuffer *evbuf, uint32_t need_tag,
struct kill *msg)
609 struct evbuffer *tmp = evbuffer_new();
611 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
614 if (kill_unmarshal(msg, tmp) == -1)
625 evtag_marshal_kill(
struct evbuffer *evbuf, uint32_t tag,
const struct kill *msg)
627 struct evbuffer *_buf = evbuffer_new();
628 assert(_buf != NULL);
629 evbuffer_drain(_buf, -1);
630 kill_marshal(_buf, msg);
631 evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
642 run_some_bytes_assign,
644 run_fixed_bytes_assign,
652 if ((tmp = malloc(
sizeof(
struct run))) == NULL) {
653 event_warn(
"%s: malloc", __func__);
656 tmp->base = &__run_base;
658 tmp->how_data = NULL;
661 tmp->some_bytes_data = NULL;
662 tmp->some_bytes_length = 0;
663 tmp->some_bytes_set = 0;
665 memset(tmp->fixed_bytes_data, 0,
sizeof(tmp->fixed_bytes_data));
666 tmp->fixed_bytes_set = 0;
675 run_how_assign(
struct run *msg,
678 if (msg->how_data != NULL)
680 if ((msg->how_data = strdup(value)) == NULL)
687 run_some_bytes_assign(
struct run *msg,
const uint8_t * value, uint32_t len)
689 if (msg->some_bytes_data != NULL)
690 free (msg->some_bytes_data);
691 msg->some_bytes_data = malloc(len);
692 if (msg->some_bytes_data == NULL)
694 msg->some_bytes_set = 1;
695 msg->some_bytes_length = len;
696 memcpy(msg->some_bytes_data, value, len);
701 run_fixed_bytes_assign(
struct run *msg,
const uint8_t *value)
703 msg->fixed_bytes_set = 1;
704 memcpy(msg->fixed_bytes_data, value, 24);
709 run_how_get(
struct run *msg,
char * *value)
711 if (msg->how_set != 1)
713 *value = msg->how_data;
718 run_some_bytes_get(
struct run *msg, uint8_t * *value, uint32_t *plen)
720 if (msg->some_bytes_set != 1)
722 *value = msg->some_bytes_data;
723 *plen = msg->some_bytes_length;
728 run_fixed_bytes_get(
struct run *msg, uint8_t **value)
730 if (msg->fixed_bytes_set != 1)
732 *value = msg->fixed_bytes_data;
737 run_clear(
struct run *tmp)
739 if (tmp->how_set == 1) {
740 free (tmp->how_data);
741 tmp->how_data = NULL;
744 if (tmp->some_bytes_set == 1) {
745 free (tmp->some_bytes_data);
746 tmp->some_bytes_data = NULL;
747 tmp->some_bytes_length = 0;
748 tmp->some_bytes_set = 0;
750 tmp->fixed_bytes_set = 0;
751 memset(tmp->fixed_bytes_data, 0,
sizeof(tmp->fixed_bytes_data));
755 run_free(
struct run *tmp)
757 if (tmp->how_data != NULL)
758 free (tmp->how_data);
759 if (tmp->some_bytes_data != NULL)
760 free (tmp->some_bytes_data);
765 run_marshal(
struct evbuffer *evbuf,
const struct run *tmp){
766 evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
767 if (tmp->some_bytes_set) {
768 evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
770 evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data,
sizeof(tmp->fixed_bytes_data));
774 run_unmarshal(
struct run *tmp,
struct evbuffer *evbuf)
777 while (EVBUFFER_LENGTH(evbuf) > 0) {
778 if (evtag_peek(evbuf, &tag) == -1)
786 if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
787 event_warnx(
"%s: failed to unmarshal how", __func__);
795 if (tmp->some_bytes_set)
797 if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
799 if (tmp->some_bytes_length > EVBUFFER_LENGTH(evbuf))
801 if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
803 if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
804 event_warnx(
"%s: failed to unmarshal some_bytes", __func__);
807 tmp->some_bytes_set = 1;
810 case RUN_FIXED_BYTES:
812 if (tmp->fixed_bytes_set)
814 if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data,
sizeof(tmp->fixed_bytes_data)) == -1) {
815 event_warnx(
"%s: failed to unmarshal fixed_bytes", __func__);
818 tmp->fixed_bytes_set = 1;
826 if (run_complete(tmp) == -1)
832 run_complete(
struct run *msg)
836 if (!msg->fixed_bytes_set)
842 evtag_unmarshal_run(
struct evbuffer *evbuf, uint32_t need_tag,
struct run *msg)
847 struct evbuffer *tmp = evbuffer_new();
849 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
852 if (run_unmarshal(msg, tmp) == -1)
863 evtag_marshal_run(
struct evbuffer *evbuf, uint32_t tag,
const struct run *msg)
865 struct evbuffer *_buf = evbuffer_new();
866 assert(_buf != NULL);
867 evbuffer_drain(_buf, -1);
868 run_marshal(_buf, msg);
869 evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));