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