2009-04-14

load_defualt


感觉条件,或从默认配置文件里读取参数配置,或从指定配置文件里读取参数.

如果指定了配置文件,则从配置文件里读取参数.
如果指定了使用默认配置文件配置,则从默认配置文件读取参数.
如果以上都没有指定,则从默认的配置目录读取配置文件.

参数读取的方式都是递归的.
也就是说,如果配置文件包含类似 !include 之类的语句的话,会递归调用search_default_file 这个函数(位于 mysys/default.c line 574 )


// mysys/default.c
/*
Read options from configurations files

SYNOPSIS
load_defaults()
conf_file Basename for configuration file to search for.
If this is a path, then only this file is read.
groups Which [group] entrys to read.
Points to an null terminated array of pointers
argc Pointer to argc of original program
argv Pointer to argv of original program

IMPLEMENTATION

Read options from configuration files and put them BEFORE the arguments
that are already in argc and argv. This way the calling program can
easily command line options override options in configuration files

NOTES
In case of fatal error, the function will print a warning and do
exit(1)

To free used memory one should call free_defaults() with the argument
that was put in *argv

RETURN
0 ok
1 The given conf_file didn't exists
*/


int load_defaults(const char *conf_file, const char **groups,
int *argc, char ***argv)
{
DYNAMIC_ARRAY args;
TYPELIB group;
my_bool found_print_defaults= 0;
uint args_used= 0;
int error= 0;
MEM_ROOT alloc;
char *ptr,**res;
struct handle_option_ctx ctx;
DBUG_ENTER("load_defaults");

init_default_directories();
init_alloc_root(&alloc,512,0);
/*
Check if the user doesn't want any default option processing
--no-defaults is always the first option
*/
if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults"))
{
/* remove the --no-defaults argument and return only the other arguments */
uint i;
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
(*argc + 1)*sizeof(char*)))) //申请内存,并将其添加到alloc的free链表里.返回实际可用区间的指针.
goto err;
res= (char**) (ptr+sizeof(alloc)); //上面char* 指针存放的开始位置.位MEM_ROOT结构预留位置
res[0]= **argv; /* Copy program name */
for (i=2 ; i < (uint) *argc ; i++) //存放参数,忽略已有的 --no-defaults 参数
res[i-1]=argv[0][i];
res[i-1]=0; /* End pointer */
(*argc)--; //忽略 --no-defaults 参数
*argv=res; //重置参数列表
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
DBUG_RETURN(0);
} //以上动作是把main的命令行参数存到自己的内存区域里.MEM_ROOT链表.

group.count=0;
group.name= "defaults";
group.type_names= groups; //groups = { "mysql","client",0 }.

for (; *groups ; groups++)
group.count++;
//至此.group结构初始化完毕.
if (my_init_dynamic_array(&args, sizeof(char*),*argc, 32)) //将args指向指定大小的内存区域.
goto err;

ctx.alloc= &alloc;
ctx.args= &args;
ctx.group= &group;
//conf_file = "my".递归将 'my' 的配置项目写入ctx指定的上下文环境中
error= my_search_option_files(conf_file, argc, argv, &args_used,
handle_default_option, (void *) &ctx); //conf_file = "my" 以上动作,handle_default_option 将option插入到指定的上下文中
/*
Here error contains <> 0 only if we have a fully specified conf_file
or a forced default file
*/
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
(args.elements + *argc +1) *sizeof(char*)))) // 在alloc指定的上下文环境中,为参数开辟内存区域
goto err;
res= (char**) (ptr+sizeof(alloc)); //偏移内存链表头数据结构位置

/* copy name + found arguments + command line arguments to new array */
res[0]= argv[0][0]; /* Name MUST be set, even by embedded library */
memcpy((uchar*) (res+1), args.buffer, args.elements*sizeof(char*)); //将命令行参数复制到上下文环境
/* Skip --defaults-xxx options */
(*argc)-= args_used;
(*argv)+= args_used;

/*
Check if we wan't to see the new argument list
This options must always be the last of the default options
*/
if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults"))
{
found_print_defaults=1;
--*argc; ++*argv; /* skip argument */
}
// 经过场面的参数过滤,重新复制参数到上下文环境
if (*argc)
memcpy((uchar*) (res+1+args.elements), (char*) ((*argv)+1),
(*argc-1)*sizeof(char*));
res[args.elements+ *argc]=0; /* last null */

(*argc)+=args.elements; // 更新参数个数,加上从配置文件读取的参数个数
*argv= (char**) res; // 将agrv重形象到上下文环境的参数区域.即现在存放原有参数和配置文件参数的那块区域
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
delete_dynamic(&args); //释放已经不需要的aggrs.其内容已经放置到上下文环境中了.
if (found_print_defaults)
{
int i;
printf("%s would have been started with the following arguments:\n",
**argv);
for (i=1 ; i < *argc ; i++)
printf("%s ", (*argv)[i]);
puts("");
exit(0);
}
DBUG_RETURN(error);

err:
fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
exit(1);
return 0; /* Keep compiler happy */
}

没有评论:

发表评论

瑕不掩瑜

新加坡哪吒2终于上映了. 也终于有机会去看了. 客观地说, 剧本应该是还算可以的.但是叙事成熟度还是不太够. 虽然哪吒二阶重生的片段确实很打动人,但切割开来看的话,缺少一个比较明显的叙事主线. 或者说在剧情长短安排上还是有些不太平衡. 像第一关的土拨鼠. 作为一个单元片段放出来算...