5 string timed_read(file f, int timeout) {
6 thread reader = fork func() {
9 } catch Thread::signal(int i) {
14 thread killer = fork func() {
17 Thread::send_signal(reader, 1);
18 } catch Thread::signal(int i) {
23 poly v = Thread::join(reader);
24 Thread::send_signal(killer, 1);
31 void flush_input(file f) {
33 string s = timed_read(f, 200);
39 string[*] gps(file f) {
43 fprintf (f, "g\nv\n");
46 string l = timed_read(f, 1000);
48 File::fprintf(stderr, "Read timedout\n");
52 if (String::index(l, "software-version") == 0)
58 string[*] find_gps(string[*] s, string match) {
59 for (int i = 0; i < dim(s); i++)
60 if (String::index(s[i], match) >= 0)
61 return String::wordsplit(s[i], " ");
62 return (string[*]) {};
69 string[*] flags = find_gps(i, "Flags:");
70 string[*] sats = find_gps(i, "Sats:");
72 int actual_flags = string_to_integer(flags[1]);
73 int actual_sats = string_to_integer(sats[1]);
75 while ((actual_flags & (1 << 4)) == 0) {
76 printf("Flags: %s\n", flags[1]);
77 printf("Sats: %s\n", sats[1]);
81 flags = find_gps(i, "Flags:");
82 sats = find_gps(i, "Sats:");
84 actual_flags = string_to_integer(flags[1]);
87 printf("Flags: %s\n", flags[1]);
88 printf("Sats: %s\n", sats[1]);
89 printf("GPS locked\n");
94 string name = argv[1];
95 file f = open(name, "r+");