2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: hist.c /main/3 1995/11/01 16:28:36 rswiston $ */
24 /***************************************************************
26 * AT&T - PROPRIETARY *
28 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF *
29 * AT&T BELL LABORATORIES *
30 * AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN *
31 * ACCORDANCE WITH APPLICABLE AGREEMENTS *
33 * Copyright (c) 1995 AT&T Corp. *
34 * Unpublished & Not for Publication *
35 * All Rights Reserved *
37 * The copyright notice above does not evidence any *
38 * actual or intended publication of such source code *
40 * This software was created by the *
41 * Advanced Software Technology Department *
42 * AT&T Bell Laboratories *
44 * For further information contact *
45 * {research,attmail}!dgk *
47 ***************************************************************/
49 /* : : generated by proto : : */
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__ "C"
59 #define __PROTO__(x) x
61 #define __PARAM__(n,o) n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
74 #define __PROTO__(x) ()
75 #define __OTORP__(x) x
76 #define __PARAM__(n,o) o
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__ ...
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a) va_start(p,a)
92 #define __VA_START__(p,a) va_start(p)
100 #include "variables.h"
104 #include "builtins.h"
106 static void hist_subst __PROTO__((const char*, int fd, char*));
108 int b_hist __PARAM__((int argc,char *argv[], __V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[]; __V_ *extra;){
109 register History_t *hp;
111 register int flag,fdo;
116 int range[2], incr, index2, indx= -1;
117 char *edit = 0; /* name of editor */
118 char *replace = 0; /* replace old=new */
119 int lflag = 0, nflag = 0, rflag = 0;
124 error(ERROR_system(1),e_histopen);
126 while((flag = optget(argv,sh_opthist))) switch(flag)
146 if((flag = hist_max(hp) - opt_num-1) < 0)
148 range[++indx] = flag;
155 error(ERROR_usage(2), opt_arg);
158 if(error_info.errors)
159 error(ERROR_usage(2),optusage((char*)0));
160 argv += (opt_index-1);
162 while(flag<1 && (arg=argv[1]))
164 /* look for old=new argument */
165 if(!replace && strchr(arg+1,'='))
171 else if(isdigit(*arg) || *arg == '-')
173 /* see if completely numeric */
175 while(isdigit(*arg));
179 range[++flag] = atoi(arg);
181 range[flag] += (hist_max(hp)-1);
186 /* search for last line starting with string */
187 location = hist_find(hp,argv[1],hist_max(hp)-1,0,-1);
188 if((range[++flag] = location.hist_command) < 0)
189 error(ERROR_exit(1),e_found,argv[1]);
194 /* set default starting range */
197 flag = hist_max(hp)-17;
202 flag = hist_max(hp)-2;
206 index2 = hist_min(hp);
210 /* set default termination range */
211 range[1] = (lflag?hist_max(hp)-2:range[0]);
212 if(range[1]>=(flag=(hist_max(hp) - 1)))
214 /* check for valid ranges */
215 if(range[1]<index2 || range[0]>=flag)
216 error(ERROR_exit(1),e_badrange,range[0],range[1]);
217 if(edit && *edit=='-' && range[0]!=range[1])
218 error(ERROR_exit(1),e_eneedsarg);
219 /* now list commands from range[rflag] to range[1-rflag] */
222 if(range[1-flag] < range[flag])
231 if(!(fname=pathtemp(NIL(char*),0,0)))
232 error(ERROR_exit(1),e_create,"");
233 if((fdo=open(fname,O_CREAT|O_RDWR,S_IRUSR|S_IWUSR)) < 0)
234 error(ERROR_system(1),e_create,fname);
235 outfile= sfnew(NIL(Sfio_t*),sh.outbuff,IOBSIZE,fdo,SF_WRITE);
242 sfprintf(outfile,"%d\t",range[flag]);
244 sfputc(outfile,'\t');
245 hist_list(sh.hist_ptr,outfile,hist_tell(sh.hist_ptr,range[flag]),0,arg);
248 if(range[flag] == range[1-flag])
254 if(fstat(sffileno(outfile),&statb)>=0)
255 before = statb.st_mtime;
259 if(!arg && !(arg=nv_getval(nv_scoped(HISTEDIT))) && !(arg=nv_getval(nv_scoped(FCEDNOD))))
260 arg = (char*)e_defedit;
263 * Code to support the FC using the pad editor.
264 * Exampled of how to use: HISTEDIT=pad
266 if (strcmp (arg, "pad") == 0)
268 extern __MANGLE__ int pad_create __PROTO__((char*));
270 fdo = pad_create(fname);
273 strcat(fname, ".bak");
275 lseek(fdo,(off_t)0,SEEK_SET);
286 error_info.errors = sh_eval(sh_sfeval(com),0);
288 fdo = sh_chkopen(fname);
294 /* don't history fc itself unless forked */
295 error_info.flags |= ERROR_SILENT;
296 if(!sh_isstate(SH_FORKED))
298 sh_onstate(SH_VERBOSE|SH_HISTORY); /* echo lines as read */
300 hist_subst(error_info.id,fdo,replace);
301 else if(error_info.errors == 0)
303 char buff[IOBSIZE+1];
304 Sfio_t *iop = sfnew(NIL(Sfio_t*),buff,IOBSIZE,fdo,SF_READ);
305 /* read in and run the command */
311 if(!sh_isoption(SH_VERBOSE))
312 sh_offstate(SH_VERBOSE|SH_HISTORY);
319 * given a file containing a command and a string of the form old=new,
320 * execute the command with the string old replaced by new
323 static void hist_subst __PARAM__((const char *command,int fd,char *replace), (command, fd, replace)) __OTORP__(const char *command;int fd;char *replace;){
324 register char *newp=replace;
329 while(*++newp != '='); /* skip to '=' */
330 if((size = lseek(fd,(off_t)0,SEEK_END)) < 0)
332 lseek(fd,(off_t)0,SEEK_SET);
334 string = stakalloc(c+1);
335 if(read(fd,string,c)!=c)
339 if((sp=sh_substitute(string,replace,newp))==0)
340 error(ERROR_exit(1),e_subst,command);
342 sh_eval(sfopen(NIL(Sfio_t*),sp,"s"),1);