Do not run mandoc for lintmanpages if MANPAGES is empty.
[netbsd-mini2440.git] / lib / libform / driver.c
blob90fa289f34e2f9c7eb47bfa53a3576dbc4eca79e
1 /* $NetBSD: driver.c,v 1.15 2003/03/09 00:57:17 lukem Exp $ */
3 /*-
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn@baea.com.au, brett_lymn@yahoo.com.au)
6 * All rights reserved.
8 * This code has been donated to The NetBSD Foundation by the Author.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: driver.c,v 1.15 2003/03/09 00:57:17 lukem Exp $");
35 #include <ctype.h>
36 #include "form.h"
37 #include "internals.h"
39 static int
40 traverse_form_links(FORM *form, int direction);
43 * Traverse the links of the current field in the given direction until
44 * either a active & visible field is found or we return to the current
45 * field. Direction is the REQ_{LEFT,RIGHT,UP,DOWN}_FIELD driver commands.
46 * The function returns E_OK if a valid field is found, E_REQUEST_DENIED
47 * otherwise.
49 static int
50 traverse_form_links(FORM *form, int direction)
52 unsigned idx;
54 idx = form->cur_field;
56 do {
57 switch (direction) {
58 case REQ_LEFT_FIELD:
59 if (form->fields[idx]->left == NULL)
60 return E_REQUEST_DENIED;
61 idx = form->fields[idx]->left->index;
62 break;
64 case REQ_RIGHT_FIELD:
65 if (form->fields[idx]->right == NULL)
66 return E_REQUEST_DENIED;
67 idx = form->fields[idx]->right->index;
68 break;
70 case REQ_UP_FIELD:
71 if (form->fields[idx]->up == NULL)
72 return E_REQUEST_DENIED;
73 idx = form->fields[idx]->up->index;
74 break;
76 case REQ_DOWN_FIELD:
77 if (form->fields[idx]->down == NULL)
78 return E_REQUEST_DENIED;
79 idx = form->fields[idx]->down->index;
80 break;
82 default:
83 return E_REQUEST_DENIED;
86 if ((form->fields[idx]->opts & (O_ACTIVE | O_VISIBLE))
87 == (O_ACTIVE | O_VISIBLE)) {
88 form->cur_field = idx;
89 return E_OK;
91 } while (idx != form->cur_field);
93 return E_REQUEST_DENIED;
96 int
97 form_driver(FORM *form, int c)
99 FIELD *fieldp;
100 int update_page, update_field, old_field, old_page, status;
101 int start_field;
102 unsigned int pos;
104 if (form == NULL)
105 return E_BAD_ARGUMENT;
107 if ((form->fields == NULL) || (*(form->fields) == NULL))
108 return E_INVALID_FIELD;
110 if (form->posted != 1)
111 return E_NOT_POSTED;
113 if (form->in_init == 1)
114 return E_BAD_STATE;
117 old_field = start_field = form->cur_field;
118 fieldp = form->fields[form->cur_field];
119 update_page = update_field = 0;
120 status = E_OK;
122 if (c < REQ_MIN_REQUEST) {
123 if (isprint(c) || isblank(c)) {
124 do {
125 pos = fieldp->start_char + fieldp->row_xpos;
127 /* check if we are allowed to edit this field */
128 if ((fieldp->opts & O_EDIT) != O_EDIT)
129 return E_REQUEST_DENIED;
131 if ((status =
132 (_formi_add_char(fieldp, pos, c)))
133 == E_REQUEST_DENIED) {
136 * Need to check here if we
137 * want to autoskip. we
138 * call the form driver
139 * recursively to pos us on
140 * the next field and then
141 * we loop back to ensure
142 * the next field selected
143 * can have data added to it
145 if ((fieldp->opts & O_AUTOSKIP)
146 != O_AUTOSKIP)
147 return E_REQUEST_DENIED;
148 status = form_driver(form,
149 REQ_NEXT_FIELD);
150 if (status != E_OK)
151 return status;
154 * check if we have looped
155 * around all the fields.
156 * This can easily happen if
157 * all the fields are full.
159 if (start_field == form->cur_field)
160 return E_REQUEST_DENIED;
162 old_field = form->cur_field;
163 fieldp = form->fields[form->cur_field];
164 status = _formi_add_char(fieldp,
165 fieldp->start_char
166 + fieldp->cursor_xpos,
168 } else if (status == E_INVALID_FIELD)
169 /* char failed validation, just
170 * return the status.
172 return status;
173 else if (status == E_NO_ROOM)
174 /* we will get this if the line
175 * wrapping fails. Deny the
176 * request.
178 return E_REQUEST_DENIED;
180 while (status != E_OK);
181 update_field = (status == E_OK);
182 } else
183 return E_REQUEST_DENIED;
184 } else {
185 if (c > REQ_MAX_COMMAND)
186 return E_UNKNOWN_COMMAND;
188 if ((c >= REQ_NEXT_PAGE) && (c <= REQ_DOWN_FIELD)) {
189 /* first check the field we are in is ok */
190 if (_formi_validate_field(form) != E_OK)
191 return E_INVALID_FIELD;
193 if (form->field_term != NULL)
194 form->field_term(form);
197 * if we have a page movement then the form term
198 * needs to be called too
200 if ((c <= REQ_LAST_PAGE) && (form->form_term != NULL))
201 form->form_term(form);
205 switch (c) {
206 case REQ_NEXT_PAGE:
207 if (form->page < form->max_page) {
208 old_page = form->page;
209 form->page++;
210 update_page = 1;
211 if (_formi_pos_first_field(form) != E_OK) {
212 form->page = old_page;
213 status = E_REQUEST_DENIED;
215 } else
216 status = E_REQUEST_DENIED;
217 break;
219 case REQ_PREV_PAGE:
220 if (form->page > 0) {
221 old_page = form->page;
222 form->page--;
223 update_page = 1;
224 if (_formi_pos_first_field(form) != E_OK) {
225 form->page = old_page;
226 status = E_REQUEST_DENIED;
228 } else
229 status = E_REQUEST_DENIED;
230 break;
232 case REQ_FIRST_PAGE:
233 old_page = form->page;
234 form->page = 0;
235 update_page = 1;
236 if (_formi_pos_first_field(form) != E_OK) {
237 form->page = old_page;
238 status = E_REQUEST_DENIED;
240 break;
242 case REQ_LAST_PAGE:
243 old_page = form->page;
244 form->page = form->max_page - 1;
245 update_page = 1;
246 if (_formi_pos_first_field(form) != E_OK) {
247 form->page = old_page;
248 status = E_REQUEST_DENIED;
250 break;
252 case REQ_NEXT_FIELD:
253 status = _formi_pos_new_field(form, _FORMI_FORWARD,
254 FALSE);
255 update_field = 1;
256 break;
258 case REQ_PREV_FIELD:
259 status = _formi_pos_new_field(form, _FORMI_BACKWARD,
260 FALSE);
261 update_field = 1;
262 break;
264 case REQ_FIRST_FIELD:
265 form->cur_field = 0;
266 update_field = 1;
267 break;
269 case REQ_LAST_FIELD:
270 form->cur_field = form->field_count - 1;
271 update_field = 1;
272 break;
274 case REQ_SNEXT_FIELD:
275 status = _formi_pos_new_field(form, _FORMI_FORWARD,
276 TRUE);
277 update_field = 1;
278 break;
280 case REQ_SPREV_FIELD:
281 status = _formi_pos_new_field(form, _FORMI_BACKWARD,
282 TRUE);
283 update_field = 1;
284 break;
286 case REQ_SFIRST_FIELD:
287 fieldp = CIRCLEQ_FIRST(&form->sorted_fields);
288 form->cur_field = fieldp->index;
289 update_field = 1;
290 break;
292 case REQ_SLAST_FIELD:
293 fieldp = CIRCLEQ_LAST(&form->sorted_fields);
294 form->cur_field = fieldp->index;
295 update_field = 1;
296 break;
299 * The up, down, left and right field traversals
300 * are rolled up into a single function, allow a
301 * fall through to that function.
303 case REQ_LEFT_FIELD:
304 case REQ_RIGHT_FIELD:
305 case REQ_UP_FIELD:
306 case REQ_DOWN_FIELD:
307 status = traverse_form_links(form, c);
308 update_field = 1;
309 break;
311 /* the following commands modify the buffer, check if
312 this is allowed first before falling through. */
314 case REQ_DEL_PREV:
316 * need to check for the overloading of this
317 * request. If overload flag set and we are
318 * at the start of field this request turns
319 * into a previous field request. Otherwise
320 * fallthrough to the field handler.
322 if ((form->opts & O_BS_OVERLOAD) == O_BS_OVERLOAD) {
323 if ((fieldp->start_char == 0) &&
324 (fieldp->start_line == 0) &&
325 (fieldp->row_xpos == 0)) {
326 update_field =
327 _formi_manipulate_field(form,
328 REQ_PREV_FIELD);
329 break;
333 /* FALLTHROUGH */
334 case REQ_NEW_LINE:
336 * need to check for the overloading of this
337 * request. If overload flag set and we are
338 * at the start of field this request turns
339 * into a next field request. Otherwise
340 * fallthrough to the field handler.
342 if ((form->opts & O_NL_OVERLOAD) == O_NL_OVERLOAD) {
343 if ((fieldp->start_char == 0) &&
344 (fieldp->start_line == 0) &&
345 (fieldp->row_xpos == 0)) {
346 update_field =
347 _formi_manipulate_field(form,
348 REQ_NEXT_FIELD);
349 break;
353 /* FALLTHROUGH */
354 case REQ_INS_CHAR:
355 case REQ_INS_LINE:
356 case REQ_DEL_CHAR:
357 case REQ_DEL_LINE:
358 case REQ_DEL_WORD:
359 case REQ_CLR_EOL:
360 case REQ_CLR_EOF:
361 case REQ_CLR_FIELD:
362 case REQ_OVL_MODE:
363 case REQ_INS_MODE:
364 /* check if we are allowed to edit the field and fall
365 * through if we are.
367 if ((form->fields[form->cur_field]->opts & O_EDIT) != O_EDIT)
368 return E_REQUEST_DENIED;
370 /* the following manipulate the field contents, bundle
371 them into one function.... */
372 /* FALLTHROUGH */
373 case REQ_NEXT_CHAR:
374 case REQ_PREV_CHAR:
375 case REQ_NEXT_LINE:
376 case REQ_PREV_LINE:
377 case REQ_NEXT_WORD:
378 case REQ_PREV_WORD:
379 case REQ_BEG_FIELD:
380 case REQ_END_FIELD:
381 case REQ_BEG_LINE:
382 case REQ_END_LINE:
383 case REQ_LEFT_CHAR:
384 case REQ_RIGHT_CHAR:
385 case REQ_UP_CHAR:
386 case REQ_DOWN_CHAR:
387 case REQ_SCR_FLINE:
388 case REQ_SCR_BLINE:
389 case REQ_SCR_FPAGE:
390 case REQ_SCR_BPAGE:
391 case REQ_SCR_FHPAGE:
392 case REQ_SCR_BHPAGE:
393 case REQ_SCR_FCHAR:
394 case REQ_SCR_BCHAR:
395 case REQ_SCR_HFLINE:
396 case REQ_SCR_HBLINE:
397 case REQ_SCR_HFHALF:
398 case REQ_SCR_HBHALF:
399 update_field = _formi_manipulate_field(form, c);
400 break;
402 case REQ_VALIDATION:
403 return _formi_validate_field(form);
404 /* NOTREACHED */
405 break;
407 case REQ_PREV_CHOICE:
408 case REQ_NEXT_CHOICE:
409 update_field = _formi_field_choice(form, c);
410 /* reinit the cursor pos just in case */
411 if (update_field == 1) {
412 _formi_init_field_xpos(fieldp);
413 fieldp->row_xpos = 0;
415 break;
417 default: /* should not need to do this, but.... */
418 return E_UNKNOWN_COMMAND;
419 /* NOTREACHED */
420 break;
424 /* call the field and form init functions if required. */
425 if ((c >= REQ_NEXT_PAGE) && (c <= REQ_DOWN_FIELD)) {
426 if (form->field_init != NULL)
427 form->field_init(form);
430 * if we have a page movement then the form init
431 * needs to be called too
433 if ((c <= REQ_LAST_PAGE) && (form->form_init != NULL))
434 form->form_init(form);
437 * if there was an error just return now...
439 if (status != E_OK)
440 return status;
442 /* if we have no error, reset the various offsets */
443 fieldp = form->fields[form->cur_field];
444 fieldp->start_char = 0;
445 fieldp->start_line = fieldp->lines;
446 fieldp->cur_line = fieldp->lines;
447 fieldp->row_xpos = 0;
448 fieldp->cursor_ypos = 0;
449 _formi_init_field_xpos(fieldp);
452 if (update_field < 0)
453 return update_field;
455 if (update_field == 1)
456 update_page |= _formi_update_field(form, old_field);
458 if (update_page == 1)
459 _formi_draw_page(form);
461 pos_form_cursor(form);
463 if ((update_page == 1) || (update_field == 1))
464 wrefresh(form->scrwin);
466 return E_OK;