Install script and bugfix

This commit is contained in:
gauvain-thomas
2024-02-25 15:54:51 +01:00
parent 92f2d344b2
commit f09d15be31
5 changed files with 46 additions and 12 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.o
.vscode

23
Makefile Normal file
View File

@ -0,0 +1,23 @@
.POSIX:
CC = gcc
# CFLAGS = -Wall -Wextra -ansi -pedantic -std=c99 -g
CFLAGS = -Wall -ansi -pedantic -std=c99 -g -fsanitize=address
all: pkmn
install: all
cp pkmn /usr/local/bin
cp -r pokemons /usr/local/share/pkmn
uninstall:
rm /usr/local/bin/pkmn
rm -r /usr/local/share/pkmn
pkmn: pkmn.o
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
pkmn.o: pkmn.c
clean:
rm -f *.o

View File

@ -4,6 +4,12 @@
A simple program to display a Pokémon in the terminal. Based on cowsay. A simple program to display a Pokémon in the terminal. Based on cowsay.
## Installation
```bash
sudo make install
```
## Usage ## Usage
```bash ```bash
@ -38,6 +44,7 @@ pkmn -rs
## TODO ## TODO
- [ ] Add option to list available pokémon - [ ] Add option to list available pokémon
- [ ] Add option to open an fzf window to select a pokémon - [ ] Add option to open an fzf window to select a pokémon
- Until added use : `ls ~/pokemons/yes | fzf --prompt='Type a pokemon name : ' --preview 'pkmn {}' | xargs pkmn`
- [ ] Add argument to display a message - [ ] Add argument to display a message

BIN
pkmn Executable file

Binary file not shown.

21
pkmn.c
View File

@ -7,6 +7,8 @@
#include <string.h> #include <string.h>
#include <regex.h> #include <regex.h>
const char *pkmn_path = "/usr/local/share/pkmn";
const char *argp_program_version = const char *argp_program_version =
"pkmn 1.0"; "pkmn 1.0";
const char *argp_program_bug_address = const char *argp_program_bug_address =
@ -14,7 +16,7 @@ const char *argp_program_bug_address =
/* Program documentation. */ /* Program documentation. */
static char doc[] = static char doc[] =
"Argp example #3 -- a program with options and arguments using argp"; "pkmn -- A simple program to display a Pokémon in the terminal. Based on cowsay.";
/* A description of the arguments we accept. */ /* A description of the arguments we accept. */
static char args_doc[] = "pokemon_name"; static char args_doc[] = "pokemon_name";
@ -115,9 +117,9 @@ int pkmn_name_from_file(char *pokemon_file, char *pokemon_name) {
} else { } else {
fprintf(stderr, "Match too long for buffer\n"); fprintf(stderr, "Match too long for buffer\n");
} }
} else if (ret == REG_NOMATCH) { } else if (ret == REG_NOMATCH) {
fprintf(stderr, "No match found\n"); fprintf(stderr, "No match found\n");
} else { } else {
fprintf(stderr, "Regex match failed\n"); fprintf(stderr, "Regex match failed\n");
} }
@ -129,10 +131,8 @@ int pkmn_name_from_file(char *pokemon_file, char *pokemon_name) {
int get_pokemon_file(char *arg, char *pokemon_file) { int get_pokemon_file(char *arg, char *pokemon_file) {
// Finds a matching file in the pokemons directory from the argument // Finds a matching file in the pokemons directory from the argument
// Returns 0 if the file was found, 1 otherwise
// ls ~/pokemons/yes | fzf -f wp | head -1
char command[512]; char command[512];
sprintf(command, "ls ~/pokemons/yes | fzf -f %s | head -1 > /tmp/pkmn", arg); sprintf(command, "ls %s/yes | fzf -f %s | head -1 > /tmp/pkmn", pkmn_path, arg);
system(command); system(command);
FILE *f = fopen("/tmp/pkmn", "r"); FILE *f = fopen("/tmp/pkmn", "r");
fscanf(f, "%s", pokemon_file); fscanf(f, "%s", pokemon_file);
@ -146,7 +146,6 @@ int get_pokemon_file(char *arg, char *pokemon_file) {
int main (int argc, char **argv) { int main (int argc, char **argv) {
struct arguments arguments; struct arguments arguments;
char pkmn_path[] = "~/pokemons/";
/* Default values. */ /* Default values. */
arguments.verbose = 0; arguments.verbose = 0;
@ -170,7 +169,9 @@ int main (int argc, char **argv) {
char pokemon_file[512]; char pokemon_file[512];
if (arguments.random) { if (arguments.random) {
system("ls ~/pokemons/yes | shuf -n 1 > /tmp/pkmn"); char command[512];
sprintf(command, "ls %s/yes | shuf -n 1 > /tmp/pkmn", pkmn_path);
system(command);
FILE *f = fopen("/tmp/pkmn", "r"); FILE *f = fopen("/tmp/pkmn", "r");
fscanf(f, "%s", pokemon_file); fscanf(f, "%s", pokemon_file);
fclose(f); fclose(f);
@ -206,9 +207,9 @@ int main (int argc, char **argv) {
// Execute the command echo "$pokemon_name!" | cowsay -f "$cow_file" -W 70 // Execute the command echo "$pokemon_name!" | cowsay -f "$cow_file" -W 70
char command[1024]; char command[1024];
if (arguments.shiney) { if (arguments.shiney) {
sprintf(command, "echo \"Shiny %s!\" | cowsay -f %s%s -W 70", pokemon_name, pkmn_path, pokemon_file); sprintf(command, "echo \"Shiny %s!\" | cowsay -f %s/%s -W 70", pokemon_name, pkmn_path, pokemon_file);
} else { } else {
sprintf(command, "echo \"%s!\" | cowsay -f %syes/%s -W 70", pokemon_name, pkmn_path, pokemon_file); sprintf(command, "echo \"%s!\" | cowsay -f %s/yes/%s -W 70", pokemon_name, pkmn_path, pokemon_file);
} }
system(command); system(command);