From cc0ddd7c74c8c0d32b84d9c21661873e8b450f25 Mon Sep 17 00:00:00 2001 From: Erkin Batu Altunbaş Date: Sat, 12 May 2012 19:57:55 +0200 Subject: Bunch of edits * Make truncater rather readable and indented (hope you won't mind!) * List people in CREDITS (ask to get your name removed or changed) * Make README better --- ponysaytruncater.c | 198 ++++++++++++++++++++++++----------------------------- 1 file changed, 89 insertions(+), 109 deletions(-) (limited to 'ponysaytruncater.c') diff --git a/ponysaytruncater.c b/ponysaytruncater.c index 9c7191e..a89b1e0 100644 --- a/ponysaytruncater.c +++ b/ponysaytruncater.c @@ -1,58 +1,37 @@ -/** - * ponysaytruncater — Output truncater used by ponysay to stop large ponies from being printed badly. +/* ponysaytruncater + * Output truncater used by ponysay to stop + * large ponies from being printed badly. * - * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * Licensed under WTFPL * See COPYING for details */ #include - - #define String char* #define boolean char - #define true 1 #define false 0 - - -/** - * Stdin file descriptor ID - */ +/* Stdin file descriptor ID */ #define STDIN 0 - - -/** - * The number of columns on the current line - */ +/* The number of columns on the current line */ static int x = 0; -/** - * Escape sequence state - */ +/* Escape sequence state */ static int esc = 0; -/** - * Last bytes as written - */ +/* Last bytes as written */ static boolean ok = true; - - - void write(char b, int width); int toInt(String string); - -/** - *

Mane method!

- *

+/* Mane method! * The only argument, in addition to the executed file, * should be the width of the terminal which you get by * adding `tput cols || echo 0` as and argument. - *

- * + * * @param argc The number of startup arguments * @param argv The startup arguments, the first is the file itself * @@ -60,115 +39,116 @@ int toInt(String string); */ void main(int argc, String* argv) { - int width = 0; - if (argc > 1) - width = toInt(*(argv + 1)); - - char b = 0; - - if (width > 15) //sanity - while (read(STDIN, &b, 1)) - write(b, width); - else - while (read(STDIN, &b, 1)) - printf("%c", b); + int width = 0; + if (argc > 1) + width = toInt(*(argv + 1)); + + char b = 0; + + if (width > 15) /* sanity */ + while (read(STDIN, &b, 1)) + write(b, width); + else + while (read(STDIN, &b, 1)) + printf("%c", b); } - -/** - * Writes a character to stdout, iff it fits within the terminal +/* Writes a character to stdout, iff it fits within the terminal * * @param b The character (byte) to write * @param width The width of the terminal */ void write(char b, int width) { - int i; - char nx; - - if (esc == 0) + int i; + char nx; + + if (esc == 0) { - if (b == '\n') + if (b == '\n') { - if (x >= width) + if (x >= width) { - // Reset background colour - write('\e', width); - write('[', width); - write('4', width); - write('9', width); - write('m', width); + /* Reset background colour */ + write('\e', width); + write('[', width); + write('4', width); + write('9', width); + write('m', width); } - x = -1; + x = -1; } - else if (b == '\t') + else if (b == '\t') { - // Tab to next pos ≡₈ 0 - nx = 8 - (x & 7); - for (i = 0; i < nx; i++) - write(' ', width); - return; //(!) + /* Tab to next pos ≡₈ 0 */ + nx = 8 - (x & 7); + for (i = 0; i < nx; i++) + write(' ', width); + return; /* (!) */ } - else if (b == '\e') - esc = 1; + else if (b == '\e') + esc = 1; } - else if (esc == 1) + else if (esc == 1) { - if (b == '[') esc = 2; //CSI ends with a letter, m is for colour - else if (b == ']') esc = 3; //OSI, OSI P is for palett editing in Linux VT - else esc = 10; //Nothing to see here, move along + /* CSI ends with a letter, m is for colour + OSI, OSI P is for palett editing in Linux VT */ + if (b == '[') esc = 2; + else if (b == ']') esc = 3; + else esc = 10; /* Nothing to see here, move along */ } - else if (esc == 2) + else if (esc == 2) { - if ((('a' <= b) && (b <= 'z')) || (('A' <= b) && (b <= 'Z'))) - esc = 10; + if ((('a' <= b) && (b <= 'z')) || (('A' <= b) && (b <= 'Z'))) + esc = 10; } - else if ((esc == 3) && (b == 'P')) + else if ((esc == 3) && (b == 'P')) { - esc = ~0; + esc = ~0; } - else if (esc < 0) + else if (esc < 0) { - esc--; - if (esc == ~7) - esc = 10; + esc--; + if (esc == ~7) + esc = 10; } - else - esc = 10; - - if ( // Can be printed: - (x < width) || // within bounds ∨ - (esc != 0) || // ∨ escape sequence ∨ - (ok && ((b & 0xC0) == 0x80))) // ∨ last with printed ∧ not first byte in character + else + esc = 10; + + if ( + /* Can be printed: + within bounds ∨ + ∨ escape sequence ∨ + ∨ last with printed ∧ not first byte in character */ + (x < width) || + (esc != 0) || + (ok && ((b & 0xC0) == 0x80))) { - printf("%c", b); - if ((esc == 0) && ((b & 0xC0) != 0x80)) // Count up columns of not in escape sequnce and - x++; // the byte is not the first byte in the character - ok = true; + printf("%c", b); + if ((esc == 0) && ((b & 0xC0) != 0x80)) + /* Count up columns of not in escape sequnce and */ + x++; /* the byte is not the first byte in the character */ + ok = true; } - else - ok = false; - - if (esc == 10) - esc = 0; + else + ok = false; + + if (esc == 10) + esc = 0; } - -/** - * Converts a string to an integer - * +/* Converts a string to an integer * @param string The string to convert * @return The integer represented by the string */ int toInt(String string) { - int rc = 0; - String str = string; - char c = 0; - - while ((c = *str++) != 0) - rc = (rc << 1) + (rc << 3) - (c & 15); - - return -rc; + int rc = 0; + String str = string; + char c = 0; + + while ((c = *str++) != 0) + rc = (rc << 1) + (rc << 3) - (c & 15); + + return -rc; } - -- cgit From 013b6558c97d8ef6732db85525589b5a26280e05 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 12 May 2012 19:24:10 +0200 Subject: minor documentation fix --- ponysaytruncater.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'ponysaytruncater.c') diff --git a/ponysaytruncater.c b/ponysaytruncater.c index a89b1e0..58f3d91 100644 --- a/ponysaytruncater.c +++ b/ponysaytruncater.c @@ -91,10 +91,8 @@ void write(char b, int width) } else if (esc == 1) { - /* CSI ends with a letter, m is for colour - OSI, OSI P is for palett editing in Linux VT */ - if (b == '[') esc = 2; - else if (b == ']') esc = 3; + if (b == '[') esc = 2; /* CSI: CSI ends with a letter, m is for colour */ + else if (b == ']') esc = 3; /* OSI: OSI P is for palett editing in Linux VT */ else esc = 10; /* Nothing to see here, move along */ } else if (esc == 2) @@ -117,8 +115,8 @@ void write(char b, int width) if ( /* Can be printed: - within bounds ∨ - ∨ escape sequence ∨ + within bounds ∨ + ∨ escape sequence ∨ ∨ last with printed ∧ not first byte in character */ (x < width) || (esc != 0) || @@ -138,6 +136,7 @@ void write(char b, int width) } /* Converts a string to an integer + * * @param string The string to convert * @return The integer represented by the string */ -- cgit