[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] Move some term code from lib to src
From: |
Jose E. Marchesi |
Subject: |
Re: [PATCH] Move some term code from lib to src |
Date: |
Wed, 15 Apr 2020 19:56:57 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
Hi Tim.
The attached patch is for discussion.
It moves some (IMO application) code from lib/ to src/poke.c.
I'm in the middle of a considerably big reorganization of the library
code. If you don't mind, can we postpone this discussion for a few days
until I merge the branch?
From 6490a215f763f689abcc0de357be1e2357479cb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20R=C3=BChsen?= <address@hidden>
Date: Wed, 15 Apr 2020 19:40:30 +0200
Subject: [PATCH] Move some term code from lib to src
* lib/pk-term.c (pk_term_init): Take styled_ostream_t as argument.
Move argument parsing to src/poke.c.
Move pk_term_color_p to src/poke.c.
* lib/pk-term.h (pk_term_init): Take styled_ostream_t as argument.
Remove pk_term_color_p.
* src/poke.c: Add functions term_color_p and term_init.
(initialize): Make use of term_init and term_color_p.
---
lib/pk-term.c | 65 +++---------------------------------------------
lib/pk-term.h | 6 +----
src/poke.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 71 insertions(+), 69 deletions(-)
diff --git a/lib/pk-term.c b/lib/pk-term.c
index 6307e9ae..9c49ec31 100644
--- a/lib/pk-term.c
+++ b/lib/pk-term.c
@@ -26,62 +26,12 @@
/* The following global is the libtextstyle output stream to use to
emit contents to the terminal. */
-styled_ostream_t pk_ostream;
+static styled_ostream_t pk_ostream;
void
-pk_term_init (int argc, char *argv[])
+pk_term_init (styled_ostream_t ostream)
{
- int i;
-
- /* Process terminal-related command-line options. */
- for (i = 1; i < argc; i++)
- {
- const char *arg = argv[i];
-
- if (strncmp (arg, "--color=", 8) == 0)
- {
- if (handle_color_option (arg + 8))
- exit (EXIT_FAILURE);
- }
- else if (strncmp (arg, "--style=", 8) == 0)
- handle_style_option (arg + 8);
- }
-
- /* Handle the --color=test special argument. */
- if (color_test_mode)
- {
- print_color_test ();
- exit (EXIT_SUCCESS);
- }
-
- /* Note that the following code needs to be compiled conditionally
- because the textstyle.h file provided by the gnulib module
- libtextstyle-optional defines style_file_name as an r-value. */
-
-#ifdef HAVE_LIBTEXTSTYLE
- /* Open the specified style. */
- if (color_mode == color_yes
- || (color_mode == color_tty
- && isatty (STDOUT_FILENO)
- && getenv ("NO_COLOR") == NULL)
- || color_mode == color_html)
- {
- /* Find the style file. */
- style_file_prepare ("POKE_STYLE", "POKESTYLESDIR", PKGDATADIR,
- "poke-default.css");
- }
- else
- /* No styling. */
- style_file_name = NULL;
-#endif
-
- /* Create the output styled stream. */
- pk_ostream =
- (color_mode == color_html
- ? html_styled_ostream_create (file_ostream_create (stdout),
- style_file_name)
- : styled_ostream_create (STDOUT_FILENO, "(stdout)",
- TTYCTL_AUTO, style_file_name));
+ pk_ostream = ostream;
}
void
@@ -178,12 +128,3 @@ pk_term_end_hyperlink (void)
styled_ostream_set_hyperlink (pk_ostream, NULL, NULL);
#endif
}
-
-int
-pk_term_color_p (void)
-{
- return (color_mode == color_yes
- || (color_mode == color_tty
- && isatty (STDOUT_FILENO)
- && getenv ("NO_COLOR") == NULL));
-}
diff --git a/lib/pk-term.h b/lib/pk-term.h
index d42449d3..e003d11d 100644
--- a/lib/pk-term.h
+++ b/lib/pk-term.h
@@ -24,13 +24,9 @@
#include <textstyle.h>
/* Initialize and finalize the terminal subsystem. */
-void pk_term_init (int argc, char *argv[]);
+void pk_term_init (styled_ostream_t ostream);
void pk_term_shutdown (void);
-/* Return 1 if the terminal supports colors/hyperlinks. Return 0
- otherwise. */
-extern int pk_term_color_p (void);
-
/* Flush the terminal output. */
extern void pk_term_flush (void);
diff --git a/src/poke.c b/src/poke.c
index a40eb97a..ba9f6582 100644
--- a/src/poke.c
+++ b/src/poke.c
@@ -363,6 +363,71 @@ parse_args (int argc, char *argv[])
exit (EXIT_FAILURE);
}
+/* Return 1 if the terminal supports colors/hyperlinks. Return 0
+ otherwise. */
+static int
+term_color_p (void)
+{
+ return (color_mode == color_yes
+ || (color_mode == color_tty
+ && isatty (STDOUT_FILENO)
+ && getenv ("NO_COLOR") == NULL));
+}
+
+static styled_ostream_t
+term_init (int argc, char *argv[])
+{
+ /* Process terminal-related command-line options. */
+ for (int i = 1; i < argc; i++)
+ {
+ const char *arg = argv[i];
+
+ if (strncmp (arg, "--color=", 8) == 0)
+ {
+ if (handle_color_option (arg + 8))
+ exit (EXIT_FAILURE);
+ }
+ else if (strncmp (arg, "--style=", 8) == 0)
+ handle_style_option (arg + 8);
+ }
+
+ /* Handle the --color=test special argument. */
+ if (color_test_mode)
+ {
+ print_color_test ();
+ exit (EXIT_SUCCESS);
+ }
+
+ /* Note that the following code needs to be compiled conditionally
+ because the textstyle.h file provided by the gnulib module
+ libtextstyle-optional defines style_file_name as an r-value. */
+
+#ifdef HAVE_LIBTEXTSTYLE
+ /* Open the specified style. */
+ if (color_mode == color_yes
+ || (color_mode == color_tty
+ && isatty (STDOUT_FILENO)
+ && getenv ("NO_COLOR") == NULL)
+ || color_mode == color_html)
+ {
+ /* Find the style file. */
+ style_file_prepare ("POKE_STYLE", "POKESTYLESDIR", PKGDATADIR,
+ "poke-default.css");
+ }
+ else
+ /* No styling. */
+ style_file_name = NULL;
+#endif
+
+ /* Create the output styled stream. */
+ return
+ (color_mode == color_html
+ ? html_styled_ostream_create (file_ostream_create (stdout),
+ style_file_name)
+ : styled_ostream_create (STDOUT_FILENO, "(stdout)",
+ TTYCTL_AUTO, style_file_name));
+}
+
static void
initialize (int argc, char *argv[])
{
@@ -392,7 +457,7 @@ initialize (int argc, char *argv[])
poke_infodir = PKGINFODIR;
/* Initialize the terminal output. */
- pk_term_init (argc, argv);
+ pk_term_init (term_init (argc, argv));
/* Initialize the Poke Virtual Machine. Note this should be done
before initializing the compiler, since the later constructs and
@@ -411,7 +476,7 @@ initialize (int argc, char *argv[])
ios_init ();
#ifdef HAVE_HSERVER
- poke_hserver_p = poke_interactive_p && pk_term_color_p ();
+ poke_hserver_p = poke_interactive_p && term_color_p ();
/* Initialize and start the terminal hyperlinks server. */
if (poke_hserver_p)