Source-Changes-HG archive

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

[src/trunk]: src/lib/libform Added field and character checking.



details:   https://anonhg.NetBSD.org/src/rev/e485a5284e39
branches:  trunk
changeset: 503055:e485a5284e39
user:      blymn <blymn%NetBSD.org@localhost>
date:      Tue Jan 30 06:44:42 2001 +0000

description:
Added field and character checking.

diffstat:

 lib/libform/driver.c    |  72 ++++++++++++++++++++++++++++++------------------
 lib/libform/form.h      |   8 ++++-
 lib/libform/internals.c |  68 +++++++++++++++++++++++++++++++++++++++++----
 lib/libform/internals.h |   6 ++-
 lib/libform/type_enum.c |   8 ++--
 5 files changed, 120 insertions(+), 42 deletions(-)

diffs (truncated from 386 to 300 lines):

diff -r c7e1d4d7df64 -r e485a5284e39 lib/libform/driver.c
--- a/lib/libform/driver.c      Tue Jan 30 06:33:51 2001 +0000
+++ b/lib/libform/driver.c      Tue Jan 30 06:44:42 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: driver.c,v 1.3 2001/01/23 01:59:29 blymn Exp $ */
+/*     $NetBSD: driver.c,v 1.4 2001/01/30 06:44:42 blymn Exp $ */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn
@@ -112,12 +112,13 @@
 
 
        old_field = form->cur_field;
+       fieldp = form->fields[form->cur_field];
        update_page = update_field = 0;
+       status = E_OK;
        
        if (c < REQ_MIN_REQUEST) {
                if (isprint(c)) {
                  next_field:
-                       fieldp = form->fields[form->cur_field];
                        buf = fieldp->buffers[0];
                        
                        pos = fieldp->start_char + fieldp->cursor_xpos
@@ -160,6 +161,23 @@
                if (c > REQ_MAX_COMMAND)
                        return E_UNKNOWN_COMMAND;
 
+               if ((c >= REQ_NEXT_PAGE) && (c <= REQ_DOWN_FIELD)) {
+                         /* first check the field we are in is ok */
+                       if (_formi_validate_field(form) != E_OK)
+                               return E_INVALID_FIELD;
+
+                       if (form->field_term != NULL)
+                               form->field_term(form);
+
+                         /*
+                          * if we have a page movement then the form term
+                          * needs to be called too
+                          */ 
+                       if ((c <= REQ_LAST_PAGE) && (form->form_term != NULL))
+                               form->form_term(form);
+               }
+               
+                       
                switch (c) {
                case REQ_NEXT_PAGE:
                        if (form->page < form->max_page) {
@@ -168,10 +186,10 @@
                                update_page = 1;
                                if (_formi_pos_first_field(form) != E_OK) {
                                        form->page = old_page;
-                                       return E_REQUEST_DENIED;
+                                       status = E_REQUEST_DENIED;
                                }
                        } else
-                               return E_REQUEST_DENIED;
+                               status = E_REQUEST_DENIED;
                        break;
                
                case REQ_PREV_PAGE:
@@ -181,10 +199,10 @@
                                update_page = 1;
                                if (_formi_pos_first_field(form) != E_OK) {
                                        form->page = old_page;
-                                       return E_REQUEST_DENIED;
+                                       status = E_REQUEST_DENIED;
                                }
                        } else
-                               return E_REQUEST_DENIED;
+                               status = E_REQUEST_DENIED;
                        break;
                
                case REQ_FIRST_PAGE:
@@ -193,7 +211,7 @@
                        update_page = 1;
                        if (_formi_pos_first_field(form) != E_OK) {
                                form->page = old_page;
-                               return E_REQUEST_DENIED;
+                               status = E_REQUEST_DENIED;
                        }
                        break;
                
@@ -203,27 +221,19 @@
                        update_page = 1;
                        if (_formi_pos_first_field(form) != E_OK) {
                                form->page = old_page;
-                               return E_REQUEST_DENIED;
+                               status = E_REQUEST_DENIED;
                        }
                        break;
                
                case REQ_NEXT_FIELD:
                        status = _formi_pos_new_field(form, _FORMI_FORWARD,
                                                      FALSE);
-                       if (status != E_OK) {
-                               return status;
-                       }
-                       
                        update_field = 1;
                        break;
                
                case REQ_PREV_FIELD:
                        status = _formi_pos_new_field(form, _FORMI_BACKWARD,
                                                      FALSE);
-                       
-                       if (status != E_OK)
-                               return status;
-
                        update_field = 1;
                        break;
                
@@ -240,18 +250,12 @@
                case REQ_SNEXT_FIELD:
                        status = _formi_pos_new_field(form, _FORMI_FORWARD,
                                                      TRUE);
-                       if (status != E_OK)
-                               return status;
-
                        update_field = 1;
                        break;
                
                case REQ_SPREV_FIELD:
                        status = _formi_pos_new_field(form, _FORMI_BACKWARD,
                                                      TRUE);
-                       if (status != E_OK)
-                               return status;
-
                        update_field = 1;
                        break;
                
@@ -278,9 +282,6 @@
                case REQ_UP_FIELD:
                case REQ_DOWN_FIELD:
                        status = traverse_form_links(form, c);
-                       if (status != E_OK)
-                               return status;
-                       
                        update_field = 1;
                        break;
 
@@ -295,7 +296,6 @@
                           * into a previous field request. Otherwise
                           * fallthrough to the field handler.
                           */
-                       fieldp = form->fields[old_field];
                        if ((form->opts & O_BS_OVERLOAD) == O_BS_OVERLOAD) {
                                if ((fieldp->start_char == 0) &&
                                    (fieldp->start_line == 0) &&
@@ -317,7 +317,6 @@
                           * into a next field request. Otherwise
                           * fallthrough to the field handler.
                           */
-                       fieldp = form->fields[old_field];
                        if ((form->opts & O_NL_OVERLOAD) == O_NL_OVERLOAD) {
                                if ((fieldp->start_char == 0) &&
                                    (fieldp->start_line == 0) &&
@@ -395,6 +394,25 @@
                        break;
                }
        }
+
+         /* call the field and form init functions if required. */
+       if ((c >= REQ_NEXT_PAGE) && (c <= REQ_DOWN_FIELD)) {
+               if (form->field_init != NULL)
+                       form->field_init(form);
+
+                 /*
+                  * if we have a page movement then the form init
+                  * needs to be called too
+                  */ 
+               if ((c <= REQ_LAST_PAGE) && (form->form_init != NULL))
+                       form->form_init(form);
+
+                 /*
+                  * if there was an error just return now...
+                  */
+               if (status != E_OK)
+                       return status;
+       }
        
        if (update_field < 0)
                return update_field;
diff -r c7e1d4d7df64 -r e485a5284e39 lib/libform/form.h
--- a/lib/libform/form.h        Tue Jan 30 06:33:51 2001 +0000
+++ b/lib/libform/form.h        Tue Jan 30 06:44:42 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: form.h,v 1.6 2001/01/23 01:59:29 blymn Exp $   */
+/*     $NetBSD: form.h,v 1.7 2001/01/30 06:44:42 blymn Exp $   */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn
@@ -65,7 +65,11 @@
 #define O_STATIC   0x100  /* Field is not dynamic */
 #define O_PASSOK   0x200  /* ??? */
 
-/* Form driver requests */
+/*
+ * Form driver requests - be VERY careful about changing the ordering
+ * of the requests below.  The form driver code depends on a particular
+ * order for the requests.
+ */
 #define REQ_MIN_REQUEST   0x100 /* must equal value of the first request */
 
 #define REQ_NEXT_PAGE     0x100 /* next page in form */
diff -r c7e1d4d7df64 -r e485a5284e39 lib/libform/internals.c
--- a/lib/libform/internals.c   Tue Jan 30 06:33:51 2001 +0000
+++ b/lib/libform/internals.c   Tue Jan 30 06:44:42 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: internals.c,v 1.7 2001/01/23 02:01:56 blymn Exp $      */
+/*     $NetBSD: internals.c,v 1.8 2001/01/30 06:44:42 blymn Exp $      */
 
 /*-
  * Copyright (c) 1998-1999 Brett Lymn
@@ -57,6 +57,8 @@
 #define JOIN_PREV_NW 4 /* previous join, don't wrap the joined line */
 
 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, char *str, unsigned int pos, int direction);
@@ -382,7 +384,7 @@
  * the first non-blank character.
  */
 unsigned
-skip_blanks(char *string, unsigned int start)
+_formi_skip_blanks(char *string, unsigned int start)
 {
        unsigned int i;
 
@@ -843,6 +845,13 @@
        if (field->buffers[0].string == NULL) {
                set_field_buffer(field, 0, "");
        }
+
+       if (_formi_validate_char(field, c) != E_OK) {
+#ifdef DEBUG
+               fprintf(dbg, "add_char: char %c failed char validation\n", c);
+#endif
+               return E_INVALID_FIELD;
+       }
        
 #ifdef DEBUG
        fprintf(dbg, "add_char: pos=%d, char=%c\n", pos, c);
@@ -1411,6 +1420,47 @@
 }
 
 /*
+ * Validate the give character by passing it to any type character
+ * checking routines, if they exist.
+ */
+int
+_formi_validate_char(FIELD *field, char c)
+{
+       int ret_val;
+       
+       if (field->type == NULL)
+               return E_OK;
+
+       ret_val = E_INVALID_FIELD;
+       _formi_do_char_validation(field, field->type, c, &ret_val);
+
+       return ret_val;
+}
+
+               
+/*
+ * Perform the validation of the character, invoke all field_type validation
+ * routines.  If the field is ok then update ret_val to E_OK otherwise
+ * ret_val is not changed.
+ */
+static void
+_formi_do_char_validation(FIELD *field, FIELDTYPE *type, char c, int *ret_val)
+{
+       if ((type->flags & _TYPE_IS_LINKED) == _TYPE_IS_LINKED) {
+               _formi_do_char_validation(field, type->link->next, c, ret_val);
+               _formi_do_char_validation(field, type->link->prev, c, ret_val);
+       } else {
+               if (type->char_check == NULL)
+                       *ret_val = E_OK;
+               else {
+                       if (type->char_check((int)(unsigned char) c,
+                                            field->args) == TRUE)
+                               *ret_val = E_OK;
+               }
+       }
+}
+
+/*
  * Validate the current field.  If the field validation returns success then
  * return E_OK otherwise return E_INVALID_FIELD.
  *
@@ -1419,7 +1469,8 @@
 _formi_validate_field(FORM *form)
 {
        FIELD *cur;
-       int ret_val;
+       char *bp;



Home | Main Index | Thread Index | Old Index