From 1e256e1f0adf0d230597138a3f02a92291924c5f Mon Sep 17 00:00:00 2001 From: rodri Date: Sat, 22 Feb 2020 22:59:40 +0000 Subject: now version controlled. --- geolocate | 2 ++ geoparse.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ install | 11 ++++++++ mkfile | 9 +++++++ 4 files changed, 107 insertions(+) create mode 100755 geolocate create mode 100644 geoparse.c create mode 100755 install create mode 100644 mkfile diff --git a/geolocate b/geolocate new file mode 100755 index 0000000..769aa99 --- /dev/null +++ b/geolocate @@ -0,0 +1,2 @@ +#!/bin/rc +hget http://ip-api.com/json/$1 | stalker/geoparse diff --git a/geoparse.c b/geoparse.c new file mode 100644 index 0000000..b58a0f2 --- /dev/null +++ b/geoparse.c @@ -0,0 +1,85 @@ +#include +#include +#include + +typedef struct TargetInfo TargetInfo; + +struct TargetInfo { + char *name; + int type; +}; + +void +usage(void) +{ + fprint(2, "usage: geoparse [jsonfile]\n"); + exits("usage"); +} + +char * +jsontypestr(int type) +{ + char *tab[] = { + [JSONNull] "null", + [JSONBool] "bool", + [JSONNumber] "number", + [JSONString] "string", + [JSONArray] "array", + [JSONObject] "object" + }; + return tab[type]; +} + +TargetInfo ti[] = { + { "as", JSONString }, + { "city", JSONString }, + { "country", JSONString }, + { "countryCode", JSONString }, + { "isp", JSONString }, + { "lat", JSONNumber }, + { "lon", JSONNumber }, + { "org", JSONString }, + { "query", JSONString }, + { "region", JSONString }, + { "regionName", JSONString }, + { "status", JSONString }, + { "timezone", JSONString }, + { "zip", JSONString } +}; + +void +main(int argc, char *argv[]) +{ + char buf[1024]; + JSON *obj, *record; + int fd, n; + int i; + + if(argc < 2) + fd = 0; + else{ + fd = open(argv[1], OREAD); + if(fd < 0) + sysfatal("open: %r"); + } + if((n = read(fd, buf, sizeof(buf)-1)) <= 0) + sysfatal("read: %r"); + buf[n] = 0; + obj = jsonparse(buf); + if(obj == nil || obj->t != JSONObject) + sysfatal("wrong input"); + for(i = 0; i < nelem(ti); i++){ + record = jsonbyname(obj, ti[i].name); + if(record == nil) + continue; + if(record->t != ti[i].type) + sysfatal("%s field not a %s", + ti[i].name, jsontypestr(ti[i].type)); + if(ti[i].type == JSONString) + print("%s=%s\n", ti[i].name, record->s); + else + print("%s=%g\n", ti[i].name, record->n); + } + jsonfree(obj); + exits(0); +} diff --git a/install b/install new file mode 100755 index 0000000..06a5f25 --- /dev/null +++ b/install @@ -0,0 +1,11 @@ +#!/bin/rc +. <{cat /$objtype/mkfile | grep -v '^<'} + +RCBIN=$home/bin/rc/stalker + +if(! test -d $RCBIN) + mkdir $RCBIN +mk +cp $O.out $RCBIN/geoparse +cp geolocate $RCBIN +mk clean diff --git a/mkfile b/mkfile new file mode 100644 index 0000000..a7f1ef8 --- /dev/null +++ b/mkfile @@ -0,0 +1,9 @@ +