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