xref: /openbsd/lib/libmenu/m_req_name.c (revision 5dea098c)
1 /* $OpenBSD: m_req_name.c,v 1.7 2023/10/17 09:52:10 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2020,2021 Thomas E. Dickey                                     *
5  * Copyright 1998-2012,2015 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *   Author:  Juergen Pfeifer, 1995,1997                                    *
34  ****************************************************************************/
35 
36 /***************************************************************************
37 * Module m_request_name                                                    *
38 * Routines to handle external names of menu requests                       *
39 ***************************************************************************/
40 
41 #include "menu.priv.h"
42 
43 MODULE_ID("$Id: m_req_name.c,v 1.7 2023/10/17 09:52:10 nicm Exp $")
44 
45 #define DATA(s) { s }
46 
47 static const char request_names[MAX_MENU_COMMAND - MIN_MENU_COMMAND + 1][14] =
48 {
49   DATA("LEFT_ITEM"),
50   DATA("RIGHT_ITEM"),
51   DATA("UP_ITEM"),
52   DATA("DOWN_ITEM"),
53   DATA("SCR_ULINE"),
54   DATA("SCR_DLINE"),
55   DATA("SCR_DPAGE"),
56   DATA("SCR_UPAGE"),
57   DATA("FIRST_ITEM"),
58   DATA("LAST_ITEM"),
59   DATA("NEXT_ITEM"),
60   DATA("PREV_ITEM"),
61   DATA("TOGGLE_ITEM"),
62   DATA("CLEAR_PATTERN"),
63   DATA("BACK_PATTERN"),
64   DATA("NEXT_MATCH"),
65   DATA("PREV_MATCH")
66 };
67 
68 #define A_SIZE (sizeof(request_names)/sizeof(request_names[0]))
69 
70 /*---------------------------------------------------------------------------
71 |   Facility      :  libnmenu
72 |   Function      :  const char * menu_request_name (int request);
73 |
74 |   Description   :  Get the external name of a menu request.
75 |
76 |   Return Values :  Pointer to name      - on success
77 |                    NULL                 - on invalid request code
78 +--------------------------------------------------------------------------*/
79 MENU_EXPORT(const char *)
80 menu_request_name(int request)
81 {
82   T((T_CALLED("menu_request_name(%d)"), request));
83   if ((request < MIN_MENU_COMMAND) || (request > MAX_MENU_COMMAND))
84     {
85       SET_ERROR(E_BAD_ARGUMENT);
86       returnCPtr((const char *)0);
87     }
88   else
89     returnCPtr(request_names[request - MIN_MENU_COMMAND]);
90 }
91 
92 /*---------------------------------------------------------------------------
93 |   Facility      :  libnmenu
94 |   Function      :  int menu_request_by_name (const char *str);
95 |
96 |   Description   :  Search for a request with this name.
97 |
98 |   Return Values :  Request Id       - on success
99 |                    E_NO_MATCH       - request not found
100 +--------------------------------------------------------------------------*/
101 MENU_EXPORT(int)
102 menu_request_by_name(const char *str)
103 {
104   /* because the table is so small, it doesn't really hurt
105      to run sequentially through it.
106    */
107   size_t i = 0;
108 
109   T((T_CALLED("menu_request_by_name(%s)"), _nc_visbuf(str)));
110 
111   if (str != 0 && (i = strlen(str)) != 0)
112     {
113       char buf[16];
114 
115       if (i > sizeof(buf) - 2)
116 	i = sizeof(buf) - 2;
117       memcpy(buf, str, i);
118       buf[i] = '\0';
119 
120       for (i = 0; buf[i] != '\0'; ++i)
121 	{
122 	  buf[i] = (char)toupper(UChar(buf[i]));
123 	}
124 
125       for (i = 0; i < A_SIZE; i++)
126 	{
127 	  if (strcmp(request_names[i], buf) == 0)
128 	    returnCode(MIN_MENU_COMMAND + (int)i);
129 	}
130     }
131   RETURN(E_NO_MATCH);
132 }
133 
134 /* m_req_name.c ends here */
135