Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: src/platform/vboot_reference/utility/cgpt/cgpt_options.c

Issue 2374001: cgpt supports dev and add/delete/modify commands. (Closed) Base URL: ssh://git@chromiumos-git/chromeos
Patch Set: Created 10 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 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 2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file. 3 * found in the LICENSE file.
4 * 4 *
5 * Update GPT attribute bits. 5 * Update GPT attribute bits.
6 */ 6 */
7 #include <getopt.h> 7 #include <getopt.h>
8 #include <stdint.h>
8 #include <stdio.h> 9 #include <stdio.h>
9 #include <stdlib.h> 10 #include <stdlib.h>
10 #include "cgpt.h" 11 #include "cgpt.h"
11 #include "utility.h" 12 #include "utility.h"
12 13
13 /* Special validator. Set 'integer' as 1 to indicate the option is present. */ 14 /* Special validator. Set 'integer' as 1 to indicate the option is present. */
14 int AssignTrue(const char *argument, void *pointer, void *integer) { 15 int AssignTrue(const char *argument, void *pointer, void *integer) {
15 *(int*)integer = 1; 16 *(int*)integer = 1;
16 return CGPT_OK; 17 return CGPT_OK;
17 } 18 }
18 19
20 /* Special validator. Copy string to 'parsed' with max 'valid_range' bytes. */
21 int CopyString(const char *argument, void *max_len, void *dst) {
22 memcpy(dst, argument, (intptr_t)max_len);
23 return CGPT_OK;
24 }
25
19 /* Validator function. Returns 1 if 'argument' is between 'max' and 'min' 26 /* Validator function. Returns 1 if 'argument' is between 'max' and 'min'
20 * in 'valid_range'. */ 27 * in 'valid_range'. */
21 int InNumberRange(const char *argument, void *valid_range, void *parsed) { 28 int InNumberRange(const char *argument, void *valid_range, void *parsed) {
22 struct number_range *range = valid_range; 29 struct number_range *range = valid_range;
23 char *endptr; 30 char *endptr;
24 int number; 31 int number;
25 32
26 assert(valid_range);
27
28 number = strtol(argument, &endptr, 10); 33 number = strtol(argument, &endptr, 10);
29 if (*endptr) { 34 if (*endptr) {
30 printf("[ERROR] argument '%s' is not a number.\n", argument); 35 printf("[ERROR] argument '%s' is not a number.\n", argument);
31 return CGPT_FAILED; 36 return CGPT_FAILED;
32 } 37 }
33 38
34 if (number < range->min) { 39 if (range) {
35 printf("[ERROR] argument is too small (min is %d, but you gave: %d).\n", 40 if (number < range->min) {
36 range->min, number); 41 printf("[ERROR] argument is too small (min is %d, but you gave: %d).\n",
37 return CGPT_FAILED; 42 range->min, number);
38 } else if (number > range->max) { 43 return CGPT_FAILED;
39 printf("[ERROR] argument is too large (max is %d, but you gave: %d).\n", 44 } else if (number > range->max) {
40 range->max, number); 45 printf("[ERROR] argument is too large (max is %d, but you gave: %d).\n",
41 return CGPT_FAILED; 46 range->max, number);
47 return CGPT_FAILED;
48 } else {
49 if (parsed) *(int*)parsed = number;
50 return CGPT_OK;
51 }
42 } else { 52 } else {
53 /* no range to check, assign integer. */
43 if (parsed) *(int*)parsed = number; 54 if (parsed) *(int*)parsed = number;
44 return CGPT_OK; 55 return CGPT_OK;
45 } 56 }
46 } 57 }
47 58
48 void ShowOptions(const struct option *opts, 59 void ShowOptions(const struct option *opts,
49 const struct option_details *details, 60 const struct option_details *details,
50 const int num) { 61 const int num) {
51 int i; 62 int i;
52 for (i = 0; i < num; ++i) { 63 for (i = 0; i < num; ++i) {
53 char buf[32]; 64 char buf[32];
65 if (!opts[i].name) break;
54 snprintf(buf, sizeof(buf), "--%s %s", opts[i].name, 66 snprintf(buf, sizeof(buf), "--%s %s", opts[i].name,
55 opts[i].has_arg ? "ARG" : ""); 67 opts[i].has_arg ? "ARG" : "");
56 printf(" %-20s (-%c) %s\n", buf, opts[i].val, details[i].comment); 68 printf(" %-20s (-%c) %s\n", buf, opts[i].val, details[i].comment);
57 } 69 }
58 } 70 }
59 71
60 int HandleOptions(const int argc, 72 int HandleOptions(const int argc,
61 char *const *argv, 73 char *const *argv,
62 const char *short_options, 74 const char *short_options,
63 const int option_count, 75 const int option_count,
64 const struct option *options, 76 const struct option *options,
65 const struct option_details *details) { 77 const struct option_details *details) {
66 while (1) { 78 while (1) {
67 int index; 79 int index;
68 int option; 80 int option;
69 81
70 /* We assume every short option has an entry in long option (for validator). 82 /* 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 83 * So please add corresponding entry in attribute_options if you add short
72 * option. */ 84 * option. */
73 index = NOT_INITED; 85 index = NOT_INITED;
74 option = getopt_long(argc, argv, short_options, options, &index); 86 option = getopt_long(argc, argv, short_options, options, &index);
75 if (option == -1) { 87 if (option == -1) {
76 break; 88 break;
77 } else if (option == 0) { 89 } else if (option == 0) {
78 /* option 'val' has been saved in 'flag'. We do nothing here. */ 90 /* option 'val' has been saved in 'flag'. We do nothing here. */
91 } else if (option == ':') {
92 printf("[ERROR] Missing parameter for option.\n");
93 ShowOptions(options, details, option_count);
94 return CGPT_FAILED;
95 } else if (option == '?') {
96 printf("[ERROR] unknown option name: %s\n", argv[optind - 1]);
97 ShowOptions(options, details, option_count);
98 return CGPT_FAILED;
79 } else { 99 } else {
80 /* Short option doesn't update 'index'. We search whole array to find out 100 /* Short option doesn't update 'index'. We search whole array to find out
81 * the corresponding long option. */ 101 * the corresponding long option. */
82 if (index == NOT_INITED) { 102 if (index == NOT_INITED) {
83 for (index = 0; index < option_count; ++index) 103 for (index = 0; index < option_count; ++index)
84 if (option == options[index].val) break; 104 if (option == options[index].val) break;
85 /* assumes every short option has a corresponding long option. */ 105 /* assumes every short option has a corresponding long option. */
86 assert(index < option_count); 106 assert(index < option_count);
87 } 107 }
88 assert(option == options[index].val); 108 assert(option == options[index].val);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 printf("[ERROR] DriveOpen(%s) error!\n", drive_path); 142 printf("[ERROR] DriveOpen(%s) error!\n", drive_path);
123 return CGPT_FAILED; 143 return CGPT_FAILED;
124 } 144 }
125 } else { 145 } else {
126 printf("[ERROR] One (and only one) non-option argument is required.\n"); 146 printf("[ERROR] One (and only one) non-option argument is required.\n");
127 return CGPT_FAILED; 147 return CGPT_FAILED;
128 } 148 }
129 149
130 return CGPT_OK; 150 return CGPT_OK;
131 } 151 }
OLDNEW
« no previous file with comments | « src/platform/vboot_reference/utility/cgpt/cgpt_dev.c ('k') | src/platform/vboot_reference/utility/cgpt/cgpt_repair.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698