summaryrefslogtreecommitdiff
path: root/tests/test-termcap.c
blob: ab4062169e7b433486b34f5432c9c473c4d7af50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Ad-hoc testing program for <termcap.h>.
   Copyright (C) 2022-2023 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include "termcap.h"

#include <stdio.h>
/* Get getenv().  */
#include <stdlib.h>
/* Get STDOUT_FILENO.  */
#include <unistd.h>

int
main (int argc, char *argv[])
{
  const char *underline_on = NULL;
  const char *underline_off = NULL;
  const char *bold_on = NULL;
  const char *invert_on = NULL;
  const char *attributes_off = NULL;

  {
    const char *term = getenv ("TERM");
    if (term != NULL && *term != '\0')
      {
        #if HAVE_TERMCAP
        static char tbuf[2048];
        if (tgetent (tbuf, term) > 0)
          {
            underline_on = tgetstr ("us", NULL);
            underline_off = tgetstr ("ue", NULL);
            bold_on = tgetstr ("md", NULL);
            invert_on = tgetstr ("mr", NULL);
            attributes_off = tgetstr ("me", NULL);
          }
        #elif HAVE_TERMINFO
        int err = 1;
        if (setupterm (term, STDOUT_FILENO, &err) == 0 || err == 1)
          {
            underline_on = tigetstr ("smul");
            underline_off = tigetstr ("rmul");
            bold_on = tigetstr ("bold");
            invert_on = tigetstr ("rev");
            attributes_off = tigetstr ("sgr0");
          }
        #endif
      }
  }

  if (bold_on) tputs (bold_on, 1, putchar);
  printf ("#include");
  if (attributes_off) tputs (attributes_off, 1, putchar);
  printf (" <stdio.h>\n");
  if (underline_on) tputs (underline_on, 1, putchar);
  printf ("int");
  if (underline_off) tputs (underline_off, 1, putchar);
  printf ("\n");
  if (invert_on) tputs (invert_on, 1, putchar);
  printf ("main");
  if (attributes_off) tputs (attributes_off, 1, putchar);
  printf (" ()\n");
  printf ("{\n");
  printf ("  printf (\"Hello world\\n\");\n");
  printf ("  ");
  if (bold_on) tputs (bold_on, 1, putchar);
  printf ("return");
  if (attributes_off) tputs (attributes_off, 1, putchar);
  printf (" 0;\n");
  printf ("}\n");

  return 0;
}