Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] search platform list to find a command #116

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 22 additions & 11 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdbool.h>

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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/tldr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down