xref: /csrg-svn/local/toolchest/ksh/sh/editlib.c (revision 35138)
1*35138Smarc /*
2*35138Smarc 
3*35138Smarc  *      Copyright (c) 1984, 1985, 1986 AT&T
4*35138Smarc  *      All Rights Reserved
5*35138Smarc 
6*35138Smarc  *      THIS IS UNPUBLISHED PROPRIETARY SOURCE
7*35138Smarc  *      CODE OF AT&T.
8*35138Smarc  *      The copyright notice above does not
9*35138Smarc  *      evidence any actual or intended
10*35138Smarc  *      publication of such source code.
11*35138Smarc 
12*35138Smarc  */
13*35138Smarc /*
14*35138Smarc  * Miscellaneous routine needed for standalone library for edit modes
15*35138Smarc  */
16*35138Smarc 
17*35138Smarc /*
18*35138Smarc  * copy string a to string b and return pointer to end of string b
19*35138Smarc  */
20*35138Smarc 
21*35138Smarc #include	<stdio.h>
22*35138Smarc #ifdef BSD
23*35138Smarc #include	<sgtty.h>
24*35138Smarc #define DUPFLG	0100
25*35138Smarc #endif
26*35138Smarc #include	<setjmp.h>
27*35138Smarc #include	"history.h"
28*35138Smarc #include	"edit.h"
29*35138Smarc #undef read
30*35138Smarc 
31*35138Smarc #define badcreate	"cannot create"
32*35138Smarc 
33*35138Smarc extern char *strrchr();
34*35138Smarc extern char *getenv();
35*35138Smarc char opt_flag;
36*35138Smarc static unsigned char errbuf[BUFSIZ];
37*35138Smarc static int	editfd;
38*35138Smarc 
39*35138Smarc /*
40*35138Smarc  * read routine with edit modes
41*35138Smarc  */
42*35138Smarc 
read(fd,buff,n)43*35138Smarc int read(fd,buff,n)
44*35138Smarc char *buff;
45*35138Smarc {
46*35138Smarc 	register int r;
47*35138Smarc 	register int flag;
48*35138Smarc 	register char *sp;
49*35138Smarc 	static char beenhere;
50*35138Smarc 	if(fd==editfd && beenhere==0)
51*35138Smarc 	{
52*35138Smarc 		beenhere++;
53*35138Smarc 		hist_open();
54*35138Smarc 		sp  = getenv("VISUAL");
55*35138Smarc 		if(sp==NULL)
56*35138Smarc 			sp = getenv("EDITOR");
57*35138Smarc 		if(sp)
58*35138Smarc 		{
59*35138Smarc 			if(strrchr(sp,'/'))
60*35138Smarc 				sp = strrchr(sp,'/')+1;
61*35138Smarc 			if(strcmp(sp,"vi") == 0)
62*35138Smarc 				opt_flag = EDITVI;
63*35138Smarc 			else if(strcmp(sp,"emacs")==0)
64*35138Smarc 				opt_flag = EMACS;
65*35138Smarc 			else if(strcmp(sp,"gmacs")==0)
66*35138Smarc 				opt_flag = GMACS;
67*35138Smarc 		}
68*35138Smarc 	}
69*35138Smarc 	flag = (fd==editfd?opt_flag&EDITMASK:0);
70*35138Smarc 	if(flag && (unsigned char*)stderr->_base != errbuf)
71*35138Smarc 	{
72*35138Smarc 		fflush(stderr);
73*35138Smarc 		setbuf(stderr,errbuf);
74*35138Smarc 	}
75*35138Smarc 	switch(flag)
76*35138Smarc 	{
77*35138Smarc 		case EMACS:
78*35138Smarc 		case GMACS:
79*35138Smarc 			r = hread(fd,buff,n);
80*35138Smarc 			break;
81*35138Smarc 
82*35138Smarc 		case VIRAW:
83*35138Smarc 		case EDITVI:
84*35138Smarc 			r = vread(fd,buff,n);
85*35138Smarc 			break;
86*35138Smarc 		default:
87*35138Smarc 			if((unsigned char*)stderr->_base == errbuf)
88*35138Smarc 				fflush(stderr);
89*35138Smarc 			r = syscall(3,fd,buff,n);
90*35138Smarc 	}
91*35138Smarc 	if(fd==editfd && fc_fix && (opt_flag&NOHIST)==0 && r>0)
92*35138Smarc 	{
93*35138Smarc 		/* write and flush history */
94*35138Smarc 		int c = buff[r];
95*35138Smarc 		buff[r] = 0;
96*35138Smarc 		hist_eof();
97*35138Smarc 		fputs(buff,fc_fix->fixfd);
98*35138Smarc 		hist_flush();
99*35138Smarc 		buff[r] = c;
100*35138Smarc 	}
101*35138Smarc 	return(r);
102*35138Smarc }
103*35138Smarc 
104*35138Smarc 
105*35138Smarc /*
106*35138Smarc  * enable edit mode <mode> on file number <fd>
107*35138Smarc  * the NOHIST bit can also be set to avoid writing the history file
108*35138Smarc  * <fd> cannot be file two
109*35138Smarc  */
110*35138Smarc 
set_edit(fd,mode)111*35138Smarc int	set_edit(fd,mode)
112*35138Smarc {
113*35138Smarc 	opt_flag = mode;
114*35138Smarc 	if(editfd==2)
115*35138Smarc 		return(-1);
116*35138Smarc 	editfd = fd;
117*35138Smarc }
118*35138Smarc 
e_movstr(a,b)119*35138Smarc char *e_movstr(a,b)
120*35138Smarc register char *a,*b;
121*35138Smarc {
122*35138Smarc 	while(*b++ = *a++);
123*35138Smarc 	return(--b);
124*35138Smarc }
125*35138Smarc 
126*35138Smarc /*
127*35138Smarc  * print and error message and exit
128*35138Smarc  */
129*35138Smarc 
e_failed(name,message)130*35138Smarc e_failed(name,message)
131*35138Smarc char *name,*message;
132*35138Smarc {
133*35138Smarc 	fputs(name,stderr);
134*35138Smarc 	fputs(" : ",stderr);
135*35138Smarc 	fputs(message,stderr);
136*35138Smarc 	putc('\n',stderr);
137*35138Smarc 	exit(2);
138*35138Smarc }
139*35138Smarc 
140*35138Smarc 
141*35138Smarc /*
142*35138Smarc  * move the file number on stream fd to unit fb
143*35138Smarc  */
144*35138Smarc 
hist_rename(fd,fb)145*35138Smarc FILE *hist_rename(fd, fb)
146*35138Smarc register FILE *fd;
147*35138Smarc register int 	fb;
148*35138Smarc {
149*35138Smarc 	register int fa = fileno(fd);
150*35138Smarc #ifdef BSD
151*35138Smarc 	dup(fa|DUPFLG, fb);
152*35138Smarc 	ioctl(fb, FIOCLEX, 0);
153*35138Smarc #else	/*	TS lacks two-arg dup, ioctl	*/
154*35138Smarc 	if(fa >= 0)
155*35138Smarc 	{
156*35138Smarc 		close(fb);
157*35138Smarc 		fcntl(fa,0,fb); /* normal dup */
158*35138Smarc 		fcntl(fb,2,1);	/* autoclose for fb */
159*35138Smarc 	}
160*35138Smarc #endif	/* BSD */
161*35138Smarc 	fd->_file = fb;
162*35138Smarc 	return(fd);
163*35138Smarc }
164*35138Smarc 
165*35138Smarc /*
166*35138Smarc  * print a prompt
167*35138Smarc  */
pr_prompt(string)168*35138Smarc void pr_prompt(string)
169*35138Smarc register char *string;
170*35138Smarc {
171*35138Smarc 	register int c;
172*35138Smarc #ifdef BSD
173*35138Smarc 	int mode;
174*35138Smarc #include	<sys/ioctl.h>
175*35138Smarc 	mode = LFLUSHO;
176*35138Smarc 	ioctl(fileno(stderr),TIOCLBIC,&mode);
177*35138Smarc #endif	/* BSD */
178*35138Smarc 	fflush(stderr);
179*35138Smarc 	if((unsigned char*)stderr->_base != errbuf)
180*35138Smarc 		setbuf(stderr,errbuf);
181*35138Smarc 	while(c= *string++)
182*35138Smarc 		putc(c,stderr);
183*35138Smarc }
184*35138Smarc 
185*35138Smarc #ifdef BSD
186*35138Smarc /*
187*35138Smarc  *	tmpfile - return a pointer to an update file that can be
188*35138Smarc  *		used for scratch. The file will automatically
189*35138Smarc  *		go away if the program using it terminates.
190*35138Smarc  */
191*35138Smarc 
192*35138Smarc extern FILE *fopen();
193*35138Smarc extern int unlink();
194*35138Smarc extern char *tmpnam();
195*35138Smarc 
196*35138Smarc FILE *
tmpfile()197*35138Smarc tmpfile()
198*35138Smarc {
199*35138Smarc 	char	tfname[1024];
200*35138Smarc 	register FILE	*p;
201*35138Smarc 
202*35138Smarc 	tmpnam(tfname);
203*35138Smarc 	if((p = fopen(tfname, "w+")) == NULL)
204*35138Smarc 		return NULL;
205*35138Smarc 	else
206*35138Smarc 		unlink(tfname);
207*35138Smarc 	return(p);
208*35138Smarc }
209*35138Smarc #endif	/* BSD */
210*35138Smarc 
211