Mercurial > flash_v2
comparison packages/redboot/current/src/parse.c @ 182:f62680ef1804
Merge from eCos master repository on 2001-09-07-06:43:02-BST
| author | jlarmour |
|---|---|
| date | Wed, 12 Sep 2001 00:59:17 +0000 |
| parents | ac086aa3217e |
| children | 9160a8005d76 |
comparison
equal
deleted
inserted
replaced
| 181:d61da071934c | 182:f62680ef1804 |
|---|---|
| 74 break; // Line ended with a string of spaces | 74 break; // Line ended with a string of spaces |
| 75 } | 75 } |
| 76 if (indx < MAX_ARGV) { | 76 if (indx < MAX_ARGV) { |
| 77 argv[indx++] = cp; | 77 argv[indx++] = cp; |
| 78 } else { | 78 } else { |
| 79 printf("Too many arguments - stopped at: '%s'\n", cp); | 79 diag_printf("Too many arguments - stopped at: '%s'\n", cp); |
| 80 } | 80 } |
| 81 while (*cp) { | 81 while (*cp) { |
| 82 if (*cp == ' ') { | 82 if (*cp == ' ') { |
| 83 *cp++ = '\0'; | 83 *cp++ = '\0'; |
| 84 break; | 84 break; |
| 95 } | 95 } |
| 96 // Move string to swallow escapes | 96 // Move string to swallow escapes |
| 97 *pp++ = *cp++; | 97 *pp++ = *cp++; |
| 98 } | 98 } |
| 99 if (!*cp) { | 99 if (!*cp) { |
| 100 printf("Unbalanced string!\n"); | 100 diag_printf("Unbalanced string!\n"); |
| 101 } else { | 101 } else { |
| 102 if (pp != cp) *pp = '\0'; | 102 if (pp != cp) *pp = '\0'; |
| 103 *cp++ = '\0'; | 103 *cp++ = '\0'; |
| 104 break; | 104 break; |
| 105 } | 105 } |
| 122 struct cmd *cmd, *cmd2; | 122 struct cmd *cmd, *cmd2; |
| 123 // Search command table | 123 // Search command table |
| 124 cmd_len = strlen(arg); | 124 cmd_len = strlen(arg); |
| 125 cmd = tab; | 125 cmd = tab; |
| 126 while (cmd != tabend) { | 126 while (cmd != tabend) { |
| 127 if (strncmpci(arg, cmd->str, cmd_len) == 0) { | 127 if (strncasecmp(arg, cmd->str, cmd_len) == 0) { |
| 128 if (strlen(cmd->str) > cmd_len) { | 128 if (strlen(cmd->str) > cmd_len) { |
| 129 // Check for ambiguous commands here | 129 // Check for ambiguous commands here |
| 130 // Note: If there are commands which are not length-unique | 130 // Note: If there are commands which are not length-unique |
| 131 // then this check will be invalid. E.g. "du" and "dump" | 131 // then this check will be invalid. E.g. "du" and "dump" |
| 132 bool first = true; | 132 bool first = true; |
| 133 cmd2 = tab; | 133 cmd2 = tab; |
| 134 while (cmd2 != tabend) { | 134 while (cmd2 != tabend) { |
| 135 if ((cmd != cmd2) && | 135 if ((cmd != cmd2) && |
| 136 (strncmpci(arg, cmd2->str, cmd_len) == 0)) { | 136 (strncasecmp(arg, cmd2->str, cmd_len) == 0)) { |
| 137 if (first) { | 137 if (first) { |
| 138 printf("Ambiguous command '%s', choices are: %s", arg, cmd->str); | 138 diag_printf("Ambiguous command '%s', choices are: %s", |
| 139 arg, cmd->str); | |
| 139 first = false; | 140 first = false; |
| 140 } | 141 } |
| 141 printf(" %s", cmd2->str); | 142 diag_printf(" %s", cmd2->str); |
| 142 } | 143 } |
| 143 cmd2++; | 144 cmd2++; |
| 144 } | 145 } |
| 145 if (!first) { | 146 if (!first) { |
| 146 // At least one ambiguity found - fail the lookup | 147 // At least one ambiguity found - fail the lookup |
| 147 printf("\n"); | 148 diag_printf("\n"); |
| 148 return (struct cmd *)0; | 149 return (struct cmd *)0; |
| 149 } | 150 } |
| 150 } | 151 } |
| 151 return cmd; | 152 return cmd; |
| 152 } | 153 } |
| 157 | 158 |
| 158 void | 159 void |
| 159 cmd_usage(struct cmd *tab, struct cmd *tabend, char *prefix) | 160 cmd_usage(struct cmd *tab, struct cmd *tabend, char *prefix) |
| 160 { | 161 { |
| 161 struct cmd *cmd; | 162 struct cmd *cmd; |
| 162 printf("Usage:\n"); for (cmd = tab; cmd != tabend; cmd++) { | 163 |
| 163 printf(" %s%s %s\n", prefix, cmd->str, cmd->usage); | 164 diag_printf("Usage:\n"); |
| 165 for (cmd = tab; cmd != tabend; cmd++) { | |
| 166 diag_printf(" %s%s %s\n", prefix, cmd->str, cmd->usage); | |
| 164 } | 167 } |
| 165 } | 168 } |
| 166 | 169 |
| 167 // Option processing | 170 // Option processing |
| 168 | 171 |
| 221 flag_ok = false; | 224 flag_ok = false; |
| 222 opt = opts; | 225 opt = opts; |
| 223 for (j = 0; j < num_opts; j++, opt++) { | 226 for (j = 0; j < num_opts; j++, opt++) { |
| 224 if (c == opt->flag) { | 227 if (c == opt->flag) { |
| 225 if (opt->arg_set && *opt->arg_set) { | 228 if (opt->arg_set && *opt->arg_set) { |
| 226 printf("** Error: %s already specified\n", opt->name); | 229 diag_printf("** Error: %s already specified\n", opt->name); |
| 227 ret = false; | 230 ret = false; |
| 228 } | 231 } |
| 229 if (opt->takes_arg) { | 232 if (opt->takes_arg) { |
| 230 if (argv[i][2] == '=') { | 233 if (argv[i][2] == '=') { |
| 231 s = &argv[i][3]; | 234 s = &argv[i][3]; |
| 234 i++; | 237 i++; |
| 235 } | 238 } |
| 236 switch (opt->arg_type) { | 239 switch (opt->arg_type) { |
| 237 case OPTION_ARG_TYPE_NUM: | 240 case OPTION_ARG_TYPE_NUM: |
| 238 if (!parse_num(s, (unsigned long *)opt->arg, 0, 0)) { | 241 if (!parse_num(s, (unsigned long *)opt->arg, 0, 0)) { |
| 239 printf("** Error: invalid number '%s' for %s\n", s, opt->name); | 242 diag_printf("** Error: invalid number '%s' for %s\n", |
| 243 s, opt->name); | |
| 240 ret = false; | 244 ret = false; |
| 241 } | 245 } |
| 242 break; | 246 break; |
| 243 case OPTION_ARG_TYPE_STR: | 247 case OPTION_ARG_TYPE_STR: |
| 244 *opt->arg = s; | 248 *opt->arg = s; |
| 258 flag_ok = true; | 262 flag_ok = true; |
| 259 break; | 263 break; |
| 260 } | 264 } |
| 261 } | 265 } |
| 262 if (!flag_ok) { | 266 if (!flag_ok) { |
| 263 printf("** Error: invalid flag '%c'\n", c); | 267 diag_printf("** Error: invalid flag '%c'\n", c); |
| 264 ret = false; | 268 ret = false; |
| 265 } | 269 } |
| 266 } else { | 270 } else { |
| 267 if (def_arg) { | 271 if (def_arg) { |
| 268 if (def_arg_set) { | 272 if (def_arg_set) { |
| 269 printf("** Error: %s already specified\n", def_descr); | 273 diag_printf("** Error: %s already specified\n", def_descr); |
| 270 ret = false; | 274 ret = false; |
| 271 } | 275 } |
| 272 switch (def_arg_type) { | 276 switch (def_arg_type) { |
| 273 case OPTION_ARG_TYPE_NUM: | 277 case OPTION_ARG_TYPE_NUM: |
| 274 if (!parse_num(argv[i], (unsigned long *)def_arg, 0, 0)) { | 278 if (!parse_num(argv[i], (unsigned long *)def_arg, 0, 0)) { |
| 275 printf("** Error: invalid number '%s' for %s\n", argv[i], def_descr); | 279 diag_printf("** Error: invalid number '%s' for %s\n", |
| 280 argv[i], def_descr); | |
| 276 ret = false; | 281 ret = false; |
| 277 } | 282 } |
| 278 break; | 283 break; |
| 279 case OPTION_ARG_TYPE_STR: | 284 case OPTION_ARG_TYPE_STR: |
| 280 *def_arg = argv[i]; | 285 *def_arg = argv[i]; |
| 281 break; | 286 break; |
| 282 } | 287 } |
| 283 def_arg_set = true; | 288 def_arg_set = true; |
| 284 } else { | 289 } else { |
| 285 printf("** Error: no default/non-flag arguments supported\n"); | 290 diag_printf("** Error: no default/non-flag arguments supported\n"); |
| 286 ret = false; | 291 ret = false; |
| 287 } | 292 } |
| 288 } | 293 } |
| 289 } | 294 } |
| 290 return ret; | 295 return ret; |
| 302 unsigned long result = 0; | 307 unsigned long result = 0; |
| 303 int digit; | 308 int digit; |
| 304 | 309 |
| 305 while (*s == ' ') s++; | 310 while (*s == ' ') s++; |
| 306 while (*s) { | 311 while (*s) { |
| 307 if (first && (s[0] == '0') && (tolower(s[1]) == 'x')) { | 312 if (first && (s[0] == '0') && (_tolower(s[1]) == 'x')) { |
| 308 radix = 16; | 313 radix = 16; |
| 309 s += 2; | 314 s += 2; |
| 310 } | 315 } |
| 311 first = false; | 316 first = false; |
| 312 c = *s++; | 317 c = *s++; |
