Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libform Fix limits on dynamic fields.



details:   https://anonhg.NetBSD.org/src/rev/dded137efde9
branches:  trunk
changeset: 511861:dded137efde9
user:      blymn <blymn%NetBSD.org@localhost>
date:      Thu Jun 28 11:38:19 2001 +0000

description:
Fix limits on dynamic fields.

diffstat:

 lib/libform/field.c     |   4 +-
 lib/libform/internals.c |  93 ++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 75 insertions(+), 22 deletions(-)

diffs (189 lines):

diff -r a617b0e99de0 -r dded137efde9 lib/libform/field.c
--- a/lib/libform/field.c       Thu Jun 28 10:40:04 2001 +0000
+++ b/lib/libform/field.c       Thu Jun 28 11:38:19 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: field.c,v 1.12 2001/06/13 10:45:58 wiz Exp $   */
+/*     $NetBSD: field.c,v 1.13 2001/06/28 11:38:19 blymn Exp $ */
 /*-
  * Copyright (c) 1998-1999 Brett Lymn
  *                         (blymn%baea.com.au@localhost, brett_lymn%yahoo.com.au@localhost)
@@ -397,7 +397,7 @@
 int
 set_max_field(FIELD *fptr, int max)
 {
-       FIELD *field = (field == NULL)? &_formi_default_field : fptr;
+       FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
 
        if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
                return E_BAD_ARGUMENT;
diff -r a617b0e99de0 -r dded137efde9 lib/libform/internals.c
--- a/lib/libform/internals.c   Thu Jun 28 10:40:04 2001 +0000
+++ b/lib/libform/internals.c   Thu Jun 28 11:38:19 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: internals.c,v 1.20 2001/06/23 13:34:01 blymn Exp $     */
+/*     $NetBSD: internals.c,v 1.21 2001/06/28 11:38:19 blymn Exp $     */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn
@@ -83,6 +83,8 @@
 split_line(FIELD *field, unsigned pos);
 static void
 bump_lines(FIELD *field, int pos, int amt, bool do_len);
+static bool
+check_dynamic_size(FIELD *field);
 
 
 /*
@@ -166,6 +168,34 @@
 }
 
 /*
+ * Check the sizing of the dynamic field, if the maximum size is set 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.
+ *
+ */
+bool
+check_dynamic_size(FIELD *field)
+{
+       if (field->max == 0) /* unlimited */
+               return TRUE;
+
+       if (field->rows == 1) {
+               if (field->buffers[0].length >= field->max)
+                       return FALSE;
+               else
+                       return TRUE;
+       } else {
+               if (field->row_count > field->max)
+                       return FALSE;
+               else
+                       return TRUE;
+       }
+}
+
+/*
  * Set the form's current field to the first valid field on the page.
  * Assume the fields have been sorted and stitched.
  */
@@ -1042,7 +1072,7 @@
 int
 _formi_add_char(FIELD *field, unsigned int pos, char c)
 {
-       char *new;
+       char *new, old_c;
        unsigned int new_size;
        int status;
 
@@ -1094,9 +1124,7 @@
                if ((((field->opts & O_STATIC) == O_STATIC) &&
                     (field->buffers[0].length >= (field->cols * field->rows))) ||
                    (((field->opts & O_STATIC) != O_STATIC) &&
-/*XXXXX this is wrong - should check max row or col */
-                    ((field->max > 0) &&
-                     (field->buffers[0].length >= field->max))))
+                    (check_dynamic_size(field) == FALSE)))
                        return E_REQUEST_DENIED;
                
                if (field->buffers[0].length + 1
@@ -1116,7 +1144,8 @@
                      &field->buffers[0].string[pos + 1],
                      field->buffers[0].length - pos + 1);
        }
-       
+
+       old_c = field->buffers[0].string[pos];
        field->buffers[0].string[pos] = c;
        if (pos >= field->buffers[0].length) {
                  /* make sure the string is terminated if we are at the
@@ -1138,13 +1167,31 @@
 
          /* wrap the field, if needed */
        status = _formi_wrap_field(field, pos);
-       if (status != E_OK) {
-                 /* wrap failed for some reason, back out the char insert */
-               bcopy(&field->buffers[0].string[pos + 1],
-                     &field->buffers[0].string[pos],
-                     field->buffers[0].length - pos);
-               field->buffers[0].length--;
-               bump_lines(field, (int) pos, -1, TRUE);
+       
+         /*
+          * check the wrap worked or that we have not exceeded the
+          * max field size - this can happen if the field is re-wrapped
+          * and the row count is increased past the set limit.
+          */
+       if ((status != E_OK) || (check_dynamic_size(field) == FALSE)) {
+               if ((field->overlay == 0)
+                   || ((field->overlay == 1)
+                       && (pos >= field->buffers[0].length))) {
+                         /*
+                          * wrap failed for some reason, back out the
+                          * char insert
+                          */
+                       bcopy(&field->buffers[0].string[pos + 1],
+                             &field->buffers[0].string[pos],
+                             field->buffers[0].length - pos);
+                       field->buffers[0].length--;
+                       bump_lines(field, (int) pos, -1, TRUE);
+               } else if (field->overlay == 1) {
+                         /* back out character overlay */
+                       field->buffers[0].string[pos] = old_c;
+               }
+               
+               _formi_wrap_field(field, ((pos > 0)? pos - 1 : 0));
        } else {
                field->buf0_status = TRUE;
                if ((field->rows + field->nrows) == 1) {
@@ -1164,11 +1211,14 @@
                                        - field->cursor_ypos - 1;
                        } else
                                field->cursor_ypos = new_size;
+
+                       if ((field->lines[new_size].start) <= (pos + 1)) {
+                               field->cursor_xpos = pos
+                                       - field->lines[new_size].start + 1;
+                       } else {
+                               field->cursor_xpos = 0;
+                       }
                        
-                       field->cursor_xpos = pos
-                               - field->lines[field->cursor_ypos
-                                             + field->start_line].start + 1;
-
                          /*
                           * Annoying corner case - if we are right in
                           * the bottom right corner of the field we
@@ -1185,7 +1235,7 @@
        }
        
 #ifdef DEBUG
-       assert((field->cursor_xpos < 400000)
+       assert((field->cursor_xpos <= field->cols)
               && (field->cursor_ypos < 400000)
               && (field->start_line < 400000));
               
@@ -1413,6 +1463,7 @@
                
        case REQ_BEG_LINE:
                cur->cursor_xpos = 0;
+               cur->start_char = 0;
                break;
                        
        case REQ_END_FIELD:
@@ -1559,8 +1610,10 @@
                break;
                
        case REQ_INS_CHAR:
-               _formi_add_char(cur, cur->start_char + cur->cursor_xpos,
-                               cur->pad);
+               if ((status = _formi_add_char(cur, cur->start_char
+                                             + cur->cursor_xpos,
+                                             cur->pad)) != E_OK)
+                       return status;
                break;
                
        case REQ_INS_LINE:



Home | Main Index | Thread Index | Old Index