Skip to content

Commit

Permalink
Added current version utility, int limits and math lib .h
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Jul 21, 2022
1 parent 85f6e0f commit df69846
Show file tree
Hide file tree
Showing 21 changed files with 206 additions and 11 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ primus-os.bin: $(SRC_DIR)/linker.ld $(OBJ_FILES1) $(OBJ_FILES2) $(OBJ_FILES3)
ld $(LDPARAMS) -T $< -o $@ $(OBJ_DIR)/*.o

primus-os.iso: primus-os.bin
./update_version
mkdir iso
mkdir iso/boot
mkdir iso/boot/grub
Expand Down
1 change: 1 addition & 0 deletions current_version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.0.27
File renamed without changes.
4 changes: 4 additions & 0 deletions include/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "stdint.h"
#include "tty.h"

#define true 1
#define false 0

/* The I/O ports */
#define FB_COMMAND_PORT 0x3d4
#define FB_DATA_PORT 0x3d5
Expand All @@ -30,6 +33,7 @@
#define check_flag(flags, n) ((flags)&bit(n))

void reboot();
void shutdown();
uint8_t input_bytes(uint16_t port);
void output_bytes(uint16_t port, uint8_t val);
uint8_t inw(uint16_t port);
Expand Down
7 changes: 7 additions & 0 deletions include/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _LIMITS_H
#define _LIMITS_H 1

#define INT_MAX 2147483647
#define INT_MIN -2147483648

#endif
19 changes: 19 additions & 0 deletions include/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef _MATH_H
#define _MATH_H 1

#include "stdint.h"

uint32_t factorial(uint32_t n);
uint32_t pow(uint32_t num, uint32_t n);
double sqrt(double n);
double sin(double n);
double cos(double n);
double tan(double n);
double asin(double n);
double acos(double n);
double atan(double n);
double exp(double n);
double log(double n);
double log10(double n);

#endif
9 changes: 9 additions & 0 deletions include/math_shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _MATH_SHELL_H
#define _MATH_SHELL_H 1

#include "stdint.h"

uint32_t parse_int(char *string);
double parse_float(char *string);

#endif
1 change: 1 addition & 0 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ char *tolower(char *string);
static char *sitoa(char *buf, unsigned int num, int width, enum flag_itoa flags);
int vsprintf(char *buf, const char *fmt, va_list va);
int sprintf(char *buf, const char *fmt, ...);
uint32_t atoi(const char *str);

#endif
File renamed without changes.
8 changes: 8 additions & 0 deletions include/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _VERSION_H
#define _VERSION_H 1

#define V1 1
#define V2 0
#define V3 27

#endif
Binary file modified primus-os.bin
Binary file not shown.
Binary file modified primus-os.iso
Binary file not shown.
File renamed without changes.
7 changes: 7 additions & 0 deletions src/io.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#include "../include/io.h"
#include "../include/tty.h"

void shutdown()
{
outw(0xB004, 0x2000);
outw(0x604, 0x2000);
outw(0x4004, 0x3400);
}

void reboot()
{
uint8_t temp;
Expand Down
32 changes: 21 additions & 11 deletions src/kernel.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
#include "../include/version.h"
#include "../include/tty.h"
#include "../include/io.h"
#include "../include/kbd.h"
#include "../include/string.h"
#include "../include/time.h"
#include "../include/date.h"
#include "../include/datetime.h"
#include "../include/math.h"

#define BUFFER_SIZE 1024

uint8_t numlock = true;
uint8_t capslock = false;
uint8_t scrolllock = false;
uint8_t shift = false;
char current_version[7];

int main(void)
{
char buffer[BUFFER_SIZE];
int command_size = 0;
uint8_t byte;

terminal_initialize(COLOR_LIGHT_GREY, COLOR_BLACK);
printf("Welcome! I'm a super advanced operational system with lots of features.\n\n");
printf("Last build - date: %s - time: %s\n", date(), time());
sprintf(current_version, "%u.%u.%u", V1, V2, V3 + 1);
printf("\nPrimusOS - version %s\n", current_version);
printf("Last build - date: %s - time: %s\n", __DATE__, __TIME__);
printf("Current datetime: ");
datetime();
printf("\nWelcome!\n\n");

strcpy(&buffer[strlen(buffer)], "");
print_prompt();
while (1)
while (true)
{
while (byte = scan())
{
Expand Down Expand Up @@ -109,18 +117,22 @@ int main(void)
printf("\nA Brazilian developer whose name is Leonardo Araujo. Check out his GitHub:\nhttps://github.com/araujo88");
terminal_set_colors(COLOR_LIGHT_GREY, COLOR_BLACK);
}
else if (strlen(buffer) > 0 && strstr(buffer, "factorial(") != NULL)
{
char *parser;
parser = strstr(buffer, "factorial(");
printf("\n%s", parser);
}
else if (strlen(buffer) > 0 && strcmp(buffer, "clear") == 0)
{
terminal_initialize(COLOR_LIGHT_GREY, COLOR_BLACK);
strcpy(&buffer[strlen(buffer)], "");
}
else if (strlen(buffer) > 0 && strcmp(buffer, "time") == 0)
{
printf("\nTime: %s", time());
}
else if (strlen(buffer) > 0 && strcmp(buffer, "date") == 0)
{
printf("\nDate: %s", date());
}
else if (strlen(buffer) > 0 && strcmp(buffer, "datetime") == 0)
{
Expand All @@ -137,9 +149,7 @@ int main(void)
}
else if (strlen(buffer) > 0 && strcmp(buffer, "shutdown") == 0)
{
outw(0xB004, 0x2000);
outw(0x604, 0x2000);
outw(0x4004, 0x3400);
shutdown();
}
else if (strlen(buffer) == 0)
{
Expand Down Expand Up @@ -179,4 +189,4 @@ int main(void)
command_size = 0;
}
return 0;
}
}
17 changes: 17 additions & 0 deletions src/math.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "../include/math.h"

uint32_t factorial(uint32_t n)
{
if (n == 0)
{
return 1;
}
else if (n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
13 changes: 13 additions & 0 deletions src/math_shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "../include/math_shell.h"
#include "../include/string.h"

#include "stdint.h"

uint32_t parse_int(char *string)
{
uint32_t i;
char *parser;
while (string[i] != '\0')
{
}
}
36 changes: 36 additions & 0 deletions src/string.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "../include/string.h"
#include "../include/limits.h"

#include "stdarg.h"
#include "stdint.h"

char *ctos(char s[2], const char c)
{
Expand Down Expand Up @@ -278,4 +282,36 @@ int sprintf(char *buf, const char *fmt, ...)
int ret = vsprintf(buf, fmt, va);
va_end(va);
return ret;
}

uint32_t atoi(const char *str)
{
uint32_t sign = 1, base = 0, i = 0;

// if whitespaces then ignore.
while (str[i] == ' ')
{
i++;
}

// sign of number
if (str[i] == '-' || str[i] == '+')
{
sign = 1 - 2 * (str[i++] == '-');
}

// checking for valid input
while (str[i] >= '0' && str[i] <= '9')
{
// handling overflow test case
if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7))
{
if (sign == 1)
return INT_MAX;
else
return INT_MIN;
}
base = 10 * base + (str[i++] - '0');
}
return base * sign;
}
File renamed without changes.
Binary file added update_version
Binary file not shown.
62 changes: 62 additions & 0 deletions update_version.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
const char s[2] = ".";
char *token;
unsigned int version[3];
unsigned short int i = 0;
int count = 0;

fp = fopen("current_version.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
{
/* get the first token */
token = strtok(line, s);

/* walk through other tokens */
while (token != NULL)
{
version[i] = atoi(token);
i++;
token = strtok(NULL, s);
}
}
fclose(fp);
if (line)
free(line);
line = NULL;
len = 0;

fp = fopen("current_version.txt", "w");
if (fp == NULL)
exit(EXIT_FAILURE);
printf("%d\n", version[2]);
version[2] = version[2] + 1;
printf("%d\n", version[2]);
fprintf(fp, "%u.%u.%u", version[0], version[1], version[2]);
fclose(fp);

i = 0;
fp = fopen("include/version.h", "w");
if (fp == NULL)
exit(EXIT_FAILURE);

fprintf(fp, "#ifndef _VERSION_H\n");
fprintf(fp, "#define _VERSION_H 1\n\n");
for (i = 0; i < 3; i++)
{
fprintf(fp, "#define V%d %u\n", i + 1, version[i]);
}
fprintf(fp, "\n#endif");

return 0;
}

0 comments on commit df69846

Please sign in to comment.