Format the rest

This commit is contained in:
Aoran Zeng 2024-04-25 16:33:17 +08:00
parent 58da758b21
commit bcb702fee0

138
chsrc.c
View File

@ -3,7 +3,7 @@
* License : GPLv3
* Authors : Aoran Zeng <ccmywish@qq.com>
* Created on : <2023-08-28>
* Last modified : <2024-04-18>
* Last modified : <2024-04-25>
*
* chsrc:
*
@ -12,7 +12,7 @@
* GPLv3 LICENSE.txt
* ------------------------------------------------------------*/
#define Chsrc_Version "v0.1.3-2024/04/18"
#define Chsrc_Version "v0.1.3-2024/04/25"
#include "chsrc.h"
@ -818,7 +818,8 @@ os_ubuntu_setsrc (char* option)
{
cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/ubuntu/?@", source.url, "@g\' /etc/apt/sources.list");
}
else {
else
{
cmd = xy_strjoin (3, "sed -E -i \'s@https?://.*/ubuntu-ports/?@", source.url,
"-ports@g\' /etc/apt/sources.list");
}
@ -1140,11 +1141,13 @@ os_arch_setsrc(char* option)
char *new_file = NULL;
char *arch = xy_getcmd ("arch", 0, NULL);
if (strncmp(arch, "x86_64", 6)==0) {
if (strncmp(arch, "x86_64", 6)==0)
{
arch_flag = true;
new_file = xy_strjoin (3, "Server = ", source.url, "archlinux/$repo/os/$arch");
}
else {
else
{
arch_flag = false;
new_file = xy_strjoin (3, "Server = ", source.url, "archlinuxarm/$repo/os/$arch");
}
@ -1159,9 +1162,12 @@ os_arch_setsrc(char* option)
chsrc_run ("pacman -Sy archlinux-keyring");
if (arch_flag) {
if (arch_flag)
{
chsrc_run ("pacman -Syyu");
} else {
}
else
{
chsrc_run ("pacman -Syy");
}
chsrc_say_thanks (&source);
@ -1401,7 +1407,10 @@ os_openeuler_setsrc (char* option)
chsrc_backup ("/etc/yum.repos.d/openEuler.repo");
char* towrite = xy_strjoin(3, "s#http://repo.openeuler.org#", source.url, "#\'< /etc/yum.repos.d/openEuler.repo.bak");;
char *towrite = xy_strjoin (3, "s#http://repo.openeuler.org#",
source.url,
"#\'< /etc/yum.repos.d/openEuler.repo.bak");
chsrc_overwrite_file (towrite, "/etc/yum.repos.d/openEuler.repo");
chsrc_run ("sudo dnf makecache");
@ -1478,8 +1487,10 @@ os_freebsd_setsrc (char* option)
// 但是我认为由于使用Git还是要比非Git方便许多我们尽可能坚持使用Git
// 而 gitup 又要额外修改它自己的配置,比较麻烦
bool git_exist = query_program_exist (xy_str_to_quietcmd ("git version"), "git");
if (git_exist) {
if (xy_streql("nju",source.mirror->code)) {
if (git_exist)
{
if (xy_streql("nju",source.mirror->code))
{
source = os_freebsd_sources[index-1]; // 使用NJU的前一个源即USTC源
}
char *git_cmd = xy_strjoin (3, "git clone --depth 1 https://", source.url, "/freebsd-ports/ports.git /usr/ports");
@ -1487,7 +1498,8 @@ os_freebsd_setsrc (char* option)
source = os_freebsd_sources[index]; // 恢复至选中的源
chsrc_warn ("下次更新请使用 git -C /usr/ports pull 而非使用 gitup");
}
else {
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";
@ -1630,7 +1642,6 @@ os_ros_setsrc (char* option)
cmd = "sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654";
chsrc_run (cmd);
chsrc_run ("sudo apt update");
chsrc_say_thanks (&source);
}
@ -1649,7 +1660,8 @@ wr_tex_check_cmd (bool* tlmgr_exist, bool* mpm_exist)
check_cmd = xy_str_to_quietcmd ("mpm --version");
*mpm_exist = query_program_exist (check_cmd, "mpm");
if (!*tlmgr_exist && !*mpm_exist) {
if (!*tlmgr_exist && !*mpm_exist)
{
xy_error ("chsrc: 未找到 tlmgr 或 mpm 命令,请检查是否存在(其一)");
exit(1);
}
@ -1661,10 +1673,12 @@ wr_tex_getsrc(char* option)
bool tlmgr_exist, mpm_exist;
wr_tex_check_cmd (&tlmgr_exist, &mpm_exist);
if (tlmgr_exist) {
if (tlmgr_exist)
{
chsrc_run("tlmgr option repository");
}
if (mpm_exist) {
if (mpm_exist)
{
chsrc_run("mpm --get-repository");
}
}
@ -1685,12 +1699,14 @@ wr_tex_setsrc(char* option)
char *cmd = NULL;
if (tlmgr_exist) {
if (tlmgr_exist)
{
cmd = xy_2strjoin ("tlmgr option repository ", source.url);
chsrc_run (cmd);
}
if (mpm_exist) {
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);
chsrc_run (cmd);
@ -1787,7 +1803,8 @@ wr_nix_check_cmd ()
char *check_cmd = xy_str_to_quietcmd ("nix-channel --version");
bool exist = query_program_exist (check_cmd, "nix-channel");
if (!exist) {
if (!exist)
{
chsrc_error ("未找到 nix-channel 命令,请检查是否存在");
exit (1);
}
@ -1888,10 +1905,12 @@ wr_anaconda_setsrc(char* option)
// TODO: 待确认 windows 上也是这里吗?
char *config = xy_2strjoin (xy_os_home, "/.condarc");
if (xy_on_windows) {
if (xy_on_windows)
{
char *check_cmd = xy_str_to_quietcmd("conda --version");
bool exist = query_program_exist (check_cmd, "conda");
if (!exist) {
if (!exist)
{
xy_error ("chsrc: 未找到 conda 命令,请检查是否存在");
exit (1);
}
@ -2050,7 +2069,8 @@ void
call_cmd (void *cmdptr, const char *arg)
{
void (*cmd_func)(const char *) = cmdptr;
if (NULL==arg) {
if (NULL==arg)
{
xy_info ("chsrc: 将使用默认镜像");
}
cmd_func (arg);
@ -2147,7 +2167,8 @@ print_help ()
{
puts (xy_strjoin (3, "chsrc: Change Source (GPLv3) ",
xy_str_to_magenta (Chsrc_Version), " by RubyMetric\n"));
for (int i=0; i<xy_arylen(usage); i++) {
for (int i=0; i<xy_arylen(usage); i++)
{
puts (usage[i]);
}
}
@ -2170,11 +2191,14 @@ iterate_targets_(const char*** array, size_t size, const char* input, const char
int k = 0;
const char *alias = NULL;
for (int i=0; i<size; i++) {
for (int i=0; i<size; i++)
{
target = array[i];
alias = target[k];
while (NULL!=alias) {
if (xy_streql(input, alias)) {
while (NULL!=alias)
{
if (xy_streql(input, alias))
{
matched = 1; break;
}
k++;
@ -2184,7 +2208,8 @@ iterate_targets_(const char*** array, size_t size, const char* input, const char
if (matched) break;
}
if (!matched) {
if (!matched)
{
*target_info = NULL;
return false;
}
@ -2252,13 +2277,13 @@ get_target (const char* input, TargetOp code, char* option)
{
char* check_cmd = xy_str_to_quietcmd ("curl --version");
bool exist_b = query_program_exist (check_cmd, "curl");
if (!exist_b) {
if (!exist_b)
{
xy_error ("chsrc: 没有curl命令无法测速");
exit (1);
}
auto_select_ (target->sources, target->sources_n, input-3);
return true;
}
return true;
}
@ -2270,7 +2295,8 @@ main (int argc, char const *argv[])
{
xy_useutf8 (); argc -= 1;
if (argc==0) {
if (argc==0)
{
print_help (); return 0;
}
@ -2293,42 +2319,55 @@ main (int argc, char const *argv[])
xy_streql (command, "l") ||
xy_streql (command, "ls"))
{
if (argc < 2) {
if (argc < 2)
{
print_available_mirrors ();
puts ("");
print_supported_targets ();
} else {
if (xy_streql(argv[2],"mirrors")) {
}
else
{
if (xy_streql(argv[2],"mirrors"))
{
print_available_mirrors(); return 0;
}
if (xy_streql(argv[2],"mirror")) {
if (xy_streql(argv[2],"mirror"))
{
print_available_mirrors(); return 0;
}
if (xy_streql(argv[2],"targets")) {
if (xy_streql(argv[2],"targets"))
{
print_supported_targets(); return 0;
}
if (xy_streql(argv[2],"target")) {
if (xy_streql(argv[2],"target"))
{
print_supported_targets(); return 0;
}
if (xy_streql(argv[2],"os")) {
if (xy_streql(argv[2],"os"))
{
print_supported_os(); return 0;
}
if (xy_streql(argv[2],"lang")) {
if (xy_streql(argv[2],"lang"))
{
print_supported_pl(); return 0;
}
if (xy_streql(argv[2],"pl")) {
if (xy_streql(argv[2],"pl"))
{
print_supported_pl(); return 0;
}
if (xy_streql(argv[2],"language")) {
if (xy_streql(argv[2],"language"))
{
print_supported_pl(); return 0;
}
if (xy_streql(argv[2],"software")) {
if (xy_streql(argv[2],"software"))
{
print_supported_wr(); return 0;
}
if (xy_streql(argv[2],"ware")) {
if (xy_streql(argv[2],"ware"))
{
print_supported_wr(); return 0;
}
matched = get_target(argv[2], Target_List_Source, NULL);
if (!matched) goto not_matched;
}
@ -2341,7 +2380,8 @@ main (int argc, char const *argv[])
xy_streql (command, "ce") ||
xy_streql (command, "c"))
{
if (argc < 2) {
if (argc < 2)
{
xy_error ("chsrc: 请您提供想要测速源的软件名; 使用 chsrc list targets 查看所有支持的软件");
return 1;
}
@ -2355,7 +2395,8 @@ main (int argc, char const *argv[])
else if (xy_streql (command, "get") ||
xy_streql (command, "g"))
{
if (argc < 2) {
if (argc < 2)
{
xy_error ("chsrc: 请您提供想要查看源的软件名; 使用 chsrc list targets 查看所有支持的软件");
return 1;
}
@ -2368,13 +2409,15 @@ main (int argc, char const *argv[])
else if (xy_streql (command, "set") ||
xy_streql (command, "s"))
{
if (argc < 2) {
if (argc < 2)
{
xy_error ("chsrc: 请您提供想要设置源的软件名; 使用 chsrc list targets 查看所有支持的软件");
return 1;
}
char *option = NULL;
if (argc >= 3) {
if (argc >= 3)
{
option = (char*) argv[3]; // 暂时我们只接受最多三个参数
}
matched = get_target (argv[2], Target_Set_Source, option);
@ -2390,7 +2433,8 @@ main (int argc, char const *argv[])
}
not_matched:
if (!matched) {
if (!matched)
{
xy_info ("chsrc: 暂不支持的换源目标,请使用 chsrc list targets 查看可换源软件");
return 1;
}