eval my balls

This commit is contained in:
_N3m0 2024-01-04 19:36:30 +01:00
parent 25f9d42e9f
commit 8619caa1b6
7 changed files with 224 additions and 8 deletions

View File

@ -1,5 +1,5 @@
BIN=webpage
SRCS=main.c config.c page.c lexer.c
SRCS=main.c config.c page.c lexer.c eval.c
INC=.
LIB=curl
FLAGS=-Wall -Wextra -Og -g -ggdb -fvar-tracking
@ -12,7 +12,11 @@ $(BIN) : $(SRCS)
run : $(BIN)
./$(BIN) $(input)
check :
cppcheck --enable=all --suppress=missingIncludeSystem -I$(INC) .
flawfinder .
clean :
rm -f $(BIN) *.o
.PHONY : run clean
.PHONY : run check clean

118
eval.c Normal file
View File

@ -0,0 +1,118 @@
#include "eval.h"
#include "format.h"
#include "lexer.h"
#include "form.h"
#define PAGE_WIDTH 80
#define EXPAND_LIT(x) x, sizeof(x)
PageState state = {0};
void print_text(const char* text, int len){
for (int i=0; i<len; i++){
if (state.beginLine) printf("\n");
printf("%c", text[i]);
state.y++;
state.beginLine = 0;
if (state.y >= PAGE_WIDTH || text[i] == '\n'){
state.y = 0;
state.x++;
state.beginLine = 1;
}
}
state.beginLine = state.beginLine || text[len] == '\n';
}
void break_line(void){
state.y = 0;
state.x++;
state.beginLine = 1;
}
void evaluate(Token* token){
if (token->type == TITLE)
{
printf(FC_BOLD);
print_text(EXPAND_LIT("--- "));
}
else if (token->type == END_TITLE)
{
print_text(EXPAND_LIT(" ---"));
printf(FC_nBOLD);
break_line();
}
else if (token->type == LI)
{
print_text(EXPAND_LIT("- "));
}
else if (token->type == END_LI)
{
//print_text(EXPAND_LIT("\n"));
}
else if (token->type == STRONG || token->type == B)
{
printf(FC_BOLD);
}
else if (token->type == END_STRONG || token->type == END_B)
{
printf(FC_nBOLD);
print_text(EXPAND_LIT(" "));
}
else if (token->type == EM || token->type == I)
{
printf(FC_ITA);
}
else if (token->type == END_EM || token->type == END_I)
{
printf(FC_nITA);
}
else if (token->type == IMG)
{
print_text(EXPAND_LIT("["));
print_text(EXPAND_LIT("IMG"));
print_text(EXPAND_LIT("]"));
}
else if (token->type == A)
{
print_text(EXPAND_LIT("["));
char* s = malloc(sizeof(int)*2);
sprintf(s, "%d", state.nbLink);
print_text(s, strlen(s));
state.nbLink++;
free(s);
print_text(EXPAND_LIT("]"));
printf(FC_UDL);
}
else if (token->type == END_A)
{
printf(FC_nUDL);
print_text(EXPAND_LIT(" "));
}
else if (token->type == END_P)
{
break_line();
}
else if (token->type == TEXT)
{
print_text(token->value, token->len);
}
else if (token->type == H1 || token->type == H2 || token->type == H3 || token->type == H4 || token->type == H5 || token->type == H6)
{
break_line();
printf(FC_BOLD);
}
else if (token->type == END_H1 || token->type == END_H2 || token->type == END_H3 || token->type == END_H4 || token->type == END_H5 || token->type == END_H6)
{
printf(FC_nBOLD);
}
else if (token->type == HR)
{
printf(FC_nUDL);
break_line();
print_text(EXPAND_LIT("-------------------------------------------------------------------------------"));
}
}

15
eval.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef EVAL_H
#define EVAL_H
#include "lexer.h"
typedef struct PageState {
int nbLink;
int x;
int y;
int beginLine;
} PageState;
void evaluate(Token* token);
#endif // EVAL_H

60
format.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef FORMAT_H
#define FORMAT_H
// COLOR
#define FC_RES "\033[0m"
#define FC_BOLD "\033[1m"
#define FC_ITA "\033[3m"
#define FC_UDL "\033[4m"
#define FC_BIK "\033[5m"
#define FC_SWAP "\033[7m"
#define FC_HID "\033[8m"
#define FC_STR "\033[9m"
#define FC_DEF "\033[39m"
#define FC_nBOLD "\033[22m"
#define FC_nITA "\033[23m"
#define FC_nUDL "\033[24m"
#define FC_nBIK "\033[25m"
#define FC_nSWAP "\033[27m"
#define FC_nHID "\033[28m"
#define FC_nSTR "\033[29m"
#define FC_bDEF "\033[49m"
#define FC_BLK "\033[30m"
#define FC_RED "\033[31m"
#define FC_GRN "\033[32m"
#define FC_YEL "\033[33m"
#define FC_BLU "\033[34m"
#define FC_MAG "\033[35m"
#define FC_CYA "\033[36m"
#define FC_WHI "\033[37m"
#define FC_lBLK "\033[90m"
#define FC_lRED "\033[91m"
#define FC_lGRN "\033[92m"
#define FC_lYEL "\033[93m"
#define FC_lBLU "\033[94m"
#define FC_lMAG "\033[95m"
#define FC_lCYA "\033[96m"
#define FC_lWHI "\033[97m"
#define FC_bBLK "\033[40m"
#define FC_bRED "\033[41m"
#define FC_bGRN "\033[42m"
#define FC_bYEL "\033[43m"
#define FC_bBLU "\033[44m"
#define FC_bMAG "\033[45m"
#define FC_bCYA "\033[46m"
#define FC_bWHI "\033[47m"
#define FC_blBLK "\033[100m"
#define FC_blRED "\033[101m"
#define FC_blGRN "\033[102m"
#define FC_blYEL "\033[103m"
#define FC_blBLU "\033[104m"
#define FC_blMAG "\033[105m"
#define FC_blCYA "\033[106m"
#define FC_blWHI "\033[107m"
#endif // FORMAT_H

21
lexer.c
View File

@ -1,6 +1,4 @@
#include "lexer.h"
#include <stdlib.h>
#include <string.h>
#define HTML_BALISE_LEN 12
#define DA_LEN 64
@ -173,7 +171,6 @@ Token* create_balise_token(Token* token, char* cursor){
char* alt = getParam("alt", sizeof("alt"), cursor, &altlen);
totallen = srclen * altlen + 1;
printf("IMG: src = '%s', alt = '%s'.\n", src, alt);
token->value = malloc(sizeof(char) * (totallen));
strncpy(token->value, src, srclen);
token->value[srclen-1] = ' ';
@ -224,6 +221,10 @@ TokenType token_by_name(const char name[HTML_BALISE_LEN]){
return END_BODY;
} else if (strncmp(name, "/html", HTML_BALISE_LEN) == 0){
return END_HTML;
} else if (strncmp(name, "title", HTML_BALISE_LEN) == 0){
return TITLE;
} else if (strncmp(name, "/title", HTML_BALISE_LEN) == 0){
return END_TITLE;
} else if (strncmp(name, "em", HTML_BALISE_LEN) == 0){
return EM;
} else if (strncmp(name, "/em", HTML_BALISE_LEN) == 0){
@ -244,6 +245,8 @@ TokenType token_by_name(const char name[HTML_BALISE_LEN]){
return HR;
} else if (strncmp(name, "br", HTML_BALISE_LEN) == 0){
return BR;
} else if (strncmp(name, "/p", HTML_BALISE_LEN) == 0){
return END_P;
} else if (strncmp(name, "a", HTML_BALISE_LEN) == 0){
return A;
} else if (strncmp(name, "/a", HTML_BALISE_LEN) == 0){
@ -284,6 +287,18 @@ TokenType token_by_name(const char name[HTML_BALISE_LEN]){
return H5;
} else if (strncmp(name, "h6", HTML_BALISE_LEN) == 0){
return H6;
} else if (strncmp(name, "/h1", HTML_BALISE_LEN) == 0){
return END_H1;
} else if (strncmp(name, "/h2", HTML_BALISE_LEN) == 0){
return END_H2;
} else if (strncmp(name, "/h3", HTML_BALISE_LEN) == 0){
return END_H3;
} else if (strncmp(name, "/h4", HTML_BALISE_LEN) == 0){
return END_H4;
} else if (strncmp(name, "/h5", HTML_BALISE_LEN) == 0){
return END_H5;
} else if (strncmp(name, "/h6", HTML_BALISE_LEN) == 0){
return H6;
} else if (strncmp(name, "table", HTML_BALISE_LEN) == 0){
return TABLE;
} else if (strncmp(name, "/table", HTML_BALISE_LEN) == 0){

View File

@ -11,13 +11,14 @@
typedef enum TokenType {
UNDEFINED_TYPE,
DONT_CARE,
TITLE, END_TITLE,
TEXT, END_HTML,
BODY, END_BODY,
A, END_A,
IMG, END_IMG,
BLOCKQUOTE, END_BLOCKQUOTE,
CODE, END_CODE,
HR, BR,
HR, BR, END_P,
PROGRESS, END_PROGRESS,
STRONG, B, I, EM,
END_STRONG, END_B, END_I, END_EM,
@ -26,6 +27,7 @@ typedef enum TokenType {
END_THEAD, TBODY, END_TBODY,
TR, TH, END_TR, END_TH,
H1, H2, H3, H4, H5, H6,
END_H1, END_H2, END_H3, END_H4, END_H5, END_H6,
} TokenType;
typedef struct Token {

6
main.c
View File

@ -1,6 +1,7 @@
#include "config.h"
#include "page.h"
#include "lexer.h"
#include "eval.h"
int main(int argc, char* argv[]){
@ -10,9 +11,10 @@ int main(int argc, char* argv[]){
Token* token = NULL;
do {
token = nexttoken();
printtoken(token);
//evaluate(token);
evaluate(token);
} while (token != NULL && token->type != END_HTML);
printf("\n");
return 0;
}