diff --git a/chsrc.c b/chsrc.c deleted file mode 100644 index ee5b3a6..0000000 --- a/chsrc.c +++ /dev/null @@ -1,1107 +0,0 @@ -/* -------------------------------------------------------------- -* File : chsrc.c -* Authors : Aoran Zeng -* Created on : <2023-08-28> -* Last modified : <2023-09-01> -* -* chsrc: -* -* Change Source —— 命令行换源工具 -* -------------------------------------------------------------*/ - -#include "chsrc.h" - -#define Chsrc_Version "v0.1.0.20230910.pre" - - -/** - * 检测二进制程序是否存在 - * - * @param[in] check_cmd 检测 `progname` 是否存在的一段命令,一般来说,填 `progname` 本身即可, - * 但是某些情况下,需要使用其他命令绕过一些特殊情况,比如 python 这个命令在Windows上 - * 会自动打开 Microsoft Store,需避免 - * - * @param[in] progname 要检测的二进制程序名 - */ -bool -does_the_program_exist (char* check_cmd, char* progname) -{ - char* which = check_cmd; - - int ret = system(which); - - char buf[32] = {0}; - sprintf(buf, "错误码: %d", ret); - - if (0!=ret) { - xy_warn (xy_strjoin(4, "× 命令 ", progname, " 不存在,", buf)); - return false; - } else { - xy_success (xy_strjoin(3, "√ 命令 ", progname, " 存在")); - return true; - } -} - - - -/** - * 测速代码参考自 https://github.com/mirrorz-org/oh-my-mirrorz/blob/master/oh-my-mirrorz.py - * 修改为C语言,一切功劳属于原作者 - * - * @return 返回测得的速度,若无速度或出错,返回0 - */ -double -test_speed (char* url) -{ - char* curl_cmd, *devnull = NULL; - if (xy_on_windows) - devnull = "nul"; - else - devnull = "/dev/null"; - - curl_cmd = xy_strjoin(4, "curl -qs -o ", devnull, "-w '%{http_code} %{speed_download}' -m8 -A chsrc/" Chsrc_Version - "(+https://gitee.com/RubyMetric/chsrc)", url); - - FILE* fp = popen(curl_cmd, "r"); - char buf[64] = {0}; - fgets(buf, 64, fp); - fclose(fp); - char* split = strchr(buf, ' '); - *split = '\0'; - int http_code = atoi(buf); - double speed = atof(split+1); - - char* speedstr = to_human_readable_speed(speed); - - puts(url); - - if (200!=http_code) { - xy_warn (xy_2strjoin("HTTP码 = ", buf)); - } - puts(xy_2strjoin("速度 = ", speedstr)); -} - - -char* -to_human_readable_speed (double speed) -{ - char* scale[] = {"Byte/s", "KByte/s", "MByte/s", "GByte/s", "TByte/s"}; - int i = 0; - while (speed > 1024.0) - { - i += 1; - speed /= 1024.0; - } - char* buf = xy_malloc0(64); - sprintf(buf, "%.2f %s", speed, scale[i]); -} - - - -/** - * Perl换源 - * - * 参考:https://help.mirrors.cernet.edu.cn/CPAN/ - */ -void -pl_perl_setsrc (char* option) -{ - int selected = 0; char* check_cmd, *prog = NULL; - - if (xy_on_windows) check_cmd = "perl --version >nul 2>nul"; - else check_cmd = "perl --version 1>/dev/null 2>&1"; - - bool exist_b = does_the_program_exist (check_cmd, "perl"); - - if (!exist_b) { - xy_error ("chsrc: 未找到 perl 相关命令,请检查是否存在"); - return; - } - - for (int i=0;iname; - const char* source_abbr = pl_perl_sources[selected].mirror->abbr; - const char* source_url = pl_perl_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - char* cmd = xy_strjoin(3, - "perl -MCPAN -e 'CPAN::HandleConfig->edit(\"pushy_https\", 0); CPAN::HandleConfig->edit(\"urllist\", \"unshift\", \"", - source_url, - "\"); mkmyconfig'"); - - system(cmd); - free(cmd); - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - -/** - * NodeJS换源 - * - * 参考:https://npmmirror.com/ - */ -void -pl_nodejs_setsrc (char* option) -{ - int selected = 0; char* check_cmd, *prog = NULL; - - if (xy_on_windows) check_cmd = "npm -v >nul 2>nul"; - else check_cmd = "npm -v 1>/dev/null 2>&1"; - - bool exist_b = does_the_program_exist (check_cmd, "npm"); - - if (!exist_b) { - xy_error ("chsrc: 未找到 npm 相关命令,请检查是否存在"); - return; - } - - for (int i=0;iname; - const char* source_abbr = pl_nodejs_sources[selected].mirror->abbr; - const char* source_url = pl_nodejs_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - char* cmd = xy_2strjoin("npm config set registry ", source_url); - system(cmd); - free(cmd); - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - -/** - * Python换源 - * - * 参考:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/ - * - * 经测试,Windows上调用换源命令,会写入 C:\Users\RubyMetric\AppData\Roaming\pip\pip.ini - */ -void -pl_python_setsrc (char* option) -{ - int selected = 0; char* check_cmd, *prog = NULL; - - // 不要调用 python 自己,而是使用 python --version,避免Windows弹出Microsoft Store - if (xy_on_windows) check_cmd = "python --version >nul 2>nul"; - else check_cmd = "python --version 1>/dev/null 2>&1"; - - bool exist_b = does_the_program_exist (check_cmd, "python"); - - if (!exist_b) { - if (xy_on_windows) check_cmd = "python3 --version >nul 2>nul"; - else check_cmd = "python3 --version 1>/dev/null 2>&1"; - exist_b = does_the_program_exist (check_cmd, "python3"); - if (exist_b) prog = "python3"; - } - else { - prog = "python"; - } - - if (!exist_b) { - xy_error ("chsrc: 未找到 Python 相关命令,请检查是否存在"); - return; - } - - for (int i=0;iname; - const char* source_abbr = pl_python_sources[selected].mirror->abbr; - const char* source_url = pl_python_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - char* cmd = xy_2strjoin(prog, xy_2strjoin(" -m pip config set global.index-url ", source_url)); - system(cmd); - free(cmd); - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - -/** - * Ruby换源 - * - * 参考:https://gitee.com/RubyKids/rbenv-cn - */ -void -pl_ruby_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - for (int i=0;iname; - const char* source_abbr = pl_ruby_sources[selected].mirror->abbr; - const char* source_url = pl_ruby_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - xy_info("chsrc: 为 gem 命令换源"); - system("gem source -r https://rubygems.org/"); - - char* cmd = xy_2strjoin("gem source -a ", source_url); - system(cmd); - free(cmd); - - - if (xy_on_windows) check_cmd = "bundle -v >nul 2>nul"; - else check_cmd = "bundle -v 1>/dev/null 2>&1"; - exist_b = does_the_program_exist (check_cmd, "bundle"); - if (!exist_b) { - xy_error ("chsrc: 未找到 bundle 相关命令,请检查是否存在"); - return; - } - - cmd = xy_2strjoin("bundle config 'mirror.https://rubygems.org' ", source_url); - xy_info("chsrc: 为 bundler 命令换源"); - system(cmd); - free(cmd); - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - -/** - * Go换源 - * - * 参考:https://goproxy.cn/ - */ -void -pl_go_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - - if (xy_on_windows) check_cmd = "go --version >nul 2>nul"; - else check_cmd = "go --version 1>/dev/null 2>&1"; - - bool exist_b = does_the_program_exist (check_cmd, "go"); - - if (!exist_b) { - xy_error ("chsrc: 未找到 go 相关命令,请检查是否存在"); - return; - } - - for (int i=0;iname; - const char* source_abbr = pl_go_sources[selected].mirror->abbr; - const char* source_url = pl_go_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - char* cmd = "go env -w GO111MODULE=on"; - system(cmd); - - cmd = xy_strjoin(3, "go env -w GOPROXY=", source_url, ",direct"); - system(cmd); - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - -/** - * Rust 换源 - * - * 参考:https://help.mirrors.cernet.edu.cn/crates.io-index.git - */ -void -pl_rust_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - - for (int i=0;iname; - const char* source_abbr = pl_rust_sources[selected].mirror->abbr; - const char* source_url = pl_rust_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - - const char* file = xy_strjoin (3, - "[source.crates-io]\n" - "replace-with = 'mirror'\n\n" - - "[source.mirror]\n" - "registry = \"", source_url, "\""); - - - char* cmd = NULL; - if (xy_on_windows) - cmd = xy_strjoin(3, "echo ", file, ">> \%USERPROFILE%\\.cargo"); - else - cmd = xy_strjoin(3, "echo ", file, ">> $HOME/.cargo"); - - system(cmd); - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - - -/** - * NuGet 换源 - * - */ -void -pl_dotnet_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - - xy_error ("chsrc: 暂时无法为NuGet换源,若有需求,请您提交issue"); -} - - - - -/** - * PHP 换源 - * - * 参考:https://developer.aliyun.com/composer - */ -void -pl_php_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - - if (xy_on_windows) check_cmd = "composer --version >nul 2>nul"; - else check_cmd = "composer --version 1>/dev/null 2>&1"; - - bool exist_b = does_the_program_exist (check_cmd, "composer"); - - if (!exist_b) { - xy_error ("chsrc: 未找到 composer 相关命令,请检查是否存在"); - return; - } - - for (int i=0;iname; - const char* source_abbr = pl_php_sources[selected].mirror->abbr; - const char* source_url = pl_php_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - char* cmd = xy_2strjoin("composer config repo.packagist composer ", source_url); - system(cmd); - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - -/** - * Java 换源 - * - * 参考:https://developer.aliyun.com/mirror/maven - */ -void -pl_java_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - - if (xy_on_windows) check_cmd = "mvn --version >nul 2>nul"; - else check_cmd = "mvn --version 1>/dev/null 2>&1"; - bool mvn_exist_b = does_the_program_exist (check_cmd, "mvn"); - - if (xy_on_windows) check_cmd = "gradle --version >nul 2>nul"; - else check_cmd = "gradle --version 1>/dev/null 2>&1"; - bool gradle_exist_b = does_the_program_exist (check_cmd, "gradle"); - - if (!mvn_exist_b && !gradle_exist_b) { - xy_error ("chsrc: maven 与 gradle 命令均未找到,请检查是否存在(其一)"); - return; - } - - for (int i=0;iname; - const char* source_abbr = pl_java_sources[selected].mirror->abbr; - const char* source_url = pl_java_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - if (mvn_exist_b) { - const char* file = xy_strjoin(3, - "\n" - " aliyunmaven\n" - " *\n" - " 阿里云公共仓库\n" - " ", source_url, "\n" - ""); - - xy_info ("chsrc: 请在您的 maven安装目录/conf/settings.xml 中添加:\n"); - puts (file); - } - - if (gradle_exist_b) { - const char* file = xy_strjoin(3, - "allprojects {\n" - " repositories {\n" - " maven { url '", source_url, "' }\n" - " mavenLocal()\n" - " mavenCentral()\n" - " }\n" - "}"); - - xy_info ("chsrc: 请在您的 build.gradle 中添加:\n"); - puts (file); - } - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - -/** - * R 换源 - * - * 参考:https://help.mirrors.cernet.edu.cn/CRAN/ - */ -void -pl_r_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - - for (int i=0;iname; - const char* source_abbr = pl_r_sources[selected].mirror->abbr; - const char* source_url = pl_r_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - - const char* file = xy_strjoin (3, "options(\"repos\" = c(CRAN=\"", source_url, "\"))" ); - - - char* cmd = NULL; - // TODO: 待确认,Windows 下是否也是该文件 - if (xy_on_windows) - cmd = xy_strjoin(3, "echo ", file, " >> %USERPROFILE%/.Rprofile"); - else - cmd = xy_strjoin(3, "echo ", file, " >> ~/.Rprofile"); - - system(cmd); - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - - -/** - * Julia 换源 - * - * 参考:https://help.mirrors.cernet.edu.cn/julia/ - */ -void -pl_julia_setsrc (char* option) -{ - int selected = 0; char* check_cmd = NULL; - - for (int i=0;iname; - const char* source_abbr = pl_r_sources[selected].mirror->abbr; - const char* source_url = pl_r_sources[selected].url; - - xy_info (xy_2strjoin("chsrc: 选中镜像站:", source_abbr)); - - const char* file = xy_strjoin (3, "ENV[\"JULIA_PKG_SERVER\"] = \"", source_url, "\""); - - char* cmd = NULL; - // TODO: $JULIA_DEPOT_PATH/config/startup.jl 是否要考虑环境变量 - if (xy_on_windows) - cmd = xy_strjoin(3, "echo ", file, " >> %USERPROFILE%/.julia/config/startup.jl"); - else - cmd = xy_strjoin(3, "echo ", file, " >> ~/.julia/config/startup.jl"); - system(cmd); - - xy_success(xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - -/** - * ubuntu不同架构下的换源是不一样的,这个针对x86架构 - */ -void -os_ubuntu_setsrc (char* option) -{ - int selected = 0; - for (int i=0;iname; - const char* source_abbr = os_ubuntu_sources[selected].mirror->abbr; - const char* source_url = os_ubuntu_sources[selected].url; - - char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak"; - system(backup); - - xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); - - char* cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/ubuntu\\/@\\1", - source_url, - "@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list"); - system(cmd); - free(cmd); - - // char* rm = "rm -rf /etc/apt/source.list.bak"; - // system(rm); - - xy_info ("chsrc: 为 ubuntu 命令换源"); - xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} -/** - * Debian Buster 以上版本默认支持 HTTPS 源。如果遇到无法拉取 HTTPS 源的情况,请先使用 HTTP 源并安装 - * sudo apt install apt-transport-https ca-certificates - */ -void -os_debian_setsrc (char* option) -{ - int selected = 0; - for (int i=0;iname; - const char* source_abbr = os_ubuntu_sources[selected].mirror->abbr; - const char* source_url = os_ubuntu_sources[selected].url; - - char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak"; - system(backup); - - xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); - - char* cmd = xy_strjoin(3, "sed -E \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/debian\\/@\\1", - source_url, - "@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list"); - system(cmd); - free(cmd); - - // char* rm = "rm -rf /etc/apt/source.list.bak"; - // system(rm); - - xy_info ("chsrc: 为 debian 命令换源"); - xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} -/** - * fedora29版本及以下暂不支持 - */ -void -os_fedora_setsrc (char* option) -{ - int selected = 0; - for (int i=0;iname; - const char* source_abbr = os_fedora_sources[selected].mirror->abbr; - const char* source_url = os_fedora_sources[selected].url; - - char* backup = "cp -rf /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.bak"; - system(backup); - backup = "cp -rf /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.bak"; - system(backup); - - xy_info ("chsrc: 备份文件名:1. /etc/yum.repos.d/fedora.repo.bak"); - xy_info ("chsrc: 备份文件名:2. /etc/yum.repos.d/fedora-updates.repo.bak"); - - - char* cmd = xy_strjoin(9, "sed -e 's|^metalink=|#metalink=|g' ", - "-e 's|^#baseurl=http://download.example/pub/fedora/linux/|baseurl=", - source_url, - "|g' ", - "-i.bak ", - "/etc/yum.repos.d/fedora.repo ", - "/etc/yum.repos.d/fedora-modular.repo ", - "/etc/yum.repos.d/fedora-updates.repo ", - "/etc/yum.repos.d/fedora-updates-modular.repo"); - system(cmd); - free(cmd); - - xy_info("替换文件:/etc/yum.repos.d/fedora.repo"); - xy_info("新增文件:/etc/yum.repos.d/fedora-modular.repo"); - xy_info("替换文件:/etc/yum.repos.d/fedora-updates.repo"); - xy_info("新增文件:/etc/yum.repos.d/fedora-updates-modular.repo"); - - // char* rm = "rm -rf /etc/yum.repos.d/fedora.repo.bak"; - // system(rm); - // char* rm = "rm -rf /etc/yum.repos.d/fedora-updates.repo.bak"; - // system(rm); - - xy_info ("chsrc: 为 fedora 命令换源"); - xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - - - -void -os_kali_setsrc(char* option) -{ - int selected = 0; - for (int i=0;iname; - const char* source_abbr = os_kali_sources[selected].mirror->abbr; - const char* source_url = os_kali_sources[selected].url; - - char* backup = "cp -rf /etc/apt/sources.list /etc/apt/sources.list.bak"; - system(backup); - - xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); - - char* cmd = xy_strjoin(3, "sed -i \'s@(^[^#]* .*)http[:|\\.|\\/|a-z|A-Z]*\\/kali\\/@\\1", - source_url, - "@g\' /etc/apt/sources.list"); - system(cmd); - free(cmd); - - // char* rm = "rm -rf /etc/apt/source.list.bak"; - // system(rm); - - xy_info ("chsrc: 为 kali 命令换源"); - xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - -void -os_openbsd_setsrc(char* option) -{ - int selected = 0; - for (int i=0;iname; - const char* source_abbr = os_openbsd_sources[selected].mirror->abbr; - const char* source_url = os_openbsd_sources[selected].url; - - char* backup = "cp -rf /etc/installurl /etc/installurl.bak"; - system(backup); - - xy_info ("chsrc: 备份文件名: /etc/installurl.bak"); - - char* cmd = xy_strjoin(3,"echo ",source_url," > /etc/installurl"); - system(cmd); - free(cmd); - - // char* rm = "rm -rf /etc/installurl.bak"; - // system(rm); - - xy_info ("chsrc: 为 openbsd 命令换源"); - xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - - -void -os_mysys2_setsrc(char* option) -{ - int selected = 0; - for (int i=0;iname; - const char* source_abbr = os_mysys2_sources[selected].mirror->abbr; - const char* source_url = os_mysys2_sources[selected].url; - - char* backup = "cp -rf /etc/pacman.d/mirrorlist.mingw32 /etc/pacman.d/mirrorlist.mingw32.bak"; - system(backup); - backup = "cp -rf /etc/pacman.d/mirrorlist.mingw64 /etc/pacman.d/mirrorlist.mingw64.bak"; - system(backup); - backup = "cp -rf /etc/pacman.d/mirrorlist.msys /etc/pacman.d/mirrorlist.msys.bak"; - system(backup); - - xy_info ("chsrc: 备份文件名:1. /etc/pacman.d/mirrorlist.mingw32.bak"); - xy_info ("chsrc: 备份文件名:2. /etc/pacman.d/mirrorlist.mingw64.bak"); - xy_info ("chsrc: 备份文件名:3. /etc/pacman.d/mirrorlist.msys.bak"); - - char* cmd = xy_strjoin(3,"sed -i \"s#https\?://mirror.msys2.org/#",source_url,"#g\" /etc/pacman.d/mirrorlist* "); - system(cmd); - free(cmd); - - // char* rm = "rm -rf /etc/pacman.d/mirrorlist.mingw32.bak"; - // system(rm); - // rm = "rm -rf /etc/pacman.d/mirrorlist.mingw64.bak"; - // system(rm); - // rm = "rm -rf /etc/pacman.d/mirrorlist.msys.bak"; - // system(rm); - - xy_info ("chsrc: 为 mysys2 命令换源"); - xy_success (xy_2strjoin("chsrc: 感谢镜像提供方:", source_name)); -} - -#define setsrc_fn(func) (const char const*)func -#define getsrc_fn(func) (const char const*)func -#define sources_ptr(ptr) (const char const*)ptr -static const char const -*pl_ruby [] = {"gem", "ruby", "rb", "rubygems", NULL, setsrc_fn(pl_ruby_setsrc) , NULL, sources_ptr(pl_ruby_sources) }, -*pl_python[] = {"pip", "python", "py", "pypi", NULL, setsrc_fn(pl_python_setsrc), NULL, sources_ptr(pl_python_sources)}, -*pl_nodejs[] = {"npm", "node", "js", "nodejs", NULL, setsrc_fn(pl_nodejs_setsrc), NULL, sources_ptr(pl_nodejs_sources)}, -*pl_perl [] = {"perl", "cpan", NULL, setsrc_fn(pl_perl_setsrc), NULL, sources_ptr(pl_perl_sources)}, -*pl_rust [] = {"rust", "cargo", "crate", "crates", NULL, setsrc_fn(pl_rust_setsrc), NULL, sources_ptr(pl_rust_sources)}, -*pl_go [] = {"go", "golang", "goproxy", NULL, setsrc_fn(pl_go_setsrc), NULL, sources_ptr(pl_go_sources)}, -*pl_dotnet[] = {"nuget", "net", ".net", "dotnet", NULL, setsrc_fn(pl_dotnet_setsrc), NULL, sources_ptr(pl_dotnet_sources)}, -*pl_java [] = {"java", "maven", "gradle", NULL, setsrc_fn(pl_java_setsrc), NULL, sources_ptr(pl_java_sources)}, -*pl_php [] = {"php", "composer", NULL, setsrc_fn(pl_php_setsrc), NULL, sources_ptr(pl_php_sources)}, -*pl_cran [] = {"r", "cran", NULL, setsrc_fn(pl_r_setsrc), NULL, sources_ptr(pl_r_sources)}, -*pl_julia [] = {"julia", NULL, setsrc_fn(pl_julia_setsrc), NULL, sources_ptr(pl_julia_sources)}, -**pl_packagers[] = -{ - pl_ruby, pl_python, pl_nodejs, pl_perl, - pl_rust, pl_go, pl_dotnet, pl_java, pl_php, - pl_cran, pl_julia -}, - - -*os_ubuntu [] = {"ubuntu", NULL, setsrc_fn(os_ubuntu_setsrc), NULL,sources_ptr(os_ubuntu_sources)}, -*os_debian [] = {"debian", NULL, setsrc_fn(os_debian_setsrc), NULL,sources_ptr(os_debian_sources)}, -*os_fedora [] = {"fedora", NULL, setsrc_fn(os_fedora_setsrc), NULL,sources_ptr(os_fedora_sources)}, -*os_kali [] = {"kali", NULL, setsrc_fn(os_kali_setsrc), NULL,sources_ptr(os_kali_sources)}, -*os_openbsd [] = {"openbsd", NULL, setsrc_fn(os_openbsd_setsrc), NULL,sources_ptr(os_openbsd_sources)}, -*os_mysys2 [] = {"mysys2", NULL, setsrc_fn(os_mysys2_setsrc), NULL,sources_ptr(os_mysys2_sources)}, -**os_systems[] = -{ - os_ubuntu,os_debian,os_fedora,os_kali,os_openbsd,os_mysys2 -}, - - -*wr_anaconda[] = {"conda", "anaconda", NULL, NULL}, -*wr_tex [] = {"latex", "ctan", "tex", NULL, NULL}, -*wr_emacs [] = {"emacs", NULL, NULL}, -**wr_softwares[] = -{ - wr_anaconda, wr_tex, wr_emacs -}; -#undef getsrc_fn -#undef getsrc_fn -#undef sources_ptr - - - -static const char const* -usage[] = { - "chsrc: Change Source \e[1;35m" Chsrc_Version "\e[0m by \e[4;31mRubyMetric\e[0m\n", - - "维护: https://gitee.com/RubyMetric/chsrc\n", - - "使用:chsrc [target]", - "help # 打印此帮助,或 h, -h, --help", - "list (或 ls, 或 l) # 查看可用镜像源,和可换源软件", - "list mirror(s) # 查看可用镜像源", - "list target(s) # 查看可换源软件", - "list # 查看该软件可以使用哪些源", - "cesu # 对该软件所有源测速", - "get # 查看当前软件的源使用情况", - "set # 换源,自动测速后挑选最快源", - "set -1 # 1,2,3的1。换源,不测速,挑选经维护者测速排序的第一源", - "set -v # 换源,并打印换源所执行的具体操作\n" -}; - - -void -call_cmd (void* cmdptr, const char* arg) -{ - void (*cmd_func)(const char*) = cmdptr; - if (NULL==arg) { - xy_info("chsrc: 将使用默认镜像"); - } - cmd_func(arg); -} - - - -void -print_available_mirrors () -{ - xy_info ("chsrc: 支持以下镜像站,荣耀与感恩均归于这些站点,以及它们的开发/维护者们"); - for (int i=0; iabbr, mir->site); puts(mir->name); - } -} - - -void -print_supported_targets_ (const char const*** array, size_t size) -{ - for (int i=0; i -* Created on : <2023-08-29> -* Last modified : <2023-09-01> -* -* chsrc: -* -* chsrc.c 头文件 -* -------------------------------------------------------------*/ - -#include -#include "helper.h" - -typedef struct { - const char* abbr; - const char* name; - const char* site; -} mirror_info; - -// 教育网 -// 我们目前根据 https://github.com/mirrorz-org/oh-my-mirrorz 挑选速度前10位 -// -mirror_info - MirrorZ = {"MirrorZ", "MirrorZ校园网镜像站", "https://mirrors.cernet.edu.cn/"}, - Tuna = {"TUNA", "清华大学开源软件镜像站", "https://mirrors.tuna.tsinghua.edu.cn/"}, - Sjtug_Zhiyuan = {"SJTUG-zhiyuan", "上海交通大学致远镜像站", "https://mirrors.sjtug.sjtu.edu.cn/"}, - Zju = {"ZJU", "浙江大学开源软件镜像站", "https://mirrors.zju.edu.cn/"}, - Lzuoss = {"LZUOSS", "兰州大学开源社区镜像站", "https://mirror.lzu.edu.cn/"}, - Jlu = {"JLU", "吉林大学开源镜像站", "https://mirrors.jlu.edu.cn/"}, - Bfsu = {"BFSU", "北京外国语大学开源软件镜像站","https://mirrors.bfsu.edu.cn/"}, - Pku = {"PKU", "北京大学开源镜像站", "https://mirrors.pku.edu.cn/"}, - Bjtu = {"BJTU", "北京交通大学自由与开源软件镜像站", "https://mirror.bjtu.edu.cn/"}, - Sustech = {"SUSTech", "南方科技大学开源软件镜像站", "https://mirrors.sustech.edu.cn/"}, - Ustc = {"USTC", "中国科学技术大学开源镜像站", "https://mirrors.ustc.edu.cn/"}, - -// 速度暂时处于10位以后,但是目前可用的源 - Nju = {"NJU", "南京大学开源镜像站", "https://mirrors.nju.edu.cn/"}, - Cqu = {"CQU", "重庆大学开源软件镜像站", "https://mirrors.cqu.edu.cn/"}; - - -// 大型公司 -// 注意,腾讯软件源中,有很多链接都已失效,请仔细检查 -mirror_info - Ali = {"Ali OPSX", "阿里巴巴开源镜像站", "https://developer.aliyun.com/mirror/"}, - Tencent = {"Tencent", "腾讯软件源", "https://mirrors.tencent.com/"}, - Netease = {"Netease", "网易开源镜像站", "https://mirrors.163.com/"}, - Sohu = {"SOHU", "搜狐开源镜像站", "https://mirrors.sohu.com/"}; - - -// 开源社区 -mirror_info - RubyChina = {"RubyChina", "Ruby China 社区", "https://ruby-china.org/"}, - GoProxyCN = {"Goproxy.cn", "七牛云 Goproxy.cn", "https://www.qiniu.com/"}, - GoProxyIO = {"GOPROXY.IO", "GOPROXY.IO", "https://goproxy.io/"}, - NugetOrg = {"NuGet Org", "Nuget Organization", "https://www.nuget.org/"}; - - -mirror_info* -available_mirrors[] = { - &MirrorZ, &Tuna, &Sjtug_Zhiyuan, &Zju, &Lzuoss, &Jlu, &Bfsu, &Pku, &Bjtu, &Sustech, &Ustc, &Nju, &Cqu, - &Ali, &Tencent, &Netease, &Sohu, - &RubyChina, &GoProxyCN, &GoProxyIO - // 暂不支持 NugetOrg -}; - - -typedef struct { - const mirror_info* mirror; - const char* url; -} source_info; - - -/** - * 源信息 - * - * 我们要求每个源至少有一个教育网镜像,至少有一个商业公司或开源社区维护的镜像 - */ - - -/** - * 2023-08-29 更新 - * - * 速度已经过测试,目前北外最快,Ruby China 的源慢了一半 - */ -static source_info -pl_ruby_sources[] = { - {&Bfsu, "https://mirrors.bfsu.edu.cn/rubygems/"}, - {&Ali, "https://mirrors.aliyun.com/rubygems/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/rubygems/"}, - {&Tencent, "https://mirrors.tencent.com/rubygems/"}, - {&RubyChina, "https://gems.ruby-china.com"}, -}, - - -/** - * 2023-08-29 更新 - * - * 不要添加Zju,浙大的pypi在校外访问会自动转向Tuna - * - * TODO: 1. 速度只经过简单测试,请Python用户协助, - * 2. 列表暂时未添加商业公司源,以及其他大学镜像 - */ -pl_python_sources[] = { - {&Sjtug_Zhiyuan, "https://mirror.sjtu.edu.cn/pypi/web/simple"}, - {&Tuna, "https://pypi.tuna.tsinghua.edu.cn/simple"}, - {&Lzuoss, "https://mirror.lzu.edu.cn/pypi/web/simple"}, - {&Jlu, "https://mirrors.jlu.edu.cn/pypi//web/simple"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/pypi/web/simple"} -}, - - - -/** - * 2023-08-30 更新 - * - * Sjtug, Tuna, Lzuoss, Jlu, Bfsu, 网易,搜狐 都没有 - * - * 腾讯软件源虽然有npm的名,但名存实亡 - */ -pl_nodejs_sources[] = { - {&Ali, "https://registry.npmmirror.com"}, - {&Zju, "https://mirrors.zju.edu.cn/docs/npm/"} -}, - - -/** - * 2023-08-30 更新 - * - * 参考:https://help.mirrors.cernet.edu.cn/CPAN/ - * - * Jlu 吉林大学没有该源 - * - * TODO: 速度未经测试,请Perl用户协助 - */ -pl_perl_sources[] = { - {&Ali, "https://mirrors.aliyun.com/CPAN/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/CPAN/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/CPAN/"}, - {&Bjtu, "https://mirror.bjtu.edu.cn/cpan/"}, - {&Lzuoss, "https://mirror.lzu.edu.cn/CPAN/"} -}, - - -/** - * 2023-08-30 更新 - * - * TODO: 速度未经测试,请R用户协助 - */ -pl_r_sources[] = { - {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/cran/"}, - {&Ali, "https://mirrors.aliyun.com/CRAN/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/CRAN/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/CRAN/"}, - {&Bjtu, "https://mirror.bjtu.edu.cn/cran/"}, -}, - - -/** - * 2023-08-30 更新 - * - * 阿里云没有该源 - * - * TODO: 1. 速度未经测试,请Julia用户协助 - * 2. 缺少商业公司或开源社区软件源 - */ -pl_julia_sources[] = { - {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/julia"}, - {&Pku, "https://mirrors.pku.edu.cn/julia"}, - {&Nju, "https://mirror.nju.edu.cn/julia"} -}, - - -/** - * 2023-08-30 更新 - * - * TODO: 1. 速度未经测试,请Go用户协助 - * 2. 缺少教育网软件源 - */ -pl_go_sources[] = { - {&Ali, "https://mirrors.aliyun.com/goproxy/"}, - {&GoProxyCN, "https://goproxy.cn"}, - {&GoProxyIO, "https://goproxy.io"} -}, - - -/** - * 2023-08-30 更新 - * - * TODO: 1. 速度未经测试,请Rust用户协助 - * 2. 缺少商业公司或开源社区软件源 - */ -pl_rust_sources[] = { - {&Sjtug_Zhiyuan, "https://mirrors.sjtug.sjtu.edu.cn/crates.io-index/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/crates.io-index/"}, - {&Ustc, "https://mirrors.ustc.edu.cn/crates.io-index/"}, - {&Cqu, "https://mirrors.cqu.edu.cn/crates.io-index/"} -}, - - -/** - * 2023-08-30 更新 - * - * TODO: 1. 速度未经测试,请Java用户协助 - * 2. 缺少教育网或开源社区软件源 - * 3. 当前仅有一个源 - */ -pl_java_sources[] = { - {&Ali, "https://maven.aliyun.com/repository/public/"} -}, - - -/** - * 2023-08-30 更新 - * - * 暂时未实现该换源功能 - * - * TODO: 1. 速度未经测试,请Java用户协助 - * 2. 缺少教育网或开源社区软件源 - * 3. 当前仅有一个源 - */ -pl_dotnet_sources[] = { - {&NugetOrg, "https://www.nuget.org/api/v2/"} -}, - - - -/** - * 2023-08-30 更新 - * - * TODO: 1. 速度未经测试,请PHP用户协助 - * 2. 缺少教育网或开源社区软件源 - * 3. 当前仅有一个源 - */ -pl_php_sources[] = { - {&Ali, "https://mirrors.aliyun.com/composer/"} -}; - - - -/** - * 2023-09-01 更新 - * - * TODO: 1. 源并不完整,且未经测试是否有效 - */ -static source_info -os_ubuntu_sources[] = { - {&Ali, "https://mirrors.aliyun.com/ubuntu/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/ubuntu/"}, - {&Ustc, "https://mirrors.ustc.edu.cn/ubuntu/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/ubuntu/"}, - {&Tencent, "https://mirrors.tencent.com/ubuntu/"}, - {&Netease, "https://mirrors.163.com/ubuntu/"}, - {&Sohu, "https://mirrors.sohu.com/ubuntu/"}, -}, - -/** - * 2023-09-01 更新 - * - * TODO: 1. 源并不完整,且未经测试是否有效 - */ -os_debian_sources[] = { - {&Ali, "https://mirrors.aliyun.com/debian/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/debian/"}, - {&Ustc, "https://mirrors.ustc.edu.cn/debian/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/debian/"}, - {&Tencent, "https://mirrors.tencent.com/debian/"}, - {&Netease, "https://mirrors.163.com/debian/"}, - {&Sohu, "https://mirrors.sohu.com/debian/"}, -}, - - - -/** - * 2023-09-02 更新 - * - * TODO: 1. 源并不完整,且未经测试是否有效 - */ -os_fedora_sources[] = { - {&Ali, "https://mirrors.aliyun.com/fedora/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/fedora/"}, - {&Ustc, "https://mirrors.ustc.edu.cn/fedora/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/fedora/"}, - {&Tencent, "https://mirrors.tencent.com/fedora/"}, - {&Netease, "https://mirrors.163.com/fedora/"}, - {&Sohu, "https://mirrors.sohu.com/fedora/"}, -}, - - -/** - * 2023-09-02 更新 - * - * TODO: 1. 源并不完整,且未经测试是否有效 - */ -os_kali_sources[] = { - {&Ali, "https://mirrors.aliyun.com/kali/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/kali/"}, - {&Ustc, "https://mirrors.ustc.edu.cn/kali/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/kali/"}, - {&Tencent, "https://mirrors.tencent.com/kali/"}, - {&Netease, "https://mirrors.163.com/kali/"}, - {&Sohu, "https://mirrors.sohu.com/kali/"}, -}, - - - -/** - * 2023-09-02 更新 - * - * TODO: 1. 源并不完整,且未经测试是否有效 - */ -os_openbsd_sources[] = { - {&Ali, "https://mirrors.aliyun.com/OpenBSD/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/OpenBSD/"}, - {&Ustc, "https://mirrors.ustc.edu.cn/OpenBSD/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/OpenBSD/"}, - {&Tencent, "https://mirrors.tencent.com/OpenBSD/"}, - {&Netease, "https://mirrors.163.com/OpenBSD/"}, - {&Sohu, "https://mirrors.sohu.com/OpenBSD/"}, -}, - - -/** - * 2023-09-02 更新 - * - * TODO: 1. 源并不完整,且未经测试是否有效 - */ -os_mysys2_sources[] = { - {&Ali, "https://mirrors.aliyun.com/msys2/"}, - {&Bfsu, "https://mirrors.bfsu.edu.cn/msys2/"}, - {&Ustc, "https://mirrors.ustc.edu.cn/msys2/"}, - {&Tuna, "https://mirrors.tuna.tsinghua.edu.cn/msys2/"}, - {&Tencent, "https://mirrors.tencent.com/msys2/"}, - {&Netease, "https://mirrors.163.com/msys2/"}, - {&Sohu, "https://mirrors.sohu.com/msys2/"}, -} -; - -/* 函数签名 */ -bool does_the_program_exist (char* check_cmd, char* progname); diff --git a/helper.h b/helper.h deleted file mode 100644 index 3f83c87..0000000 --- a/helper.h +++ /dev/null @@ -1,236 +0,0 @@ -/* -------------------------------------------------------------- -* File : helper.h -* Authors : Aoran Zeng -* Created on : <2023-08-28> -* Last modified : <2023-08-31> -* -* helper: -* -* helper functions and macros -* -------------------------------------------------------------*/ - -#ifndef XY_H -#define XY_H - -#include -#include -#include -#include -#include - -// #define NDEBUG - -#ifdef _WIN32 - - static bool xy_on_windows = true; - static bool xy_on_linux = false; - static bool xy_on_macos = false; - static bool xy_on_bsds = false; - - #include - #define xy_useutf8() SetConsoleOutputCP(65001) - -#elif defined(__linux__) || defined(__linux) - - static bool xy_on_windows = false; - static bool xy_on_linux = true; - static bool xy_on_macos = false; - static bool xy_on_bsds = false; - - #define xy_useutf8() - -#elif defined(TARGET_OS_MAC) ||defined(__MACOSX__) - - static bool xy_on_windows = false; - static bool xy_on_linux = false; - static bool xy_on_macos = true; - static bool xy_on_bsds = false; - - #define xy_useutf8() - -#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) - - static bool xy_on_windows = false; - static bool xy_on_linux = false; - static bool xy_on_macos = false; - static bool xy_on_bsds = true; - - #define xy_useutf8() - -#endif - - -#define xy_arylen(x) (sizeof(x) / sizeof(x[0])) - - -static inline void* -xy_malloc0 (size_t size) -{ - void* ptr = malloc(size); - memset(ptr, 0, size); - return ptr; -} - - -#define XY_INFO 00001 -#define XY_SUCCESS 00001<<1 -#define XY_WARN 00001<<2 -#define XY_ERROR 00001<<3 - -static void -xy_log (int level, const char* str) -{ - char* color_fmt_str = NULL; - - bool to_stderr = false; - - if (level & XY_INFO) { - color_fmt_str = "\033[34m%s\033[0m"; // 蓝色 - } - else if (level & XY_SUCCESS) { - color_fmt_str = "\033[32m%s\033[0m"; // 绿色 - } - else if (level & XY_WARN) { - color_fmt_str = "\033[33m%s\033[0m\n"; // 黄色 - to_stderr = true; - } - else if (level & XY_ERROR) { - color_fmt_str = "\033[31m%s\033[0m\n"; // 红色 - to_stderr = true; - } - else { - //xy_assert ("CAN'T REACH!"); - } - - // -2 把中间%s减掉 - size_t len = strlen(color_fmt_str) -2; - char* buf = malloc(strlen(str) + len + 1); - - sprintf (buf, color_fmt_str, str); - if (to_stderr) { - fprintf(stderr, buf); - } else { - puts(buf); - } - free(buf); -} - -#define xy_info(str) xy_log (XY_INFO, str) -#define xy_error(str) xy_log (XY_ERROR, str) - - -/** - * 将str中所有的src字符替换成dest,并返回一个全新的字符串 - * 现在已经废弃不用 - */ -static char* -xy_strch (const char* str, char src,const char* dest) -{ - size_t str_len = strlen(str); - size_t dest_len = strlen(dest); - size_t size = str_len*dest_len; - char* ret = (char*)malloc(size); - int i=0; - int j=0; - while(i al_cur) { - al_times += 1; al_cur = al_times * al_fixed; - ret = realloc(ret, al_cur); - if (NULL==ret) { xy_error ("xy: No availble memory!"); return NULL; } - } - strcpy(cur, str); - cur += strlen(str); - } - va_end(args); - - *cur = '\0'; - return ret; -} - - -bool -xy_streql(const char* str1, const char* str2) { - return strcmp(str1, str2) == 0 ? true : false; -} - -#endif