titlebar: correctly ban/unban (FS#443)
[awesome.git] / build-utils / gperf.sh
blob56717d5e83150aed656b2fe8ce8362e3ab371d58
1 #! /bin/sh -e
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 'a-z-./ ' 'A-Z____' | 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 'a-z-./ ' 'A-Z____'`"
68 esac
69 done
72 do_c() {
73 if ! which gperf > /dev/null; then
74 echo "gperf not found. You need to install gperf." > /dev/stderr;
75 exit 1;
76 fi;
78 cat <<EOF | gperf -l -t -C -F",0" \
79 --language=ANSI-C -Na_tokenize_aux \
80 | sed -e '/__gnu_inline__/d;s/\<\(__\|\)inline\>//g'
82 `do_hdr`
84 #include <string.h>
85 #include "common/tokenize.h"
87 static const struct tok *a_tokenize_aux(const char *str, unsigned int len);
90 struct tok { const char *name; int val; };
92 `do_tokens`
95 awesome_token_t a_tokenize(const char *s, int len)
97 if (len < 0)
98 len = (int)strlen(s);
100 if (len) {
101 const struct tok *res = a_tokenize_aux(s, len);
102 return res ? res->val : A_TK_UNKNOWN;
103 } else {
104 return A_TK_UNKNOWN;
110 extract_tokens() {
111 grep '^### ' "$1" | cut -d ' ' -f 2
115 TOKENS_FILE="$1"
116 TARGET="$2"
118 trap "rm -f ${TARGET}" 0
120 rm -f "${TARGET}"
121 case "${TARGET}" in
122 *.h) cat "${TOKENS_FILE}" | do_h > "${TARGET}";;
123 *.c) cat "${TOKENS_FILE}" | do_c > "${TARGET}";;
124 *) die "you must ask for the 'h' or 'c' generation";;
125 esac
126 chmod -w "${TARGET}"
128 trap - 0
129 exit 0