Commit 0f630342 authored by dkg's avatar dkg

imported tweak version 3.01 from the upstream tarball found at

http://www.chiark.greenend.org.uk/~sgtatham/tweak/

this is in preparation for debian packaging.



git-svn-id: svn+ssh://lair/media/bigdisk/groups/cmrg/svn/trunk/tweak/src/tweak@330 40a9a947-7019-0410-b042-cc93585cadc9
parents
Tweak is copyright 1994-2004 Simon Tatham.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Useful options you might want to put on the make command line:
#
# - `SLANG=yes' to build against libslang instead of libncurses
# (libncurses is better and more reliable, but libslang might be
# all you have on a particular platform if you're unlucky).
#
# - `XFLAGS=-DNO_LARGE_FILES' to leave out the 64-bit file access
# support (restricts Tweak to editing files under 2Gb, but
# should cause it to compile successfully on platforms without
# fseeko and ftello and/or long long support).
#
# - `VERSION=X.XX' (for whatever X.XX you like) to cause the `make
# release' target to build a release tarball called
# `tweak-X.XX.tar.gz' which unpacks into a directory
# `tweak-X.XX'. Note that you also need to modify the version
# number in tweak.h, or else the resulting binary won't match
# the version number on the archive.
CC := gcc
CFLAGS := -g -c -Wall $(XFLAGS)
LINK := gcc
LFLAGS :=
LIBS :=
PREFIX=/usr/local
BINDIR=$(PREFIX)/bin
MANDIR=$(PREFIX)/man/man1
TWEAK := main.o keytab.o actions.o search.o rcfile.o buffer.o btree.o
ifeq ($(SLANG),yes)
# INCLUDE += -I/path/to/slang/include
# LIBS += -L/path/to/slang/lib
LIBS += -lslang
TWEAK += slang.o
else
LIBS += -lncurses
TWEAK += curses.o
endif
.c.o:
$(CC) $(CFLAGS) $*.c
all: tweak tweak.1 btree.html
tweak: $(TWEAK)
$(LINK) -o tweak $(TWEAK) $(LIBS)
tweak.1: manpage.but
halibut --man=$@ $<
btree.html: btree.but
halibut --html=$@ $<
# Ensure tweak.h reflects this version number, and then run a
# command like `make release VERSION=3.00'.
release: tweak.1 btree.html
mkdir -p reltmp/tweak-$(VERSION)
for i in LICENCE *.c *.h *.but tweak.1 btree.html Makefile; do \
ln -s ../../$$i reltmp/tweak-$(VERSION); \
done
(cd reltmp; tar chzvf ../tweak-$(VERSION).tar.gz tweak-$(VERSION))
rm -rf reltmp
install: tweak tweak.1
mkdir -p $(BINDIR)
install tweak $(BINDIR)/tweak
mkdir -p $(MANDIR)
install -m 0644 tweak.1 $(MANDIR)/tweak.1
clean:
rm -f *.o tweak tweak.1 btree.html
main.o: main.c tweak.h
keytab.o: keytab.c tweak.h
actions.o: actions.c tweak.h
search.o: search.c tweak.h
rcfile.o: rcfile.c tweak.h
buffer.o: buffer.c tweak.h btree.h
slang.o: slang.c tweak.h
curses.o: curses.c tweak.h
btree.o: btree.c btree.h
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
* Flexible B-tree implementation. Supports reference counting for
* copy-on-write, user-defined node properties, and variable
* degree.
*
* This file is copyright 2001,2004 Simon Tatham.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef BTREE_H
#define BTREE_H
#include <stddef.h> /* for offsetof */
#ifndef alignof
#define alignof(typ) ( offsetof(struct { char c; typ t; }, t) )
#endif
typedef struct btree btree;
typedef void *bt_element_t;
typedef int (*cmpfn_t)(void *state, bt_element_t, bt_element_t);
typedef bt_element_t (*copyfn_t)(void *state, bt_element_t);
typedef void (*freefn_t)(void *state, bt_element_t);
typedef void (*propmakefn_t)(void *state, bt_element_t, void *dest);
/* s1 may be NULL (indicating copy s2 into dest). s2 is never NULL. */
typedef void (*propmergefn_t)(void *state, void *s1, void *s2, void *dest);
typedef int (*searchfn_t)(void *tstate, void *sstate, int ntrees,
void **props, int *counts,
bt_element_t *elts, int *is_elt);
enum {
BT_REL_EQ, BT_REL_LT, BT_REL_LE, BT_REL_GT, BT_REL_GE
};
btree *bt_new(cmpfn_t cmp, copyfn_t copy, freefn_t freeelt,
int propsize, int propalign, propmakefn_t propmake,
propmergefn_t propmerge, void *state, int mindegree);
void bt_free(btree *bt);
btree *bt_clone(btree *bt);
int bt_count(btree *bt);
bt_element_t bt_index(btree *bt, int index);
bt_element_t bt_index_w(btree *bt, int index);
bt_element_t bt_findrelpos(btree *bt, bt_element_t element, cmpfn_t cmp,
int relation, int *index);
bt_element_t bt_findrel(btree *bt, bt_element_t element, cmpfn_t cmp,
int relation);
bt_element_t bt_findpos(btree *bt, bt_element_t element, cmpfn_t cmp,
int *index);
bt_element_t bt_find(btree *bt, bt_element_t element, cmpfn_t cmp);
bt_element_t bt_propfind(btree *bt, searchfn_t search, void *sstate,
int *index);
bt_element_t bt_replace(btree *bt, bt_element_t element, int index);
void bt_addpos(btree *bt, bt_element_t element, int pos);
bt_element_t bt_add(btree *bt, bt_element_t element);
bt_element_t bt_delpos(btree *bt, int pos);
bt_element_t bt_del(btree *bt, bt_element_t element);
btree *bt_join(btree *bt1, btree *bt2);
btree *bt_joinr(btree *bt1, btree *bt2);
btree *bt_splitpos(btree *bt, int index, int before);
btree *bt_split(btree *bt, bt_element_t element, cmpfn_t cmp, int rel);
#endif /* BTREE_H */
This diff is collapsed.
This diff is collapsed.
#include "tweak.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <curses.h>
int display_rows, display_cols;
void display_beep(void)
{
beep();
}
static void get_screen_size (void) {
getmaxyx(stdscr, display_rows, display_cols);
}
void display_setup(void)
{
initscr();
noecho();
keypad(stdscr, 0);
raw();
move(0,0);
refresh();
get_screen_size();
if (has_colors()) {
start_color();
use_default_colors();
}
}
void display_cleanup(void)
{
endwin();
}
void display_moveto(int y, int x)
{
wmove(stdscr, y, x);
}
void display_refresh(void)
{
refresh();
}
void display_write_str(char *str)
{
waddstr(stdscr, str);
}
void display_write_chars(char *str, int len)
{
waddnstr(stdscr, str, len);
}
#define MAXCOLOURS 32
int attrs[MAXCOLOURS];
void display_define_colour(int colour, int fg, int bg, int reverse)
{
static int colours[8] = {
COLOR_BLACK,
COLOR_RED,
COLOR_GREEN,
COLOR_YELLOW,
COLOR_BLUE,
COLOR_MAGENTA,
COLOR_CYAN,
COLOR_WHITE,
};
if (fg < 0 && bg < 0) {
attrs[colour] = 0;
} else {
assert(colour >= 0 && colour < MAXCOLOURS);
assert(!(bg & ~7)); /* bold backgrounds are nonportable */
if (colour < COLOR_PAIRS-2) {
init_pair(colour+1, colours[fg & 7], colours[bg]);
attrs[colour] = (fg & 8 ? A_BOLD : 0) | COLOR_PAIR(colour+1);
} else {
/* can't allocate a colour pair, so we just use b&w attrs */
attrs[colour] = (fg & 8 ? A_BOLD : 0) | (reverse ? A_REVERSE : 0);
}
}
}
void display_set_colour(int colour)
{
wattrset(stdscr, attrs[colour]);
}
void display_clear_to_eol(void)
{
wclrtoeol(stdscr);
}
int last_getch = ERR;
int display_getkey(void)
{
int ret;
extern void schedule_update(void);
if (last_getch != ERR) {
int ret = last_getch;
last_getch = ERR;
return ret;
}
while (1) {
ret = getch();
if (ret == KEY_RESIZE) {
schedule_update();
continue;
}
return ret;
}
}
int display_input_to_flush(void)
{
int ret;
if (last_getch != ERR)
return TRUE;
nodelay(stdscr, 1);
ret = getch();
nodelay(stdscr, 0);
if (ret == ERR)
return FALSE;
last_getch = ret;
return TRUE;
}
void display_post_error(void)
{
/* I don't _think_ we need do anything here */
}
void display_recheck_size(void)
{
get_screen_size ();
}
#include "tweak.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef union keytab keytab;
union keytab {
enum {ACTION, EXTENDED} type;
struct {
int type;
keyact action;
} a;
struct {
int type;
keytab *extended[256];
} e;
};
keytab *base[256] = { NULL256 };
/*
* Bind a key sequence to an action.
*/
void bind_key (char *sequence, int len, keyact action) {
keytab *(*table)[256];
int k, i;
table = &base;
while (--len) {
k = (unsigned char) *sequence++;
if ( !(*table)[k] ) {
/*
* We must create an EXTENDED entry.
*/
(*table)[k] = malloc(sizeof(base[0]->e));
(*table)[k]->type = EXTENDED;
for (i=0; i<256; i++)
(*table)[k]->e.extended[i] = NULL;
} else if ( (*table)[k]->type == ACTION ) {
/*
* A subsequence is already bound: fail.
*/
return;
}
table = &(*table)[k]->e.extended;
}
k = (unsigned char) *sequence;
if ( !(*table)[k] ) {
/*
* We can bind the key.
*/
(*table)[k] = malloc(sizeof(base[0]->a));
(*table)[k]->type = ACTION;
(*table)[k]->a.action = action;
}
}
/*
* Format an ASCII code into a printable description of the key stroke.
*/
static void strkey (char *s, int k) {
k &= 255; /* force unsigned */
if (k==27)
strcpy(s, " ESC");
else if (k<32 || k==127)
sprintf(s, " ^%c", k ^ 64);
else if (k<127)
sprintf(s, " %c", k);
else
sprintf(s, " <0x%2X>", k);
}
/*
* Get and process a key stroke.
*/
void proc_key (void) {
keytab *kt;
#if defined(unix) && !defined(GO32)
if (update_required)
update();
safe_update = TRUE;
#endif
last_char = display_getkey();
#if defined(unix) && !defined(GO32)
safe_update = FALSE;
#endif
strcpy(message, "Unknown key sequence");
strkey(message+strlen(message), last_char);
kt = base[(unsigned char) last_char];
if (!kt) {
display_beep();
while (display_input_to_flush())
strkey(message+strlen(message), display_getkey());
return;
}
while (kt->type == EXTENDED) {
#if defined(unix) && !defined(GO32)
if (update_required)
update();
safe_update = TRUE;
#endif
last_char = display_getkey();
#if defined(unix) && !defined(GO32)
safe_update = FALSE;
#endif
strkey(message+strlen(message), last_char);
kt = kt->e.extended[(unsigned char) last_char];
if (!kt) {
display_beep();
while (display_input_to_flush())
strkey(message+strlen(message), display_getkey());
return;
}
}
message[0] = '\0'; /* clear the "unknown" message */
(*kt->a.action)();
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#include "tweak.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static DFA build_dfa (char *pattern, int len)
{
int i, j, k, b;
char *tmp = malloc(len);
DFA dfa = malloc(len * sizeof(*dfa));
if (!dfa)
return NULL;
if (!tmp)
return NULL;
memcpy (tmp, pattern, len);
for (i=len; i-- ;) {
j = i+1;
for (b=0; b<256; b++) {
dfa[i][b] = 0;
if (memchr(pattern, b, len)) {
tmp[j-1] = b;
for (k=1; k<=j; k++)
if (!memcmp(tmp+j-k, pattern, k))
dfa[i][b] = k;
}
}
}
return dfa;
}
Search *build_search(char *pattern, int len)
{
Search *ret = malloc(sizeof(Search));
char *revpat = malloc(len);
int i;
ret->len = len;
ret->forward = build_dfa(pattern, len);
for (i = 0; i < len; i++)
revpat[i] = pattern[len-1-i];
ret->reverse = build_dfa(revpat, len);
return ret;
}
void free_search(Search *s)
{
free(s->forward);
free(s->reverse);
free(s);
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment