forked from IIJ-NetBSD/netbsd-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinternals.c
3651 lines (3195 loc) · 87 KB
/
internals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: internals.c,v 1.37 2013/11/26 01:17:00 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
* (blymn@baea.com.au, brett_lymn@yahoo.com.au)
* All rights reserved.
*
* This code has been donated to The NetBSD Foundation by the Author.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: internals.c,v 1.37 2013/11/26 01:17:00 christos Exp $");
#include <limits.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <assert.h>
#include "internals.h"
#include "form.h"
#ifdef DEBUG
/*
* file handle to write debug info to, this will be initialised when
* the form is first posted.
*/
FILE *dbg = NULL;
/*
* map the request numbers to strings for debug
*/
char *reqs[] = {
"NEXT_PAGE", "PREV_PAGE", "FIRST_PAGE", "LAST_PAGE", "NEXT_FIELD",
"PREV_FIELD", "FIRST_FIELD", "LAST_FIELD", "SNEXT_FIELD",
"SPREV_FIELD", "SFIRST_FIELD", "SLAST_FIELD", "LEFT_FIELD",
"RIGHT_FIELD", "UP_FIELD", "DOWN_FIELD", "NEXT_CHAR", "PREV_CHAR",
"NEXT_LINE", "PREV_LINE", "NEXT_WORD", "PREV_WORD", "BEG_FIELD",
"END_FIELD", "BEG_LINE", "END_LINE", "LEFT_CHAR", "RIGHT_CHAR",
"UP_CHAR", "DOWN_CHAR", "NEW_LINE", "INS_CHAR", "INS_LINE",
"DEL_CHAR", "DEL_PREV", "DEL_LINE", "DEL_WORD", "CLR_EOL",
"CLR_EOF", "CLR_FIELD", "OVL_MODE", "INS_MODE", "SCR_FLINE",
"SCR_BLINE", "SCR_FPAGE", "SCR_BPAGE", "SCR_FHPAGE", "SCR_BHPAGE",
"SCR_FCHAR", "SCR_BCHAR", "SCR_HFLINE", "SCR_HBLINE", "SCR_HFHALF",
"SCR_HBHALF", "VALIDATION", "PREV_CHOICE", "NEXT_CHOICE" };
#endif
/* define our own min function - this is not generic but will do here
* (don't believe me? think about what value you would get
* from min(x++, y++)
*/
#define min(a,b) (((a) > (b))? (b) : (a))
/* for the line joining function... */
#define JOIN_NEXT 1
#define JOIN_NEXT_NW 2 /* next join, don't wrap the joined line */
#define JOIN_PREV 3
#define JOIN_PREV_NW 4 /* previous join, don't wrap the joined line */
/* for the bump_lines function... */
#define _FORMI_USE_CURRENT -1 /* indicates current cursor pos to be used */
/* used in add_char for initial memory allocation for string in row */
#define INITIAL_LINE_ALLOC 16
unsigned
field_skip_blanks(unsigned int start, _FORMI_FIELD_LINES **rowp);
static void
_formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val);
static void
_formi_do_validation(FIELD *field, FIELDTYPE *type, int *ret_val);
static int
_formi_join_line(FIELD *field, _FORMI_FIELD_LINES **rowp, int direction);
void
_formi_hscroll_back(FIELD *field, _FORMI_FIELD_LINES *row, unsigned int amt);
void
_formi_hscroll_fwd(FIELD *field, _FORMI_FIELD_LINES *row, unsigned int amt);
static void
_formi_scroll_back(FIELD *field, unsigned int amt);
static void
_formi_scroll_fwd(FIELD *field, unsigned int amt);
static int
_formi_set_cursor_xpos(FIELD *field, int no_scroll);
static int
find_sow(unsigned int offset, _FORMI_FIELD_LINES **rowp);
static int
split_line(FIELD *field, bool hard_split, unsigned pos,
_FORMI_FIELD_LINES **rowp);
static bool
check_field_size(FIELD *field);
static int
add_tab(FORM *form, _FORMI_FIELD_LINES *row, unsigned int i, char c);
static int
tab_size(_FORMI_FIELD_LINES *row, unsigned int i);
static unsigned int
tab_fit_len(_FORMI_FIELD_LINES *row, unsigned int len);
static int
tab_fit_window(FIELD *field, unsigned int pos, unsigned int window);
static void
add_to_free(FIELD *field, _FORMI_FIELD_LINES *line);
static void
adjust_ypos(FIELD *field, _FORMI_FIELD_LINES *line);
static _FORMI_FIELD_LINES *
copy_row(_FORMI_FIELD_LINES *row);
static void
destroy_row_list(_FORMI_FIELD_LINES *start);
/*
* Calculate the cursor y position to make the given row appear on the
* field. This may be as simple as just changing the ypos (if at all) but
* may encompass resetting the start_line of the field to place the line
* at the bottom of the field. The field is assumed to be a multi-line one.
*/
static void
adjust_ypos(FIELD *field, _FORMI_FIELD_LINES *line)
{
unsigned ypos;
_FORMI_FIELD_LINES *rs;
ypos = 0;
rs = field->alines;
while (rs != line) {
rs = rs->next;
ypos++;
}
field->cursor_ypos = ypos;
field->start_line = field->alines;
if (ypos > (field->rows - 1)) {
/*
* cur_line off the end of the field,
* adjust start_line so fix this.
*/
field->cursor_ypos = field->rows - 1;
ypos = ypos - (field->rows - 1);
while (ypos > 0) {
ypos--;
field->start_line = field->start_line->next;
}
}
}
/*
* Delete the given row and add it to the free list of the given field.
*/
static void
add_to_free(FIELD *field, _FORMI_FIELD_LINES *line)
{
_FORMI_FIELD_LINES *saved;
saved = line;
/* don't remove if only one line... */
if ((line->prev == NULL) && (line->next == NULL))
return;
if (line->prev == NULL) {
/* handle top of list */
field->alines = line->next;
field->alines->prev = NULL;
if (field->cur_line == saved)
field->cur_line = field->alines;
if (field->start_line == saved)
field->start_line = saved;
} else if (line->next == NULL) {
/* handle bottom of list */
line->prev->next = NULL;
if (field->cur_line == saved)
field->cur_line = saved->prev;
if (field->start_line == saved)
field->cur_line = saved->prev;
} else {
saved->next->prev = saved->prev;
saved->prev->next = saved->next;
if (field->cur_line == saved)
field->cur_line = saved->prev;
if (field->start_line == saved)
field->start_line = saved;
}
saved->next = field->free;
field->free = saved;
saved->prev = NULL;
if (saved->next != NULL)
saved->next->prev = line;
}
/*
* Duplicate the given row, return the pointer to the new copy or
* NULL if the copy fails.
*/
static _FORMI_FIELD_LINES *
copy_row(_FORMI_FIELD_LINES *row)
{
_FORMI_FIELD_LINES *new;
_formi_tab_t *tp, *newt;
if ((new = (_FORMI_FIELD_LINES *) malloc(sizeof(_FORMI_FIELD_LINES)))
== NULL) {
return NULL;
}
memcpy(new, row, sizeof(_FORMI_FIELD_LINES));
/* nuke the pointers from the source row so we don't get confused */
new->next = NULL;
new->prev = NULL;
new->tabs = NULL;
if ((new->string = (char *) malloc((size_t)new->allocated)) == NULL) {
free(new);
return NULL;
}
memcpy(new->string, row->string, (size_t) row->length + 1);
if (row->tabs != NULL) {
tp = row->tabs;
if ((new->tabs = (_formi_tab_t *) malloc(sizeof(_formi_tab_t)))
== NULL) {
free(new->string);
free(new);
return NULL;
}
memcpy(new->tabs, row->tabs, sizeof(_formi_tab_t));
new->tabs->back = NULL;
new->tabs->fwd = NULL;
tp = tp->fwd;
newt = new->tabs;
while (tp != NULL) {
if ((newt->fwd =
(_formi_tab_t *) malloc(sizeof(_formi_tab_t)))
== NULL) {
/* error... unwind allocations */
tp = new->tabs;
while (tp != NULL) {
newt = tp->fwd;
free(tp);
tp = newt;
}
free(new->string);
free(new);
return NULL;
}
memcpy(newt->fwd, tp, sizeof(_formi_tab_t));
newt->fwd->back = newt;
newt = newt->fwd;
newt->fwd = NULL;
tp = tp->fwd;
}
}
return new;
}
/*
* Initialise the row offset for a field, depending on the type of
* field it is and the type of justification used. The justification
* is only used on static single line fields, everything else will
* have the cursor_xpos set to 0.
*/
void
_formi_init_field_xpos(FIELD *field)
{
/* not static or is multi-line which are not justified, so 0 it is */
if (((field->opts & O_STATIC) != O_STATIC) ||
((field->rows + field->nrows) != 1)) {
field->cursor_xpos = 0;
return;
}
switch (field->justification) {
case JUSTIFY_RIGHT:
field->cursor_xpos = field->cols - 1;
break;
case JUSTIFY_CENTER:
field->cursor_xpos = (field->cols - 1) / 2;
break;
default: /* assume left justify */
field->cursor_xpos = 0;
break;
}
}
/*
* Open the debug file if it is not already open....
*/
#ifdef DEBUG
int
_formi_create_dbg_file(void)
{
if (dbg == NULL) {
dbg = fopen("___form_dbg.out", "w");
if (dbg == NULL) {
fprintf(stderr, "Cannot open debug file!\n");
return E_SYSTEM_ERROR;
}
}
return E_OK;
}
#endif
/*
* Check the sizing of the field, if the maximum size is set for a
* dynamic field then check that the number of rows or columns does
* not exceed the set maximum. The decision to check the rows or
* columns is made on the basis of how many rows are in the field -
* one row means the max applies to the number of columns otherwise it
* applies to the number of rows. If the row/column count is less
* than the maximum then return TRUE.
*
*/
static bool
check_field_size(FIELD *field)
{
if ((field->opts & O_STATIC) != O_STATIC) {
/* dynamic field */
if (field->max == 0) /* unlimited */
return TRUE;
if (field->rows == 1) {
return (field->alines->length < field->max);
} else {
return (field->row_count <= field->max);
}
} else {
if ((field->rows + field->nrows) == 1) {
return (field->alines->length <= field->cols);
} else {
return (field->row_count <= (field->rows
+ field->nrows));
}
}
}
/*
* Set the form's current field to the first valid field on the page.
* Assume the fields have been sorted and stitched.
*/
int
_formi_pos_first_field(FORM *form)
{
FIELD *cur;
int old_page;
old_page = form->page;
/* scan forward for an active page....*/
while (form->page_starts[form->page].in_use == 0) {
form->page++;
if (form->page > form->max_page) {
form->page = old_page;
return E_REQUEST_DENIED;
}
}
/* then scan for a field we can use */
cur = form->fields[form->page_starts[form->page].first];
while ((cur->opts & (O_VISIBLE | O_ACTIVE))
!= (O_VISIBLE | O_ACTIVE)) {
cur = TAILQ_NEXT(cur, glue);
if (cur == NULL) {
form->page = old_page;
return E_REQUEST_DENIED;
}
}
form->cur_field = cur->index;
return E_OK;
}
/*
* Set the field to the next active and visible field, the fields are
* traversed in index order in the direction given. If the parameter
* use_sorted is TRUE then the sorted field list will be traversed instead
* of using the field index.
*/
int
_formi_pos_new_field(FORM *form, unsigned direction, unsigned use_sorted)
{
FIELD *cur;
int i;
i = form->cur_field;
cur = form->fields[i];
do {
if (direction == _FORMI_FORWARD) {
if (use_sorted == TRUE) {
if ((form->wrap == FALSE) &&
(cur == TAILQ_LAST(&form->sorted_fields,
_formi_sort_head)))
return E_REQUEST_DENIED;
cur = TAILQ_NEXT(cur, glue);
i = cur->index;
} else {
if ((form->wrap == FALSE) &&
((i + 1) >= form->field_count))
return E_REQUEST_DENIED;
i++;
if (i >= form->field_count)
i = 0;
}
} else {
if (use_sorted == TRUE) {
if ((form->wrap == FALSE) &&
(cur == TAILQ_FIRST(&form->sorted_fields)))
return E_REQUEST_DENIED;
cur = TAILQ_PREV(cur, _formi_sort_head, glue);
i = cur->index;
} else {
if ((form->wrap == FALSE) && (i <= 0))
return E_REQUEST_DENIED;
i--;
if (i < 0)
i = form->field_count - 1;
}
}
if ((form->fields[i]->opts & (O_VISIBLE | O_ACTIVE))
== (O_VISIBLE | O_ACTIVE)) {
form->cur_field = i;
return E_OK;
}
}
while (i != form->cur_field);
return E_REQUEST_DENIED;
}
/*
* Destroy the list of line structs passed by freeing all allocated
* memory.
*/
static void
destroy_row_list(_FORMI_FIELD_LINES *start)
{
_FORMI_FIELD_LINES *temp, *row;
_formi_tab_t *tt, *tp;
row = start;
while (row != NULL) {
if (row->tabs != NULL) {
/* free up the tab linked list... */
tp = row->tabs;
while (tp != NULL) {
tt = tp->fwd;
free(tp);
tp = tt;
}
}
if (row->string != NULL)
free(row->string);
temp = row->next;
free(row);
row = temp;
}
}
/*
* Word wrap the contents of the field's buffer 0 if this is allowed.
* If the wrap is successful, that is, the row count nor the buffer
* size is exceeded then the function will return E_OK, otherwise it
* will return E_REQUEST_DENIED.
*/
int
_formi_wrap_field(FIELD *field, _FORMI_FIELD_LINES *loc)
{
int width, wrap_err;
unsigned int pos, saved_xpos, saved_ypos, saved_cur_xpos;
unsigned int saved_row_count;
_FORMI_FIELD_LINES *saved_row, *row, *row_backup, *saved_cur_line;
_FORMI_FIELD_LINES *saved_start_line, *temp;
if ((field->opts & O_STATIC) == O_STATIC) {
if ((field->rows + field->nrows) == 1) {
return E_OK; /* cannot wrap a single line */
}
width = field->cols;
} else {
/* if we are limited to one line then don't try to wrap */
if ((field->drows + field->nrows) == 1) {
return E_OK;
}
/*
* hueristic - if a dynamic field has more than one line
* on the screen then the field grows rows, otherwise
* it grows columns, effectively a single line field.
* This is documented AT&T behaviour.
*/
if (field->rows > 1) {
width = field->cols;
} else {
return E_OK;
}
}
row = loc;
/* if we are not at the top of the field then back up one
* row because we may be able to merge the current row into
* the one above.
*/
if (row->prev != NULL)
row = row->prev;
saved_row = row;
saved_xpos = field->row_xpos;
saved_cur_xpos = field->cursor_xpos;
saved_ypos = field->cursor_ypos;
saved_row_count = field->row_count;
/*
* Save a copy of the lines affected, just in case things
* don't work out.
*/
if ((row_backup = copy_row(row)) == NULL)
return E_SYSTEM_ERROR;
temp = row_backup;
row = row->next;
saved_cur_line = temp;
saved_start_line = temp;
while (row != NULL) {
if ((temp->next = copy_row(row)) == NULL) {
/* a row copy failed... free up allocations */
destroy_row_list(row_backup);
return E_SYSTEM_ERROR;
}
temp->next->prev = temp;
temp = temp->next;
if (row == field->start_line)
saved_start_line = temp;
if (row == field->cur_line)
saved_cur_line = temp;
row = row->next;
}
row = saved_row;
while (row != NULL) {
pos = row->length - 1;
if (row->expanded < width) {
/* line may be too short, try joining some lines */
if ((row->hard_ret == TRUE) && (row->next != NULL)) {
/*
* Skip the line if it has a hard return
* and it is not the last, we cannot join
* anything to it.
*/
row = row->next;
continue;
}
if (row->next == NULL) {
/*
* If there are no more lines and this line
* is too short then our job is over.
*/
break;
}
if (_formi_join_line(field, &row,
JOIN_NEXT_NW) == E_OK) {
continue;
} else
break;
} else if (row->expanded > width) {
/* line is too long, split it */
/*
* split on first whitespace before current word
* if the line has tabs we need to work out where
* the field border lies when the tabs are expanded.
*/
if (row->tabs == NULL) {
pos = width - 1;
if (pos >= row->expanded)
pos = row->expanded - 1;
} else {
pos = tab_fit_len(row, field->cols);
}
if ((!isblank((unsigned char)row->string[pos])) &&
((field->opts & O_WRAP) == O_WRAP)) {
if (!isblank((unsigned char)row->string[pos - 1]))
pos = find_sow((unsigned int) pos,
&row);
/*
* If we cannot split the line then return
* NO_ROOM so the driver can tell that it
* should not autoskip (if that is enabled)
*/
if ((pos == 0)
|| (!isblank((unsigned char)row->string[pos - 1]))) {
wrap_err = E_NO_ROOM;
goto restore_and_exit;
}
}
/* if we are at the end of the string and it has
* a trailing blank, don't wrap the blank.
*/
if ((row->next == NULL) && (pos == row->length - 1) &&
(isblank((unsigned char)row->string[pos])) &&
row->expanded <= field->cols)
continue;
/*
* otherwise, if we are still sitting on a
* blank but not at the end of the line
* move forward one char so the blank
* is on the line boundary.
*/
if ((isblank((unsigned char)row->string[pos])) &&
(pos != row->length - 1))
pos++;
if (split_line(field, FALSE, pos, &row) != E_OK) {
wrap_err = E_REQUEST_DENIED;
goto restore_and_exit;
}
} else
/* line is exactly the right length, do next one */
row = row->next;
}
/* Check if we have not run out of room */
if ((((field->opts & O_STATIC) == O_STATIC) &&
field->row_count > (field->rows + field->nrows)) ||
((field->max != 0) && (field->row_count > field->max))) {
wrap_err = E_REQUEST_DENIED;
restore_and_exit:
if (saved_row->prev == NULL) {
field->alines = row_backup;
} else {
saved_row->prev->next = row_backup;
row_backup->prev = saved_row->prev;
}
field->row_xpos = saved_xpos;
field->cursor_xpos = saved_cur_xpos;
field->cursor_ypos = saved_ypos;
field->row_count = saved_row_count;
field->start_line = saved_start_line;
field->cur_line = saved_cur_line;
destroy_row_list(saved_row);
return wrap_err;
}
destroy_row_list(row_backup);
return E_OK;
}
/*
* Join the two lines that surround the location pos, the type
* variable indicates the direction of the join, JOIN_NEXT will join
* the next line to the current line, JOIN_PREV will join the current
* line to the previous line, the new lines will be wrapped unless the
* _NW versions of the directions are used. Returns E_OK if the join
* was successful or E_REQUEST_DENIED if the join cannot happen.
*/
static int
_formi_join_line(FIELD *field, _FORMI_FIELD_LINES **rowp, int direction)
{
int old_len, count;
struct _formi_field_lines *saved;
char *newp;
_FORMI_FIELD_LINES *row = *rowp;
#ifdef DEBUG
int dbg_ok = FALSE;
if (_formi_create_dbg_file() == E_OK) {
dbg_ok = TRUE;
}
if (dbg_ok == TRUE) {
fprintf(dbg, "join_line: working on row %p, row_count = %d\n",
row, field->row_count);
}
#endif
if ((direction == JOIN_NEXT) || (direction == JOIN_NEXT_NW)) {
/*
* See if there is another line following, or if the
* line contains a hard return then we don't join
* any lines to it.
*/
if ((row->next == NULL) || (row->hard_ret == TRUE)) {
return E_REQUEST_DENIED;
}
#ifdef DEBUG
if (dbg_ok == TRUE) {
fprintf(dbg,
"join_line: join_next before length = %d, expanded = %d",
row->length, row->expanded);
fprintf(dbg,
" :: next row length = %d, expanded = %d\n",
row->length, row->expanded);
}
#endif
if (row->allocated < (row->length + row->next->length + 1)) {
if ((newp = realloc(row->string, (size_t)(row->length +
row->next->length
+ 1))) == NULL)
return E_REQUEST_DENIED;
row->string = newp;
row->allocated = row->length + row->next->length + 1;
}
strcat(row->string, row->next->string);
old_len = row->length;
row->length += row->next->length;
if (row->length > 0)
row->expanded =
_formi_tab_expanded_length(row->string, 0,
row->length - 1);
else
row->expanded = 0;
_formi_calculate_tabs(row);
row->hard_ret = row->next->hard_ret;
/* adjust current line if it is on the row being eaten */
if (field->cur_line == row->next) {
field->cur_line = row;
field->row_xpos += old_len;
field->cursor_xpos =
_formi_tab_expanded_length(row->string, 0,
field->row_xpos);
if (field->cursor_xpos > 0)
field->cursor_xpos--;
if (field->cursor_ypos > 0)
field->cursor_ypos--;
else {
if (field->start_line->prev != NULL)
field->start_line =
field->start_line->prev;
}
}
/* remove joined line record from the row list */
add_to_free(field, row->next);
#ifdef DEBUG
if (dbg_ok == TRUE) {
fprintf(dbg,
"join_line: exit length = %d, expanded = %d\n",
row->length, row->expanded);
}
#endif
} else {
if (row->prev == NULL) {
return E_REQUEST_DENIED;
}
saved = row->prev;
/*
* Don't try to join if the line above has a hard
* return on it.
*/
if (saved->hard_ret == TRUE) {
return E_REQUEST_DENIED;
}
#ifdef DEBUG
if (dbg_ok == TRUE) {
fprintf(dbg,
"join_line: join_prev before length = %d, expanded = %d",
row->length, row->expanded);
fprintf(dbg,
" :: prev row length = %d, expanded = %d\n",
saved->length, saved->expanded);
}
#endif
if (saved->allocated < (row->length + saved->length + 1)) {
if ((newp = realloc(saved->string,
(size_t) (row->length +
saved->length
+ 1))) == NULL)
return E_REQUEST_DENIED;
saved->string = newp;
saved->allocated = row->length + saved->length + 1;
}
strcat(saved->string, row->string);
old_len = saved->length;
saved->length += row->length;
if (saved->length > 0)
saved->expanded =
_formi_tab_expanded_length(saved->string, 0,
saved->length - 1);
else
saved->length = 0;
saved->hard_ret = row->hard_ret;
/* adjust current line if it was on the row being eaten */
if (field->cur_line == row) {
field->cur_line = saved;
field->row_xpos += old_len;
field->cursor_xpos =
_formi_tab_expanded_length(saved->string, 0,
field->row_xpos);
if (field->cursor_xpos > 0)
field->cursor_xpos--;
}
add_to_free(field, row);
#ifdef DEBUG
if (dbg_ok == TRUE) {
fprintf(dbg,
"join_line: exit length = %d, expanded = %d\n",
saved->length, saved->expanded);
}
#endif
row = saved;
}
/*
* Work out where the line lies in the field in relation to
* the cursor_ypos. First count the rows from the start of
* the field until we hit the row we just worked on.
*/
saved = field->start_line;
count = 0;
while (saved->next != NULL) {
if (saved == row)
break;
count++;
saved = saved->next;
}
/* now check if we need to adjust cursor_ypos */
if (field->cursor_ypos > count) {
field->cursor_ypos--;
}
field->row_count--;
*rowp = row;
/* wrap the field if required, if this fails undo the change */
if ((direction == JOIN_NEXT) || (direction == JOIN_PREV)) {
if (_formi_wrap_field(field, row) != E_OK) {
return E_REQUEST_DENIED;
}
}
return E_OK;
}
/*
* Split the line at the given position, if possible. If hard_split is
* TRUE then split the line regardless of the position, otherwise don't
* split at the beginning of a line.
*/
static int
split_line(FIELD *field, bool hard_split, unsigned pos,
_FORMI_FIELD_LINES **rowp)
{
struct _formi_field_lines *new_line;
char *newp;
_FORMI_FIELD_LINES *row = *rowp;
#ifdef DEBUG
short dbg_ok = FALSE;
#endif
/* if asked to split right where the line already starts then
* just return - nothing to do unless we are appending a line
* to the buffer.
*/
if ((pos == 0) && (hard_split == FALSE))
return E_OK;
#ifdef DEBUG
if (_formi_create_dbg_file() == E_OK) {
fprintf(dbg, "split_line: splitting line at %d\n", pos);
dbg_ok = TRUE;
}
#endif
/* Need an extra line struct, check free list first */
if (field->free != NULL) {
new_line = field->free;
field->free = new_line->next;
if (field->free != NULL)
field->free->prev = NULL;
} else {
if ((new_line = (struct _formi_field_lines *)
malloc(sizeof(struct _formi_field_lines))) == NULL)
return E_SYSTEM_ERROR;
new_line->prev = NULL;
new_line->next = NULL;
new_line->allocated = 0;
new_line->length = 0;
new_line->expanded = 0;
new_line->string = NULL;
new_line->hard_ret = FALSE;
new_line->tabs = NULL;
}
#ifdef DEBUG
if (dbg_ok == TRUE) {
fprintf(dbg,
"split_line: enter: length = %d, expanded = %d\n",
row->length, row->expanded);
}
#endif
assert((row->length < INT_MAX) && (row->expanded < INT_MAX));
/* add new line to the row list */
new_line->next = row->next;
new_line->prev = row;
row->next = new_line;
if (new_line->next != NULL)
new_line->next->prev = new_line;
new_line->length = row->length - pos;
if (new_line->length >= new_line->allocated) {
if ((newp = realloc(new_line->string,
(size_t) new_line->length + 1)) == NULL)
return E_SYSTEM_ERROR;
new_line->string = newp;
new_line->allocated = new_line->length + 1;
}
strcpy(new_line->string, &row->string[pos]);
row->length = pos;
row->string[pos] = '\0';
if (row->length != 0)
row->expanded = _formi_tab_expanded_length(row->string, 0,
row->length - 1);
else
row->expanded = 0;
_formi_calculate_tabs(row);
if (new_line->length != 0)
new_line->expanded =
_formi_tab_expanded_length(new_line->string, 0,
new_line->length - 1);
else
new_line->expanded = 0;
_formi_calculate_tabs(new_line);
/*
* If the given row was the current line then adjust the