util  Check-in [70991b55c3]

Overview
Comment:refactor tenki
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 70991b55c336efe6d230977e2f952829c4ddccba4534d2a267ac2ec49031c023
User & Date: lexi on 2019-05-01 01:03:44
Other Links: manifest | tags
Context
2019-05-03
15:21
add vpn tool check-in: 75223838e1 user: lexi tags: trunk
2019-05-01
01:03
refactor tenki check-in: 70991b55c3 user: lexi tags: trunk
2019-04-30
23:26
add tenki check-in: ae8a97d783 user: lexi tags: trunk
Changes

Added tenki/make.sh version [f7f3655bf1].













>
>
>
>
>
>
1
2
3
4
5
6
#!/bin/bash
if test -e apikey; then
	cc -Ofast -Dds_apikey="\"$(cat apikey)\"" tenki.c -lssl -otenki
else
	echo "tenki requires an API key. get one from dark sky and put it in a file in this directory named 'apikey' before you try to build"
fi

Deleted tenki/makefile version [d561bb7339].

1
2
tenki: tenki.cc
	$(CC) -Ofast tenki.cc -o tenki -lssl
<
<




Added tenki/tenki.c version [b2cd504148].







































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/* [ʞ] tenki.c
 *  ~ lexi hale <lexi@hale.su>
 *  > ./make.sh
 *  © affero general public license

 *  tenki  is a  client for  the Dark  Sky API.  it is  a
 *  bail-early stream parser which  retrieves a fixed set
 *  of keys (this set may  be easily changed in the code)
 *  and prints them  as a formatted string  to stdout. it
 *  was designed purely as a polybar plugin to display my
 *  local  weather, but  it could  be just  as easily  be
 *  reworked for many other purposes
 *
 *  you need to  get an api key from dark  sky before you
 *  compile tenki, and place it in the file apikey - it's
 *  segregated from the main code this way to allow it to
 *  be excluded from version control.
 *
 *  the default language of  the results is Japanese; you
 *  can change  this by  changing the macro  ds_lang from
 *  "ja" to your language  code of choice. the ds_exclude
 *  macro is used to minimize the bandwidth tenki uses by
 *  instructing  the dark  sky server  not to  send those
 *  fields back; if you want to access information in any
 *  of  them, you  need to  remove them  from ds_exclude.
 *  delete the #define entirely if you wish to access all
 *  of the excluded fields.
 *  
 *  the  function main()  contains most  of the  code you
 *  will  likely wish  to alter.  it contains  a variable
 *  "paths" which is used to  specify which fields of the
 *  returned json object you  wish the parser to extract.
 *  each path is a "p-string of p-strings" - that is, its
 *  first  character is  an integer  which specifies  the
 *  depth of the path,  and each "directory" is specified
 *  with a string prefixed  with the number of characters
 *  in  it.  so  the  path "a/bc/def"  would  be  encoded
 *  "\3\1a\2bc\3def" - \3 for  the three constituents "a"
 *  "bc" and "def", \1 for the  strlen of "a", \2 for the
 *  strlen  of "bc",  etc.  shoutout to  the C  standards
 *  committee for forcing me to do this horrible thing.
 *
 *  in conclusion, i should have written this in scheme.
 **/

#define ds_lang    "ja"
#define ds_exclude "minutely,hourly,daily,alerts,flags"

// posix includes
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>

// libc includes
#include <stdio.h>
#include <string.h>

// networking includes
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <openssl/ssl.h>

#ifndef ds_apikey
#	error missing api key; compile with make.sh
#   define ds_apikey "" // tidy up errors
#endif

#ifdef ds_exclude
#	define _http "?exclude=" ds_exclude "&"
#else
#	define _http "?"
#endif
#define http _http "lang=" ds_lang " HTTP/1.1\r\nHost: api.darksky.net\r\n\r\n"
#define start "GET /forecast/" ds_apikey "/"

#define min(x,y) (x>y?y:x)
#define len(x) (sizeof(x)/sizeof(*x))

typedef enum bool { false, true } bool;

SSL_CTX* sc;
bool head(size_t* len, char* txt, char** body) {
	char* hend = strstr(txt, "\r\n\r\n");
	if (hend == NULL) return false;
	#define ctlhead "\r\nContent-Length:"
	char* ctl = strstr(txt, ctlhead);
	if (ctl == NULL || ctl > hend) return false;
	ctl += len(ctlhead);
	*len = 0;
	while(*ctl != '\r') {
		if (*ctl >= '0' && *ctl <= '9') *len*=10,*len+=*ctl-'0';
		++ctl;
	}
	*len+=(hend-txt)+4;
	*body = hend+4;
	return true;
}
size_t get(char* resp, size_t max, const char* msg, size_t total, char** body){

	int port = 443;
	const char* host = "api.darksky.net";
	//printf("req: %s\n", msg);	
	struct hostent* api;
	struct sockaddr_in api_addr;
	int sockfd, bytes;
	size_t sent, recd;
	
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) exit(2);
	SSL *con = SSL_new(sc);
	SSL_set_fd(con, sockfd);

	api = gethostbyname(host);
	if (api == NULL) exit(3);

	memset(&api_addr, 0, sizeof(api_addr));
	api_addr.sin_family = AF_INET;
	api_addr.sin_port = htons(port);
	memcpy(&api_addr.sin_addr.s_addr, api -> h_addr, api -> h_length);

	if (connect(sockfd,(struct sockaddr*)&api_addr,sizeof(api_addr)))
		exit(4);
	int err = SSL_connect(con);


	sent = 0;
	do {
		bytes = SSL_write(con,msg+sent,total-sent);
		if (bytes < 0) exit (4);
		if (bytes == 0) break;
		++sent;
	} while (sent < total);
	recd = 0;
	size_t resplen = max;
	bool rhead = false;
	do {
		bytes = SSL_read(con,resp+recd,min(resplen-recd,15000));
		if (bytes < 0) exit (5);
		if (bytes == 0) break;
		if(!rhead)rhead=head(&resplen, resp, body);
		recd += bytes;
	} while (recd < resplen - 1);

	close(sockfd);
	SSL_shutdown(con);

	return recd;
}
size_t pos(char* msg, const char* lat, const char* lng) {
	char* cur = msg + len(start) - 1;
	size_t latl = strlen(lat);
	size_t longl = strlen(lng);
	strcpy(cur, lat); cur += latl;
	*cur++=','; 
	strcpy(cur, lng); cur += longl;
	strcpy(cur, http); cur += len(http);
	*cur--=0;
	return cur-msg;
}
bool estrc(size_t sz, const char* str1, const char* str2) {
	for(size_t i = 0;i<sz;++i) {
		if(*str1 == *str2) {
			++str1; ++str2;
		} else if (*str2 =='\\') {
			++str2;
			if(*str1 == *str2) {
				++str1; ++str2;
			} else return false;
		} else {
			return false;
		}
	}
	return true;
}
typedef struct pstr {
	size_t sz;
	const char* a;
} pstr;
typedef union jsv {
	float f;
	pstr s;
} jsv;
/* void printkey(const char* key) {
	size_t len = *key++;
	for (size_t i = 0; i<len; ++i) {
		printf("/%.*s",*key,key+1);
		key += *key + 1;
	};
	printf("\n");
} */
void pplt(const char* str, const char* const end, const char** keys, const char** const keyend, jsv* values) {
	// initialize parser state
	size_t depth = 0;
	const char* fld = NULL;
	const char* path[32];
	path[0] = NULL;
	
	while(*str++!='{'); //cheat
	init: {
		if (keys > keyend) return;
		if (str > end) return;
		/* print_path: {
			printf("current path: /");
			for (size_t i = 0; i < depth; ++i) {
				printf("%.*s/",strchr(path[i],'"')-path[i],path[i]);
			}
			printf("\n");
			printf("current key: ");
			printkey(*keys);
		} */
		parse_char: {
			switch(*str++) {
				case ' ': case '\t': case '\n':
					goto parse_char;
				case '"':
					path[depth] = str;
					goto path_find_quote_end;
				case '}':
					if (depth == 0) return;
					depth--;
					while(*str!=',') ++str; ++str;
					goto init;
				default:
					printf("found illegal char %c\n", *(str-1));
					exit(6);
			}
		}
		path_find_quote_end: {
			switch (*str++) {
				case '\\': ++str; goto path_find_quote_end;
				case '"': goto read_value;
				default: goto path_find_quote_end;
			}
		}
		read_value: {
			if (*str++!=':') {
				printf("illegal char\n");
				exit(6);
			}
			bool iskey;
			const char* key = *keys;
			jsv* value;
			if (depth + 1 != *key++) {
				iskey = false;
				goto comp;
			} else for (size_t i = 0; i <= depth; ++i) {
				if (estrc(*key, key+1, path[i])) {
					key += *key + 1;
				} else {
					iskey = false;
					goto comp;
				}
			}
			iskey = true;
			value = values;
			++keys; ++values;
			comp: if (*str == '{') {
				++depth; ++str;
				goto init;
			} else if (*str == '"') {
				goto copy_str_value;
			} else if (*str == '[') {
				goto skip_array;
			} else if ((*str >= '0' && *str <= '9') || *str=='-') {
				goto copy_int_value;
			} else {
				printf("illegal character found in value %c\n", *str);
				exit(6);
			}

			copy_str_value: {
				fld = ++str;
				while (*++str != '"') if (*str == '\\') { ++str; continue; }
				if (iskey) {
					value -> s.sz = str - fld;
					value -> s.a = fld;
				}
				while (*++str != ',');
				++str; goto init;
			}
			skip_array: {
				size_t ard = 0;
				skip_loop: switch (*++str) {
					case '{': case '[': ++ard; goto skip_loop;
					case '"': goto skip_str;
					case '}': if (ard == 0) {
								  printf("bad json\n");
								  exit(7);
							  } else { --ard; goto skip_loop; }
					case ']': if (ard == 0) {
							if (*++str == ',') { ++str; goto init; }
							else goto init;
						} else { --ard; }
					default: goto skip_loop;
				}
				skip_str: while (*str != '"') if (*str == '\\') { str += 2; continue; } else ++str; goto skip_loop;
			}
			copy_int_value: {
				if (!iskey) {
					while(*str++!=',') if (str > end) return;
					goto init;
				}
				value -> f = 0;
				bool neg;
				if (*str == '-') { neg=true; ++str; } else neg=false;
				while(*str >= '0' && *str <= '9') {
					value -> f *= 10;
					value -> f += *str - '0';
					++str;
				}
				if(*str == '.') {
					float fac = 0.1;
					float val = 0;
					while(*++str >= '0' && *str <= '9') {
						val += (((float)(*str - '0')) * fac);
						fac *= 0.1;
					}
					value -> f += val;
				}
				if(neg) value -> f *= -1;
				if (*str == ',') {
					++str; goto init;
				} else {
					printf("illegal character %.*s\n",15,str);
					exit(6);
				}
			}
		}
	}
}

int main(int argc, char** argv) {
	if (argc != 3) exit(1);
	SSL_load_error_strings();
	SSL_library_init();
	sc = SSL_CTX_new(SSLv23_client_method());

	char msg[256] = start;
	char resp[32368];
	char* body;

	// name the fields to extract from the json stream
	// note: MUST BE LISTED IN ORDER THEY APPEAR
	// TODO: performance worth lack of flexibility?
	const char* paths[] = {
		"\2\x9""currently\7summary",
		"\2\x9""currently\xfprecipIntensity",
		"\2\x9""currently\xbtemperature",
		"\2\x9""currently\x9windspeed",
	};

	//copy the latitude and longitude into the request
	size_t msgsz = pos(msg, argv[1], argv[2]);
		 //msgsz = size of what we're going to transmit

	//for (;;) {
		size_t rspsz = get(resp, sizeof(resp), msg, msgsz, &body);
		// if paths contains the keys, this contains the values
		jsv values[len(paths)]; 
		
		parse: {
			pplt(body, body + (rspsz - (resp - body)), paths, paths+len(paths), values);
			
			pstr summary = values[0].s;
			float pcpint = values[1].f,
				temp = values[2].f,
				windspd = values[3].f;

			printf("%.1f°  %%{F#ff9bbb}%.*s\n",
					summary.sz,
					summary.a,
					temp);
		}
		// dark sky's API allows us to make 1000 free requests a day.
		// let's try not to get too near that.
		// hours in day 24 - minutes in day 24 * 60 = 1440
		// so once a minute is almost half again too many
		// 1440 / 2 = 720 leaving reasonable refresh time without
		// going too near the limit. so we aim for one update
		// every two minutes.
	//	sleep(60 * 2);
//	}

	return 0;
}

Deleted tenki/tenki.cc version [eaa2ec3cc1].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdint.h>
#include <openssl/ssl.h>

#define ds_apikey "a6aa0337fafd8b8fcfafe84a403ed24f"
#define ds_lang "ja"
#define ds_exclude "minutely,hourly,daily,alerts,flags"

#define req "GET /forecast/"
#define http "?exclude=" ds_exclude "&lang=" ds_lang " HTTP/1.1\r\nHost: api.darksky.net\r\n\r\n"

#define len(x) (sizeof(x)/sizeof(*x))
#define start req ds_apikey "/"
#define min(x,y) (x>y?y:x)

SSL_CTX* sc;
bool head(size_t* len, char* txt, char** body) {
	char* hend = strstr(txt, "\r\n\r\n");
	if (hend == nullptr) return false;
	#define ctlhead "\r\nContent-Length:"
	char* ctl = strstr(txt, ctlhead);
	if (ctl == nullptr or ctl > hend) return false;
	ctl += len(ctlhead);
	*len = 0;
	while(*ctl != '\r') {
		if (*ctl >= '0' and *ctl <= '9') *len*=10,*len+=*ctl-'0';
		++ctl;
	}
	*len+=(hend-txt)+4;
	*body = hend+4;
	return true;
}
size_t get(char* resp, size_t max, const char* msg, size_t total, char** body){

	int port = 443;
	const char* host = "api.darksky.net";
	//printf("req: %s\n", msg);	
	struct hostent* api;
	struct sockaddr_in api_addr;
	int sockfd, bytes;
	size_t sent, recd;
	
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0) exit(2);
	SSL *con = SSL_new(sc);
	SSL_set_fd(con, sockfd);

	//printf("getting host\n");
	api = gethostbyname(host);
	if (api == nullptr) exit(3);

	memset(&api_addr, 0, sizeof(api_addr));
	api_addr.sin_family = AF_INET;
	api_addr.sin_port = htons(port);
	memcpy(&api_addr.sin_addr.s_addr, api -> h_addr, api -> h_length);

	//printf("connecting\n");
	if (connect(sockfd,(struct sockaddr*)&api_addr,sizeof(api_addr)))
		exit(4);
	int err = SSL_connect(con);


	sent = 0;
//	printf("writing\n");
	do {
	//	printf("writing byte %x\n",*(msg+sent));
		bytes = SSL_write(con,msg+sent,total-sent);
		if (bytes < 0) exit (4);
		if (bytes == 0) break;
		++sent;
	} while (sent < total);
//	printf("receiving\n");
	recd = 0;
	size_t resplen = max;
	bool rhead = false;
	do {
	//	printf("\n\nreading - addr %x; max %d\n",resp+recd,resplen-recd);
		bytes = SSL_read(con,resp+recd,min(resplen-recd,15000));
		if (bytes < 0) exit (5);
		if (bytes == 0) break;
		if(!rhead)rhead=head(&resplen, resp, body);
		// printf("got %d bytes: %s", bytes, resp+recd);
		recd += bytes;
	} while (recd < resplen - 1);

	close(sockfd);
	SSL_shutdown(con);

	return recd;
}
size_t pos(char* msg, const char* lat, const char* lng) {
	char* cur = msg + len(start) - 1;
	size_t latl = strlen(lat);
	size_t longl = strlen(lng);
	strcpy(cur, lat); cur += latl;
	*cur++=','; 
	strcpy(cur, lng); cur += longl;
	strcpy(cur, http); cur += len(http);
	*cur--=0;
	return cur-msg;
}
bool estrc(size_t sz, const char* str1, const char* str2) {
	for(size_t i = 0;i<sz;++i) {
		if(*str1 == *str2) {
			++str1; ++str2;
		} else if (*str2 =='\\') {
			++str2;
			if(*str1 == *str2) {
				++str1; ++str2;
			} else return false;
		} else {
			return false;
		}
	}
	return true;
}
struct pstr {
	size_t sz;
	const char* a;
};
union jsv {
	float f;
	pstr s;
};
/* void printkey(const char* key) {
	size_t len = *key++;
	for (size_t i = 0; i<len; ++i) {
		printf("/%.*s",*key,key+1);
		key += *key + 1;
	};
	printf("\n");
} */
void pplt(const char* str, const char* const end, const char** keys, const char** const keyend, jsv* values) {
	// initialize parser state
	size_t depth = 0;
	const char* fld = nullptr;
	const char* path[32];
	path[0] = nullptr;
	
	while(*str++!='{'); //cheat
	init: {
		if (keys > keyend) return;
		if (str > end) return;
		/* print_path: {
			printf("current path: /");
			for (size_t i = 0; i < depth; ++i) {
				printf("%.*s/",strchr(path[i],'"')-path[i],path[i]);
			}
			printf("\n");
			printf("current key: ");
			printkey(*keys);
		} */
		parse_char: {
			switch(*str++) {
				case ' ': case '\t': case '\n':
					goto parse_char;
				case '"':
					path[depth] = str;
					goto path_find_quote_end;
				case '}':
					if (depth == 0) return;
					depth--;
					while(*str!=',') ++str; ++str;
					goto init;
				default:
					printf("found illegal char %c\n", *(str-1));
					exit(6);
			}
		}
		path_find_quote_end: {
			switch (*str++) {
				case '\\': ++str; goto path_find_quote_end;
				case '"': goto read_value;
				default: goto path_find_quote_end;
			}
		}
		read_value: {
			if (*str++!=':') {
				printf("illegal char\n");
				exit(6);
			}
			bool iskey;
			const char* key = *keys;
			jsv* value;
			if (depth + 1 != *key++) {
				iskey = false;
				goto comp;
			} else for (size_t i = 0; i <= depth; ++i) {
				if (estrc(*key, key+1, path[i])) {
					key += *key + 1;
				} else {
					iskey = false;
					goto comp;
				}
			}
			iskey = true;
			value = values;
			++keys; ++values;
			comp: if (*str == '{') {
				++depth; ++str;
				goto init;
			} else if (*str == '"') {
				goto copy_str_value;
			} else if (*str == '[') {
				goto skip_array;
			} else if ((*str >= '0' and *str <= '9') or *str=='-') {
				goto copy_int_value;
			} else {
				printf("illegal character found in value %c\n", *str);
				exit(6);
			}

			copy_str_value: {
				fld = ++str;
				while (*++str != '"') if (*str == '\\') { ++str; continue; }
				if (iskey) {
					value -> s.sz = str - fld;
					value -> s.a = fld;
				}
				while (*++str != ',');
				++str; goto init;
			}
			skip_array: {
				size_t ard = 0;
				skip_loop: switch (*++str) {
					case '{': case '[': ++ard; goto skip_loop;
					case '"': goto skip_str;
					case '}': if (ard == 0) {
								  printf("bad json\n");
								  exit(7);
							  } else { --ard; goto skip_loop; }
					case ']': if (ard == 0) {
							if (*++str == ',') { ++str; goto init; }
							else goto init;
						} else { --ard; }
					default: goto skip_loop;
				}
				skip_str: while (*str != '"') if (*str == '\\') { str += 2; continue; } else ++str; goto skip_loop;
			}
			copy_int_value: {
				if (!iskey) {
					while(*str++!=',') if (str > end) return;
					goto init;
				}
				value -> f = 0;
				bool neg;
				if (*str == '-') { neg=true; ++str; } else neg=false;
				while(*str >= '0' and *str <= '9') {
					value -> f *= 10;
					value -> f += *str - '0';
					++str;
				}
				if(*str == '.') {
					float fac = 0.1;
					float val = 0;
					while(*++str >= '0' and *str <= '9') {
						val += (((float)(*str - '0')) * fac);
						fac *= 0.1;
					}
					value -> f += val;
				}
				if(neg) value -> f *= -1;
				if (*str == ',') {
					++str; goto init;
				} else {
					printf("illegal character %.*s\n",15,str);
					exit(6);
				}
			}
		}
	}
}
int main(int argc, char** argv) {
	if (argc != 3) exit(1);
	SSL_load_error_strings();
	SSL_library_init();
	sc = SSL_CTX_new(SSLv23_client_method());

	char msg[256] = start;
	char resp[32368];
	char* body;

	// name the fields to extract from the json stream
	// note: MUST BE LISTED IN ORDER THEY APPEAR
	// paths are encoded as sequences of pstrings
	// initial value indicates depth of path
	// TODO: performance worth lack of flexibility?
	const char* paths[] = {
		"\2\x9""currently\7summary",
		"\2\x9""currently\xfprecipIntensity",
		"\2\x9""currently\xbtemperature",
		"\2\x9""currently\x9windspeed",
	};

	size_t msgsz = pos(msg, argv[1], argv[2]);
		//msgsz = size of transmit
	//for (;;) {
		size_t rspsz = get(resp, sizeof(resp), msg, msgsz, &body);
		// if paths contains the keys, this contains the values
		jsv values[len(paths)]; 
		
		parse: {
			pplt(body, body + (rspsz - (resp - body)), paths, paths+len(paths), values);
			
			pstr summary = values[0].s;
			float pcpint = values[1].f,
				temp = values[2].f,
				windspd = values[3].f;

			printf("%.1f°  %%{F#ff9bbb}%.*s\n",
					summary.sz,
					summary.a,
					temp);
		}
		// dark sky's API allows us to make 1000 free requests a day.
		// let's try not to get too near that.
		// hours in day 24 - minutes in day 24 * 60 = 1440
		// so once a minute is almost half again too many
		// 1440 / 2 = 720 leaving reasonable refresh time without
		// going too near the limit. so we aim for one update
		// every two minutes.
	//	sleep(60 * 2);
//	}

	return 0;
}
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<