From 54e33cab23917c51b81696b20a3e477e58786be5 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Tue, 26 Sep 2023 21:41:47 +0800 Subject: [PATCH] Rename back to `chsrc_` --- chsrc.c | 214 +++++++++++++++++++++++++++--------------------------- chsrc.h | 33 +++++++++ test_xy.c | 5 -- xy.h | 34 --------- 4 files changed, 140 insertions(+), 146 deletions(-) diff --git a/chsrc.c b/chsrc.c index 9e31986..9e7c22c 100644 --- a/chsrc.c +++ b/chsrc.c @@ -20,9 +20,9 @@ void pl_ruby_getsrc (char* option) { char* cmd = "gem sources"; - xy_run(cmd); + chsrc_run(cmd); cmd = "bundle config get mirror.https://rubygems.org"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -56,14 +56,14 @@ pl_ruby_setsrc (char* option) if (xy_str_start_with(buf, "http")){ cmd = xy_str_delete_suffix(buf, "\n"); cmd = xy_2strjoin("gem sources -r ", cmd); - xy_run(cmd); + chsrc_run(cmd); } memset(buf, 0, 512); } pclose(fp); cmd = xy_2strjoin("gem source -a ", source.url); - xy_run(cmd); + chsrc_run(cmd); check_cmd = xy_str_to_quietcmd("bundle -v"); exist = does_the_program_exist (check_cmd, "bundle"); @@ -73,7 +73,7 @@ pl_ruby_setsrc (char* option) } cmd = xy_2strjoin("bundle config 'mirror.https://rubygems.org' ", source.url); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); puts(""); @@ -115,7 +115,7 @@ pl_python_getsrc (char* option) char* prog = NULL; pl_python_check_cmd_ (&prog); char* cmd = xy_2strjoin(prog, " -m pip config get global.index-url"); - xy_run(cmd); + chsrc_run(cmd); } /** @@ -140,7 +140,7 @@ pl_python_setsrc (char* option) chsrc_say_selection(&source); char* cmd = xy_2strjoin(prog, xy_2strjoin(" -m pip config set global.index-url ", source.url)); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -171,12 +171,12 @@ pl_nodejs_getsrc (char* option) if (npm_exist) { char* cmd = "npm config get registry"; - xy_run(cmd); + chsrc_run(cmd); } if (yarn_exist) { char* cmd = "yarn config get registry"; - xy_run(cmd); + chsrc_run(cmd); } } @@ -203,13 +203,13 @@ pl_nodejs_setsrc (char* option) if (npm_exist) { char* cmd = xy_2strjoin("npm config set registry ", source.url); - xy_run(cmd); + chsrc_run(cmd); } if (yarn_exist) { char* cmd = xy_str_to_quietcmd(xy_2strjoin("yarn config set registry ", source.url)); - xy_run(cmd); + chsrc_run(cmd); } chsrc_say_thanks(&source); } @@ -236,7 +236,7 @@ pl_perl_getsrc (char* option) // 可以使用 CPAN::Shell->o('conf', 'urllist'); // 另外,上述两种方法无论哪种,都要首先load() char* cmd = "perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->prettyprint('urllist')\" "; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -258,7 +258,7 @@ pl_perl_setsrc (char* option) char* cmd = xy_strjoin(3, "perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->edit('urllist', 'unshift', '", source.url, "'); CPAN::HandleConfig->commit()\""); - xy_run(cmd); + chsrc_run(cmd); chsrc_warn ("请您使用 perl -v 以及 cpan -v,若 Perl >= v5.36 或 CPAN >= 2.29,请额外手动调用下面的命令"); puts("perl -MCPAN -e \"CPAN::HandleConfig->load(); CPAN::HandleConfig->edit('pushy_https', 0);; CPAN::HandleConfig->commit()\""); @@ -287,7 +287,7 @@ pl_php_getsrc (char* option) { pl_php_check_cmd_ (); char* cmd = "composer config -g repositories"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -310,7 +310,7 @@ pl_php_setsrc (char* option) chsrc_say_selection (&source); char* cmd = xy_2strjoin("composer config -g repo.packagist composer ", source.url); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -334,7 +334,7 @@ pl_go_getsrc (char* option) { pl_go_check_cmd_ (); char* cmd = "go env GOPROXY"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -356,10 +356,10 @@ pl_go_setsrc (char* option) chsrc_say_selection (&source); char* cmd = "go env -w GO111MODULE=on"; - xy_run(cmd); + chsrc_run(cmd); cmd = xy_strjoin(3, "go env -w GOPROXY=", source.url, ",direct"); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -374,7 +374,7 @@ pl_rust_getsrc (char* option) } else { cmd = "cat ~/.cargo"; } - xy_run(cmd); + chsrc_run(cmd); } /** @@ -555,10 +555,10 @@ pl_dart_getsrc(char* option) char* cmd = NULL; if (xy_on_windows) { cmd = "set PUB_HOSTED_URL & set FLUTTER_STORAGE_BASE_URL"; - xy_run(cmd); + chsrc_run(cmd); } else { cmd = "echo $PUB_HOSTED_URL; echo $FLUTTER_STORAGE_BASE_URL"; - xy_run(cmd); + chsrc_run(cmd); } } @@ -588,23 +588,23 @@ pl_dart_setsrc (char* option) { if (xy_file_exist(xy_win_powershell_profile)) { - xy_run(xy_strjoin(4, "echo $env:PUB_HOSTED_URL = \"", pub, "\" >> ", xy_win_powershell_profile)); - xy_run(xy_strjoin(4, "echo $env:FLUTTER_STORAGE_BASE_URL = \"", flutter, "\" >> ", xy_win_powershell_profile)); + chsrc_run(xy_strjoin(4, "echo $env:PUB_HOSTED_URL = \"", pub, "\" >> ", xy_win_powershell_profile)); + chsrc_run(xy_strjoin(4, "echo $env:FLUTTER_STORAGE_BASE_URL = \"", flutter, "\" >> ", xy_win_powershell_profile)); } if (xy_file_exist(xy_win_powershellv5_profile)) { - xy_run(xy_strjoin(4, "echo $env:PUB_HOSTED_URL = \"", pub, "\" >> ", xy_win_powershellv5_profile)); - xy_run(xy_strjoin(4, "echo $env:FLUTTER_STORAGE_BASE_URL = \"", flutter, "\" >> ", xy_win_powershell_profile)); + chsrc_run(xy_strjoin(4, "echo $env:PUB_HOSTED_URL = \"", pub, "\" >> ", xy_win_powershellv5_profile)); + chsrc_run(xy_strjoin(4, "echo $env:FLUTTER_STORAGE_BASE_URL = \"", flutter, "\" >> ", xy_win_powershell_profile)); } } else { cmd = xy_strjoin(3, "echo 'export PUB_HOSTED_URL=\"", pub, "\"' >> ~/.bashrc >> ~/.zshrc"); - xy_run(cmd); + chsrc_run(cmd); cmd = xy_strjoin(3, "export 'FLUTTER_STORAGE_BASE_URL=\"", flutter, "\"' >> ~/.bashrc >> ~/.zshrc"); - xy_run(cmd); + chsrc_run(cmd); } chsrc_say_thanks(&source); } @@ -678,7 +678,7 @@ pl_ocaml_getsrc(char* option) { pl_ocaml_check_cmd_(); char* cmd = "opam repo get-url default"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -703,7 +703,7 @@ pl_ocaml_setsrc(char* option) source.url, " --all --set-default"); - xy_run(cmd); + chsrc_run(cmd); chsrc_info("如果是首次使用 opam ,请使用以下命令进行初始化"); puts(xy_2strjoin("opam init default ", source.url)); @@ -727,7 +727,7 @@ pl_r_getsrc (char* option) } else { cmd = "cat ~/.Rprofile"; } - xy_run(cmd); + chsrc_run(cmd); } /** @@ -759,7 +759,7 @@ pl_r_setsrc (char* option) else cmd = xy_strjoin(3, "echo '", file, "' >> ~/.Rprofile"); - xy_run(cmd); + chsrc_run(cmd); file = xy_strjoin (3, "options(BioC_mirror=\"", bioconductor_url, "\")" ); // 或者我们调用 r.exe --slave -e 上面的内容 @@ -768,7 +768,7 @@ pl_r_setsrc (char* option) else cmd = xy_strjoin(3, "echo '", file, "' >> ~/.Rprofile"); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -790,7 +790,7 @@ pl_julia_getsrc (char* option) } else { cmd = "cat ~/.julia/config/startup.jl"; } - xy_run(cmd); + chsrc_run(cmd); } /** @@ -822,7 +822,7 @@ pl_julia_setsrc (char* option) cmd = xy_strjoin(4, xy_str_to_quietcmd("mkdir -p ~/.julia/config"), ";echo '", file, "' >> ~/.julia/config/startup.jl"); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -836,7 +836,7 @@ void os_ubuntu_getsrc(char* option) { char* cmd = "cat /etc/apt/sources.list"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -859,7 +859,7 @@ os_ubuntu_setsrc (char* option) chsrc_say_selection(&source); char* backup = "cp /etc/apt/sources.list /etc/apt/sources.list.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); // xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); @@ -880,7 +880,7 @@ os_ubuntu_setsrc (char* option) "-ports@g\' /etc/apt/sources.list"); } - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); @@ -892,7 +892,7 @@ void os_debian_getsrc(char* option) { char* cmd = "cat /etc/apt/sources.list"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -919,14 +919,14 @@ os_debian_setsrc (char* option) xy_info ("chsrc: sudo apt install apt-transport-https ca-certificates"); char* backup = "cp /etc/apt/sources.list /etc/apt/sources.list.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); char* cmd = xy_strjoin(3, "sed -E -i \'s@https?://.*/debian/?@", source.url, "@g\' /etc/apt/sources.list"); - xy_run(cmd); + chsrc_run(cmd); // char* rm = "rm -rf /etc/apt/source.list.bak"; // system(rm); @@ -940,7 +940,7 @@ void os_deepin_getsrc(char* option) { char* cmd = "cat /etc/apt/sources.list"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -962,7 +962,7 @@ os_deepin_setsrc (char* option) chsrc_say_selection(&source); char* backup = "cp /etc/apt/sources.list /etc/apt/sources.list.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); @@ -971,7 +971,7 @@ os_deepin_setsrc (char* option) source.url, "@g\' /etc/apt/sources.list"); - xy_run(cmd); + chsrc_run(cmd); // char* rm = "rm -rf /etc/apt/source.list.bak"; // system(rm); @@ -1002,10 +1002,10 @@ os_fedora_setsrc (char* option) xy_warn ("chsrc: fedora 29 及以下版本暂不支持"); char* backup = "cp /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); backup = "cp /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.bak"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名:1. /etc/yum.repos.d/fedora.repo.bak"); xy_info ("chsrc: 备份文件名:2. /etc/yum.repos.d/fedora-updates.repo.bak"); @@ -1021,7 +1021,7 @@ os_fedora_setsrc (char* option) "/etc/yum.repos.d/fedora-updates.repo ", "/etc/yum.repos.d/fedora-updates-modular.repo"); - xy_run(cmd); + chsrc_run(cmd); xy_info ("chsrc: 替换文件:/etc/yum.repos.d/fedora.repo"); xy_info ("chsrc: 新增文件:/etc/yum.repos.d/fedora-modular.repo"); @@ -1057,7 +1057,7 @@ os_opensuse_setsrc (char* option) chsrc_say_selection(&source); char* source_nselect = "zypper mr -da"; - xy_run(source_nselect); + chsrc_run(source_nselect); char* cmd1 = xy_strjoin(3, "zypper ar -cfg '", @@ -1084,17 +1084,17 @@ os_opensuse_setsrc (char* option) source.url, "/opensuse/distribution/leap/$releasever/backports/' mirror-backports-update"); - xy_run(cmd1); - xy_run(cmd2); - xy_run(cmd3); - xy_run(cmd4); + chsrc_run(cmd1); + chsrc_run(cmd2); + chsrc_run(cmd3); + chsrc_run(cmd4); xy_info("chsrc: leap 15.3用户还需 要添加sle和backports源"); xy_info("chsrc: 另外请确保系统在更新后仅启用了六个软件源,可以使用 zypper lr 检查软件源状态"); xy_info("chsrc: 并使用 zypper mr -d 禁用多余的软件源"); - xy_run(cmd5); - xy_run(cmd6); + chsrc_run(cmd5); + chsrc_run(cmd6); // char* rm = "rm -f /etc/apt/source.list.bak"; // chsrc_runcmd(rm); chsrc_say_thanks(&source); @@ -1120,7 +1120,7 @@ os_kali_setsrc(char* option) chsrc_say_selection(&source); char* backup = "cp /etc/apt/sources.list /etc/apt/sources.list.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); @@ -1129,7 +1129,7 @@ os_kali_setsrc(char* option) source.url, "@g\' /etc/apt/sources.list"); - xy_run(cmd); + chsrc_run(cmd); // char* rm = "rm -rf /etc/apt/source.list.bak"; // system(rm); @@ -1158,13 +1158,13 @@ os_msys2_setsrc(char* option) char* backup = "cp -f /etc/pacman.d/mirrorlist.mingw32 /etc/pacman.d/mirrorlist.mingw32.bak"; - xy_run(backup); + chsrc_run(backup); backup = "cp -f /etc/pacman.d/mirrorlist.mingw64 /etc/pacman.d/mirrorlist.mingw64.bak"; - xy_run(backup); + chsrc_run(backup); backup = "cp -f /etc/pacman.d/mirrorlist.msys /etc/pacman.d/mirrorlist.msys.bak"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: 1. /etc/pacman.d/mirrorlist.mingw32.bak"); xy_info ("chsrc: 备份文件名: 2. /etc/pacman.d/mirrorlist.mingw64.bak"); @@ -1179,7 +1179,7 @@ os_msys2_setsrc(char* option) source.url, "#g\" /etc/pacman.d/mirrorlist* "); - xy_run(cmd); + chsrc_run(cmd); // char* rm = "rm -rf /etc/pacman.d/mirrorlist.mingw32.bak"; // system(rm); @@ -1213,7 +1213,7 @@ os_arch_setsrc(char* option) char* backup = "cp -f /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); bool arch_flag = false; @@ -1234,20 +1234,20 @@ os_arch_setsrc(char* option) // TODO: 这里用的是 > 吗? cmd = xy_strjoin(3,"echo ", new_file, " > /etc/pacman.d/mirrorlist"); - xy_run(cmd); + chsrc_run(cmd); xy_info("chsrc: 使用 archlinuxcn "); cmd = xy_strjoin(3, "cat [archlinuxcn] \r\n Server=",source.url,"archlinuxcn/$repo/os/$arch >> /etc/pacman.d/mirrorlist"); - xy_run(cmd); + chsrc_run(cmd); cmd = "pacman -Sy archlinux-keyring"; - xy_run(cmd); + chsrc_run(cmd); if(arch_flag) { - xy_run("pacman -Syyu"); + chsrc_run("pacman -Syyu"); } else { - xy_run("pacman -Syy"); + chsrc_run("pacman -Syy"); } chsrc_say_thanks(&source); } @@ -1274,13 +1274,13 @@ os_gentoo_setsrc(char* option) char* backup = "cp -f /etc/portage/repos.conf/gentoo.conf /etc/portage/repos.conf/gentoo.conf.bak"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: /etc/portage/repos.conf/gentoo.conf.bak"); char* cmd = xy_strjoin(3,"sed -i \"s#rsync://.*/gentoo-portage#rsync://", source.url, "gentoo-portage#g"); - xy_run(cmd); + chsrc_run(cmd); char * yuan = xy_strjoin(3,"GENTOO_MIRRORS=\"https://", source.url, @@ -1288,7 +1288,7 @@ os_gentoo_setsrc(char* option) cmd = xy_strjoin(3,"cat ", yuan, " >> /etc/portage/make.conf"); - xy_run(cmd); + chsrc_run(cmd); // char* rm = "rm -rf /etc/portage/repos.conf/gentoo.conf.bak"; // system(rm); @@ -1321,7 +1321,7 @@ os_rocky_setsrc (char* option) "-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=", source.url, "|g' " "-i.bak /etc/yum.repos.d/rocky-extras.repo /etc/yum.repos.d/rocky.repo" ); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -1349,10 +1349,10 @@ os_alpine_setsrc (char* option) char* cmd = xy_strjoin(3, "sed -i 's#https\\?://dl-cdn.alpinelinux.org/alpine#", source.url, "#g' /etc/apk/repositories" ); - xy_run(cmd); + chsrc_run(cmd); cmd = "apk update"; - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -1363,7 +1363,7 @@ void os_void_getsrc (char* option) { char* cmd = "xbps-query -L"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -1386,15 +1386,15 @@ os_void_setsrc (char* option) chsrc_say_selection(&source); char* cmd = "mkdir -p /etc/xbps.d"; - xy_run(cmd); + chsrc_run(cmd); cmd = "cp /usr/share/xbps.d/*-repository-*.conf /etc/xbps.d/"; - xy_run(cmd); + chsrc_run(cmd); cmd = xy_strjoin(3, "sed -i 's|https://repo-default.voidlinux.org|", source.url, "|g' /etc/xbps.d/*-repository-*.conf" ); - xy_run(cmd); + chsrc_run(cmd); cmd = xy_strjoin(3, "sed -i 's|https://alpha.de.repo.voidlinux.org|", source.url, "|g' /etc/xbps.d/*-repository-*.conf" @@ -1415,9 +1415,9 @@ os_manjaro_setsrc(char* option) { ensure_root(); char* cmd = "sudo pacman-mirrors -i -c China -m rank"; - xy_run(cmd); + chsrc_run(cmd); - xy_run("sudo pacman -Syy"); + chsrc_run("sudo pacman -Syy"); } @@ -1441,7 +1441,7 @@ os_openeuler_setsrc (char* option) chsrc_say_selection(&source); char* backup = "cp -f /etc/yum.repos.d/openEuler.repot /etc/yum.repos.d/openEuler.repo.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: /etc/yum.repos.d/openEuler.repo.bak"); @@ -1451,7 +1451,7 @@ os_openeuler_setsrc (char* option) source.url, "#\'< /etc/yum.repos.d/openEuler.repo.bak | cat > /etc/yum.repos.d/openEuler.repo"); - xy_run(cmd); + chsrc_run(cmd); // char* rm = "rm -rf /etc/yum.repos.d/openEuler.repo.bak"; // system(rm); @@ -1480,7 +1480,7 @@ os_openkylin_setsrc (char* option) chsrc_say_selection(&source); char* backup = "cp -f /etc/apt/sources.list /etc/apt/sources.list.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: /etc/apt/sources.list.bak"); @@ -1490,7 +1490,7 @@ os_openkylin_setsrc (char* option) source.url, "@\'< /etc/apt/sources.list.bak | cat > /etc/apt/sources.list"); - xy_run(cmd); + chsrc_run(cmd); // char* rm = "rm -rf /etc/apt/source.list.bak"; // system(rm); @@ -1527,8 +1527,8 @@ os_freebsd_setsrc (char* option) xy_info("chsrc: 1. 添加 freebsd-pkg 源 (二进制安装包)"); char* pkg_mkdir = "mkdir -p /usr/local/etc/pkg/repos"; char* pkg_createconf = xy_strjoin(3, "ee /usr/local/etc/pkg/repos/", source.mirror->code, ".conf"); - xy_run(pkg_mkdir); - xy_run(pkg_createconf); + chsrc_run(pkg_mkdir); + chsrc_run(pkg_createconf); char* pkg_content = xy_strjoin(4, @@ -1541,7 +1541,7 @@ os_freebsd_setsrc (char* option) }"); char* pkg_cmd = xy_strjoin(3, "cat ", pkg_content, "> /usr/local/etc/pkg/repos/", source.mirror->code , ".conf"); - xy_run(pkg_cmd); + chsrc_run(pkg_cmd); xy_warn("chsrc: 若要使用HTTPS源,请先安装securtiy/ca_root_ns,并将 'http' 改成 'https' ,最后使用 'pkg update -f' 刷新缓存即可\n"); @@ -1551,14 +1551,14 @@ os_freebsd_setsrc (char* option) bool git_exist = does_the_program_exist (xy_str_to_quietcmd("git version"), "git"); if (git_exist) { char* git_cmd = xy_strjoin(3, "git clone --depth 1 https://", source.url, "/freebsd-ports/ports.git /usr/ports"); - xy_run(git_cmd); + chsrc_run(git_cmd); } else { char* fetch = xy_strjoin(3, "fetch https://", source.url, "/freebsd-ports/ports.tar.gz"); // 70多MB char* unzip = "tar -zxvf ports.tar.gz -C /usr/ports"; char* delete = "rm ports.tar.gz"; - xy_run(fetch); - xy_run(unzip); - xy_run(delete); + chsrc_run(fetch); + chsrc_run(unzip); + chsrc_run(delete); } /* https://help.mirrors.cernet.edu.cn/FreeBSD-ports/ 的换源方法 */ @@ -1613,7 +1613,7 @@ void os_netbsd_getsrc (char* option) { char* cmd = "cat /usr/pkg/etc/pkgin/repositories.conf"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -1644,7 +1644,7 @@ os_netbsd_setsrc(char* option) char* backup = "cp -f /usr/pkg/etc/pkgin/repositories.conf /usr/pkg/etc/pkgin/repositories.conf.bak"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: /usr/pkg/etc/pkgin/repositories.conf.bak"); @@ -1656,7 +1656,7 @@ os_netbsd_setsrc(char* option) "/", version, "/All > /usr/pkg/etc/pkgin/repositories.conf"); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -1667,7 +1667,7 @@ void os_openbsd_getsrc (char* option) { char* cmd = "cat /etc/installurl"; - xy_run(cmd); + chsrc_run(cmd); } /** @@ -1691,12 +1691,12 @@ os_openbsd_setsrc(char* option) chsrc_say_selection(&source); char* backup = "cp -f /etc/installurl /etc/installurl.bak --backup='t'"; - xy_run(backup); + chsrc_run(backup); xy_info ("chsrc: 备份文件名: /etc/installurl.bak"); char* cmd = xy_strjoin(3,"echo ", source.url, " > /etc/installurl"); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -1728,10 +1728,10 @@ wr_tex_getsrc(char* option) wr_tex_check_cmd_(&tlmgr_exist, &mpm_exist); if (tlmgr_exist) { - xy_run("tlmgr option repository"); + chsrc_run("tlmgr option repository"); } if (mpm_exist) { - xy_run("mpm --get-repository"); + chsrc_run("mpm --get-repository"); } } @@ -1758,13 +1758,13 @@ wr_tex_setsrc(char* option) if (tlmgr_exist) { cmd = xy_2strjoin("tlmgr option repository ", source.url); - xy_run(cmd); + chsrc_run(cmd); } if (mpm_exist) { char* miktex_url = xy_2strjoin(xy_str_delete_suffix(source.url, "texlive/tlnet"), "win32/miktex/tm/packages/"); cmd = xy_2strjoin("mpm --set-repository=", miktex_url); - xy_run(cmd); + chsrc_run(cmd); } chsrc_say_thanks(&source); @@ -1829,10 +1829,10 @@ wr_brew_setsrc(char* option) char* brew_git_remote = xy_strjoin(3, "export HOMEBREW_BREW_GIT_REMOTE=\"", xy_2strjoin(source.url, "git/homebrew/brew.git"), "\""); char* core_git_remote = xy_strjoin(3, "export HOMEBREW_CORE_GIT_REMOTE=\"", xy_2strjoin(source.url, "git/homebrew/homebrew-core.git"), "\""); - xy_run(xy_strjoin(3,"echo ", api_domain, " >> ~/.bashrc >> ~/.zshrc")); - xy_run(xy_strjoin(3,"echo ", bottle_domain, " >> ~/.bashrc >> ~/.zshrc")); - xy_run(xy_strjoin(3,"echo ", brew_git_remote, " >> ~/.bashrc >> ~/.zshrc")); - xy_run(xy_strjoin(3,"echo ", core_git_remote, " >> ~/.bashrc >> ~/.zshrc")); + chsrc_run(xy_strjoin(3,"echo ", api_domain, " >> ~/.bashrc >> ~/.zshrc")); + chsrc_run(xy_strjoin(3,"echo ", bottle_domain, " >> ~/.bashrc >> ~/.zshrc")); + chsrc_run(xy_strjoin(3,"echo ", brew_git_remote, " >> ~/.bashrc >> ~/.zshrc")); + chsrc_run(xy_strjoin(3,"echo ", core_git_remote, " >> ~/.bashrc >> ~/.zshrc")); chsrc_say_thanks (&source); puts(""); xy_warn("chsrc: 请您重启终端使环境变量生效"); @@ -1900,13 +1900,13 @@ wr_nix_setsrc (char* option) chsrc_say_selection (&source); char* cmd = xy_strjoin(3, "nix-channel --add ", source.url, "nixpkgs-unstable nixpkgs"); - xy_run(cmd); + chsrc_run(cmd); cmd = xy_strjoin (3, "echo \"substituters = ", source.url, "store https://cache.nixos.org/\" >> ~/.config/nix/nix.conf"); - xy_run(cmd); + chsrc_run(cmd); cmd = "nix-channel --update"; - xy_run(cmd); + chsrc_run(cmd); xy_info("chsrc: 若您使用的是NixOS,请确认您的系统版本(如22.11),并手动运行:"); cmd = xy_strjoin(3, "nix-channel --add ", source.url, "nixpkgs- nixpkgs"); @@ -1945,7 +1945,7 @@ wr_flathub_setsrc(char* option) puts(note); char* cmd = xy_2strjoin("sudo flatpak remote-modify flathub --url=", source.url); - xy_run(cmd); + chsrc_run(cmd); chsrc_say_thanks(&source); } @@ -1998,7 +1998,7 @@ wr_anaconda_setsrc(char* option) xy_error ("chsrc: 未找到 conda 命令,请检查是否存在"); exit(1); } - xy_run("conda config --set show_channel_urls yes"); + chsrc_run("conda config --set show_channel_urls yes"); } xy_info(xy_strjoin(3, "chsrc: 请向 ", config, " 中手动添加:")); diff --git a/chsrc.h b/chsrc.h index 44b143d..f2d75ff 100644 --- a/chsrc.h +++ b/chsrc.h @@ -273,6 +273,39 @@ not_root: #define chsrc_error(str) xy_error(xy_2strjoin(App_Prefix, (str))) +static void +chsrc_run (const char* cmd) +{ + xy_info (xy_2strjoin ("chsrc: 运行 ", cmd)); + system(cmd); +} + +static void +chsrc_append_to_file (char* prefix, char* str, char* file) +{ + char* cmd = NULL; + if (xy_on_windows) { + cmd = xy_strjoin (4, "echo ", str, " >> ", file); + } else { + cmd = xy_strjoin (4, "echo '", str, "' >> ", file); + } + chsrc_run(cmd); +} + +static void +chsrc_overwrite_file (char* prefix, char* str, char* file) +{ + char* cmd = NULL; + if (xy_on_windows) { + cmd = xy_strjoin (4, "echo ", str, " > ", file); + } else { + cmd = xy_strjoin (4, "echo '", str, "' > ", file); + } + chsrc_run(cmd); +} + + + /* Target Info */ typedef struct { void (*setfn)(char* option); diff --git a/test_xy.c b/test_xy.c index 9310875..d679064 100644 --- a/test_xy.c +++ b/test_xy.c @@ -10,7 +10,6 @@ * 测试 xy.h * ------------------------------------------------------------*/ -#define XY_Run_Prefix "test_xy: " #include "xy.h" int @@ -76,9 +75,5 @@ main (int argc, char const *argv[]) putb(xy_file_exist(xy_win_powershell_profile)); putb(xy_file_exist(xy_win_powershellv5_profile)); - char* str = "nix.settings.substituters = [ \"https://mirrors.bfsu.edu.cn/nix-channels/store\" ]"; - xy_overwrite_file(str, "test.txt"); - xy_append_to_file(str, "test.txt"); - return 0; } diff --git a/xy.h b/xy.h index 8ca0540..133b149 100644 --- a/xy.h +++ b/xy.h @@ -524,38 +524,4 @@ xy_file_exist(char* path) return access(newpath, 0) ? false : true; } - -static void -xy_run (const char* cmd) -{ - char* log = XY_Run_Prefix; - xy_info (xy_2strjoin (log, cmd)); - system(cmd); -} - - -static void -xy_append_to_file (char* str, char* file) -{ - char* cmd = NULL; - if (xy_on_windows) { - cmd = xy_strjoin (4, "echo ", str, " >> ", file); - } else { - cmd = xy_strjoin (4, "echo '", str, "' >> ", file); - } - xy_run(cmd); -} - -static void -xy_overwrite_file (char* str, char* file) -{ - char* cmd = NULL; - if (xy_on_windows) { - cmd = xy_strjoin (4, "echo ", str, " > ", file); - } else { - cmd = xy_strjoin (4, "echo '", str, "' > ", file); - } - xy_run(cmd); -} - #endif