From 225972a9480eaff05806f4353972cad2fd0adc5c Mon Sep 17 00:00:00 2001 From: jimmy Date: Wed, 1 May 2024 01:05:53 +0800 Subject: [PATCH] [Enhancement] search platform list to find a command declare platformlist in tldr.h so everyfile can access platformlist --- CHANGELOG.md | 1 + src/parser.c | 33 ++++++++++++++++++++++----------- src/tldr.h | 2 ++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d2ea1..abb6ec2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ will be removed after two major releases. - Fix updating a local database - Prevent freezing of auto-completion in zsh - Fix print usage of flags +- Search a command in all platforms if it is not found in user's platform. ### Removed diff --git a/src/parser.c b/src/parser.c index fa12cac..7cc3116 100644 --- a/src/parser.c +++ b/src/parser.c @@ -12,6 +12,9 @@ #include #include #include +#include + +const char * const platformlist[PLATFORM_CNT] = {"linux", "common", "osx", "sunos", "windows"}; int construct_url(char *buf, size_t buflen, char const *input, char const *platform) @@ -149,6 +152,23 @@ parse_tldrpage(char const *input, int color_enabled) return 0; } +bool +IsPageInPlatformList(char const* const command, char const * const curPlatform, /* out */char **output) +{ + char url[URLBUFSIZ]; + construct_url(url, URLBUFSIZ, command, curPlatform); + download_content(url, output, 0); + if(*output == NULL) { + for(int i = 0 ; i < PLATFORM_CNT ; i++) { + if(strcmp(platformlist[i], curPlatform) == 0) continue; + construct_url(url, URLBUFSIZ, command, platformlist[i]); + download_content(url, output, 0); + if(*output != NULL) break; + } + } + return *output != NULL; +} + int print_tldrpage(char const *input, char const *poverride, int color_enabled) { @@ -206,19 +226,10 @@ print_tldrpage(char const *input, char const *poverride, int color_enabled) if (getenv(PREVENT_UPDATE_ENV_VARIABLE)) return 1; - - construct_url(url, URLBUFSIZ, input, platform); - - /* make clang's static analyzer happy */ output = NULL; - download_content(url, &output, 0); - if (output == NULL) { - construct_url(url, URLBUFSIZ, input, "common"); - download_content(url, &output, 0); - if (output == NULL) - return 1; + if(IsPageInPlatformList(input, platform, &output) == false) { + return 1; } - parse_tldrpage(output, color_enabled); free(output); diff --git a/src/tldr.h b/src/tldr.h index 74d0e22..a4b9c3b 100644 --- a/src/tldr.h +++ b/src/tldr.h @@ -53,6 +53,8 @@ #define ANSI_BOLD_ON "\x1b[1m" #define ANSI_BOLD_OFF "\x1b[22m" +#define PLATFORM_CNT 5 +extern const char * const platformlist[PLATFORM_CNT]; /* local.c */ long check_localdate (void); int update_localdate (void);