generalize the idea of tokens a bit more, don't restrict it to the rfc822
[apps/madmutt.git] / lib-mime / mime-token.sh
diff --git a/lib-mime/mime-token.sh b/lib-mime/mime-token.sh
new file mode 100644 (file)
index 0000000..bb55179
--- /dev/null
@@ -0,0 +1,87 @@
+#! /bin/sh -e
+
+die() {
+    echo "$@" 1>&2
+    exit 2
+}
+
+do_hdr() {
+    cat <<EOF
+/*
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or (at
+ *  your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful, but
+ *  WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  Copyright © 2006 Pierre Habouzit
+ */
+
+/*****     THIS FILE IS AUTOGENERATED DO NOT MODIFY DIRECTLY !    *****/
+
+EOF
+}
+
+do_h() {
+    do_hdr
+    cat <<EOF
+#ifndef MUTT_LIB_MIME_MIME_TOKEN_H
+#define MUTT_LIB_MIME_MIME_TOKEN_H
+
+enum mime_token {
+    MUTT_MIME_TOKEN_UNKNOWN,
+`tr 'a-z-' 'A-Z_' | sed -e 's/.*/    MIME_&,/'`
+};
+
+__attribute__((pure))
+enum mime_token mime_which_token(const char *s, ssize_t len);
+#endif /* MUTT_LIB_MIME_MIME_TOKEN_H */
+EOF
+}
+
+do_c() {
+    cat <<EOF | gperf --ignore-case -l -t -C -F,0
+%{
+`do_hdr`
+
+#include <lib-lib/str.h>
+#include "mime-token.h"
+
+%}
+struct tok { const char *name; int val; };
+%%
+`awk '{print $0 ", " NR }'`
+%%
+
+enum mime_token mime_which_token(const char *s, ssize_t len) {
+    const struct tok *res;
+
+    if (len < 0)
+        len = m_strlen(s);
+    if (!len)
+        return MUTT_MIME_TOKEN_UNKNOWN;
+
+    res = in_word_set(s, len);
+    return res ? res->val : MUTT_MIME_TOKEN_UNKNOWN;
+}
+EOF
+}
+
+trap "rm -f $1" 1 2 3 15
+
+case "$1" in
+    *.h) do_h > "$1";;
+    *.c) do_c > "$1";;
+    *)  die "you must ask for the 'h' or 'c' generation";;
+esac
+
+exit 0