1 /****************************************************************************
2 * Copyright 2018-2021,2024 Thomas E. Dickey *
3 * Copyright 2008-2012,2016 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /***************************************************************************
31 * *
32 * Author : Juergen Pfeifer *
33 * *
34 ***************************************************************************/
35
36 #include "form.priv.h"
37
38 MODULE_ID("$Id: fty_generic.c,v 1.17 2024/12/07 23:12:11 tom Exp $")
39
40 /*
41 * This is not a full implementation of a field type, but adds some
42 * support for higher level languages with some restrictions to interop
43 * with C language. In particular, the collection of arguments for the
44 * various fieldtypes is not based on the vararg C mechanism, but on a
45 * iterator based callback mechanism that allows the high level language
46 * to provide the arguments as a structure. Most languages have mechanisms
47 * to layout structures so that they can be passed to C.
48 *
49 * The languages can register a new generic fieldtype dynamically and store
50 * a handle (key) to the calling object as an argument. Together with that
51 * it can register a freearg callback, so that the high level language
52 * remains in control of the memory management of the arguments they pass.
53 * The design idea is, that the high-level language - typically a OO
54 * language like C# or Java, uses its own dispatching mechanisms
55 * (polymorphism) to call the proper check routines responsible for the
56 * argument type. So these language implement typically only one generic
57 * fieldtype they register with the forms library using this call.
58 *
59 * For that purpose we have extended the fieldtype structure by a new element
60 * that gets the arguments from a single struct passed by the caller.
61 *
62 */
63 #if NCURSES_INTEROP_FUNCS
64
65 /*---------------------------------------------------------------------------
66 | Facility : libnform
67 | Function : static void *Generic_This_Type( void * arg )
68 |
69 | Description : We interpret the passed arg just as a handle the
70 | calling language uses to keep track of its allocated
71 | argument structures. We can simply copy it back.
72 |
73 | Return Values : Pointer to argument structure
74 +--------------------------------------------------------------------------*/
75 static void *
Generic_This_Type(void * arg)76 Generic_This_Type(void *arg)
77 {
78 return (arg);
79 }
80
81 /*---------------------------------------------------------------------------
82 | Facility : libnform
83 | Function : FIELDTYPE *_nc_generic_fieldtype(
84 | bool (* const field_check)(FIELD *,const void *),
85 | bool (* const char_check) (int, const void *),
86 | bool (*const next)(FORM*,FIELD*,const void*),
87 | bool (*const prev)(FORM*,FIELD*,const void*),
88 | void (*freecallback)(void*))
89 |
90 | Description : Create a new fieldtype. The application programmer must
91 | write a field_check and a char_check function and give
92 | them as input to this call. A callback to allow the
93 | release of the allocated memory must also be provided.
94 | For generic field types, we provide some more
95 | information about the field as parameters.
96 |
97 | If an error occurs, errno is set to
98 | E_BAD_ARGUMENT - invalid arguments
99 | E_SYSTEM_ERROR - system error (no memory)
100 |
101 | Return Values : Fieldtype pointer or NULL if error occurred
102 +--------------------------------------------------------------------------*/
103 FORM_EXPORT(FIELDTYPE *)
_nc_generic_fieldtype(bool (* const field_check)(FORM *,FIELD *,const void *),bool (* const char_check)(int,FORM *,FIELD *,const void *),bool (* const next)(FORM *,FIELD *,const void *),bool (* const prev)(FORM *,FIELD *,const void *),void (* freecallback)(void *))104 _nc_generic_fieldtype(bool (*const field_check) (FORM *, FIELD *, const void *),
105 bool (*const char_check) (int, FORM *, FIELD *, const
106 void *),
107 bool (*const next) (FORM *, FIELD *, const void *),
108 bool (*const prev) (FORM *, FIELD *, const void *),
109 void (*freecallback) (void *))
110 {
111 int code = E_SYSTEM_ERROR;
112 FIELDTYPE *res = (FIELDTYPE *)0;
113
114 TR_FUNC_BFR(5);
115
116 T((T_CALLED("_nc_generic_fieldtype(%s,%s,%s,%s,%s)"),
117 TR_FUNC_ARG(0, field_check),
118 TR_FUNC_ARG(1, char_check),
119 TR_FUNC_ARG(2, next),
120 TR_FUNC_ARG(3, prev),
121 TR_FUNC_ARG(4, freecallback)));
122
123 if (field_check || char_check)
124 {
125 res = typeMalloc(FIELDTYPE, 1);
126
127 if (res)
128 {
129 *res = *_nc_Default_FieldType;
130 SetStatus(res, (_HAS_ARGS | _GENERIC));
131 res->fieldcheck.gfcheck = field_check;
132 res->charcheck.gccheck = char_check;
133 res->genericarg = Generic_This_Type;
134 res->freearg = freecallback;
135 res->enum_next.gnext = next;
136 res->enum_prev.gprev = prev;
137 code = E_OK;
138 }
139 }
140 else
141 code = E_BAD_ARGUMENT;
142
143 if (E_OK != code)
144 SET_ERROR(code);
145
146 returnFieldType(res);
147 }
148
149 /*---------------------------------------------------------------------------
150 | Facility : libnform
151 | Function : static TypeArgument *GenericArgument(
152 | const FIELDTYPE* typ,
153 | int (*argiterator)(void**),
154 | int* err)
155 |
156 | Description : The iterator callback must browse through all fieldtype
157 | parameters that have an argument associated with the
158 | type. The iterator returns 1 if the operation to get
159 | the next element was successful, 0 otherwise. If the
160 | iterator could move to the next argument, it fills
161 | the void* pointer representing the argument into the
162 | location provided as argument to the iterator.
163 | The err reference is used to keep track of errors.
164 |
165 | Return Values : Pointer to argument structure
166 +--------------------------------------------------------------------------*/
167 static TypeArgument *
GenericArgument(const FIELDTYPE * typ,int (* argiterator)(void **),int * err)168 GenericArgument(const FIELDTYPE *typ,
169 int (*argiterator) (void **), int *err)
170 {
171 TypeArgument *res = (TypeArgument *)0;
172
173 if (typ != NULL
174 && (typ->status & _HAS_ARGS) != 0
175 && err != NULL
176 && argiterator != NULL)
177 {
178 if (typ->status & _LINKED_TYPE)
179 {
180 /* Composite fieldtypes keep track internally of their own memory */
181 TypeArgument *p = typeMalloc(TypeArgument, 1);
182
183 if (p)
184 {
185 p->left = GenericArgument(typ->left, argiterator, err);
186 p->right = GenericArgument(typ->right, argiterator, err);
187 return p;
188 }
189 else
190 *err += 1;
191 }
192 else
193 {
194 assert(typ->genericarg != (void *)0);
195 if (typ->genericarg == NULL)
196 *err += 1;
197 else
198 {
199 void *argp;
200 int valid = argiterator(&argp);
201
202 if (valid == 0 || argp == NULL ||
203 !(res = (TypeArgument *)typ->genericarg(argp)))
204 {
205 *err += 1;
206 }
207 }
208 }
209 }
210 return res;
211 }
212
213 /*---------------------------------------------------------------------------
214 | Facility : libnform
215 | Function : int _nc_set_generic_fieldtype(
216 | FIELD* field,
217 | FIELDTYPE* ftyp,
218 | int (*argiterator)(void**))
219 |
220 | Description : Assign the fieldtype to the field and use the iterator
221 | mechanism to get the arguments when a check is
222 | performed.
223 |
224 | Return Values : E_OK if all went well
225 | E_SYSTEM_ERROR if an error occurred
226 +--------------------------------------------------------------------------*/
227 FORM_EXPORT(int)
_nc_set_generic_fieldtype(FIELD * field,FIELDTYPE * ftyp,int (* argiterator)(void **))228 _nc_set_generic_fieldtype(FIELD *field,
229 FIELDTYPE *ftyp,
230 int (*argiterator) (void **))
231 {
232 int code = E_SYSTEM_ERROR;
233 int err = 0;
234
235 if (field)
236 {
237 if (field->type)
238 _nc_Free_Type(field);
239
240 field->type = ftyp;
241 if (ftyp)
242 {
243 if (argiterator)
244 {
245 /* The precondition is that the iterator is reset */
246 field->arg = (void *)GenericArgument(field->type, argiterator, &err);
247
248 if (err)
249 {
250 _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
251 field->type = (FIELDTYPE *)0;
252 field->arg = (void *)0;
253 }
254 else
255 {
256 code = E_OK;
257 if (field->type)
258 field->type->ref++;
259 }
260 }
261 }
262 else
263 {
264 field->arg = (void *)0;
265 code = E_OK;
266 }
267 }
268 return code;
269 }
270
271 /*---------------------------------------------------------------------------
272 | Facility : libnform
273 | Function : WINDOW* _nc_form_cursor(
274 | FORM* form,
275 | int *pRow, int *pCol)
276 |
277 | Description : Get the current position of the form cursor position
278 | We also return the field window
279 |
280 | Return Values : The field's Window or NULL on error
281 +--------------------------------------------------------------------------*/
282 FORM_EXPORT(WINDOW *)
_nc_form_cursor(const FORM * form,int * pRow,int * pCol)283 _nc_form_cursor(const FORM *form, int *pRow, int *pCol)
284 {
285 int code = E_SYSTEM_ERROR;
286 WINDOW *res = (WINDOW *)0;
287
288 if (form != NULL && pRow != NULL && pCol != NULL)
289 {
290 *pRow = form->currow;
291 *pCol = form->curcol;
292 res = form->w;
293 code = E_OK;
294 }
295 if (code != E_OK)
296 SET_ERROR(code);
297 return res;
298 }
299
300 #else
301 extern void _nc_fty_generic(void);
302 void
303 _nc_fty_generic(void)
304 {
305 }
306 #endif
307
308 /* fty_generic.c ends here */
309