2013-11-21 13:40:19 +04:00
|
|
|
/* $NetBSD: field.c,v 1.26 2013/11/21 09:40:19 blymn Exp $ */
|
2000-12-17 15:04:30 +03:00
|
|
|
/*-
|
|
|
|
* 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
|
2001-06-13 14:45:57 +04:00
|
|
|
* derived from this software without specific prior written permission
|
2000-12-17 15:04:30 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-03-09 03:57:17 +03:00
|
|
|
#include <sys/cdefs.h>
|
2013-11-21 13:40:19 +04:00
|
|
|
__RCSID("$NetBSD: field.c,v 1.26 2013/11/21 09:40:19 blymn Exp $");
|
2003-03-09 03:57:17 +03:00
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
2002-08-09 18:15:12 +04:00
|
|
|
#include <stdarg.h>
|
2000-12-17 15:04:30 +03:00
|
|
|
#include <form.h>
|
|
|
|
#include "internals.h"
|
|
|
|
|
|
|
|
extern FORM _formi_default_form;
|
|
|
|
|
|
|
|
FIELD _formi_default_field = {
|
|
|
|
0, /* rows in the field */
|
|
|
|
0, /* columns in the field */
|
|
|
|
0, /* dynamic rows */
|
|
|
|
0, /* dynamic columns */
|
|
|
|
0, /* maximum growth */
|
|
|
|
0, /* starting row in the form subwindow */
|
|
|
|
0, /* starting column in the form subwindow */
|
|
|
|
0, /* number of off screen rows */
|
|
|
|
0, /* index of this field in form fields array. */
|
|
|
|
0, /* number of buffers associated with this field */
|
|
|
|
FALSE, /* set to true if buffer 0 has changed. */
|
|
|
|
NO_JUSTIFICATION, /* justification style of the field */
|
|
|
|
FALSE, /* set to true if field is in overlay mode */
|
2004-11-24 14:57:09 +03:00
|
|
|
NULL, /* pointer to the current line cursor is on */
|
2000-12-17 15:04:30 +03:00
|
|
|
0, /* starting char in string (horiz scroll) */
|
2004-11-24 14:57:09 +03:00
|
|
|
NULL, /* starting line in field (vert scroll) */
|
2000-12-17 15:04:30 +03:00
|
|
|
0, /* number of rows actually used in field */
|
2002-05-20 19:00:11 +04:00
|
|
|
0, /* actual pos of cursor in row, not same as x pos due to tabs */
|
2000-12-17 15:04:30 +03:00
|
|
|
0, /* x pos of cursor in field */
|
|
|
|
0, /* y pos of cursor in field */
|
|
|
|
0, /* start of a new page on the form if 1 */
|
|
|
|
0, /* number of the page this field is on */
|
|
|
|
A_NORMAL, /* character attributes for the foreground */
|
2001-01-16 04:02:47 +03:00
|
|
|
A_NORMAL, /* character attributes for the background */
|
2000-12-17 15:04:30 +03:00
|
|
|
' ', /* padding character */
|
|
|
|
DEFAULT_FORM_OPTS, /* options for the field */
|
|
|
|
NULL, /* the form this field is bound to, if any */
|
|
|
|
NULL, /* field above this one */
|
|
|
|
NULL, /* field below this one */
|
|
|
|
NULL, /* field to the left of this one */
|
|
|
|
NULL, /* field to the right of this one */
|
|
|
|
NULL, /* user defined pointer. */
|
|
|
|
NULL, /* used if fields are linked */
|
|
|
|
NULL, /* type struct for the field */
|
|
|
|
{NULL, NULL}, /* circle queue glue for sorting fields */
|
|
|
|
NULL, /* args for field type. */
|
2001-05-11 18:04:48 +04:00
|
|
|
NULL, /* pointer to the array of lines structures. */
|
2004-11-24 14:57:09 +03:00
|
|
|
NULL, /* list of lines available for reuse */
|
2000-12-17 15:04:30 +03:00
|
|
|
NULL, /* array of buffers for the field */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* internal function prototypes */
|
2002-08-09 18:15:12 +04:00
|
|
|
static int
|
2004-11-24 14:57:09 +03:00
|
|
|
field_buffer_init(FIELD *field, int buffer, unsigned int len);
|
2000-12-17 15:04:30 +03:00
|
|
|
static FIELD *
|
|
|
|
_formi_create_field(FIELD *, int, int, int, int, int, int);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the userptr for the field
|
|
|
|
*/
|
|
|
|
int
|
2001-01-04 15:30:37 +03:00
|
|
|
set_field_userptr(FIELD *field, void *ptr)
|
2000-12-17 15:04:30 +03:00
|
|
|
{
|
|
|
|
FIELD *fp = (field == NULL) ? &_formi_default_field : field;
|
|
|
|
|
|
|
|
fp->userptr = ptr;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the userptr for the field.
|
|
|
|
*/
|
|
|
|
|
2001-01-04 15:30:37 +03:00
|
|
|
void *
|
2000-12-17 15:04:30 +03:00
|
|
|
field_userptr(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return _formi_default_field.userptr;
|
|
|
|
else
|
|
|
|
return field->userptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the options for the designated field.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_opts(FIELD *field, Form_Options options)
|
|
|
|
{
|
2001-03-25 16:21:06 +04:00
|
|
|
int i;
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
FIELD *fp = (field == NULL) ? &_formi_default_field : field;
|
|
|
|
|
2001-03-25 16:21:06 +04:00
|
|
|
/* not allowed to set opts if the field is the current one */
|
|
|
|
if ((field != NULL) && (field->parent != NULL) &&
|
|
|
|
(field->parent->cur_field == field->index))
|
|
|
|
return E_CURRENT;
|
|
|
|
|
|
|
|
if ((options & O_STATIC) == O_STATIC) {
|
2002-08-05 16:43:44 +04:00
|
|
|
for (i = 0; i < fp->nbuf; i++) {
|
|
|
|
if (fp->buffers[i].length > fp->cols)
|
|
|
|
fp->buffers[i].string[fp->cols] = '\0';
|
2001-03-25 16:21:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
fp->opts = options;
|
|
|
|
|
2002-08-03 18:34:22 +04:00
|
|
|
/* if appropriate, redraw the field */
|
|
|
|
if ((field != NULL) && (field->parent != NULL)
|
|
|
|
&& (field->parent->posted == 1)) {
|
|
|
|
_formi_redraw_field(field->parent, field->index);
|
|
|
|
pos_form_cursor(field->parent);
|
|
|
|
wrefresh(field->parent->scrwin);
|
|
|
|
}
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn on the passed field options.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
field_opts_on(FIELD *field, Form_Options options)
|
|
|
|
{
|
2001-03-25 16:21:06 +04:00
|
|
|
int i;
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
FIELD *fp = (field == NULL) ? &_formi_default_field : field;
|
|
|
|
|
2001-03-25 16:21:06 +04:00
|
|
|
/* not allowed to set opts if the field is the current one */
|
|
|
|
if ((field != NULL) && (field->parent != NULL) &&
|
|
|
|
(field->parent->cur_field == field->index))
|
|
|
|
return E_CURRENT;
|
|
|
|
|
|
|
|
if ((options & O_STATIC) == O_STATIC) {
|
2002-08-05 16:43:44 +04:00
|
|
|
for (i = 0; i < fp->nbuf; i++) {
|
|
|
|
if (fp->buffers[i].length > fp->cols)
|
|
|
|
fp->buffers[i].string[fp->cols] = '\0';
|
2001-03-25 16:21:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
fp->opts |= options;
|
2002-08-03 18:34:22 +04:00
|
|
|
|
|
|
|
/* if appropriate, redraw the field */
|
|
|
|
if ((field != NULL) && (field->parent != NULL)
|
|
|
|
&& (field->parent->posted == 1)) {
|
|
|
|
_formi_redraw_field(field->parent, field->index);
|
|
|
|
pos_form_cursor(field->parent);
|
|
|
|
wrefresh(field->parent->scrwin);
|
|
|
|
}
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Turn off the passed field options.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
field_opts_off(FIELD *field, Form_Options options)
|
|
|
|
{
|
|
|
|
FIELD *fp = (field == NULL) ? &_formi_default_field : field;
|
|
|
|
|
2001-03-25 16:21:06 +04:00
|
|
|
/* not allowed to set opts if the field is the current one */
|
|
|
|
if ((field != NULL) && (field->parent != NULL) &&
|
|
|
|
(field->parent->cur_field == field->index))
|
|
|
|
return E_CURRENT;
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
fp->opts &= ~options;
|
2002-08-03 18:34:22 +04:00
|
|
|
|
|
|
|
/* if appropriate, redraw the field */
|
|
|
|
if ((field != NULL) && (field->parent != NULL)
|
|
|
|
&& (field->parent->posted == 1)) {
|
|
|
|
_formi_redraw_field(field->parent, field->index);
|
|
|
|
pos_form_cursor(field->parent);
|
|
|
|
wrefresh(field->parent->scrwin);
|
|
|
|
}
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the field options associated with the passed field.
|
|
|
|
*/
|
|
|
|
Form_Options
|
|
|
|
field_opts(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return _formi_default_field.opts;
|
|
|
|
else
|
|
|
|
return field->opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the justification for the passed field.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_just(FIELD *field, int justification)
|
|
|
|
{
|
|
|
|
FIELD *fp = (field == NULL) ? &_formi_default_field : field;
|
|
|
|
|
2001-03-25 16:21:06 +04:00
|
|
|
/*
|
|
|
|
* not allowed to set justification if the field is
|
|
|
|
* the current one
|
|
|
|
*/
|
|
|
|
if ((field != NULL) && (field->parent != NULL) &&
|
|
|
|
(field->parent->cur_field == field->index))
|
|
|
|
return E_CURRENT;
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
if ((justification < MIN_JUST_STYLE) /* check justification valid */
|
|
|
|
|| (justification > MAX_JUST_STYLE))
|
|
|
|
return E_BAD_ARGUMENT;
|
2002-07-29 09:17:37 +04:00
|
|
|
|
|
|
|
/* only allow justification on static, single row fields */
|
|
|
|
if (((fp->opts & O_STATIC) != O_STATIC) ||
|
|
|
|
((fp->rows + fp->nrows) > 1))
|
|
|
|
return E_BAD_ARGUMENT;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
fp->justification = justification;
|
2002-07-29 09:17:37 +04:00
|
|
|
|
|
|
|
_formi_init_field_xpos(fp);
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the justification style of the field passed.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
field_just(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return _formi_default_field.justification;
|
|
|
|
else
|
|
|
|
return field->justification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return information about the field passed.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
field_info(FIELD *field, int *rows, int *cols, int *frow, int *fcol,
|
|
|
|
int *nrow, int *nbuf)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
*rows = field->rows;
|
|
|
|
*cols = field->cols;
|
|
|
|
*frow = field->form_row;
|
|
|
|
*fcol = field->form_col;
|
|
|
|
*nrow = field->nrows;
|
|
|
|
*nbuf = field->nbuf;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report the dynamic field information.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
dynamic_field_info(FIELD *field, int *drows, int *dcols, int *max)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
2001-03-25 16:21:06 +04:00
|
|
|
if ((field->opts & O_STATIC) == O_STATIC) {
|
|
|
|
*drows = field->rows;
|
|
|
|
*dcols = field->cols;
|
|
|
|
} else {
|
|
|
|
*drows = field->drows;
|
|
|
|
*dcols = field->dcols;
|
|
|
|
}
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
*max = field->max;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
2002-08-09 18:15:12 +04:00
|
|
|
/*
|
|
|
|
* Init all the field variables, perform wrapping and other tasks
|
|
|
|
* after the field buffer is set.
|
|
|
|
*/
|
|
|
|
static int
|
2004-11-24 14:57:09 +03:00
|
|
|
field_buffer_init(FIELD *field, int buffer, unsigned int len)
|
2002-08-09 18:15:12 +04:00
|
|
|
{
|
|
|
|
int status;
|
2004-11-24 14:57:09 +03:00
|
|
|
char *newp;
|
2002-08-09 18:15:12 +04:00
|
|
|
|
|
|
|
if (buffer == 0) {
|
|
|
|
field->start_char = 0;
|
|
|
|
field->start_line = 0;
|
|
|
|
field->row_xpos = 0;
|
|
|
|
field->cursor_xpos = 0;
|
|
|
|
field->cursor_ypos = 0;
|
2004-11-24 14:57:09 +03:00
|
|
|
field->row_count = 1; /* must be at least one row XXX need to shift old rows (if any) to free list??? */
|
2010-02-03 18:34:37 +03:00
|
|
|
field->alines->length = len;
|
|
|
|
if ((newp = realloc(field->alines->string,
|
2006-02-07 23:07:42 +03:00
|
|
|
(size_t) len + 1)) == NULL)
|
2004-11-24 14:57:09 +03:00
|
|
|
return E_SYSTEM_ERROR;
|
2010-02-03 18:34:37 +03:00
|
|
|
field->alines->string = newp;
|
|
|
|
field->alines->allocated = len + 1;
|
|
|
|
strlcpy(field->alines->string, field->buffers[buffer].string,
|
2006-02-07 23:07:42 +03:00
|
|
|
(size_t) len + 1);
|
2010-02-03 18:34:37 +03:00
|
|
|
field->alines->expanded =
|
|
|
|
_formi_tab_expanded_length(field->alines->string,
|
|
|
|
0, field->alines->length);
|
2004-11-24 14:57:09 +03:00
|
|
|
|
2010-02-03 18:34:37 +03:00
|
|
|
field->start_line = field->alines;
|
|
|
|
field->cur_line = field->alines;
|
2004-11-24 14:57:09 +03:00
|
|
|
|
2002-08-09 18:15:12 +04:00
|
|
|
/* we have to hope the wrap works - if it does not then the
|
|
|
|
buffer is pretty much borked */
|
2004-11-24 14:57:09 +03:00
|
|
|
status = _formi_wrap_field(field, field->cur_line);
|
2002-08-09 18:15:12 +04:00
|
|
|
if (status != E_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calculate the tabs for a single row field, the
|
|
|
|
* multiline case is handled when the wrap is done.
|
|
|
|
*/
|
|
|
|
if (field->row_count == 1)
|
2010-02-03 18:34:37 +03:00
|
|
|
_formi_calculate_tabs(field->alines);
|
2002-08-09 18:15:12 +04:00
|
|
|
|
|
|
|
/* redraw the field to reflect the new contents. If the field
|
|
|
|
* is attached....
|
|
|
|
*/
|
|
|
|
if ((field->parent != NULL) && (field->parent->posted == 1)) {
|
|
|
|
_formi_redraw_field(field->parent, field->index);
|
|
|
|
/* make sure cursor goes back to current field */
|
|
|
|
pos_form_cursor(field->parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the field buffer to the string that results from processing
|
|
|
|
* the given format (fmt) using sprintf.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_printf(FIELD *field, int buffer, char *fmt, ...)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (field == NULL)
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
if (buffer >= field->nbuf)
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
/* check for buffer already existing, free the storage */
|
|
|
|
if (field->buffers[buffer].allocated != 0)
|
|
|
|
free(field->buffers[buffer].string);
|
|
|
|
|
|
|
|
len = vasprintf(&field->buffers[buffer].string, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
if (len < 0)
|
|
|
|
return E_SYSTEM_ERROR;
|
|
|
|
|
|
|
|
field->buffers[buffer].length = len;
|
|
|
|
field->buffers[buffer].allocated = len + 1;
|
|
|
|
if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
|
|
|
|
&& ((field->rows + field->nrows) == 1))
|
|
|
|
len = field->cols;
|
|
|
|
|
|
|
|
field->buffers[buffer].string[len] = '\0';
|
|
|
|
return field_buffer_init(field, buffer, (unsigned int) len);
|
|
|
|
}
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
/*
|
|
|
|
* Set the value of the field buffer to the value given.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
set_field_buffer(FIELD *field, int buffer, char *value)
|
|
|
|
{
|
2004-11-24 14:57:09 +03:00
|
|
|
unsigned int len;
|
2001-02-03 15:35:14 +03:00
|
|
|
int status;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
if (field == NULL)
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
if (buffer >= field->nbuf) /* make sure buffer is valid */
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
2004-11-24 14:57:09 +03:00
|
|
|
len = (unsigned int) strlen(value);
|
2001-05-11 18:04:48 +04:00
|
|
|
if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
|
|
|
|
&& ((field->rows + field->nrows) == 1))
|
2001-02-15 08:20:42 +03:00
|
|
|
len = field->cols;
|
2001-06-04 15:42:09 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (_formi_create_dbg_file() != E_OK)
|
|
|
|
return E_SYSTEM_ERROR;
|
|
|
|
|
|
|
|
fprintf(dbg,
|
|
|
|
"set_field_buffer: entry: len = %d, value = %s, buffer=%d\n",
|
|
|
|
len, value, buffer);
|
|
|
|
fprintf(dbg, "set_field_buffer: entry: string = ");
|
|
|
|
if (field->buffers[buffer].string != NULL)
|
|
|
|
fprintf(dbg, "%s, len = %d\n", field->buffers[buffer].string,
|
|
|
|
field->buffers[buffer].length);
|
|
|
|
else
|
|
|
|
fprintf(dbg, "(null), len = 0\n");
|
|
|
|
fprintf(dbg, "set_field_buffer: entry: lines.len = %d\n",
|
2010-02-03 18:34:37 +03:00
|
|
|
field->alines[0].length);
|
2001-06-04 15:42:09 +04:00
|
|
|
#endif
|
|
|
|
|
2001-02-15 08:20:42 +03:00
|
|
|
if ((field->buffers[buffer].string =
|
2004-11-24 14:57:09 +03:00
|
|
|
(char *) realloc(field->buffers[buffer].string,
|
|
|
|
(size_t) len + 1)) == NULL)
|
2000-12-17 15:04:30 +03:00
|
|
|
return E_SYSTEM_ERROR;
|
|
|
|
|
2004-11-24 14:57:09 +03:00
|
|
|
strlcpy(field->buffers[buffer].string, value, (size_t) len + 1);
|
2000-12-17 15:04:30 +03:00
|
|
|
field->buffers[buffer].length = len;
|
|
|
|
field->buffers[buffer].allocated = len + 1;
|
2002-08-09 18:15:12 +04:00
|
|
|
status = field_buffer_init(field, buffer, len);
|
2001-06-04 15:42:09 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(dbg, "set_field_buffer: exit: len = %d, value = %s\n",
|
|
|
|
len, value);
|
|
|
|
fprintf(dbg, "set_field_buffer: exit: string = %s, len = %d\n",
|
|
|
|
field->buffers[buffer].string, field->buffers[buffer].length);
|
|
|
|
fprintf(dbg, "set_field_buffer: exit: lines.len = %d\n",
|
2010-02-03 18:34:37 +03:00
|
|
|
field->alines[0].length);
|
2001-06-04 15:42:09 +04:00
|
|
|
#endif
|
2000-12-17 15:04:30 +03:00
|
|
|
|
2002-08-09 18:15:12 +04:00
|
|
|
return status;
|
2000-12-17 15:04:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the requested field buffer to the caller.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
field_buffer(FIELD *field, int buffer)
|
|
|
|
{
|
|
|
|
|
2004-11-24 14:57:09 +03:00
|
|
|
char *reformat, *p;
|
|
|
|
_FORMI_FIELD_LINES *linep;
|
2013-11-21 13:40:19 +04:00
|
|
|
size_t bufsize;
|
2004-11-24 14:57:09 +03:00
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
if (field == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (buffer >= field->nbuf)
|
|
|
|
return NULL;
|
2004-11-24 14:57:09 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We force a sync from the line structs to the buffer here.
|
|
|
|
* Traditional libform say we don't need to because it is
|
|
|
|
* done on a REQ_VALIDATE but NetBSD libform previously did
|
|
|
|
* not enforce this because the buffer contents were always
|
|
|
|
* current. Changes to line handling make this no longer so
|
|
|
|
* - the line structs may contain different data to the
|
|
|
|
* buffer if unsynced.
|
|
|
|
*/
|
|
|
|
if (_formi_sync_buffer(field) != E_OK)
|
|
|
|
return NULL;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
2004-11-24 14:57:09 +03:00
|
|
|
if ((field->opts & O_REFORMAT) != O_REFORMAT) {
|
|
|
|
return field->buffers[buffer].string;
|
|
|
|
} else {
|
|
|
|
if (field->row_count > 1) {
|
|
|
|
|
2013-11-21 13:40:19 +04:00
|
|
|
/*
|
|
|
|
* compute reformat buffer size
|
|
|
|
* each line length plus one line feed
|
|
|
|
* except for last line without line feed
|
|
|
|
* but plus one NUL character
|
|
|
|
*/
|
|
|
|
bufsize = 0;
|
|
|
|
for (linep=field->alines; linep; linep=linep->next)
|
|
|
|
bufsize += strlen(linep->string)+1;
|
|
|
|
|
|
|
|
reformat = (char *)malloc(bufsize);
|
2004-11-24 14:57:09 +03:00
|
|
|
if (reformat == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* foreach row copy line, append newline, no
|
|
|
|
* newline on last row.
|
|
|
|
*/
|
|
|
|
p = reformat;
|
2013-11-21 13:40:19 +04:00
|
|
|
for (linep=field->alines; linep; linep=linep->next) {
|
|
|
|
strcpy(p, linep->string);
|
|
|
|
p += strlen(linep->string);
|
|
|
|
if (linep->next)
|
|
|
|
*p++ = '\n';
|
2004-11-24 14:57:09 +03:00
|
|
|
}
|
2013-11-21 13:40:19 +04:00
|
|
|
*p = '\0';
|
2004-11-24 14:57:09 +03:00
|
|
|
|
|
|
|
return reformat;
|
|
|
|
} else {
|
|
|
|
asprintf(&reformat, "%s",
|
|
|
|
field->buffers[buffer].string);
|
|
|
|
return reformat;
|
|
|
|
}
|
|
|
|
}
|
2000-12-17 15:04:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the buffer 0 field status.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_status(FIELD *field, int status)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (field == NULL)
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
if (status != FALSE)
|
|
|
|
field->buf0_status = TRUE;
|
|
|
|
else
|
|
|
|
field->buf0_status = FALSE;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the buffer 0 status flag for the given field.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
field_status(FIELD *field)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (field == NULL) /* the default buffer 0 never changes :-) */
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return field->buf0_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the maximum growth for a dynamic field.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_max_field(FIELD *fptr, int max)
|
|
|
|
{
|
2001-06-28 15:38:19 +04:00
|
|
|
FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
if (max < 0) /* negative numbers are bad.... */
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
field->max = max;
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the field foreground character attributes.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_fore(FIELD *fptr, chtype attribute)
|
|
|
|
{
|
|
|
|
FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
|
|
|
|
|
|
|
|
field->fore = attribute;
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the foreground character attribute for the given field.
|
|
|
|
*/
|
|
|
|
chtype
|
|
|
|
field_fore(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return _formi_default_field.fore;
|
|
|
|
else
|
|
|
|
return field->fore;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the background character attribute for the given field.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_back(FIELD *field, chtype attribute)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
_formi_default_field.back = attribute;
|
|
|
|
else
|
|
|
|
field->back = attribute;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
2001-07-08 16:15:06 +04:00
|
|
|
/*
|
|
|
|
* Get the background character attribute for the given field.
|
|
|
|
*/
|
|
|
|
chtype
|
|
|
|
field_back(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return _formi_default_field.back;
|
|
|
|
else
|
|
|
|
return field->back;
|
|
|
|
}
|
|
|
|
|
2000-12-17 15:04:30 +03:00
|
|
|
/*
|
|
|
|
* Set the pad character for the given field.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_pad(FIELD *field, int pad)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
_formi_default_field.pad = pad;
|
|
|
|
else
|
|
|
|
field->pad = pad;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the padding character for the given field.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
field_pad(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return _formi_default_field.pad;
|
|
|
|
else
|
|
|
|
return field->pad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the field initialisation function hook.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_init(FORM *form, Form_Hook function)
|
|
|
|
{
|
|
|
|
if (form == NULL)
|
|
|
|
_formi_default_form.field_init = function;
|
|
|
|
else
|
|
|
|
form->field_init = function;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the function hook for the field initialisation.
|
|
|
|
*/
|
|
|
|
Form_Hook
|
|
|
|
field_init(FORM *form)
|
|
|
|
{
|
|
|
|
if (form == NULL)
|
|
|
|
return _formi_default_form.field_init;
|
|
|
|
else
|
|
|
|
return form->field_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the field termination function hook.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_field_term(FORM *form, Form_Hook function)
|
|
|
|
{
|
|
|
|
if (form == NULL)
|
|
|
|
_formi_default_form.field_term = function;
|
|
|
|
else
|
|
|
|
form->field_term = function;
|
|
|
|
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the function hook defined for the field termination.
|
|
|
|
*/
|
|
|
|
Form_Hook
|
|
|
|
field_term(FORM *form)
|
|
|
|
{
|
|
|
|
if (form == NULL)
|
|
|
|
return _formi_default_form.field_term;
|
|
|
|
else
|
|
|
|
return form->field_term;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the page flag on the given field to indicate it is the start of a
|
|
|
|
* new page.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
set_new_page(FIELD *fptr, int page)
|
|
|
|
{
|
|
|
|
FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
|
|
|
|
|
|
|
|
if (field->parent != NULL) /* check if field is connected to a form */
|
|
|
|
return E_CONNECTED;
|
|
|
|
|
|
|
|
field->page_break = (page != FALSE);
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the page status for the given field. TRUE is returned if the
|
|
|
|
* field is the start of a new page.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
new_page(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
|
|
|
return _formi_default_field.page_break;
|
|
|
|
else
|
|
|
|
return field->page_break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the index of the field in the form fields array.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
field_index(FIELD *field)
|
|
|
|
{
|
|
|
|
if (field == NULL)
|
2001-07-08 16:15:06 +04:00
|
|
|
return E_BAD_ARGUMENT;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
if (field->parent == NULL)
|
2001-07-08 16:15:06 +04:00
|
|
|
return E_NOT_CONNECTED;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
return field->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal function that does most of the work to create a new field.
|
|
|
|
* The new field is initialised from the information in the prototype
|
|
|
|
* field passed.
|
|
|
|
* Returns NULL on error.
|
|
|
|
*/
|
|
|
|
static FIELD *
|
|
|
|
_formi_create_field(FIELD *prototype, int rows, int cols, int frow,
|
|
|
|
int fcol, int nrows, int nbuf)
|
|
|
|
{
|
|
|
|
FIELD *new;
|
|
|
|
|
|
|
|
if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) ||
|
|
|
|
(nrows < 0) || (nbuf < 0))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy in the default field info */
|
|
|
|
bcopy(prototype, new, sizeof(FIELD));
|
|
|
|
|
|
|
|
new->nbuf = nbuf + 1;
|
|
|
|
new->rows = rows;
|
|
|
|
new->cols = cols;
|
|
|
|
new->form_row = frow;
|
|
|
|
new->form_col = fcol;
|
|
|
|
new->nrows = nrows;
|
|
|
|
new->link = new;
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new field structure.
|
|
|
|
*/
|
|
|
|
FIELD *
|
|
|
|
new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf)
|
|
|
|
{
|
|
|
|
FIELD *new;
|
|
|
|
size_t buf_len;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
|
|
if ((new = _formi_create_field(&_formi_default_field, rows, cols,
|
|
|
|
frow, fcol, nrows, nbuf)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buf_len = (nbuf + 1) * sizeof(FORM_STR);
|
|
|
|
|
|
|
|
if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
|
|
|
|
free(new);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-01-30 09:33:51 +03:00
|
|
|
/* Initialise the strings to a zero length string */
|
2000-12-17 15:04:30 +03:00
|
|
|
for (i = 0; i < nbuf + 1; i++) {
|
2001-01-30 09:33:51 +03:00
|
|
|
if ((new->buffers[i].string =
|
2001-05-11 18:04:48 +04:00
|
|
|
(char *) malloc(sizeof(char))) == NULL) {
|
|
|
|
free(new->buffers);
|
|
|
|
free(new);
|
2001-01-30 09:33:51 +03:00
|
|
|
return NULL;
|
2001-05-11 18:04:48 +04:00
|
|
|
}
|
2001-01-30 09:33:51 +03:00
|
|
|
new->buffers[i].string[0] = '\0';
|
2000-12-17 15:04:30 +03:00
|
|
|
new->buffers[i].length = 0;
|
2001-01-30 09:33:51 +03:00
|
|
|
new->buffers[i].allocated = 1;
|
2000-12-17 15:04:30 +03:00
|
|
|
}
|
2001-05-11 18:04:48 +04:00
|
|
|
|
2010-02-03 18:34:37 +03:00
|
|
|
if ((new->alines = (_FORMI_FIELD_LINES *)
|
2001-05-11 18:04:48 +04:00
|
|
|
malloc(sizeof(struct _formi_field_lines))) == NULL) {
|
|
|
|
free(new->buffers);
|
|
|
|
free(new);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-02-03 18:34:37 +03:00
|
|
|
new->alines->prev = NULL;
|
|
|
|
new->alines->next = NULL;
|
|
|
|
new->alines->allocated = 0;
|
|
|
|
new->alines->length = 0;
|
|
|
|
new->alines->expanded = 0;
|
|
|
|
new->alines->string = NULL;
|
|
|
|
new->alines->hard_ret = FALSE;
|
|
|
|
new->alines->tabs = NULL;
|
|
|
|
new->start_line = new->alines;
|
|
|
|
new->cur_line = new->alines;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Duplicate the given field, including it's buffers.
|
|
|
|
*/
|
|
|
|
FIELD *
|
|
|
|
dup_field(FIELD *field, int frow, int fcol)
|
|
|
|
{
|
|
|
|
FIELD *new;
|
|
|
|
size_t row_len, buf_len;
|
|
|
|
|
|
|
|
if (field == NULL)
|
|
|
|
return NULL;
|
2001-07-08 16:15:06 +04:00
|
|
|
|
|
|
|
/* XXXX this right???? */
|
2000-12-17 15:04:30 +03:00
|
|
|
if ((new = _formi_create_field(field, (int) field->rows,
|
|
|
|
(int ) field->cols,
|
|
|
|
frow, fcol, (int) field->nrows,
|
|
|
|
field->nbuf - 1)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
row_len = (field->rows + field->nrows + 1) * field->cols;
|
|
|
|
buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR);
|
|
|
|
|
|
|
|
if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) {
|
|
|
|
free(new);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the buffers from the source field into the new copy */
|
|
|
|
bcopy(field->buffers, new->buffers, buf_len);
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new field at the specified location by duplicating the given
|
|
|
|
* field. The buffers are shared with the parent field.
|
|
|
|
*/
|
|
|
|
FIELD *
|
|
|
|
link_field(FIELD *field, int frow, int fcol)
|
|
|
|
{
|
|
|
|
FIELD *new;
|
|
|
|
|
|
|
|
if (field == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if ((new = _formi_create_field(field, (int) field->rows,
|
|
|
|
(int) field->cols,
|
|
|
|
frow, fcol, (int) field->nrows,
|
|
|
|
field->nbuf - 1)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new->link = field->link;
|
|
|
|
field->link = new;
|
|
|
|
|
|
|
|
/* we are done. The buffer pointer was copied during the field
|
|
|
|
creation. */
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release all storage allocated to the field
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
free_field(FIELD *field)
|
|
|
|
{
|
|
|
|
FIELD *flink;
|
2002-05-20 19:00:11 +04:00
|
|
|
int i;
|
|
|
|
_formi_tab_t *ts, *nts;
|
2000-12-17 15:04:30 +03:00
|
|
|
|
|
|
|
if (field == NULL)
|
|
|
|
return E_BAD_ARGUMENT;
|
|
|
|
|
|
|
|
if (field->parent != NULL)
|
|
|
|
return E_CONNECTED;
|
|
|
|
|
|
|
|
if (field->link == field) { /* check if field linked */
|
|
|
|
/* no it is not - release the buffers */
|
|
|
|
free(field->buffers);
|
2002-05-20 19:00:11 +04:00
|
|
|
/* free the tab structures */
|
|
|
|
for (i = 0; i < field->row_count - 1; i++) {
|
2010-02-03 18:34:37 +03:00
|
|
|
if (field->alines[i].tabs != NULL) {
|
|
|
|
ts = field->alines[i].tabs;
|
2002-05-20 19:00:11 +04:00
|
|
|
while (ts != NULL) {
|
|
|
|
nts = ts->fwd;
|
|
|
|
free(ts);
|
|
|
|
ts = nts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-12-17 15:04:30 +03:00
|
|
|
} else {
|
|
|
|
/* is linked, traverse the links to find the field referring
|
|
|
|
* to the one to be freed.
|
|
|
|
*/
|
|
|
|
for (flink = field->link; flink != field; flink = flink->link);
|
|
|
|
flink->link = field->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(field);
|
|
|
|
return E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|