Chinaunix首页 | 论坛 | 博客

分类: LINUX

2011-10-12 14:10:20

下面就是运行修改default的设置了。
  1. 983       switch (opt->type)
  2. 984         {
  3. 985         case OPT_VALUE:  /*如果得到的是含有参数的选项*/

  4. 986           setoptval (opt->data, optarg, opt->long_name); /*1、______--------->
  5. 987           break;
复制代码
  1. 781 void
  2. 782 setoptval (const char *com, const char *val, const char *optname)/*com就是命令了,val是选项参数,optname就是长选项的名字*/
  3. 783 {
  4. 784   /* Prepend "--" to OPTNAME. */
  5. 785   char *dd_optname = (char *) alloca (2 + strlen (optname) + 1);
  6. 786   dd_optname[0] = '-';
  7. 787   dd_optname[1] = '-';
  8. 788   strcpy (dd_optname + 2, optname);  /*前面几个是在optname前面加上 "--"符号*/
  9. 789  
  10. 790   assert (val != NULL);
  11. 791   if (!setval_internal (command_by_name (com), dd_optname, val))/*2、_______----->重要的函数在这里*/
  12. 792     exit (2);
  13. 793 }
复制代码
  1. /*这是折半法查找对应项的索引值。就是commands[]中的数组下标,找到返回此下标,如果没有就返回  -1 。*/
  2. 274 static int
  3. 275 command_by_name (const char *cmdname)
  4. 276 {
  5. 277   /* Use binary search for speed.  Wget has ~100 commands, which
  6. 278      guarantees a worst case performance of 7 string comparisons.  */
  7. 279   int lo = 0, hi = countof (commands) - 1;
  8. 280
  9. 281   while (lo <= hi)
  10. 282     {
  11. 283       int mid = (lo + hi) >> 1;
  12. 284       int cmp = strcasecmp (cmdname, commands[mid].name);
  13. 285       if (cmp < 0)
  14. 286         hi = mid - 1;
  15. 287       else if (cmp > 0)
  16. 288         lo = mid + 1;
  17. 289       else
  18. 290         return mid;
  19. 291     }
  20. 292  return -1;
  21. }
  22. [code]
  23. 732 static bool
  24. 733 setval_internal (int comind, const char *com, const char *val) /*
  25. 734 {
  26. 735   assert (0 <= comind && ((size_t) comind) < countof (commands));
  27. 736   DEBUGP (("Setting %s (%s) to %s\n", com, commands[comind].name, val));
  28. 737   return commands[comind].action (com, val, commands[comind].place);/*这里就又一步处理解析过程3、________---------->*/
  29. 738 }
复制代码
  1. commands[comind].action() ,其中有这几中类型:
  2. 117   { "accept",           &opt.accepts,           cmd_vector },
  3. 118   { "addhostdir",       &opt.add_hostdir,       cmd_boolean },
  4. 126   { "backups",          &opt.backups,           cmd_number },
  5. 127   { "base",             &opt.base_href,         cmd_string },
  6. 130   { "cacertificate",    &opt.ca_cert,           cmd_file },
  7. 134   { "cadirectory",      &opt.ca_directory,      cmd_directory },
  8. 135   { "certificate",      &opt.cert_file,         cmd_file },
  9. 136   { "certificatetype",  &opt.cert_type,         cmd_cert_type },
  10. etc*/
  11.                这些都是去改变command[]数组中的place的值*/
  12. 因为类似这里只说一个*/
复制代码
  1. 957 static bool
  2. 958 cmd_vector (const char *com, const char *val, void *place) /*place是指
  3. 959 {
  4. 960   char ***pvec = (char ***)place;
  5. 961   
  6. 962   if (*val)
  7. 963     *pvec = merge_vecs (*pvec, sepstring (val));/sepstring()将参数再次解析成以数粗形式存储的变量4、___------------->*/
复制代码
  1. 1310 char **
  2. 1311 merge_vecs (char **v1, char **v2)
  3. 1312 {   
  4. 1313   int i, j;
  5. 1314   
  6. 1315   if (!v1)
  7. 1316     return v2;
  8. 1317   if (!v2)
  9. 1318     return v1;
  10. 1319   if (!*v2)
  11. 1320     {
  12. 1321       /* To avoid j == 0 */
  13. 1322       xfree (v2);
  14. 1323       return v1; /*如果参数是0的话,那么就采用默认值*/
  15. 1324     }
  16. 1325   /* Count v1.  */
  17. 1326   for (i = 0; v1[i]; i++)
  18. 1327     ;
  19. 1328   /* Count v2.  */
  20. 1329   for (j = 0; v2[j]; j++)
  21. 1330     ;
  22. 1331   /* Reallocate v1.  */
  23. 1332   v1 = xrealloc (v1, (i + j + 1) * sizeof (char **));  
  24. 1333   memcpy (v1 + i, v2, (j + 1) * sizeof (char *));
  25. 1334   xfree (v2);
  26. 1335   return v1;
  27. 1336 }
复制代码
/*这里是改变commands[]数组中 *place的内容,就是改变的当初defaults[]的内容*/
  1. 下面就是一系列的判断,如果出错就打印出来*/
  2. 比如
  3. if(!opt.locale)
  4. opt.locale = find_locale(); /*最终为系统调用*/

  5. 1202   if (opt.ask_passwd)
  6. 1203     {
  7. 1204       opt.passwd = prompt_for_password (); /*接受密码6、________----->*/
  8. 1205
  9. 1206       if (opt.passwd == NULL || opt.passwd[0] == '\0')
  10. 1207         exit (1);
  11. 1208     }
复制代码
  1. 6、________---------->
  2. static char *prompt_for_password(void)
  3. {
  4. . . .
  5. return getpass(""); /*接受函数*/
  6. }
复制代码
  1. 86 char *
  2. 87 getpass (const char *prompt)
  3. 88 {
  4. 89   FILE *tty;
  5. 90   FILE *in, *out;
  6. 91   struct termios s, t;
  7. 92   bool tty_changed = false;
  8. 93   static char *buf;
  9. 94   static size_t bufsize;
  10. 95   ssize_t nread;
  11. 96
  12. 97   /* Try to write to and read from the terminal if we can.
  13. 98      If we can't open the terminal, use stderr and stdin.  */
  14. 99
  15. 100   tty = fopen ("/dev/tty", "w+");
  16. 101   if (tty == NULL)  /*如果可以从终端输入打开终端设备文件,进行输入*/
  17. 102     {
  18. 103       in = stdin;  /*如果没有,那么用标准输入进行输出*/
  19. 104       out = stderr; /*输出文件为stderr*/
  20. 105     }
  21. 106   else
  22. 107     {
  23. 108       /* We do the locking ourselves.  */
  24. 109       __fsetlocking (tty, FSETLOCKING_BYCALLER);
  25. 110
  26. 111       out = in = tty;
  27. 112     }
  28. 113
  29. 114   flockfile (out);
  30. . . . . .  .  /*省略的是对文件进行加锁*/
  31. 133   nread = getline (&buf, &bufsize, in);
阅读(964) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~