xC: improve the --format mode

Avoid having formatting spill over the rest of the line,
by placing the automatic formatting reset before newlines.

Also handle longer lines properly.
This commit is contained in:
Přemysl Eric Janouch 2023-05-22 04:35:00 +02:00
parent 9932b35a10
commit 3f9a365d36
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 18 additions and 3 deletions

21
xC.c
View File

@ -16004,14 +16004,29 @@ show_logo (struct app_context *ctx)
static void static void
format_input_and_die (struct app_context *ctx) format_input_and_die (struct app_context *ctx)
{ {
char buf[513]; // XXX: it might make sense to allow for redirection, using FLUSH_OPT_RAW
while (fgets (buf, sizeof buf, stdin)) struct str s = str_make ();
int c = 0;
while ((c = fgetc (stdin)) != EOF)
{ {
if (c != '\n')
{
str_append_c (&s, c);
continue;
}
struct formatter f = formatter_make (ctx, NULL); struct formatter f = formatter_make (ctx, NULL);
formatter_add (&f, "#m", buf); formatter_add (&f, "#m\n", s.str);
formatter_flush (&f, stdout, FLUSH_OPT_NOWRAP); formatter_flush (&f, stdout, FLUSH_OPT_NOWRAP);
formatter_free (&f); formatter_free (&f);
str_reset (&s);
} }
struct formatter f = formatter_make (ctx, NULL);
formatter_add (&f, "#m", s.str);
formatter_flush (&f, stdout, FLUSH_OPT_NOWRAP);
formatter_free (&f);
str_free (&s);
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
} }