xref: /netbsd-src/lib/libform/field.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: field.c,v 1.14 2001/07/08 12:15:06 blymn Exp $	*/
2 /*-
3  * Copyright (c) 1998-1999 Brett Lymn
4  *                         (blymn@baea.com.au, brett_lymn@yahoo.com.au)
5  * All rights reserved.
6  *
7  * This code has been donated to The NetBSD Foundation by the Author.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *
29  */
30 
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <form.h>
34 #include "internals.h"
35 
36 extern FORM _formi_default_form;
37 
38 FIELD _formi_default_field = {
39 	0, /* rows in the field */
40 	0, /* columns in the field */
41 	0, /* dynamic rows */
42 	0, /* dynamic columns */
43 	0, /* maximum growth */
44 	0, /* starting row in the form subwindow */
45 	0, /* starting column in the form subwindow */
46 	0, /* number of off screen rows */
47 	0, /* index of this field in form fields array. */
48 	0, /* number of buffers associated with this field */
49 	FALSE, /* set to true if buffer 0 has changed. */
50 	NO_JUSTIFICATION, /* justification style of the field */
51 	FALSE, /* set to true if field is in overlay mode */
52 	0, /* starting char in string (horiz scroll) */
53 	0, /* starting line in field (vert scroll) */
54 	0, /* number of rows actually used in field */
55 	0, /* x pos of cursor in field */
56 	0, /* y pos of cursor in field */
57 	0, /* start of a new page on the form if 1 */
58 	0, /* number of the page this field is on */
59 	A_NORMAL, /* character attributes for the foreground */
60 	A_NORMAL, /* character attributes for the background */
61 	' ', /* padding character */
62 	DEFAULT_FORM_OPTS, /* options for the field */
63 	NULL, /* the form this field is bound to, if any */
64 	NULL, /* field above this one */
65 	NULL, /* field below this one */
66 	NULL, /* field to the left of this one */
67 	NULL, /* field to the right of this one */
68 	NULL,  /* user defined pointer. */
69 	NULL, /* used if fields are linked */
70 	NULL, /* type struct for the field */
71 	{NULL, NULL}, /* circle queue glue for sorting fields */
72 	NULL, /* args for field type. */
73 	0,    /* number of allocated slots in lines array */
74 	NULL, /* pointer to the array of lines structures. */
75 	NULL, /* array of buffers for the field */
76 };
77 
78 /* internal function prototypes */
79 static FIELD *
80 _formi_create_field(FIELD *, int, int, int, int, int, int);
81 
82 
83 /*
84  * Set the userptr for the field
85  */
86 int
87 set_field_userptr(FIELD *field, void *ptr)
88 {
89 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
90 
91 	fp->userptr = ptr;
92 
93 	return E_OK;
94 }
95 
96 /*
97  * Return the userptr for the field.
98  */
99 
100 void *
101 field_userptr(FIELD *field)
102 {
103 	if (field == NULL)
104 		return _formi_default_field.userptr;
105 	else
106 		return field->userptr;
107 }
108 
109 /*
110  * Set the options for the designated field.
111  */
112 int
113 set_field_opts(FIELD *field, Form_Options options)
114 {
115 	int i;
116 
117 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
118 
119 	  /* not allowed to set opts if the field is the current one */
120 	if ((field != NULL) && (field->parent != NULL) &&
121 	    (field->parent->cur_field == field->index))
122 		return E_CURRENT;
123 
124 	if ((options & O_STATIC) == O_STATIC) {
125 		for (i = 0; i < field->nbuf; i++) {
126 			if (field->buffers[i].length > field->cols)
127 				field->buffers[i].string[field->cols] = '\0';
128 		}
129 	}
130 
131 	fp->opts = options;
132 
133 	return E_OK;
134 }
135 
136 /*
137  * Turn on the passed field options.
138  */
139 int
140 field_opts_on(FIELD *field, Form_Options options)
141 {
142 	int i;
143 
144 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
145 
146 	  /* not allowed to set opts if the field is the current one */
147 	if ((field != NULL) && (field->parent != NULL) &&
148 	    (field->parent->cur_field == field->index))
149 		return E_CURRENT;
150 
151 	if ((options & O_STATIC) == O_STATIC) {
152 		for (i = 0; i < field->nbuf; i++) {
153 			if (field->buffers[i].length > field->cols)
154 				field->buffers[i].string[field->cols] = '\0';
155 		}
156 	}
157 
158 	fp->opts |= options;
159 
160 	return E_OK;
161 }
162 
163 /*
164  * Turn off the passed field options.
165  */
166 int
167 field_opts_off(FIELD *field, Form_Options options)
168 {
169 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
170 
171 	  /* not allowed to set opts if the field is the current one */
172 	if ((field != NULL) && (field->parent != NULL) &&
173 	    (field->parent->cur_field == field->index))
174 		return E_CURRENT;
175 
176 	fp->opts &= ~options;
177 	return E_OK;
178 }
179 
180 /*
181  * Return the field options associated with the passed field.
182  */
183 Form_Options
184 field_opts(FIELD *field)
185 {
186 	if (field == NULL)
187 		return _formi_default_field.opts;
188 	else
189 		return field->opts;
190 }
191 
192 /*
193  * Set the justification for the passed field.
194  */
195 int
196 set_field_just(FIELD *field, int justification)
197 {
198 	FIELD *fp = (field == NULL) ? &_formi_default_field : field;
199 
200 	  /*
201 	   * not allowed to set justification if the field is
202 	   * the current one
203 	   */
204 	if ((field != NULL) && (field->parent != NULL) &&
205 	    (field->parent->cur_field == field->index))
206 		return E_CURRENT;
207 
208 	if ((justification < MIN_JUST_STYLE) /* check justification valid */
209 	    || (justification > MAX_JUST_STYLE))
210 		return E_BAD_ARGUMENT;
211 
212 	fp->justification = justification;
213 	return E_OK;
214 }
215 
216 /*
217  * Return the justification style of the field passed.
218  */
219 int
220 field_just(FIELD *field)
221 {
222 	if (field == NULL)
223 		return _formi_default_field.justification;
224 	else
225 		return field->justification;
226 }
227 
228 /*
229  * Return information about the field passed.
230  */
231 int
232 field_info(FIELD *field, int *rows, int *cols, int *frow, int *fcol,
233 	   int *nrow, int *nbuf)
234 {
235 	if (field == NULL)
236 		return E_BAD_ARGUMENT;
237 
238 	*rows = field->rows;
239 	*cols = field->cols;
240 	*frow = field->form_row;
241 	*fcol = field->form_col;
242 	*nrow = field->nrows;
243 	*nbuf = field->nbuf;
244 
245 	return E_OK;
246 }
247 
248 /*
249  * Report the dynamic field information.
250  */
251 int
252 dynamic_field_info(FIELD *field, int *drows, int *dcols, int *max)
253 {
254 	if (field == NULL)
255 		return E_BAD_ARGUMENT;
256 
257 	if ((field->opts & O_STATIC) == O_STATIC) {
258 		*drows = field->rows;
259 		*dcols = field->cols;
260 	} else {
261 		*drows = field->drows;
262 		*dcols = field->dcols;
263 	}
264 
265 	*max = field->max;
266 
267 	return E_OK;
268 }
269 
270 /*
271  * Set the value of the field buffer to the value given.
272  */
273 
274 int
275 set_field_buffer(FIELD *field, int buffer, char *value)
276 {
277 	unsigned len;
278 	int status;
279 
280 	if (field == NULL)
281 		return E_BAD_ARGUMENT;
282 
283 	if (buffer >= field->nbuf) /* make sure buffer is valid */
284 		return E_BAD_ARGUMENT;
285 
286 	len = strlen(value);
287 	if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
288 	    && ((field->rows + field->nrows) == 1))
289 		len = field->cols;
290 
291 #ifdef DEBUG
292 	if (_formi_create_dbg_file() != E_OK)
293 		return E_SYSTEM_ERROR;
294 
295 	fprintf(dbg,
296 		"set_field_buffer: entry: len = %d, value = %s, buffer=%d\n",
297 		len, value, buffer);
298 	fprintf(dbg, "set_field_buffer: entry: string = ");
299 	if (field->buffers[buffer].string != NULL)
300 		fprintf(dbg, "%s, len = %d\n", field->buffers[buffer].string,
301 			field->buffers[buffer].length);
302 	else
303 		fprintf(dbg, "(null), len = 0\n");
304 	fprintf(dbg, "set_field_buffer: entry: lines.len = %d\n",
305 		field->lines[0].length);
306 #endif
307 
308 	if ((field->buffers[buffer].string =
309 	     (char *) realloc(field->buffers[buffer].string, len + 1)) == NULL)
310 		return E_SYSTEM_ERROR;
311 
312 	strlcpy(field->buffers[buffer].string, value, len + 1);
313 	field->buffers[buffer].length = len;
314 	field->buffers[buffer].allocated = len + 1;
315 
316 	if (buffer == 0) {
317 		field->row_count = 1; /* must be at least one row */
318 		field->lines[0].start = 0;
319 		field->lines[0].end = (len > 0)? (len - 1) : 0;
320 		field->lines[0].length = len;
321 
322 		  /* we have to hope the wrap works - if it does not then the
323 		     buffer is pretty much borked */
324 		status = _formi_wrap_field(field, 0);
325 		if (status != E_OK)
326 			return status;
327 
328 		  /* redraw the field to reflect the new contents. If the field
329 		   * is attached....
330 		   */
331 		if (field->parent != NULL)
332 			_formi_redraw_field(field->parent, field->index);
333 	}
334 
335 #ifdef DEBUG
336 	fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
337 		len, value);
338 	fprintf(dbg, "set_field_buffer: exit: string = %s, len = %d\n",
339 		field->buffers[buffer].string, field->buffers[buffer].length);
340 	fprintf(dbg, "set_field_buffer: exit: lines.len = %d\n",
341 		field->lines[0].length);
342 #endif
343 
344 	return E_OK;
345 }
346 
347 /*
348  * Return the requested field buffer to the caller.
349  */
350 char *
351 field_buffer(FIELD *field, int buffer)
352 {
353 
354 	if (field == NULL)
355 		return NULL;
356 
357 	if (buffer >= field->nbuf)
358 		return NULL;
359 
360 	return field->buffers[buffer].string;
361 }
362 
363 /*
364  * Set the buffer 0 field status.
365  */
366 int
367 set_field_status(FIELD *field, int status)
368 {
369 
370 	if (field == NULL)
371 		return E_BAD_ARGUMENT;
372 
373 	if (status != FALSE)
374 		field->buf0_status = TRUE;
375 	else
376 		field->buf0_status = FALSE;
377 
378 	return E_OK;
379 }
380 
381 /*
382  * Return the buffer 0 status flag for the given field.
383  */
384 int
385 field_status(FIELD *field)
386 {
387 
388 	if (field == NULL) /* the default buffer 0 never changes :-) */
389 		return FALSE;
390 
391 	return field->buf0_status;
392 }
393 
394 /*
395  * Set the maximum growth for a dynamic field.
396  */
397 int
398 set_max_field(FIELD *fptr, int max)
399 {
400 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
401 
402 	if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
403 		return E_BAD_ARGUMENT;
404 
405 	if (max < 0) /* negative numbers are bad.... */
406 		return E_BAD_ARGUMENT;
407 
408 	field->max = max;
409 	return E_OK;
410 }
411 
412 /*
413  * Set the field foreground character attributes.
414  */
415 int
416 set_field_fore(FIELD *fptr, chtype attribute)
417 {
418 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
419 
420 	field->fore = attribute;
421 	return E_OK;
422 }
423 
424 /*
425  * Return the foreground character attribute for the given field.
426  */
427 chtype
428 field_fore(FIELD *field)
429 {
430 	if (field == NULL)
431 		return _formi_default_field.fore;
432 	else
433 		return field->fore;
434 }
435 
436 /*
437  * Set the background character attribute for the given field.
438  */
439 int
440 set_field_back(FIELD *field, chtype attribute)
441 {
442 	if (field == NULL)
443 		_formi_default_field.back = attribute;
444 	else
445 		field->back = attribute;
446 
447 	return E_OK;
448 }
449 
450 /*
451  * Get the background character attribute for the given field.
452  */
453 chtype
454 field_back(FIELD *field)
455 {
456 	if (field == NULL)
457 		return _formi_default_field.back;
458 	else
459 		return field->back;
460 }
461 
462 /*
463  * Set the pad character for the given field.
464  */
465 int
466 set_field_pad(FIELD *field, int pad)
467 {
468 	if (field == NULL)
469 		_formi_default_field.pad = pad;
470 	else
471 		field->pad = pad;
472 
473 	return E_OK;
474 }
475 
476 /*
477  * Return the padding character for the given field.
478  */
479 int
480 field_pad(FIELD *field)
481 {
482 	if (field == NULL)
483 		return _formi_default_field.pad;
484 	else
485 		return field->pad;
486 }
487 
488 /*
489  * Set the field initialisation function hook.
490  */
491 int
492 set_field_init(FORM *form, Form_Hook function)
493 {
494 	if (form == NULL)
495 		_formi_default_form.field_init = function;
496 	else
497 		form->field_init = function;
498 
499 	return E_OK;
500 }
501 
502 /*
503  * Return the function hook for the field initialisation.
504  */
505 Form_Hook
506 field_init(FORM *form)
507 {
508 	if (form == NULL)
509 		return _formi_default_form.field_init;
510 	else
511 		return form->field_init;
512 }
513 
514 /*
515  * Set the field termination function hook.
516  */
517 int
518 set_field_term(FORM *form, Form_Hook function)
519 {
520 	if (form == NULL)
521 		_formi_default_form.field_term = function;
522 	else
523 		form->field_term = function;
524 
525 	return E_OK;
526 }
527 
528 /*
529  * Return the function hook defined for the field termination.
530  */
531 Form_Hook
532 field_term(FORM *form)
533 {
534 	if (form == NULL)
535 		return _formi_default_form.field_term;
536 	else
537 		return form->field_term;
538 }
539 
540 /*
541  * Set the page flag on the given field to indicate it is the start of a
542  * new page.
543  */
544 int
545 set_new_page(FIELD *fptr, int page)
546 {
547 	FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
548 
549 	if (field->parent != NULL) /* check if field is connected to a form */
550 		return E_CONNECTED;
551 
552 	field->page_break = (page != FALSE);
553 	return E_OK;
554 }
555 
556 /*
557  * Return the page status for the given field.  TRUE is returned if the
558  * field is the start of a new page.
559  */
560 int
561 new_page(FIELD *field)
562 {
563 	if (field == NULL)
564 		return _formi_default_field.page_break;
565 	else
566 		return field->page_break;
567 }
568 
569 /*
570  * Return the index of the field in the form fields array.
571  */
572 int
573 field_index(FIELD *field)
574 {
575 	if (field == NULL)
576 		return E_BAD_ARGUMENT;
577 
578 	if (field->parent == NULL)
579 		return E_NOT_CONNECTED;
580 
581 	return field->index;
582 }
583 
584 /*
585  * Internal function that does most of the work to create a new field.
586  * The new field is initialised from the information in the prototype
587  * field passed.
588  * Returns NULL on error.
589  */
590 static FIELD *
591 _formi_create_field(FIELD *prototype, int rows, int cols, int frow,
592 		    int fcol, int nrows, int nbuf)
593 {
594 	FIELD *new;
595 
596 	if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) ||
597 	    (nrows < 0) || (nbuf < 0))
598 		return NULL;
599 
600 	if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
601 		return NULL;
602 	}
603 
604 	  /* copy in the default field info */
605 	bcopy(prototype, new, sizeof(FIELD));
606 
607 	new->nbuf = nbuf + 1;
608 	new->rows = rows;
609 	new->cols = cols;
610 	new->form_row = frow;
611 	new->form_col = fcol;
612 	new->nrows = nrows;
613 	new->link = new;
614 	return new;
615 }
616 
617 /*
618  * Create a new field structure.
619  */
620 FIELD *
621 new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
622 {
623 	FIELD *new;
624 	size_t buf_len;
625 	int i;
626 
627 
628 	if ((new = _formi_create_field(&_formi_default_field, rows, cols,
629 				       frow, fcol, nrows, nbuf)) == NULL)
630 		return NULL;
631 
632 	buf_len = (nbuf + 1) * sizeof(FORM_STR);
633 
634 	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
635 		free(new);
636 		return NULL;
637 	}
638 
639 	  /* Initialise the strings to a zero length string */
640 	for (i = 0; i < nbuf + 1; i++) {
641 		if ((new->buffers[i].string =
642 		     (char *) malloc(sizeof(char))) == NULL) {
643 			free(new->buffers);
644 			free(new);
645 			return NULL;
646 		}
647 		new->buffers[i].string[0] = '\0';
648 		new->buffers[i].length = 0;
649 		new->buffers[i].allocated = 1;
650 	}
651 
652 	if ((new->lines = (_FORMI_FIELD_LINES *)
653 	     malloc(sizeof(struct _formi_field_lines))) == NULL) {
654 		free(new->buffers);
655 		free(new);
656 		return NULL;
657 	}
658 
659 	new->lines_alloced = 1;
660 	new->lines[0].length = 0;
661 	new->lines[0].start = 0;
662 	new->lines[0].end = 0;
663 
664 	return new;
665 }
666 
667 /*
668  * Duplicate the given field, including it's buffers.
669  */
670 FIELD *
671 dup_field(FIELD *field, int frow, int fcol)
672 {
673 	FIELD *new;
674 	size_t row_len, buf_len;
675 
676 	if (field == NULL)
677 		return NULL;
678 
679 	  /* XXXX this right???? */
680 	if ((new = _formi_create_field(field, (int) field->rows,
681 				       (int ) field->cols,
682 				       frow, fcol, (int) field->nrows,
683 				       field->nbuf - 1)) == NULL)
684 		return NULL;
685 
686 	row_len = (field->rows + field->nrows + 1) * field->cols;
687 	buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
688 
689 	if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
690 		free(new);
691 		return NULL;
692 	}
693 
694 	  /* copy the buffers from the source field into the new copy */
695 	bcopy(field->buffers, new->buffers, buf_len);
696 
697 	return new;
698 }
699 
700 /*
701  * Create a new field at the specified location by duplicating the given
702  * field.  The buffers are shared with the parent field.
703  */
704 FIELD *
705 link_field(FIELD *field, int frow, int fcol)
706 {
707 	FIELD *new;
708 
709 	if (field == NULL)
710 		return NULL;
711 
712 	if ((new = _formi_create_field(field, (int) field->rows,
713 				       (int) field->cols,
714 				       frow, fcol, (int) field->nrows,
715 				       field->nbuf - 1)) == NULL)
716 		return NULL;
717 
718 	new->link = field->link;
719 	field->link = new;
720 
721 	  /* we are done.  The buffer pointer was copied during the field
722 	     creation. */
723 	return new;
724 }
725 
726 /*
727  * Release all storage allocated to the field
728  */
729 int
730 free_field(FIELD *field)
731 {
732 	FIELD *flink;
733 
734 	if (field == NULL)
735 		return E_BAD_ARGUMENT;
736 
737 	if (field->parent != NULL)
738 		return E_CONNECTED;
739 
740 	if (field->link == field) { /* check if field linked */
741 		  /* no it is not - release the buffers */
742 		free(field->buffers);
743 	} else {
744 		  /* is linked, traverse the links to find the field referring
745 		   * to the one to be freed.
746 		   */
747 		for (flink = field->link; flink != field; flink = flink->link);
748 		flink->link = field->link;
749 	}
750 
751 	free(field);
752 	return E_OK;
753 }
754 
755 
756