读取并分析wgetrc文件 2013-02-23 21:07 529人阅读 评论(0) 收藏
2013-02-23 21:07
218 查看
main():
run_wgetrc():
parse_line():
/* If the user did not specify a config, read the system wgetrc and ~/.wgetrc. */ if (use_userconfig == false) initialize ();initialize():
/* Initialize the defaults and run the system wgetrc and user's own wgetrc. */ void initialize (void) { char *file, *env_sysrc; bool ok = true; /* Run a non-standard system rc file when the according environment variable has been set. For internal testing purposes only! */ env_sysrc = getenv ("SYSTEM_WGETRC"); if (env_sysrc && file_exists_p (env_sysrc)) { ok &= run_wgetrc (env_sysrc); /* If there are any problems parsing the system wgetrc file, tell the user and exit */ if (! ok) { fprintf (stderr, _("\ Parsing system wgetrc file (env SYSTEM_WGETRC) failed. Please check\n\ '%s',\n\ or specify a different file using --config.\n"), env_sysrc); exit (2); } } /* Otherwise, if SYSTEM_WGETRC is defined, use it. */ #ifdef SYSTEM_WGETRC else if (file_exists_p (SYSTEM_WGETRC)) ok &= run_wgetrc (SYSTEM_WGETRC); /* If there are any problems parsing the system wgetrc file, tell the user and exit */ if (! ok) { fprintf (stderr, _("\ Parsing system wgetrc file failed. Please check\n\ '%s',\n\ or specify a different file using --config.\n"), SYSTEM_WGETRC); exit (2); } #endif /* Override it with your own, if one exists. */ file = wgetrc_file_name (); if (!file) return; /* #### We should canonicalize `file' and SYSTEM_WGETRC with something like realpath() before comparing them with `strcmp' */ #ifdef SYSTEM_WGETRC if (!strcmp (file, SYSTEM_WGETRC)) { fprintf (stderr, _("\ %s: Warning: Both system and user wgetrc point to %s.\n"), exec_name, quote (file)); } else #endif ok &= run_wgetrc (file); /* If there were errors processing either `.wgetrc', abort. */ if (!ok) exit (2); xfree (file); return; }
run_wgetrc():
/* Initialize variables from a wgetrc file. Returns zero (failure) if there were errors in the file. */ bool run_wgetrc (const char *file) { FILE *fp; char *line; int ln; int errcnt = 0; fp = fopen (file, "r"); if (!fp) { fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name, file, strerror (errno)); return true; /* not a fatal error */ } ln = 1; while ((line = read_whole_line (fp)) != NULL) { char *com = NULL, *val = NULL; int comind; /* Parse the line. */ switch (parse_line (line, &com, &val, &comind)) { case line_ok: /* If everything is OK, set the value. */ if (!setval_internal_tilde (comind, com, val)) { fprintf (stderr, _("%s: Error in %s at line %d.\n"), exec_name, file, ln); ++errcnt; } break; case line_syntax_error: fprintf (stderr, _("%s: Syntax error in %s at line %d.\n"), exec_name, file, ln); ++errcnt; break; case line_unknown_command: fprintf (stderr, _("%s: Unknown command %s in %s at line %d.\n"), exec_name, quote (com), file, ln); ++errcnt; break; case line_empty: break; default: abort (); } xfree_null (com); xfree_null (val); xfree (line); ++ln; } fclose (fp); return errcnt == 0; }
parse_line():
static enum parse_line parse_line (const char *line, char **com, char **val, int *comind) { const char *p; const char *end = line + strlen (line); const char *cmdstart, *cmdend; const char *valstart, *valend; char *cmdcopy; int ind; /* Skip leading and trailing whitespace. */ while (*line && c_isspace (*line)) ++line; while (end > line && c_isspace (end[-1])) --end; /* Skip empty lines and comments. */ if (!*line || *line == '#') return line_empty; p = line; cmdstart = p; while (p < end && (c_isalnum (*p) || *p == '_' || *p == '-')) ++p; cmdend = p; /* Skip '=', as well as any space before or after it. */ while (p < end && c_isspace (*p)) ++p; if (p == end || *p != '=') return line_syntax_error; ++p; while (p < end && c_isspace (*p)) ++p; valstart = p; valend = end; /* The syntax is valid (even though the command might not be). Fill in the command name and value. */ *com = strdupdelim (cmdstart, cmdend); *val = strdupdelim (valstart, valend); /* The line now known to be syntactically correct. Check whether the command is valid. */ BOUNDED_TO_ALLOCA (cmdstart, cmdend, cmdcopy); dehyphen (cmdcopy); ind = command_by_name (cmdcopy); if (ind == -1) return line_unknown_command; /* Report success to the caller. */ *comind = ind; return line_ok; }
相关文章推荐
- Java读取文件内容的几种方式 分类: Java 2015-07-20 17:55 2人阅读 评论(0) 收藏
- 详解python linecache模块读取文件的方法 分类: python Module 2014-07-21 18:32 1057人阅读 评论(0) 收藏
- 依次读取文件中的一个字符 分类: python基础学习 python 小练习 2013-08-22 11:14 289人阅读 评论(0) 收藏
- 【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法 分类: H3_NUTCH 2014-08-22 21:57 928人阅读 评论(0) 收藏
- winform 读取TXT文件 放在Label中 分类: WinForm 2014-07-31 09:56 310人阅读 评论(0) 收藏
- mpeg文件格式分析 分类: 生活百科 2014-06-19 14:25 426人阅读 评论(0) 收藏
- HDFS读取文件步骤 分类: hadoop 2015-05-27 22:08 116人阅读 评论(0) 收藏
- HDFS读取文件步骤 分类: hadoop 2015-05-27 22:08 117人阅读 评论(0) 收藏
- 使用C语言读取properties文件V1.0 分类: H_HISTORY 2013-02-26 12:37 946人阅读 评论(0) 收藏
- 【转载】fscanf和fprintf函数从文件中格式化读取double型数据 分类: Linux --- C 2014-11-11 18:45 98人阅读 评论(0) 收藏
- 使用glob模块,遍历E盘下的mp3文件 分类: python 小练习 2013-05-15 15:40 405人阅读 评论(0) 收藏
- 如何把任意文件隐藏在一张图片里? 分类: DirectX 2013-11-15 16:05 524人阅读 评论(0) 收藏
- ubuntu下卸载.run文件 分类: 软件插件学习 2014-12-17 16:39 163人阅读 评论(0) 收藏
- AndroidManifest.xml配置文件详解-… 分类: Android开发 2014-05-30 10:54 46人阅读 评论(0) 收藏
- 文件的截取:truncate方法 分类: open()文件读写 2014-01-21 18:59 245人阅读 评论(0) 收藏
- Cloudera Manager(CDH5)内部结构、功能包括配置文件、目录位置等 2016-05-26 15:46 2112人阅读 评论(0) 收藏 举报 分类: CDH(19) 1. 相关
- 15. 访问文件 2010-02-28 22:55 129人阅读 评论(0) 收藏
- Hi3531用SPI FLASH启动 使用Nand做文件系统 分类: HI3531 2013-08-28 10:26 884人阅读 评论(0) 收藏
- hpp文件 分类: C/C++ 2015-04-28 10:54 26人阅读 评论(0) 收藏
- .NET中反射机制的使用与分析 分类: .NET 2014-02-17 15:51 346人阅读 评论(0) 收藏