/* Extern's for names and abbreviations of all the commands.

	Copyright (C) 1993-1998 Sebastiano Vigna 
	Copyright (C) 1999-2021 Todd M. Lewis and Sebastiano Vigna

	This file is part of ne, the nice editor.

	This library 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 3 of the License, or (at your
	option) any later version.

	This library 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, see <http://www.gnu.org/licenses/>.  */

#include "ne.h"
#include "strings.h"

struct e2s {
	const char * ext;
	const char * syn;
};

/* A list of mappings extension -> syntax. MUST be sorted on the first field. */

static struct e2s const e2s[] = {
<%%EXTMAP%%>
};

static int extcmp(struct e2s const *a, struct e2s const *b) {
	return strcasecmp(a->ext, b->ext);
}

/* Searches for a mapping matching the given extension in e2s; if found, returns the
   resulting syntax. Otherwise, returns ext. */

const char *ext2syntax(const char * const ext) {
	struct e2s key, *t;
	key.ext = ext;
	key.syn = NULL;
	t = bsearch(&key, e2s, sizeof e2s / sizeof *e2s, sizeof *e2s, (int (*)(const void *, const void *))extcmp);
	return t ? t->syn : ext;
}
