Handle newline
authorNils Faerber <nils.faerber@kernelconcepts.de>
Sat, 6 Jul 2013 21:11:49 +0000 (23:11 +0200)
committerNils Faerber <nils.faerber@kernelconcepts.de>
Sat, 6 Jul 2013 21:11:49 +0000 (23:11 +0200)
linux-bt/l2cap_client.c

index a3fb782..3c427b4 100644 (file)
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/l2cap.h>
 
+typedef enum {
+       CMD_NULL = 0,
+       CMD_INVAL,
+       CMD_QUIT,
+} cmd_t;
+
+// replace "markups" with appropr. chars
+char *parse_buffer(char *inbuf, int *cmd)
+{
+       int i,o;
+       static char outbuf[240];
+       
+       memset(outbuf,0,240);
+
+       *cmd = CMD_NULL;
+
+       // if a line starts with a \ then it is an internal command
+       if (inbuf[0] == '\\') {
+               switch (inbuf[1]) {
+                       case 'q':
+                               *cmd = CMD_QUIT;
+                               break;
+                       default:
+                               *cmd = CMD_INVAL;
+                               break;
+               }
+               return NULL;
+       }
+       o=0;
+       for (i=0; i<strlen(inbuf); i++) {
+               if (inbuf[i] == '\\') {
+                       i++;
+                       if (inbuf[i] == 'n')
+                               outbuf[o++] = '\n';
+               } else
+                       outbuf[o++] = inbuf[i];
+       }
+       return outbuf;
+}
+
+
 int main(int argc, char **argv)
 {
        struct sockaddr_l2 addr = { 0 };
-       int s, status, len, i;
+       int s, status, len, i, cmd;
        char buf[255];
        char dest[18];
+       char *out;
        fd_set infds;
        fd_set efds;
 
@@ -68,10 +110,20 @@ int main(int argc, char **argv)
                        if (FD_ISSET(0, &infds)) {
                                len = read(0, buf, 240);
                                if (len > 0) {
-                                       status = write(s, buf, len);
-                                       if (status < 0)
-                                               perror("write");
-                                       fsync(s);
+                                       // status = write(s, buf, len);
+                                       out = parse_buffer(buf, &cmd);
+                                       if (cmd != CMD_NULL) {
+                                               if (cmd == CMD_QUIT) {
+                                                       close(s);
+                                                       exit(0);
+                                               }
+                                       } else {
+                                               len = strlen(out);
+                                               status = write(s, out, len);
+                                               if (status < 0)
+                                                       perror("write");
+                                               fsync(s);
+                                       }
                                }
                        } else if (FD_ISSET(s, &infds)) {
                                len = read(s, buf, 240);