diff --git a/README.md b/README.md index c000bfc..650616d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,29 @@ # xuxuxu -xuxuxu lol +I'm gonna update this, I swear... --- Render xolatile-approved C source code as 'png' image... +xuxuxu -- Program for highlighting C source code and exporting it as PNG image. + +- Everything related to my libraries is clean of all warning options on Clang, GCC and Valgrind. +- I don't know what else to write here, program is being done in my free time, so expect more... +- It kinda looks more serious if I have very long 'README.md' file, almost like a valid project. + +Compile: +```bash +$ sh compile.sh +``` + +Install: +```bash +$ sudo sh install.sh +``` + +Use: +```bash +$ cat my_c_program.c | xuxuxu +$ xuxuxu < my_c_program.c +``` + +Example of rendered and exported Xuxuxu source code: + +![screenshot](xuxuxu.png) diff --git a/compile.sh b/compile.sh index c29da21..04e45a0 100644 --- a/compile.sh +++ b/compile.sh @@ -2,8 +2,6 @@ set -xe -gcc -ansi -Wall -Wextra -Wpedantic -o xuxuxu xuxuxu.c -lpng -clang -ansi -Weverything -o xuxuxu xuxuxu.c -lpng -#~cat xuxuxu.c | valgrind --show-leak-kinds=all --leak-check=full ./xuxuxu +gcc -g -ansi -Wall -Wextra -Wpedantic -Werror -o xuxuxu xuxuxu.c -lpng exit diff --git a/install.sh b/install.sh index 9d3d3a3..1d69e78 100644 --- a/install.sh +++ b/install.sh @@ -2,6 +2,6 @@ set -xe -sudo cp xuxuxu /usr/bin/xuxuxu +cp xuxuxu /usr/bin/xuxuxu exit diff --git a/xuxuxu.c b/xuxuxu.c index 3d9c917..fa5f05c 100644 --- a/xuxuxu.c +++ b/xuxuxu.c @@ -6,13 +6,13 @@ * 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. */ -#include +#include #include #include -#define FOREGROUND (0XFFFFFFFF) -#define BACKGROUND (0XFF000000) +#define FOREGROUND (0XFFCCCCCC) +#define BACKGROUND (0XFF111111) #define TAB_WIDTH ( 8) #define FONT_WIDTH ( 8) @@ -48,10 +48,7 @@ static int fetch_width (char * data) { if (* data == '\t') { count += TAB_WIDTH; } else if (* data == '\n') { - ++count; - image_width = (count > image_width) - ? count - : image_width; + image_width = (++count > image_width) ? count : image_width; count = 0; } else { ++count; @@ -73,84 +70,47 @@ static int fetch_height (char * data) { return (image_height + 1); } -static void fetch_font_glyph (unsigned int * glyph, const char character) { - const unsigned long int font_code_list [FONT_COUNT] = { - 0X0000000000000000, 0X00180018183C3C18, 0X0000000000363636, - 0X006C6CFE6CFE6C6C, 0X00187ED07C16FC30, 0X0060660C18306606, - 0X00DC66B61C36361C, 0X0000000000181818, 0X0030180C0C0C1830, - 0X000C18303030180C, 0X0000187E3C7E1800, 0X000018187E181800, - 0X0C18180000000000, 0X000000007E000000, 0X0018180000000000, - 0X0000060C18306000, 0X003C666E7E76663C, 0X007E181818181C18, - 0X007E0C183060663C, 0X003C66603860663C, 0X0030307E363C3830, - 0X003C6660603E067E, 0X003C66663E060C38, 0X000C0C0C1830607E, - 0X003C66663C66663C, 0X001C30607C66663C, 0X0018180018180000, - 0X0C18180018180000, 0X0030180C060C1830, 0X0000007E007E0000, - 0X000C18306030180C, 0X001800181830663C, 0X003C06765676663C, - 0X006666667E66663C, 0X003E66663E66663E, 0X003C66060606663C, - 0X001E36666666361E, 0X007E06063E06067E, 0X000606063E06067E, - 0X003C66667606663C, 0X006666667E666666, 0X007E18181818187E, - 0X001C36303030307C, 0X0066361E0E1E3666, 0X007E060606060606, - 0X00C6C6D6D6FEEEC6, 0X006666767E6E6666, 0X003C66666666663C, - 0X000606063E66663E, 0X006C36566666663C, 0X006666363E66663E, - 0X003C66603C06663C, 0X001818181818187E, 0X003C666666666666, - 0X00183C6666666666, 0X00C6EEFED6D6C6C6, 0X0066663C183C6666, - 0X001818183C666666, 0X007E060C1830607E, 0X003E06060606063E, - 0X00006030180C0600, 0X007C60606060607C, 0X000000000000663C, - 0XFFFF000000000000, 0X000000000030180C, 0X007C667C603C0000, - 0X003E6666663E0606, 0X003C6606663C0000, 0X007C6666667C6060, - 0X003C067E663C0000, 0X000C0C0C3E0C0C38, 0X3C607C66667C0000, - 0X00666666663E0606, 0X003C1818181C0018, 0X0E181818181C0018, - 0X0066361E36660606, 0X003C18181818181C, 0X00C6D6D6FE6C0000, - 0X00666666663E0000, 0X003C6666663C0000, 0X06063E66663E0000, - 0XE0607C66667C0000, 0X000606066E360000, 0X003E603C067C0000, - 0X00380C0C0C3E0C0C, 0X007C666666660000, 0X00183C6666660000, - 0X006CFED6D6C60000, 0X00663C183C660000, 0X3C607C6666660000, - 0X007E0C18307E0000, 0X003018180E181830, 0X0018181818181818, - 0X000C18187018180C, 0X000000000062D68C, 0X0000000000000000 +static void render_character (char character) { + const unsigned long int font_code [FONT_COUNT] = { + 0X0000000000000000, 0X00180018183C3C18, 0X0000000000363636, 0X006C6CFE6CFE6C6C, 0X00187ED07C16FC30, 0X0060660C18306606, 0X00DC66B61C36361C, 0X0000000000181818, + 0X0030180C0C0C1830, 0X000C18303030180C, 0X0000187E3C7E1800, 0X000018187E181800, 0X0C18180000000000, 0X000000007E000000, 0X0018180000000000, 0X0000060C18306000, + 0X003C666E7E76663C, 0X007E181818181C18, 0X007E0C183060663C, 0X003C66603860663C, 0X0030307E363C3830, 0X003C6660603E067E, 0X003C66663E060C38, 0X000C0C0C1830607E, + 0X003C66663C66663C, 0X001C30607C66663C, 0X0018180018180000, 0X0C18180018180000, 0X0030180C060C1830, 0X0000007E007E0000, 0X000C18306030180C, 0X001800181830663C, + 0X003C06765676663C, 0X006666667E66663C, 0X003E66663E66663E, 0X003C66060606663C, 0X001E36666666361E, 0X007E06063E06067E, 0X000606063E06067E, 0X003C66667606663C, + 0X006666667E666666, 0X007E18181818187E, 0X001C36303030307C, 0X0066361E0E1E3666, 0X007E060606060606, 0X00C6C6D6D6FEEEC6, 0X006666767E6E6666, 0X003C66666666663C, + 0X000606063E66663E, 0X006C36566666663C, 0X006666363E66663E, 0X003C66603C06663C, 0X001818181818187E, 0X003C666666666666, 0X00183C6666666666, 0X00C6EEFED6D6C6C6, + 0X0066663C183C6666, 0X001818183C666666, 0X007E060C1830607E, 0X003E06060606063E, 0X00006030180C0600, 0X007C60606060607C, 0X000000000000663C, 0XFFFF000000000000, + 0X000000000030180C, 0X007C667C603C0000, 0X003E6666663E0606, 0X003C6606663C0000, 0X007C6666667C6060, 0X003C067E663C0000, 0X000C0C0C3E0C0C38, 0X3C607C66667C0000, + 0X00666666663E0606, 0X003C1818181C0018, 0X0E181818181C0018, 0X0066361E36660606, 0X003C18181818181C, 0X00C6D6D6FE6C0000, 0X00666666663E0000, 0X003C6666663C0000, + 0X06063E66663E0000, 0XE0607C66667C0000, 0X000606066E360000, 0X003E603C067C0000, 0X00380C0C0C3E0C0C, 0X007C666666660000, 0X00183C6666660000, 0X006CFED6D6C60000, + 0X00663C183C660000, 0X3C607C6666660000, 0X007E0C18307E0000, 0X003018180E181830, 0X0018181818181818, 0X000C18187018180C, 0X000000000062D68C, 0X0000000000000000 }; - int offset = 0; - unsigned long int font_code = 0; + int offset; - font_code = font_code_list [(int) character]; - - do { - glyph [offset] = ((font_code >> offset) & 1) - ? colour - : BACKGROUND; - } while (++offset != FONT_WIDTH * FONT_HEIGHT); -} - -static void render_character (char character) { - unsigned int glyph [FONT_WIDTH * FONT_HEIGHT] = { 0 }; - - int offset = 0; - - fetch_font_glyph (glyph, character); - - do { + for (offset = 0; offset != FONT_WIDTH * FONT_HEIGHT; ++offset) { int u = offset / FONT_WIDTH + y; int v = offset % FONT_WIDTH + x; - render [u * width + v] = glyph [offset]; - } while (++offset != FONT_WIDTH * FONT_HEIGHT); + render [u * width + v] = ((font_code [(int) (character - ' ')] >> offset) % 2) ? colour : BACKGROUND; + } x += FONT_WIDTH + 1; } static void render_string (char * string, int length) { - int offset = 0; + int offset; - do { + for (offset = 0; offset != length; ++offset) { if (string [offset] == '\t') { x += FONT_WIDTH * TAB_WIDTH; } else if (string [offset] == '\n') { y += FONT_HEIGHT + 1; x = 1; } else { - render_character (string [offset] - ' '); + render_character (string [offset]); } - } while (++offset != length); + } } int main (void) { @@ -160,19 +120,6 @@ int main (void) { int length = 0; char * buffer = NULL; - int preprocessor = 0; - int line_comment = 0; - int multiline_comment = 0; - int character = 0; - int string = 0; - int bracket = 0; - int operator = 0; - int keyword = 0; - int digit = 0; - int uppercase = 0; - int lowercase = 0; - int underscore = 0; - char separator [29] = ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n"; char * c_keywords [32] = { @@ -182,22 +129,20 @@ int main (void) { "char", "short", "int", "long", "signed", "unsigned", "float", "double" }; - syntax_define (& preprocessor, 0, 0, "#", "\n", '\\', COLOUR_RED, EFFECT_BOLD); - syntax_define (& line_comment, 0, 0, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD); - syntax_define (& multiline_comment, 0, 0, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD); - syntax_define (& character, 0, 0, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD); - syntax_define (& string, 0, 0, "\"", "\"", '\\', COLOUR_RED, EFFECT_BOLD); - syntax_define (& bracket, 1, 0, "()[]{}", "", '\0', COLOUR_GREEN, EFFECT_BOLD); - syntax_define (& operator, 1, 0, ".,:;<=>+-*/%!&~^?|", "", '\0', COLOUR_BLUE, EFFECT_BOLD); + (void) syntax_insert (0, 0, "#", "\n", '\\', COLOUR_PINK, 0); + (void) syntax_insert (0, 0, "//", "\n", '\0', COLOUR_GREY, 0); + (void) syntax_insert (0, 0, "/*", "*/", '\0', COLOUR_GREY, 0); + (void) syntax_insert (0, 0, "'", "'", '\\', COLOUR_RED, 0); + (void) syntax_insert (0, 0, "\"", "\"", '\\', COLOUR_RED, 0); + (void) syntax_insert (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', COLOUR_BLUE, 0); for (word = 0; word != 32; ++word) { - syntax_define (& keyword, 0, 1, c_keywords [word], separator, '\0', COLOUR_YELLOW, EFFECT_BOLD); + (void) syntax_insert (0, 1, c_keywords [word], separator, '\0', COLOUR_YELLOW, 0); } - syntax_define (& digit, 1, 1, "0123456789", separator, '\0', COLOUR_CYAN, EFFECT_BOLD); - syntax_define (& uppercase, 1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separator, '\0', COLOUR_PINK, EFFECT_ITALIC); - syntax_define (& lowercase, 1, 1, "abcdefghijklmnopqrstuvwxyz", separator, '\0', COLOUR_WHITE, EFFECT_ITALIC); - syntax_define (& underscore, 0, 1, "_", separator, '\0', COLOUR_YELLOW, EFFECT_ITALIC); + (void) syntax_insert (1, 1, "0123456789", separator, '\0', COLOUR_CYAN, 0); + + (void) syntax_insert (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_", separator, '\0', COLOUR_WHITE, 0); buffer = record (); @@ -211,18 +156,18 @@ int main (void) { } for (offset = 0; buffer [offset] != '\0'; offset += length) { - syntax_select (& buffer [offset], & index, & length); + index = syntax_select (& buffer [offset], & length); switch (syntax_colour [index]) { case COLOUR_GREY: colour = 0XFF333333; break; - case COLOUR_RED: colour = 0XFF3333FF; break; - case COLOUR_GREEN: colour = 0XFF33FF33; break; - case COLOUR_YELLOW: colour = 0XFF33FFFF; break; - case COLOUR_BLUE: colour = 0XFFFF3333; break; - case COLOUR_PINK: colour = 0XFFFF33FF; break; - case COLOUR_CYAN: colour = 0XFFFFFF33; break; - case COLOUR_WHITE: colour = 0XFFFFFFFF; break; - default: colour = 0XFFFFFFFF; break; + case COLOUR_RED: colour = 0XFF3311CC; break; + case COLOUR_GREEN: colour = 0XFF33CC11; break; + case COLOUR_YELLOW: colour = 0XFF33CCCC; break; + case COLOUR_BLUE: colour = 0XFFCC3311; break; + case COLOUR_PINK: colour = 0XFFCC33CC; break; + case COLOUR_CYAN: colour = 0XFFCCCC33; break; + case COLOUR_WHITE: colour = 0XFFCCCCCC; break; + default: colour = 0XFFCCCCCC; break; } render_string (& buffer [offset], length); diff --git a/xuxuxu.png b/xuxuxu.png new file mode 100644 index 0000000..0ac7833 Binary files /dev/null and b/xuxuxu.png differ