field.c
5.19 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* Copyright (C) 2023 Perry Werneck <perry.werneck@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <internals.h>
#include <lib3270.h>
#include "3270ds.h"
/**
* @brief Get field address.
*
* @return Negative on error(sets errno) or field address.
*
*/
LIB3270_EXPORT int lib3270_get_field_start(H3270 *hSession, int baddr) {
int sbaddr;
if(check_online_session(hSession))
return - errno;
if (!hSession->formatted)
return - (errno = ENOTSUP);
if(baddr < 0)
baddr = hSession->cursor_addr;
sbaddr = baddr;
do {
if(hSession->ea_buf[baddr].fa)
return baddr;
DEC_BA(baddr);
} while (baddr != sbaddr);
return -1;
}
LIB3270_EXPORT int lib3270_is_formatted(const H3270 *hSession) {
if(check_online_session(hSession))
return 0;
return hSession->formatted ? 1 : 0;
}
LIB3270_EXPORT int lib3270_get_field_len(H3270 *hSession, int baddr) {
int saddr;
int addr;
int width = 0;
if(check_online_session(hSession))
return - errno;
if (!hSession->formatted)
return - (errno = ENOTSUP);
if(baddr < 0)
baddr = hSession->cursor_addr;
addr = lib3270_field_addr(hSession,baddr);
if(addr < 0)
return addr;
saddr = addr;
INC_BA(addr);
do {
if(hSession->ea_buf[addr].fa)
return width;
INC_BA(addr);
width++;
} while (addr != saddr);
return -(errno = ENODATA);
}
LIB3270_EXPORT int lib3270_field_addr(const H3270 *hSession, int baddr) {
int sbaddr;
if(!lib3270_is_connected(hSession))
return -(errno = ENOTCONN);
if(!hSession->formatted)
return -(errno = ENOTSUP);
if(baddr < 0)
baddr = lib3270_get_cursor_address(hSession);
if( (unsigned int) baddr > lib3270_get_length(hSession))
return -(errno = EOVERFLOW);
sbaddr = baddr;
do {
if(hSession->ea_buf[baddr].fa)
return baddr;
DEC_BA(baddr);
} while (baddr != sbaddr);
return -(errno = ENODATA);
}
LIB3270_EXPORT LIB3270_FIELD_ATTRIBUTE lib3270_get_field_attribute(H3270 *hSession, int baddr) {
int sbaddr;
FAIL_IF_NOT_ONLINE(hSession);
if(!hSession->formatted) {
errno = ENOTCONN;
return LIB3270_FIELD_ATTRIBUTE_INVALID;
}
if(baddr < 0)
baddr = lib3270_get_cursor_address(hSession);
sbaddr = baddr;
do {
if(hSession->ea_buf[baddr].fa)
return (LIB3270_FIELD_ATTRIBUTE) hSession->ea_buf[baddr].fa;
DEC_BA(baddr);
} while (baddr != sbaddr);
errno = EINVAL;
return LIB3270_FIELD_ATTRIBUTE_INVALID;
}
/**
* @brief Get the length of the field at given buffer address.
*
* @param hSession Session handle.
* @param addr Buffer address of the field.
*
* @return field length or negative if invalid or not connected (sets errno).
*
*/
int lib3270_field_length(H3270 *hSession, int baddr) {
int saddr;
int addr;
int width = 0;
addr = lib3270_field_addr(hSession,baddr);
if(addr < 0)
return addr;
saddr = addr;
INC_BA(addr);
do {
if(hSession->ea_buf[addr].fa)
return width;
INC_BA(addr);
width++;
} while (addr != saddr);
return -(errno = EINVAL);
}
/**
* @brief Find the field attribute for the given buffer address.
*
* @return Field attribute.
*
*/
unsigned char get_field_attribute(H3270 *hSession, int baddr) {
baddr = lib3270_field_addr(hSession,baddr);
if(baddr < 0)
return 0;
return hSession->ea_buf[baddr].fa;
}
/**
* @brief Find the next unprotected field.
*
* @param hSession Session handle.
* @param baddr0 Search start addr (-1 to use current cursor position).
*
* @return address following the unprotected attribute byte, or 0 if no nonzero-width unprotected field can be found, negative if failed.
*
*/
LIB3270_EXPORT int lib3270_get_next_unprotected(H3270 *hSession, int baddr0) {
register int baddr, nbaddr;
FAIL_IF_NOT_ONLINE(hSession);
if(!hSession->formatted)
return -(errno = ENOTSUP);
if(baddr0 < 0)
baddr0 = hSession->cursor_addr;
nbaddr = baddr0;
do {
baddr = nbaddr;
INC_BA(nbaddr);
if(hSession->ea_buf[baddr].fa &&!FA_IS_PROTECTED(hSession->ea_buf[baddr].fa) &&!hSession->ea_buf[nbaddr].fa)
return nbaddr;
} while (nbaddr != baddr0);
return 0;
}
LIB3270_EXPORT int lib3270_get_is_protected_at(const H3270 *h, unsigned int row, unsigned int col) {
return lib3270_get_is_protected(h, lib3270_translate_to_address(h,row,col));
}
LIB3270_EXPORT int lib3270_get_is_protected(const H3270 *hSession, int baddr) {
FAIL_IF_NOT_ONLINE(hSession);
if(baddr < 0)
baddr = hSession->cursor_addr;
int faddr = lib3270_field_addr(hSession,baddr);
return FA_IS_PROTECTED(hSession->ea_buf[faddr].fa) ? 1 : 0;
}
LIB3270_EXPORT int lib3270_is_protected(H3270 *h, unsigned int baddr) {
return lib3270_get_is_protected(h, baddr);
}