File: tc.cxx

package info (click to toggle)
curves 0.8.7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 704 kB
  • ctags: 1,001
  • sloc: cpp: 6,197; ansic: 519; makefile: 270; sh: 192; fortran: 149
file content (145 lines) | stat: -rw-r--r-- 3,911 bytes parent folder | download | duplicates (2)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* tc.cxx
     $Id: tc.cxx,v 1.3 1998/09/07 00:49:37 elf Exp $

   written by Marc Singer
   21 December 1996

   This file is part of the project CurVeS.  See the file README for
   more information.

   Copyright (C) 1996,1997,1998 Marc Singer

   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 2 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
   in a file called COPYING along with this program; if not, write to
   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
   02139, USA.

   -----------
   DESCRIPTION
   -----------

*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <malloc.h>

typedef struct {
  int type;
  int id;
  const char* sz;
} TERMINFO_FIELD;

#define __(t,i,s) { t, i, s },
TERMINFO_FIELD g_rgTerminfoField[] = {
# include "tc.d"
  { 0 }
#undef __
};

const char* name_field (int type, int id)
{
  for (TERMINFO_FIELD* pField = g_rgTerminfoField; pField->type; ++pField)
    if (pField->type == type && pField->id == id)
      return pField->sz;
  return NULL;
}

int main (int argc, char** argv)
{
  const char* szTerm = getenv ("TERM");
  if (argc > 1)
    szTerm = argv[1];

  char sz[200];
  sprintf (sz, "/usr/lib/terminfo/%c/%s", szTerm[0], szTerm);
  struct stat stat;
  if (::stat (sz, &stat)) {
    printf ("file '%s' not found\n", sz);
    exit (0);
  }
  long cb = stat.st_size;
  char* pb = (char*) malloc (cb + 1);
  pb[cb] = 0;

  int fh = open (sz, O_RDONLY);
  read (fh, pb, cb);
  close (fh);
  
  int cbName;
  int cBoolean;
  int cNum;
  int cStrings;
  int cbStrings;
  int cbHeader = 6*2;		// Size of header is just these six shorts
  printf ("Examining terminfo '%s'\n", sz);
  printf ("header:\n magic (0432, 282) %d\n"
	  " cbNames %d  cBoolean %d  cNum %d  cStrings %d  "
	  "cbStrings %d\n", 
	  pb[0] + pb[1]*256,
	  cbName = pb[2] + pb[3]*256,
	  cBoolean = pb[4] + pb[5]*256,
	  cNum = pb[6] + pb[7]*256,
	  cStrings = pb[8] + pb[9]*256,
	  cbStrings = pb[10] + pb[11]*256);
  printf ("name:\n '%s'\n", pb + cbHeader);

  int ibBoolean = cbHeader + cbName;
  int ibNum = ((ibBoolean + cBoolean + 1) & ~1);
  int ibStrings = ibNum + cNum*2;
  int ibStringTable = ibStrings + cStrings*2;

  printf ("offsets:\n"
	  " ibBoolean 0x%x  ibNum 0x%x  ibStrings 0x%x  ibStringTable 0x%x\n",
	  ibBoolean, ibNum, ibStrings, ibStringTable);

  for (int i = 0; i < cBoolean; ++i)
    printf ("bol %3d (%-20.20s): %s\n", i, name_field (1, i),
	    pb[ibBoolean + i] ? "true" : "false");

  for (int i = 0; i < cNum; ++i) {
    int value = pb[ibNum + i*2] + pb[ibNum + i*2 + 1]*256;
    printf ("num %3d (%-20.20s): %d (0x%x)\n", i, name_field (2, i),
	    value, value);
  }

  for (int i = 0; i < cStrings; ++i) {
    int ib = (unsigned char)  pb[ibStrings + i*2]
      | (unsigned) ((unsigned char) pb[ibStrings + i*2 + 1] << 8);
    char* sz;
    if ((ib & 0xffff) != 0xffff)
      sz = pb + ibStringTable + ib;
    else
      sz = NULL;
    if (sz == NULL)		// Enhancement to eliminate blank string items
      continue;
    printf ("str %3d (%-20.20s): %s", i, name_field (4, i),
	    sz ? "'" : "NULL");
    for (; sz && *sz; ++sz)
      if (*sz == 27)
	printf ("\\E");
      else if (*sz < 32 && *sz >= 0)
	printf ("^%c", *sz + '@');
      else if (*sz > 126 || *sz < 0)
	printf ("\\x%02x", unsigned (*sz & 0xff));
      else
	printf ("%c", *sz);
    printf ("%s\n", sz ? "'" : "");
  }

  free (pb);
  exit (0);
}