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