⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 history.c

📁 asterisk 是一个很有知名度开源软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/*	$NetBSD: history.c,v 1.19 2002/03/18 16:00:54 christos Exp $	*//*- * Copyright (c) 1992, 1993 *	The Regents of the University of California.  All rights reserved. * * This code is derived from software contributed to Berkeley by * Christos Zoulas of Cornell University. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software *    must display the following acknowledgement: *	This product includes software developed by the University of *	California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors *    may be used to endorse or promote products derived from this software *    without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#include "config.h"#if !defined(lint) && !defined(SCCSID)#if 0static char sccsid[] = "@(#)history.c	8.1 (Berkeley) 6/4/93";#else__RCSID("$NetBSD: history.c,v 1.19 2002/03/18 16:00:54 christos Exp $");#endif#endif /* not lint && not SCCSID *//* * hist.c: History access functions */#include <string.h>#include <stdlib.h>#include <stdarg.h>#ifdef HAVE_VIS_H#include <vis.h>#else#include "np/vis.h"#endif#include <sys/stat.h>static const char hist_cookie[] = "_HiStOrY_V2_\n";#include "histedit.h"typedef int (*history_gfun_t)(ptr_t, HistEvent *);typedef int (*history_efun_t)(ptr_t, HistEvent *, const char *);typedef void (*history_vfun_t)(ptr_t, HistEvent *);typedef int (*history_sfun_t)(ptr_t, HistEvent *, const int);struct history {	ptr_t h_ref;		/* Argument for history fcns	 */	int h_ent;		/* Last entry point for history	 */	history_gfun_t h_first;	/* Get the first element	 */	history_gfun_t h_next;	/* Get the next element		 */	history_gfun_t h_last;	/* Get the last element		 */	history_gfun_t h_prev;	/* Get the previous element	 */	history_gfun_t h_curr;	/* Get the current element	 */	history_sfun_t h_set;	/* Set the current element	 */	history_vfun_t h_clear;	/* Clear the history list	 */	history_efun_t h_enter;	/* Add an element		 */	history_efun_t h_add;	/* Append to an element		 */};#define	HNEXT(h, ev)		(*(h)->h_next)((h)->h_ref, ev)#define	HFIRST(h, ev)		(*(h)->h_first)((h)->h_ref, ev)#define	HPREV(h, ev)		(*(h)->h_prev)((h)->h_ref, ev)#define	HLAST(h, ev)		(*(h)->h_last)((h)->h_ref, ev)#define	HCURR(h, ev)		(*(h)->h_curr)((h)->h_ref, ev)#define	HSET(h, ev, n)		(*(h)->h_set)((h)->h_ref, ev, n)#define	HCLEAR(h, ev)		(*(h)->h_clear)((h)->h_ref, ev)#define	HENTER(h, ev, str)	(*(h)->h_enter)((h)->h_ref, ev, str)#define	HADD(h, ev, str)	(*(h)->h_add)((h)->h_ref, ev, str)#define	h_malloc(a)	malloc(a)#define	h_realloc(a, b)	realloc((a), (b))#define	h_free(a)	free(a)typedef struct {    int		num;    char	*str;} HistEventPrivate;private int history_setsize(History *, HistEvent *, int);private int history_getsize(History *, HistEvent *);private int history_set_fun(History *, History *);private int history_load(History *, const char *);private int history_save(History *, const char *);private int history_prev_event(History *, HistEvent *, int);private int history_next_event(History *, HistEvent *, int);private int history_next_string(History *, HistEvent *, const char *);private int history_prev_string(History *, HistEvent *, const char *);/***********************************************************************//* * Builtin- history implementation */typedef struct hentry_t {	HistEvent ev;		/* What we return		 */	struct hentry_t *next;	/* Next entry			 */	struct hentry_t *prev;	/* Previous entry		 */}        hentry_t;typedef struct history_t {	hentry_t list;		/* Fake list header element	 */	hentry_t *cursor;	/* Current element in the list	 */	int max;		/* Maximum number of events	 */	int cur;		/* Current number of events	 */	int eventid;		/* For generation of unique event id	 */}         history_t;private int history_def_first(ptr_t, HistEvent *);private int history_def_last(ptr_t, HistEvent *);private int history_def_next(ptr_t, HistEvent *);private int history_def_prev(ptr_t, HistEvent *);private int history_def_curr(ptr_t, HistEvent *);private int history_def_set(ptr_t, HistEvent *, const int n);private int history_def_enter(ptr_t, HistEvent *, const char *);private int history_def_add(ptr_t, HistEvent *, const char *);private void history_def_init(ptr_t *, HistEvent *, int);private void history_def_clear(ptr_t, HistEvent *);private int history_def_insert(history_t *, HistEvent *, const char *);private void history_def_delete(history_t *, HistEvent *, hentry_t *);#define	history_def_setsize(p, num)(void) (((history_t *) p)->max = (num))#define	history_def_getsize(p)  (((history_t *) p)->cur)#define	he_strerror(code)	he_errlist[code]#define	he_seterrev(evp, code)	{\				    evp->num = code;\				    evp->str = he_strerror(code);\				}/* error messages */static const char *const he_errlist[] = {	"OK",	"unknown error",	"malloc() failed",	"first event not found",	"last event not found",	"empty list",	"no next event",	"no previous event",	"current event is invalid",	"event not found",	"can't read history from file",	"can't write history",	"required parameter(s) not supplied",	"history size negative",	"function not allowed with other history-functions-set the default",	"bad parameters"};/* error codes */#define	_HE_OK                   0#define	_HE_UNKNOWN		 1#define	_HE_MALLOC_FAILED        2#define	_HE_FIRST_NOTFOUND       3#define	_HE_LAST_NOTFOUND        4#define	_HE_EMPTY_LIST           5#define	_HE_END_REACHED          6#define	_HE_START_REACHED	 7#define	_HE_CURR_INVALID	 8#define	_HE_NOT_FOUND		 9#define	_HE_HIST_READ		10#define	_HE_HIST_WRITE		11#define	_HE_PARAM_MISSING	12#define	_HE_SIZE_NEGATIVE	13#define	_HE_NOT_ALLOWED		14#define	_HE_BAD_PARAM		15/* history_def_first(): *	Default function to return the first event in the history. */private inthistory_def_first(ptr_t p, HistEvent *ev){	history_t *h = (history_t *) p;	h->cursor = h->list.next;	if (h->cursor != &h->list)		*ev = h->cursor->ev;	else {		he_seterrev(ev, _HE_FIRST_NOTFOUND);		return (-1);	}	return (0);}/* history_def_last(): *	Default function to return the last event in the history. */private inthistory_def_last(ptr_t p, HistEvent *ev){	history_t *h = (history_t *) p;	h->cursor = h->list.prev;	if (h->cursor != &h->list)		*ev = h->cursor->ev;	else {		he_seterrev(ev, _HE_LAST_NOTFOUND);		return (-1);	}	return (0);}/* history_def_next(): *	Default function to return the next event in the history. */private inthistory_def_next(ptr_t p, HistEvent *ev){	history_t *h = (history_t *) p;	if (h->cursor != &h->list)		h->cursor = h->cursor->next;	else {		he_seterrev(ev, _HE_EMPTY_LIST);		return (-1);	}	if (h->cursor != &h->list)		*ev = h->cursor->ev;	else {		he_seterrev(ev, _HE_END_REACHED);		return (-1);	}	return (0);}/* history_def_prev(): *	Default function to return the previous event in the history. */private inthistory_def_prev(ptr_t p, HistEvent *ev){	history_t *h = (history_t *) p;	if (h->cursor != &h->list)		h->cursor = h->cursor->prev;	else {		he_seterrev(ev,		    (h->cur > 0) ? _HE_END_REACHED : _HE_EMPTY_LIST);		return (-1);	}	if (h->cursor != &h->list)		*ev = h->cursor->ev;	else {		he_seterrev(ev, _HE_START_REACHED);		return (-1);	}	return (0);}/* history_def_curr(): *	Default function to return the current event in the history. */private inthistory_def_curr(ptr_t p, HistEvent *ev){	history_t *h = (history_t *) p;	if (h->cursor != &h->list)		*ev = h->cursor->ev;	else {		he_seterrev(ev,		    (h->cur > 0) ? _HE_CURR_INVALID : _HE_EMPTY_LIST);		return (-1);	}	return (0);}/* history_def_set(): *	Default function to set the current event in the history to the *	given one. */private inthistory_def_set(ptr_t p, HistEvent *ev, const int n){	history_t *h = (history_t *) p;	if (h->cur == 0) {		he_seterrev(ev, _HE_EMPTY_LIST);		return (-1);	}	if (h->cursor == &h->list || h->cursor->ev.num != n) {		for (h->cursor = h->list.next; h->cursor != &h->list;		    h->cursor = h->cursor->next)			if (h->cursor->ev.num == n)				break;	}	if (h->cursor == &h->list) {		he_seterrev(ev, _HE_NOT_FOUND);		return (-1);	}	return (0);}/* history_def_add(): *	Append string to element */private inthistory_def_add(ptr_t p, HistEvent *ev, const char *str){	history_t *h = (history_t *) p;	size_t len;	char *s;	HistEventPrivate *evp = (void *)&h->cursor->ev;	if (h->cursor == &h->list)		return (history_def_enter(p, ev, str));	len = strlen(evp->str) + strlen(str) + 1;	s = (char *) h_malloc(len);	if (!s) {		he_seterrev(ev, _HE_MALLOC_FAILED);		return (-1);	}	(void) strlcpy(s, h->cursor->ev.str, len);	(void) strlcat(s, str, len);	h_free(evp->str);	evp->str = s;	*ev = h->cursor->ev;	return (0);}/* history_def_delete(): *	Delete element hp of the h list *//* ARGSUSED */private voidhistory_def_delete(history_t *h, HistEvent *ev, hentry_t *hp){	HistEventPrivate *evp = (void *)&hp->ev;	if (hp == &h->list)		abort();	hp->prev->next = hp->next;	hp->next->prev = hp->prev;	h_free((ptr_t) evp->str);	h_free(hp);	h->cur--;}/* history_def_insert(): *	Insert element with string str in the h list */private inthistory_def_insert(history_t *h, HistEvent *ev, const char *str){	h->cursor = (hentry_t *) h_malloc(sizeof(hentry_t));	if (h->cursor)		h->cursor->ev.str = strdup(str);	if (!h->cursor || !h->cursor->ev.str) {		he_seterrev(ev, _HE_MALLOC_FAILED);		return (-1);	}	h->cursor->ev.num = ++h->eventid;	h->cursor->next = h->list.next;	h->cursor->prev = &h->list;	h->list.next->prev = h->cursor;	h->list.next = h->cursor;	h->cur++;	*ev = h->cursor->ev;	return (0);}/* history_def_enter(): *	Default function to enter an item in the history */private inthistory_def_enter(ptr_t p, HistEvent *ev, const char *str){	history_t *h = (history_t *) p;	if (history_def_insert(h, ev, str) == -1)		return (-1);	/* error, keep error message */	/*         * Always keep at least one entry.         * This way we don't have to check for the empty list.         */	while (h->cur > h->max && h->cur > 0)		history_def_delete(h, ev, h->list.prev);	return (0);}/* history_def_init(): *	Default history initialization function *//* ARGSUSED */private voidhistory_def_init(ptr_t *p, HistEvent *ev, int n){	history_t *h = (history_t *) h_malloc(sizeof(history_t));	if (n <= 0)		n = 0;	h->eventid = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -