File: readres.c

package info (click to toggle)
ibm-3270 4.0ga12-3
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 17,740 kB
  • sloc: ansic: 120,111; sh: 4,284; makefile: 822; pascal: 798; perl: 344; exp: 184; tcl: 94
file content (211 lines) | stat: -rw-r--r-- 5,629 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright (c) 2009, 2013-2016, 2018 Paul Mattes.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the names of Paul Mattes, Jeff Sparkes, GTRC nor the names of
 *       their contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *	readres.c
 *		A displayless 3270 Terminal Emulator
 *		Resource file reader.
 */

#include "globals.h"

#include <errno.h>

#include "appres.h"
#include "glue.h"
#include "readres.h"
#include "utils.h"

/*
 * Make sure a resource definition begins with the application name, then
 * split it into the name and the value.
 */
int
validate_and_split_resource(const char *where, const char *arg,
	const char **left, size_t *rnlenp, const char **right)
{
    size_t match_len;
    size_t rnlen;
    const char *s = arg;
    static char *me_dot = NULL;
    static char *me_star = NULL;
    static size_t me_len = 0;
    static char *alias_dot = NULL;
    static char *alias_star = NULL;
    static size_t alias_len = 0;
    static char *or_alias = "";

    if (me_dot == NULL) {
	me_dot = xs_buffer("%s.", app);
	me_star = xs_buffer("%s*", app);
	me_len = strlen(me_dot);
	if (appres.alias != NULL) {
	    alias_dot = xs_buffer("%s.", appres.alias);
	    alias_star = xs_buffer("%s*", appres.alias);
	    alias_len = strlen(alias_dot);
	    if (strcmp(app, appres.alias)) {
		or_alias = xs_buffer(" or '%s'", alias_dot);
	    }
	}
    }

    /* Enforce "-3270." or "-3270*" or "*". */
    if (!strncmp(s, me_dot, me_len)) {
	match_len = me_len;
    } else if (!strncmp(arg, me_star, me_len)) {
	match_len = me_len;
    } else if (alias_len != 0 && !strncmp(s, alias_dot, alias_len)) {
	match_len = alias_len;
    } else if (alias_len != 0 && !strncmp(arg, alias_star, alias_len)) {
	match_len = alias_len;
    } else if (arg[0] == '*') {
	match_len = 1;
    } else {
	xs_warning("%s: Invalid resource syntax '%.*s', name must begin with "
		"'%s', '*'%s", where, (int)me_len, arg, me_dot, or_alias);
	return -1;
    }

    /* Separate the parts. */
    s = arg + match_len;
    while (*s && *s != ':' && !isspace((unsigned char)*s)) {
	s++;
    }
    rnlen = s - (arg + match_len);
    if (!rnlen) {
	xs_warning("%s: Invalid resource syntax, missing resource name",
		where);
	return -1;
    }
    while (isspace((unsigned char)*s)) {
	s++;
    }
    if (*s != ':') {
	xs_warning("%s: Invalid resource syntax, missing ':'", where);
	return -1;
    }
    s++;
    while (isspace((unsigned char)*s)) {
	s++;
    }

    /* Return what we got. */
    *left = arg + match_len;
    *rnlenp = rnlen;
    *right = s;
    return 0;
}

/* Read resources from a file. */
bool
read_resource_filex(const char *filename, bool fatal)
{
    FILE *f;
    size_t ilen;
    char buf[4096];
    char *where;
    int lno = 0;

    f = fopen(filename, "r");
    if (f == NULL) {
	if (fatal) {
	    xs_warning("Cannot open '%s': %s", filename, strerror(errno));
	}
	return false;
    }

    /* Merge in what's in the file into the resource database. */
    ilen = 0;
    while (fgets(buf + ilen, (int)(sizeof(buf) - ilen), f) != NULL ||
	    ilen) {
	char *s;
	size_t sl;
	bool bsl = false;

	lno++;

	/* Stip any trailing newline. */
	sl = strlen(buf + ilen);
	if (sl && (buf + ilen)[sl-1] == '\n') {
	    (buf + ilen)[--sl] = '\0';
	}

	/* Check for a trailing backslash. */
	s = buf + ilen;
	if ((sl > 0) && (s[sl - 1] == '\\')) {
	    s[sl - 1] = '\0';
	    bsl = true;
	}

	/* Skip leading whitespace. */
	s = buf;
	while (isspace((unsigned char)*s)) {
	    s++;
	}

	/* If this line is a continuation, try again. */
	if (bsl) {
	    ilen += strlen(buf + ilen);
	    if ((unsigned)ilen >= sizeof(buf) - 1) {
		xs_warning("%s:%d: Line too long\n", filename, lno);
		break;
	    }
	    continue;
	}

	/* Skip comments. */
	if (*s == '!') {
	    ilen = 0;
	    continue;
	}
	if (*s == '#') {
	    xs_warning("%s:%d: Invalid profile syntax ('#' ignored)", filename,
		    lno);
	    ilen = 0;
	    continue;
	}

	/* Strip trailing whitespace and check for empty lines. */
	sl = strlen(s);
	while (sl && isspace((unsigned char)s[sl-1])) {
	    s[--sl] = '\0';
	}
	if (!sl) {
	    ilen = 0;
	    continue;
	}

	/* Digest it. */
	where = xs_buffer("%s:%d", filename, lno);
	parse_xrm(s, where);
	Free(where);

	/* Get ready for the next iteration. */
	ilen = 0;
    }
    fclose(f);
    return true;
}