main 函數首先獲得程式的名字,大概是wget的名稱
系統宏:
#ifdef TARGET_OS_MAC#endif#ifdef __linux__//標識是linux系統#endif#ifdef _WIN32 || _WIN64//不同作業系統使用不同的宏#endif
Option_data |
Long_option |
long_name(char*) |
name |
short_name(char) |
Optmap為short_name做索引 |
Type(enum) |
Has_arg base on type 不是bool和OPT_VALUE的直接使用argtype參數作為has_arg結果 |
Data(void*) |
Val->option_data index |
Argtype(int) |
Has_arg base on type 不是bool和OPT_VALUE的直接使用argtype參數作為has_arg結果 |
optmap 映射shortname 到long_option的索引
所使用的方法是optmap[opt->short_name- 32] = longopt - long_options;
從main函數開始:
首先獲得程式的名字如果是wget在window上啟動並執行話,還得去掉.exe尾碼名,linux就不用了,linux上只要有可執行許可權就好了
i18n_initialize ();//配置國際化的東西,和系統內容有關係,這裡就是為了支援所有語言環//境 exec_name = strrchr (argv[0], PATH_SEPARATOR); if (!exec_name) exec_name = argv[0]; else ++exec_name;#ifdef WINDOWS /* Drop extension (typically .EXE) from executable filename. */ windows_main ((char **) &exec_name);#endif
設定opt選項的預設值和調用設定檔
initialize (void){ char *file; intok = true; defaults ();//這裡設定wget中opt的預設值,為了是初始化。#ifdef SYSTEM_WGETRC if(file_exists_p (SYSTEM_WGETRC)) ok &= run_wgetrc (SYSTEM_WGETRC);#endif /*Override it with your own, if one exists. */ file = wgetrc_file_name ();//'TODO::linux上是不存在這個檔案的。 if(!file)//llinux 上不存在wgetrc的設定檔直接返回return;
下面的函數吧option_data轉化成long_option 和optmap
TODO::這裡是根據option_data 來轉換short_option和long_option的
static voidinit_switches (void){ char *p = short_options; inti, o = 0; for(i = 0; i < countof (option_data); i++) { struct cmdline_option *opt = &option_data[i]; struct option *longopt; if (!opt->long_name) /* The option is disabled. */ continue; longopt = &long_options[o++]; longopt->name = opt->long_name; /* long_option option_data name -> long_name; val -> option_data 所在索引。 has_arg -> re_argument,optional_argment, noset the member of flag*****?????****** add by LeoK 2011-11-28 */ longopt->val = i; if (opt->short_name) { *p++ = opt->short_name; optmap[opt->short_name - 32] = longopt - long_options; } switch (opt->type) { case OPT_VALUE: longopt->has_arg = required_argument; if (opt->short_name) *p++ = ':'; break; case OPT_BOOLEAN: /* Specify an optional argument for long options, so that --option=off works the same as --no-option, for compatibility with pre-1.10 Wget. However, don't specify optional arguments short-option booleans because they prevent combining of short options. */ longopt->has_arg = optional_argument; longopt = &long_options[o++]; longopt->name = no_prefix (opt->long_name); longopt->has_arg = no_argument; longopt->val = i | BOOLEAN_NEG_MARKER; break; default: assert (opt->argtype != -1); longopt->has_arg = opt->argtype; if (opt->short_name) { if (longopt->has_arg ==required_argument) *p++ = ':'; } } } *p= '\0'; assert (o <= countof (long_options));}
note:
單詞:catalog:目錄,編目錄bail out: 保釋parse:解析,分析verbose:冗長fatal:致命的,致死的recursive:遞迴的,迴歸的span:跨越,穿越porting:移植reside:所在目錄,居住