xighlight/xighlight.c

324 lines
21 KiB
C
Raw Normal View History

2023-10-02 16:27:35 -04:00
#include <xolatile/xtandard.c>
2023-08-25 17:04:01 -04:00
#include <xolatile/xyntax.c>
2023-09-17 16:17:16 -04:00
static void echo_version (void) {
echo ("xighlight: Terminal syntax highlighter (version 144000)\n");
}
static void echo_license (void) {
echo ("xighlight: Terminal syntax highlighter (GNU general public license version 3)\n");
}
2024-01-03 06:43:02 -05:00
static void highlight_common (void) {
2023-11-18 17:34:05 -05:00
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\"@#$` \t\r\n";
2023-09-17 16:17:16 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
syntax_define (true, false, "()[]{}", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ".,:;<=>+*-/%!&~^?|@#$`", "", '\0', colour_cyan, effect_normal);
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
2023-09-17 16:17:16 -04:00
}
static void highlight_c (void) {
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
2023-08-25 17:04:01 -04:00
2023-10-07 02:46:46 -04:00
char * keywords [] = {
"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
};
2023-11-18 17:34:05 -05:00
int word;
2023-08-25 17:04:01 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (false, false, "/*", "*/", '\0', colour_grey, effect_bold);
syntax_define (false, false, "//", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "#", "\n", '\\', colour_yellow, effect_italic);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
2023-11-18 17:34:05 -05:00
}
2023-10-07 02:46:46 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, false, "()[]{}", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ".,:;<=>+*-/%!&~^?|", "", '\0', colour_cyan, effect_normal);
2023-09-17 16:17:16 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
syntax_define (true, true, "abcdefghijklmnopqrstuvwxyz", separators, '\0', colour_white, effect_normal);
syntax_define (true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', colour_white, effect_bold);
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
}
static void highlight_ada (void) {
char * separators = ".,:;<=>+-*/&|()\" \t\r\n";
2023-10-07 02:46:46 -04:00
char * keywords [] = {
"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"
};
2023-11-18 17:34:05 -05:00
int word;
2024-01-03 06:43:02 -05:00
syntax_define (false, false, "--", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
2023-11-18 17:34:05 -05:00
}
2023-10-07 02:46:46 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, false, "()", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ".,:;<=>+-*/&|'", "", '\0', colour_cyan, effect_normal);
2023-09-17 16:17:16 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
syntax_define (true, true, "abcdefghijklmnopqrstuvwxyz", separators, '\0', colour_white, effect_normal);
syntax_define (true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', colour_white, effect_bold);
}
static void highlight_cpp (void) {
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
2023-10-07 02:46:46 -04:00
char * keywords [] = {
"alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept",
"auto", "bitand", "bitor", "bool", "break", "case", "catch", "char",
"char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", "consteval",
"constexpr", "constinit", "const_cast", "continue", "co_await", "co_return", "co_yield", "decltype",
"default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit",
"export", "extern", "false", "float", "for", "friend", "goto", "if",
"inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not",
"not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public",
"reflexpr", "register", "reinterpret_cast", "requires", "return", "short", "signed", "sizeof",
"static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this",
"thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union",
"unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor",
"xor_eq", "final", "override", "import", "module", "transaction_safe"
};
2023-11-18 17:34:05 -05:00
int word;
2024-01-03 06:43:02 -05:00
syntax_define (false, false, "/*", "*/", '\0', colour_grey, effect_bold);
syntax_define (false, false, "//", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "#", "\n", '\\', colour_yellow, effect_italic);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
2023-10-07 02:46:46 -04:00
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
2023-09-18 15:38:04 -04:00
}
2024-01-03 06:43:02 -05:00
syntax_define (true, false, "()[]{}", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ".,:;<=>+*-/%!&~^?|", "", '\0', colour_cyan, effect_normal);
2023-10-07 02:46:46 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
syntax_define (true, true, "abcdefghijklmnopqrstuvwxyz", separators, '\0', colour_white, effect_normal);
syntax_define (true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', colour_white, effect_bold);
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
2023-09-18 15:38:04 -04:00
}
2023-11-18 17:34:05 -05:00
2023-10-01 13:34:06 -04:00
static void highlight_valgrind (void) {
char * separators = "./-=?() \t\r\n";
2024-01-03 06:43:02 -05:00
syntax_define (false, false, "==", "==", '\0', colour_grey, effect_bold);
syntax_define (false, false, "???", "\n", '\0', colour_yellow, effect_bold);
2023-10-01 13:34:06 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
2023-10-07 02:46:46 -04:00
}
static void highlight_common_assembly (void) {
char * separators = ".,+-[]<> \t\r\n";
char * instructions [] = {
"mov", "movabs", "movapd", "movaps", "movebe", "movsd", "movsx", "movzx",
"movsxd", "movd", "movq", "movs", "movsb", "movsw", "movsd", "movsq",
"cmovmp", "cmovrcxz", "cmovc", "cmovnc", "cmove", "cmovne", "cmovz", "cmovnz",
"cmovg", "cmovng", "cmovge", "cmovnge", "cmovl", "cmovnl", "cmovle", "cmovnle",
"cmova", "cmovna", "cmovae", "cmovnae", "cmovb", "cmovnb", "cmovbe", "cmovnbe",
"cmovs", "cmovns", "cmovo", "cmovno", "cmovp", "cmovnp", "cmovpo", "cmovpe",
"cmp", "cmps", "cmpsb", "cmpsw", "cmpsd", "cmpsq", "cmpxchg", "lea",
"monitor", "cpuid", "in", "out", "syscall", "sysenter", "sysret", "sysexit",
"swap", "bswap", "pop", "push", "call", "ret", "enter", "leave",
"and", "or", "not", "neg", "sal", "sar", "shl", "shr",
"inc", "dec", "add", "sub", "mul", "div", "imul", "idiv",
"nop", "fnop", "adc", "sbb", "aaa", "aas", "aam", "aad",
"jmp", "jrcxz", "jc", "jnc", "je", "jne", "jz", "jnz",
"jg", "jng", "jge", "jnge", "jl", "jnl", "jle", "jnle",
"ja", "jna", "jae", "jnae", "jb", "jnb", "jbe", "jnbe",
"js", "jns", "jo", "jno", "jp", "jnp", "jpo", "jpe",
"rep", "repe", "repz", "repne", "repnz", "loop", "loope", "loopne"
};
char * registers [] = {
"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
"r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
"ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
"r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w",
"al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
"r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b",
"ah", "ch", "dh", "bh"
};
2023-11-18 17:34:05 -05:00
int word;
2023-10-07 02:46:46 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (false, false, ";", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
2023-10-07 02:46:46 -04:00
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (instructions) / sizeof (instructions [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, instructions [word], separators, '\0', colour_yellow, effect_bold);
2023-11-18 17:34:05 -05:00
}
2023-10-07 02:46:46 -04:00
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (registers) / sizeof (registers [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, registers [word], separators, '\0', colour_cyan, effect_bold);
2023-11-18 17:34:05 -05:00
}
2023-10-07 02:46:46 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, false, "()[]{}", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ".,+*-/%$<>", "", '\0', colour_cyan, effect_normal);
2023-10-01 13:34:06 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
syntax_define (true, true, "abcdefghijklmnopqrstuvwxyz", separators, '\0', colour_white, effect_normal);
syntax_define (true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', colour_white, effect_bold);
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
2023-10-01 13:34:06 -04:00
}
2023-10-07 03:07:55 -04:00
static void highlight_flat_assembly (void) {
2023-11-18 17:34:05 -05:00
char * separators = ".,+-=:;(){}[]%$<> \t\r\n";
2023-10-07 03:07:55 -04:00
char * instructions [] = {
"mov", "movabs", "movapd", "movaps", "movebe", "movsd", "movsx", "movzx",
"movsxd", "movd", "movq", "movs", "movsb", "movsw", "movsd", "movsq",
"cmovmp", "cmovrcxz", "cmovc", "cmovnc", "cmove", "cmovne", "cmovz", "cmovnz",
"cmovg", "cmovng", "cmovge", "cmovnge", "cmovl", "cmovnl", "cmovle", "cmovnle",
"cmova", "cmovna", "cmovae", "cmovnae", "cmovb", "cmovnb", "cmovbe", "cmovnbe",
"cmovs", "cmovns", "cmovo", "cmovno", "cmovp", "cmovnp", "cmovpo", "cmovpe",
"cmp", "cmps", "cmpsb", "cmpsw", "cmpsd", "cmpsq", "cmpxchg", "lea",
"monitor", "cpuid", "in", "out", "syscall", "sysenter", "sysret", "sysexit",
"swap", "bswap", "pop", "push", "call", "ret", "enter", "leave",
"and", "or", "not", "neg", "sal", "sar", "shl", "shr",
"inc", "dec", "add", "sub", "mul", "div", "imul", "idiv",
"nop", "fnop", "adc", "sbb", "aaa", "aas", "aam", "aad",
"jmp", "jrcxz", "jc", "jnc", "je", "jne", "jz", "jnz",
"jg", "jng", "jge", "jnge", "jl", "jnl", "jle", "jnle",
"ja", "jna", "jae", "jnae", "jb", "jnb", "jbe", "jnbe",
"js", "jns", "jo", "jno", "jp", "jnp", "jpo", "jpe",
"rep", "repe", "repz", "repne", "repnz", "loop", "loope", "loopne"
};
char * registers [] = {
"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
"r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
"r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
"ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
"r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w",
"al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
"r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b",
"ah", "ch", "dh", "bh"
};
char * keywords [] = {
"format", "executable", "readable", "writable", "segment", "sector", "entry", "macro",
"db", "dw", "dd", "dq", "rb", "rw", "rd", "rq"
};
2023-11-18 17:34:05 -05:00
int word;
2023-10-07 03:07:55 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (false, false, ";", "\n", '\0', colour_grey, effect_bold);
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
syntax_define (false, false, "\"", "\"", '\\', colour_pink, effect_normal);
2023-10-07 03:07:55 -04:00
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (instructions) / sizeof (instructions [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, instructions [word], separators, '\0', colour_yellow, effect_bold);
2023-11-18 17:34:05 -05:00
}
2023-10-07 03:07:55 -04:00
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (registers) / sizeof (registers [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, registers [word], separators, '\0', colour_cyan, effect_bold);
2023-11-18 17:34:05 -05:00
}
2023-10-07 03:07:55 -04:00
2023-11-18 17:34:05 -05:00
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
2024-01-03 06:43:02 -05:00
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_italic);
2023-11-18 17:34:05 -05:00
}
2023-10-07 03:07:55 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, false, "()[]{}", "", '\0', colour_blue, effect_normal);
syntax_define (true, false, ".,+-=:;%$<>", "", '\0', colour_cyan, effect_normal);
2023-10-07 03:07:55 -04:00
2024-01-03 06:43:02 -05:00
syntax_define (true, true, "0123456789", separators, '\0', colour_pink, effect_bold);
syntax_define (true, true, "abcdefghijklmnopqrstuvwxyz", separators, '\0', colour_white, effect_normal);
syntax_define (true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', colour_white, effect_bold);
syntax_define (true, true, "_", separators, '\0', colour_white, effect_italic);
2023-10-07 03:07:55 -04:00
}
int main (int argc, char * * argv) {
int offset = 0;
int select = 0;
int length = 0;
2024-01-03 06:43:02 -05:00
char * buffer = null;
2023-10-07 03:07:55 -04:00
argument_define ("-v", "--version", echo_version);
argument_define ("-l", "--license", echo_license);
argument_define ("-c", "--c", highlight_c);
argument_define ("-a", "--ada", highlight_ada);
argument_define ("-C", "--c++", highlight_cpp);
argument_define ("-V", "--valgrind", highlight_valgrind);
argument_define ("-A", "--assembly", highlight_common_assembly);
argument_define ("-F", "--flat-assembly", highlight_flat_assembly);
2023-09-17 16:17:16 -04:00
if (argc != 1) {
argument_select (argc, argv);
}
2024-01-03 06:43:02 -05:00
if (buffer == null) {
if (argument_input == null) {
2023-09-17 16:17:16 -04:00
buffer = record ();
} else {
select = file_type (argument_input);
buffer = file_import (argument_input);
}
}
2024-01-03 06:43:02 -05:00
if (syntax_active == false) {
if ((select == file_type_c_source) || (select == file_type_c_header)) {
highlight_c ();
2024-01-03 06:43:02 -05:00
} else if ((select == file_type_ada_body) || (select == file_type_ada_specification)) {
highlight_ada ();
2024-01-03 06:43:02 -05:00
} else if ((select == file_type_cpp_source) || (select == file_type_cpp_header)) {
highlight_cpp ();
2024-01-03 06:43:02 -05:00
} else if (select == file_type_common_assembly) {
2023-10-07 02:46:46 -04:00
highlight_common_assembly ();
2024-01-03 06:43:02 -05:00
} else if (select == file_type_flat_assembly) {
2023-10-07 03:07:55 -04:00
highlight_flat_assembly ();
} else {
2023-09-17 16:17:16 -04:00
highlight_common ();
}
}
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) {
2024-01-03 06:43:02 -05:00
terminal_colour (colour_white, effect_normal);
2023-08-28 09:03:47 -04:00
} else {
2023-09-20 06:29:11 -04:00
terminal_colour (syntax_colour [select], syntax_effect [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-20 06:29:11 -04:00
terminal_cancel ();
2023-08-28 09:03:47 -04:00
}
2023-08-25 17:04:01 -04:00
buffer = deallocate (buffer);
2023-09-17 16:17:16 -04:00
argument_delete ();
2024-01-03 06:43:02 -05:00
return (0);
2023-08-25 17:04:01 -04:00
}