• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Ad-hoc testing program for GNU libtextstyle.
2    Copyright (C) 2018-2019 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2018.
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17 
18 #include <config.h>
19 
20 #include <textstyle.h>
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 int
main(int argc,char * argv[])28 main (int argc, char *argv[])
29 {
30   const char *program_name = argv[0];
31   int i;
32 
33   /* Parse the command-line arguments.  */
34   for (i = 1; i < argc; i++)
35     {
36       const char *arg = argv[i];
37       if (strncmp (arg, "--color=", 8) == 0)
38         handle_color_option (arg + 8);
39       else if (strncmp (arg, "--style=", 8) == 0)
40         handle_style_option (arg + 8);
41       else if (arg[0] == '-')
42         {
43           fprintf (stderr, "%s: invalid argument: %s\n", program_name, arg);
44           exit (1);
45         }
46       else
47         /* Handle non-option arguments here.  */
48         ;
49     }
50 
51   /* Handle the --color=test special argument.  */
52   if (color_test_mode)
53     {
54       print_color_test ();
55       exit (0);
56     }
57 
58   if (color_mode == color_yes
59       || (color_mode == color_tty
60           && isatty (STDOUT_FILENO)
61           && getenv ("NO_COLOR") == NULL)
62       || color_mode == color_html)
63     {
64       /* If no style file is explicitly specified, use the default in the
65          source directory.  */
66       if (style_file_name == NULL)
67         style_file_name = SRCDIR "hello-default.css";
68     }
69   else
70     /* No styling.  */
71     style_file_name = NULL;
72 
73   /* Create a terminal output stream that uses this style file.  */
74   styled_ostream_t stream =
75     (color_mode == color_html
76      ? html_styled_ostream_create (file_ostream_create (stdout),
77                                    style_file_name)
78      : styled_ostream_create (STDOUT_FILENO, "(stdout)", TTYCTL_AUTO,
79                               style_file_name));
80 
81   ostream_write_str (stream, "Hello ");
82 
83   /* Associate the entire full name in CSS class 'name'.  */
84   styled_ostream_begin_use_class (stream, "name");
85 
86   ostream_write_str (stream, "Dr. ");
87   styled_ostream_begin_use_class (stream, "boy-name");
88   /* Start a hyperlink.  */
89   styled_ostream_set_hyperlink (stream, "https://en.wikipedia.org/wiki/Linus_Pauling", NULL);
90   ostream_write_str (stream, "Linus");
91   styled_ostream_end_use_class (stream, "boy-name");
92   ostream_write_str (stream, " Pauling");
93   /* End the current hyperlink.  */
94   styled_ostream_set_hyperlink (stream, NULL, NULL);
95 
96   /* Terminate the name.  */
97   styled_ostream_end_use_class (stream, "name");
98 
99   ostream_write_str (stream, "!\n");
100 
101   /* Flush and close the terminal stream.  */
102   styled_ostream_free (stream);
103 
104   return 0;
105 }
106