1 /* $OpenBSD: fld_newftyp.c,v 1.8 2023/10/17 09:52:10 nicm Exp $ */
2 /****************************************************************************
3 * Copyright 2018,2020 Thomas E. Dickey *
4 * Copyright 1998-2010,2016 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: Juergen Pfeifer, 1995,1997 *
33 ****************************************************************************/
34
35 #include "form.priv.h"
36
37 MODULE_ID("$Id: fld_newftyp.c,v 1.8 2023/10/17 09:52:10 nicm Exp $")
38
39 static FIELDTYPE default_fieldtype =
40 {
41 0, /* status */
42 0L, /* reference count */
43 (FIELDTYPE *)0, /* pointer to left operand */
44 (FIELDTYPE *)0, /* pointer to right operand */
45 NULL, /* makearg function */
46 NULL, /* copyarg function */
47 NULL, /* freearg function */
48 INIT_FT_FUNC(NULL), /* field validation function */
49 INIT_FT_FUNC(NULL), /* Character check function */
50 INIT_FT_FUNC(NULL), /* enumerate next function */
51 INIT_FT_FUNC(NULL), /* enumerate previous function */
52 #if NCURSES_INTEROP_FUNCS
53 NULL /* generic callback alternative to makearg */
54 #endif
55 };
56
57 FORM_EXPORT_VAR(FIELDTYPE *)
58 _nc_Default_FieldType = &default_fieldtype;
59
60 /*---------------------------------------------------------------------------
61 | Facility : libnform
62 | Function : FIELDTYPE *new_fieldtype(
63 | bool (* const field_check)(FIELD *,const void *),
64 | bool (* const char_check) (int, const void *) )
65 |
66 | Description : Create a new fieldtype. The application programmer must
67 | write a field_check and a char_check function and give
68 | them as input to this call.
69 | If an error occurs, errno is set to
70 | E_BAD_ARGUMENT - invalid arguments
71 | E_SYSTEM_ERROR - system error (no memory)
72 |
73 | Return Values : Fieldtype pointer or NULL if error occurred
74 +--------------------------------------------------------------------------*/
75 FORM_EXPORT(FIELDTYPE *)
new_fieldtype(bool (* const field_check)(FIELD *,const void *),bool (* const char_check)(int,const void *))76 new_fieldtype(bool (*const field_check) (FIELD *, const void *),
77 bool (*const char_check) (int, const void *))
78 {
79 FIELDTYPE *nftyp = (FIELDTYPE *)0;
80
81 TR_FUNC_BFR(2);
82
83 T((T_CALLED("new_fieldtype(%s,%s)"),
84 TR_FUNC_ARG(0, field_check),
85 TR_FUNC_ARG(1, char_check)));
86
87 if ((field_check) || (char_check))
88 {
89 nftyp = typeMalloc(FIELDTYPE, 1);
90
91 if (nftyp)
92 {
93 T((T_CREATE("fieldtype %p"), (void *)nftyp));
94 *nftyp = default_fieldtype;
95 #if NCURSES_INTEROP_FUNCS
96 nftyp->fieldcheck.ofcheck = field_check;
97 nftyp->charcheck.occheck = char_check;
98 #else
99 nftyp->fcheck = field_check;
100 nftyp->ccheck = char_check;
101 #endif
102 }
103 else
104 {
105 SET_ERROR(E_SYSTEM_ERROR);
106 }
107 }
108 else
109 {
110 SET_ERROR(E_BAD_ARGUMENT);
111 }
112 returnFieldType(nftyp);
113 }
114
115 /*---------------------------------------------------------------------------
116 | Facility : libnform
117 | Function : int free_fieldtype(FIELDTYPE *typ)
118 |
119 | Description : Release the memory associated with this fieldtype.
120 |
121 | Return Values : E_OK - success
122 | E_CONNECTED - there are fields referencing the type
123 | E_BAD_ARGUMENT - invalid fieldtype pointer
124 +--------------------------------------------------------------------------*/
125 FORM_EXPORT(int)
free_fieldtype(FIELDTYPE * typ)126 free_fieldtype(FIELDTYPE *typ)
127 {
128 T((T_CALLED("free_fieldtype(%p)"), (void *)typ));
129
130 if (!typ)
131 RETURN(E_BAD_ARGUMENT);
132
133 if (typ->ref != 0)
134 RETURN(E_CONNECTED);
135
136 if (typ->status & _RESIDENT)
137 RETURN(E_CONNECTED);
138
139 if (typ->status & _LINKED_TYPE)
140 {
141 if (typ->left)
142 typ->left->ref--;
143 if (typ->right)
144 typ->right->ref--;
145 }
146 free(typ);
147 RETURN(E_OK);
148 }
149
150 /* fld_newftyp.c ends here */
151