diff options
Diffstat (limited to 'tools/plistutil.c')
| -rw-r--r-- | tools/plistutil.c | 78 |
1 files changed, 36 insertions, 42 deletions
diff --git a/tools/plistutil.c b/tools/plistutil.c index 71972f9..bd83e92 100644 --- a/tools/plistutil.c +++ b/tools/plistutil.c | |||
| @@ -41,7 +41,7 @@ | |||
| 41 | typedef struct _options | 41 | typedef struct _options |
| 42 | { | 42 | { |
| 43 | char *in_file, *out_file; | 43 | char *in_file, *out_file; |
| 44 | uint8_t debug, in_fmt, out_fmt; // fmts 0 = undef, 1 = bin, 2 = xml, 3 = json someday | 44 | uint8_t debug, in_fmt, out_fmt; // fmts 0 = undef, 1 = bin, 2 = xml, 3 = json |
| 45 | } options_t; | 45 | } options_t; |
| 46 | 46 | ||
| 47 | static void print_usage(int argc, char *argv[]) | 47 | static void print_usage(int argc, char *argv[]) |
| @@ -50,14 +50,19 @@ static void print_usage(int argc, char *argv[]) | |||
| 50 | name = strrchr(argv[0], '/'); | 50 | name = strrchr(argv[0], '/'); |
| 51 | printf("Usage: %s [OPTIONS] [-i FILE] [-o FILE]\n", (name ? name + 1: argv[0])); | 51 | printf("Usage: %s [OPTIONS] [-i FILE] [-o FILE]\n", (name ? name + 1: argv[0])); |
| 52 | printf("\n"); | 52 | printf("\n"); |
| 53 | printf("Convert a plist FILE from binary to XML format or vice-versa.\n"); | 53 | printf("Convert a plist FILE between binary, XML, and JSON format.\n"); |
| 54 | printf("If -f is omitted, XML plist data will be converted to binary and vice-versa.\n"); | ||
| 55 | printf("To convert to/from JSON the output format needs to be specified.\n"); | ||
| 54 | printf("\n"); | 56 | printf("\n"); |
| 55 | printf("OPTIONS:\n"); | 57 | printf("OPTIONS:\n"); |
| 56 | printf(" -i, --infile FILE Optional FILE to convert from or stdin if - or not used\n"); | 58 | printf(" -i, --infile FILE Optional FILE to convert from or stdin if - or not used\n"); |
| 57 | printf(" -o, --outfile FILE Optional FILE to convert to or stdout if - or not used\n"); | 59 | printf(" -o, --outfile FILE Optional FILE to convert to or stdout if - or not used\n"); |
| 58 | printf(" -f, --format [bin|xml] Force output format, regardless of input type\n"); | 60 | printf(" -f, --format FORMAT Force output format, regardless of input type\n"); |
| 59 | printf(" -d, --debug Enable extended debug output\n"); | 61 | printf(" FORMAT is one of xml, bin, or json\n"); |
| 60 | printf(" -v, --version Print version information\n"); | 62 | printf(" If omitted XML will be converted to binary,\n"); |
| 63 | printf(" and binary to XML.\n"); | ||
| 64 | printf(" -d, --debug Enable extended debug output\n"); | ||
| 65 | printf(" -v, --version Print version information\n"); | ||
| 61 | printf("\n"); | 66 | printf("\n"); |
| 62 | printf("Homepage: <" PACKAGE_URL ">\n"); | 67 | printf("Homepage: <" PACKAGE_URL ">\n"); |
| 63 | printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); | 68 | printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); |
| @@ -105,8 +110,10 @@ static options_t *parse_arguments(int argc, char *argv[]) | |||
| 105 | options->out_fmt = 1; | 110 | options->out_fmt = 1; |
| 106 | } else if (!strncmp(argv[i+1], "xml", 3)) { | 111 | } else if (!strncmp(argv[i+1], "xml", 3)) { |
| 107 | options->out_fmt = 2; | 112 | options->out_fmt = 2; |
| 113 | } else if (!strncmp(argv[i+1], "json", 4)) { | ||
| 114 | options->out_fmt = 3; | ||
| 108 | } else { | 115 | } else { |
| 109 | printf("ERROR: Unsupported output format\n"); | 116 | fprintf(stderr, "ERROR: Unsupported output format\n"); |
| 110 | free(options); | 117 | free(options); |
| 111 | return NULL; | 118 | return NULL; |
| 112 | } | 119 | } |
| @@ -129,7 +136,7 @@ static options_t *parse_arguments(int argc, char *argv[]) | |||
| 129 | } | 136 | } |
| 130 | else | 137 | else |
| 131 | { | 138 | { |
| 132 | printf("ERROR: Invalid option '%s'\n", argv[i]); | 139 | fprintf(stderr, "ERROR: Invalid option '%s'\n", argv[i]); |
| 133 | free(options); | 140 | free(options); |
| 134 | return NULL; | 141 | return NULL; |
| 135 | } | 142 | } |
| @@ -140,6 +147,7 @@ static options_t *parse_arguments(int argc, char *argv[]) | |||
| 140 | 147 | ||
| 141 | int main(int argc, char *argv[]) | 148 | int main(int argc, char *argv[]) |
| 142 | { | 149 | { |
| 150 | int ret = 0; | ||
| 143 | FILE *iplist = NULL; | 151 | FILE *iplist = NULL; |
| 144 | plist_t root_node = NULL; | 152 | plist_t root_node = NULL; |
| 145 | char *plist_out = NULL; | 153 | char *plist_out = NULL; |
| @@ -162,7 +170,7 @@ int main(int argc, char *argv[]) | |||
| 162 | plist_entire = malloc(sizeof(char) * read_capacity); | 170 | plist_entire = malloc(sizeof(char) * read_capacity); |
| 163 | if(plist_entire == NULL) | 171 | if(plist_entire == NULL) |
| 164 | { | 172 | { |
| 165 | printf("ERROR: Failed to allocate buffer to read from stdin"); | 173 | fprintf(stderr, "ERROR: Failed to allocate buffer to read from stdin"); |
| 166 | free(options); | 174 | free(options); |
| 167 | return 1; | 175 | return 1; |
| 168 | } | 176 | } |
| @@ -176,7 +184,7 @@ int main(int argc, char *argv[]) | |||
| 176 | plist_entire = realloc(plist_entire, sizeof(char) * read_capacity); | 184 | plist_entire = realloc(plist_entire, sizeof(char) * read_capacity); |
| 177 | if (plist_entire == NULL) | 185 | if (plist_entire == NULL) |
| 178 | { | 186 | { |
| 179 | printf("ERROR: Failed to reallocate stdin buffer\n"); | 187 | fprintf(stderr, "ERROR: Failed to reallocate stdin buffer\n"); |
| 180 | free(old); | 188 | free(old); |
| 181 | free(options); | 189 | free(options); |
| 182 | return 1; | 190 | return 1; |
| @@ -190,7 +198,7 @@ int main(int argc, char *argv[]) | |||
| 190 | plist_entire = realloc(plist_entire, sizeof(char) * (read_capacity+1)); | 198 | plist_entire = realloc(plist_entire, sizeof(char) * (read_capacity+1)); |
| 191 | if (plist_entire == NULL) | 199 | if (plist_entire == NULL) |
| 192 | { | 200 | { |
| 193 | printf("ERROR: Failed to reallocate stdin buffer\n"); | 201 | fprintf(stderr, "ERROR: Failed to reallocate stdin buffer\n"); |
| 194 | free(old); | 202 | free(old); |
| 195 | free(options); | 203 | free(options); |
| 196 | return 1; | 204 | return 1; |
| @@ -201,14 +209,14 @@ int main(int argc, char *argv[]) | |||
| 201 | // Not positive we need this, but it doesnt seem to hurt lol | 209 | // Not positive we need this, but it doesnt seem to hurt lol |
| 202 | if(ferror(stdin)) | 210 | if(ferror(stdin)) |
| 203 | { | 211 | { |
| 204 | printf("ERROR: reading from stdin.\n"); | 212 | fprintf(stderr, "ERROR: reading from stdin.\n"); |
| 205 | free(plist_entire); | 213 | free(plist_entire); |
| 206 | free(options); | 214 | free(options); |
| 207 | return 1; | 215 | return 1; |
| 208 | } | 216 | } |
| 209 | 217 | ||
| 210 | if (read_size < 8) { | 218 | if (read_size < 8) { |
| 211 | printf("ERROR: Input file is too small to contain valid plist data.\n"); | 219 | fprintf(stderr, "ERROR: Input file is too small to contain valid plist data.\n"); |
| 212 | free(plist_entire); | 220 | free(plist_entire); |
| 213 | free(options); | 221 | free(options); |
| 214 | return 1; | 222 | return 1; |
| @@ -219,7 +227,7 @@ int main(int argc, char *argv[]) | |||
| 219 | // read input file | 227 | // read input file |
| 220 | iplist = fopen(options->in_file, "rb"); | 228 | iplist = fopen(options->in_file, "rb"); |
| 221 | if (!iplist) { | 229 | if (!iplist) { |
| 222 | printf("ERROR: Could not open input file '%s': %s\n", options->in_file, strerror(errno)); | 230 | fprintf(stderr, "ERROR: Could not open input file '%s': %s\n", options->in_file, strerror(errno)); |
| 223 | free(options); | 231 | free(options); |
| 224 | return 1; | 232 | return 1; |
| 225 | } | 233 | } |
| @@ -228,7 +236,7 @@ int main(int argc, char *argv[]) | |||
| 228 | fstat(fileno(iplist), &filestats); | 236 | fstat(fileno(iplist), &filestats); |
| 229 | 237 | ||
| 230 | if (filestats.st_size < 8) { | 238 | if (filestats.st_size < 8) { |
| 231 | printf("ERROR: Input file is too small to contain valid plist data.\n"); | 239 | fprintf(stderr, "ERROR: Input file is too small to contain valid plist data.\n"); |
| 232 | free(options); | 240 | free(options); |
| 233 | fclose(iplist); | 241 | fclose(iplist); |
| 234 | return -1; | 242 | return -1; |
| @@ -240,7 +248,7 @@ int main(int argc, char *argv[]) | |||
| 240 | } | 248 | } |
| 241 | 249 | ||
| 242 | if (options->out_fmt == 0) { | 250 | if (options->out_fmt == 0) { |
| 243 | // convert from binary to xml or vice-versa<br> | 251 | // convert from binary to xml or vice-versa |
| 244 | if (plist_is_binary(plist_entire, read_size)) | 252 | if (plist_is_binary(plist_entire, read_size)) |
| 245 | { | 253 | { |
| 246 | plist_from_bin(plist_entire, read_size, &root_node); | 254 | plist_from_bin(plist_entire, read_size, &root_node); |
| @@ -254,29 +262,13 @@ int main(int argc, char *argv[]) | |||
| 254 | } | 262 | } |
| 255 | else | 263 | else |
| 256 | { | 264 | { |
| 265 | plist_from_memory(plist_entire, read_size, &root_node); | ||
| 257 | if (options->out_fmt == 1) { | 266 | if (options->out_fmt == 1) { |
| 258 | if (plist_is_binary(plist_entire, read_size)) | 267 | plist_to_bin(root_node, &plist_out, &size); |
| 259 | { | ||
| 260 | plist_out = malloc(sizeof(char) * read_size); | ||
| 261 | memcpy(plist_out, plist_entire, read_size); | ||
| 262 | size = read_size; | ||
| 263 | } | ||
| 264 | else | ||
| 265 | { | ||
| 266 | plist_from_xml(plist_entire, read_size, &root_node); | ||
| 267 | plist_to_bin(root_node, &plist_out, &size); | ||
| 268 | } | ||
| 269 | } else if (options->out_fmt == 2) { | 268 | } else if (options->out_fmt == 2) { |
| 270 | if (plist_is_binary(plist_entire, read_size)) { | 269 | plist_to_xml(root_node, &plist_out, &size); |
| 271 | plist_from_bin(plist_entire, read_size, &root_node); | 270 | } else if (options->out_fmt == 3) { |
| 272 | plist_to_xml(root_node, &plist_out, &size); | 271 | plist_to_json(root_node, &plist_out, &size, 0); |
| 273 | } | ||
| 274 | else | ||
| 275 | { | ||
| 276 | plist_out = malloc(sizeof(char) * read_size); | ||
| 277 | memcpy(plist_out, plist_entire, read_size); | ||
| 278 | size = read_size; | ||
| 279 | } | ||
| 280 | } | 272 | } |
| 281 | } | 273 | } |
| 282 | plist_free(root_node); | 274 | plist_free(root_node); |
| @@ -288,7 +280,7 @@ int main(int argc, char *argv[]) | |||
| 288 | { | 280 | { |
| 289 | FILE *oplist = fopen(options->out_file, "wb"); | 281 | FILE *oplist = fopen(options->out_file, "wb"); |
| 290 | if (!oplist) { | 282 | if (!oplist) { |
| 291 | printf("ERROR: Could not open output file '%s': %s\n", options->out_file, strerror(errno)); | 283 | fprintf(stderr, "ERROR: Could not open output file '%s': %s\n", options->out_file, strerror(errno)); |
| 292 | free(options); | 284 | free(options); |
| 293 | return 1; | 285 | return 1; |
| 294 | } | 286 | } |
| @@ -301,9 +293,11 @@ int main(int argc, char *argv[]) | |||
| 301 | 293 | ||
| 302 | free(plist_out); | 294 | free(plist_out); |
| 303 | } | 295 | } |
| 304 | else | 296 | else { |
| 305 | printf("ERROR: Failed to convert input file.\n"); | 297 | fprintf(stderr, "ERROR: Failed to convert input file.\n"); |
| 298 | ret = 2; | ||
| 299 | } | ||
| 306 | 300 | ||
| 307 | free(options); | 301 | free(options); |
| 308 | return 0; | 302 | return ret; |
| 309 | } | 303 | } |
