Xighlight C source code...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
22KB

  1. /*
  2. Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. Xighlight is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  4. 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.
  5. 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.
  6. */
  7. #include <xolatile/xtandard.c>
  8. #include <xolatile/xyntax.c>
  9. static void echo_version (void) {
  10. echo ("xighlight: Terminal syntax highlighter (version 144000)\n");
  11. }
  12. static void echo_license (void) {
  13. echo ("xighlight: Terminal syntax highlighter (GNU general public license version 3)\n");
  14. }
  15. static void highlight_common (void) { /* Should be changed to support basic GCC, Clang, Valgrind and Splint output... */
  16. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\"@#$` \t\r\n";
  17. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  18. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  19. syntax_define (TRUE, FALSE, ".,:;<=>+*-/%!&~^?|@#$`", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  20. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  21. }
  22. static void highlight_c (void) {
  23. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  24. char * keywords [] = {
  25. "register", "volatile", "auto", "const", "static", "extern", "if", "else",
  26. "do", "while", "for", "continue", "switch", "case", "default", "break",
  27. "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
  28. "char", "short", "int", "long", "signed", "unsigned", "float", "double"
  29. };
  30. int word;
  31. syntax_define (FALSE, FALSE, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  32. syntax_define (FALSE, FALSE, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  33. syntax_define (FALSE, FALSE, "#", "\n", '\\', COLOUR_YELLOW, EFFECT_ITALIC);
  34. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  35. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  36. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  37. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  38. }
  39. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  40. syntax_define (TRUE, FALSE, ".,:;<=>+*-/%!&~^?|", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  41. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  42. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  43. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  44. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  45. }
  46. static void highlight_ada (void) {
  47. char * separators = ".,:;<=>+-*/&|()\" \t\r\n";
  48. char * keywords [] = {
  49. "abort", "else", "new", "return", "abs", "elsif", "not", "reverse",
  50. "abstract", "end", "null", "accept", "entry", "select", "access", "of",
  51. "separate", "aliased", "exit", "or", "some", "all", "others", "subtype",
  52. "and", "for", "out", "array", "function", "at", "tagged", "generic",
  53. "package", "task", "begin", "goto", "pragma", "body", "private", "then",
  54. "type", "case", "in", "constant", "until", "is", "raise", "use",
  55. "if", "declare", "range", "delay", "limited", "record", "when", "delta",
  56. "loop", "rem", "while", "digits", "renames", "with", "do", "mod",
  57. "requeue", "xor", "procedure", "protected", "interface", "synchronized", "exception", "overriding",
  58. "terminate"
  59. };
  60. int word;
  61. syntax_define (FALSE, FALSE, "--", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  62. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  63. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  64. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  65. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  66. }
  67. syntax_define (TRUE, FALSE, "()", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  68. syntax_define (TRUE, FALSE, ".,:;<=>+-*/&|'", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  69. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  70. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  71. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  72. }
  73. static void highlight_cpp (void) {
  74. char * separators = ".,:;<=>+-*/%!&~^?|()[]{}'\" \t\r\n";
  75. char * keywords [] = {
  76. "alignas", "alignof", "and", "and_eq", "asm", "atomic_cancel", "atomic_commit", "atomic_noexcept",
  77. "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char",
  78. "char8_t", "char16_t", "char32_t", "class", "compl", "concept", "const", "consteval",
  79. "constexpr", "constinit", "const_cast", "continue", "co_await", "co_return", "co_yield", "decltype",
  80. "default", "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit",
  81. "export", "extern", "false", "float", "for", "friend", "goto", "if",
  82. "inline", "int", "long", "mutable", "namespace", "new", "noexcept", "not",
  83. "not_eq", "nullptr", "operator", "or", "or_eq", "private", "protected", "public",
  84. "reflexpr", "register", "reinterpret_cast", "requires", "return", "short", "signed", "sizeof",
  85. "static", "static_assert", "static_cast", "struct", "switch", "synchronized", "template", "this",
  86. "thread_local", "throw", "true", "try", "typedef", "typeid", "typename", "union",
  87. "unsigned", "using", "virtual", "void", "volatile", "wchar_t", "while", "xor",
  88. "xor_eq", "final", "override", "import", "module", "transaction_safe"
  89. };
  90. int word;
  91. syntax_define (FALSE, FALSE, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
  92. syntax_define (FALSE, FALSE, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  93. syntax_define (FALSE, FALSE, "#", "\n", '\\', COLOUR_YELLOW, EFFECT_ITALIC);
  94. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  95. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  96. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  97. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  98. }
  99. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  100. syntax_define (TRUE, FALSE, ".,:;<=>+*-/%!&~^?|", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  101. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  102. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  103. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  104. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  105. }
  106. static void highlight_valgrind (void) {
  107. char * separators = "./-=?() \t\r\n";
  108. syntax_define (FALSE, FALSE, "==", "==", '\0', COLOUR_GREY, EFFECT_BOLD);
  109. syntax_define (FALSE, FALSE, "???", "\n", '\0', COLOUR_YELLOW, EFFECT_BOLD);
  110. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  111. }
  112. static void highlight_common_assembly (void) {
  113. char * separators = ".,+-[]<> \t\r\n";
  114. char * instructions [] = {
  115. "mov", "movabs", "movapd", "movaps", "movebe", "movsd", "movsx", "movzx",
  116. "movsxd", "movd", "movq", "movs", "movsb", "movsw", "movsd", "movsq",
  117. "cmovmp", "cmovrcxz", "cmovc", "cmovnc", "cmove", "cmovne", "cmovz", "cmovnz",
  118. "cmovg", "cmovng", "cmovge", "cmovnge", "cmovl", "cmovnl", "cmovle", "cmovnle",
  119. "cmova", "cmovna", "cmovae", "cmovnae", "cmovb", "cmovnb", "cmovbe", "cmovnbe",
  120. "cmovs", "cmovns", "cmovo", "cmovno", "cmovp", "cmovnp", "cmovpo", "cmovpe",
  121. "cmp", "cmps", "cmpsb", "cmpsw", "cmpsd", "cmpsq", "cmpxchg", "lea",
  122. "monitor", "cpuid", "in", "out", "syscall", "sysenter", "sysret", "sysexit",
  123. "swap", "bswap", "pop", "push", "call", "ret", "enter", "leave",
  124. "and", "or", "not", "neg", "sal", "sar", "shl", "shr",
  125. "inc", "dec", "add", "sub", "mul", "div", "imul", "idiv",
  126. "nop", "fnop", "adc", "sbb", "aaa", "aas", "aam", "aad",
  127. "jmp", "jrcxz", "jc", "jnc", "je", "jne", "jz", "jnz",
  128. "jg", "jng", "jge", "jnge", "jl", "jnl", "jle", "jnle",
  129. "ja", "jna", "jae", "jnae", "jb", "jnb", "jbe", "jnbe",
  130. "js", "jns", "jo", "jno", "jp", "jnp", "jpo", "jpe",
  131. "rep", "repe", "repz", "repne", "repnz", "loop", "loope", "loopne"
  132. };
  133. char * registers [] = {
  134. "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  135. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  136. "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
  137. "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
  138. "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
  139. "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w",
  140. "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
  141. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b",
  142. "ah", "ch", "dh", "bh"
  143. };
  144. int word;
  145. syntax_define (FALSE, FALSE, ";", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  146. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  147. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  148. for (word = 0; word != (int) (sizeof (instructions) / sizeof (instructions [0])); ++word) {
  149. syntax_define (FALSE, TRUE, instructions [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  150. }
  151. for (word = 0; word != (int) (sizeof (registers) / sizeof (registers [0])); ++word) {
  152. syntax_define (FALSE, TRUE, registers [word], separators, '\0', COLOUR_CYAN, EFFECT_BOLD);
  153. }
  154. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  155. syntax_define (TRUE, FALSE, ".,+*-/%$<>", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  156. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  157. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  158. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  159. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  160. }
  161. static void highlight_flat_assembly (void) {
  162. char * separators = ".,+-=:;(){}[]%$<> \t\r\n";
  163. char * instructions [] = {
  164. "mov", "movabs", "movapd", "movaps", "movebe", "movsd", "movsx", "movzx",
  165. "movsxd", "movd", "movq", "movs", "movsb", "movsw", "movsd", "movsq",
  166. "cmovmp", "cmovrcxz", "cmovc", "cmovnc", "cmove", "cmovne", "cmovz", "cmovnz",
  167. "cmovg", "cmovng", "cmovge", "cmovnge", "cmovl", "cmovnl", "cmovle", "cmovnle",
  168. "cmova", "cmovna", "cmovae", "cmovnae", "cmovb", "cmovnb", "cmovbe", "cmovnbe",
  169. "cmovs", "cmovns", "cmovo", "cmovno", "cmovp", "cmovnp", "cmovpo", "cmovpe",
  170. "cmp", "cmps", "cmpsb", "cmpsw", "cmpsd", "cmpsq", "cmpxchg", "lea",
  171. "monitor", "cpuid", "in", "out", "syscall", "sysenter", "sysret", "sysexit",
  172. "swap", "bswap", "pop", "push", "call", "ret", "enter", "leave",
  173. "and", "or", "not", "neg", "sal", "sar", "shl", "shr",
  174. "inc", "dec", "add", "sub", "mul", "div", "imul", "idiv",
  175. "nop", "fnop", "adc", "sbb", "aaa", "aas", "aam", "aad",
  176. "jmp", "jrcxz", "jc", "jnc", "je", "jne", "jz", "jnz",
  177. "jg", "jng", "jge", "jnge", "jl", "jnl", "jle", "jnle",
  178. "ja", "jna", "jae", "jnae", "jb", "jnb", "jbe", "jnbe",
  179. "js", "jns", "jo", "jno", "jp", "jnp", "jpo", "jpe",
  180. "rep", "repe", "repz", "repne", "repnz", "loop", "loope", "loopne"
  181. };
  182. char * registers [] = {
  183. "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
  184. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  185. "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi",
  186. "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
  187. "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
  188. "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w",
  189. "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil",
  190. "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b",
  191. "ah", "ch", "dh", "bh"
  192. };
  193. char * keywords [] = {
  194. "format", "executable", "readable", "writable", "segment", "sector", "entry", "macro",
  195. "db", "dw", "dd", "dq", "rb", "rw", "rd", "rq"
  196. };
  197. int word;
  198. syntax_define (FALSE, FALSE, ";", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
  199. syntax_define (FALSE, FALSE, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
  200. syntax_define (FALSE, FALSE, "\"", "\"", '\\', COLOUR_PINK, EFFECT_NORMAL);
  201. for (word = 0; word != (int) (sizeof (instructions) / sizeof (instructions [0])); ++word) {
  202. syntax_define (FALSE, TRUE, instructions [word], separators, '\0', COLOUR_YELLOW, EFFECT_BOLD);
  203. }
  204. for (word = 0; word != (int) (sizeof (registers) / sizeof (registers [0])); ++word) {
  205. syntax_define (FALSE, TRUE, registers [word], separators, '\0', COLOUR_CYAN, EFFECT_BOLD);
  206. }
  207. for (word = 0; word != (int) (sizeof (keywords) / sizeof (keywords [0])); ++word) {
  208. syntax_define (FALSE, TRUE, keywords [word], separators, '\0', COLOUR_YELLOW, EFFECT_ITALIC);
  209. }
  210. syntax_define (TRUE, FALSE, "()[]{}", "", '\0', COLOUR_BLUE, EFFECT_NORMAL);
  211. syntax_define (TRUE, FALSE, ".,+-=:;%$<>", "", '\0', COLOUR_CYAN, EFFECT_NORMAL);
  212. syntax_define (TRUE, TRUE, "0123456789", separators, '\0', COLOUR_PINK, EFFECT_BOLD);
  213. syntax_define (TRUE, TRUE, "abcdefghijklmnopqrstuvwxyz", separators, '\0', COLOUR_WHITE, EFFECT_NORMAL);
  214. syntax_define (TRUE, TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separators, '\0', COLOUR_WHITE, EFFECT_BOLD);
  215. syntax_define (TRUE, TRUE, "_", separators, '\0', COLOUR_WHITE, EFFECT_ITALIC);
  216. }
  217. int main (int argc, char * * argv) {
  218. int offset = 0;
  219. int select = 0;
  220. int length = 0;
  221. char * buffer = NULL;
  222. argument_define ("-v", "--version", echo_version);
  223. argument_define ("-l", "--license", echo_license);
  224. argument_define ("-c", "--c", highlight_c);
  225. argument_define ("-a", "--ada", highlight_ada);
  226. argument_define ("-C", "--c++", highlight_cpp);
  227. argument_define ("-V", "--valgrind", highlight_valgrind);
  228. argument_define ("-A", "--assembly", highlight_common_assembly);
  229. argument_define ("-F", "--flat-assembly", highlight_flat_assembly);
  230. if (argc != 1) {
  231. argument_select (argc, argv);
  232. }
  233. if (buffer == NULL) {
  234. if (argument_input == NULL) {
  235. buffer = record ();
  236. } else {
  237. select = file_type (argument_input);
  238. buffer = file_import (argument_input);
  239. }
  240. }
  241. if (syntax_active == FALSE) {
  242. if ((select == FILE_TYPE_C_SOURCE) || (select == FILE_TYPE_C_HEADER)) {
  243. highlight_c ();
  244. } else if ((select == FILE_TYPE_ADA_BODY) || (select == FILE_TYPE_ADA_SPECIFICATION)) {
  245. highlight_ada ();
  246. } else if ((select == FILE_TYPE_CPP_SOURCE) || (select == FILE_TYPE_CPP_HEADER)) {
  247. highlight_cpp ();
  248. } else if (select == FILE_TYPE_COMMON_ASSEMBLY) {
  249. highlight_common_assembly ();
  250. } else if (select == FILE_TYPE_FLAT_ASSEMBLY) {
  251. highlight_flat_assembly ();
  252. } else {
  253. highlight_common ();
  254. }
  255. }
  256. for (offset = 0; buffer [offset] != '\0'; offset += length) {
  257. select = syntax_select (& buffer [offset], & length);
  258. if (select >= syntax_count) {
  259. terminal_colour (COLOUR_WHITE, EFFECT_NORMAL);
  260. } else {
  261. terminal_colour (syntax_colour [select], syntax_effect [select]);
  262. }
  263. out (& buffer [offset], length);
  264. terminal_cancel ();
  265. }
  266. buffer = deallocate (buffer);
  267. argument_delete ();
  268. return (EXIT_SUCCESS);
  269. }