xref: /openbsd/lib/libform/fld_arg.c (revision c7ef0cfc)
1 /*	$OpenBSD: fld_arg.c,v 1.7 2023/10/17 09:52:10 nicm Exp $	*/
2 /****************************************************************************
3  * Copyright 2018,2020 Thomas E. Dickey                                     *
4  * Copyright 1998-2012,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_arg.c,v 1.7 2023/10/17 09:52:10 nicm Exp $")
38 
39 /*---------------------------------------------------------------------------
40 |   Facility      :  libnform
41 |   Function      :  int set_fieldtype_arg(
42 |                            FIELDTYPE *typ,
43 |                            void * (* const make_arg)(va_list *),
44 |                            void * (* const copy_arg)(const void *),
45 |                            void   (* const free_arg)(void *) )
46 |
47 |   Description   :  Connects to the type additional arguments necessary
48 |                    for a set_field_type call. The various function pointer
49 |                    arguments are:
50 |                       make_arg : allocates a structure for the field
51 |                                  specific parameters.
52 |                       copy_arg : duplicate the structure created by
53 |                                  make_arg
54 |                       free_arg : Release the memory allocated by make_arg
55 |                                  or copy_arg
56 |
57 |                    At least make_arg must be non-NULL.
58 |                    You may pass NULL for copy_arg and free_arg if your
59 |                    make_arg function doesn't allocate memory and your
60 |                    arg fits into the storage for a (void*).
61 |
62 |   Return Values :  E_OK           - success
63 |                    E_BAD_ARGUMENT - invalid argument
64 +--------------------------------------------------------------------------*/
FORM_EXPORT(int)65 FORM_EXPORT(int)
66 set_fieldtype_arg(FIELDTYPE *typ,
67 		  void *(*const make_arg)(va_list *),
68 		  void *(*const copy_arg)(const void *),
69 		  void (*const free_arg) (void *))
70 {
71   TR_FUNC_BFR(3);
72 
73   T((T_CALLED("set_fieldtype_arg(%p,%s,%s,%s)"),
74      (void *)typ,
75      TR_FUNC_ARG(0, make_arg),
76      TR_FUNC_ARG(1, copy_arg),
77      TR_FUNC_ARG(2, free_arg)));
78 
79   if (typ != 0 && make_arg != (void *)0)
80     {
81       SetStatus(typ, _HAS_ARGS);
82       typ->makearg = make_arg;
83       typ->copyarg = copy_arg;
84       typ->freearg = free_arg;
85       RETURN(E_OK);
86     }
87   RETURN(E_BAD_ARGUMENT);
88 }
89 
90 /*---------------------------------------------------------------------------
91 |   Facility      :  libnform
92 |   Function      :  void *field_arg(const FIELD *field)
93 |
94 |   Description   :  Retrieve pointer to the field's argument structure.
95 |
96 |   Return Values :  Pointer to structure or NULL if none is defined.
97 +--------------------------------------------------------------------------*/
98 FORM_EXPORT(void *)
field_arg(const FIELD * field)99 field_arg(const FIELD *field)
100 {
101   T((T_CALLED("field_arg(%p)"), (const void *)field));
102   returnVoidPtr(Normalize_Field(field)->arg);
103 }
104 
105 /* fld_arg.c ends here */
106