Browse code

Handle newline

Nils Faerber authored on 06/07/2013 21:11:49
Showing 1 changed files
... ...
@@ -15,12 +15,54 @@
15 15
 #include <bluetooth/bluetooth.h>
16 16
 #include <bluetooth/l2cap.h>
17 17
 
18
+typedef enum {
19
+	CMD_NULL = 0,
20
+	CMD_INVAL,
21
+	CMD_QUIT,
22
+} cmd_t;
23
+
24
+// replace "markups" with appropr. chars
25
+char *parse_buffer(char *inbuf, int *cmd)
26
+{
27
+	int i,o;
28
+	static char outbuf[240];
29
+	
30
+	memset(outbuf,0,240);
31
+
32
+	*cmd = CMD_NULL;
33
+
34
+	// if a line starts with a \ then it is an internal command
35
+	if (inbuf[0] == '\\') {
36
+		switch (inbuf[1]) {
37
+			case 'q':
38
+				*cmd = CMD_QUIT;
39
+				break;
40
+			default:
41
+				*cmd = CMD_INVAL;
42
+				break;
43
+		}
44
+		return NULL;
45
+	}
46
+	o=0;
47
+	for (i=0; i<strlen(inbuf); i++) {
48
+		if (inbuf[i] == '\\') {
49
+			i++;
50
+			if (inbuf[i] == 'n')
51
+				outbuf[o++] = '\n';
52
+		} else
53
+			outbuf[o++] = inbuf[i];
54
+	}
55
+	return outbuf;
56
+}
57
+
58
+
18 59
 int main(int argc, char **argv)
19 60
 {
20 61
 	struct sockaddr_l2 addr = { 0 };
21
-	int s, status, len, i;
62
+	int s, status, len, i, cmd;
22 63
 	char buf[255];
23 64
 	char dest[18];
65
+	char *out;
24 66
 	fd_set infds;
25 67
 	fd_set efds;
26 68
 
... ...
@@ -68,10 +110,20 @@ int main(int argc, char **argv)
68 110
 			if (FD_ISSET(0, &infds)) {
69 111
 				len = read(0, buf, 240);
70 112
 				if (len > 0) {
71
-					status = write(s, buf, len);
72
-					if (status < 0)
73
-						perror("write");
74
-					fsync(s);
113
+					// status = write(s, buf, len);
114
+					out = parse_buffer(buf, &cmd);
115
+					if (cmd != CMD_NULL) {
116
+						if (cmd == CMD_QUIT) {
117
+							close(s);
118
+							exit(0);
119
+						}
120
+					} else {
121
+						len = strlen(out);
122
+						status = write(s, out, len);
123
+						if (status < 0)
124
+							perror("write");
125
+						fsync(s);
126
+					}
75 127
 				}
76 128
 			} else if (FD_ISSET(s, &infds)) {
77 129
 				len = read(s, buf, 240);