diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2025-06-29 20:48:41 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2025-06-29 20:48:41 -0400 |
| commit | 129a10d1ee57a047ae9a3084e175de1e7daf6ffe (patch) | |
| tree | 079c753b5a9e4856c121a1a2eb59dc6ceba9396f | |
| parent | Ignore .cache (diff) | |
| download | cli-129a10d1ee57a047ae9a3084e175de1e7daf6ffe.zip | |
Fix justify behavior
| -rw-r--r-- | .clangd | 2 | ||||
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | Dockerfile | 5 | ||||
| -rw-r--r-- | doc/justify.1 | 53 | ||||
| -rw-r--r-- | include/linebuffer.h (renamed from src/linebuffer.h) | 15 | ||||
| -rw-r--r-- | include/strlcat.h (renamed from src/strlcat.h) | 0 | ||||
| -rw-r--r-- | include/word.h (renamed from src/word.h) | 0 | ||||
| -rw-r--r-- | src/linebuffer.c | 139 | ||||
| -rw-r--r-- | src/main.c | 49 |
9 files changed, 195 insertions, 71 deletions
@@ -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 @@ -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 <hello@eugenetriguba.com> + +.SH SEE ALSO +fmt(1), fold(1) diff --git a/src/linebuffer.h b/include/linebuffer.h index 173bad3..a3d9e4c 100644 --- a/src/linebuffer.h +++ b/include/linebuffer.h @@ -2,6 +2,7 @@ #define LINEBUFFER_H #include <stddef.h> +#include <stdbool.h> #include <stdio.h> typedef struct { @@ -40,8 +41,20 @@ void linebuffer_clear(LineBuffer *lb); * * @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. */ -void linebuffer_append_word(LineBuffer *lb, const char *word); +bool linebuffer_can_fit(LineBuffer *lb, const char *word); /** * @brief Check how many characters can be added. diff --git a/src/strlcat.h b/include/strlcat.h index 0470b83..0470b83 100644 --- a/src/strlcat.h +++ b/include/strlcat.h diff --git a/src/word.h b/include/word.h index b5d94b5..b5d94b5 100644 --- a/src/word.h +++ b/include/word.h 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 <ctype.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#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; } @@ -1,18 +1,43 @@ #include <stdbool.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <getopt.h> #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; } |