xref: /openbsd-src/lib/libform/fld_newftyp.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: fld_newftyp.c,v 1.5 2001/01/22 18:02:13 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,2000 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 <juergen.pfeifer@gmx.net> 1995,1997            *
33  ****************************************************************************/
34 
35 #include "form.priv.h"
36 
37 MODULE_ID("$From: fld_newftyp.c,v 1.6 2000/12/10 02:09:38 tom Exp $")
38 
39 static FIELDTYPE const default_fieldtype = {
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*) _nc_Default_FieldType = &default_fieldtype;
54 
55 /*---------------------------------------------------------------------------
56 |   Facility      :  libnform
57 |   Function      :  FIELDTYPE *new_fieldtype(
58 |                       bool (* const field_check)(FIELD *,const void *),
59 |                       bool (* const char_check) (int, const void *) )
60 |
61 |   Description   :  Create a new fieldtype. The application programmer must
62 |                    write a field_check and a char_check function and give
63 |                    them as input to this call.
64 |                    If an error occurs, errno is set to
65 |                       E_BAD_ARGUMENT  - invalid arguments
66 |                       E_SYSTEM_ERROR  - system error (no memory)
67 |
68 |   Return Values :  Fieldtype pointer or NULL if error occured
69 +--------------------------------------------------------------------------*/
70 NCURSES_EXPORT(FIELDTYPE *)
71 new_fieldtype (
72  bool (* const field_check)(FIELD *,const void *),
73  bool (* const char_check) (int,const void *) )
74 {
75   FIELDTYPE *nftyp = (FIELDTYPE *)0;
76 
77   if ( (field_check) || (char_check) )
78     {
79       nftyp = (FIELDTYPE *)malloc(sizeof(FIELDTYPE));
80       if (nftyp)
81 	{
82 	  *nftyp = default_fieldtype;
83 	  nftyp->fcheck = field_check;
84 	  nftyp->ccheck = char_check;
85 	}
86       else
87 	{
88 	  SET_ERROR( E_SYSTEM_ERROR );
89 	}
90     }
91   else
92     {
93       SET_ERROR( E_BAD_ARGUMENT );
94     }
95   return nftyp;
96 }
97 
98 /*---------------------------------------------------------------------------
99 |   Facility      :  libnform
100 |   Function      :  int free_fieldtype(FIELDTYPE *typ)
101 |
102 |   Description   :  Release the memory associated with this fieldtype.
103 |
104 |   Return Values :  E_OK            - success
105 |                    E_CONNECTED     - there are fields referencing the type
106 |                    E_BAD_ARGUMENT  - invalid fieldtype pointer
107 +--------------------------------------------------------------------------*/
108 NCURSES_EXPORT(int)
109 free_fieldtype (FIELDTYPE *typ)
110 {
111   if (!typ)
112     RETURN(E_BAD_ARGUMENT);
113 
114   if (typ->ref!=0)
115     RETURN(E_CONNECTED);
116 
117   if (typ->status & _RESIDENT)
118     RETURN(E_CONNECTED);
119 
120   if (typ->status & _LINKED_TYPE)
121     {
122       if (typ->left ) typ->left->ref--;
123       if (typ->right) typ->right->ref--;
124     }
125   free(typ);
126   RETURN(E_OK);
127 }
128 
129 /* fld_newftyp.c ends here */
130