gw_info: move code to where it belongs
[monky] / src / core.c
index 31a0bea..6decbd3 100644 (file)
@@ -772,79 +772,12 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
 
 #ifdef __linux__
        END OBJ_ARG(i2c, 0, "i2c needs arguments")
-               char buf1[64], buf2[64];
-               float factor, offset;
-               int n, found = 0;
-
-#define HWMON_RESET() {\
-               buf1[0] = 0; \
-               factor = 1.0; \
-               offset = 0.0; }
-
-               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
-
-               if (!found) {
-                       NORM_ERR("i2c failed to parse arguments");
-                       obj->type = OBJ_text;
-                       return NULL;
-               }
-               DBGP("parsed i2c args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
-               obj->data.sysfs.fd = open_i2c_sensor((*buf1) ? buf1 : 0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-               strncpy(obj->data.sysfs.type, buf2, 63);
-               obj->data.sysfs.factor = factor;
-               obj->data.sysfs.offset = offset;
-
+               parse_i2c_sensor(obj, arg);
        END OBJ_ARG(platform, 0, "platform needs arguments")
-               char buf1[64], buf2[64];
-               float factor, offset;
-               int n, found = 0;
-
-               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
-
-               if (!found) {
-                       NORM_ERR("platform failed to parse arguments");
-                       obj->type = OBJ_text;
-                       return NULL;
-               }
-               DBGP("parsed platform args: '%s' '%s' %d %f %f", buf1, buf2, n, factor, offset);
-               obj->data.sysfs.fd = open_platform_sensor((*buf1) ? buf1 : 0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-               strncpy(obj->data.sysfs.type, buf2, 63);
-               obj->data.sysfs.factor = factor;
-               obj->data.sysfs.offset = offset;
-
+               parse_platform_sensor(obj, arg);
        END OBJ_ARG(hwmon, 0, "hwmon needs argumanets")
-               char buf1[64], buf2[64];
-               float factor, offset;
-               int n, found = 0;
-
-               if (sscanf(arg, "%63s %d %f %f", buf2, &n, &factor, &offset) == 4) found = 1; else HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d %f %f", buf1, buf2, &n, &factor, &offset) == 5) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %63s %d", buf1, buf2, &n) == 3) found = 1; else if (!found) HWMON_RESET();
-               if (!found && sscanf(arg, "%63s %d", buf2, &n) == 2) found = 1; else if (!found) HWMON_RESET();
-
-#undef HWMON_RESET
-
-               if (!found) {
-                       NORM_ERR("hwmon failed to parse arguments");
-                       obj->type = OBJ_text;
-                       return NULL;
-               }
-               DBGP("parsed hwmon args: '%s' '%s' %d %f %f\n", buf1, buf2, n, factor, offset);
-               obj->data.sysfs.fd = open_hwmon_sensor((*buf1) ? buf1 : 0, buf2, n,
-                               &obj->data.sysfs.arg, obj->data.sysfs.devtype);
-               strncpy(obj->data.sysfs.type, buf2, 63);
-               obj->data.sysfs.factor = factor;
-               obj->data.sysfs.offset = offset;
-
-#endif /* !__OpenBSD__ */
+               parse_hwmon_sensor(obj, arg);
+#endif /* __linux__ */
 
        END
        /* we have four different types of top (top, top_mem, top_time and top_io). To
@@ -1512,138 +1445,23 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
 #endif
 #ifdef EVE
        END OBJ_ARG(eve, 0, "eve needs arguments: <userid> <apikey> <characterid>")
-               int argc;
-               char *userid = (char *) malloc(20 * sizeof(char));
-               char *apikey = (char *) malloc(64 * sizeof(char));
-               char *charid = (char *) malloc(20 * sizeof(char));
-
-               argc = sscanf(arg, "%20s %64s %20s", userid, apikey, charid);
-               obj->data.eve.charid = charid;
-               obj->data.eve.userid = userid;
-               obj->data.eve.apikey = apikey;
-
-               init_eve();
+               scan_eve(obj, arg);
 #endif
 #ifdef HAVE_CURL
        END OBJ_ARG(curl, 0, "curl needs arguments: <uri> <interval in minutes>")
-               int argc;
-               float interval = 0;
-               char *uri = (char *) malloc(128 * sizeof(char));
-
-               argc = sscanf(arg, "%127s %f", uri, &interval);
-               if (argc == 2) {
-                       obj->data.curl.uri = uri;
-                       obj->data.curl.interval = interval > 0 ? interval * 60 : 15*60;
-               } else {
-                       NORM_ERR("wrong number of arguments for $curl");
-               }
+               curl_parse_arg(obj, arg);
 #endif
 #ifdef RSS
        END OBJ_ARG(rss, 0, "rss needs arguments: <uri> <interval in minutes> <action> [act_par] [spaces in front]")
-               float interval = 0;
-               int argc, act_par = 0;
-               unsigned int nrspaces = 0;
-               char *uri = (char *) malloc(128 * sizeof(char));
-               char *action = (char *) malloc(64 * sizeof(char));
-
-               argc = sscanf(arg, "%127s %f %63s %d %u", uri, &interval, action,
-                               &act_par, &nrspaces);
-               if (argc >= 3) {
-                       obj->data.rss.uri = uri;
-                       obj->data.rss.interval = interval > 0 ? interval * 60 : 15*60;
-                       obj->data.rss.action = action;
-                       obj->data.rss.act_par = act_par;
-                       obj->data.rss.nrspaces = nrspaces;
-               } else {
-                       NORM_ERR("wrong number of arguments for $rss");
-               }
+               rss_scan_arg(obj, arg);
 #endif
 #ifdef WEATHER
        END OBJ_ARG(weather, 0, "weather needs arguments: <uri> <locID> <data_type> [interval in minutes]")
-               int argc;
-               float interval = 0;
-               char *locID = (char *) malloc(9 * sizeof(char));
-               char *uri = (char *) malloc(128 * sizeof(char));
-               char *data_type = (char *) malloc(32 * sizeof(char));
-
-               argc = sscanf(arg, "%119s %8s %31s %f", uri, locID, data_type, &interval);
-
-               if (argc >= 3) {
-                       if (process_weather_uri(uri, locID, 0)) {
-                               free(data_type);
-                               free(uri);
-                               free(locID);
-                               CRIT_ERR(obj, free_at_crash, \
-                                               "could not recognize the weather uri");
-                       }
-
-                       obj->data.weather.uri = uri;
-                       obj->data.weather.data_type = data_type;
-
-                       /* Limit the data retrieval interval to half hour min */
-                       if (interval < 30) {
-                               interval = 30;
-                       }
-
-                       /* Convert to seconds */
-                       obj->data.weather.interval = interval * 60;
-                       free(locID);
-
-                       DBGP("weather: fetching %s from %s every %d seconds", \
-                                       data_type, uri, obj->data.weather.interval);
-               } else {
-                       free(data_type);
-                       free(uri);
-                       free(locID);
-                       CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather");
-               }
+               scan_weather_arg(obj, arg, free_at_crash);
 #endif
 #ifdef XOAP
        END OBJ_ARG(weather_forecast, 0, "weather_forecast needs arguments: <uri> <locID> <day> <data_type> [interval in minutes]")
-               int argc;
-               unsigned int day;
-               float interval = 0;
-               char *locID = (char *) malloc(9 * sizeof(char));
-               char *uri = (char *) malloc(128 * sizeof(char));
-               char *data_type = (char *) malloc(32 * sizeof(char));
-
-               argc = sscanf(arg, "%119s %8s %1u %31s %f", uri, locID, &day, data_type, &interval);
-
-               if (argc >= 4) {
-                       if (process_weather_uri(uri, locID, 1)) {
-                               free(data_type);
-                               free(uri);
-                               free(locID);
-                               CRIT_ERR(obj, free_at_crash, \
-                                               "could not recognize the weather forecast uri");
-                       }
-
-                       obj->data.weather_forecast.uri = uri;
-                       obj->data.weather_forecast.data_type = data_type;
-
-                       /* Limit the day between 0 (today) and FORECAST_DAYS */
-                       if (day >= FORECAST_DAYS) {
-                               day = FORECAST_DAYS-1;
-                       }
-                       obj->data.weather_forecast.day = day;
-
-                       /* Limit the data retrieval interval to 3 hours and an half */
-                       if (interval < 210) {
-                               interval = 210;
-                       }
-
-                       /* Convert to seconds */
-                       obj->data.weather_forecast.interval = interval * 60;
-                       free(locID);
-
-                       DBGP("weather_forecast: fetching %s for day %d from %s every %d seconds", \
-                                data_type, day, uri, obj->data.weather_forecast.interval);
-               } else {
-                       free(data_type);
-                       free(uri);
-                       free(locID);
-                       CRIT_ERR(obj, free_at_crash, "wrong number of arguments for $weather_forecast");
-               }
+               scan_weather_forecast_arg(obj, arg, free_at_crash);
 #endif
 #ifdef HAVE_LUA
        END OBJ_ARG(lua, 0, "lua needs arguments: <function name> [function parameters]")
@@ -2159,14 +1977,7 @@ void free_text_objects(struct text_object *root, int internal)
                                free(data.ifblock.str);
                        case OBJ_gw_iface:
                        case OBJ_gw_ip:
-                               if (info.gw_info.iface) {
-                                       free(info.gw_info.iface);
-                                       info.gw_info.iface = 0;
-                               }
-                               if (info.gw_info.ip) {
-                                       free(info.gw_info.ip);
-                                       info.gw_info.ip = 0;
-                               }
+                               free_gateway_info();
                                break;
                        case OBJ_ioscheduler:
                                if(data.s)