# HG changeset patch # User Matti Hamalainen # Date 1688258674 -10800 # Node ID 0e60d6fdfe9f997f4b445173d73a92841c803508 # Parent a4afed6feeebc7011973ef6d83977f3d778649c0 Cleanups. diff -r a4afed6feeeb -r 0e60d6fdfe9f src/mapsearch.c --- a/src/mapsearch.c Wed Jun 21 01:46:51 2023 +0300 +++ b/src/mapsearch.c Sun Jul 02 03:44:34 2023 +0300 @@ -151,8 +151,7 @@ "::[::]\n" "World offsets are optional and default to 0, 0 if not specified.\n" "\n" - "All the map offsets are relative to world origin coordinates, which are 0,0 by default.\n" - "-w 8192:8192\n" + "All the map offsets are relative to world origin, which is 0,0 by default.\n" ); } @@ -218,13 +217,14 @@ } -bool mapParseCoordPair(const char *str, int *xc, int *yc) +bool argParseCoordPair(const char *str, int *xc, int *yc) { char *piece, *tmp, *fmt = th_strdup(str); - bool ret = false; + bool ok = false; if ((piece = strchr(fmt, ':')) == NULL) - goto err; + goto out; + *piece++ = 0; tmp = th_strdup_trim(fmt, TH_TRIM_BOTH); @@ -235,32 +235,46 @@ *yc = atoi(tmp); th_free(tmp); - ret = true; + ok = true; -err: +out: th_free(fmt); - return ret; + return ok; } -bool mapParseMapSpec(const char *str, MAPInfoCtx *info) +bool argParseMapSpec(const char *cspec) { - char *piece, *start, *fmt = th_strdup(str); - bool ret = false; + MAPInfoCtx *info; + char *piece, *start, *spec = th_strdup(cspec); + bool ok = false; + if (optNMaps >= SET_MAX_MAPS) + { + mapERR("Maximum number of map specs already specified.\n"); + goto out; + } + + info = &optMaps[optNMaps]; memset(info, 0, sizeof(MAPInfoCtx)); // Check for map filename end - if ((piece = strchr(fmt, ':')) == NULL) - goto err; + if ((piece = strchr(spec, ':')) == NULL) + { + mapERR("Invalid map spec '%s': missing map filename separator ':'.\n", cspec); + goto out; + } *piece++ = 0; - info->mapFilename = th_strdup_trim(fmt, TH_TRIM_BOTH); + info->mapFilename = th_strdup_trim(spec, TH_TRIM_BOTH); start = piece; // Check for loc filename end if ((piece = strchr(start, ':')) == NULL) - goto err; + { + mapERR("Invalid map spec '%s': missing loc filename separator ':'.\n", cspec); + goto out; + } *piece++ = 0; info->locFile.filename = th_strdup_trim(start, TH_TRIM_BOTH); @@ -274,14 +288,17 @@ // Get world X/Y offsets, if any if (piece != NULL && - !mapParseCoordPair(piece, &info->locFile.xoffs, &info->locFile.yoffs)) - goto err; + !argParseCoordPair(piece, &info->locFile.xoffs, &info->locFile.yoffs)) + { + mapERR("Invalid map spec '%s': invalid coordinate offsets '%s'.\n", cspec, piece); + goto out; + } - ret = true; + ok = true; -err: - th_free(fmt); - return ret; +out: + th_free(spec); + return ok; } @@ -291,7 +308,7 @@ } -void mapFreeListenCtxR(MAPListenerCtx *ctx) +void mapFreeListenCtx(MAPListenerCtx *ctx) { if (ctx != NULL) { @@ -300,35 +317,38 @@ th_free(ctx->sslCertFile); th_free(ctx->sslKeyFile); th_free(ctx->sslCAFile); + th_free(ctx); } } -void mapFreeListenCtx(MAPListenerCtx *ctx) +bool argParseListenerSpec(const char *cspec) { - mapFreeListenCtxR(ctx); - th_free(ctx); -} - + MAPListenerCtx *ctx = NULL; + char *start, *end, *flags, *interface, + *port = NULL, *spec = th_strdup(cspec); + bool ok = false; -MAPListenerCtx *mapParseListenerSpec(const char *cfmt) -{ - char *start, *end, *flags, *port = NULL, - *interface, *fmt = th_strdup(cfmt); - bool ret = false; - MAPListenerCtx *ctx; + if (optNListenTo >= SET_MAX_LISTEN) + { + mapERR("Maximum number of listener specs already specified.\n"); + goto out; + } if ((ctx = mapNewListenCtx()) == NULL) + { + mapERR("Could not allocate memory for listener spec!\n"); goto out; + } - interface = fmt; + interface = spec; if (*interface == '[') { // IPv6 IP address is handled in a special case interface++; if ((end = strchr(interface, ']')) == NULL) { - mapERR("Invalid IPv6 IP address '%s'.\n", cfmt); + mapERR("Invalid IPv6 IP address '%s'.\n", cspec); goto out; } *end++ = 0; @@ -348,7 +368,7 @@ // Find port number separator if (end == NULL || *end != ':') { - mapERR("Missing listening port in '%s'.\n", cfmt); + mapERR("Missing listening port in '%s'.\n", cspec); goto out; } *end++ = 0; @@ -381,12 +401,12 @@ // Get port number if ((port = th_strdup_trim(start, TH_TRIM_BOTH)) == NULL) { - mapERR("Missing listening port in '%s'.\n", cfmt); + mapERR("Missing listening port in '%s'.\n", cspec); goto out; } if ((ctx->port = atoi(port)) < 1) { - mapERR("Invalid listening port %d in '%s'.\n", ctx->port, cfmt); + mapERR("Invalid listening port %d in '%s'.\n", ctx->port, cspec); goto out; } @@ -453,17 +473,52 @@ goto out; } } - ret = true; + ok = true; out: - th_free(fmt); + th_free(spec); th_free(port); - if (ret) - return ctx; + if (ok) + { + optListenTo[optNListenTo++] = ctx; + return true; + } mapFreeListenCtx(ctx); - return NULL; + return false; +} + + +bool argParseUID(const char *str, int *val) +{ + if (sscanf(str, "%d", val) != 1) + { + struct passwd *info = getpwnam(str); + if (info == NULL) + { + mapERR("Invalid UID '%s'.\n", str); + return false; + } + *val = info->pw_uid; + } + return true; +} + + +bool argParseGID(const char *str, int *val) +{ + if (sscanf(str, "%d", val) != 1) + { + struct group *info = getgrnam(str); + if (info == NULL) + { + mapERR("Invalid GID '%s'.\n", str); + return false; + } + *val = info->gr_gid; + } + return true; } @@ -481,27 +536,14 @@ break; case 2: - if (optNListenTo < SET_MAX_LISTEN) - { - MAPListenerCtx *ctx; - if ((ctx = mapParseListenerSpec(optArg)) != NULL) - optListenTo[optNListenTo++] = ctx; - else - return false; - } - else - { - mapERR("Maximum number of listener specs already specified.\n"); - return false; - } - break; + return argParseListenerSpec(optArg); case 3: optSSLCipherList = optArg; break; case 5: - if (!mapParseCoordPair(optArg, &optWorldXC, &optWorldYC)) + if (!argParseCoordPair(optArg, &optWorldXC, &optWorldYC)) { mapERR("Invalid world origin coordinates '%s'.\n", optArg); return false; @@ -525,32 +567,10 @@ break; case 7: - if (sscanf(optArg, "%d", &optUID) != 1) - { - struct passwd *info = getpwnam(optArg); - if (info != NULL) - optUID = info->pw_uid; - else - { - mapERR("Invalid UID '%s'.\n", optArg); - return false; - } - } - break; + return argParseUID(optArg, &optUID); case 8: - if (sscanf(optArg, "%d", &optGID) != 1) - { - struct group *info = getgrnam(optArg); - if (info != NULL) - optUID = info->gr_gid; - else - { - mapERR("Invalid GID '%s'.\n", optArg); - return false; - } - } - break; + return argParseGID(optArg, &optGID); case 9: optLogFilename = optArg; @@ -571,22 +591,7 @@ bool argHandleFile(char *currArg) { - if (optNMaps < SET_MAX_MAPS) - { - if (!mapParseMapSpec(currArg, &optMaps[optNMaps])) - { - mapERR("Invalid map spec '%s'.\n", currArg); - return false; - } - - optNMaps++; - return true; - } - else - { - mapERR("Maximum number of map specs already specified.\n"); - return false; - } + return argParseMapSpec(currArg); } @@ -1788,6 +1793,8 @@ int main(int argc, char *argv[]) { + int res = 0; + // Initialize th_init("MapSearch", "Map Search WebSockets server", "0.8", NULL, NULL); th_verbosity = 0; @@ -1795,39 +1802,48 @@ memset(&optMaps, 0, sizeof(optMaps)); memset(&optListenTo, 0, sizeof(optListenTo)); - // Parse command line arguments - bool argsOK = th_args_process(argc, argv, optList, optListN, argHandleOpt, argHandleFile, 0); + - if (!argsOK) - return -2; + // Parse command line arguments + if (!th_args_process(argc, argv, optList, optListN, + argHandleOpt, argHandleFile, 0)) + { + res = -10; + goto out; + } if (optNMaps == 0) { + argShowHelp(); mapERR("No maps specified.\n"); - goto exit; + res = -10; + goto out; } if (optNListenTo == 0 && optTest == NULL) { mapERR("No listeners specified.\n"); - goto exit; + res = -10; + goto out; } // Initialize logging - if (optLogFilename != NULL && optTest == NULL) + if (optLogFilename != NULL && optTest == NULL && + (setLogFH = fopen(optLogFilename, "a")) == NULL) { - if ((setLogFH = fopen(optLogFilename, "a")) == NULL) - { - int err = th_get_error(); - mapERR("Could not open log file '%s' for writing (%d): %s\n", - err, th_error_str(err)); - goto exit; - } + res = th_get_error(); + mapERR("Could not open log file '%s' for writing: %s\n", + th_error_str(res)); + + goto out; } // Load maps if (!mapLoadMaps()) - goto exit; + { + res = -12; + goto out; + } // Check for test mode if (optTest != NULL) @@ -1870,7 +1886,7 @@ mapERR("Could not read test file.\n"); th_free(buf); - goto exit; + goto out; } // Initialize libwebsockets and create context @@ -1881,7 +1897,8 @@ mapERR("Could not allocate %d bytes of memory for LWS buffer.\n", SET_LWS_BUF_SIZE); - goto exit; + res = -13; + goto out; } // Setup log handler @@ -1916,7 +1933,8 @@ if ((setLWSContext = lws_create_context(&info)) == NULL) { mapERR("libwebsocket initialization failed.\n"); - goto exit; + res = -14; + goto out; } // Create listener LWS vhosts .. @@ -1977,7 +1995,8 @@ if ((ctx->vhost = lws_create_vhost(setLWSContext, &info)) == NULL) { mapERR("LWS vhost creation failed!\n"); - goto exit; + res = -15; + goto out; } } @@ -1993,7 +2012,8 @@ if (lws_uv_initloop(setLWSContext, NULL, 0)) { mapERR("lws_uv_initloop() failed.\n"); - goto exit; + res = -16; + goto out; } #endif @@ -2001,7 +2021,7 @@ mapMSG(1, "Waiting for connections...\n"); lws_service(setLWSContext, 0); -exit: +out: // Shutdown sequence mapMSG(1, "Server shutting down.\n"); @@ -2027,5 +2047,5 @@ if (setLogFH) fclose(setLogFH); - return 0; + return res; }