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