Compare commits

...

10 Commits

Author SHA1 Message Date
_N3m0 562449a727 finit ? 2024-01-04 22:33:20 +01:00
_N3m0 3603776303 eval: basic done 2024-01-04 19:58:12 +01:00
_N3m0 8619caa1b6 eval my balls 2024-01-04 19:36:30 +01:00
_N3m0 25f9d42e9f create token get value 2024-01-04 15:02:39 +01:00
_N3m0 d4d0cc1812 get param dinamicly 2024-01-04 14:56:56 +01:00
_N3m0 466a76fcbc lexer: get param, A get link in value 2024-01-03 20:00:08 +01:00
_N3m0 3cf1948081 fix: token (gl) 2024-01-03 19:08:22 +01:00
_N3m0 b37d65b10f no get param, still broken 2024-01-03 17:41:19 +01:00
_N3m0 2f51400fef get param but broken 2024-01-03 16:17:52 +01:00
_N3m0 0cd1120020 MORE TOKEN 2024-01-03 03:22:27 +01:00
7 changed files with 448 additions and 109 deletions

View File

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

142
eval.c Normal file
View File

@ -0,0 +1,142 @@
#include "eval.h"
#include "format.h"
#include "lexer.h"
#include "form.h"
// TODO: BLOCKQUOTE OL PROGRESS CODE TABLE
#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");
if (text[i] != '\n'){
printf("%c", text[i]);
state.y++;
state.beginLine = 0;
}
if (!state.beginLine && (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 == UL){
state.inList = 1;
}
else if (token->type == UL){
state.inList = 0;
}
else if (token->type == LI)
{
break_line();
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 == P)
{
if (!state.inList) break_line();
}
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("-------------------------------------------------------------------------------"));
}
else if (token->type == BLOCKQUOTE)
{
print_text(EXPAND_LIT(" "));
printf(FC_SWAP);
}
else if (token->type == END_BLOCKQUOTE)
{
printf(FC_nSWAP);
}
}

16
eval.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef EVAL_H
#define EVAL_H
#include "lexer.h"
typedef struct PageState {
int nbLink;
int x;
int y;
int beginLine;
int inList;
} 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

303
lexer.c
View File

@ -1,6 +1,7 @@
#include "lexer.h" #include "lexer.h"
#define HTML_BALISE_LEN 12 #define HTML_BALISE_LEN 12
#define DA_LEN 64
typedef struct Cursor { typedef struct Cursor {
int chunk; int chunk;
@ -17,68 +18,15 @@ Cursor prev = {
.offset = -2, .offset = -2,
}; };
TokenType token_by_name(const char name[HTML_BALISE_LEN]);
void printtoken(Token* token){ void printtoken(Token* token){
if (token == NULL){ if (token == NULL){
puts("NULL TOKEN"); puts("NULL TOKEN");
return; return;
} }
switch (token->type) { printf("%d: ", token->type);
case UNDEFINED_TYPE:
printf("UNDEFINED_TYPE: ");
break;
case DONT_CARE:
printf("DONT_CARE: ");
break;
case TEXT:
printf("TEXT: ");
break;
case BODY:
printf("BODY: ");
break;
case END_BODY:
printf("END_BODY: ");
break;
case HTML:
printf("HTML: ");
break;
case END_HTML:
printf("END_HTML: ");
break;
case A:
printf("A: ");
break;
case END_A:
printf("END_A: ");
break;
case UL:
printf("UL: ");
break;
case LI:
printf("LI: ");
break;
case H1:
printf("H1: ");
break;
case H2:
printf("H2: ");
break;
case H3:
printf("H3: ");
break;
case H4:
printf("H4: ");
break;
case H5:
printf("H5: ");
break;
case H6:
printf("H6: ");
break;
default:
printf("ERROR: UNKNOWN TOKEN: ");
break;
}
if (token->value == NULL){ if (token->value == NULL){
puts("'NO VALUE FOUND'"); puts("'NO VALUE FOUND'");
@ -126,60 +74,77 @@ void go_back(void){
prev.offset--; prev.offset--;
if (curr.chunk < 0 || curr.offset < 0){ if (curr.chunk < 0 || curr.offset < 0){
puts("ERROR: go way too back."); puts("ERROR: cursor got way too back.");
exit(1); exit(1);
} }
} }
TokenType token_by_name(const char name[HTML_BALISE_LEN]){ char* getParam(const char* word, int len, char* cursor, int* size){
if (name == NULL){ char* res = NULL;
return UNDEFINED_TYPE; int found = 0;
} else if (strncmp(name, "body", HTML_BALISE_LEN) == 0){ len--;
return BODY;
} else if (strncmp(name, "/body", HTML_BALISE_LEN) == 0){
return END_BODY;
} else if (strncmp(name, "html", HTML_BALISE_LEN) == 0){
return HTML;
} else if (strncmp(name, "/html", HTML_BALISE_LEN) == 0){
return END_HTML;
} else if (strncmp(name, "a", HTML_BALISE_LEN) == 0){
return A;
} else if (strncmp(name, "/a", HTML_BALISE_LEN) == 0){
return END_A;
} else if (strncmp(name, "ul", HTML_BALISE_LEN) == 0){
return UL;
} else if (strncmp(name, "li", HTML_BALISE_LEN) == 0){
return LI;
} else if (strncmp(name, "h1", HTML_BALISE_LEN) == 0){
return H1;
} else if (strncmp(name, "h2", HTML_BALISE_LEN) == 0){
return H2;
} else if (strncmp(name, "h3", HTML_BALISE_LEN) == 0){
return H3;
} else if (strncmp(name, "h4", HTML_BALISE_LEN) == 0){
return H4;
} else if (strncmp(name, "h5", HTML_BALISE_LEN) == 0){
return H5;
} else if (strncmp(name, "h6", HTML_BALISE_LEN) == 0){
return H6;
}
return DONT_CARE;
}
Token* create_text_token(Token* token, char* cursor){
int i = 0;
do { do {
cursor = nextchar(); cursor = nextchar();
for (int i=0; i<len; i++){
if (word[i] != *cursor){
found = -1;
break;
} else {
found = 1;
cursor = nextchar();
}
}
if (found == 1){
while (*cursor != '"'){
cursor = nextchar();
}
int cap = DA_LEN;
res = malloc(sizeof(char) * cap);
*size = 0;
do {
cursor = nextchar();
res[*size] = *cursor;
(*size)++;
if (*size >= cap){
cap *= 2;
res = realloc(res, cap);
}
} while (*cursor != '"');
res[*size-1] = '\0';
break;
}
} while (*cursor != '>');
return res;
}
Token* create_text_token(Token* token, char* cursor){
token = malloc(sizeof(Token));
token->value = malloc(sizeof(char) * DA_LEN);
int i = 0, cap = DA_LEN;
go_back();
do {
cursor = nextchar();
token->value[i] = *cursor;
i++; i++;
if (i >= cap){
cap *= 2;
token->value = realloc(token->value, cap);
}
} while (*cursor != '<'); } while (*cursor != '<');
token->value[i-1] = '\0';
go_back(); go_back();
token = malloc(sizeof(Token));
token->type = TEXT; token->type = TEXT;
token->value = "TODO"; token->len = i-1;
token->len = i;
return token; return token;
} }
@ -197,9 +162,27 @@ Token* create_balise_token(Token* token, char* cursor){
token = malloc(sizeof(Token)); token = malloc(sizeof(Token));
token->type = token_by_name(balise); token->type = token_by_name(balise);
token->value = malloc(sizeof(char) * len);
strncpy(token->value, balise, len+1); if (token->type == A){
token->len = len; token->value = getParam("href", sizeof("href"), cursor, &token->len);
} else if (token->type == IMG) {
int srclen, altlen, totallen;
char* src = getParam("src", sizeof("src"), cursor, &srclen);
char* alt = getParam("alt", sizeof("alt"), cursor, &altlen);
totallen = srclen * altlen + 1;
token->value = malloc(sizeof(char) * (totallen));
strncpy(token->value, src, srclen);
token->value[srclen-1] = ' ';
strncpy(token->value+srclen, alt, altlen);
token->value[totallen] = '\0';
token->len = totallen;
} else {
token->value = malloc(sizeof(char) * len);
strncpy(token->value, balise, len+1);
token->len = len;
}
go_back(); go_back();
do { do {
@ -228,3 +211,121 @@ Token* nexttoken(void){
return token; return token;
} }
TokenType token_by_name(const char name[HTML_BALISE_LEN]){
if (name == NULL){
return UNDEFINED_TYPE;
} else if (strncmp(name, "body", HTML_BALISE_LEN) == 0){
return BODY;
} else if (strncmp(name, "/body", HTML_BALISE_LEN) == 0){
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){
return END_EM;
} else if (strncmp(name, "i", HTML_BALISE_LEN) == 0){
return I;
} else if (strncmp(name, "/i", HTML_BALISE_LEN) == 0){
return END_I;
} else if (strncmp(name, "b", HTML_BALISE_LEN) == 0){
return B;
} else if (strncmp(name, "/b", HTML_BALISE_LEN) == 0){
return END_B;
} else if (strncmp(name, "strong", HTML_BALISE_LEN) == 0){
return STRONG;
} else if (strncmp(name, "/strong", HTML_BALISE_LEN) == 0){
return END_STRONG;
} else if (strncmp(name, "hr", HTML_BALISE_LEN) == 0){
return HR;
} else if (strncmp(name, "br", HTML_BALISE_LEN) == 0){
return BR;
} else if (strncmp(name, "p", HTML_BALISE_LEN) == 0){
return P;
} 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){
return END_A;
} else if (strncmp(name, "ol", HTML_BALISE_LEN) == 0){
return OL;
} else if (strncmp(name, "/ol", HTML_BALISE_LEN) == 0){
return END_OL;
} else if (strncmp(name, "ul", HTML_BALISE_LEN) == 0){
return UL;
} else if (strncmp(name, "/ul", HTML_BALISE_LEN) == 0){
return END_UL;
} else if (strncmp(name, "li", HTML_BALISE_LEN) == 0){
return LI;
} else if (strncmp(name, "/li", HTML_BALISE_LEN) == 0){
return END_LI;
} else if (strncmp(name, "img", HTML_BALISE_LEN) == 0){
return IMG;
} else if (strncmp(name, "/img", HTML_BALISE_LEN) == 0){
return END_IMG;
} else if (strncmp(name, "blockquote", HTML_BALISE_LEN) == 0){
return BLOCKQUOTE;
} else if (strncmp(name, "/blockquote", HTML_BALISE_LEN) == 0){
return END_BLOCKQUOTE;
} else if (strncmp(name, "code", HTML_BALISE_LEN) == 0){
return CODE;
} else if (strncmp(name, "/code", HTML_BALISE_LEN) == 0){
return END_CODE;
} else if (strncmp(name, "h1", HTML_BALISE_LEN) == 0){
return H1;
} else if (strncmp(name, "h2", HTML_BALISE_LEN) == 0){
return H2;
} else if (strncmp(name, "h3", HTML_BALISE_LEN) == 0){
return H3;
} else if (strncmp(name, "h4", HTML_BALISE_LEN) == 0){
return H4;
} else if (strncmp(name, "h5", HTML_BALISE_LEN) == 0){
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){
return END_TABLE;
} else if (strncmp(name, "thead", HTML_BALISE_LEN) == 0){
return THEAD;
} else if (strncmp(name, "/thead", HTML_BALISE_LEN) == 0){
return END_THEAD;
} else if (strncmp(name, "tbody", HTML_BALISE_LEN) == 0){
return TBODY;
} else if (strncmp(name, "/tbody", HTML_BALISE_LEN) == 0){
return END_TBODY;
} else if (strncmp(name, "tr", HTML_BALISE_LEN) == 0){
return TR;
} else if (strncmp(name, "/tr", HTML_BALISE_LEN) == 0){
return END_TR;
} else if (strncmp(name, "th", HTML_BALISE_LEN) == 0){
return TH;
} else if (strncmp(name, "/th", HTML_BALISE_LEN) == 0){
return END_TH;
} else if (strncmp(name, "progress", HTML_BALISE_LEN) == 0){
return PROGRESS;
} else if (strncmp(name, "/progress", HTML_BALISE_LEN) == 0){
return END_PROGRESS;
}
return DONT_CARE;
}

18
lexer.h
View File

@ -7,15 +7,27 @@
#include "page.h" #include "page.h"
// 46 enum
typedef enum TokenType { typedef enum TokenType {
UNDEFINED_TYPE, UNDEFINED_TYPE,
DONT_CARE, DONT_CARE,
TEXT, TITLE, END_TITLE,
TEXT, END_HTML,
BODY, END_BODY, BODY, END_BODY,
HTML, END_HTML,
A, END_A, A, END_A,
UL, LI, IMG, END_IMG,
BLOCKQUOTE, END_BLOCKQUOTE,
CODE, END_CODE,
HR, BR, P, END_P,
PROGRESS, END_PROGRESS,
STRONG, B, I, EM,
END_STRONG, END_B, END_I, END_EM,
UL, OL, LI, END_UL, END_OL, END_LI,
TABLE, END_TABLE, THEAD,
END_THEAD, TBODY, END_TBODY,
TR, TH, END_TR, END_TH,
H1, H2, H3, H4, H5, H6, H1, H2, H3, H4, H5, H6,
END_H1, END_H2, END_H3, END_H4, END_H5, END_H6,
} TokenType; } TokenType;
typedef struct Token { typedef struct Token {

10
main.c
View File

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