1 /***********************************************************************
2  *                                                                      *
3  *               This software is part of the ast package               *
4  *          Copyright (c) 1982-2012 AT&T Intellectual Property          *
5  *                      and is licensed under the                       *
6  *                 Eclipse Public License, Version 1.0                  *
7  *                    by AT&T Intellectual Property                     *
8  *                                                                      *
9  *                A copy of the License is available at                 *
10  *          http://www.eclipse.org/org/documents/epl-v10.html           *
11  *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12  *                                                                      *
13  *              Information and Software Systems Research               *
14  *                            AT&T Research                             *
15  *                           Florham Park NJ                            *
16  *                                                                      *
17  *                    David Korn <dgkorn@gmail.com>                     *
18  *                                                                      *
19  ***********************************************************************/
20 //
21 // Interface for history mechanism.
22 // Written by David Korn.
23 //
24 #ifndef _HISTORY_H
25 #define _HISTORY_H 1
26 
27 #define HIST_CHAR '!'
28 #define HIST_VERSION 1  // history file format version no.
29 
30 //
31 // Each command in the history file starts on an even byte is null terminated. The first byte must
32 // contain the special character HIST_UNDO and the second byte is the version number.  The sequence
33 // HIST_UNDO 0, following a command, nullifies the previous command. A six byte sequence starting
34 // with HIST_CMDNO is used to store the command number so that it is not necessary to read the file
35 // from beginning to end to get to the last block of commands.  This format of this sequence is
36 // different in version 1 then in version 0.  Version 1 allows commands to use the full 8 bit
37 // character set.  It can understand version 0 format files.
38 //
39 #ifndef HIST_DFLT
40 #define HIST_DFLT 512  // default size of history list
41 #define HIST_MAX (sizeof(int) * HIST_BSIZE)
42 #define HIST_RECENT 600
43 #endif
44 #define HIST_LINE 32  // typical length for history line
45 #define HIST_MARKSZ 6
46 #define HIST_UNDO 0201   // invalidate previous command
47 #define HIST_CMDNO 0202  // next 3 bytes give command number
48 #define HIST_BSIZE 4096  // size of history file buffer
49 
50 typedef struct {
51     Sfdisc_t histdisc;  // discipline for history
52     Sfio_t *histfp;     // history file stream pointer
53     char *histname;     // name of history file
54     int32_t histind;    // current command number index
55     int histsize;       // number of accessible history lines
56     Shell_t *histshell;
57     off_t histcnt;                  // offset into history file
58     off_t histmarker;               // offset of last command marker
59     int histflush;                  // set if flushed outside of hflush()
60     int histmask;                   // power of two mask for histcnt
61     char histbuff[HIST_BSIZE + 1];  // history file buffer
62     int histwfail;
63     Sfio_t *auditfp;
64     char *tty;
65     int auditmask;
66     off_t histcmds[2];  // offset for recent commands, must be last
67 } History_t;
68 
69 typedef struct {
70     int hist_command;
71     int hist_line;
72     int hist_char;
73 } Histloc_t;
74 
75 // The following are readonly.
76 extern const char hist_fname[];
77 
78 extern int _Hist;
79 #define hist_min(hp) ((_Hist = ((int)((hp)->histind - (hp)->histsize))) >= 0 ? _Hist : 0)
80 #define hist_max(hp) ((int)((hp)->histind))
81 // These are the history interface routines.
82 extern int sh_histinit(void *);
83 extern void hist_cancel(History_t *);
84 extern void hist_close(History_t *);
85 extern int hist_copy(char *, int, int, int);
86 extern void hist_eof(History_t *);
87 extern Histloc_t hist_find(History_t *, char *, int, int, int);
88 extern void hist_flush(History_t *);
89 extern void hist_list(History_t *, Sfio_t *, off_t, int, const char *);
90 extern int hist_match(History_t *, off_t, char *, int *);
91 extern off_t hist_tell(History_t *, int);
92 extern off_t hist_seek(History_t *, int);
93 extern char *hist_word(char *, int, int);
94 extern Histloc_t hist_locate(History_t *, int, int, int);
95 
96 #endif  // _HISTORY_H
97