GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcurses/tinfo/make_keys.c Lines: 36 40 90.0 %
Date: 2017-11-07 Branches: 21 28 75.0 %

Line Branch Exec Source
1
/* $OpenBSD: make_keys.c,v 1.9 2010/01/12 23:22:06 nicm Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5
 *                                                                          *
6
 * Permission is hereby granted, free of charge, to any person obtaining a  *
7
 * copy of this software and associated documentation files (the            *
8
 * "Software"), to deal in the Software without restriction, including      *
9
 * without limitation the rights to use, copy, modify, merge, publish,      *
10
 * distribute, distribute with modifications, sublicense, and/or sell       *
11
 * copies of the Software, and to permit persons to whom the Software is    *
12
 * furnished to do so, subject to the following conditions:                 *
13
 *                                                                          *
14
 * The above copyright notice and this permission notice shall be included  *
15
 * in all copies or substantial portions of the Software.                   *
16
 *                                                                          *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24
 *                                                                          *
25
 * Except as contained in this notice, the name(s) of the above copyright   *
26
 * holders shall not be used in advertising or otherwise to promote the     *
27
 * sale, use or other dealings in this Software without prior written       *
28
 * authorization.                                                           *
29
 ****************************************************************************/
30
31
/****************************************************************************
32
 *  Author: Thomas E. Dickey                    1997-on                     *
33
 ****************************************************************************/
34
35
/*
36
 * This replaces an awk script which translated keys.list into keys.tries by
37
 * making the output show the indices into the TERMTYPE Strings array.  Doing
38
 * it that way lets us cut down on the size of the init_keytry() function.
39
 */
40
41
#define USE_TERMLIB 1
42
#include <curses.priv.h>
43
44
MODULE_ID("$Id: make_keys.c,v 1.9 2010/01/12 23:22:06 nicm Exp $")
45
46
#include <names.c>
47
48
#define UNKNOWN (SIZEOF(strnames) + SIZEOF(strfnames))
49
50
static size_t
51
lookup(const char *name)
52
{
53
    size_t n;
54
    bool found = FALSE;
55
249300
    for (n = 0; strnames[n] != 0; n++) {
56
124200
	if (!strcmp(name, strnames[n])) {
57
	    found = TRUE;
58
	    break;
59
	}
60
    }
61
300
    if (!found) {
62
108516
	for (n = 0; strfnames[n] != 0; n++) {
63
54258
	    if (!strcmp(name, strfnames[n])) {
64
		found = TRUE;
65
300
		break;
66
	    }
67
	}
68
    }
69
300
    return found ? n : UNKNOWN;
70
}
71
72
static void
73
make_keys(FILE *ifp, FILE *ofp)
74
{
75
4
    char buffer[BUFSIZ];
76
2
    char from[256];
77
2
    char to[256];
78
    int maxlen = 16;
79
    int scanned;
80
81
314
    while (fgets(buffer, sizeof(buffer), ifp) != NULL) {
82
310
	if (*buffer == '#')
83
	    continue;
84
85
308
	to[sizeof(to) - 1] = '\0';
86
308
	from[sizeof(from) - 1] = '\0';
87
88
308
	scanned = sscanf(buffer, "%255s %255s", to, from);
89
308
	if (scanned == 2) {
90
300
	    int code = lookup(from);
91
300
	    if (code == UNKNOWN)
92
		continue;
93
300
	    if ((int) strlen(from) > maxlen)
94
		maxlen = strlen(from);
95
300
	    fprintf(ofp, "\t{ %4d, %-*.*s },\t/* %s */\n",
96
		    code,
97
		    maxlen, maxlen,
98
		    to,
99
		    from);
100
300
	}
101
    }
102
2
}
103
104
static void
105
write_list(FILE *ofp, const char **list)
106
{
107
60
    while (*list != 0)
108
24
	fprintf(ofp, "%s\n", *list++);
109
4
}
110
111
int
112
main(int argc, char *argv[])
113
{
114
    static const char *prefix[] =
115
    {
116
	"#ifndef NCU_KEYS_H",
117
	"#define NCU_KEYS_H 1",
118
	"",
119
	"/* This file was generated by MAKE_KEYS */",
120
	"",
121
	"#if BROKEN_LINKER",
122
	"static",
123
	"#endif",
124
	"const struct tinfo_fkeys _nc_tinfo_fkeys[] = {",
125
	0
126
    };
127
    static const char *suffix[] =
128
    {
129
	"\t{ 0, 0} };",
130
	"",
131
	"#endif /* NCU_KEYS_H */",
132
	0
133
    };
134
135
4
    write_list(stdout, prefix);
136
2
    if (argc > 1) {
137
	int n;
138
8
	for (n = 1; n < argc; n++) {
139
2
	    FILE *fp = fopen(argv[n], "r");
140
2
	    if (fp != 0) {
141
2
		make_keys(fp, stdout);
142
2
		fclose(fp);
143
2
	    }
144
	}
145
2
    } else {
146
	make_keys(stdin, stdout);
147
    }
148
2
    write_list(stdout, suffix);
149
2
    return EXIT_SUCCESS;
150
}