For the love...
Of everything good...
This commit is contained in:
parent
b3f1bc6f77
commit
c53055c9b7
BIN
screenshot.png
Normal file
BIN
screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
188
xanguage.h
Normal file
188
xanguage.h
Normal file
@ -0,0 +1,188 @@
|
||||
/// __ ____ _ _ __ __ _ _ _ __ _ __ _ ___
|
||||
/// \ \/ / _` | '_ \ / _` | | | |/ _` |/ _` |/ _ \
|
||||
/// > < (_| | | | | (_| | |_| | (_| | (_| | __/
|
||||
/// /_/\_\__,_|_| |_|\__, |\__,_|\__,_|\__, |\___|
|
||||
/// |___/ |___/
|
||||
///
|
||||
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
|
||||
///
|
||||
/// xolatile@chud.cyou - xanguage - Syntax definitions of programming languages that I care about (and some that I don't care about).
|
||||
///
|
||||
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
|
||||
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
|
||||
///
|
||||
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
|
||||
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
|
||||
/// for more details, if you dare, it is a lot of text that nobody wants to read...
|
||||
|
||||
typedef enum {
|
||||
language_common, language_ada, language_c, language_cpp,
|
||||
language_d, language_eaxhla, language_flat, language_fortran,
|
||||
language_pascal, language_python, language_go, language_lua,
|
||||
language_bash, language_haskell, language_valgrind, language_holy_c,
|
||||
language_count
|
||||
} language_enumeration;
|
||||
|
||||
typedef struct {
|
||||
natural comment_colour;
|
||||
natural processor_colour;
|
||||
natural character_colour;
|
||||
natural string_colour;
|
||||
natural keyword_colour;
|
||||
natural type_colour;
|
||||
natural bracket_colour;
|
||||
natural operator_colour;
|
||||
natural number_colour;
|
||||
natural lowercase_colour;
|
||||
natural uppercase_colour;
|
||||
natural underscore_colour;
|
||||
natural register_colour;
|
||||
natural extension_colour;
|
||||
natural fatal_colour;
|
||||
natural comment_effect;
|
||||
natural processor_effect;
|
||||
natural character_effect;
|
||||
natural string_effect;
|
||||
natural keyword_effect;
|
||||
natural type_effect;
|
||||
natural bracket_effect;
|
||||
natural operator_effect;
|
||||
natural number_effect;
|
||||
natural lowercase_effect;
|
||||
natural uppercase_effect;
|
||||
natural underscore_effect;
|
||||
natural register_effect;
|
||||
natural extension_effect;
|
||||
natural fatal_effect;
|
||||
} language_structure;
|
||||
|
||||
#define language_lowercase "abcdefghijklmnopqrstuvwxyz"
|
||||
#define language_uppercase "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
#define language_letters "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
#define language_digits "0123456789"
|
||||
|
||||
#include "xanguage/common.h"
|
||||
#include "xanguage/ada.h"
|
||||
#include "xanguage/c.h"
|
||||
#include "xanguage/c++.h"
|
||||
#include "xanguage/d.h"
|
||||
#include "xanguage/eaxhla.h"
|
||||
#include "xanguage/flat.h"
|
||||
#include "xanguage/fortran.h"
|
||||
#include "xanguage/pascal.h"
|
||||
#include "xanguage/python.h"
|
||||
#include "xanguage/go.h"
|
||||
#include "xanguage/holy_c.h"
|
||||
#include "xanguage/lua.h"
|
||||
#include "xanguage/bash.h"
|
||||
#include "xanguage/haskell.h"
|
||||
#include "xanguage/valgrind.h"
|
||||
|
||||
#undef language_lowercase
|
||||
#undef language_uppercase
|
||||
#undef language_letters
|
||||
#undef language_digits
|
||||
|
||||
static procedure (* language_highlighter [language_count]) (language_structure * language, syntax_structure * syntax) = {
|
||||
language_highlight_common, language_highlight_ada, language_highlight_c, language_highlight_cpp,
|
||||
language_highlight_d, language_highlight_eaxhla, language_highlight_flat, language_highlight_fortran,
|
||||
language_highlight_pascal, language_highlight_python, language_highlight_go, language_highlight_lua,
|
||||
language_highlight_bash, language_highlight_haskell, language_highlight_valgrind, language_highlight_holy_c
|
||||
};
|
||||
|
||||
static character * language_short_option [language_count] = {
|
||||
"-X", "-A", "-C", "-S", "-D", "-E", "-T", "-F", "-P", "-Y", "-G", "-L", "-B", "-H", "-V", "-O"
|
||||
};
|
||||
|
||||
static character * language_long_option [language_count] = {
|
||||
"--common", "--ada", "--c", "--cpp", "--d", "--eaxhla", "--flat", "--fortran",
|
||||
"--pascal", "--python", "--go", "--lua", "--bash", "--haskell", "--valgrind", "--holyc"
|
||||
};
|
||||
|
||||
static character * language_identifier [language_count] = {
|
||||
"Common", "Ada", "C", "C++", "D", "EAXHLA", "Flat", "Fortran",
|
||||
"Pascal", "Python", "Go", "Lua", "Bash", "Haskell", "Valgrind", "Holy C"
|
||||
};
|
||||
|
||||
static language_structure * language_initialize (boolean true_colour) {
|
||||
language_structure * language = allocate (sizeof (* language));
|
||||
|
||||
if (true_colour == true) {
|
||||
language->comment_colour = 0xff777777u;
|
||||
language->processor_colour = 0xff3377aau;
|
||||
language->character_colour = 0xff7733ccu;
|
||||
language->string_colour = 0xffcc3377u;
|
||||
language->keyword_colour = 0xff33cceeu;
|
||||
language->type_colour = 0xff55cceeu;
|
||||
language->bracket_colour = 0xffee5533u;
|
||||
language->operator_colour = 0xffeeaa33u;
|
||||
language->number_colour = 0xffee33aau;
|
||||
language->lowercase_colour = 0xffccccccu;
|
||||
language->uppercase_colour = 0xffeeeeeeu;
|
||||
language->underscore_colour = 0xffaaaaaau;
|
||||
language->register_colour = 0xff5577aau;
|
||||
language->extension_colour = 0xff55aaccu;
|
||||
language->fatal_colour = 0xffcc7755u;
|
||||
} else {
|
||||
language->comment_colour = colour_grey;
|
||||
language->processor_colour = colour_cyan;
|
||||
language->character_colour = colour_pink;
|
||||
language->string_colour = colour_pink;
|
||||
language->keyword_colour = colour_yellow;
|
||||
language->type_colour = colour_yellow;
|
||||
language->bracket_colour = colour_blue;
|
||||
language->operator_colour = colour_cyan;
|
||||
language->number_colour = colour_pink;
|
||||
language->lowercase_colour = colour_white;
|
||||
language->uppercase_colour = colour_white;
|
||||
language->underscore_colour = colour_white;
|
||||
language->register_colour = colour_cyan;
|
||||
language->extension_colour = colour_yellow;
|
||||
language->fatal_colour = colour_red;
|
||||
language->comment_effect = effect_bold;
|
||||
language->processor_effect = effect_italic;
|
||||
language->character_effect = effect_bold;
|
||||
language->string_effect = effect_normal;
|
||||
language->keyword_effect = effect_bold;
|
||||
language->type_effect = effect_normal;
|
||||
language->bracket_effect = effect_bold;
|
||||
language->operator_effect = effect_normal;
|
||||
language->number_effect = effect_bold;
|
||||
language->lowercase_effect = effect_normal;
|
||||
language->uppercase_effect = effect_bold;
|
||||
language->underscore_effect = effect_italic;
|
||||
language->register_effect = effect_italic;
|
||||
language->extension_effect = effect_italic;
|
||||
language->fatal_effect = effect_bold;
|
||||
}
|
||||
|
||||
return (language);
|
||||
}
|
||||
|
||||
static language_structure * language_deinitialize (language_structure * language) {
|
||||
return (deallocate (language));
|
||||
}
|
||||
|
||||
static procedure language_conditionally_select (language_structure * language, syntax_structure * syntax, natural select) {
|
||||
if (syntax->count == 0) {
|
||||
if ((select == file_type_c_source) || (select == file_type_c_header)) {
|
||||
language_highlight_c (language, syntax);
|
||||
} else if ((select == file_type_ada_sexy_body) || (select == file_type_ada_specification)) {
|
||||
language_highlight_ada (language, syntax);
|
||||
} else if ((select == file_type_cpp_source) || (select == file_type_cpp_header)) {
|
||||
language_highlight_cpp (language, syntax);
|
||||
} else if (select == file_type_flat_assembly) {
|
||||
language_highlight_flat (language, syntax);
|
||||
} else if (select == file_type_fortran_90_source) {
|
||||
language_highlight_fortran (language, syntax);
|
||||
} else if (select == file_type_pascal_source) {
|
||||
language_highlight_pascal (language, syntax);
|
||||
} else if (select == file_type_eax_assembly) {
|
||||
language_highlight_eaxhla (language, syntax);
|
||||
} else if (select == file_type_python_script) {
|
||||
language_highlight_python (language, syntax);
|
||||
} else {
|
||||
language_highlight_common (language, syntax);
|
||||
}
|
||||
}
|
||||
}
|
123
xighlight.c
Normal file
123
xighlight.c
Normal file
@ -0,0 +1,123 @@
|
||||
/// _ _ _ _ _ _
|
||||
/// __ _(_) __ _| |__ | (_) __ _| |__ | |_
|
||||
/// \ \/ / |/ _` | '_ \| | |/ _` | '_ \| __|
|
||||
/// > <| | (_| | | | | | | (_| | | | | |_
|
||||
/// /_/\_\_|\__, |_| |_|_|_|\__, |_| |_|\__|
|
||||
/// |___/ |___/
|
||||
///
|
||||
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
|
||||
///
|
||||
/// xolatile@chud.cyou - xighlight - Generic source code terminal highlighter using VT100 escape sequences, very very slow...
|
||||
///
|
||||
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
|
||||
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
|
||||
///
|
||||
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
|
||||
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
|
||||
/// for more details, if you dare, it is a lot of text that nobody wants to read...
|
||||
|
||||
#include "xtandard.h"
|
||||
#include "xyntax.h"
|
||||
#include "xanguage.h"
|
||||
|
||||
static procedure conditionally_exit (language_structure * language, syntax_structure * syntax, boolean terminate) {
|
||||
syntax = syntax_deinitialize (syntax);
|
||||
language = language_deinitialize (language);
|
||||
|
||||
if (terminate == true) {
|
||||
exit (log_success);
|
||||
}
|
||||
}
|
||||
|
||||
static procedure print_common (none) {
|
||||
print ("/B/4xighlight/-: /4Terminal syntax highlighter/-\n\n");
|
||||
print ("\tAuthor: /4Ognjen 'xolatile' Milan Robovic/-\n");
|
||||
print ("\tLicense: /4GNU//GPLv3/-\n\n");
|
||||
}
|
||||
|
||||
static procedure print_help (none) {
|
||||
print_common ();
|
||||
|
||||
print ("Example usage:\n\n");
|
||||
print ("\t/6$ cat file.ext | xighlight [flags]/- /0---/- You need to pass language flag in this case.\n");
|
||||
print ("\t/6$ xighlight [flags] < file.ext/- /0---/- You need to pass language flag in this case.\n");
|
||||
print ("\t/6$ xighlight file.ext/- /0---/- Language is automatically detected in this case.\n\n");
|
||||
print ("Supported languages:\n\n");
|
||||
|
||||
for (language_enumeration index = 0; index < language_count; ++index) {
|
||||
character align [32] = "";
|
||||
|
||||
print ("\t/B/4%s/- /4%s/- /0---/- %s syntax highlighting\n",
|
||||
language_short_option [index],
|
||||
string_align_left (string_copy (align, language_long_option [index]), 9, ' '),
|
||||
language_identifier [index]);
|
||||
}
|
||||
}
|
||||
|
||||
static procedure print_version (none) {
|
||||
print_common ();
|
||||
|
||||
print ("\tVersion: /40 (Zero)/-\n");
|
||||
}
|
||||
|
||||
integer main (integer argc, character * * argv) {
|
||||
natural select = language_count;
|
||||
natural length = 0;
|
||||
character * buffer = null;
|
||||
|
||||
syntax_structure * syntax = syntax_initialize (360);
|
||||
language_structure * language = language_initialize (false);
|
||||
|
||||
for (integer argument = 1; argument < argc; ++argument) {
|
||||
if (argument_compare (argv [argument], "-h", "--help") == true) {
|
||||
print_help ();
|
||||
conditionally_exit (language, syntax, true);
|
||||
} else if (argument_compare (argv [argument], "-v", "--version") == true) {
|
||||
print_version ();
|
||||
conditionally_exit (language, syntax, true);
|
||||
}
|
||||
|
||||
for (natural index = 0; index < language_count; ++index) {
|
||||
if (argument_compare (argv [argument], language_short_option [index], language_long_option [index]) == true) {
|
||||
(* (language_highlighter [index])) (language, syntax);
|
||||
select = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (file_exists (argv [argument]) == true) {
|
||||
if (select == language_count) {
|
||||
select = (natural) file_type (argv [argument]);
|
||||
}
|
||||
if (buffer == null) {
|
||||
buffer = file_import (argv [argument]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (buffer == null) {
|
||||
buffer = record ();
|
||||
}
|
||||
|
||||
language_conditionally_select (language, syntax, select);
|
||||
|
||||
for (natural offset = 0; buffer [offset] != '\0'; offset += length) {
|
||||
select = syntax_select (syntax, & buffer [offset], & length);
|
||||
|
||||
if (select >= syntax->count) {
|
||||
echo_colour (colour_white, effect_normal);
|
||||
} else {
|
||||
echo_colour (syntax->colour [select], syntax->effect [select]);
|
||||
}
|
||||
|
||||
output (& buffer [offset], length);
|
||||
|
||||
echo_cancel ();
|
||||
}
|
||||
|
||||
conditionally_exit (language, syntax, false);
|
||||
|
||||
buffer = deallocate (buffer);
|
||||
|
||||
return (log_success);
|
||||
}
|
1238
xtandard.h
Normal file
1238
xtandard.h
Normal file
File diff suppressed because it is too large
Load Diff
175
xyntax.h
Normal file
175
xyntax.h
Normal file
@ -0,0 +1,175 @@
|
||||
/// _
|
||||
/// __ ___ _ _ __ | |_ __ ___ __
|
||||
/// \ \/ / | | | '_ \| __/ _` \ \/ /
|
||||
/// > <| |_| | | | | || (_| |> <
|
||||
/// /_/\_\\__, |_| |_|\__\__,_/_/\_\
|
||||
/// |___/
|
||||
///
|
||||
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
|
||||
///
|
||||
/// xolatile@chud.cyou - xyntax - Tiny, unsafe and somewhat insane unity header for generic syntax definition.
|
||||
///
|
||||
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
|
||||
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
|
||||
///
|
||||
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
|
||||
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
|
||||
/// for more details, if you dare, it is a lot of text that nobody wants to read...
|
||||
|
||||
typedef struct {
|
||||
natural count;
|
||||
natural limit;
|
||||
boolean * enrange;
|
||||
boolean * derange;
|
||||
character * * begin;
|
||||
character * * end;
|
||||
character * escape;
|
||||
natural * colour;
|
||||
natural * effect;
|
||||
} syntax_structure;
|
||||
|
||||
static syntax_structure * syntax_initialize (natural limit) {
|
||||
syntax_structure * syntax = allocate (sizeof (* syntax));
|
||||
|
||||
syntax->limit = limit;
|
||||
|
||||
if (limit != 0) {
|
||||
syntax->enrange = allocate (syntax->limit * sizeof (* syntax->enrange));
|
||||
syntax->derange = allocate (syntax->limit * sizeof (* syntax->derange));
|
||||
syntax->begin = allocate (syntax->limit * sizeof (* syntax->begin));
|
||||
syntax->end = allocate (syntax->limit * sizeof (* syntax->end));
|
||||
syntax->escape = allocate (syntax->limit * sizeof (* syntax->escape));
|
||||
syntax->colour = allocate (syntax->limit * sizeof (* syntax->colour));
|
||||
syntax->effect = allocate (syntax->limit * sizeof (* syntax->effect));
|
||||
}
|
||||
|
||||
return (syntax);
|
||||
}
|
||||
|
||||
static syntax_structure * syntax_deinitialize (syntax_structure * syntax) {
|
||||
for (natural index = 0; index < syntax->count; ++index) {
|
||||
syntax->begin [index] = deallocate (syntax->begin [index]);
|
||||
syntax->end [index] = deallocate (syntax->end [index]);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
return (deallocate (syntax));
|
||||
}
|
||||
|
||||
static natural syntax_define (syntax_structure * syntax, boolean enrange, boolean derange, character * begin, character * end, character escape,
|
||||
natural colour, natural effect) {
|
||||
++syntax->count;
|
||||
|
||||
natural current = syntax->count - 1;
|
||||
|
||||
fatal_failure (begin == null, "syntax_define: Begin string is null pointer.");
|
||||
fatal_failure (end == null, "syntax_define: End string is null pointer.");
|
||||
|
||||
fatal_failure (syntax->count >= syntax->limit, "syntax_define: Reached the hardcoded limit.");
|
||||
|
||||
if (syntax->limit == 0) {
|
||||
syntax->enrange = reallocate (syntax->enrange, syntax->count * sizeof (* syntax->enrange));
|
||||
syntax->derange = reallocate (syntax->derange, syntax->count * sizeof (* syntax->derange));
|
||||
syntax->begin = reallocate (syntax->begin, syntax->count * sizeof (* syntax->begin));
|
||||
syntax->end = reallocate (syntax->end, syntax->count * sizeof (* syntax->end));
|
||||
syntax->escape = reallocate (syntax->escape, syntax->count * sizeof (* syntax->escape));
|
||||
syntax->colour = reallocate (syntax->colour, syntax->count * sizeof (* syntax->colour));
|
||||
syntax->effect = reallocate (syntax->effect, syntax->count * sizeof (* syntax->effect));
|
||||
}
|
||||
|
||||
syntax->begin [current] = allocate ((string_length (begin) + 1) * sizeof (* * syntax->begin));
|
||||
syntax->end [current] = allocate ((string_length (end) + 1) * sizeof (* * syntax->end));
|
||||
|
||||
syntax->enrange [current] = enrange;
|
||||
syntax->derange [current] = derange;
|
||||
syntax->escape [current] = escape;
|
||||
syntax->colour [current] = colour;
|
||||
syntax->effect [current] = effect;
|
||||
|
||||
string_copy (syntax->begin [current], begin);
|
||||
string_copy (syntax->end [current], end);
|
||||
|
||||
return (current);
|
||||
}
|
||||
|
||||
static natural syntax_select (syntax_structure * syntax, character * string, natural * length) {
|
||||
natural offset = 0;
|
||||
natural subset = 0;
|
||||
natural select = 0;
|
||||
|
||||
natural_64 begin_length = 0;
|
||||
natural_64 end_length = 0;
|
||||
|
||||
for (; select != syntax->count; ++select) {
|
||||
begin_length = string_length (syntax->begin [select]);
|
||||
|
||||
if (! syntax->enrange [select]) {
|
||||
if (! syntax->derange [select]) {
|
||||
if (string_compare_limit (string, syntax->begin [select], begin_length)) {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if ((string_compare_limit (string, syntax->begin [select], begin_length))
|
||||
&& (character_compare_array (string [offset + begin_length], syntax->end [select]))) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (subset = 0; subset != begin_length; ++subset) {
|
||||
if (string [offset] == syntax->begin [select] [subset]) {
|
||||
goto selected;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
selected:
|
||||
|
||||
if (select >= syntax->count) {
|
||||
* length = 1;
|
||||
|
||||
return (syntax->count);
|
||||
}
|
||||
|
||||
end_length = string_length (syntax->end [select]);
|
||||
|
||||
for (offset = 1; string [offset - 1] != character_null; ++offset) {
|
||||
if (string [offset] == syntax->escape [select]) {
|
||||
++offset;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (syntax->derange [select]) {
|
||||
subset = 0;
|
||||
if (end_length == 0) {
|
||||
break;
|
||||
} do {
|
||||
if (string [offset] == syntax->end [select] [subset]) {
|
||||
* length = offset;
|
||||
goto finished;
|
||||
}
|
||||
} while (++subset != end_length);
|
||||
} else {
|
||||
if (end_length != 0) {
|
||||
if (string_compare_limit (& string [offset], syntax->end [select], end_length)) {
|
||||
* length = offset + end_length;
|
||||
return (select);
|
||||
}
|
||||
} else {
|
||||
* length = 1;
|
||||
return (select);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
finished:
|
||||
|
||||
return (select);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user