Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/jshn.c
diff options
context:
space:
mode:
authorPetr Štetiar <ynezz@true.cz>2019-11-24 00:27:46 +0300
committerPetr Štetiar <ynezz@true.cz>2019-11-24 15:26:58 +0300
commit93848ec96dc58bbeb4bb3ed84fa5848ce5a04667 (patch)
treee254fb016aa36a35dcad61cc520421952b4c83d6 /jshn.c
parent9b6ede0e5312071400e6b009c6b92413061bbfaa (diff)
jshn: refactor main into smaller pieces
Turn longer switch cases into separate functions in order to make it easier to follow. Don't return from the cases as it makes future cleaning up harder. Signed-off-by: Petr Štetiar <ynezz@true.cz>
Diffstat (limited to 'jshn.c')
-rw-r--r--jshn.c107
1 files changed, 68 insertions, 39 deletions
diff --git a/jshn.c b/jshn.c
index 0aa120c..1efe254 100644
--- a/jshn.c
+++ b/jshn.c
@@ -333,6 +333,61 @@ static int avl_strcmp_var(const void *k1, const void *k2, void *ptr)
return c1 - c2;
}
+static int jshn_parse_file(const char *path)
+{
+ struct stat sb;
+ int ret = 0;
+ char *fbuf;
+ int fd;
+
+ if ((fd = open(path, O_RDONLY)) == -1) {
+ fprintf(stderr, "Error opening %s\n", path);
+ return 3;
+ }
+
+ if (fstat(fd, &sb) == -1) {
+ fprintf(stderr, "Error getting size of %s\n", path);
+ close(fd);
+ return 3;
+ }
+
+ if (!(fbuf = malloc(sb.st_size))) {
+ fprintf(stderr, "Error allocating memory for %s\n", path);
+ close(fd);
+ return 3;
+ }
+
+ if (read(fd, fbuf, sb.st_size) != sb.st_size) {
+ fprintf(stderr, "Error reading %s\n", path);
+ free(fbuf);
+ close(fd);
+ return 3;
+ }
+
+ ret = jshn_parse(fbuf);
+ free(fbuf);
+ close(fd);
+
+ return ret;
+}
+
+static int jshn_format_file(const char *path, bool no_newline, bool indent)
+{
+ FILE *fp = NULL;
+ int ret = 0;
+
+ fp = fopen(path, "w");
+ if (!fp) {
+ fprintf(stderr, "Error opening %s\n", path);
+ return 3;
+ }
+
+ ret = jshn_format(no_newline, indent, fp);
+ fclose(fp);
+
+ return ret;
+}
+
int main(int argc, char **argv)
{
extern char **environ;
@@ -340,12 +395,8 @@ int main(int argc, char **argv)
bool indent = false;
struct env_var *vars;
int i;
+ int ret = 0;
int ch;
- int fd;
- FILE *fp = NULL;
- struct stat sb;
- char *fbuf;
- int ret;
avl_init(&env_vars, avl_strcmp_var, false, NULL);
for (i = 0; environ[i]; i++);
@@ -374,43 +425,17 @@ int main(int argc, char **argv)
var_prefix_len = strlen(var_prefix);
break;
case 'r':
- return jshn_parse(optarg);
+ ret = jshn_parse(optarg);
+ goto exit;
case 'R':
- if ((fd = open(optarg, O_RDONLY)) == -1) {
- fprintf(stderr, "Error opening %s\n", optarg);
- return 3;
- }
- if (fstat(fd, &sb) == -1) {
- fprintf(stderr, "Error getting size of %s\n", optarg);
- close(fd);
- return 3;
- }
- if (!(fbuf = malloc(sb.st_size))) {
- fprintf(stderr, "Error allocating memory for %s\n", optarg);
- close(fd);
- return 3;
- }
- if (read(fd, fbuf, sb.st_size) != sb.st_size) {
- fprintf(stderr, "Error reading %s\n", optarg);
- free(fbuf);
- close(fd);
- return 3;
- }
- ret = jshn_parse(fbuf);
- free(fbuf);
- close(fd);
- return ret;
+ ret = jshn_parse_file(optarg);
+ goto exit;
case 'w':
- return jshn_format(no_newline, indent, stdout);
+ ret = jshn_format(no_newline, indent, stdout);
+ goto exit;
case 'o':
- fp = fopen(optarg, "w");
- if (!fp) {
- fprintf(stderr, "Error opening %s\n", optarg);
- return 3;
- }
- jshn_format(no_newline, indent, fp);
- fclose(fp);
- return 0;
+ ret = jshn_format_file(optarg, no_newline, indent);
+ goto exit;
case 'n':
no_newline = true;
break;
@@ -421,5 +446,9 @@ int main(int argc, char **argv)
return usage(argv[0]);
}
}
+
return usage(argv[0]);
+
+exit:
+ return ret;
}