wgt-info: Enforce lower case ids and version
[src/app-framework-main.git] / src / wgt-info.c
index 61779e0..089a563 100644 (file)
@@ -1,5 +1,7 @@
 /*
- Copyright 2015 IoT.bzh
+ Copyright 2015, 2016 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
@@ -18,7 +20,8 @@
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
-#include <syslog.h>
+#include <ctype.h>
+
 #include <libxml/tree.h>
 
 #include "verbose.h"
@@ -73,6 +76,58 @@ static xmlChar *optcontent(xmlNodePtr node)
        return node ? xmlNodeGetContent(node) : NULL;
 }
 
+static char *mkver(char *version)
+{
+       unsigned int lver;
+       char c, *r;
+       if (version) {
+               c = version[lver = 0];
+               while(c && c != ' ' && c != '.')
+                       c = version[++lver];
+               if (c == '.') {
+                       c = version[++lver];
+                       while(c && c != ' ' && c != '.')
+                               c = version[++lver];
+               }
+               r = malloc(lver + 1);
+               if (r) {
+                       memcpy(r, version, lver);
+                       r[lver] = 0;
+                       return r;
+               }
+       }
+       return NULL;
+}
+
+static char *mkidaver(char *id, char *ver)
+{
+       size_t lid, lver;
+       char *r;
+       if (id && ver) {
+               lid = strlen(id);
+               lver = strlen(ver);
+               r = malloc(2 + lid + lver);
+               if (r) {
+                       memcpy(r, id, lid);
+                       r[lid] = '@';
+                       memcpy(r + lid + 1, ver, lver);
+                       r[lid + lver + 1] = 0;
+                       return r;
+               }
+       }
+       return NULL;
+}
+
+static void make_lowercase(char *s)
+{
+       if (s) {
+               while(*s) {
+                       *s = (char)tolower(*s);
+                       s++;
+               }
+       }
+}
+
 static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, int want_preferences)
 {
        xmlNodePtr node, pnode;
@@ -83,12 +138,16 @@ static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, i
 
        node = wgt_config_widget();
        if (!node) {
-               warning("no widget");
+               WARNING("no widget");
                errno = EINVAL;
                return -1;
        }
        desc->id = xmlGetProp(node, wgt_config_string_id);
+       make_lowercase(desc->id);
        desc->version = xmlGetProp(node, wgt_config_string_version);
+       desc->ver = mkver(desc->version);
+       make_lowercase(desc->ver);
+       desc->idaver = mkidaver(desc->id, desc->ver);
        desc->width = getpropnum(node, wgt_config_string_width, 0);
        desc->height = getpropnum(node, wgt_config_string_height, 0);
        desc->viewmodes = xmlGetProp(node, wgt_config_string_viewmodes);
@@ -113,7 +172,7 @@ static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, i
        node = wgt_config_content();
        desc->content_src = optprop(node, wgt_config_string_src);
        if (node && desc->content_src == NULL) {
-               warning("content without src");
+               WARNING("content without src");
                errno = EINVAL;
                return -1;
        }
@@ -137,7 +196,7 @@ static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, i
                        *icontail = icon;
 
                        if (icon->src == NULL) {
-                               warning("icon without src");
+                               WARNING("icon without src");
                                errno = EINVAL;
                                return -1;
                        }
@@ -163,7 +222,7 @@ static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, i
                        *featuretail = feature;
 
                        if (feature->name == NULL) {
-                               warning("feature without name");
+                               WARNING("feature without name");
                                errno = EINVAL;
                                return -1;
                        }
@@ -183,7 +242,7 @@ static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, i
                                *paramtail = param;
 
                                if (param->name == NULL || param->value == NULL) {
-                                       warning("param without name or value");
+                                       WARNING("param without name or value");
                                        errno = EINVAL;
                                        return -1;
                                }
@@ -214,7 +273,7 @@ static int fill_desc(struct wgt_desc *desc, int want_icons, int want_features, i
                        preference->next = NULL;
 
                        if (preference->name == NULL) {
-                               warning("preference without name");
+                               WARNING("preference without name");
                                errno = EINVAL;
                                return -1;
                        }
@@ -235,6 +294,8 @@ static void free_desc(struct wgt_desc *desc)
 
        xmlFree(desc->id);
        xmlFree(desc->version);
+       free(desc->ver);
+       free(desc->idaver);
        xmlFree(desc->viewmodes);
        xmlFree(desc->defaultlocale);
        xmlFree(desc->name);
@@ -287,9 +348,11 @@ static void dump_desc(struct wgt_desc *desc, FILE *f, const char *prefix)
        struct wgt_desc_param *param;
 
        if (desc->id) fprintf(f, "%sid: %s\n", prefix, desc->id);
+       if (desc->version) fprintf(f, "%sversion: %s\n", prefix, desc->version);
+       if (desc->ver) fprintf(f, "%sver: %s\n", prefix, desc->ver);
+       if (desc->idaver) fprintf(f, "%sidaver: %s\n", prefix, desc->idaver);
        if (desc->width) fprintf(f, "%swidth: %d\n", prefix, desc->width);
        if (desc->height) fprintf(f, "%sheight: %d\n", prefix, desc->height);
-       if (desc->version) fprintf(f, "%sversion: %s\n", prefix, desc->version);
        if (desc->viewmodes) fprintf(f, "%sviewmodes: %s\n", prefix, desc->viewmodes);
        if (desc->defaultlocale) fprintf(f, "%sdefaultlocale: %s\n", prefix, desc->defaultlocale);
        if (desc->name) fprintf(f, "%sname: %s\n", prefix, desc->name);
@@ -416,7 +479,7 @@ void wgt_info_dump(struct wgt_info *ifo, int fd, const char *prefix)
        assert(ifo);
        f = fdopen(fd, "w");
        if (f == NULL)
-               warning("can't fdopen in wgt_info_dump");
+               WARNING("can't fdopen in wgt_info_dump");
        else {
                dump_desc(&ifo->desc, f, prefix);
                fclose(f);