awful.menu: support for access keys
[awesome.git] / build-utils / gperf.sh
blob221a096fbb6132a8901066abe387e7cff917f9da
1 #!/bin/sh
3 # Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 die() {
22 echo "$@" 1>&2
23 exit 2
26 do_hdr() {
27 cat <<EOF
28 /* This file is autogenerated by $(basename $0) */
30 EOF
33 out=
34 type_t=
36 while true; do
37 test $# -gt 2 || break
38 case "$1" in
39 -o) shift; out="$1"; shift;;
40 -t) shift; type_t="$1"; shift;;
41 *) break;;
42 esac
43 done
45 do_h() {
46 cat <<EOF
47 `do_hdr`
48 #ifndef AWESOME_TOKENIZE_H
49 #define AWESOME_TOKENIZE_H
51 typedef enum awesome_token_t {
52 A_TK_UNKNOWN,
53 `tr '[:lower:]' '[:upper:]' | sed -e "s/^[^/].*/ A_TK_&,/"`
54 } awesome_token_t;
56 __attribute__((pure)) enum awesome_token_t a_tokenize(const char *s, int len);
57 #endif
58 EOF
61 do_tokens() {
62 while read tok; do
63 case "$tok" in
64 "") continue;;
66 echo "$tok, A_TK_`echo $tok | tr '[:lower:]' '[:upper:]'`"
68 esac
69 done
72 do_c() {
73 which gperf > /dev/null
74 if test $? = 1 ; then
75 echo "gperf not found. You need to install gperf." > /dev/stderr;
76 exit 1;
77 fi;
79 gperf -l -t -C -F",0" \
80 --language=ANSI-C -Na_tokenize_aux <<EOF \
81 | sed -e '/__gnu_inline__/d;s/\<\(__\|\)inline\>//g'
83 `do_hdr`
85 #include <string.h>
86 #include "common/tokenize.h"
88 static const struct tok *a_tokenize_aux(const char *str, unsigned int len);
91 struct tok { const char *name; int val; };
93 `do_tokens`
96 awesome_token_t a_tokenize(const char *s, int len)
98 if (len < 0)
99 len = (int)strlen(s);
101 if (len) {
102 const struct tok *res = a_tokenize_aux(s, len);
103 return res ? res->val : A_TK_UNKNOWN;
104 } else {
105 return A_TK_UNKNOWN;
111 extract_tokens() {
112 grep '^### ' "$1" | cut -d ' ' -f 2
116 TOKENS_FILE="$1"
117 TARGET="$2"
119 trap "rm -f ${TARGET}" 0
121 rm -f "${TARGET}"
122 case "${TARGET}" in
123 *.h) do_h > "${TARGET}" < "${TOKENS_FILE}" ;;
124 *.c) do_c > "${TARGET}" < "${TOKENS_FILE}" ;;
125 *) die "you must ask for the 'h' or 'c' generation";;
126 esac
127 chmod -w "${TARGET}"
129 trap - 0
130 exit 0