xref: /openbsd/lib/libform/form.priv.h (revision c7ef0cfc)
1 /*	$OpenBSD: form.priv.h,v 1.9 2023/10/17 09:52:10 nicm Exp $	*/
2 /****************************************************************************
3  * Copyright 2018-2020,2021 Thomas E. Dickey                                *
4  * Copyright 1998-2016,2017 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 /* $Id: form.priv.h,v 1.9 2023/10/17 09:52:10 nicm Exp $ */
36 
37 #ifndef FORM_PRIV_H
38 #define FORM_PRIV_H 1
39 /* *INDENT-OFF*/
40 #include "curses.priv.h"
41 
42 #define NCURSES_OPAQUE_FORM  0
43 
44 #include "mf_common.h"
45 
46 #if USE_WIDEC_SUPPORT
47 #if HAVE_WCTYPE_H
48 #include <wctype.h>
49 #endif
50 
51 #ifndef MB_LEN_MAX
52 #define MB_LEN_MAX 8 /* should be >= MB_CUR_MAX, but that may be a function */
53 #endif
54 
55 #define FIELD_CELL NCURSES_CH_T
56 
57 #define NCURSES_FIELD_INTERNALS char** expanded; WINDOW *working;
58 #define NCURSES_FIELD_EXTENSION , (char **)0, (WINDOW *)0
59 
60 #else
61 
62 #define FIELD_CELL char
63 
64 #define NCURSES_FIELD_EXTENSION /* nothing */
65 
66 #endif
67 
68 #include "form.h"
69 
70 	/***********************
71 	*   Default objects    *
72 	***********************/
73 extern FORM_EXPORT_VAR(FORM *)      _nc_Default_Form;
74 extern FORM_EXPORT_VAR(FIELD *)     _nc_Default_Field;
75 extern FORM_EXPORT_VAR(FIELDTYPE *) _nc_Default_FieldType;
76 
77 /* form  status values */
78 #define _OVLMODE         (0x04U) /* Form is in overlay mode                */
79 #define _WINDOW_MODIFIED (0x10U) /* Current field window has been modified */
80 #define _FCHECK_REQUIRED (0x20U) /* Current field needs validation         */
81 
82 /* field status values */
83 #define _CHANGED         (0x01U) /* Field has been changed                 */
84 #define _NEWTOP          (0x02U) /* Vertical scrolling occurred            */
85 #define _NEWPAGE         (0x04U) /* field begins new page of form          */
86 #define _MAY_GROW        (0x08U) /* dynamic field may still grow           */
87 
88 /* fieldtype status values */
89 #define _LINKED_TYPE     (0x01U) /* Type is a linked type                  */
90 #define _HAS_ARGS        (0x02U) /* Type has arguments                     */
91 #define _HAS_CHOICE      (0x04U) /* Type has choice methods                */
92 #define _RESIDENT        (0x08U) /* Type is built-in                       */
93 #define _GENERIC         (0x10U) /* A generic field type                   */
94 
95 /* This are the field options required to be a selectable field in field
96    navigation requests */
97 #define O_SELECTABLE (O_ACTIVE | O_VISIBLE)
98 
99 /* If form is NULL replace form argument by default-form */
100 #define Normalize_Form(form) \
101   ((form) = (form != 0) ? (form) : _nc_Default_Form)
102 
103 /* If field is NULL replace field argument by default-field */
104 #define Normalize_Field(field) \
105   ((field) = (field != 0) ? (field) : _nc_Default_Field)
106 
107 #if NCURSES_SP_FUNCS
108 #define Get_Form_Screen(form) \
109   ((form)->win ? _nc_screen_of((form->win)):CURRENT_SCREEN)
110 #else
111 #define Get_Form_Screen(form) CURRENT_SCREEN
112 #endif
113 
114 /* Retrieve form's window */
115 #define Get_Form_Window(form) \
116   ((form)->sub \
117    ? (form)->sub \
118    : ((form)->win \
119       ? (form)->win \
120       : StdScreen(Get_Form_Screen(form))))
121 
122 /* Calculate the size for a single buffer for this field */
123 #define Buffer_Length(field) ((field)->drows * (field)->dcols)
124 
125 /* Calculate the total size of all buffers for this field */
126 #define Total_Buffer_Size(field) \
127    ( (size_t)(Buffer_Length(field) + 1) * (size_t)(1+(field)->nbuf) * sizeof(FIELD_CELL) )
128 
129 /* Logic to determine whether or not a field is single lined */
130 #define Single_Line_Field(field) \
131    (((field)->rows + (field)->nrow) == 1)
132 
133 #define Field_Has_Option(f,o)      ((((unsigned)(f)->opts) & o) != 0)
134 
135 /* Logic to determine whether or not a field is selectable */
136 #define Field_Is_Selectable(f)     (((unsigned)((f)->opts) & O_SELECTABLE)==O_SELECTABLE)
137 #define Field_Is_Not_Selectable(f) (((unsigned)((f)->opts) & O_SELECTABLE)!=O_SELECTABLE)
138 
139 typedef struct typearg
140   {
141     struct typearg *left;
142     struct typearg *right;
143   }
144 TypeArgument;
145 
146 /* This is a dummy request code (normally invalid) to be used internally
147    with the form_driver() routine to position to the first active field
148    on the form
149 */
150 #define FIRST_ACTIVE_MAGIC (-291056)
151 
152 #define ALL_FORM_OPTS  (                \
153 			O_NL_OVERLOAD  |\
154 			O_BS_OVERLOAD   )
155 
156 #define STD_FIELD_OPTS (Field_Options)( \
157 			O_VISIBLE |\
158 			O_ACTIVE  |\
159 			O_PUBLIC  |\
160 			O_EDIT    |\
161 			O_WRAP    |\
162 			O_BLANK   |\
163 			O_AUTOSKIP|\
164 			O_NULLOK  |\
165 			O_PASSOK  |\
166 			O_STATIC)
167 
168 #define ALL_FIELD_OPTS (Field_Options)( \
169 			STD_FIELD_OPTS |\
170 			O_DYNAMIC_JUSTIFY |\
171 			O_NO_LEFT_STRIP |\
172 			O_EDGE_INSERT_STAY |\
173 			O_INPUT_LIMIT)
174 
175 #define C_BLANK ' '
176 #define is_blank(c) ((c)==C_BLANK)
177 
178 #define C_ZEROS '\0'
179 
180 extern FORM_EXPORT(TypeArgument *) _nc_Make_Argument (const FIELDTYPE*, va_list*, int*);
181 extern FORM_EXPORT(TypeArgument *) _nc_Copy_Argument (const FIELDTYPE*, const TypeArgument*, int*);
182 extern FORM_EXPORT(void) _nc_Free_Argument (const FIELDTYPE*, TypeArgument*);
183 extern FORM_EXPORT(bool) _nc_Copy_Type (FIELD*, FIELD const *);
184 extern FORM_EXPORT(void) _nc_Free_Type (FIELD *);
185 
186 extern FORM_EXPORT(int) _nc_Synchronize_Attributes (FIELD*);
187 extern FORM_EXPORT(int) _nc_Synchronize_Options (FIELD*, Field_Options);
188 extern FORM_EXPORT(int) _nc_Set_Form_Page (FORM*, int, FIELD*);
189 extern FORM_EXPORT(int) _nc_Refresh_Current_Field (FORM*);
190 extern FORM_EXPORT(FIELD *) _nc_First_Active_Field (FORM*);
191 extern FORM_EXPORT(bool) _nc_Internal_Validation (FORM*);
192 extern FORM_EXPORT(int) _nc_Set_Current_Field (FORM*, FIELD*);
193 extern FORM_EXPORT(int) _nc_Position_Form_Cursor (FORM*);
194 extern FORM_EXPORT(void) _nc_Unset_Current_Field(FORM *form);
195 
196 #if NCURSES_INTEROP_FUNCS
197 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_INTEGER(void);
198 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ALNUM(void);
199 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ALPHA(void);
200 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_ENUM(void);
201 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_NUMERIC(void);
202 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_REGEXP(void);
203 extern FORM_EXPORT(FIELDTYPE *) _nc_TYPE_IPV4(void);
204 
205 extern FORM_EXPORT(FIELDTYPE *)
206 _nc_generic_fieldtype(bool (*const field_check) (FORM*,
207 						 FIELD *,
208 						 const void *),
209 		      bool (*const char_check)  (int,
210 						 FORM*,
211 						 FIELD*,
212 						 const void *),
213 		      bool (*const next)(FORM*,FIELD*,const void*),
214 		      bool (*const prev)(FORM*,FIELD*,const void*),
215 		      void (*freecallback)(void*));
216 extern FORM_EXPORT(int) _nc_set_generic_fieldtype(FIELD*, FIELDTYPE*, int (*)(void**));
217 extern FORM_EXPORT(WINDOW*) _nc_form_cursor(const FORM* , int* , int* );
218 
219 #define INIT_FT_FUNC(func) {func}
220 #else
221 #define INIT_FT_FUNC(func) func
222 #endif
223 
224 extern FORM_EXPORT(void) _nc_get_fieldbuffer(FORM*, FIELD*, FIELD_CELL*);
225 
226 #if USE_WIDEC_SUPPORT
227 extern FORM_EXPORT(wchar_t *) _nc_Widen_String(char *, int *);
228 #endif
229 
230 #ifdef TRACE
231 
232 #define returnField(code)	TRACE_RETURN1(code,field)
233 #define returnFieldPtr(code)	TRACE_RETURN1(code,field_ptr)
234 #define returnForm(code)	TRACE_RETURN1(code,form)
235 #define returnFieldType(code)	TRACE_RETURN1(code,field_type)
236 #define returnFormHook(code)	TRACE_RETURN1(code,form_hook)
237 
238 extern FORM_EXPORT(FIELD **)	    _nc_retrace_field_ptr (FIELD **);
239 extern FORM_EXPORT(FIELD *)	    _nc_retrace_field (FIELD *);
240 extern FORM_EXPORT(FIELDTYPE *)  _nc_retrace_field_type (FIELDTYPE *);
241 extern FORM_EXPORT(FORM *)       _nc_retrace_form (FORM *);
242 extern FORM_EXPORT(Form_Hook)    _nc_retrace_form_hook (Form_Hook);
243 
244 #else /* !TRACE */
245 
246 #define returnFieldPtr(code)	return code
247 #define returnFieldType(code)	return code
248 #define returnField(code)	return code
249 #define returnForm(code)	return code
250 #define returnFormHook(code)	return code
251 
252 #endif /* TRACE/!TRACE */
253 
254 /*
255  * Use Check_CTYPE_Field() to simplify FIELDTYPE's that use only the ccheck()
256  * function.
257  */
258 #if USE_WIDEC_SUPPORT
259 #define Check_CTYPE_Field(result, buffer, width, ccheck) \
260   while (*buffer && *buffer == ' ') \
261     buffer++; \
262   if (*buffer) \
263     { \
264       bool blank = FALSE; \
265       int len; \
266       int n; \
267       wchar_t *list = _nc_Widen_String((char *)buffer, &len); \
268       if (list != 0) \
269 	{ \
270 	  result = TRUE; \
271 	  for (n = 0; n < len; ++n) \
272 	    { \
273 	      if (blank) \
274 		{ \
275 		  if (list[n] != ' ') \
276 		    { \
277 		      result = FALSE; \
278 		      break; \
279 		    } \
280 		} \
281 	      else if (list[n] == ' ') \
282 		{ \
283 		  blank = TRUE; \
284 		  result = (n + 1 >= width); \
285 		} \
286 	      else if (!ccheck(list[n], NULL)) \
287 		{ \
288 		  result = FALSE; \
289 		  break; \
290 		} \
291 	    } \
292 	  free(list); \
293 	} \
294     }
295 #else
296 #define Check_CTYPE_Field(result, buffer, width, ccheck) \
297   while (*buffer && *buffer == ' ') \
298     buffer++; \
299   if (*buffer) \
300     { \
301       unsigned char *s = buffer; \
302       int l = -1; \
303       while (*buffer && ccheck(*buffer, NULL)) \
304 	buffer++; \
305       l = (int)(buffer - s); \
306       while (*buffer && *buffer == ' ') \
307 	buffer++; \
308       result = ((*buffer || (l < width)) ? FALSE : TRUE); \
309     }
310 #endif
311 /* *INDENT-ON*/
312 
313 #endif /* FORM_PRIV_H */
314