专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
嵌入式
嵌入式 wget 参数解析篇
2019-07-12 18:25
发布
生成海报
站内文章
/
嵌入式Linux
11555
0
1230
1 整体概括:
前提说明:
本篇wget分析仅仅是参数解析内容,不包括wget的递归和非递归下载,后面文章会陆续进行分析。本次主要分析参数为tries(t) timeout(T) no-clobber quiet(q) recursive(r) help(h)version(V) append-output(a) execute(e) no(n) clobber, 其中括号里面的为wget短选项,括号前面的为长选项。
在wget运行下载文件或页面时,用户可以通过参数来改变wget的行为,比如想查看wget的调试和http数据包可以使用 wget --debug
www.baidu.com/index.html
。
我们这次分析下载url以baidu 搜索页面(
http://www.baidu.com/index.html
)为样本,进行分析不同类型的参数,以达到抛砖引玉的目的。wget支持长选项和短选项,比如输出调试信息短选项为-d长选项为—debug
wget有全局的struct options opt;保存着wget用户参数设置值,来修改wget行为。本篇主要讲解用户输入参数如何转化为 opt的成员。
wget分析的版本为1.13,gcc版本为3.4.5,linux内核版本2.6.9_5-9-0-0
2 详细代码解析:
2.1数据结构
wget 对于配置转化,设置struct options opt 有两张表和长短选项数组
命令行表:
struct cmdline_option option_data
此表保存着wget支持的长短选项和长短选项属性
命令转化设置opt表:
commands
此表用于设置根据参数来设置opt成员。
长选项:
struct option long_options[2*countof(option_data) + 1]
短选项:
struct char short_options[128]
2.2参数解析流程
Main 首先根据不同平台来设置使用时间函数,blog里有monotonic time和wall time讲解,这里就不分析。
2.2.1 defaults();
然后调用defaults函数,该函数主要是给全局opt设置默认值(
因为代码太长,给出部分代码
)。
[cpp]
view plain
copy
//#######################src/init.c
/* Reset the variables to default values. */
void
defaults (
void
)
{
char
*tmp;
/* Most of the default values are 0 (and 0.0, NULL, and false).
Just reset everything, and fill in the non-zero values. Note
that initializing pointers to NULL this way is technically
illegal, but porting Wget to a machine where NULL is not all-zero
bit pattern will be the least of the implementors' worries. */
xzero (opt);
opt.cookies =
true
;
opt.verbose = -1;
opt.ntry = 20;
opt.reclevel = 5;
opt.add_hostdir =
true
;
opt.netrc =
true
;
opt.ftp_glob =
true
;
2.2.2 init_switches()
函数很简单,追加一些ch注释
[cpp]
view plain
copy
static
void
init_switches (
void
)
{
//p指向短选项数组
char
*p = short_options;
size_t
i, 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指向长选项一个依次节点
longopt = &long_options[o++];
//长选项name指向opt的long_name
longopt->name = opt->long_name;
//长选项val执行opt的数组索引,用于根据长选项查找opt
longopt->val = i;
if
(opt->short_name)
{
//如果存在短选项,把opt short_name保存在short_options中
*p++ = opt->short_name;
//用optmap保存short_name的value 来索引长选项数组
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:
/* 如果是bool类型(开关类型参数) 需要支持--option=off and --no-option .look the note of the blow*/
/* 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;
/* For Boolean options, add the "--no-FOO" variant, which is
identical to "--foo", except it has opposite meaning and
it doesn't allow an argument. */
longopt = &long_options[o++];
longopt->name = no_prefix (opt->long_name);
longopt->has_arg = no_argument;
/* Mask the value so we'll be able to recognize that we're
dealing with the false value. */
//索引加一个负数符号
longopt->val = i | BOOLEAN_NEG_MARKER;
break
;
default
:
//others 根据情况设置不同的值
assert (opt->argtype != -1);
longopt->has_arg = opt->argtype;
if
(opt->short_name)
{
Ta的文章
更多
>>
十分钟读懂『卡尔曼滤波算法』
0 个评论
H.264算法的优化策略
0 个评论
嵌入式 wget 参数解析篇
0 个评论
热门文章
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮