New stuff...

This commit is contained in:
Ognjen Milan Robovic 2023-08-25 17:00:12 -04:00
parent 3e578583be
commit 883e8e546e
3 changed files with 172 additions and 137 deletions

View File

@ -3,5 +3,6 @@
set -xe
sudo cp xyntax.h /usr/include/xolatile/xyntax.h
sudo cp xyntax.c /usr/include/xolatile/xyntax.c
exit

167
xyntax.c Normal file
View File

@ -0,0 +1,167 @@
/*
* 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.
* 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.
*/
#ifndef XYNTAX_SOURCE
#define XYNTAX_SOURCE
#include <xolatile/xyntax.h>
int syntax_count = 0;
int * syntax_mode = NULL;
int * syntax_enrange = NULL;
int * syntax_derange = NULL;
char * * syntax_begin = NULL;
char * * syntax_end = NULL;
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;
do {
if (character == character_array [i]) {
return (1);
}
} while (++i != count);
return (0);
}
int syntax_output (char * string) {
int offset = 0;
int select = 0;
char format [8] = "\033[ ;3 m";
do {
if (syntax_enrange [select] == 0) {
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;
}
}
}
/*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 {
if (string [offset] == syntax_begin [select] [subset]) {
goto selected;
}
} while (++subset != (int) string_length (syntax_begin [select]));
}
} while (++select != syntax_count);
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);
}
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]));
}
} else {
int subset = 0;
if (strcmp (syntax_end [select], "") == 0) {
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
goto finished;
}
} 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) {
--syntax_count;
do {
syntax_begin [syntax_count] = deallocate (syntax_begin [syntax_count]);
syntax_end [syntax_count] = deallocate (syntax_end [syntax_count]);
} while (--syntax_count != -1);
syntax_mode = deallocate (syntax_mode);
syntax_enrange = deallocate (syntax_enrange);
syntax_derange = deallocate (syntax_derange);
syntax_begin = deallocate (syntax_begin);
syntax_end = deallocate (syntax_end);
syntax_escape = deallocate (syntax_escape);
syntax_colour = deallocate (syntax_colour);
syntax_effect = deallocate (syntax_effect);
}
#endif

141
xyntax.h
View File

@ -1,18 +1,16 @@
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
* Xyntax is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
* Xyntax is deallocate 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.
*/
#ifdef XYNTAX_DECLARATION
#undef XYNTAX_DECLARATION
#define XTANDARD_DECLARATION
#define XTANDARD_DEFINITION
#ifndef XYNTAX_HEADER
#define XYNTAX_HEADER
#include <xolatile/xtandard.h>
#include <xolatile/xtandard.c>
extern int syntax_count;
extern int * syntax_mode;
@ -29,134 +27,3 @@ extern int syntax_output (char *);
extern void syntax_delete (void);
#endif
#ifdef XYNTAX_DEFINITION
#undef XYNTAX_DEFINITION
int syntax_count = 0;
int * syntax_mode = NULL;
int * syntax_enrange = NULL;
int * syntax_derange = NULL;
char * * syntax_begin = NULL;
char * * syntax_end = NULL;
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 = realloc (syntax_mode, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_mode));
syntax_enrange = realloc (syntax_enrange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_enrange));
syntax_derange = realloc (syntax_derange, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_derange));
syntax_begin = realloc (syntax_begin, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_begin));
syntax_end = realloc (syntax_end, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_end));
syntax_escape = realloc (syntax_escape, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_escape));
syntax_colour = realloc (syntax_colour, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_colour));
syntax_effect = realloc (syntax_effect, (unsigned long int) (syntax_count + 1) * sizeof (* syntax_effect));
syntax_begin [syntax_count] = calloc (strlen (begin) + 1UL, sizeof (* * syntax_begin));
syntax_end [syntax_count] = calloc (strlen (end) + 1UL, 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;
}
int syntax_output (char * string) {
int offset = 0;
int select = 0;
char format [8] = "\033[ ;3 m";
do {
if (syntax_enrange [select] == 0) {
if (string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select]))) {
break;
}
} else {
int subset = 0;
do {
if (string [offset] == syntax_begin [select] [subset]) {
goto selected;
}
} while (++subset != (int) string_length (syntax_begin [select]));
}
} while (++select != syntax_count);
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);
}
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]));
}
} else {
int subset = 0;
if (strcmp (syntax_end [select], "") == 0) {
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
goto finished;
}
} 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) {
--syntax_count;
do {
free (syntax_begin [syntax_count]);
free (syntax_end [syntax_count]);
} while (--syntax_count != -1);
free (syntax_mode);
free (syntax_enrange);
free (syntax_derange);
free (syntax_begin);
free (syntax_end);
free (syntax_escape);
free (syntax_colour);
free (syntax_effect);
}
#endif