Browse Source

Cleanup XML parser function

Serj Kalichev 8 years ago
parent
commit
e0de2286ce
1 changed files with 74 additions and 68 deletions
  1. 74 68
      clish/shell/shell_xml.c

+ 74 - 68
clish/shell/shell_xml.c

@@ -82,12 +82,14 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 	char *buffer;
 	char *dirname;
 	char *saveptr = NULL;
-	int res = 0;
-	clish_xmldoc_t *doc;
+	int res = -1;
+	clish_xmldoc_t *doc = NULL;
+	DIR *dir;
 
 #ifdef HAVE_LIB_LIBXSLT
-	clish_xslt_t *xslt;
+	clish_xslt_t *xslt = NULL;
 
+	/* Load global XSLT stylesheet */
 	if (xslt_path) {
 		xslt = clish_xslt_read(xslt_path);
 		if (!clish_xslt_is_valid(xslt)) {
@@ -98,19 +100,17 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 	}
 #endif
 
-	/* use the default path */
+	/* Use the default path */
 	if (!path)
 		path = default_path;
-	/* take a copy of the path */
 	buffer = lub_system_tilde_expand(path);
 
-	/* now loop though each directory */
+	/* Loop though each directory */
 	for (dirname = strtok_r(buffer, ";", &saveptr);
 		dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
-		DIR *dir;
 		struct dirent *entry;
 
-		/* search this directory for any XML files */
+		/* Search this directory for any XML files */
 		dir = opendir(dirname);
 		if (NULL == dir) {
 #ifdef DEBUG
@@ -122,83 +122,89 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 		}
 		for (entry = readdir(dir); entry; entry = readdir(dir)) {
 			const char *extension = strrchr(entry->d_name, '.');
-			/* check the filename */
-			if ((NULL != extension) &&
-				(0 == strcmp(".xml", extension))) {
-				char *filename = NULL;
+			char *filename = NULL;
+			clish_xmlnode_t *root;
+			int r;
+
+			/* Check the filename */
+			if (!extension || strcmp(".xml", extension))
+				continue;
 
-				/* build the filename */
-				lub_string_cat(&filename, dirname);
-				lub_string_cat(&filename, "/");
-				lub_string_cat(&filename, entry->d_name);
+			/* Build the filename */
+			lub_string_cat(&filename, dirname);
+			lub_string_cat(&filename, "/");
+			lub_string_cat(&filename, entry->d_name);
 
 #ifdef DEBUG
-				fprintf(stderr, "Parse XML-file: %s\n", filename);
+			fprintf(stderr, "Parse XML-file: %s\n", filename);
 #endif
-				/* Load this XML file */
-				doc = clish_xmldoc_read(filename);
-				if (clish_xmldoc_is_valid(doc)) {
-					clish_xmlnode_t *root;
+			/* Load current XML file */
+			doc = clish_xmldoc_read(filename);
+			if (!clish_xmldoc_is_valid(doc)) {
+				int errcaps = clish_xmldoc_error_caps(doc);
+				printf("Unable to open file '%s'", filename);
+				if ((errcaps & CLISH_XMLERR_LINE) == CLISH_XMLERR_LINE)
+					printf(", at line %d", clish_xmldoc_get_err_line(doc));
+				if ((errcaps & CLISH_XMLERR_COL) == CLISH_XMLERR_COL)
+					printf(", at column %d", clish_xmldoc_get_err_col(doc));
+				if ((errcaps & CLISH_XMLERR_DESC) == CLISH_XMLERR_DESC)
+					printf(", message is %s", clish_xmldoc_get_err_msg(doc));
+				printf("\n");
+				goto error;
+			}
 #ifdef HAVE_LIB_LIBXSLT
-					clish_xmldoc_t *tmp;
-					/* Use embedded stylesheet if stylesheet
-					 * filename is not specified.
-					 */
-					if (!xslt_path)
-						xslt = clish_xslt_read_embedded(doc);
-
-					if (clish_xslt_is_valid(xslt)) {
-						tmp = clish_xslt_apply(doc, xslt);
-						if (!clish_xmldoc_is_valid(tmp)) {
-							fprintf(stderr, CLISH_XML_ERROR_STR"Can't load XSLT file %s\n", xslt_path);
-							res = -1;
-						} else {
-							clish_xmldoc_release(doc);
-							doc = tmp;
-						}
-					}
-
-					if (!xslt_path && clish_xslt_is_valid(xslt))
-						clish_xslt_release(xslt);
-#endif
-					if (!res) {
-						root = clish_xmldoc_get_root(doc);
-						res = process_node(this, root, NULL);
-					}
-				} else {
-					int errcaps = clish_xmldoc_error_caps(doc);
-					printf("Unable to open file '%s'", filename);
-					if ((errcaps & CLISH_XMLERR_LINE) == CLISH_XMLERR_LINE)
-							printf(", at line %d", clish_xmldoc_get_err_line(doc));
-						if ((errcaps & CLISH_XMLERR_COL) == CLISH_XMLERR_COL)
-						printf(", at column %d", clish_xmldoc_get_err_col(doc));
-					if ((errcaps & CLISH_XMLERR_DESC) == CLISH_XMLERR_DESC)
-						printf(", message is %s", clish_xmldoc_get_err_msg(doc));
-					printf("\n");
-					res = -1;
+			/* Use embedded stylesheet if stylesheet
+			 * filename is not specified.
+			 */
+			if (!xslt_path)
+				xslt = clish_xslt_read_embedded(doc);
+
+			if (clish_xslt_is_valid(xslt)) {
+				clish_xmldoc_t *tmp = NULL;
+				tmp = clish_xslt_apply(doc, xslt);
+				if (!clish_xmldoc_is_valid(tmp)) {
+					fprintf(stderr, CLISH_XML_ERROR_STR"Can't load XSLT file %s\n", xslt_path);
+					goto error;
 				}
 				clish_xmldoc_release(doc);
+				doc = tmp;
+			}
 
-				/* Error message */
-				if (res)
-					fprintf(stderr, CLISH_XML_ERROR_STR"File %s\n",
-						filename);
-				/* release the resource */
+			if (!xslt_path && clish_xslt_is_valid(xslt))
+				clish_xslt_release(xslt);
+#endif
+			root = clish_xmldoc_get_root(doc);
+			r = process_node(this, root, NULL);
+			clish_xmldoc_release(doc);
+
+			/* Error message */
+			if (r) {
+				fprintf(stderr, CLISH_XML_ERROR_STR"File %s\n",
+					filename);
 				lub_string_free(filename);
-			}
-			if (res)
 				goto error;
+			}
+			lub_string_free(filename);
 		}
-		/* all done for this directory */
 		closedir(dir);
-		if (res)
-			goto error;
 	}
 
+/* To don't free memory twice on cleanup */
+#ifdef HAVE_LIB_LIBXSLT
+	if (!xslt_path)
+		xslt = NULL;
+#endif
+	doc = NULL;
+	dir = NULL;
+	res = 0; /* Success */
 error:
 	lub_string_free(buffer);
+	if (dir)
+		closedir(dir);
+	if (clish_xmldoc_is_valid(doc))
+		clish_xmldoc_release(doc);
 #ifdef HAVE_LIB_LIBXSLT
-	if (xslt_path)
+	if (clish_xslt_is_valid(xslt))
 		clish_xslt_release(xslt);
 #endif