]> ncurses.scripts.mit.edu Git - ncurses.git/blob - form/frm_req_name.c
ncurses 6.1 - patch 20190112
[ncurses.git] / form / frm_req_name.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2015 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  *   Author:  Juergen Pfeifer, 1995,1997                                    *
31  ****************************************************************************/
32
33 /***************************************************************************
34 * Module form_request_name                                                 *
35 * Routines to handle external names of menu requests                       *
36 ***************************************************************************/
37
38 #include "form.priv.h"
39
40 MODULE_ID("$Id: frm_req_name.c,v 1.19 2015/04/04 17:11:46 tom Exp $")
41
42 #define DATA(s) { s }
43
44 static const char request_names[MAX_FORM_COMMAND - MIN_FORM_COMMAND + 1][13] =
45 {
46   DATA("NEXT_PAGE"),
47   DATA("PREV_PAGE"),
48   DATA("FIRST_PAGE"),
49   DATA("LAST_PAGE"),
50
51   DATA("NEXT_FIELD"),
52   DATA("PREV_FIELD"),
53   DATA("FIRST_FIELD"),
54   DATA("LAST_FIELD"),
55   DATA("SNEXT_FIELD"),
56   DATA("SPREV_FIELD"),
57   DATA("SFIRST_FIELD"),
58   DATA("SLAST_FIELD"),
59   DATA("LEFT_FIELD"),
60   DATA("RIGHT_FIELD"),
61   DATA("UP_FIELD"),
62   DATA("DOWN_FIELD"),
63
64   DATA("NEXT_CHAR"),
65   DATA("PREV_CHAR"),
66   DATA("NEXT_LINE"),
67   DATA("PREV_LINE"),
68   DATA("NEXT_WORD"),
69   DATA("PREV_WORD"),
70   DATA("BEG_FIELD"),
71   DATA("END_FIELD"),
72   DATA("BEG_LINE"),
73   DATA("END_LINE"),
74   DATA("LEFT_CHAR"),
75   DATA("RIGHT_CHAR"),
76   DATA("UP_CHAR"),
77   DATA("DOWN_CHAR"),
78
79   DATA("NEW_LINE"),
80   DATA("INS_CHAR"),
81   DATA("INS_LINE"),
82   DATA("DEL_CHAR"),
83   DATA("DEL_PREV"),
84   DATA("DEL_LINE"),
85   DATA("DEL_WORD"),
86   DATA("CLR_EOL"),
87   DATA("CLR_EOF"),
88   DATA("CLR_FIELD"),
89   DATA("OVL_MODE"),
90   DATA("INS_MODE"),
91   DATA("SCR_FLINE"),
92   DATA("SCR_BLINE"),
93   DATA("SCR_FPAGE"),
94   DATA("SCR_BPAGE"),
95   DATA("SCR_FHPAGE"),
96   DATA("SCR_BHPAGE"),
97   DATA("SCR_FCHAR"),
98   DATA("SCR_BCHAR"),
99   DATA("SCR_HFLINE"),
100   DATA("SCR_HBLINE"),
101   DATA("SCR_HFHALF"),
102   DATA("SCR_HBHALF"),
103
104   DATA("VALIDATION"),
105   DATA("NEXT_CHOICE"),
106   DATA("PREV_CHOICE")
107 };
108
109 #undef DATA
110
111 #define A_SIZE (sizeof(request_names)/sizeof(request_names[0]))
112
113 /*---------------------------------------------------------------------------
114 |   Facility      :  libnform  
115 |   Function      :  const char * form_request_name (int request);
116 |   
117 |   Description   :  Get the external name of a form request.
118 |
119 |   Return Values :  Pointer to name      - on success
120 |                    NULL                 - on invalid request code
121 +--------------------------------------------------------------------------*/
122 NCURSES_EXPORT(const char *)
123 form_request_name(int request)
124 {
125   T((T_CALLED("form_request_name(%d)"), request));
126
127   if ((request < MIN_FORM_COMMAND) || (request > MAX_FORM_COMMAND))
128     {
129       SET_ERROR(E_BAD_ARGUMENT);
130       returnCPtr((const char *)0);
131     }
132   else
133     returnCPtr(request_names[request - MIN_FORM_COMMAND]);
134 }
135
136 /*---------------------------------------------------------------------------
137 |   Facility      :  libnform  
138 |   Function      :  int form_request_by_name (const char *str);
139 |   
140 |   Description   :  Search for a request with this name.
141 |
142 |   Return Values :  Request Id       - on success
143 |                    E_NO_MATCH       - request not found
144 +--------------------------------------------------------------------------*/
145 NCURSES_EXPORT(int)
146 form_request_by_name(const char *str)
147 {
148   /* because the table is so small, it doesn't really hurt
149      to run sequentially through it.
150    */
151   size_t i = 0;
152   char buf[16];                 /* longest name is 10 chars */
153
154   T((T_CALLED("form_request_by_name(%s)"), _nc_visbuf(str)));
155
156   if (str != 0 && (i = strlen(str)) != 0)
157     {
158       if (i > sizeof(buf) - 2)
159         i = sizeof(buf) - 2;
160       memcpy(buf, str, i);
161       buf[i] = '\0';
162
163       for (i = 0; buf[i] != '\0'; ++i)
164         {
165           buf[i] = (char)toupper(UChar(buf[i]));
166         }
167
168       for (i = 0; i < A_SIZE; i++)
169         {
170           if (strcmp(request_names[i], buf) == 0)
171             returnCode(MIN_FORM_COMMAND + (int)i);
172         }
173     }
174   RETURN(E_NO_MATCH);
175 }
176
177 /* frm_req_name.c ends here */