internals.c revision 1.11 1 /* $NetBSD: internals.c,v 1.11 2001/02/10 14:55:18 blymn Exp $ */
2
3 /*-
4 * Copyright (c) 1998-1999 Brett Lymn
5 * (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
6 * All rights reserved.
7 *
8 * This code has been donated to The NetBSD Foundation by the Author.
9 *
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 withough specific prior written permission
17 *
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.
28 *
29 *
30 */
31
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 #include "internals.h"
37 #include "form.h"
38
39 #ifdef DEBUG
40 /*
41 * file handle to write debug info to, this will be initialised when
42 * the form is first posted.
43 */
44 FILE *dbg = NULL;
45 #endif
46
47 /* define our own min function - this is not generic but will do here
48 * (don't believe me? think about what value you would get
49 * from min(x++, y++)
50 */
51 #define min(a,b) (((a) > (b))? (b) : (a))
52
53 /* for the line joining function... */
54 #define JOIN_NEXT 1
55 #define JOIN_NEXT_NW 2 /* next join, don't wrap the joined line */
56 #define JOIN_PREV 3
57 #define JOIN_PREV_NW 4 /* previous join, don't wrap the joined line */
58
59 static void
60 _formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val);
61 static void
62 _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val);
63 static int
64 _formi_join_line(FIELD *field, char *str, unsigned int pos, int direction);
65 void
66 _formi_hscroll_back(FIELD *field, unsigned int amt);
67 void
68 _formi_hscroll_fwd(FIELD *field, unsigned int amt);
69 static void
70 _formi_scroll_back(FIELD *field, unsigned int amt);
71 static void
72 _formi_scroll_fwd(FIELD *field, unsigned int amt);
73 static int
74 find_sow(char *str, unsigned int offset);
75 static int
76 find_cur_line(FIELD *cur);
77
78 /*
79 * Open the debug file if it is not already open....
80 */
81 #ifdef DEBUG
82 int
83 _formi_create_dbg_file(void)
84 {
85 if (dbg == NULL) {
86 dbg = fopen("___form_dbg.out", "w");
87 if (dbg == NULL) {
88 fprintf(stderr, "Cannot open debug file!\n");
89 return E_SYSTEM_ERROR;
90 }
91 }
92
93 return E_OK;
94 }
95 #endif
96
97 /*
98 * Set the form's current field to the first valid field on the page.
99 * Assume the fields have been sorted and stitched.
100 */
101 int
102 _formi_pos_first_field(FORM *form)
103 {
104 FIELD *cur;
105 int old_page;
106
107 old_page = form->page;
108
109 /* scan forward for an active page....*/
110 while (form->page_starts[form->page].in_use == 0) {
111 form->page++;
112 if (form->page > form->max_page) {
113 form->page = old_page;
114 return E_REQUEST_DENIED;
115 }
116 }
117
118 cur = form->fields[form->page_starts[form->page].first];
119 while ((cur->opts & (O_VISIBLE | O_ACTIVE))
120 != (O_VISIBLE | O_ACTIVE)) {
121 cur = CIRCLEQ_NEXT(cur, glue);
122 if (cur == (void *) &form->sorted_fields) {
123 form->page = old_page;
124 return E_REQUEST_DENIED;
125 }
126 }
127
128 form->cur_field = cur->index;
129 return E_OK;
130 }
131
132 /*
133 * Set the field to the next active and visible field, the fields are
134 * traversed in index order in the direction given. If the parameter
135 * use_sorted is TRUE then the sorted field list will be traversed instead
136 * of using the field index.
137 */
138 int
139 _formi_pos_new_field(FORM *form, unsigned direction, unsigned use_sorted)
140 {
141 FIELD *cur;
142 int i;
143
144 i = form->cur_field;
145 cur = form->fields[i];
146
147 do {
148 if (direction == _FORMI_FORWARD) {
149 if (use_sorted == TRUE) {
150 if ((form->wrap == FALSE) &&
151 (cur == CIRCLEQ_LAST(&form->sorted_fields)))
152 return E_REQUEST_DENIED;
153 cur = CIRCLEQ_NEXT(cur, glue);
154 i = cur->index;
155 } else {
156 if ((form->wrap == FALSE) &&
157 ((i + 1) >= form->field_count))
158 return E_REQUEST_DENIED;
159 i++;
160 if (i >= form->field_count)
161 i = 0;
162 }
163 } else {
164 if (use_sorted == TRUE) {
165 if ((form->wrap == FALSE) &&
166 (cur == CIRCLEQ_FIRST(&form->sorted_fields)))
167 return E_REQUEST_DENIED;
168 cur = CIRCLEQ_PREV(cur, glue);
169 i = cur->index;
170 } else {
171 if ((form->wrap == FALSE) && (i <= 0))
172 return E_REQUEST_DENIED;
173 i--;
174 if (i < 0)
175 i = form->field_count - 1;
176 }
177 }
178
179 if ((form->fields[i]->opts & (O_VISIBLE | O_ACTIVE))
180 == (O_VISIBLE | O_ACTIVE)) {
181 form->cur_field = i;
182 return E_OK;
183 }
184 }
185 while (i != form->cur_field);
186
187 return E_REQUEST_DENIED;
188 }
189
190 /*
191 * Find the line in a field that the cursor is currently on.
192 */
193 static int
194 find_cur_line(FIELD *cur)
195 {
196 unsigned start, end, pos, row;
197 const char *str;
198
199 str = cur->buffers[0].string;
200 pos = cur->start_char + cur->hscroll + cur->cursor_xpos;
201
202 start = 0;
203 end = 0;
204
205 for (row = 1; row < cur->row_count; row++) {
206 start = _formi_find_bol(str, start);
207 end = _formi_find_eol(str, end);
208 if ((pos >= start) && (pos <= end))
209 return row;
210 }
211
212 return 0;
213 }
214
215
216 /*
217 * Word wrap the contents of the field's buffer 0 if this is allowed.
218 * If the wrap is successful, that is, the row count nor the buffer
219 * size is exceeded then the function will return E_OK, otherwise it
220 * will return E_REQUEST_DENIED.
221 */
222 int
223 _formi_wrap_field(FIELD *field, unsigned int pos)
224 {
225 char *str, *new;
226 int width, length, allocated, row_count, sol, eol, wrapped;
227 size_t new_size;
228
229 wrapped = FALSE;
230 row_count = 0;
231 allocated = field->buffers[0].allocated;
232 length = field->buffers[0].length;
233 if ((str = (char *) malloc(sizeof(char) * allocated)) == NULL)
234 return E_SYSTEM_ERROR;
235
236 strcpy(str,field->buffers[0].string);
237
238 if ((field->opts & O_STATIC) == O_STATIC)
239 width = field->cols + 1;
240 else
241 width = field->dcols;
242
243 while (str[pos] != '\0') {
244 row_count++;
245 sol = _formi_find_bol(str, pos);
246 eol = _formi_find_eol(str, pos);
247 if ((eol - sol) <= width) {
248 /* line may be too short, try joining some lines */
249 pos = eol;
250 if ((eol - sol) == width) {
251 /* if line is just right then don't wrap */
252 pos++;
253 continue;
254 }
255
256 if (_formi_join_line(field, str, pos, JOIN_NEXT_NW)
257 == E_OK) {
258 row_count--; /* cuz we just joined a line */
259 wrapped = TRUE;
260 } else
261 break;
262 } else {
263 /* line is too long, split it - maybe */
264 /* split on first whitespace before current word */
265 pos = sol + width;
266 if (!isblank(str[pos]))
267 pos = find_sow(str, pos);
268
269 if (pos != sol) {
270 if (length + 1 >= allocated) {
271 new_size = allocated + 64
272 - (allocated % 64);
273
274 if ((new = (char *) realloc(str,
275 sizeof(char) * new_size)
276 ) == NULL) {
277 free(str);
278 return E_SYSTEM_ERROR;
279 }
280 str = new;
281 allocated = new_size;
282 }
283
284 bcopy(&str[pos], &str[pos + 1],
285 (unsigned) length - pos - 1);
286 str[pos] = '\n';
287 pos = pos + 1;
288 length++;
289 wrapped = TRUE;
290 } else
291 break;
292 }
293 }
294
295 if (row_count > field->rows) {
296 free(str);
297 return E_REQUEST_DENIED;
298 }
299
300 if (wrapped == TRUE) {
301 field->buffers[0].length = length;
302 field->buffers[0].allocated = allocated;
303 free(field->buffers[0].string);
304 field->buffers[0].string = str;
305 } else /* all that work was in vain.... */
306 free(str);
307
308 return E_OK;
309 }
310
311 /*
312 * Join the two lines that surround the location pos, the type
313 * variable indicates the direction of the join. Note that pos is
314 * assumed to be at either the end of the line for a JOIN_NEXT or at
315 * the beginning of the line for a JOIN_PREV. We need to check the
316 * field options to ensure the join does not overflow the line limit
317 * (if wrap is off) or wrap the field buffer again. Returns E_OK if
318 * the join was successful or E_REQUEST_DENIED if the join cannot
319 * happen.
320 */
321 static int
322 _formi_join_line(FIELD *field, char *str, unsigned int pos, int direction)
323 {
324 unsigned int len, sol, eol, npos, start, dest;
325
326 npos = pos;
327
328 if ((direction == JOIN_NEXT) || (direction == JOIN_NEXT_NW)) {
329 sol = _formi_find_bol(str, pos);
330 npos++;
331 /* see if there is another line following... */
332 if (str[npos] == '\0')
333 return E_REQUEST_DENIED;
334 eol = _formi_find_eol(str, npos);
335
336 start = npos;
337 dest = pos;
338 len = eol - npos;
339 } else {
340 if (pos == 0)
341 return E_REQUEST_DENIED;
342 eol = _formi_find_eol(str, pos);
343 npos--;
344 sol = _formi_find_bol(str, npos);
345
346 start = pos;
347 dest = npos;
348 len = eol - pos;
349 }
350
351
352 /* if we cannot wrap and the length of the resultant line
353 * is bigger than our field width we shall deny the request.
354 */
355 if (((field->opts & O_WRAP) != O_WRAP) && /* XXXXX check for dynamic field */
356 ((sol + eol - 1) > field->cols))
357 return E_REQUEST_DENIED;
358
359 bcopy(&str[start], &str[dest], (unsigned) len);
360
361 /* wrap the field if required, if this fails undo the change */
362 if ((direction == JOIN_NEXT) || (direction == JOIN_PREV)) {
363 if (_formi_wrap_field(field, (unsigned int) pos) != E_OK) {
364 bcopy(&str[dest], &str[start], (unsigned) len);
365 str[dest] = '\n';
366 return E_REQUEST_DENIED;
367 }
368 }
369
370 return E_OK;
371 }
372
373 /*
374 * skip the blanks in the given string, start at the index start and
375 * continue forward until either the end of the string or a non-blank
376 * character is found. Return the index of either the end of the string or
377 * the first non-blank character.
378 */
379 unsigned
380 _formi_skip_blanks(char *string, unsigned int start)
381 {
382 unsigned int i;
383
384 i = start;
385
386 while ((string[i] != '\0') && isblank(string[i]))
387 i++;
388
389 return i;
390 }
391
392 /*
393 * Return the index of the top left most field of the two given fields.
394 */
395 static int
396 _formi_top_left(FORM *form, int a, int b)
397 {
398 /* lower row numbers always win here.... */
399 if (form->fields[a]->form_row < form->fields[b]->form_row)
400 return a;
401
402 if (form->fields[a]->form_row > form->fields[b]->form_row)
403 return b;
404
405 /* rows must be equal, check columns */
406 if (form->fields[a]->form_col < form->fields[b]->form_col)
407 return a;
408
409 if (form->fields[a]->form_col > form->fields[b]->form_col)
410 return b;
411
412 /* if we get here fields must be in exactly the same place, punt */
413 return a;
414 }
415
416 /*
417 * Return the index to the field that is the bottom-right-most of the
418 * two given fields.
419 */
420 static int
421 _formi_bottom_right(FORM *form, int a, int b)
422 {
423 /* check the rows first, biggest row wins */
424 if (form->fields[a]->form_row > form->fields[b]->form_row)
425 return a;
426 if (form->fields[a]->form_row < form->fields[b]->form_row)
427 return b;
428
429 /* rows must be equal, check cols, biggest wins */
430 if (form->fields[a]->form_col > form->fields[b]->form_col)
431 return a;
432 if (form->fields[a]->form_col < form->fields[b]->form_col)
433 return b;
434
435 /* fields in the same place, punt */
436 return a;
437 }
438
439 /*
440 * Find the next '\n' character in the given string starting at offset
441 * if there are no newlines found then return the index to the end of the
442 * string.
443 */
444 int
445 _formi_find_eol(const char *string, unsigned int offset)
446 {
447 char *location;
448 int eol;
449
450 if ((location = index(&string[offset], '\n')) != NULL)
451 eol = location - string;
452 else
453 eol = strlen(string);
454
455 if (eol > 0)
456 eol--;
457
458 return eol;
459 }
460
461 /*
462 * Find the previous '\n' character in the given string starting at offset
463 * if there are no newlines found then return 0.
464 */
465 int
466 _formi_find_bol(const char *string, unsigned int offset)
467 {
468 int cnt;
469
470 cnt = offset;
471 while ((cnt > 0) && (string[cnt] != '\n'))
472 cnt--;
473
474 /* if we moved and found a newline go forward one to point at the
475 * actual start of the line....
476 */
477 if ((cnt != offset) && (string[cnt] == '\n'))
478 cnt++;
479
480 return cnt;
481 }
482
483 /*
484 * Find the end of the current word in the string str, starting at
485 * offset - the end includes any trailing whitespace. If the end of
486 * the string is found before a new word then just return the offset
487 * to the end of the string.
488 */
489 static int
490 find_eow(char *str, unsigned int offset)
491 {
492 int start;
493
494 start = offset;
495 /* first skip any non-whitespace */
496 while ((str[start] != '\0') && !isblank(str[start]))
497 start++;
498
499 /* see if we hit the end of the string */
500 if (str[start] == '\0')
501 return start;
502
503 /* otherwise skip the whitespace.... */
504 while ((str[start] != '\0') && isblank(str[start]))
505 start++;
506
507 return start;
508 }
509
510 /*
511 * Find the beginning of the current word in the string str, starting
512 * at offset.
513 */
514 static int
515 find_sow(char *str, unsigned int offset)
516 {
517 int start;
518
519 start = offset;
520
521 if (start > 0) {
522 if (isblank(str[start]) || isblank(str[start - 1])) {
523 if (isblank(str[start - 1]))
524 start--;
525 /* skip the whitespace.... */
526 while ((start >= 0) && isblank(str[start]))
527 start--;
528 }
529 }
530
531 /* see if we hit the start of the string */
532 if (start < 0)
533 return 0;
534
535 /* now skip any non-whitespace */
536 while ((start >= 0) && !isblank(str[start]))
537 start--;
538
539 if (start > 0)
540 start++; /* last loop has us pointing at a space, adjust */
541
542 if (start < 0)
543 start = 0;
544
545 return start;
546 }
547
548 /*
549 * Scroll the field forward the given number of lines.
550 */
551 static void
552 _formi_scroll_fwd(FIELD *field, unsigned int amt)
553 {
554 /* check if we have lines to scroll */
555 if (field->row_count < (field->start_line + field->rows))
556 return;
557
558 field->start_line += min(amt,
559 field->row_count - field->start_line
560 - field->rows);
561 }
562
563 /*
564 * Scroll the field backward the given number of lines.
565 */
566 static void
567 _formi_scroll_back(FIELD *field, unsigned int amt)
568 {
569 if (field->start_line == 0)
570 return;
571
572 field->start_line -= min(field->start_line, amt);
573 }
574
575 /*
576 * Scroll the field forward the given number of characters.
577 */
578 void
579 _formi_hscroll_fwd(FIELD *field, int unsigned amt)
580 {
581 int end, scroll_amt;
582
583 end = _formi_find_eol(field->buffers[0].string,
584 field->start_char + field->hscroll
585 + field->cursor_xpos) - field->start_char
586 - field->hscroll - field->cursor_xpos;
587
588 scroll_amt = min(amt, end);
589 if (scroll_amt < 0)
590 scroll_amt = 0;
591
592 field->hscroll += scroll_amt;
593 if (amt > field->cursor_xpos)
594 field->cursor_xpos = 0;
595 else
596 field->cursor_xpos -= scroll_amt;
597 }
598
599 /*
600 * Scroll the field backward the given number of characters.
601 */
602 void
603 _formi_hscroll_back(FIELD *field, unsigned int amt)
604 {
605 int flen, sa;
606
607 sa = min(field->hscroll, amt);
608 field->hscroll -= sa;
609 field->cursor_xpos += sa;
610 flen = field->cols;
611 if (field->start_char > 0)
612 flen--;
613 if (field->cursor_xpos > flen)
614 field->cursor_xpos = flen;
615 }
616
617 /*
618 * Find the different pages in the form fields and assign the form
619 * page_starts array with the information to find them.
620 */
621 int
622 _formi_find_pages(FORM *form)
623 {
624 int i, cur_page = 0;
625
626 if ((form->page_starts = (_FORMI_PAGE_START *)
627 malloc((form->max_page + 1) * sizeof(_FORMI_PAGE_START))) == NULL)
628 return E_SYSTEM_ERROR;
629
630 /* initialise the page starts array */
631 memset(form->page_starts, 0,
632 (form->max_page + 1) * sizeof(_FORMI_PAGE_START));
633
634 for (i =0; i < form->field_count; i++) {
635 if (form->fields[i]->page_break == 1)
636 cur_page++;
637 if (form->page_starts[cur_page].in_use == 0) {
638 form->page_starts[cur_page].in_use = 1;
639 form->page_starts[cur_page].first = i;
640 form->page_starts[cur_page].last = i;
641 form->page_starts[cur_page].top_left = i;
642 form->page_starts[cur_page].bottom_right = i;
643 } else {
644 form->page_starts[cur_page].last = i;
645 form->page_starts[cur_page].top_left =
646 _formi_top_left(form,
647 form->page_starts[cur_page].top_left,
648 i);
649 form->page_starts[cur_page].bottom_right =
650 _formi_bottom_right(form,
651 form->page_starts[cur_page].bottom_right,
652 i);
653 }
654 }
655
656 return E_OK;
657 }
658
659 /*
660 * Completely redraw the field of the given form.
661 */
662 void
663 _formi_redraw_field(FORM *form, int field)
664 {
665 unsigned int pre, post, flen, slen, i, row, start, end, offset;
666 char *str;
667 FIELD *cur;
668 #ifdef DEBUG
669 char buffer[100];
670 #endif
671
672 cur = form->fields[field];
673 str = cur->buffers[0].string;
674 flen = cur->cols;
675 slen = 0;
676 start = 0;
677 end = 0;
678
679 wmove(form->subwin, (int) cur->form_row, (int) cur->form_col);
680 for (row = 1; row <= cur->row_count; row++) {
681 if (str == NULL) {
682 start = end = 0;
683 } else {
684 if ((str[end] == '\0') || (str[end + 1] == '\0')
685 || (row == 1))
686 start = end;
687 else
688 start = end + 1;
689 }
690
691 if (cur->buffers[0].length > 0) {
692 end = _formi_find_eol(str, start);
693 slen = end - start + 1;
694 } else
695 slen = 0;
696
697 switch (cur->justification) {
698 case JUSTIFY_RIGHT:
699 post = 0;
700 if (flen < slen)
701 pre = 0;
702 else
703 pre = flen - slen;
704
705 break;
706
707 case JUSTIFY_CENTER:
708 if (flen < slen) {
709 pre = 0;
710 post = 0;
711 } else {
712 pre = flen - slen;
713 post = pre = pre / 2;
714 /* get padding right if centring is not even */
715 if ((post + pre + slen) < flen)
716 post++;
717 }
718 break;
719
720 case NO_JUSTIFICATION:
721 case JUSTIFY_LEFT:
722 default:
723 pre = 0;
724 if (flen <= slen)
725 post = 0;
726 else {
727 post = flen - slen;
728 if (post > flen)
729 post = flen;
730 }
731 break;
732 }
733
734 if (pre > cur->hscroll - start)
735 pre = pre - cur->hscroll + start;
736 else
737 pre = 0;
738
739 if (slen > cur->hscroll) {
740 slen -= cur->hscroll;
741 post += cur->hscroll;
742 if (post > flen)
743 post = flen;
744 } else {
745 slen = 0;
746 post = flen - pre;
747 }
748
749 if (form->cur_field == field)
750 wattrset(form->subwin, cur->fore);
751 else
752 wattrset(form->subwin, cur->back);
753
754 #ifdef DEBUG
755 fprintf(dbg, "redraw_field: start=%d, pre=%d, slen=%d, flen=%d, post=%d, hscroll=%d\n",
756 start, pre, slen, flen, post, cur->hscroll);
757 if (str != NULL) {
758 strncpy(buffer, &str[cur->start_char], flen);
759 } else {
760 strcpy(buffer, "(null)");
761 }
762 buffer[flen] = '\0';
763 fprintf(dbg, "redraw_field: %s\n", buffer);
764 #endif
765
766 for (i = start + cur->hscroll; i < pre; i++)
767 waddch(form->subwin, cur->pad);
768
769 offset = cur->hscroll;
770 if (cur->start_char > 0)
771 offset += cur->start_char - 1;
772
773 if (flen > cur->hscroll + 1) {
774 if (flen > slen)
775 flen -= cur->hscroll + 1;
776 } else
777 flen = 0;
778
779 #ifdef DEBUG
780 fprintf(dbg, "redraw_field: will add %d chars, offset is %d\n",
781 min(slen, flen), offset);
782 #endif
783 for (i = 0;
784 i < min(slen, flen); i++)
785 {
786 #ifdef DEBUG
787 fprintf(dbg, "adding char str[%d]=%c\n",
788 i + offset, str[i + offset]);
789 #endif
790 waddch(form->subwin,
791 ((cur->opts & O_PUBLIC) == O_PUBLIC)?
792 str[i + offset] : cur->pad);
793 }
794
795 for (i = 0; i < post; i++)
796 waddch(form->subwin, cur->pad);
797 }
798
799 return;
800 }
801
802 /*
803 * Display the fields attached to the form that are on the current page
804 * on the screen.
805 *
806 */
807 int
808 _formi_draw_page(FORM *form)
809 {
810 int i;
811
812 if (form->page_starts[form->page].in_use == 0)
813 return E_BAD_ARGUMENT;
814
815 wclear(form->subwin);
816
817 for (i = form->page_starts[form->page].first;
818 i <= form->page_starts[form->page].last; i++)
819 _formi_redraw_field(form, i);
820
821 return E_OK;
822 }
823
824 /*
825 * Add the character c at the position pos in buffer 0 of the given field
826 */
827 int
828 _formi_add_char(FIELD *field, unsigned int pos, char c)
829 {
830 char *new;
831 unsigned int new_size;
832 int status;
833
834 /*
835 * If buffer has not had a string before, set it to a blank
836 * string. Everything should flow from there....
837 */
838 if (field->buffers[0].string == NULL) {
839 set_field_buffer(field, 0, "");
840 }
841
842 if (_formi_validate_char(field, c) != E_OK) {
843 #ifdef DEBUG
844 fprintf(dbg, "add_char: char %c failed char validation\n", c);
845 #endif
846 return E_INVALID_FIELD;
847 }
848
849 #ifdef DEBUG
850 fprintf(dbg, "add_char: pos=%d, char=%c\n", pos, c);
851 fprintf(dbg,
852 "add_char enter: xpos=%d, start=%d, length=%d(%d), allocated=%d\n",
853 field->cursor_xpos, field->start_char,
854 field->buffers[0].length, strlen(field->buffers[0].string),
855 field->buffers[0].allocated);
856 fprintf(dbg, "add_char enter: %s\n", field->buffers[0].string);
857 fprintf(dbg, "add_char enter: buf0_status=%d\n", field->buf0_status);
858 #endif
859 if (((field->opts & O_BLANK) == O_BLANK) &&
860 (field->buf0_status == FALSE)) {
861 field->buffers[0].length = 0;
862 field->buffers[0].string[0] = '\0';
863 pos = 0;
864 field->start_char = 0;
865 field->start_line = 0;
866 field->hscroll = 0;
867 field->row_count = 1;
868 field->cursor_xpos = 0;
869 field->cursor_ypos = 0;
870 }
871
872
873 if ((field->overlay == 0)
874 || ((field->overlay == 1) && (pos >= field->buffers[0].length))) {
875 /* first check if the field can have more chars...*/
876 if ((((field->opts & O_STATIC) == O_STATIC) &&
877 (field->buffers[0].length >= field->cols)) ||
878 (((field->opts & O_STATIC) != O_STATIC) &&
879 ((field->max > 0) &&
880 (field->buffers[0].length >= field->max))))
881 return E_REQUEST_DENIED;
882
883 if (field->buffers[0].length + 1
884 >= field->buffers[0].allocated) {
885 new_size = field->buffers[0].allocated + 64
886 - (field->buffers[0].allocated % 64);
887 if ((new = (char *) realloc(field->buffers[0].string,
888 new_size )) == NULL)
889 return E_SYSTEM_ERROR;
890 field->buffers[0].allocated = new_size;
891 field->buffers[0].string = new;
892 }
893 }
894
895 if ((field->overlay == 0) && (field->buffers[0].length > pos)) {
896 bcopy(&field->buffers[0].string[pos],
897 &field->buffers[0].string[pos + 1],
898 field->buffers[0].length - pos + 1);
899 }
900
901 field->buffers[0].string[pos] = c;
902 if (pos >= field->buffers[0].length) {
903 /* make sure the string is terminated if we are at the
904 * end of the string, the terminator would be missing
905 * if we are are at the end of the field.
906 */
907 field->buffers[0].string[pos + 1] = '\0';
908 }
909
910 /* only increment the length if we are inserting characters
911 * OR if we are at the end of the field in overlay mode.
912 */
913 if ((field->overlay == 0)
914 || ((field->overlay == 1) && (pos >= field->buffers[0].length)))
915 field->buffers[0].length++;
916
917 /* wrap the field, if needed */
918 status = _formi_wrap_field(field, pos);
919 if (status != E_OK) {
920 /* wrap failed for some reason, back out the char insert */
921 bcopy(&field->buffers[0].string[pos + 1],
922 &field->buffers[0].string[pos],
923 field->buffers[0].length - pos);
924 field->buffers[0].length--;
925 } else {
926 field->buf0_status = TRUE;
927 field->cursor_xpos++;
928 if (field->cursor_xpos > field->cols - 1) {
929 field->start_char++;
930 field->cursor_xpos = field->cols - 1;
931 }
932 }
933
934 #ifdef DEBUG
935 fprintf(dbg,
936 "add_char exit: xpos=%d, start=%d, length=%d(%d), allocated=%d\n",
937 field->cursor_xpos, field->start_char,
938 field->buffers[0].length, strlen(field->buffers[0].string),
939 field->buffers[0].allocated);
940 fprintf(dbg,"add_char exit: %s\n", field->buffers[0].string);
941 fprintf(dbg, "add_char exit: buf0_status=%d\n", field->buf0_status);
942 fprintf(dbg, "add_char exit: status = %s\n",
943 (status == E_OK)? "OK" : "FAILED");
944 #endif
945 return status;
946 }
947
948 /*
949 * Manipulate the text in a field, this takes the given form and performs
950 * the passed driver command on the current text field. Returns 1 if the
951 * text field was modified.
952 */
953 int
954 _formi_manipulate_field(FORM *form, int c)
955 {
956 FIELD *cur;
957 char *str;
958 unsigned int i, start, end, pos;
959
960 cur = form->fields[form->cur_field];
961
962 #ifdef DEBUG
963 fprintf(dbg,
964 "entry: xpos=%d, start_char=%d, length=%d, allocated=%d\n",
965 cur->cursor_xpos, cur->start_char, cur->buffers[0].length,
966 cur->buffers[0].allocated);
967 fprintf(dbg, "entry: string=");
968 if (cur->buffers[0].string == NULL)
969 fprintf(dbg, "(null)\n");
970 else
971 fprintf(dbg, "\"%s\"\n", cur->buffers[0].string);
972 #endif
973
974 /* Cannot manipulate a null string! */
975 if (cur->buffers[0].string == NULL)
976 return E_REQUEST_DENIED;
977
978 switch (c) {
979 case REQ_NEXT_CHAR:
980 if ((cur->cursor_xpos + cur->start_char
981 - ((cur->start_char > 0)? 1 : 0) + cur->hscroll + 1)
982 > cur->buffers[0].length) {
983 return E_REQUEST_DENIED;
984 }
985 cur->cursor_xpos++;
986 if (cur->cursor_xpos >= cur->cols - cur->hscroll - 1) {
987 if (cur->cols < (cur->hscroll + 1))
988 cur->cursor_xpos = 0;
989 else
990 cur->cursor_xpos = cur->cols
991 - cur->hscroll - 1;
992 cur->start_char++;
993 }
994 break;
995
996 case REQ_PREV_CHAR:
997 if (cur->cursor_xpos == 0) {
998 if (cur->start_char > 0)
999 cur->start_char--;
1000 else if (cur->hscroll > 0)
1001 cur->hscroll--;
1002 else
1003 return E_REQUEST_DENIED;
1004 } else
1005 cur->cursor_xpos--;
1006 break;
1007
1008 case REQ_NEXT_LINE:
1009 cur->cursor_ypos++;
1010 if (cur->cursor_ypos > cur->rows) {
1011 if ((cur->opts & O_STATIC) == O_STATIC) {
1012 if (cur->start_line + cur->cursor_ypos
1013 > cur->drows) {
1014 cur->cursor_ypos--;
1015 return E_REQUEST_DENIED;
1016 }
1017 } else {
1018 if (cur->start_line + cur->cursor_ypos
1019 > cur->nrows + cur->rows) {
1020 cur->cursor_ypos--;
1021 return E_REQUEST_DENIED;
1022 }
1023 }
1024 cur->start_line++;
1025 }
1026 break;
1027
1028 case REQ_PREV_LINE:
1029 if (cur->cursor_ypos == 0) {
1030 if (cur->start_line == 0)
1031 return E_REQUEST_DENIED;
1032 cur->start_line--;
1033 } else
1034 cur->cursor_ypos--;
1035 break;
1036
1037 case REQ_NEXT_WORD:
1038 start = cur->start_char + cur->cursor_xpos;
1039 str = cur->buffers[0].string;
1040
1041 start = find_eow(str, start);
1042
1043 /* check if we hit the end */
1044 if (str[start] == '\0')
1045 return E_REQUEST_DENIED;
1046
1047 /* otherwise we must have found the start of a word...*/
1048 if (start - cur->start_char < cur->cols) {
1049 cur->cursor_xpos = start;
1050 } else {
1051 cur->start_char = start;
1052 cur->cursor_xpos = 0;
1053 }
1054 break;
1055
1056 case REQ_PREV_WORD:
1057 start = cur->start_char + cur->cursor_xpos;
1058 if (cur->start_char > 0)
1059 start--;
1060
1061 if (start == 0)
1062 return E_REQUEST_DENIED;
1063
1064 str = cur->buffers[0].string;
1065
1066 start = find_sow(str, start);
1067
1068 if (start - cur->start_char > 0) {
1069 cur->cursor_xpos = start;
1070 } else {
1071 cur->start_char = start;
1072 cur->cursor_xpos = 0;
1073 }
1074 break;
1075
1076 case REQ_BEG_FIELD:
1077 cur->start_char = 0;
1078 cur->start_line = 0;
1079 cur->cursor_xpos = 0;
1080 cur->cursor_ypos = 0;
1081 break;
1082
1083 case REQ_END_FIELD:
1084 if (cur->row_count > cur->rows) {
1085 cur->start_line = cur->row_count - cur->rows;
1086 cur->cursor_ypos = cur->rows - 1;
1087 } else {
1088 cur->start_line = 0;
1089 cur->cursor_ypos = cur->row_count - 1;
1090 }
1091
1092 if ((str = rindex(cur->buffers[0].string, '\n')) == NULL) {
1093 cur->cursor_xpos = cur->cols - 1;
1094 if (cur->start_char < (cur->buffers[0].length +
1095 cur->cols)) {
1096 cur->start_char = 0;
1097 cur->cursor_xpos = cur->buffers[0].length;
1098 } else {
1099 cur->start_char = cur->buffers[0].length -
1100 cur->cols;
1101 }
1102 } else {
1103 cur->start_char = (str - cur->buffers[0].string);
1104 if (strlen(str) > cur->cols)
1105 cur->cursor_xpos = cur->cols;
1106 else
1107 cur->cursor_xpos = strlen(str);
1108 }
1109 break;
1110
1111 case REQ_BEG_LINE:
1112 start = cur->start_char + cur->cursor_xpos;
1113 if (cur->buffers[0].string[start] == '\n') {
1114 if (start > 0)
1115 start--;
1116 else
1117 return E_REQUEST_DENIED;
1118 }
1119
1120 while ((start > 0)
1121 && (cur->buffers[0].string[start] != '\n'))
1122 start--;
1123
1124 if (start > 0)
1125 start++;
1126
1127 cur->start_char = start;
1128 cur->cursor_xpos = 0;
1129 break;
1130
1131 case REQ_END_LINE:
1132 start = cur->start_char + cur->cursor_xpos;
1133 end = _formi_find_eol(cur->buffers[0].string, start);
1134 start = _formi_find_bol(cur->buffers[0].string, start);
1135
1136 if (end - start > cur->cols - 1) {
1137 cur->cursor_xpos = cur->cols - 1;
1138 cur->start_char = end - cur->cols + 3;
1139 } else {
1140 cur->cursor_xpos = end - start + 1;
1141 cur->start_char = start;
1142 }
1143 break;
1144
1145 case REQ_LEFT_CHAR:
1146 if ((cur->cursor_xpos == 0) && (cur->start_char == 0))
1147 return E_REQUEST_DENIED;
1148
1149 if (cur->cursor_xpos == 0) {
1150 cur->start_char--;
1151 if (cur->buffers[0].string[cur->start_char] == '\n') {
1152 if ((cur->cursor_ypos == 0) &&
1153 (cur->start_line == 0))
1154 {
1155 cur->start_char++;
1156 return E_REQUEST_DENIED;
1157 }
1158
1159 if (cur->cursor_ypos == 0)
1160 cur->start_line--;
1161 else
1162 cur->cursor_ypos--;
1163
1164 end = _formi_find_eol(cur->buffers[0].string,
1165 cur->start_char);
1166 start = _formi_find_bol(cur->buffers[0].string,
1167 cur->start_char);
1168 if (end - start >= cur->cols) {
1169 cur->cursor_xpos = cur->cols - 1;
1170 cur->start_char = end - cur->cols;
1171 } else {
1172 cur->cursor_xpos = end - start;
1173 cur->start_char = start;
1174 }
1175 }
1176 } else
1177 cur->cursor_xpos--;
1178 break;
1179
1180 case REQ_RIGHT_CHAR:
1181 pos = cur->start_char + cur->cursor_xpos;
1182 if (cur->buffers[0].string[pos] == '\0')
1183 return E_REQUEST_DENIED;
1184
1185 #ifdef DEBUG
1186 fprintf(dbg, "req_right_char enter: start=%d, xpos=%d, c=%c\n",
1187 cur->start_char, cur->cursor_xpos,
1188 cur->buffers[0].string[pos]);
1189 #endif
1190
1191 if (cur->buffers[0].string[pos] == '\n') {
1192 start = pos + 1;
1193 if (cur->buffers[0].string[start] == 0)
1194 return E_REQUEST_DENIED;
1195 end = _formi_find_eol(cur->buffers[0].string, start);
1196 if (end - start > cur->cols) {
1197 cur->cursor_xpos = cur->cols - 1;
1198 cur->start_char = end - cur->cols - 1;
1199 } else {
1200 cur->cursor_xpos = end - start;
1201 cur->start_char = start;
1202 }
1203 } else {
1204 if (cur->cursor_xpos == cur->cols - 1)
1205 cur->start_char++;
1206 else
1207 cur->cursor_xpos++;
1208 }
1209 #ifdef DEBUG
1210 fprintf(dbg, "req_right_char exit: start=%d, xpos=%d, c=%c\n",
1211 cur->start_char, cur->cursor_xpos,
1212 cur->buffers[0].string[cur->start_char +
1213 cur->cursor_xpos]);
1214 #endif
1215 break;
1216
1217 case REQ_UP_CHAR:
1218 if (cur->cursor_ypos == 0) {
1219 if (cur->start_line == 0)
1220 return E_REQUEST_DENIED;
1221
1222 cur->start_line--;
1223 } else
1224 cur->cursor_ypos--;
1225
1226 start = find_cur_line(cur);
1227 end = _formi_find_eol(cur->buffers[0].string, start);
1228 cur->start_char = start;
1229 if (cur->cursor_xpos > end - start)
1230 cur->cursor_xpos = end - start;
1231 break;
1232
1233 case REQ_DOWN_CHAR:
1234 if (cur->cursor_ypos == cur->rows - 1) {
1235 if (cur->start_line + cur->rows == cur->row_count)
1236 return E_REQUEST_DENIED;
1237 cur->start_line++;
1238 } else
1239 cur->cursor_ypos++;
1240
1241 start = find_cur_line(cur);
1242 end = _formi_find_eol(cur->buffers[0].string, start);
1243 cur->start_char = start;
1244 if (cur->cursor_xpos > end - start)
1245 cur->cursor_xpos = end - start;
1246 break;
1247
1248 case REQ_NEW_LINE:
1249 if (_formi_add_char(cur, cur->start_char + cur->cursor_xpos,
1250 '\n') == E_OK)
1251 cur->row_count++;
1252 break;
1253
1254 case REQ_INS_CHAR:
1255 _formi_add_char(cur, cur->start_char + cur->cursor_xpos,
1256 cur->pad);
1257 break;
1258
1259 case REQ_INS_LINE:
1260 start = _formi_find_bol(cur->buffers[0].string, cur->start_char);
1261 if (_formi_add_char(cur, start, '\n') == E_OK)
1262 cur->row_count++;
1263 break;
1264
1265 case REQ_DEL_CHAR:
1266 if (cur->buffers[0].string[cur->start_char + cur->cursor_xpos]
1267 == '\0')
1268 return E_REQUEST_DENIED;
1269
1270 start = cur->start_char + cur->cursor_xpos;
1271 end = cur->buffers[0].length;
1272 if (cur->buffers[0].string[start] == '\n') {
1273 if (cur->row_count > 1) {
1274 cur->row_count--;
1275 _formi_join_line(cur, cur->buffers[0].string,
1276 start, JOIN_NEXT);
1277 } else
1278 cur->buffers[0].string[start] = '\0';
1279 } else {
1280 bcopy(&cur->buffers[0].string[start + 1],
1281 &cur->buffers[0].string[start],
1282 (unsigned) end - start + 1);
1283 }
1284
1285 cur->buffers[0].length--;
1286 break;
1287
1288 case REQ_DEL_PREV:
1289 if ((cur->cursor_xpos == 0) && (cur->start_char == 0))
1290 return E_REQUEST_DENIED;
1291
1292 start = cur->cursor_xpos + cur->start_char;
1293 end = cur->buffers[0].length;
1294
1295 if (cur->buffers[0].string[cur->start_char + cur->cursor_xpos] == '\n') {
1296 _formi_join_line(cur, cur->buffers[0].string,
1297 cur->start_char + cur->cursor_xpos,
1298 JOIN_PREV);
1299 cur->row_count--;
1300 } else {
1301 bcopy(&cur->buffers[0].string[start],
1302 &cur->buffers[0].string[start - 1],
1303 (unsigned) end - start + 1);
1304 }
1305
1306 cur->buffers[0].length--;
1307 if ((cur->cursor_xpos == 0) && (cur->start_char > 0))
1308 cur->start_char--;
1309 else if ((cur->cursor_xpos == cur->cols - 1)
1310 && (cur->start_char > 0))
1311 cur->start_char--;
1312 else if (cur->cursor_xpos > 0)
1313 cur->cursor_xpos--;
1314
1315 break;
1316
1317 case REQ_DEL_LINE:
1318 start = cur->start_char + cur->cursor_xpos;
1319 end = _formi_find_eol(cur->buffers[0].string, start);
1320 start = _formi_find_bol(cur->buffers[0].string, start);
1321 bcopy(&cur->buffers[0].string[end + 1],
1322 &cur->buffers[0].string[start],
1323 (unsigned) cur->buffers[0].length - end + 1);
1324 if (cur->row_count > 1)
1325 cur->row_count--;
1326 break;
1327
1328 case REQ_DEL_WORD:
1329 start = cur->start_char + cur->cursor_xpos;
1330 end = find_eow(cur->buffers[0].string, start);
1331 start = find_sow(cur->buffers[0].string, start);
1332 bcopy(&cur->buffers[0].string[end + 1],
1333 &cur->buffers[0].string[start],
1334 (unsigned) cur->buffers[0].length - end + 1);
1335 cur->buffers[0].length -= end - start;
1336 break;
1337
1338 case REQ_CLR_EOL:
1339 /*XXXX this right or should we just toast the chars? */
1340 start = cur->start_char + cur->cursor_xpos;
1341 end = _formi_find_eol(cur->buffers[0].string, start);
1342 for (i = start; i < end; i++)
1343 cur->buffers[0].string[i] = cur->pad;
1344 break;
1345
1346 case REQ_CLR_EOF:
1347 for (i = cur->start_char + cur->cursor_xpos;
1348 i < cur->buffers[0].length; i++)
1349 cur->buffers[0].string[i] = cur->pad;
1350 break;
1351
1352 case REQ_CLR_FIELD:
1353 for (i = 0; i < cur->buffers[0].length; i++)
1354 cur->buffers[0].string[i] = cur->pad;
1355 break;
1356
1357 case REQ_OVL_MODE:
1358 cur->overlay = 1;
1359 break;
1360
1361 case REQ_INS_MODE:
1362 cur->overlay = 0;
1363 break;
1364
1365 case REQ_SCR_FLINE:
1366 _formi_scroll_fwd(cur, 1);
1367 break;
1368
1369 case REQ_SCR_BLINE:
1370 _formi_scroll_back(cur, 1);
1371 break;
1372
1373 case REQ_SCR_FPAGE:
1374 _formi_scroll_fwd(cur, cur->rows);
1375 break;
1376
1377 case REQ_SCR_BPAGE:
1378 _formi_scroll_back(cur, cur->rows);
1379 break;
1380
1381 case REQ_SCR_FHPAGE:
1382 _formi_scroll_fwd(cur, cur->rows / 2);
1383 break;
1384
1385 case REQ_SCR_BHPAGE:
1386 _formi_scroll_back(cur, cur->rows / 2);
1387 break;
1388
1389 case REQ_SCR_FCHAR:
1390 _formi_hscroll_fwd(cur, 1);
1391 break;
1392
1393 case REQ_SCR_BCHAR:
1394 _formi_hscroll_back(cur, 1);
1395 break;
1396
1397 case REQ_SCR_HFLINE:
1398 _formi_hscroll_fwd(cur, cur->cols);
1399 break;
1400
1401 case REQ_SCR_HBLINE:
1402 _formi_hscroll_back(cur, cur->cols);
1403 break;
1404
1405 case REQ_SCR_HFHALF:
1406 _formi_hscroll_fwd(cur, cur->cols / 2);
1407 break;
1408
1409 case REQ_SCR_HBHALF:
1410 _formi_hscroll_back(cur, cur->cols / 2);
1411 break;
1412
1413 default:
1414 return 0;
1415 }
1416
1417 #ifdef DEBUG
1418 fprintf(dbg, "exit: xpos=%d, start_char=%d, length=%d, allocated=%d\n",
1419 cur->cursor_xpos, cur->start_char, cur->buffers[0].length,
1420 cur->buffers[0].allocated);
1421 fprintf(dbg, "exit: string=\"%s\"\n", cur->buffers[0].string);
1422 #endif
1423 return 1;
1424 }
1425
1426 /*
1427 * Validate the give character by passing it to any type character
1428 * checking routines, if they exist.
1429 */
1430 int
1431 _formi_validate_char(FIELD *field, char c)
1432 {
1433 int ret_val;
1434
1435 if (field->type == NULL)
1436 return E_OK;
1437
1438 ret_val = E_INVALID_FIELD;
1439 _formi_do_char_validation(field, field->type, c, &ret_val);
1440
1441 return ret_val;
1442 }
1443
1444
1445 /*
1446 * Perform the validation of the character, invoke all field_type validation
1447 * routines. If the field is ok then update ret_val to E_OK otherwise
1448 * ret_val is not changed.
1449 */
1450 static void
1451 _formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val)
1452 {
1453 if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
1454 _formi_do_char_validation(field, type->link->next, c, ret_val);
1455 _formi_do_char_validation(field, type->link->prev, c, ret_val);
1456 } else {
1457 if (type->char_check == NULL)
1458 *ret_val = E_OK;
1459 else {
1460 if (type->char_check((int)(unsigned char) c,
1461 field->args) == TRUE)
1462 *ret_val = E_OK;
1463 }
1464 }
1465 }
1466
1467 /*
1468 * Validate the current field. If the field validation returns success then
1469 * return E_OK otherwise return E_INVALID_FIELD.
1470 *
1471 */
1472 int
1473 _formi_validate_field(FORM *form)
1474 {
1475 FIELD *cur;
1476 char *bp;
1477 int ret_val, count;
1478
1479
1480 if ((form == NULL) || (form->fields == NULL) ||
1481 (form->fields[0] == NULL))
1482 return E_INVALID_FIELD;
1483
1484 cur = form->fields[form->cur_field];
1485
1486 bp = cur->buffers[0].string;
1487 count = _formi_skip_blanks(bp, 0);
1488
1489 /* check if we have a null field, depending on the nullok flag
1490 * this may be acceptable or not....
1491 */
1492 if (cur->buffers[0].string[count] == '\0') {
1493 if ((cur->opts & O_NULLOK) == O_NULLOK)
1494 return E_OK;
1495 else
1496 return E_INVALID_FIELD;
1497 }
1498
1499 /* check if an unmodified field is ok */
1500 if (cur->buf0_status == 0) {
1501 if ((cur->opts & O_PASSOK) == O_PASSOK)
1502 return E_OK;
1503 else
1504 return E_INVALID_FIELD;
1505 }
1506
1507 /* if there is no type then just accept the field */
1508 if (cur->type == NULL)
1509 return E_OK;
1510
1511 ret_val = E_INVALID_FIELD;
1512 _formi_do_validation(cur, cur->type, &ret_val);
1513
1514 return ret_val;
1515 }
1516
1517 /*
1518 * Perform the validation of the field, invoke all field_type validation
1519 * routines. If the field is ok then update ret_val to E_OK otherwise
1520 * ret_val is not changed.
1521 */
1522 static void
1523 _formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val)
1524 {
1525 if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
1526 _formi_do_validation(field, type->link->next, ret_val);
1527 _formi_do_validation(field, type->link->prev, ret_val);
1528 } else {
1529 if (type->field_check == NULL)
1530 *ret_val = E_OK;
1531 else {
1532 if (type->field_check(field, field_buffer(field, 0))
1533 == TRUE)
1534 *ret_val = E_OK;
1535 }
1536 }
1537 }
1538
1539 /*
1540 * Select the next/previous choice for the field, the driver command
1541 * selecting the direction will be passed in c. Return 1 if a choice
1542 * selection succeeded, 0 otherwise.
1543 */
1544 int
1545 _formi_field_choice(FORM *form, int c)
1546 {
1547 FIELDTYPE *type;
1548 FIELD *field;
1549
1550 if ((form == NULL) || (form->fields == NULL) ||
1551 (form->fields[0] == NULL) ||
1552 (form->fields[form->cur_field]->type == NULL))
1553 return 0;
1554
1555 field = form->fields[form->cur_field];
1556 type = field->type;
1557
1558 switch (c) {
1559 case REQ_NEXT_CHOICE:
1560 if (type->next_choice == NULL)
1561 return 0;
1562 else
1563 return type->next_choice(field,
1564 field_buffer(field, 0));
1565
1566 case REQ_PREV_CHOICE:
1567 if (type->prev_choice == NULL)
1568 return 0;
1569 else
1570 return type->prev_choice(field,
1571 field_buffer(field, 0));
1572
1573 default: /* should never happen! */
1574 return 0;
1575 }
1576 }
1577
1578 /*
1579 * Update the fields if they have changed. The parameter old has the
1580 * previous current field as the current field may have been updated by
1581 * the driver. Return 1 if the form page needs updating.
1582 *
1583 */
1584 int
1585 _formi_update_field(FORM *form, int old_field)
1586 {
1587 int cur, i;
1588
1589 cur = form->cur_field;
1590
1591 if (old_field != cur) {
1592 if (!((cur >= form->page_starts[form->page].first) &&
1593 (cur <= form->page_starts[form->page].last))) {
1594 /* not on same page any more */
1595 for (i = 0; i < form->max_page; i++) {
1596 if ((form->page_starts[i].in_use == 1) &&
1597 (form->page_starts[i].first <= cur) &&
1598 (form->page_starts[i].last >= cur)) {
1599 form->page = i;
1600 return 1;
1601 }
1602 }
1603 }
1604 }
1605
1606 _formi_redraw_field(form, old_field);
1607 _formi_redraw_field(form, form->cur_field);
1608 return 0;
1609 }
1610
1611 /*
1612 * Compare function for the field sorting
1613 *
1614 */
1615 static int
1616 field_sort_compare(const void *one, const void *two)
1617 {
1618 const FIELD *a, *b;
1619 int tl;
1620
1621 /* LINTED const castaway; we don't modify these! */
1622 a = (const FIELD *) *((const FIELD **) one);
1623 b = (const FIELD *) *((const FIELD **) two);
1624
1625 if (a == NULL)
1626 return 1;
1627
1628 if (b == NULL)
1629 return -1;
1630
1631 /*
1632 * First check the page, we want the fields sorted by page.
1633 *
1634 */
1635 if (a->page != b->page)
1636 return ((a->page > b->page)? 1 : -1);
1637
1638 tl = _formi_top_left(a->parent, a->index, b->index);
1639
1640 /*
1641 * sort fields left to right, top to bottom so the top left is
1642 * the less than value....
1643 */
1644 return ((tl == a->index)? -1 : 1);
1645 }
1646
1647 /*
1648 * Sort the fields in a form ready for driver traversal.
1649 */
1650 void
1651 _formi_sort_fields(FORM *form)
1652 {
1653 FIELD **sort_area;
1654 int i;
1655
1656 CIRCLEQ_INIT(&form->sorted_fields);
1657
1658 if ((sort_area = (FIELD **) malloc(sizeof(FIELD *) * form->field_count))
1659 == NULL)
1660 return;
1661
1662 bcopy(form->fields, sort_area, sizeof(FIELD *) * form->field_count);
1663 qsort(sort_area, (unsigned) form->field_count, sizeof(FIELD *),
1664 field_sort_compare);
1665
1666 for (i = 0; i < form->field_count; i++)
1667 CIRCLEQ_INSERT_TAIL(&form->sorted_fields, sort_area[i], glue);
1668
1669 free(sort_area);
1670 }
1671
1672 /*
1673 * Set the neighbours for all the fields in the given form.
1674 */
1675 void
1676 _formi_stitch_fields(FORM *form)
1677 {
1678 int above_row, below_row, end_above, end_below, cur_row, real_end;
1679 FIELD *cur, *above, *below;
1680
1681 /*
1682 * check if the sorted fields circle queue is empty, just
1683 * return if it is.
1684 */
1685 if (CIRCLEQ_EMPTY(&form->sorted_fields))
1686 return;
1687
1688 /* initially nothing is above..... */
1689 above_row = -1;
1690 end_above = TRUE;
1691 above = NULL;
1692
1693 /* set up the first field as the current... */
1694 cur = CIRCLEQ_FIRST(&form->sorted_fields);
1695 cur_row = cur->form_row;
1696
1697 /* find the first field on the next row if any */
1698 below = CIRCLEQ_NEXT(cur, glue);
1699 below_row = -1;
1700 end_below = TRUE;
1701 real_end = TRUE;
1702 while (below != (void *)&form->sorted_fields) {
1703 if (below->form_row != cur_row) {
1704 below_row = below->form_row;
1705 end_below = FALSE;
1706 real_end = FALSE;
1707 break;
1708 }
1709 below = CIRCLEQ_NEXT(below, glue);
1710 }
1711
1712 /* walk the sorted fields, setting the neighbour pointers */
1713 while (cur != (void *) &form->sorted_fields) {
1714 if (cur == CIRCLEQ_FIRST(&form->sorted_fields))
1715 cur->left = NULL;
1716 else
1717 cur->left = CIRCLEQ_PREV(cur, glue);
1718
1719 if (cur == CIRCLEQ_LAST(&form->sorted_fields))
1720 cur->right = NULL;
1721 else
1722 cur->right = CIRCLEQ_NEXT(cur, glue);
1723
1724 if (end_above == TRUE)
1725 cur->up = NULL;
1726 else {
1727 cur->up = above;
1728 above = CIRCLEQ_NEXT(above, glue);
1729 if (above_row != above->form_row) {
1730 end_above = TRUE;
1731 above_row = above->form_row;
1732 }
1733 }
1734
1735 if (end_below == TRUE)
1736 cur->down = NULL;
1737 else {
1738 cur->down = below;
1739 below = CIRCLEQ_NEXT(below, glue);
1740 if (below == (void *) &form->sorted_fields) {
1741 end_below = TRUE;
1742 real_end = TRUE;
1743 } else if (below_row != below->form_row) {
1744 end_below = TRUE;
1745 below_row = below->form_row;
1746 }
1747 }
1748
1749 cur = CIRCLEQ_NEXT(cur, glue);
1750 if ((cur != (void *) &form->sorted_fields)
1751 && (cur_row != cur->form_row)) {
1752 cur_row = cur->form_row;
1753 if (end_above == FALSE) {
1754 for (; above != CIRCLEQ_FIRST(&form->sorted_fields);
1755 above = CIRCLEQ_NEXT(above, glue)) {
1756 if (above->form_row != above_row) {
1757 above_row = above->form_row;
1758 break;
1759 }
1760 }
1761 } else if (above == NULL) {
1762 above = CIRCLEQ_FIRST(&form->sorted_fields);
1763 end_above = FALSE;
1764 above_row = above->form_row;
1765 } else
1766 end_above = FALSE;
1767
1768 if (end_below == FALSE) {
1769 while (below_row == below->form_row) {
1770 below = CIRCLEQ_NEXT(below,
1771 glue);
1772 if (below ==
1773 (void *)&form->sorted_fields) {
1774 real_end = TRUE;
1775 end_below = TRUE;
1776 break;
1777 }
1778 }
1779
1780 if (below != (void *)&form->sorted_fields)
1781 below_row = below->form_row;
1782 } else if (real_end == FALSE)
1783 end_below = FALSE;
1784
1785 }
1786 }
1787 }
1788