Obsolete
/
libipc-old
Archived
3
0
Fork 0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues/pull-requests.
libipc-old/drop/msg.c

56 lines
1.4 KiB
C
Raw Normal View History

#include "../lib/pubsubd.h"
#include <cbor.h>
#include <stdlib.h>
#include <string.h>
2016-09-15 23:08:08 +02:00
#define PKT_CLOSE 0
#define PKT_MSG 1
#define PKT_ERROR 2
void
ohshit(int rvalue, const char* str) {
fprintf (stderr, "\033[31merr: %s\033[00m\n", str);
exit (rvalue);
}
void usage (char **argv)
{
printf ( "NOT DONE YET\n");
printf ( "usage: %s [type [param]]...\n", argv[0]);
printf ( "ex: echo data | %s char_t 1\n", argv[0]);
printf ( " This sends a CBOR msg [ 1, \"data\" ]\n");
}
int
main(int argc, char **argv)
{
if (argc == 2 && strcmp ("-h", argv[1]) == 0) {
usage (argv);
exit (1);
}
2016-09-15 23:08:08 +02:00
unsigned char buf[BUFSIZ];
memset (buf, 0, BUFSIZ);
ssize_t buflen = read (0, buf, BUFSIZ);
/* Preallocate the map structure */
cbor_item_t * root = cbor_new_definite_map(1);
/* Add the content */
cbor_map_add(root, (struct cbor_pair) {
.key = cbor_move(cbor_build_uint8(PKT_MSG)),
.value = cbor_move(cbor_build_bytestring(buf, buflen))
});
/* Output: `length` bytes of data in the `buffer` */
unsigned char * buffer;
size_t buffer_size, length = cbor_serialize_alloc (root, &buffer, &buffer_size);
fwrite(buffer, 1, length, stdout);
free(buffer);
fflush(stdout);
cbor_decref(&root);
return EXIT_SUCCESS;
}