xighlight/xighlight.c

104 lines
3.8 KiB
C
Raw Normal View History

2023-08-28 09:03:47 -04:00
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
* Xighlight 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.
*/
2023-08-25 17:04:01 -04:00
#include <xolatile/xyntax.h>
#include <xolatile/xyntax.c>
static void highlight_c (void) {
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
2023-08-25 17:04:01 -04:00
char * keywords [32] = {
2023-08-28 09:03:47 -04:00
"register", "volatile", "auto", "const", "static", "extern", "if", "else",
"do", "while", "for", "continue", "switch", "case", "default", "break",
"enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
"char", "short", "int", "long", "signed", "unsigned", "float", "double"
2023-08-25 17:04:01 -04:00
};
syntax_define_separators (separators);
2023-08-25 17:04:01 -04:00
syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
syntax_define_range ("/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
syntax_define_range ("//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
syntax_define_range ("#", "\n", '\\', COLOUR_PINK, EFFECT_NORMAL);
syntax_define_range ("'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
syntax_define_operators (".,:;<=>+*-/%!&~^?|()[]{}", COLOUR_BLUE, EFFECT_BOLD);
syntax_define_words (keywords, 32, COLOUR_BLUE, EFFECT_NORMAL);
}
static void highlight_ada (void) {
char * separators = ".,:;<=>+-*/&|()\" \t\r\n";
char * keywords [73] = {
"abort", "else", "new", "return", "abs", "elsif", "not", "reverse",
"abstract", "end", "null", "accept", "entry", "select", "access", "of",
"separate", "aliased", "exit", "or", "some", "all", "others", "subtype",
"and", "for", "out", "array", "function", "at", "tagged", "generic",
"package", "task", "begin", "goto", "pragma", "body", "private", "then",
"type", "case", "in", "constant", "until", "is", "raise", "use",
"if", "declare", "range", "delay", "limited", "record", "when", "delta",
"loop", "rem", "while", "digits", "renames", "with", "do", "mod",
"requeue", "xor", "procedure", "protected", "interface",
"synchronized", "exception", "overriding", "terminate"
};
syntax_define_separators (separators);
2023-08-25 17:04:01 -04:00
syntax_define_default (COLOUR_RED, EFFECT_NORMAL, COLOUR_CYAN, EFFECT_BOLD);
syntax_define_range ("--", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
syntax_define_range ("'", "'", '\0', COLOUR_PINK, EFFECT_BOLD);
syntax_define_operators (".,:;<=>+-*/&|()'", COLOUR_BLUE, EFFECT_BOLD);
syntax_define_words (keywords, 73, COLOUR_BLUE, EFFECT_NORMAL);
}
int main (int argc, char * * argv) {
int offset = 0;
int select = 0;
int length = 0;
char * buffer = NULL;
if (argc != 1) {
if (string_compare (argv [1], "c") != 0) {
highlight_c ();
} else if (string_compare (argv [1], "ada") != 0) {
highlight_ada ();
} else {
highlight_c ();
}
} else {
highlight_c ();
}
2023-08-25 17:04:01 -04:00
2023-08-28 09:03:47 -04:00
buffer = record ();
2023-08-25 17:04:01 -04:00
2023-08-28 09:03:47 -04:00
for (offset = 0; buffer [offset] != '\0'; offset += length) {
2023-09-12 05:04:54 -04:00
select = syntax_select (& buffer [offset], & length);
2023-08-25 17:04:01 -04:00
2023-09-12 05:04:54 -04:00
if (select >= syntax_count) {
terminal_style (EFFECT_REVERSE, COLOUR_RED);
2023-08-28 09:03:47 -04:00
} else {
2023-09-12 05:23:59 -04:00
terminal_style (syntax_effect [select], syntax_colour [select]);
2023-08-28 09:03:47 -04:00
}
2023-08-25 17:04:01 -04:00
2023-08-28 09:03:47 -04:00
out (& buffer [offset], length);
2023-08-25 17:04:01 -04:00
2023-09-12 05:23:59 -04:00
terminal_style (-1, -1);
2023-08-28 09:03:47 -04:00
}
2023-08-25 17:04:01 -04:00
buffer = deallocate (buffer);
syntax_delete ();
return (EXIT_SUCCESS);
}