From 129a10d1ee57a047ae9a3084e175de1e7daf6ffe Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 29 Jun 2025 20:48:41 -0400 Subject: Fix justify behavior --- .clangd | 2 + CMakeLists.txt | 3 +- Dockerfile | 5 +- doc/justify.1 | 53 ++++++++++++++++++++ include/linebuffer.h | 88 ++++++++++++++++++++++++++++++++ include/strlcat.h | 15 ++++++ include/word.h | 28 +++++++++++ src/linebuffer.c | 139 +++++++++++++++++++++++++++++---------------------- src/linebuffer.h | 75 --------------------------- src/main.c | 49 ++++++++++++++---- src/strlcat.h | 15 ------ src/word.h | 28 ----------- 12 files changed, 312 insertions(+), 188 deletions(-) create mode 100644 .clangd create mode 100644 doc/justify.1 create mode 100644 include/linebuffer.h create mode 100644 include/strlcat.h create mode 100644 include/word.h delete mode 100644 src/linebuffer.h delete mode 100644 src/strlcat.h delete mode 100644 src/word.h diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..91e78fc --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-Iinclude] diff --git a/CMakeLists.txt b/CMakeLists.txt index 67ab38d..7c825be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.29) project(justify VERSION 0.1.0) +include_directories(${CMAKE_SOURCE_DIR}/include) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED True) @@ -13,7 +14,7 @@ endif() include(CheckFunctionExists) check_function_exists(strlcat HAVE_STRLCAT) if(HAVE_STRLCAT) - add_definitions(-DHAVE_STRLCAT=1) + add_definitions(-DHAVE_STRLCAT=1) endif() set(SRC_FILES diff --git a/Dockerfile b/Dockerfile index 33568a0..acc515c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,12 @@ -FROM alpine:3.20.1 +FROM alpine:3.22.0 RUN apk update && \ apk upgrade && \ apk add --no-cache \ build-base \ + pkgconf \ + cmocka \ + cmocka-dev \ lldb \ clang \ cmake \ diff --git a/doc/justify.1 b/doc/justify.1 new file mode 100644 index 0000000..611f1ae --- /dev/null +++ b/doc/justify.1 @@ -0,0 +1,53 @@ +.TH JUSTIFY 1 "July 2025" "justify 0.1.0" "User Commands" +.SH NAME +justify \- format and justify text to a specified line width +.SH SYNOPSIS +.B justify +[\fB-w\fR \fIWIDTH\fR] +.SH DESCRIPTION +.B justify +reads text from standard input, formats it into lines of a specified width, and justifies the text so that both the left and right margins are aligned. Justification is achieved by distributing extra spaces between words so that each line (except the last line of a paragraph) is exactly the specified width. + +The last line of each paragraph is left-aligned and not justified. + +.SH OPTIONS +.TP +.BR \-w ", " \-\-width " " \fIWIDTH\fR +Set the line width for justification. The default is 72 characters. + +.SH USAGE +Input is read from standard input. Output is written to standard output. + +To justify a file to 80 columns: +.RS +.nf +$ justify -w 80 < input.txt +.fi +.RE + +To justify text from a pipe: +.RS +.nf +$ echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit." | justify +.fi +.RE + +.SH EXIT STATUS +.TP +.B 0 +Success. +.TP +.B 1 +An error occurred (e.g., read error). + +.SH NOTES +Before appending a word to the current line, the program ensures there is enough space for the word (and a space if not the first word). If not enough space remains, the current line is flushed (justified) and a new line is started. + +.SH LIMITATIONS +The current implementation does not fully wrap or split words that are longer than the specified line width. Overlong words may be truncated or marked, rather than wrapped across lines. + +.SH AUTHOR +Eugene Triguba + +.SH SEE ALSO +fmt(1), fold(1) diff --git a/include/linebuffer.h b/include/linebuffer.h new file mode 100644 index 0000000..a3d9e4c --- /dev/null +++ b/include/linebuffer.h @@ -0,0 +1,88 @@ +#ifndef LINEBUFFER_H +#define LINEBUFFER_H + +#include +#include +#include + +typedef struct { + size_t _num_words; + size_t _buffer_length; + size_t _buffer_capacity; + char *_buffer; +} LineBuffer; + +/** + * @brief Create a new LineBuffer + * + * @param line_length The length of the buffer to use. + * + * @returns The newly allocated LineBuffer; otherwise NULL + * if the allocation fails. + */ +LineBuffer *linebuffer_create(size_t line_length); + +/** + * @brief Release any allocated memory for the LineBuffer + * + * @param lb The LineBuffer to destroy. + */ +void linebuffer_destroy(LineBuffer *lb); + +/** + * @brief Clear the current buffer + * + * @param lb The LineBuffer to clear. + */ +void linebuffer_clear(LineBuffer *lb); + +/** + * @brief Append a word to the end of the buffer. + * + * @param lb The line buffer to append to. + * @param word The word to append. + * + * @retursns true if the word was appended; false if it could not fit. + */ +bool linebuffer_append_word(LineBuffer *lb, const char *word); + +/** + * @brief Check if a word can fit in the current line buffer. + * + * @param lb The line buffer to check. + * @param word The word to check. + * + * @returns true if the word can fit; false otherwise. + */ +bool linebuffer_can_fit(LineBuffer *lb, const char *word); + +/** + * @brief Check how many characters can be added. + * + * @param lb The line buffer to check the space of. + * + * @returns The number of characters left in the line. + */ +size_t linebuffer_space_remaining(LineBuffer *lb); + +/** + * @brief Write the current line. + * + * @param lb The line buffer to read. + * @param fp The file to write out the line buffer to. + * + * @returns >= 0 on success; EOF on error. + */ +int linebuffer_write(LineBuffer *lb, FILE *fp); + +/** + * @brief Write the current line as justified text. + * + * @param lb The line buffer to read. + * @param fp The file to write out the line buffer to. + * + * @returns >= 0 on success; EOF on error. + */ +int linebuffer_write_justified(LineBuffer *lb, FILE *fp); + +#endif diff --git a/include/strlcat.h b/include/strlcat.h new file mode 100644 index 0000000..0470b83 --- /dev/null +++ b/include/strlcat.h @@ -0,0 +1,15 @@ +#ifndef HAVE_STRLCAT +#define HAVE_STRLCAT + +#include + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t strlcat(char *dst, const char *src, size_t dstsize); + +#endif diff --git a/include/word.h b/include/word.h new file mode 100644 index 0000000..b5d94b5 --- /dev/null +++ b/include/word.h @@ -0,0 +1,28 @@ +#ifndef WORD_H +#define WORD_H + +#include + +#define READ_WORD_ERR_READ -1 +#define READ_WORD_ERR_EOF 0 +#define READ_WORD_SUCCESS 1 + +/** + * Read in a word from a file up to size length. + * + * If there is any leading whitespace, it will be discarded + * before starting to read in a word. + * + * @param[in] fp The file to read a word from. + * @param[out] word The word that was read as a null-terminated string. + * @param[in] length The length of the word to read in. + * + * @note `length` should be the number of characters you'd + * like to read in plus one to take into account the + * null terminator. + * + * @returns < 0 on read error; 0 on EOF; > 0 on success. + */ +int read_word(FILE *fp, char *word, size_t length); + +#endif diff --git a/src/linebuffer.c b/src/linebuffer.c index 6dfb6c3..772ba77 100644 --- a/src/linebuffer.c +++ b/src/linebuffer.c @@ -1,89 +1,110 @@ #include "linebuffer.h" - +#include "strlcat.h" #include +#include #include #include #include -#include "strlcat.h" +#define NULL_TERMINATOR_LEN 1 LineBuffer *linebuffer_create(size_t buffer_capacity) { - LineBuffer *lb = malloc(sizeof(LineBuffer)); - if (lb == NULL) { - return NULL; - } - lb->_buffer = malloc(buffer_capacity + 1); - if (lb->_buffer == NULL) { - free(lb); - return NULL; - } - memset(lb->_buffer, 0, buffer_capacity + 1); - lb->_num_words = 0; - lb->_buffer_length = 0; - lb->_buffer_capacity = buffer_capacity; - return lb; + LineBuffer *lb = malloc(sizeof(LineBuffer)); + if (lb == NULL) { + return NULL; + } + lb->_buffer = malloc(buffer_capacity + NULL_TERMINATOR_LEN); + if (lb->_buffer == NULL) { + free(lb); + return NULL; + } + memset(lb->_buffer, 0, buffer_capacity + NULL_TERMINATOR_LEN); + lb->_num_words = 0; + lb->_buffer_length = 0; + lb->_buffer_capacity = buffer_capacity; + return lb; } void linebuffer_destroy(LineBuffer *lb) { - free(lb->_buffer); - free(lb); + free(lb->_buffer); + free(lb); } void linebuffer_clear(LineBuffer *lb) { - memset(lb->_buffer, 0, lb->_buffer_capacity + 1); - lb->_num_words = 0; - lb->_buffer_length = 0; + memset(lb->_buffer, 0, lb->_buffer_capacity + NULL_TERMINATOR_LEN); + lb->_num_words = 0; + lb->_buffer_length = 0; } -void linebuffer_append_word(LineBuffer *lb, const char *word) { - if (lb->_num_words > 0) { - lb->_buffer[lb->_buffer_length] = ' '; - lb->_buffer[lb->_buffer_length + 1] = '\0'; - lb->_buffer_length++; - } - strlcat(lb->_buffer, word, (lb->_buffer_capacity + 1) - lb->_buffer_length); - lb->_buffer_length += strlen(word); - lb->_num_words++; +bool linebuffer_append_word(LineBuffer *lb, const char *word) { + if (!linebuffer_can_fit(lb, word)) { + return false; + } + if (lb->_num_words > 0) { + lb->_buffer[lb->_buffer_length] = ' '; + lb->_buffer_length++; + } + strlcat(lb->_buffer, word, lb->_buffer_capacity + NULL_TERMINATOR_LEN); + lb->_buffer_length += strlen(word); + lb->_num_words++; + return true; +} + +bool linebuffer_can_fit(LineBuffer *lb, const char *word) { + size_t append_len = strlen(word); + if (lb->_num_words > 0) { + // Account for space before the word + append_len++; + } + return append_len <= linebuffer_space_remaining(lb); } size_t linebuffer_space_remaining(LineBuffer *lb) { - return lb->_buffer_capacity - lb->_buffer_length; + return lb->_buffer_capacity - lb->_buffer_length; } int linebuffer_write(LineBuffer *lb, FILE *fp) { - if (lb->_buffer_length > 0) { - return fputs(lb->_buffer, fp); + int return_code = 0; + if (lb->_buffer_length > 0) { + return_code = fputs(lb->_buffer, fp); + if (return_code == EOF) { + return EOF; } - return 0; + return_code = fputc('\n', fp); + if (return_code == EOF) { + return EOF; + } + } + return return_code; } int linebuffer_write_justified(LineBuffer *lb, FILE *fp) { - int return_code = 0; - size_t words_remaining = lb->_num_words; - size_t extra_spaces = linebuffer_space_remaining(lb); - size_t spaces_to_insert = 0; + int return_code = 0; + size_t words_remaining = lb->_num_words; + size_t extra_spaces = linebuffer_space_remaining(lb); + size_t spaces_to_insert = 0; - for (size_t i = 0; i < lb->_buffer_length; i++) { - if (!isspace(lb->_buffer[i])) { - return_code = fputc(lb->_buffer[i], fp); - if (return_code == EOF) { - return EOF; - } - } else { - spaces_to_insert = extra_spaces / (words_remaining - 1); - extra_spaces -= spaces_to_insert; - for (size_t j = 1; j <= spaces_to_insert + 1; j++) { - return_code = fputc(' ', fp); - if (return_code == EOF) { - return EOF; - } - } - words_remaining--; - } - } - return_code = fputc('\n', fp); - if (return_code == EOF) { + for (size_t i = 0; i < lb->_buffer_length; i++) { + if (!isspace(lb->_buffer[i])) { + return_code = fputc(lb->_buffer[i], fp); + if (return_code == EOF) { return EOF; + } + } else { + spaces_to_insert = extra_spaces / (words_remaining - 1); + extra_spaces -= spaces_to_insert; + for (size_t j = 1; j <= spaces_to_insert + 1; j++) { + return_code = fputc(' ', fp); + if (return_code == EOF) { + return EOF; + } + } + words_remaining--; } - return return_code; + } + return_code = fputc('\n', fp); + if (return_code == EOF) { + return EOF; + } + return return_code; } diff --git a/src/linebuffer.h b/src/linebuffer.h deleted file mode 100644 index 173bad3..0000000 --- a/src/linebuffer.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef LINEBUFFER_H -#define LINEBUFFER_H - -#include -#include - -typedef struct { - size_t _num_words; - size_t _buffer_length; - size_t _buffer_capacity; - char *_buffer; -} LineBuffer; - -/** - * @brief Create a new LineBuffer - * - * @param line_length The length of the buffer to use. - * - * @returns The newly allocated LineBuffer; otherwise NULL - * if the allocation fails. - */ -LineBuffer *linebuffer_create(size_t line_length); - -/** - * @brief Release any allocated memory for the LineBuffer - * - * @param lb The LineBuffer to destroy. - */ -void linebuffer_destroy(LineBuffer *lb); - -/** - * @brief Clear the current buffer - * - * @param lb The LineBuffer to clear. - */ -void linebuffer_clear(LineBuffer *lb); - -/** - * @brief Append a word to the end of the buffer. - * - * @param lb The line buffer to append to. - * @param word The word to append. - */ -void linebuffer_append_word(LineBuffer *lb, const char *word); - -/** - * @brief Check how many characters can be added. - * - * @param lb The line buffer to check the space of. - * - * @returns The number of characters left in the line. - */ -size_t linebuffer_space_remaining(LineBuffer *lb); - -/** - * @brief Write the current line. - * - * @param lb The line buffer to read. - * @param fp The file to write out the line buffer to. - * - * @returns >= 0 on success; EOF on error. - */ -int linebuffer_write(LineBuffer *lb, FILE *fp); - -/** - * @brief Write the current line as justified text. - * - * @param lb The line buffer to read. - * @param fp The file to write out the line buffer to. - * - * @returns >= 0 on success; EOF on error. - */ -int linebuffer_write_justified(LineBuffer *lb, FILE *fp); - -#endif diff --git a/src/main.c b/src/main.c index 2228279..1dbfe4c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,18 +1,43 @@ #include #include +#include #include +#include #include "linebuffer.h" #include "word.h" -#define MAX_WORD_LENGTH 20 -#define LINEBUFFER_CAPACITY 60 +#define DEFAULT_LINEBUFFER_CAPACITY 72 -int main(void) { +void print_usage(const char *prog) { + fprintf(stderr, "Usage: %s [-w WIDTH]\n", prog); +} + +int main(int argc, char *argv[]) { + int opt; + size_t linebuffer_capacity = DEFAULT_LINEBUFFER_CAPACITY; + + while ((opt = getopt(argc, argv, "w:h")) != -1) { + switch (opt) { + case 'w': + linebuffer_capacity = (size_t)atoi(optarg); + if (linebuffer_capacity == 0) { + print_usage(argv[0]); + return EXIT_FAILURE; + } + break; + case 'h': + default: + print_usage(argv[0]); + return EXIT_FAILURE; + } + } + + const size_t MAX_WORD_LENGTH = linebuffer_capacity; char word[MAX_WORD_LENGTH + 2]; size_t word_length; int read_word_result; - LineBuffer *lb = linebuffer_create(LINEBUFFER_CAPACITY); + LineBuffer *lb = linebuffer_create(linebuffer_capacity); while ((read_word_result = read_word(stdin, word, MAX_WORD_LENGTH + 1)) == READ_WORD_SUCCESS) { @@ -20,21 +45,27 @@ int main(void) { if (word_length == 0) { linebuffer_write(lb, stdout); linebuffer_destroy(lb); - return 0; + return EXIT_SUCCESS; } if (word_length > MAX_WORD_LENGTH) { + // TODO: If the word is too long for a given line, + // we should wrap it to the next line. word[MAX_WORD_LENGTH] = '*'; } - if (word_length >= linebuffer_space_remaining(lb)) { + if (!linebuffer_append_word(lb, word)) { linebuffer_write_justified(lb, stdout); linebuffer_clear(lb); + linebuffer_append_word(lb, word); } - linebuffer_append_word(lb, word); memset(word, 0, MAX_WORD_LENGTH + 2); } if (read_word_result == READ_WORD_ERR_READ) { fprintf(stderr, "Error reading word\n"); - return 1; + return EXIT_FAILURE; + } + if (lb->_num_words > 0) { + linebuffer_write(lb, stdout); } - return 0; + linebuffer_destroy(lb); + return EXIT_SUCCESS; } diff --git a/src/strlcat.h b/src/strlcat.h deleted file mode 100644 index 0470b83..0000000 --- a/src/strlcat.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HAVE_STRLCAT -#define HAVE_STRLCAT - -#include - -/* - * Appends src to string dst of size siz (unlike strncat, siz is the - * full size of dst, not space left). At most siz-1 characters - * will be copied. Always NUL terminates (unless siz <= strlen(dst)). - * Returns strlen(src) + MIN(siz, strlen(initial dst)). - * If retval >= siz, truncation occurred. - */ -size_t strlcat(char *dst, const char *src, size_t dstsize); - -#endif diff --git a/src/word.h b/src/word.h deleted file mode 100644 index b5d94b5..0000000 --- a/src/word.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef WORD_H -#define WORD_H - -#include - -#define READ_WORD_ERR_READ -1 -#define READ_WORD_ERR_EOF 0 -#define READ_WORD_SUCCESS 1 - -/** - * Read in a word from a file up to size length. - * - * If there is any leading whitespace, it will be discarded - * before starting to read in a word. - * - * @param[in] fp The file to read a word from. - * @param[out] word The word that was read as a null-terminated string. - * @param[in] length The length of the word to read in. - * - * @note `length` should be the number of characters you'd - * like to read in plus one to take into account the - * null terminator. - * - * @returns < 0 on read error; 0 on EOF; > 0 on success. - */ -int read_word(FILE *fp, char *word, size_t length); - -#endif -- cgit v1.2.3