Add: cowsay, splash

This commit is contained in:
xamidev
2024-08-24 21:50:20 +02:00
parent 2893e75ad1
commit 942b4fa88e
7 changed files with 134 additions and 6 deletions

View File

@@ -51,7 +51,11 @@ Options:
#### `primes`
Computes prime numbers up to `PRIMES_MAX`, defined in `src/programs/primes.c`.
Computes prime numbers.
Options:
- `<nothing>` will default to `PRIMES_MAX` (a million)
- `<integer>` will compute primes up to that number
#### `rainbow`

View File

@@ -7,12 +7,15 @@
#include "../libc/stdio.h"
#include "../libc/string.h"
#include "../programs/programs.h"
#include "../libc/crypto.h"
#include <stdint.h>
#define BUFFER_SIZE 256
#define MAX_COMMANDS 16
#define MAX_ARGS 64
// Splash screen: esthetic stuff.
char* ascii_title =
"\n"
"----------------------------------------------\n"
@@ -21,6 +24,22 @@ char* ascii_title =
"----------------------------------------------\n"
"\n";
char* motd[] =
{
"Now in 2D!",
"Supercalifragilisticexpialidocious!",
"Tylko jedno w głowie mam!",
};
int motd_size = sizeof(motd)/sizeof(motd[0]);
void splash()
{ // Change that seed to something RTC-related (need RTC driver)
int random = randint(global_ticks);
char* motd_pick = motd[random%motd_size];
cowsay(motd_pick, red, black);
puts("\n");
}
typedef void (*command_func_t)(int argc, char *argv[]);
typedef struct
@@ -66,8 +85,8 @@ int parse_input(char* input, char* argv[], int max_args)
}
void shell_install()
{
colorputs(ascii_title, yellow, black);
{
splash();
register_command("help", program_help);
register_command("panic", program_panic);
@@ -83,6 +102,7 @@ void shell_install()
register_command("conway", program_conway);
register_command("rot13", program_rot13);
register_command("morse", program_morse);
register_command("cowsay", program_cowsay);
for (;;)
{

View File

@@ -86,3 +86,20 @@ int atoi(char* str)
return result;
}
void strcat(char* dest, const char* src)
{
while (*dest)
{
dest++;
}
while (*src)
{
*dest = *src;
dest++;
src++;
}
*dest = '\0';
}

View File

@@ -10,5 +10,7 @@ int strlen(const char* str);
int strcmp(const char* str1, const char* str2);
char* strtok(char* str, const char* delimiter);
int atoi(char* str);
void strcat(char* dest, const char* src);
#endif

72
src/programs/cowsay.c Normal file
View File

@@ -0,0 +1,72 @@
// Cowsay-like program
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include "../libc/stdio.h"
#include "../libc/string.h"
#define MAX_MSG_LEN 128
const char* cow =
" \\ ^__^\n"
" \\ (oo)\\_______\n"
" (__)\\ )\\/\\\n"
" ||----w |\n"
" || ||\n";
void print_bubble(const char* message)
{
int len = strlen(message);
puts(" ");
for (int i=0; i<len+2; i++)
{
puts("_");
}
puts("\n");
printf("< %s >\n", message);
puts(" ");
for (int i=0; i<len+2; i++)
{
puts("-");
}
puts("\n");
}
void cowsay(char* msg, uint32_t fg, uint32_t bg)
{
print_bubble(msg);
colorputs(cow, fg, bg);
}
void program_cowsay(int argc, char* argv[])
{
if (argc < 2)
{
printf("Usage: %s <message>\n", argv[0]);
return;
}
char message[MAX_MSG_LEN];
message[0] = '\0';
for (int i=1; i<argc; i++)
{
if (strlen(message) + strlen(argv[i]) + 1 < MAX_MSG_LEN)
{
strcat(message, argv[i]);
if (i < argc-1)
{
strcat(message, " ");
}
} else {
puts("Too long message.\n");
return;
}
}
print_bubble(message);
printf("%s", cow);
}

View File

@@ -6,6 +6,7 @@
#include <stdint.h>
#include "../libc/stdio.h"
#include "../kernel/system.h"
#include "../libc/string.h"
#define PRIMES_MAX 1000000
@@ -16,15 +17,25 @@ bool isPrime(int n)
return true;
}
void program_primes()
void program_primes(int argc, char* argv[])
{
for (long long x=0; x<PRIMES_MAX; x++)
int primes_max;
if (argc == 1)
{
primes_max = PRIMES_MAX;
} else if (argc == 2)
{
primes_max = atoi(argv[1]);
}
for (long long x=0; x<primes_max; x++)
{
if (isPrime(x))
{
printf("%d ", x);
}
delay(2);
delay(1);
}
puts("\n");
}

View File

@@ -15,6 +15,8 @@ void program_sysinfo();
void get_cpuid();
void get_meminfo(unsigned int multiboot_info_address);
void program_conway();
void program_cowsay();
void cowsay(); // Splash screen
// Ciphers
void program_rot13();