xref: /openbsd-src/lib/libmenu/m_req_name.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1*c7ef0cfcSnicm /* $OpenBSD: m_req_name.c,v 1.7 2023/10/17 09:52:10 nicm Exp $ */
29f1aa62bSmillert 
3457960bfSmillert /****************************************************************************
4*c7ef0cfcSnicm  * Copyright 2020,2021 Thomas E. Dickey                                     *
5*c7ef0cfcSnicm  * Copyright 1998-2012,2015 Free Software Foundation, Inc.                  *
6457960bfSmillert  *                                                                          *
7457960bfSmillert  * Permission is hereby granted, free of charge, to any person obtaining a  *
8457960bfSmillert  * copy of this software and associated documentation files (the            *
9457960bfSmillert  * "Software"), to deal in the Software without restriction, including      *
10457960bfSmillert  * without limitation the rights to use, copy, modify, merge, publish,      *
11457960bfSmillert  * distribute, distribute with modifications, sublicense, and/or sell       *
12457960bfSmillert  * copies of the Software, and to permit persons to whom the Software is    *
13457960bfSmillert  * furnished to do so, subject to the following conditions:                 *
14457960bfSmillert  *                                                                          *
15457960bfSmillert  * The above copyright notice and this permission notice shall be included  *
16457960bfSmillert  * in all copies or substantial portions of the Software.                   *
17457960bfSmillert  *                                                                          *
18457960bfSmillert  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19457960bfSmillert  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20457960bfSmillert  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21457960bfSmillert  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22457960bfSmillert  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23457960bfSmillert  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24457960bfSmillert  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25457960bfSmillert  *                                                                          *
26457960bfSmillert  * Except as contained in this notice, the name(s) of the above copyright   *
27457960bfSmillert  * holders shall not be used in advertising or otherwise to promote the     *
28457960bfSmillert  * sale, use or other dealings in this Software without prior written       *
29457960bfSmillert  * authorization.                                                           *
30457960bfSmillert  ****************************************************************************/
31457960bfSmillert 
32457960bfSmillert /****************************************************************************
3381d8c4e1Snicm  *   Author:  Juergen Pfeifer, 1995,1997                                    *
34457960bfSmillert  ****************************************************************************/
350107aba4Smillert 
360107aba4Smillert /***************************************************************************
379f1aa62bSmillert * Module m_request_name                                                    *
380107aba4Smillert * Routines to handle external names of menu requests                       *
390107aba4Smillert ***************************************************************************/
400107aba4Smillert 
410107aba4Smillert #include "menu.priv.h"
420107aba4Smillert 
43*c7ef0cfcSnicm MODULE_ID("$Id: m_req_name.c,v 1.7 2023/10/17 09:52:10 nicm Exp $")
440107aba4Smillert 
45*c7ef0cfcSnicm #define DATA(s) { s }
46*c7ef0cfcSnicm 
47*c7ef0cfcSnicm static const char request_names[MAX_MENU_COMMAND - MIN_MENU_COMMAND + 1][14] =
4881d8c4e1Snicm {
49*c7ef0cfcSnicm   DATA("LEFT_ITEM"),
50*c7ef0cfcSnicm   DATA("RIGHT_ITEM"),
51*c7ef0cfcSnicm   DATA("UP_ITEM"),
52*c7ef0cfcSnicm   DATA("DOWN_ITEM"),
53*c7ef0cfcSnicm   DATA("SCR_ULINE"),
54*c7ef0cfcSnicm   DATA("SCR_DLINE"),
55*c7ef0cfcSnicm   DATA("SCR_DPAGE"),
56*c7ef0cfcSnicm   DATA("SCR_UPAGE"),
57*c7ef0cfcSnicm   DATA("FIRST_ITEM"),
58*c7ef0cfcSnicm   DATA("LAST_ITEM"),
59*c7ef0cfcSnicm   DATA("NEXT_ITEM"),
60*c7ef0cfcSnicm   DATA("PREV_ITEM"),
61*c7ef0cfcSnicm   DATA("TOGGLE_ITEM"),
62*c7ef0cfcSnicm   DATA("CLEAR_PATTERN"),
63*c7ef0cfcSnicm   DATA("BACK_PATTERN"),
64*c7ef0cfcSnicm   DATA("NEXT_MATCH"),
65*c7ef0cfcSnicm   DATA("PREV_MATCH")
660107aba4Smillert };
6781d8c4e1Snicm 
680107aba4Smillert #define A_SIZE (sizeof(request_names)/sizeof(request_names[0]))
690107aba4Smillert 
700107aba4Smillert /*---------------------------------------------------------------------------
710107aba4Smillert |   Facility      :  libnmenu
720107aba4Smillert |   Function      :  const char * menu_request_name (int request);
730107aba4Smillert |
740107aba4Smillert |   Description   :  Get the external name of a menu request.
750107aba4Smillert |
760107aba4Smillert |   Return Values :  Pointer to name      - on success
770107aba4Smillert |                    NULL                 - on invalid request code
780107aba4Smillert +--------------------------------------------------------------------------*/
79*c7ef0cfcSnicm MENU_EXPORT(const char *)
menu_request_name(int request)8084af20ceSmillert menu_request_name(int request)
810107aba4Smillert {
8281d8c4e1Snicm   T((T_CALLED("menu_request_name(%d)"), request));
830107aba4Smillert   if ((request < MIN_MENU_COMMAND) || (request > MAX_MENU_COMMAND))
840107aba4Smillert     {
850107aba4Smillert       SET_ERROR(E_BAD_ARGUMENT);
8681d8c4e1Snicm       returnCPtr((const char *)0);
870107aba4Smillert     }
880107aba4Smillert   else
8981d8c4e1Snicm     returnCPtr(request_names[request - MIN_MENU_COMMAND]);
900107aba4Smillert }
910107aba4Smillert 
920107aba4Smillert /*---------------------------------------------------------------------------
930107aba4Smillert |   Facility      :  libnmenu
940107aba4Smillert |   Function      :  int menu_request_by_name (const char *str);
950107aba4Smillert |
960107aba4Smillert |   Description   :  Search for a request with this name.
970107aba4Smillert |
980107aba4Smillert |   Return Values :  Request Id       - on success
990107aba4Smillert |                    E_NO_MATCH       - request not found
1000107aba4Smillert +--------------------------------------------------------------------------*/
101*c7ef0cfcSnicm MENU_EXPORT(int)
menu_request_by_name(const char * str)10284af20ceSmillert menu_request_by_name(const char *str)
1030107aba4Smillert {
1040107aba4Smillert   /* because the table is so small, it doesn't really hurt
1050107aba4Smillert      to run sequentially through it.
1060107aba4Smillert    */
107*c7ef0cfcSnicm   size_t i = 0;
1080107aba4Smillert 
10981d8c4e1Snicm   T((T_CALLED("menu_request_by_name(%s)"), _nc_visbuf(str)));
11081d8c4e1Snicm 
111*c7ef0cfcSnicm   if (str != 0 && (i = strlen(str)) != 0)
1120107aba4Smillert     {
113*c7ef0cfcSnicm       char buf[16];
114*c7ef0cfcSnicm 
115*c7ef0cfcSnicm       if (i > sizeof(buf) - 2)
116*c7ef0cfcSnicm 	i = sizeof(buf) - 2;
117*c7ef0cfcSnicm       memcpy(buf, str, i);
118*c7ef0cfcSnicm       buf[i] = '\0';
119*c7ef0cfcSnicm 
120*c7ef0cfcSnicm       for (i = 0; buf[i] != '\0'; ++i)
1210107aba4Smillert 	{
122*c7ef0cfcSnicm 	  buf[i] = (char)toupper(UChar(buf[i]));
1230107aba4Smillert 	}
1240107aba4Smillert 
1250107aba4Smillert       for (i = 0; i < A_SIZE; i++)
1260107aba4Smillert 	{
127*c7ef0cfcSnicm 	  if (strcmp(request_names[i], buf) == 0)
12881d8c4e1Snicm 	    returnCode(MIN_MENU_COMMAND + (int)i);
1290107aba4Smillert 	}
1300107aba4Smillert     }
1310107aba4Smillert   RETURN(E_NO_MATCH);
1320107aba4Smillert }
1330107aba4Smillert 
1340107aba4Smillert /* m_req_name.c ends here */
135