xref: /openbsd/lib/libform/fld_newftyp.c (revision 4cfece93)
1 /*	$OpenBSD: fld_newftyp.c,v 1.7 2015/01/23 22:48:51 krw Exp $	*/
2 /****************************************************************************
3  * Copyright (c) 1998-2004,2007 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.7 2015/01/23 22:48:51 krw Exp $")
37 
38 static FIELDTYPE const 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   NULL,				/* field validation function                   */
48   NULL,				/* Character check function                    */
49   NULL,				/* enumerate next function                     */
50   NULL				/* enumerate previous function                 */
51 };
52 
53 NCURSES_EXPORT_VAR(const FIELDTYPE *)
54 _nc_Default_FieldType = &default_fieldtype;
55 
56 /*---------------------------------------------------------------------------
57 |   Facility      :  libnform
58 |   Function      :  FIELDTYPE *new_fieldtype(
59 |                       bool (* const field_check)(FIELD *,const void *),
60 |                       bool (* const char_check) (int, const void *) )
61 |
62 |   Description   :  Create a new fieldtype. The application programmer must
63 |                    write a field_check and a char_check function and give
64 |                    them as input to this call.
65 |                    If an error occurs, errno is set to
66 |                       E_BAD_ARGUMENT  - invalid arguments
67 |                       E_SYSTEM_ERROR  - system error (no memory)
68 |
69 |   Return Values :  Fieldtype pointer or NULL if error occurred
70 +--------------------------------------------------------------------------*/
71 NCURSES_EXPORT(FIELDTYPE *)
72 new_fieldtype(bool (*const field_check) (FIELD *, const void *),
73 	      bool (*const char_check) (int, const void *))
74 {
75   FIELDTYPE *nftyp = (FIELDTYPE *)0;
76 
77   T((T_CALLED("new_fieldtype(%p,%p)"), field_check, char_check));
78   if ((field_check) || (char_check))
79     {
80       nftyp = typeMalloc(FIELDTYPE, 1);
81 
82       if (nftyp)
83 	{
84 	  T((T_CREATE("fieldtype %p"), nftyp));
85 	  *nftyp = default_fieldtype;
86 	  nftyp->fcheck = field_check;
87 	  nftyp->ccheck = char_check;
88 	}
89       else
90 	{
91 	  SET_ERROR(E_SYSTEM_ERROR);
92 	}
93     }
94   else
95     {
96       SET_ERROR(E_BAD_ARGUMENT);
97     }
98   returnFieldType(nftyp);
99 }
100 
101 /*---------------------------------------------------------------------------
102 |   Facility      :  libnform
103 |   Function      :  int free_fieldtype(FIELDTYPE *typ)
104 |
105 |   Description   :  Release the memory associated with this fieldtype.
106 |
107 |   Return Values :  E_OK            - success
108 |                    E_CONNECTED     - there are fields referencing the type
109 |                    E_BAD_ARGUMENT  - invalid fieldtype pointer
110 +--------------------------------------------------------------------------*/
111 NCURSES_EXPORT(int)
112 free_fieldtype(FIELDTYPE *typ)
113 {
114   T((T_CALLED("free_fieldtype(%p)"), typ));
115 
116   if (!typ)
117     RETURN(E_BAD_ARGUMENT);
118 
119   if (typ->ref != 0)
120     RETURN(E_CONNECTED);
121 
122   if (typ->status & _RESIDENT)
123     RETURN(E_CONNECTED);
124 
125   if (typ->status & _LINKED_TYPE)
126     {
127       if (typ->left)
128 	typ->left->ref--;
129       if (typ->right)
130 	typ->right->ref--;
131     }
132   free(typ);
133   RETURN(E_OK);
134 }
135 
136 /* fld_newftyp.c ends here */
137