免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 4007 | 回复: 16
打印 上一主题 下一主题

[C] command 的内容为什么总是空的 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-11 11:24 |只看该作者 |倒序浏览
本帖最后由 thb303663990 于 2011-03-11 11:45 编辑

int split(struct command_line * command, char cline[ ])
{
        int i;
        int pos = 0;
        cline[strlen(cline) - 1] = '\0';
        //del_blank(pos, cline);
        while(cline[pos] != '\0' && (cline[pos] == ' ' || cline[pos] == '\t'))
        {
               pos++;
        }                                                   //去除空格及制表符
                i = 0;
        while(cline[pos] != '\0'){
                if((command->argv = (char *)malloc(MAX_LENGTH)) == NULL){
                        perror("fail to malloc");
                        return -1;
                        }
                //get_arg(command->argv, pos, cline);
                int i = 0;
        while(cline[pos] != '\0' || cline[pos] != ' ' || cline[pos] != '\t'){
                command->argv[i++] = cline[pos++];//将cline的值传递给command所指的结构体



        } ;
                i++;
        //      del_blank(pos, cline);
                while(cline[pos] != '\0' && (cline[pos] == ' ' || cline[pos] == '\t'))
        {
                pos++;
        }
        command->argv = NULL;
        command->name = command->argv[0];
        return i;

}
}
为什么command'的内容总是空的
如果cline为get 123.c 能把get传给name不

论坛徽章:
1
射手座
日期:2013-08-21 13:11:46
2 [报告]
发表于 2011-03-11 11:37 |只看该作者
gdb a.out
b split
r
然后n,p,你懂得

论坛徽章:
0
3 [报告]
发表于 2011-03-11 11:39 |只看该作者
回复 2# egmkang


  就是这么调试,发现command.name一直为0

论坛徽章:
0
4 [报告]
发表于 2011-03-11 14:42 |只看该作者
command->argv = NULL;
        command->name = command->argv[0];

这是干什么?没看懂。

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
5 [报告]
发表于 2011-03-11 14:47 |只看该作者
推荐一个命令行解析函数。

  1. #include <stddef.h>

  2. /*                           phrase_command_line
  3. **
  4. **                                                 AUTHOR: Cobras Zhang
  5. **                                                 SITE  : www.cobsoft.com
  6. **                                                 DATE  : 2011-3
  7. **
  8. **    phrase_command_line will take each element seperated by space or quoted
  9. **    by double quote mark as a single argument item. for example:
  10. **
  11. **        test.exe "this is a file.jpg"another_file.jpg"yet a file.jpg" -opt
  12. **
  13. **    will be phrased as the following five argument items(not including the
  14. **    leading index number):
  15. **
  16. **        1 test.exe
  17. **        2 this is a file.jpg
  18. **        3 another_file.jpg
  19. **        4 yet a file.jpg
  20. **        5 -opt
  21. **
  22. **    as you see, two elements just seperated by double quote mark will be
  23. **    taken as two different argument items. that is the mechanism of
  24. **    phrase_command_line function.
  25. **
  26. **    empty argument item is not exist. that is "" will not be taken as a
  27. **    argument item.
  28. **
  29. **    if a element leading by double quote mark and no terminating mark, all
  30. **    the characters after that leading mark will be taken as a single
  31. **    argument item.
  32. **
  33. **    if o_argv is NULL, phrase_command_line only return the argument count
  34. **    of command line, else phrase_command_line will modify cmd_line and set
  35. **    o_argv array to point each argument item.
  36. **
  37. **    the return value of phrase_command_line function is the count of
  38. **    argument items, not including the NULL termiator.
  39. **
  40. **    please take carefull that o_argv must be great enough to store all the
  41. **    pointer array and a NULL terminator, otherwise, the program may be
  42. **    breaked down. thus, you'd better use phrase_command_line itself to
  43. **    caculate the proper count of arguments by giving a NULL o_argv and then
  44. **    allocate the memory of a pointer array with the length of this count
  45. **    plus one.
  46. */

  47. int phrase_command_line(char *cmd_line, char **o_argv)
  48. {
  49.         int argc;
  50.         if (cmd_line != NULL) {
  51.                 argc = 0;
  52.                 for (;;) {
  53.                         switch (*cmd_line) {
  54.                         case ' ':
  55.                         case '\t':
  56.                                 if (o_argv != NULL) {
  57.                                         *cmd_line = '\0';
  58.                                 }
  59.                                 cmd_line++;
  60.                                 continue;
  61.                         case '"':
  62.                                 if (o_argv != NULL) {
  63.                                         *cmd_line ='\0';
  64.                                         o_argv[argc] = cmd_line + 1;
  65.                                 }
  66.                                 cmd_line++;
  67.                                 switch (*cmd_line) {
  68.                                 case '"':
  69.                                 case '\r':
  70.                                 case '\n':
  71.                                 case '\0':
  72.                                         break;
  73.                                 default:
  74.                                         argc++;
  75.                                         break;
  76.                                 }
  77.                                 for (;;) {
  78.                                         switch (*cmd_line) {
  79.                                         case '"':
  80.                                                 if (o_argv != NULL) {
  81.                                                         *cmd_line = '\0';
  82.                                                 }
  83.                                                 cmd_line++;
  84.                                                 break;
  85.                                         case '\r':
  86.                                         case '\n':
  87.                                         case '\0':
  88.                                                 if (o_argv != NULL) {
  89.                                                         *cmd_line = '\0';
  90.                                                         o_argv[argc] = NULL;
  91.                                                 }
  92.                                                 return argc;
  93.                                         default:
  94.                                                 cmd_line++;
  95.                                                 continue;
  96.                                         }
  97.                                         break;
  98.                                 }
  99.                                 continue;
  100.                         case '\r':
  101.                         case '\n':
  102.                         case '\0':
  103.                                 if (o_argv != NULL) {
  104.                                         *cmd_line = '\0';
  105.                                         o_argv[argc] = NULL;
  106.                                 }
  107.                                 return argc;
  108.                         default:
  109.                                 if (o_argv != NULL) {
  110.                                         o_argv[argc] = cmd_line;
  111.                                 }
  112.                                 cmd_line++;
  113.                                 argc++;
  114.                                 for (;;) {
  115.                                         switch (*cmd_line) {
  116.                                         case '"':
  117.                                                 break;
  118.                                         case ' ':
  119.                                         case '\t':
  120.                                                 if (o_argv != NULL) {
  121.                                                         *cmd_line = '\0';
  122.                                                 }
  123.                                                 cmd_line++;
  124.                                                 break;
  125.                                         case '\r':
  126.                                         case '\n':
  127.                                         case '\0':
  128.                                                 if (o_argv != NULL) {
  129.                                                         *cmd_line = '\0';
  130.                                                         o_argv[argc] = NULL;
  131.                                                 }
  132.                                                 return argc;
  133.                                         default:
  134.                                                 cmd_line++;
  135.                                                 continue;
  136.                                         }
  137.                                         break;
  138.                                 }
  139.                                 continue;
  140.                         }
  141.                 }
  142.         }
  143.         return -1;
  144. }

  145. #include <stdio.h>
  146. #include <stdlib.h>

  147. int main(void)
  148. {
  149.         char command_line[1024];
  150.         int argc;
  151.         char **argv;
  152.         int i;

  153.         printf("COMMAND> ");
  154.         if (fgets(command_line, sizeof(command_line), stdin) != NULL) {
  155.                 argc = phrase_command_line(command_line, NULL);
  156.                 if (argc > 0) {
  157.                         argv = (char **)malloc(sizeof(char *) * (argc + 1));
  158.                         if (argv != NULL) {
  159.                                 phrase_command_line(command_line, argv);
  160.                                 printf("argv:\n\n");
  161.                                 for (i = 0; i < argc; i++) {
  162.                                         printf("  %d: %s\n", i, argv[i]);
  163.                                 }
  164.                                 free(argv);
  165.                                 return 0;
  166.                         }
  167.                 }
  168.         }
  169.         return -1;
  170. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2011-03-11 14:56 |只看该作者
用getopt和getopt_long吧,接口统一

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
7 [报告]
发表于 2011-03-11 15:03 |只看该作者
用getopt和getopt_long吧,接口统一
csern 发表于 2011-03-11 14:56



    楼主这是要分割命令行为参数呢,不是要分析参数。就是在main之前做的事。crt0?{:3_198:}

论坛徽章:
0
8 [报告]
发表于 2011-03-11 15:54 |只看该作者
本帖最后由 csern 于 2011-03-11 15:59 编辑

回复 7# cobras

刚才没仔细看,不好意思了,貌似是个类似于命令行ftp客户端的东西吧。
  1. command->argv = NULL;
  2. command->name = command->argv[0];
复制代码
这要段错误的吧,lz贴错代码了么,

论坛徽章:
0
9 [报告]
发表于 2011-03-11 16:07 |只看该作者
本帖最后由 thb303663990 于 2011-03-11 16:08 编辑

回复 8# csern


   恩,应该是 command->argv=NULL,系统好像显示不出来

论坛徽章:
2
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11
10 [报告]
发表于 2011-03-11 16:10 |只看该作者
我分析LZ的struct command_line的结构是不是这样的:

  1. struct command_line {
  2.     char **argv;
  3.     char *name;
  4. };
复制代码
如果是这样的,哪么你的代码完全错误,不仅功能无法实现,而且还有致命错误。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP