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