xighlight/xighlight.c

374 lines
25 KiB
C

#include <xolatile/xtandard.c>
#include <xolatile/xyntax.c>
static void highlight_common (void) {
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\"@#$` \t\r\n";
syntax_define (false, false, "'", "'", '\\', colour_pink, effect_bold);
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);
}
static void highlight_c (void) {
char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
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"
};
int word;
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);
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
}
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);
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";
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"
};
int word;
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);
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
}
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);
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";
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"
};
int word;
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);
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
}
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);
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_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"
};
int word;
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);
for (word = 0; word != (int) (sizeof (instructions) / sizeof (instructions [0])); ++word) {
syntax_define (false, true, instructions [word], separators, '\0', colour_yellow, effect_bold);
}
for (word = 0; word != (int) (sizeof (registers) / sizeof (registers [0])); ++word) {
syntax_define (false, true, registers [word], separators, '\0', colour_cyan, effect_bold);
}
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);
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_flat_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"
};
char * keywords [] = {
"format", "executable", "readable", "writable", "segment", "sector", "entry", "macro",
"db", "dw", "dd", "dq", "rb", "rw", "rd", "rq"
};
int word;
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);
for (word = 0; word != (int) (sizeof (instructions) / sizeof (instructions [0])); ++word) {
syntax_define (false, true, instructions [word], separators, '\0', colour_yellow, effect_bold);
}
for (word = 0; word != (int) (sizeof (registers) / sizeof (registers [0])); ++word) {
syntax_define (false, true, registers [word], separators, '\0', colour_cyan, effect_bold);
}
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_italic);
}
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);
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_fortran (void) {
char * separators = ",:<=>+-*/&()[]\"\' \t\r\n";
char * keywords [] = {
"allocatable", "allocate", "associate", "backspace", "block", "call", "case", "common",
"contains", "cycle", "data", "deallocate", "d0", "do", "else", "elseif",
"end", "enddo", "endfile", "endif", "entry", "equivalence", "exit", "external",
"forall", "format", "function", "goto", "if", "implicit", "inquire", "intent",
"intrinsic", "module", "namelist", "none", "nullify", "only", "open", "optional",
"parameter", "pointer", "print", "private", "program", "public", "read", "recursive",
"return", "rewind", "save", "select", "sequence", "stop", "subroutine", "target",
"then", "to", "type", "use", "where", "write"
};
char * constants [] = {
".and.", ".or.", ".not.", ".true.", ".false.", "in", "out", "character",
"integer", "logical", "real", "complex", "dimension", "modulo", "len", "advance"
};
int word;
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);
for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
syntax_define (false, true, keywords [word], separators, '\0', colour_yellow, effect_bold);
}
for (word = 0; word != (int) (sizeof (constants) / sizeof (constants [0])); ++word) {
syntax_define (false, true, constants [word], separators, '\0', colour_yellow, effect_italic);
}
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);
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);
}
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], "-v") || string_compare (argv [1], "--version")) {
echo ("xighlight: Terminal syntax highlighter (version 144)\n");
exit (log_success);
} else if (string_compare (argv [1], "-l") || string_compare (argv [1], "--license")) {
echo ("xighlight: Terminal syntax highlighter (GNU/GPLv3)\n");
exit (log_success);
} else if (string_compare (argv [1], "-a") || string_compare (argv [1], "--author")) {
echo ("xighlight: Terminal syntax highlighter (Ognjen 'xolatile' Milan Robovic)\n");
exit (log_success);
} else if (string_compare (argv [1], "-h") || string_compare (argv [1], "--help")) {
echo ("xighlight: Terminal syntax highlighter:\n");
echo ("Example usage:\n");
echo ("\t$ cat file.ext | xighlight [flag] -- You need to pass language flag in this case.\n");
echo ("\t$ xighlight [flag] < file.ext -- You need to pass language flag in this case.\n");
echo ("\t$ xighlight file.ext -- Language is automatically detected in this case.\n");
echo ("Supported languages:\n");
echo ("\t -C --c -- C syntax\n");
echo ("\t -A --ada -- Ada syntax\n");
echo ("\t -P --cpp -- C++ syntax \n");
echo ("\t -S --assembly -- General assembly syntax\n");
echo ("\t -T --flat -- Flat assembly syntax\n");
echo ("\t -F --fortran -- Fortran syntax\n");
echo ("\t -H --shell -- Bourne shell syntax\n");
} else if (string_compare (argv [1], "-C") || string_compare (argv [1], "--c")) {
highlight_c ();
} else if (string_compare (argv [1], "-A") || string_compare (argv [1], "--ada")) {
highlight_ada ();
} else if (string_compare (argv [1], "-P") || string_compare (argv [1], "--cpp")) {
highlight_cpp ();
} else if (string_compare (argv [1], "-S") || string_compare (argv [1], "--assembly")) {
highlight_common_assembly ();
} else if (string_compare (argv [1], "-T") || string_compare (argv [1], "--flat")) {
highlight_flat_assembly ();
} else if (string_compare (argv [1], "-F") || string_compare (argv [1], "--fortran")) {
highlight_fortran ();
} else {
select = file_type (argv [1]);
buffer = file_import (argv [1]);
}
}
if (buffer == null) {
buffer = record ();
}
if (syntax_active == false) {
if ((select == file_type_c_source) || (select == file_type_c_header)) {
highlight_c ();
} else if ((select == file_type_ada_body) || (select == file_type_ada_specification)) {
highlight_ada ();
} else if ((select == file_type_cpp_source) || (select == file_type_cpp_header)) {
highlight_cpp ();
} else if (select == file_type_common_assembly) {
highlight_common_assembly ();
} else if (select == file_type_flat_assembly) {
highlight_flat_assembly ();
} else if (select == file_type_fortran_90) {
highlight_fortran ();
} else {
highlight_common ();
}
}
for (offset = 0; buffer [offset] != '\0'; offset += length) {
select = syntax_select (& buffer [offset], & length);
if (select >= syntax_count) {
terminal_colour (colour_white, effect_normal);
} else {
terminal_colour (syntax_colour [select], syntax_effect [select]);
}
out (& buffer [offset], length);
terminal_cancel ();
}
buffer = deallocate (buffer);
return (log_success);
}