Made it less bad I hope...

This commit is contained in:
Ognjen Milan Robovic 2023-08-28 09:03:06 -04:00
parent 883e8e546e
commit 44ba6563eb
2 changed files with 51 additions and 68 deletions

111
xyntax.c
View File

@ -1,7 +1,7 @@
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
* Xyntax is deallocate software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
* Xyntax is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
* And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
* It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
*/
@ -21,32 +21,6 @@ char * syntax_escape = NULL;
int * syntax_colour = NULL;
int * syntax_effect = NULL;
void syntax_define (int mode, int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
syntax_mode = reallocate (syntax_mode, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_mode));
syntax_enrange = reallocate (syntax_enrange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_enrange));
syntax_derange = reallocate (syntax_derange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_derange));
syntax_begin = reallocate (syntax_begin, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_begin));
syntax_end = reallocate (syntax_end, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_end));
syntax_escape = reallocate (syntax_escape, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_escape));
syntax_colour = reallocate (syntax_colour, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_colour));
syntax_effect = reallocate (syntax_effect, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_effect));
syntax_begin [syntax_count] = allocate ((string_length (begin) + 1) * (int) sizeof (* * syntax_begin));
syntax_end [syntax_count] = allocate ((string_length (end) + 1) * (int) sizeof (* * syntax_end));
syntax_mode [syntax_count] = mode;
syntax_enrange [syntax_count] = enrange;
syntax_derange [syntax_count] = derange;
syntax_escape [syntax_count] = escape;
syntax_colour [syntax_count] = colour;
syntax_effect [syntax_count] = effect;
string_copy (syntax_begin [syntax_count], begin);
string_copy (syntax_end [syntax_count], end);
++syntax_count;
}
static int compare_character_array (char character, char * character_array, int count) {
int i = 0;
@ -59,10 +33,41 @@ static int compare_character_array (char character, char * character_array, int
return (0);
}
int syntax_output (char * string) {
int offset = 0;
int select = 0;
char format [8] = "\033[ ;3 m";
void syntax_define (int * index, int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
syntax_mode = reallocate (syntax_mode, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_mode));
syntax_enrange = reallocate (syntax_enrange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_enrange));
syntax_derange = reallocate (syntax_derange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_derange));
syntax_begin = reallocate (syntax_begin, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_begin));
syntax_end = reallocate (syntax_end, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_end));
syntax_escape = reallocate (syntax_escape, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_escape));
syntax_colour = reallocate (syntax_colour, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_colour));
syntax_effect = reallocate (syntax_effect, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_effect));
syntax_begin [syntax_count] = allocate ((string_length (begin) + 1) * (int) sizeof (* * syntax_begin));
syntax_end [syntax_count] = allocate ((string_length (end) + 1) * (int) sizeof (* * syntax_end));
syntax_mode [syntax_count] = index;
syntax_enrange [syntax_count] = enrange;
syntax_derange [syntax_count] = derange;
syntax_escape [syntax_count] = escape;
syntax_colour [syntax_count] = colour;
syntax_effect [syntax_count] = effect;
string_copy (syntax_begin [syntax_count], begin);
string_copy (syntax_end [syntax_count], end);
* index = syntax_count;
++syntax_count;
}
void syntax_select (char * string, int * index, int * length) {
int offset = 0;
int select = 0;
fatal_failure (string == NULL, "syntax_select: String is null.");
fatal_failure (index == NULL, "syntax_select: Index is null.");
fatal_failure (length == NULL, "syntax_select: Length is null.");
do {
if (syntax_enrange [select] == 0) {
@ -75,15 +80,6 @@ int syntax_output (char * string) {
}
}
}
/*if (string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) != 0) {
if (syntax_derange [select] == 0) {
break;
} else {
if (compare_character_array (string [string_length (syntax_begin [select])], syntax_end [select], string_length (syntax_end [select]))) {
break;
}
}
}*/
} else {
int subset = 0;
do {
@ -97,53 +93,38 @@ int syntax_output (char * string) {
selected:
if (select == syntax_count) {
format [2] = (char) TERMINAL_EFFECT_NORMAL + '0';
format [5] = (char) TERMINAL_COLOUR_WHITE + '0';
(void) write (STDOUT_FILENO, format, sizeof (format));
(void) write (STDOUT_FILENO, & string [offset], sizeof (string [offset]));
(void) write (STDOUT_FILENO, "\033[0m", sizeof ("\033[0m"));
return (1);
* index = 0; /* Should we handle this undefined behavior? */
* length = 1;
return;
} else {
* index = select;
}
format [2] = (char) syntax_effect [select] + '0';
format [5] = (char) syntax_colour [select] + '0';
(void) write (STDOUT_FILENO, format, sizeof (format));
do {
(void) write (STDOUT_FILENO, & string [offset], sizeof (string [offset]));
++offset;
if (string [offset] == syntax_escape [select]) {
(void) write (STDOUT_FILENO, & string [offset], sizeof (string [offset]));
++offset;
continue;
}
if (syntax_derange [select] == 0) {
if (string_compare_limit (& string [offset], syntax_end [select], string_length (syntax_end [select]))) {
(void) write (STDOUT_FILENO, syntax_end [select], string_length (syntax_end [select]));
(void) write (STDOUT_FILENO, "\033[0m", sizeof ("\033[0m"));
return (offset + (int) string_length (syntax_end [select]));
* length = offset + string_length (syntax_end [select]);
return;
}
} else {
int subset = 0;
if (strcmp (syntax_end [select], "") == 0) {
if (string_compare (syntax_end [select], "") == 0) {
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
goto finished;
* length = offset;
return;
}
} while (++subset != (int) string_length (syntax_end [select]));
}
} while (string [offset] != '\0');
finished:
(void) write (STDOUT_FILENO, "\033[0m", sizeof ("\033[0m"));
return (offset);
}
void syntax_delete (void) {

View File

@ -1,7 +1,7 @@
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
* Xyntax is deallocate software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
* Xyntax is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
* And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
* It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
*/
@ -22,8 +22,10 @@ extern char * syntax_escape;
extern int * syntax_colour;
extern int * syntax_effect;
extern void syntax_define (int, int, int, char *, char *, char, int, int);
extern int syntax_output (char *);
extern void syntax_define (int *, int, int, char *, char *, char, int, int);
extern void syntax_select (char *, int *, int *);
extern void syntax_delete (void);
#endif