2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 /* ---------------------------- included header files ---------------------- */
26 #include "safemalloc.h"
28 /* ---------------------------- local definitions -------------------------- */
30 /* ---------------------------- local macros ------------------------------- */
32 /* ---------------------------- imports ------------------------------------ */
34 /* ---------------------------- included code files ------------------------ */
36 /* ---------------------------- local types -------------------------------- */
38 /* ---------------------------- forward declarations ----------------------- */
40 /* ---------------------------- local variables ---------------------------- */
42 /* ---------------------------- exported variables (globals) --------------- */
44 /* ---------------------------- local functions ---------------------------- */
46 /* ---------------------------- interface functions ------------------------ */
48 /* Turns a string context of context or modifier values into an array of
49 * true/false values (bits). */
50 int charmap_string_to_mask(
51 int *ret
, const char *string
, charmap_t
*table
, char *errstring
)
53 int len
= strlen(string
);
58 for (i
= 0; i
< len
; ++i
)
64 c
= tolower(string
[i
]);
65 for (j
= 0, found_match
= 0; table
[j
].key
!= 0; j
++)
67 if (table
[j
].key
== c
)
69 *ret
|= table
[j
].value
;
76 fputs("charmap_string_to_mask: ", stderr
);
77 if (errstring
!= NULL
)
79 fputs(errstring
, stderr
);
91 /* Reverse function of above. Returns zero if no matching mask is found in the
93 char charmap_mask_to_char(int mask
, charmap_t
*table
)
97 for (c
= 0; table
->key
!= 0; table
++)
99 if (mask
== table
->value
)
109 /* Used from "PrintInfo Bindings". */
110 char *charmap_table_to_string(int mask
, charmap_t
*table
)
118 allmods
= safemalloc(sizeof(table
->value
) * 8 + 1);
120 for (; table
->key
!=0; table
++)
122 c
[0] = toupper(table
->key
);
124 /* Don't explicitly match "A" for any context as doing so
125 * means we never see the individual bindings. Incremental
126 * matching here for AnyContext is disasterous.*/
127 if ((modmask
& table
->value
) &&
128 (table
->value
!= C_ALL
))
130 /* incremental match */
132 modmask
&= ~table
->value
;
134 else if (mask
== table
->value
)