- 论坛徽章:
- 2
|
推荐一个命令行解析函数。
- #include <stddef.h>
- /* phrase_command_line
- **
- ** AUTHOR: Cobras Zhang
- ** SITE : www.cobsoft.com
- ** DATE : 2011-3
- **
- ** phrase_command_line will take each element seperated by space or quoted
- ** by double quote mark as a single argument item. for example:
- **
- ** test.exe "this is a file.jpg"another_file.jpg"yet a file.jpg" -opt
- **
- ** will be phrased as the following five argument items(not including the
- ** leading index number):
- **
- ** 1 test.exe
- ** 2 this is a file.jpg
- ** 3 another_file.jpg
- ** 4 yet a file.jpg
- ** 5 -opt
- **
- ** as you see, two elements just seperated by double quote mark will be
- ** taken as two different argument items. that is the mechanism of
- ** phrase_command_line function.
- **
- ** empty argument item is not exist. that is "" will not be taken as a
- ** argument item.
- **
- ** if a element leading by double quote mark and no terminating mark, all
- ** the characters after that leading mark will be taken as a single
- ** argument item.
- **
- ** if o_argv is NULL, phrase_command_line only return the argument count
- ** of command line, else phrase_command_line will modify cmd_line and set
- ** o_argv array to point each argument item.
- **
- ** the return value of phrase_command_line function is the count of
- ** argument items, not including the NULL termiator.
- **
- ** please take carefull that o_argv must be great enough to store all the
- ** pointer array and a NULL terminator, otherwise, the program may be
- ** breaked down. thus, you'd better use phrase_command_line itself to
- ** caculate the proper count of arguments by giving a NULL o_argv and then
- ** allocate the memory of a pointer array with the length of this count
- ** plus one.
- */
- int phrase_command_line(char *cmd_line, char **o_argv)
- {
- int argc;
- if (cmd_line != NULL) {
- argc = 0;
- for (;;) {
- switch (*cmd_line) {
- case ' ':
- case '\t':
- if (o_argv != NULL) {
- *cmd_line = '\0';
- }
- cmd_line++;
- continue;
- case '"':
- if (o_argv != NULL) {
- *cmd_line ='\0';
- o_argv[argc] = cmd_line + 1;
- }
- cmd_line++;
- switch (*cmd_line) {
- case '"':
- case '\r':
- case '\n':
- case '\0':
- break;
- default:
- argc++;
- break;
- }
- for (;;) {
- switch (*cmd_line) {
- case '"':
- if (o_argv != NULL) {
- *cmd_line = '\0';
- }
- cmd_line++;
- break;
- case '\r':
- case '\n':
- case '\0':
- if (o_argv != NULL) {
- *cmd_line = '\0';
- o_argv[argc] = NULL;
- }
- return argc;
- default:
- cmd_line++;
- continue;
- }
- break;
- }
- continue;
- case '\r':
- case '\n':
- case '\0':
- if (o_argv != NULL) {
- *cmd_line = '\0';
- o_argv[argc] = NULL;
- }
- return argc;
- default:
- if (o_argv != NULL) {
- o_argv[argc] = cmd_line;
- }
- cmd_line++;
- argc++;
- for (;;) {
- switch (*cmd_line) {
- case '"':
- break;
- case ' ':
- case '\t':
- if (o_argv != NULL) {
- *cmd_line = '\0';
- }
- cmd_line++;
- break;
- case '\r':
- case '\n':
- case '\0':
- if (o_argv != NULL) {
- *cmd_line = '\0';
- o_argv[argc] = NULL;
- }
- return argc;
- default:
- cmd_line++;
- continue;
- }
- break;
- }
- continue;
- }
- }
- }
- return -1;
- }
- #include <stdio.h>
- #include <stdlib.h>
- int main(void)
- {
- char command_line[1024];
- int argc;
- char **argv;
- int i;
- printf("COMMAND> ");
- if (fgets(command_line, sizeof(command_line), stdin) != NULL) {
- argc = phrase_command_line(command_line, NULL);
- if (argc > 0) {
- argv = (char **)malloc(sizeof(char *) * (argc + 1));
- if (argv != NULL) {
- phrase_command_line(command_line, argv);
- printf("argv:\n\n");
- for (i = 0; i < argc; i++) {
- printf(" %d: %s\n", i, argv[i]);
- }
- free(argv);
- return 0;
- }
- }
- }
- return -1;
- }
复制代码 |
|