summaryrefslogtreecommitdiff
path: root/form/fty_generic.c
blob: 429ceac44a054489cb33585153d66abdb47bf6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/****************************************************************************
 * Copyright (c) 2008-2010,2012 Free Software Foundation, Inc.              *
 *                                                                          *
 * Permission is hereby granted, free of charge, to any person obtaining a  *
 * copy of this software and associated documentation files (the            *
 * "Software"), to deal in the Software without restriction, including      *
 * without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute, distribute with modifications, sublicense, and/or sell       *
 * copies of the Software, and to permit persons to whom the Software is    *
 * furnished to do so, subject to the following conditions:                 *
 *                                                                          *
 * The above copyright notice and this permission notice shall be included  *
 * in all copies or substantial portions of the Software.                   *
 *                                                                          *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
 *                                                                          *
 * Except as contained in this notice, the name(s) of the above copyright   *
 * holders shall not be used in advertising or otherwise to promote the     *
 * sale, use or other dealings in this Software without prior written       *
 * authorization.                                                           *
 ****************************************************************************/

/***************************************************************************
*                                                                          *
*  Author : Juergen Pfeifer                                                *
*                                                                          *
***************************************************************************/

#include "form.priv.h"

MODULE_ID("$Id: fty_generic.c,v 1.6 2012/06/10 00:27:49 tom Exp $")

/*
 * This is not a full implementation of a field type, but adds some
 * support for higher level languages with some restrictions to interop
 * with C language. Especially the collection of arguments for the
 * various fieldtypes is not based on the vararg C mechanism, but on a
 * iterator based callback mechanism that allowes the high level language
 * to provide the arguments as a structure. Most languages have mechanisms
 * to layout structures so that they can be passed to C.
 * The languages can register a new generic fieldtype dynamically and store
 * a handle (key) to the calling object as an argument. Together with that
 * it can register a freearg callback, so that the high level language
 * remains in control of the memory management of the arguments they pass.
 * The design idea is, that the high-level language - typically a OO
 * language like C# or Java, uses it's own dispatching mechanisms
 * (polymorphism) to call the proper check routines responsible for the
 * argument type. So these language implement typically only one generic
 * fieldtype they register with the forms library using this call.
 *
 * For that purpose we have extended the fieldtype struc by a new element
 * that gets the arguments from a single struct passed by the caller. 
 * 
 */
#if NCURSES_INTEROP_FUNCS

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static void *Generic_This_Type( void * arg )
|   
|   Description   :  We interpret the passed arg just as a handle the
|                    calling language uses to keep track of its allocated
|                    argument structures. We can simply copy it back.
|
|   Return Values :  Pointer to argument structure
+--------------------------------------------------------------------------*/
static void *
Generic_This_Type(void *arg)
{
  return (arg);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform
|   Function      :  FIELDTYPE *_nc_generic_fieldtype(
|                       bool (* const field_check)(FIELD *,const void *),
|                       bool (* const char_check) (int, const void *),
|   		        bool (*const next)(FORM*,FIELD*,const void*),
|		        bool (*const prev)(FORM*,FIELD*,const void*),
|                       void (*freecallback)(void*))
|
|   Description   :  Create a new fieldtype. The application programmer must
|                    write a field_check and a char_check function and give
|                    them as input to this call. A callback to allow the
|                    release of the allocated memory must also be provided.
|                    For generic field types, we provide some more 
|                    information about the field as parameters.
|
|                    If an error occurs, errno is set to
|                       E_BAD_ARGUMENT  - invalid arguments
|                       E_SYSTEM_ERROR  - system error (no memory)
|
|   Return Values :  Fieldtype pointer or NULL if error occurred
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(FIELDTYPE *)
_nc_generic_fieldtype(bool (*const field_check) (FORM *, FIELD *, const void *),
		      bool (*const char_check) (int, FORM *, FIELD *, const
						void *),
		      bool (*const next) (FORM *, FIELD *, const void *),
		      bool (*const prev) (FORM *, FIELD *, const void *),
		      void (*freecallback) (void *))
{
  int code = E_SYSTEM_ERROR;
  FIELDTYPE *res = (FIELDTYPE *)0;

  T((T_CALLED("_nc_generic_fieldtype(%p,%p,%p,%p,%p)"),
     field_check, char_check, next, prev, freecallback));

  if (field_check || char_check)
    {
      res = typeMalloc(FIELDTYPE, 1);

      if (res)
	{
	  *res = *_nc_Default_FieldType;
	  SetStatus(res, (_HAS_ARGS | _GENERIC));
	  res->fieldcheck.gfcheck = field_check;
	  res->charcheck.gccheck = char_check;
	  res->genericarg = Generic_This_Type;
	  res->freearg = freecallback;
	  res->enum_next.gnext = next;
	  res->enum_prev.gprev = prev;
	  code = E_OK;
	}
    }
  else
    code = E_BAD_ARGUMENT;

  if (E_OK != code)
    SET_ERROR(code);

  returnFieldType(res);
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  static TypeArgument *GenericArgument(
|                      const FIELDTYPE* typ,
|                      int (*argiterator)(void**),
|                      int* err)
|   
|   Description   :  The iterator callback must browse through all fieldtype
|                    parameters that have an argument associated with the
|                    type. The iterator returns 1 if the operation to get
|                    the next element was successfull, 0 otherwise. If the
|                    iterator could move to the next argument, it fills
|                    the void* pointer representing the argument into the
|                    location provided as argument to the iterator.
|                    The err reference is used to keep track of errors.
|
|   Return Values :  Pointer to argument structure
+--------------------------------------------------------------------------*/
static TypeArgument *
GenericArgument(const FIELDTYPE *typ,
		int (*argiterator) (void **), int *err)
{
  TypeArgument *res = (TypeArgument *)0;

  if (typ != 0 && (typ->status & _HAS_ARGS) != 0 && err != 0 && argiterator != 0)
    {
      if (typ->status & _LINKED_TYPE)
	{
	  /* Composite fieldtypes keep track internally of their own memory */
	  TypeArgument *p = typeMalloc(TypeArgument, 1);

	  if (p)
	    {
	      p->left = GenericArgument(typ->left, argiterator, err);
	      p->right = GenericArgument(typ->right, argiterator, err);
	      return p;
	    }
	  else
	    *err += 1;
	}
      else
	{
	  assert(typ->genericarg != (void *)0);
	  if (typ->genericarg == 0)
	    *err += 1;
	  else
	    {
	      void *argp;
	      int valid = argiterator(&argp);

	      if (valid == 0 || argp == 0 ||
		  !(res = (TypeArgument *)typ->genericarg(argp)))
		{
		  *err += 1;
		}
	    }
	}
    }
  return res;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  int _nc_set_generic_fieldtype(
|                      FIELD* field,
|                      FIELDTYPE* ftyp,
|                      int (*argiterator)(void**))
|   
|   Description   :  Assign the fieldtype to the field and use the iterator
|                    mechanism to get the arguments when a check is 
|                    performed.
|
|   Return Values :  E_OK if all went well
|                    E_SYSTEM_ERROR if an error occurred
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)
_nc_set_generic_fieldtype(FIELD *field,
			  FIELDTYPE *ftyp,
			  int (*argiterator) (void **))
{
  int code = E_SYSTEM_ERROR;
  int err = 0;

  if (field)
    {
      if (field && field->type)
	_nc_Free_Type(field);

      field->type = ftyp;
      if (ftyp)
	{
	  if (argiterator)
	    {
	      /* The precondition is that the iterator is reset */
	      field->arg = (void *)GenericArgument(field->type, argiterator, &err);

	      if (err)
		{
		  _nc_Free_Argument(field->type, (TypeArgument *)(field->arg));
		  field->type = (FIELDTYPE *)0;
		  field->arg = (void *)0;
		}
	      else
		{
		  code = E_OK;
		  if (field->type)
		    field->type->ref++;
		}
	    }
	}
      else
	{
	  field->arg = (void *)0;
	  code = E_OK;
	}
    }
  return code;
}

/*---------------------------------------------------------------------------
|   Facility      :  libnform  
|   Function      :  WINDOW* _nc_form_cursor(
|                      FORM* form,
|                      int *pRow, int *pCol)
|   
|   Description   :  Get the current position of the form cursor position
|                    We also return the field window
|
|   Return Values :  The fields Window or NULL on error
+--------------------------------------------------------------------------*/
NCURSES_EXPORT(WINDOW *)
_nc_form_cursor(const FORM *form, int *pRow, int *pCol)
{
  int code = E_SYSTEM_ERROR;
  WINDOW *res = (WINDOW *)0;

  if (!(form == 0 || pRow == 0 || pCol == 0))
    {
      *pRow = form->currow;
      *pCol = form->curcol;
      res = form->w;
      code = E_OK;
    }
  if (code != E_OK)
    SET_ERROR(code);
  return res;
}

#else
extern void _nc_fty_generic(void);
void
_nc_fty_generic(void)
{
}
#endif

/* fty_generic.c ends here */