OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. |
| 4 * |
| 5 * Update GPT attribute bits. |
| 6 */ |
| 7 #include <getopt.h> |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include "cgpt.h" |
| 11 #include "utility.h" |
| 12 |
| 13 /* Special validator. Set 'integer' as 1 to indicate the option is present. */ |
| 14 int AssignTrue(const char *argument, void *pointer, void *integer) { |
| 15 *(int*)integer = 1; |
| 16 return CGPT_OK; |
| 17 } |
| 18 |
| 19 /* Validator function. Returns 1 if 'argument' is between 'max' and 'min' |
| 20 * in 'valid_range'. */ |
| 21 int InNumberRange(const char *argument, void *valid_range, void *parsed) { |
| 22 struct number_range *range = valid_range; |
| 23 char *endptr; |
| 24 int number; |
| 25 |
| 26 assert(valid_range); |
| 27 |
| 28 number = strtol(argument, &endptr, 10); |
| 29 if (*endptr) { |
| 30 printf("[ERROR] argument '%s' is not a number.\n", argument); |
| 31 return CGPT_FAILED; |
| 32 } |
| 33 |
| 34 if (number < range->min) { |
| 35 printf("[ERROR] argument is too small (min is %d, but you gave: %d).\n", |
| 36 range->min, number); |
| 37 return CGPT_FAILED; |
| 38 } else if (number > range->max) { |
| 39 printf("[ERROR] argument is too large (max is %d, but you gave: %d).\n", |
| 40 range->max, number); |
| 41 return CGPT_FAILED; |
| 42 } else { |
| 43 if (parsed) *(int*)parsed = number; |
| 44 return CGPT_OK; |
| 45 } |
| 46 } |
| 47 |
| 48 void ShowOptions(const struct option *opts, |
| 49 const struct option_details *details, |
| 50 const int num) { |
| 51 int i; |
| 52 for (i = 0; i < num; ++i) { |
| 53 char buf[32]; |
| 54 snprintf(buf, sizeof(buf), "--%s %s", opts[i].name, |
| 55 opts[i].has_arg ? "ARG" : ""); |
| 56 printf(" %-20s (-%c) %s\n", buf, opts[i].val, details[i].comment); |
| 57 } |
| 58 } |
| 59 |
| 60 int HandleOptions(const int argc, |
| 61 char *const *argv, |
| 62 const char *short_options, |
| 63 const int option_count, |
| 64 const struct option *options, |
| 65 const struct option_details *details) { |
| 66 while (1) { |
| 67 int index; |
| 68 int option; |
| 69 |
| 70 /* We assume every short option has an entry in long option (for validator). |
| 71 * So please add corresponding entry in attribute_options if you add short |
| 72 * option. */ |
| 73 index = NOT_INITED; |
| 74 option = getopt_long(argc, argv, short_options, options, &index); |
| 75 if (option == -1) { |
| 76 break; |
| 77 } else if (option == 0) { |
| 78 /* option 'val' has been saved in 'flag'. We do nothing here. */ |
| 79 } else { |
| 80 /* Short option doesn't update 'index'. We search whole array to find out |
| 81 * the corresponding long option. */ |
| 82 if (index == NOT_INITED) { |
| 83 for (index = 0; index < option_count; ++index) |
| 84 if (option == options[index].val) break; |
| 85 /* assumes every short option has a corresponding long option. */ |
| 86 assert(index < option_count); |
| 87 } |
| 88 assert(option == options[index].val); |
| 89 |
| 90 /* Calls validator if an argument is provided. */ |
| 91 if (details[index].validator && |
| 92 CGPT_OK != details[index].validator( |
| 93 optarg ? argv[optind - 1] : 0, |
| 94 details[index].valid_range, |
| 95 details[index].parsed)) { |
| 96 printf("[ERROR] The argument of '%s' is invalid.\n", |
| 97 options[index].name); |
| 98 return CGPT_FAILED; |
| 99 } |
| 100 } |
| 101 } |
| 102 return CGPT_OK; |
| 103 } |
| 104 |
| 105 int OpenDriveInLastArgument(const int argc, |
| 106 char *const *argv, |
| 107 struct drive *drive) { |
| 108 /* Then, we need a non-option argument. */ |
| 109 if (optind == (argc - 1)) { |
| 110 char *drive_path; |
| 111 drive_path = argv[optind]; |
| 112 if (CGPT_OK != DriveOpen(drive_path, drive)) { |
| 113 printf("[ERROR] DriveOpen(%s) error!\n", drive_path); |
| 114 return CGPT_FAILED; |
| 115 } |
| 116 } else { |
| 117 printf("[ERROR] One (and only one) non-option argument is required.\n"); |
| 118 return CGPT_FAILED; |
| 119 } |
| 120 |
| 121 return CGPT_OK; |
| 122 } |
OLD | NEW |