field.c revision 1.12
1272343Sngie/* $NetBSD: field.c,v 1.12 2001/06/13 10:45:58 wiz Exp $ */ 2272343Sngie/*- 3272343Sngie * Copyright (c) 1998-1999 Brett Lymn 4272343Sngie * (blymn@baea.com.au, brett_lymn@yahoo.com.au) 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code has been donated to The NetBSD Foundation by the Author. 8272343Sngie * 9272343Sngie * Redistribution and use in source and binary forms, with or without 10272343Sngie * modification, are permitted provided that the following conditions 11272343Sngie * are met: 12272343Sngie * 1. Redistributions of source code must retain the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer. 14272343Sngie * 2. The name of the author may not be used to endorse or promote products 15272343Sngie * derived from this software without specific prior written permission 16272343Sngie * 17272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18272343Sngie * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19272343Sngie * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20272343Sngie * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22272343Sngie * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23272343Sngie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24272343Sngie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25272343Sngie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26272343Sngie * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27272343Sngie * 28272343Sngie * 29272343Sngie */ 30272343Sngie 31272343Sngie#include <stdlib.h> 32272343Sngie#include <strings.h> 33272343Sngie#include <form.h> 34272343Sngie#include "internals.h" 35272343Sngie 36272343Sngieextern FORM _formi_default_form; 37272343Sngie 38272343SngieFIELD _formi_default_field = { 39272343Sngie 0, /* rows in the field */ 40272343Sngie 0, /* columns in the field */ 41272343Sngie 0, /* dynamic rows */ 42272343Sngie 0, /* dynamic columns */ 43272343Sngie 0, /* maximum growth */ 44272343Sngie 0, /* starting row in the form subwindow */ 45272343Sngie 0, /* starting column in the form subwindow */ 46272343Sngie 0, /* number of off screen rows */ 47272343Sngie 0, /* index of this field in form fields array. */ 48272343Sngie 0, /* number of buffers associated with this field */ 49272343Sngie FALSE, /* set to true if buffer 0 has changed. */ 50272343Sngie NO_JUSTIFICATION, /* justification style of the field */ 51272343Sngie FALSE, /* set to true if field is in overlay mode */ 52272343Sngie 0, /* starting char in string (horiz scroll) */ 53272343Sngie 0, /* starting line in field (vert scroll) */ 54272343Sngie 0, /* number of rows actually used in field */ 55272343Sngie 0, /* x pos of cursor in field */ 56272343Sngie 0, /* y pos of cursor in field */ 57272343Sngie 0, /* start of a new page on the form if 1 */ 58272343Sngie 0, /* number of the page this field is on */ 59272343Sngie A_NORMAL, /* character attributes for the foreground */ 60272343Sngie A_NORMAL, /* character attributes for the background */ 61273572Sngie ' ', /* padding character */ 62273572Sngie DEFAULT_FORM_OPTS, /* options for the field */ 63273572Sngie NULL, /* the form this field is bound to, if any */ 64273572Sngie NULL, /* field above this one */ 65273572Sngie NULL, /* field below this one */ 66273572Sngie NULL, /* field to the left of this one */ 67272343Sngie NULL, /* field to the right of this one */ 68272343Sngie NULL, /* user defined pointer. */ 69273572Sngie NULL, /* used if fields are linked */ 70272343Sngie NULL, /* type struct for the field */ 71272343Sngie {NULL, NULL}, /* circle queue glue for sorting fields */ 72272343Sngie NULL, /* args for field type. */ 73272343Sngie 0, /* number of allocated slots in lines array */ 74272343Sngie NULL, /* pointer to the array of lines structures. */ 75272343Sngie NULL, /* array of buffers for the field */ 76272343Sngie}; 77272343Sngie 78272343Sngie/* internal function prototypes */ 79272343Sngiestatic FIELD * 80272343Sngie_formi_create_field(FIELD *, int, int, int, int, int, int); 81272343Sngie 82272343Sngie 83272343Sngie/* 84272343Sngie * Set the userptr for the field 85272343Sngie */ 86272343Sngieint 87272343Sngieset_field_userptr(FIELD *field, void *ptr) 88272343Sngie{ 89272343Sngie FIELD *fp = (field == NULL) ? &_formi_default_field : field; 90272343Sngie 91272343Sngie fp->userptr = ptr; 92272343Sngie 93272343Sngie return E_OK; 94272343Sngie} 95272343Sngie 96272343Sngie/* 97272343Sngie * Return the userptr for the field. 98272343Sngie */ 99272343Sngie 100272343Sngievoid * 101272343Sngiefield_userptr(FIELD *field) 102272343Sngie{ 103272343Sngie if (field == NULL) 104272343Sngie return _formi_default_field.userptr; 105272343Sngie else 106272343Sngie return field->userptr; 107272343Sngie} 108272343Sngie 109272343Sngie/* 110272343Sngie * Set the options for the designated field. 111272343Sngie */ 112272343Sngieint 113272343Sngieset_field_opts(FIELD *field, Form_Options options) 114272343Sngie{ 115272343Sngie int i; 116272343Sngie 117272343Sngie FIELD *fp = (field == NULL) ? &_formi_default_field : field; 118272343Sngie 119272343Sngie /* not allowed to set opts if the field is the current one */ 120272343Sngie if ((field != NULL) && (field->parent != NULL) && 121272343Sngie (field->parent->cur_field == field->index)) 122272343Sngie return E_CURRENT; 123272343Sngie 124272343Sngie if ((options & O_STATIC) == O_STATIC) { 125272343Sngie for (i = 0; i < field->nbuf; i++) { 126272343Sngie if (field->buffers[i].length > field->cols) 127272343Sngie field->buffers[i].string[field->cols] = '\0'; 128272343Sngie } 129272343Sngie } 130272343Sngie 131272343Sngie fp->opts = options; 132272343Sngie 133272343Sngie return E_OK; 134272343Sngie} 135272343Sngie 136272343Sngie/* 137272343Sngie * Turn on the passed field options. 138272343Sngie */ 139272343Sngieint 140272343Sngiefield_opts_on(FIELD *field, Form_Options options) 141272343Sngie{ 142272343Sngie int i; 143272343Sngie 144272343Sngie FIELD *fp = (field == NULL) ? &_formi_default_field : field; 145272343Sngie 146272343Sngie /* not allowed to set opts if the field is the current one */ 147272343Sngie if ((field != NULL) && (field->parent != NULL) && 148272343Sngie (field->parent->cur_field == field->index)) 149272343Sngie return E_CURRENT; 150272343Sngie 151272343Sngie if ((options & O_STATIC) == O_STATIC) { 152272343Sngie for (i = 0; i < field->nbuf; i++) { 153272343Sngie if (field->buffers[i].length > field->cols) 154272343Sngie field->buffers[i].string[field->cols] = '\0'; 155272343Sngie } 156272343Sngie } 157272343Sngie 158272343Sngie fp->opts |= options; 159272343Sngie 160272343Sngie return E_OK; 161272343Sngie} 162272343Sngie 163272343Sngie/* 164272343Sngie * Turn off the passed field options. 165272343Sngie */ 166272343Sngieint 167272343Sngiefield_opts_off(FIELD *field, Form_Options options) 168272343Sngie{ 169272343Sngie FIELD *fp = (field == NULL) ? &_formi_default_field : field; 170272343Sngie 171272343Sngie /* not allowed to set opts if the field is the current one */ 172272343Sngie if ((field != NULL) && (field->parent != NULL) && 173272343Sngie (field->parent->cur_field == field->index)) 174272343Sngie return E_CURRENT; 175272343Sngie 176273572Sngie fp->opts &= ~options; 177273572Sngie return E_OK; 178273572Sngie} 179272343Sngie 180272343Sngie/* 181272343Sngie * Return the field options associated with the passed field. 182272343Sngie */ 183272343SngieForm_Options 184272343Sngiefield_opts(FIELD *field) 185272343Sngie{ 186272343Sngie if (field == NULL) 187272343Sngie return _formi_default_field.opts; 188272343Sngie else 189272343Sngie return field->opts; 190272343Sngie} 191272343Sngie 192272343Sngie/* 193272343Sngie * Set the justification for the passed field. 194272343Sngie */ 195272343Sngieint 196272343Sngieset_field_just(FIELD *field, int justification) 197272343Sngie{ 198272343Sngie FIELD *fp = (field == NULL) ? &_formi_default_field : field; 199272343Sngie 200272343Sngie /* 201272343Sngie * not allowed to set justification if the field is 202272343Sngie * 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 */ 219int 220field_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 */ 231int 232field_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 */ 251int 252dynamic_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 274int 275set_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 */ 350char * 351field_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 */ 366int 367set_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 */ 384int 385field_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 */ 397int 398set_max_field(FIELD *fptr, int max) 399{ 400 FIELD *field = (field == 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 */ 415int 416set_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 */ 427chtype 428field_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 */ 439int 440set_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 * Set the pad character for the given field. 452 */ 453int 454set_field_pad(FIELD *field, int pad) 455{ 456 if (field == NULL) 457 _formi_default_field.pad = pad; 458 else 459 field->pad = pad; 460 461 return E_OK; 462} 463 464/* 465 * Return the padding character for the given field. 466 */ 467int 468field_pad(FIELD *field) 469{ 470 if (field == NULL) 471 return _formi_default_field.pad; 472 else 473 return field->pad; 474} 475 476/* 477 * Set the field initialisation function hook. 478 */ 479int 480set_field_init(FORM *form, Form_Hook function) 481{ 482 if (form == NULL) 483 _formi_default_form.field_init = function; 484 else 485 form->field_init = function; 486 487 return E_OK; 488} 489 490/* 491 * Return the function hook for the field initialisation. 492 */ 493Form_Hook 494field_init(FORM *form) 495{ 496 if (form == NULL) 497 return _formi_default_form.field_init; 498 else 499 return form->field_init; 500} 501 502/* 503 * Set the field termination function hook. 504 */ 505int 506set_field_term(FORM *form, Form_Hook function) 507{ 508 if (form == NULL) 509 _formi_default_form.field_term = function; 510 else 511 form->field_term = function; 512 513 return E_OK; 514} 515 516/* 517 * Return the function hook defined for the field termination. 518 */ 519Form_Hook 520field_term(FORM *form) 521{ 522 if (form == NULL) 523 return _formi_default_form.field_term; 524 else 525 return form->field_term; 526} 527 528/* 529 * Set the page flag on the given field to indicate it is the start of a 530 * new page. 531 */ 532int 533set_new_page(FIELD *fptr, int page) 534{ 535 FIELD *field = (fptr == NULL)? &_formi_default_field : fptr; 536 537 if (field->parent != NULL) /* check if field is connected to a form */ 538 return E_CONNECTED; 539 540 field->page_break = (page != FALSE); 541 return E_OK; 542} 543 544/* 545 * Return the page status for the given field. TRUE is returned if the 546 * field is the start of a new page. 547 */ 548int 549new_page(FIELD *field) 550{ 551 if (field == NULL) 552 return _formi_default_field.page_break; 553 else 554 return field->page_break; 555} 556 557/* 558 * Return the index of the field in the form fields array. 559 */ 560int 561field_index(FIELD *field) 562{ 563 if (field == NULL) 564 return -1; 565 566 if (field->parent == NULL) 567 return -1; 568 569 return field->index; 570} 571 572/* 573 * Internal function that does most of the work to create a new field. 574 * The new field is initialised from the information in the prototype 575 * field passed. 576 * Returns NULL on error. 577 */ 578static FIELD * 579_formi_create_field(FIELD *prototype, int rows, int cols, int frow, 580 int fcol, int nrows, int nbuf) 581{ 582 FIELD *new; 583 584 if ((rows <= 0) || (cols <= 0) || (frow < 0) || (fcol < 0) || 585 (nrows < 0) || (nbuf < 0)) 586 return NULL; 587 588 if ((new = (FIELD *)malloc(sizeof(FIELD))) == NULL) { 589 return NULL; 590 } 591 592 /* copy in the default field info */ 593 bcopy(prototype, new, sizeof(FIELD)); 594 595 new->nbuf = nbuf + 1; 596 new->rows = rows; 597 new->cols = cols; 598 new->form_row = frow; 599 new->form_col = fcol; 600 new->nrows = nrows; 601 new->link = new; 602 return new; 603} 604 605/* 606 * Create a new field structure. 607 */ 608FIELD * 609new_field(int rows, int cols, int frow, int fcol, int nrows, int nbuf) 610{ 611 FIELD *new; 612 size_t buf_len; 613 int i; 614 615 616 if ((new = _formi_create_field(&_formi_default_field, rows, cols, 617 frow, fcol, nrows, nbuf)) == NULL) 618 return NULL; 619 620 buf_len = (nbuf + 1) * sizeof(FORM_STR); 621 622 if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) { 623 free(new); 624 return NULL; 625 } 626 627 /* Initialise the strings to a zero length string */ 628 for (i = 0; i < nbuf + 1; i++) { 629 if ((new->buffers[i].string = 630 (char *) malloc(sizeof(char))) == NULL) { 631 free(new->buffers); 632 free(new); 633 return NULL; 634 } 635 new->buffers[i].string[0] = '\0'; 636 new->buffers[i].length = 0; 637 new->buffers[i].allocated = 1; 638 } 639 640 if ((new->lines = (_FORMI_FIELD_LINES *) 641 malloc(sizeof(struct _formi_field_lines))) == NULL) { 642 free(new->buffers); 643 free(new); 644 return NULL; 645 } 646 647 new->lines_alloced = 1; 648 new->lines[0].length = 0; 649 new->lines[0].start = 0; 650 new->lines[0].end = 0; 651 652 return new; 653} 654 655/* 656 * Duplicate the given field, including it's buffers. 657 */ 658FIELD * 659dup_field(FIELD *field, int frow, int fcol) 660{ 661 FIELD *new; 662 size_t row_len, buf_len; 663 664 if (field == NULL) 665 return NULL; 666 667 if ((new = _formi_create_field(field, (int) field->rows, 668 (int ) field->cols, 669 frow, fcol, (int) field->nrows, 670 field->nbuf - 1)) == NULL) 671 return NULL; 672 673 row_len = (field->rows + field->nrows + 1) * field->cols; 674 buf_len = (field->nbuf + 1) * row_len * sizeof(FORM_STR); 675 676 if ((new->buffers = (FORM_STR *)malloc(buf_len)) == NULL) { 677 free(new); 678 return NULL; 679 } 680 681 /* copy the buffers from the source field into the new copy */ 682 bcopy(field->buffers, new->buffers, buf_len); 683 684 return new; 685} 686 687/* 688 * Create a new field at the specified location by duplicating the given 689 * field. The buffers are shared with the parent field. 690 */ 691FIELD * 692link_field(FIELD *field, int frow, int fcol) 693{ 694 FIELD *new; 695 696 if (field == NULL) 697 return NULL; 698 699 if ((new = _formi_create_field(field, (int) field->rows, 700 (int) field->cols, 701 frow, fcol, (int) field->nrows, 702 field->nbuf - 1)) == NULL) 703 return NULL; 704 705 new->link = field->link; 706 field->link = new; 707 708 /* we are done. The buffer pointer was copied during the field 709 creation. */ 710 return new; 711} 712 713/* 714 * Release all storage allocated to the field 715 */ 716int 717free_field(FIELD *field) 718{ 719 FIELD *flink; 720 721 if (field == NULL) 722 return E_BAD_ARGUMENT; 723 724 if (field->parent != NULL) 725 return E_CONNECTED; 726 727 if (field->link == field) { /* check if field linked */ 728 /* no it is not - release the buffers */ 729 free(field->buffers); 730 } else { 731 /* is linked, traverse the links to find the field referring 732 * to the one to be freed. 733 */ 734 for (flink = field->link; flink != field; flink = flink->link); 735 flink->link = field->link; 736 } 737 738 free(field); 739 return E_OK; 740} 741 742 743