xref: /freebsd/contrib/ncurses/form/fty_ipv4.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2009 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /***************************************************************************
30 *                                                                          *
31 *  Author : Per Foreby, perf@efd.lth.se                                    *
32 *                                                                          *
33 ***************************************************************************/
34 
35 #include "form.priv.h"
36 
37 MODULE_ID("$Id: fty_ipv4.c,v 1.10 2009/11/07 20:17:58 tom Exp $")
38 
39 /*---------------------------------------------------------------------------
40 |   Facility      :  libnform
41 |   Function      :  static bool Check_IPV4_Field(
42 |                                      FIELD * field,
43 |                                      const void * argp)
44 |
45 |   Description   :  Validate buffer content to be a valid IP number (Ver. 4)
46 |
47 |   Return Values :  TRUE  - field is valid
48 |                    FALSE - field is invalid
49 +--------------------------------------------------------------------------*/
50 static bool
51 Check_IPV4_Field(FIELD *field, const void *argp GCC_UNUSED)
52 {
53   char *bp = field_buffer(field, 0);
54   int num = 0, len;
55   unsigned int d1, d2, d3, d4;
56 
57   if (isdigit(UChar(*bp)))	/* Must start with digit */
58     {
59       num = sscanf(bp, "%u.%u.%u.%u%n", &d1, &d2, &d3, &d4, &len);
60       if (num == 4)
61 	{
62 	  bp += len;		/* Make bp point to what sscanf() left */
63 	  while (isspace(UChar(*bp)))
64 	    bp++;		/* Allow trailing whitespace */
65 	}
66     }
67   return ((num != 4 || *bp || d1 > 255 || d2 > 255
68 	   || d3 > 255 || d4 > 255) ? FALSE : TRUE);
69 }
70 
71 /*---------------------------------------------------------------------------
72 |   Facility      :  libnform
73 |   Function      :  static bool Check_IPV4_Character(
74 |                                      int c,
75 |                                      const void *argp )
76 |
77 |   Description   :  Check a character for unsigned type or period.
78 |
79 |   Return Values :  TRUE  - character is valid
80 |                    FALSE - character is invalid
81 +--------------------------------------------------------------------------*/
82 static bool
83 Check_IPV4_Character(int c, const void *argp GCC_UNUSED)
84 {
85   return ((isdigit(UChar(c)) || (c == '.')) ? TRUE : FALSE);
86 }
87 
88 static FIELDTYPE typeIPV4 =
89 {
90   _RESIDENT,
91   1,				/* this is mutable, so we can't be const */
92   (FIELDTYPE *)0,
93   (FIELDTYPE *)0,
94   NULL,
95   NULL,
96   NULL,
97   INIT_FT_FUNC(Check_IPV4_Field),
98   INIT_FT_FUNC(Check_IPV4_Character),
99   INIT_FT_FUNC(NULL),
100   INIT_FT_FUNC(NULL),
101 #if NCURSES_INTEROP_FUNCS
102   NULL
103 #endif
104 };
105 
106 NCURSES_EXPORT_VAR(FIELDTYPE*) TYPE_IPV4 = &typeIPV4;
107 
108 #if NCURSES_INTEROP_FUNCS
109 /* The next routines are to simplify the use of ncurses from
110    programming languages with restictions on interop with C level
111    constructs (e.g. variable access or va_list + ellipsis constructs)
112 */
113 NCURSES_EXPORT(FIELDTYPE *)
114 _nc_TYPE_IPV4(void)
115 {
116   return TYPE_IPV4;
117 }
118 #endif
119 
120 /* fty_ipv4.c ends here */
121