]> git.meshlink.io Git - meshlink/blobdiff - examples/chat.c
Format the examples and test suite.
[meshlink] / examples / chat.c
index f5f41c936126c98393e1e53c1792d8e7267b2357..7cf5a4808919d4ab7f65811352f447b7b698a4e1 100644 (file)
@@ -27,10 +27,11 @@ static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void
 }
 
 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
-       if(reachable)
+       if(reachable) {
                printf("%s joined.\n", node->name);
-       else
+       } else {
                printf("%s left.\n", node->name);
+       }
 }
 
 static meshlink_node_t **nodes;
@@ -38,8 +39,10 @@ static size_t nnodes;
 
 static void parse_command(meshlink_handle_t *mesh, char *buf) {
        char *arg = strchr(buf, ' ');
-       if(arg)
+
+       if(arg) {
                *arg++ = 0;
+       }
 
        if(!strcasecmp(buf, "invite")) {
                char *invitation;
@@ -50,6 +53,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
                }
 
                invitation = meshlink_invite(mesh, arg);
+
                if(!invitation) {
                        fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
                        return;
@@ -62,11 +66,15 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
                        fprintf(stderr, "/join requires an argument!\n");
                        return;
                }
+
                meshlink_stop(mesh);
-               if(!meshlink_join(mesh, arg))
+
+               if(!meshlink_join(mesh, arg)) {
                        fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
-               else
+               } else {
                        fprintf(stderr, "Invitation accepted!\n");
+               }
+
                if(!meshlink_start(mesh)) {
                        fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
                        exit(1);
@@ -78,6 +86,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
                }
 
                meshlink_node_t *node = meshlink_get_node(mesh, arg);
+
                if(!node) {
                        fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
                        return;
@@ -89,20 +98,26 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
        } else if(!strcasecmp(buf, "who")) {
                if(!arg) {
                        nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
-                       if(!nnodes)
+
+                       if(!nnodes) {
                                fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
-                       else {
+                       else {
                                printf("%zu known nodes:", nnodes);
-                               for(int i = 0; i < nnodes; i++)
+
+                               for(int i = 0; i < nnodes; i++) {
                                        printf(" %s", nodes[i]->name);
+                               }
+
                                printf("\n");
                        }
                } else {
                        meshlink_node_t *node = meshlink_get_node(mesh, arg);
-                       if(!node)
+
+                       if(!node) {
                                fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
-                       else
+                       } else {
                                printf("Node %s found\n", arg);
+                       }
                }
        } else if(!strcasecmp(buf, "quit")) {
                printf("Bye!\n");
@@ -117,36 +132,42 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) {
                        "/who [<name>]         List all nodes or show information about the given node.\n"
                        "/quit                 Exit this program.\n"
                );
-       } else
+       } else {
                fprintf(stderr, "Unknown command '/%s'\n", buf);
+       }
 }
 
 static void parse_input(meshlink_handle_t *mesh, char *buf) {
        static meshlink_node_t *destination;
        size_t len;
 
-       if(!buf)
+       if(!buf) {
                return;
+       }
 
        // Remove newline.
 
        len = strlen(buf);
 
-       if(len && buf[len - 1] == '\n')
+       if(len && buf[len - 1] == '\n') {
                buf[--len] = 0;
+       }
 
-       if(len && buf[len - 1] == '\r')
+       if(len && buf[len - 1] == '\r') {
                buf[--len] = 0;
+       }
 
        // Ignore empty lines.
 
-       if(!len)
+       if(!len) {
                return;
+       }
 
        // Commands start with '/'
 
-       if(*buf == '/')
+       if(*buf == '/') {
                return parse_command(mesh, buf + 1);
+       }
 
        // Lines in the form "name: message..." set the destination node.
 
@@ -156,10 +177,13 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) {
        if(colon) {
                *colon = 0;
                msg = colon + 1;
-               if(*msg == ' ')
+
+               if(*msg == ' ') {
                        msg++;
+               }
 
                destination = meshlink_get_node(mesh, buf);
+
                if(!destination) {
                        fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
                        return;
@@ -184,15 +208,18 @@ int main(int argc, char *argv[]) {
        const char *nick = NULL;
        char buf[1024];
 
-       if(argc > 1)
+       if(argc > 1) {
                confbase = argv[1];
+       }
 
-       if(argc > 2)
+       if(argc > 2) {
                nick = argv[2];
+       }
 
        meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
 
        meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
+
        if(!mesh) {
                fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
                return 1;
@@ -209,8 +236,9 @@ int main(int argc, char *argv[]) {
 
        printf("Chat started.\nType /help for a list of commands.\n");
 
-       while(fgets(buf, sizeof(buf), stdin))
+       while(fgets(buf, sizeof(buf), stdin)) {
                parse_input(mesh, buf);
+       }
 
        printf("Chat stopping.\n");