diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/plistutil.c | 188 |
1 files changed, 99 insertions, 89 deletions
diff --git a/tools/plistutil.c b/tools/plistutil.c index 56f25fd..84e7add 100644 --- a/tools/plistutil.c +++ b/tools/plistutil.c | |||
| @@ -31,6 +31,7 @@ | |||
| 31 | #include <stdlib.h> | 31 | #include <stdlib.h> |
| 32 | #include <string.h> | 32 | #include <string.h> |
| 33 | #include <sys/stat.h> | 33 | #include <sys/stat.h> |
| 34 | #include <getopt.h> | ||
| 34 | #include <errno.h> | 35 | #include <errno.h> |
| 35 | #ifndef _MSC_VER | 36 | #ifndef _MSC_VER |
| 36 | #include <unistd.h> | 37 | #include <unistd.h> |
| @@ -82,105 +83,114 @@ static void print_usage(int argc, char *argv[]) | |||
| 82 | 83 | ||
| 83 | static options_t *parse_arguments(int argc, char *argv[]) | 84 | static options_t *parse_arguments(int argc, char *argv[]) |
| 84 | { | 85 | { |
| 85 | int i = 0; | 86 | options_t *options = calloc(1, sizeof(options_t)); |
| 87 | if (!options) | ||
| 88 | return NULL; | ||
| 86 | 89 | ||
| 87 | options_t *options = (options_t*)calloc(1, sizeof(options_t)); | ||
| 88 | options->out_fmt = 0; | 90 | options->out_fmt = 0; |
| 89 | 91 | ||
| 90 | for (i = 1; i < argc; i++) | 92 | static struct option long_options[] = { |
| 93 | { "infile", required_argument, 0, 'i' }, | ||
| 94 | { "outfile", required_argument, 0, 'o' }, | ||
| 95 | { "format", required_argument, 0, 'f' }, | ||
| 96 | { "compact", no_argument, 0, 'c' }, | ||
| 97 | { "sort", no_argument, 0, 's' }, | ||
| 98 | { "print", required_argument, 0, 'p' }, | ||
| 99 | { "debug", no_argument, 0, 'd' }, | ||
| 100 | { "help", no_argument, 0, 'h' }, | ||
| 101 | { "version", no_argument, 0, 'v' }, | ||
| 102 | { 0, 0, 0, 0 } | ||
| 103 | }; | ||
| 104 | |||
| 105 | int c; | ||
| 106 | while ((c = getopt_long(argc, argv, "i:o:f:csp:dhv", long_options, NULL)) != -1) | ||
| 91 | { | 107 | { |
| 92 | if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) | 108 | switch (c) |
| 93 | { | ||
| 94 | if ((i + 1) == argc) | ||
| 95 | { | ||
| 96 | free(options); | ||
| 97 | return NULL; | ||
| 98 | } | ||
| 99 | options->in_file = argv[i + 1]; | ||
| 100 | i++; | ||
| 101 | continue; | ||
| 102 | } | ||
| 103 | else if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) | ||
| 104 | { | ||
| 105 | if ((i + 1) == argc) | ||
| 106 | { | ||
| 107 | free(options); | ||
| 108 | return NULL; | ||
| 109 | } | ||
| 110 | options->out_file = argv[i + 1]; | ||
| 111 | i++; | ||
| 112 | continue; | ||
| 113 | } | ||
| 114 | else if (!strcmp(argv[i], "--format") || !strcmp(argv[i], "-f")) | ||
| 115 | { | 109 | { |
| 116 | if ((i + 1) == argc) | 110 | case 'i': |
| 117 | { | 111 | if (!optarg || optarg[0] == '\0') { |
| 118 | free(options); | 112 | fprintf(stderr, "ERROR: --infile requires a filename or '-' for stdin\n"); |
| 119 | return NULL; | 113 | free(options); |
| 114 | return NULL; | ||
| 115 | } | ||
| 116 | options->in_file = optarg; | ||
| 117 | break; | ||
| 118 | |||
| 119 | case 'o': | ||
| 120 | if (!optarg || optarg[0] == '\0') { | ||
| 121 | fprintf(stderr, "ERROR: --outfile requires a filename or '-' for stdout\n"); | ||
| 122 | free(options); | ||
| 123 | return NULL; | ||
| 124 | } | ||
| 125 | options->out_file = optarg; | ||
| 126 | break; | ||
| 127 | |||
| 128 | case 'f': | ||
| 129 | if (!optarg || optarg[0] == '\0') { | ||
| 130 | fprintf(stderr, "ERROR: --format requires a format (bin|xml|json|openstep)\n"); | ||
| 131 | free(options); | ||
| 132 | return NULL; | ||
| 133 | } | ||
| 134 | if (!strncmp(optarg, "bin", 3)) { | ||
| 135 | options->out_fmt = PLIST_FORMAT_BINARY; | ||
| 136 | } else if (!strncmp(optarg, "xml", 3)) { | ||
| 137 | options->out_fmt = PLIST_FORMAT_XML; | ||
| 138 | } else if (!strncmp(optarg, "json", 4)) { | ||
| 139 | options->out_fmt = PLIST_FORMAT_JSON; | ||
| 140 | } else if (!strncmp(optarg, "openstep", 8) || | ||
| 141 | !strncmp(optarg, "ostep", 5)) { | ||
| 142 | options->out_fmt = PLIST_FORMAT_OSTEP; | ||
| 143 | } else { | ||
| 144 | fprintf(stderr, "ERROR: Unsupported output format\n"); | ||
| 145 | free(options); | ||
| 146 | return NULL; | ||
| 147 | } | ||
| 148 | break; | ||
| 149 | |||
| 150 | case 'c': | ||
| 151 | options->flags |= OPT_COMPACT; | ||
| 152 | break; | ||
| 153 | |||
| 154 | case 's': | ||
| 155 | options->flags |= OPT_SORT; | ||
| 156 | break; | ||
| 157 | |||
| 158 | case 'p': { | ||
| 159 | if (!optarg || optarg[0] == '\0') { | ||
| 160 | fprintf(stderr, "ERROR: --print requires a filename or '-' for stdin\n"); | ||
| 161 | free(options); | ||
| 162 | return NULL; | ||
| 163 | } | ||
| 164 | options->in_file = optarg; | ||
| 165 | options->out_fmt = PLIST_FORMAT_PRINT; | ||
| 166 | |||
| 167 | char *env_fmt = getenv("PLIST_OUTPUT_FORMAT"); | ||
| 168 | if (env_fmt) { | ||
| 169 | if (!strcmp(env_fmt, "plutil")) { | ||
| 170 | options->out_fmt = PLIST_FORMAT_PLUTIL; | ||
| 171 | } else if (!strcmp(env_fmt, "limd")) { | ||
| 172 | options->out_fmt = PLIST_FORMAT_LIMD; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | break; | ||
| 120 | } | 176 | } |
| 121 | if (!strncmp(argv[i+1], "bin", 3)) { | 177 | |
| 122 | options->out_fmt = PLIST_FORMAT_BINARY; | 178 | case 'd': |
| 123 | } else if (!strncmp(argv[i+1], "xml", 3)) { | 179 | options->flags |= OPT_DEBUG; |
| 124 | options->out_fmt = PLIST_FORMAT_XML; | 180 | break; |
| 125 | } else if (!strncmp(argv[i+1], "json", 4)) { | 181 | |
| 126 | options->out_fmt = PLIST_FORMAT_JSON; | 182 | case 'h': |
| 127 | } else if (!strncmp(argv[i+1], "openstep", 8) || !strncmp(argv[i+1], "ostep", 5)) { | ||
| 128 | options->out_fmt = PLIST_FORMAT_OSTEP; | ||
| 129 | } else { | ||
| 130 | fprintf(stderr, "ERROR: Unsupported output format\n"); | ||
| 131 | free(options); | 183 | free(options); |
| 132 | return NULL; | 184 | return NULL; |
| 133 | } | 185 | |
| 134 | i++; | 186 | case 'v': |
| 135 | continue; | 187 | printf("plistutil %s\n", libplist_version()); |
| 136 | } | 188 | exit(EXIT_SUCCESS); |
| 137 | else if (!strcmp(argv[i], "--compact") || !strcmp(argv[i], "-c")) | 189 | |
| 138 | { | 190 | default: |
| 139 | options->flags |= OPT_COMPACT; | 191 | fprintf(stderr, "ERROR: Invalid option\n"); |
| 140 | } | ||
| 141 | else if (!strcmp(argv[i], "--sort") || !strcmp(argv[i], "-s")) | ||
| 142 | { | ||
| 143 | options->flags |= OPT_SORT; | ||
| 144 | } | ||
| 145 | else if (!strcmp(argv[i], "--print") || !strcmp(argv[i], "-p")) | ||
| 146 | { | ||
| 147 | if ((i + 1) == argc) | ||
| 148 | { | ||
| 149 | free(options); | 192 | free(options); |
| 150 | return NULL; | 193 | return NULL; |
| 151 | } | ||
| 152 | options->in_file = argv[i + 1]; | ||
| 153 | options->out_fmt = PLIST_FORMAT_PRINT; | ||
| 154 | char *env_fmt = getenv("PLIST_OUTPUT_FORMAT"); | ||
| 155 | if (env_fmt) { | ||
| 156 | if (!strcmp(env_fmt, "plutil")) { | ||
| 157 | options->out_fmt = PLIST_FORMAT_PLUTIL; | ||
| 158 | } else if (!strcmp(env_fmt, "limd")) { | ||
| 159 | options->out_fmt = PLIST_FORMAT_LIMD; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | i++; | ||
| 163 | continue; | ||
| 164 | } | ||
| 165 | else if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d")) | ||
| 166 | { | ||
| 167 | options->flags |= OPT_DEBUG; | ||
| 168 | } | ||
| 169 | else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) | ||
| 170 | { | ||
| 171 | free(options); | ||
| 172 | return NULL; | ||
| 173 | } | ||
| 174 | else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) | ||
| 175 | { | ||
| 176 | printf("plistutil %s\n", libplist_version()); | ||
| 177 | exit(EXIT_SUCCESS); | ||
| 178 | } | ||
| 179 | else | ||
| 180 | { | ||
| 181 | fprintf(stderr, "ERROR: Invalid option '%s'\n", argv[i]); | ||
| 182 | free(options); | ||
| 183 | return NULL; | ||
| 184 | } | 194 | } |
| 185 | } | 195 | } |
| 186 | 196 | ||
