summaryrefslogtreecommitdiff
path: root/sirfmon.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2005-05-26 04:05:45 +0000
committerEric S. Raymond <esr@thyrsus.com>2005-05-26 04:05:45 +0000
commit9b34359ec1b611b9adf9650b316ba40391390232 (patch)
tree13221f508e3e3cb35ed76c748f8c5d53bb5897f5 /sirfmon.c
parentcfa56298e82f1a68f3f1e752fc26724cedd96b6a (diff)
downloadgpsd-9b34359ec1b611b9adf9650b316ba40391390232.tar.gz
sirfmon can now monitor through the daemon.
Diffstat (limited to 'sirfmon.c')
-rw-r--r--sirfmon.c42
1 files changed, 27 insertions, 15 deletions
diff --git a/sirfmon.c b/sirfmon.c
index 1975631f..e98942db 100644
--- a/sirfmon.c
+++ b/sirfmon.c
@@ -31,6 +31,7 @@
#include <time.h>
#include <termios.h>
#include <fcntl.h> /* for O_RDWR */
+#include <stdarg.h>
#include "config.h"
#include "gpsutils.h"
@@ -846,6 +847,25 @@ static void refresh_rightpanel1(void)
wrefresh(mid27win);
}
+static void command(char *buf, int len, const char *fmt, ... )
+/* assemble command in printf(3) style, use stderr or syslog */
+{
+ va_list ap;
+ int n;
+
+ va_start(ap, fmt) ;
+ vsnprintf(buf, sizeof(buf), fmt, ap);
+ va_end(ap);
+
+ write(devicefd, buf, strlen(buf));
+ n = read(devicefd, buf, len);
+ if (n >= 0) {
+ buf[n] = '\0';
+ while (isspace(buf[strlen(buf)-1]))
+ buf[strlen(buf)-1] = '\0';
+ }
+}
+
int main (int argc, char **argv)
{
unsigned int i, v;
@@ -901,21 +921,13 @@ int main (int argc, char **argv)
exit(1);
}
controlfd = open(controlsock, O_RDWR);
- if (device) {
- char *channelcmd = (char *)malloc(strlen(device)+5);
-
- strcpy(channelcmd, "F=");
- strcpy(channelcmd, device);
- strcat(channelcmd, "\r\n");
- write(devicefd, channelcmd, strlen(channelcmd));
- } else
- write(devicefd, "F\r\n", 3);
- read(devicefd, buf, sizeof(buf)); /* read F response */
- while (isspace(buf[strlen(buf)-1]))
- buf[strlen(buf)-1] = '\0';
- device = strdup(buf);
- write(devicefd, "R=2\r\n", 5);
- read(devicefd, buf, sizeof(buf)); /* discard R response */
+ if (device)
+ command(buf, sizeof(buf), "F=%s\r\n", device);
+ else
+ command(buf, sizeof(buf), "O\r\n"); /* force device allocation */
+ command(buf, sizeof(buf), "F\r\n");
+ device = strdup(buf+7);
+ command(buf, sizeof(buf), "R=2\r\n");
serial = FALSE;
} else {
devicefd = controlfd = serial_initialize(device = arg);