osmq

openstreetmap for command line
git clone https://logand.com/git/osmq.git/
Log | Files | Refs

commit 1198f783db53c8561ca78bb9240b749eb0a00e64
parent 0e5e6cec1ace21d11e4b3fd2568b3a7aa0a7cd54
Author: Tomas Hlavaty <tom@logand.com>
Date:   Thu, 12 Sep 2019 15:44:23 +0200

readability

Diffstat:
Mosmq | 3+--
Mosmtile.c | 184++++++++++++++++++++++++++++++++++---------------------------------------------
2 files changed, 79 insertions(+), 108 deletions(-)

diff --git a/osmq b/osmq @@ -1,6 +1,5 @@ #!/usr/bin/env bash -set -e -set -o pipefail +set -euo pipefail read \ LAT \ LON \ diff --git a/osmtile.c b/osmtile.c @@ -5,6 +5,8 @@ #include <sys/wait.h> #include <unistd.h> +#define fail(z, ...) ({fprintf(stderr, __VA_ARGS__); exit(z);}) + /* https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames */ static int long2tilex(double lon, int z) { return (int)(floor((lon + 180.0) / 360.0 * pow(2.0, z))); @@ -24,53 +26,54 @@ static double tiley2lat(int y, int z) { } static int zoom_from_string(char *string) { - int z; - if(!string) goto fail; - if(1 != sscanf(string, "%d", &z)) goto fail; - if(z < 0 || 18 < z) goto fail; - return z; - fail: - fprintf(stderr, "expected zoom 0 <= %s <= 18\n", string); - exit(1); + if(string) { + int z; + if(1 == sscanf(string, "%d", &z)) { + if(0 <= z && z <= 18) { + return z; + } + } + } + fail(1, "expected zoom 0 <= %s <= 18\n", string); } -static int lat(int argc, char *argv[]) { +static int main_lat(int argc, char *argv[]) { int z = zoom_from_string(argv[0]); char *value = argv[1]; - if(!value) goto fail; - int y; - if(1 != sscanf(value, "%d", &y)) goto fail; - printf("%lf\n", tiley2lat(y, z)); - return 0; - fail: - fprintf(stderr, "unexpected y %s\n", value); - return 1; + if(value) { + int y; + if(1 == sscanf(value, "%d", &y)) { + printf("%lf\n", tiley2lat(y, z)); + return 0; + } + } + fail(1, "unexpected y %s\n", value); } -static int lon(int argc, char *argv[]) { +static int main_lon(int argc, char *argv[]) { int z = zoom_from_string(argv[0]); char *value = argv[1]; - if(!value) goto fail; - int x; - if(1 != sscanf(value, "%d", &x)) goto fail; - printf("%lf\n", tilex2long(x, z)); - return 0; - fail: - fprintf(stderr, "unexpected x %s\n", value); - return 1; + if(value) { + int x; + if(1 == sscanf(value, "%d", &x)) { + printf("%lf\n", tilex2long(x, z)); + return 0; + } + } + fail(1, "unexpected x %s\n", value); } static double lon_from_string(char *string) { - if(!string) goto fail; - double z; - if(1 != sscanf(string, "%lf", &z)) goto fail; - return z; - fail: - fprintf(stderr, "unexpected longitude %s\n", string); - exit(1); + if(string) { + double z; + if(1 == sscanf(string, "%lf", &z)) { + return z; + } + } + fail(1, "unexpected longitude %s\n", string); } -static int x(int argc, char *argv[]) { +static int main_x(int argc, char *argv[]) { int z = zoom_from_string(argv[0]); double lon = lon_from_string(argv[1]); printf("%d\n", long2tilex(lon, z)); @@ -78,16 +81,16 @@ static int x(int argc, char *argv[]) { } static double lat_from_string(char *string) { - if(!string) goto fail; - double z; - if(1 != sscanf(string, "%lf", &z)) goto fail; - return z; - fail: - fprintf(stderr, "unexpected latitude %s\n", string); - exit(1); + if(string) { + double z; + if(1 == sscanf(string, "%lf", &z)) { + return z; + } + } + fail(1, "unexpected latitude %s\n", string); } -static int y(int argc, char *argv[]) { +static int main_y(int argc, char *argv[]) { int z = zoom_from_string(argv[0]); double lat = lat_from_string(argv[1]); printf("%d\n", lat2tiley(lat, z)); @@ -106,19 +109,13 @@ static int probe(char *path) { static void run(char *args[]) { int stat; switch(fork()) { - case -1: - fprintf(stderr, "fork failed\n"); - exit(1); + case -1: fail(1, "fork failed\n"); case 0: execvp(*args, args); - fprintf(stderr, "exec %s failed\n", *args); - exit(1); + fail(1, "exec %s failed\n", *args); default: wait(&stat); - if(stat) { - fprintf(stderr, "%s failed %d\n", *args, stat); - exit(1); - } + if(stat) fail(1, "%s failed %d\n", *args, stat); } } @@ -137,45 +134,27 @@ static int cached_file(int z, int x, int y, char *dir, char file[4096]) { if(max < y) y = 0; char d[4096]; int n = snprintf(d, sizeof(d), "%s/%d/%d", dir, z, x); - if(n < 0 || sizeof(d) < n) { - fprintf(stderr, "directory path error\n"); - exit(1); - } + if(n < 0 || sizeof(d) < n) fail(1, "directory path error\n"); n = snprintf(file, 4096, "%s/%d.png", d, y); - if(n < 0 || 4096 < n) { - fprintf(stderr, "file path error\n"); - exit(1); - } + if(n < 0 || 4096 < n) fail(1, "file path error\n"); if(probe(file)) return 1; { char url[4096]; int n = snprintf(url, sizeof(url), "https://tile.openstreetmap.org/%d/%d/%d.png", z, x, y); - if(n < 0 || sizeof(url) < n) { - fprintf(stderr, "url error\n"); - exit(1); - } + if(n < 0 || sizeof(url) < n) fail(1, "url error\n"); char *args1[] = {"mkdir", "-p", d, NULL}; run(args1); char *args2[] = {"curl", "-s", "-o", file, url, NULL}; run(args2); - if(!probe(file)) { - fprintf(stderr, "cached file '%s' not found\n", file); - exit(1); - } + if(!probe(file)) fail(1, "cached file '%s' not found\n", file); } return 1; } static void screen_size(int *w, int *h) { FILE *f = fopen("/sys/class/graphics/fb0/virtual_size", "r"); - if(!f) { - fprintf(stderr, "unknown screen\n"); - exit(1); - } - if(2 != fscanf(f, "%d,%d", w, h)) { - fprintf(stderr, "unknown screen size\n"); - exit(1); - } + if(!f) fail(1, "unknown screen\n"); + if(2 != fscanf(f, "%d,%d", w, h)) fail(1, "unknown screen size\n"); fclose(f); } @@ -204,7 +183,7 @@ static unsigned char key(int timeout) { return c; } -static int tty(int argc, char *argv[]) { +static int main_tty(int argc, char *argv[]) { int width, height; screen_size(&width, &height); // popen r stty size -> w h @@ -214,10 +193,7 @@ static int tty(int argc, char *argv[]) { run(args0b); atexit(cleanup); char *dir = argv[0]; - if(!dir) { - fprintf(stderr, "unexpected dir %s\n", dir); - return 1; - } + if(!dir) fail(1, "unexpected dir %s\n", dir); int z0 = zoom_from_string(argv[1]); double a0 = lat_from_string(argv[2]); double o0 = lon_from_string(argv[3]); @@ -227,10 +203,7 @@ static int tty(int argc, char *argv[]) { int y = lat2tiley(a, z); for(;;) { FILE *p = popen("w3mimgdisplay", "w"); - if(!p) { - fprintf(stderr, "popen w3mimgdisplay failed\n"); - exit(1); - } + if(!p) fail(1, "popen w3mimgdisplay failed\n"); int full = 1; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { @@ -362,14 +335,14 @@ static int tty(int argc, char *argv[]) { } } -static int version(int argc, char *argv[]) { +static int main_version(int argc, char *argv[]) { printf("%s\n", #include "VERSION" ); return 0; } -static int help(int argc, char *argv[]) { +static int main_help(int argc, char *argv[]) { printf("usage: osmtile cmd args\n"); printf(" lat zoom y\n"); printf(" lon zoom x\n"); @@ -383,26 +356,25 @@ static int help(int argc, char *argv[]) { int main(int argc, char *argv[]) { char *cmd = argv[1]; - if(!cmd) goto fail; - static const struct { - char *cmd; - int (*fn)(int argc, char *argv[]); - } dispatch[] = { - {"lat", lat}, - {"lon", lon}, - {"x", x}, - {"y", y}, - {"tty", tty}, - {"--version", version}, - {"--help", help}, - {NULL} - }; - for(int i = 0; dispatch[i].cmd; i++) { - if(!strcmp(dispatch[i].cmd, cmd)) { - return dispatch[i].fn(argc - 2, &argv[2]); + if(cmd) { + static const struct { + char *cmd; + int (*fn)(int argc, char *argv[]); + } dispatch[] = { + {"lat", main_lat}, + {"lon", main_lon}, + {"x", main_x}, + {"y", main_y}, + {"tty", main_tty}, + {"--version", main_version}, + {"--help", main_help}, + {NULL} + }; + for(int i = 0; dispatch[i].cmd; i++) { + if(!strcmp(dispatch[i].cmd, cmd)) { + return dispatch[i].fn(argc - 2, &argv[2]); + } } } - fail: - fprintf(stderr, "unexpected command %s\n", cmd); - return 1; + fail(1, "unexpected command %s\n", cmd); }