1 /*--------------------------------------------------------------------
2 	nxeditor
3 			FILE NAME:nxedit.c
4 			Programed by : I.Neva
5 			R & D  ADVANCED SYSTEMS. IMAGING PRODUCTS.
6 			1992.06.01
7 
8     Copyright (c) 1998,1999,2000 SASAKI Shunsuke.
9     All rights reserved.
10 --------------------------------------------------------------------*/
11 #define	VAL_impl
12 
13 #include "ed.h"
14 #include "sh.h"
15 #include <ctype.h>
16 
17 #include	<errno.h>
18 #include	<sys/types.h>
19 
20 #ifdef	HAVE_SYS_UTSNAME_H
21 #	include	<sys/utsname.h>
22 #endif
23 
24 
opening()25 static	void	opening()
26 {
27 	char *title1 = "        Multi FILE Editor      ";
28 	char *title2 = "               Ne              ";
29 	char *copyright = "Copyright (c) 1991, 1992 I.Nebashi";
30 
31 /* Emacs Ū������ɽ������롣 */
32 
33 //	term_cls();
34 //	term_color_normal();
35 
36 //	term_flush();
37 
38 //	term_puts("ne " VER);
39 //	term_inkey();
40 }
41 
42 
43 /*-------------------------------------------------------------------
44 	Main Command Loop
45 -------------------------------------------------------------------*/
46 
ne_loop(int region)47 void	ne_loop(int region)
48 {
49  	int 	key;
50 
51 	for (;;)
52 		{
53 		 if (region==0)
54 		 	CrtDrawAll();
55 
56 		 dsp_allview();
57 		 term_csrn();
58 		 key=get_keyf(region);
59 		 RefreshMessage();
60 
61 		 if (region==0&& csrse.gf)
62 		 	{
63 		 	 csrse.gf=FALSE;
64 		 	 term_locate(GetRow(), GetCol()+NumWidth);
65 		 	 crt_crmark();
66 		 	}
67 
68 		 if (key==-1)
69 		 	continue;
70 
71 		 if ((key&KF_normalcode)==0)
72 		 	funclist[0][key](); else
73 		 	{
74 		 	 if (region==0)
75 		 	 	InputAndCrt(key& ~KF_normalcode);
76 		 	}
77 		}
78 }
79 
ne_fin()80 void	ne_fin()
81 {
82 	exit(0);
83 }
84 
85 
86 
sysinfo_path(char * s,const char * t)87 void	sysinfo_path(char *s,const char *t)
88 {
89 	sprintf(s, "%s/%s", sysinfo.nxpath, t);
90 }
91 
ne_init()92 void	ne_init()
93 {
94 	char	*p;
95 
96 	sysinfo.vp_def=hash_init(NULL, MAX_val);
97 	opt_default();
98 
99 	p=getenv("HOME");
100 	if (p!=NULL)
101 		strcpy(sysinfo.nxpath, p); else
102 		getcwd(sysinfo.nxpath, LN_path);
103 	strcat(sysinfo.nxpath, "/.ne");
104 	mkdir(sysinfo.nxpath, 0777); // !!
105 
106 	sysinfo.shell=getenv("SHELL");
107 	if (sysinfo.shell==NULL)
108 		sysinfo.shell="/bin/sh";
109 
110 	config_read("nerc");
111 }
112 
ne_report()113 void	ne_report()
114 {
115 #ifndef	HAVE_UNAME
116 	report_puts("ne version " VER "\n");
117 
118 #else
119 	struct utsname	u;
120 
121 	uname(&u);
122 	report_printf("ne version " VER "  %s %s (%s)\n"
123 		, u.sysname, u.release, u.version);
124 
125 #endif
126 
127 	report_puts("***\n");
128 	report_puts("�Ķ��ѿ�:\n");
129 	report_printf("  $HOME: %s\n", getenv("HOME"));
130 	report_printf("  $TERM: %s\n", getenv("TERM"));
131 	report_printf("  $NE_TERM: %s\n", getenv("NE_TERM"));
132 	report_printf("  $SHELL: %s\n", getenv("SHELL"));
133 	report_puts("\n");
134 
135 	hash_report(sysinfo.vp_def);
136 	report_puts("\n");
137 
138 	term_report();
139 	term_keyreport();
140 	report_puts("***\n");
141 }
142 
ne_arg(int argc,char * argv[])143 bool	ne_arg(int argc,char *argv[])
144 {
145 	int 	line;
146 	int 	optcount;
147 	bool	pf;
148 	char	buf[LN_dspbuf+1];
149 	bool	f;
150 	int 	c;
151 	char	*sp,*p;
152 
153 	line=0;
154 	f=FALSE;
155 	pf=FALSE;
156 	sysinfo.f_report=FALSE;
157 
158 	for (optcount=1; optcount<argc; ++optcount) // ????
159 		{
160 		 c=getopt(argc, argv, "Vrjeod:D:");
161 		 if (c==EOF)
162 		 	break;
163 
164 		 switch(c)
165 		 	{
166 		  case 'V':
167 		  	 sysinfo.f_report=TRUE;
168 		  	 report_set(stderr);
169 		  	 break;
170 		  case 'j':
171 		  	 hash_set(sysinfo.vp_def, "japanese","true");
172 		  	 break;
173 		  case 'e':
174 		  	 hash_set(sysinfo.vp_def, "japanese","false");
175 		  	 break;
176 		  case 'r':
177 		  	 pf=TRUE;
178 		  	 break;
179 		  case 'o':
180 		  	 opening();
181 		  	 break;
182 		  case 'D':
183 		  	 strcpy(buf,optarg);
184 		  	 optcount++;
185 		  	 sp=buf;
186 		  	 p=strsep(&sp,"=");
187 		  	 hash_set(sysinfo.vp_def, buf, p);
188 		 	}
189 		}
190 
191 	sysinfo_optset();
192 
193 	if (sysinfo.f_report)
194 		return;
195 
196 	if (pf)
197 		return profile_read();
198 
199 	for (; optcount<argc; ++optcount)
200 		{
201 		 if (*argv[optcount]=='+')
202 		 	line=atoi(argv[optcount]+1); else
203 		 	{
204 		 	 if (FileOpenOp(argv[optcount]))
205 		 	 	f=TRUE;
206 		 	}
207 		}
208 
209 	if (!f&& line>0)
210 		{
211 		 int	a;
212 
213 		 a=min(line,GetRowWidth()/2+1);
214 
215 		 csr_setly(line-a+1);
216 		 csr_setdy(a);
217 		}
218 
219 	if (!f)
220 		FileOpenOp("untitled.txt");
221 
222 	return f;
223 }
224 
main(int argc,char * argv[])225 int 	main(int argc,char *argv[])
226 {
227 	term_init();
228 	term_start();
229 	term_cls();
230 	dsp_allinit();
231 	SetSignal();
232 
233 	lists_init();
234 	edbuf_init();
235 
236 	keydef_init();
237 	ne_init();
238 	ne_arg(argc, argv);
239 	key_set();
240 	sysinfo_optset();
241 
242 	if (sysinfo.f_report)
243 		{
244 		 ne_report();
245 		 exit(0);
246 		}
247 
248 	udbuf_init();
249 	bstack_init();
250 	search_init();
251 	eff_init(NULL,NULL);
252 	system_guide_init();
253 	*sysinfo.doublekey='\0'; // !!
254 
255 	ne_loop(0);
256 }
257 
258 
259 
260 /*-------------------------------------------------------------------
261 	Escape Shell
262 -------------------------------------------------------------------*/
CommandCom(char * sys_buff)263 void	CommandCom(char *sys_buff)
264 {
265 	bool	f;
266 
267 	term_stop();
268 
269 	if (*sys_buff == '\0')
270 		{
271 		 puts(TYPE_EXIT_MSG);
272 		 system(sysinfo.shell);
273 		 f=FALSE;
274 		} else
275 		{
276 		 puts(sys_buff);
277 		 system(sys_buff);
278 		 f=TRUE;
279 		}
280 
281 	term_start();
282 
283 	if (f)
284 		{
285 		 fputs(HIT_ANY_KEY_MSG, stdout);
286 		 fflush(stdout);
287 		 term_kflush();
288 		 term_inkey();
289 		}
290 
291 	term_cls();
292 }
293 
op_misc_exec()294 void	op_misc_exec()
295 {
296 	char	buf[MAXEDITLINE+1];
297 
298 	*buf='\0';
299 	if (HisGets(buf, GETS_SHELL_MSG, SHELLS_SYSTEM) != NULL)
300 		CommandCom(buf);
301 }
302 
303 
op_opt_linenum()304 void	op_opt_linenum()
305 {
306 	opt_set("number", NULL);
307 
308 //	OffsetSetByColumn();
309 }
310 
op_misc_redraw()311 void	op_misc_redraw()
312 {
313 	term_cls();
314 }
315 
316 
317 
318 /*-----------------------------------------------------------------------------
319 	fork shell and take its stdout/stderr.
320 */
op_misc_insert_output(void)321 void	op_misc_insert_output( void )
322 {
323 	pid_t	pid_child ;
324 	int		pipefds[2] ;
325 	char	buf[MAXEDITLINE+1]="";
326 
327 
328 	if (HisGets(buf, GETS_SHELL_MSG, SHELLS_SYSTEM) == NULL)
329 		return ;
330 		/* NOTREACHED */
331 
332 	/*	Normally, this may not be failed, but... */
333 	if (pipe(pipefds)!=0)
334 		{
335 		 system_msg(strerror(errno));
336 		 term_inkey();
337 		 return;
338 		/* NOTREACHED */
339 		}
340 
341 	pid_child=fork();
342 	switch(pid_child)
343 		{
344 		/*	now we are child. */
345 		case	0 :
346 			/*	pipe plumbing.
347 				child process's stdout and stderr are dup'ed from same fd.
348 				whatever written into these will end up to our pipefds[ 0 ].
349 			*/
350 //			term_stop();
351 
352 			close( 1 ) ;
353 			dup( pipefds[ 1 ] ) ;	/* duplicate to stdout */
354 
355 			close( 2 ) ;
356 			dup( pipefds[ 1 ] ) ;	/* duplicate to stderr */
357 
358 			/*	these fds are no longer useful. */
359 			close( pipefds[ 0 ] ) ;
360 			close( pipefds[ 1 ] ) ;
361 
362 			execl(sysinfo.shell, sysinfo.shell, "-c", buf, NULL) ;	/* should not be failed. */
363 			_exit( 1 ) ;
364 			/* NOTREACHED */
365 
366 		/*	cannot be forked off. */
367 		case	-1 :
368 			system_msg(strerror(errno));
369 			term_inkey();
370 			system_msg("");
371 
372 			close(pipefds[0]);
373 			close(pipefds[1]);
374 
375 			break;
376 			/* NOTREACHED */
377 
378 	/*	now we are parent. */
379 		default :
380 			{
381 			int	status ;
382 			FILE	*fp_pipe;
383 
384 			close(pipefds[1]);
385 			fp_pipe = fdopen(pipefds[ 0 ], "r");
386 			if (fp_pipe==NULL)
387 				{
388 				 system_msg(strerror(errno));
389 				 term_inkey();
390 				 system_msg("");
391 				} else
392 				{
393 				 EditLine	*ed, *ed_new;
394 				 bool	f;
395 				 int 	n;
396 
397 				 csr_leupdate();
398 				 ed_new=ed=GetList(GetLineOffset()-1);
399 				 f=FALSE;
400 
401 				 term_stop();
402 
403 				 while ( fgets(buf, sizeof(buf) - 1, fp_pipe) )
404 				 	{
405 				 	/* echo to stdout for user friendliness. :) */
406 				 	fputs(buf, stdout);
407 
408 				 	/* remove last '\n' */
409 				 	n=strlen(buf)-1;
410 				 	if (buf[n] == '\n')
411 				 		buf[n] = '\0' ;
412 
413 				 	/*	create new linebuffer and insert into list. */
414 				 	InsertLine(ed, MakeLine(buf));
415 				 	ed=ed->next;
416 				 	f=TRUE;
417 				 	}
418 				 fclose( fp_pipe ) ;
419 				 close(pipefds[0]);
420 				 term_start();
421 
422 				 if (f)
423 				 	{
424 				 	 SetFileChangeFlag( ) ;	/* now, file is dirty. */
425 				 	 csrse.ed=ed_new->next;
426 				 	 csr_lenew();
427 				 	 OffsetSetByColumn();
428 				 	}
429 				}
430 			wait( &status ) ;
431 
432 			term_cls();
433 
434 			}
435 		}
436 }
437 
438