1 /* editor.c:
2  *
3  ****************************************************************
4  * Copyright (C) 2003 Colin Walters
5  *
6  * See the file "COPYING" for further information about
7  * the copyright and warranty status of this work.
8  */
9 
10 
11 #include "hackerlab/bugs/panic.h"
12 #include "hackerlab/os/stdarg.h"
13 #include "hackerlab/os/sys/types.h"
14 #include "hackerlab/os/unistd.h"
15 #include "hackerlab/os/sys/wait.h"
16 #include "hackerlab/os/signal.h"
17 #include "hackerlab/char/str.h"
18 #include "hackerlab/fs/file-names.h"
19 #include "hackerlab/arrays/ar.h"
20 #include "hackerlab/vu/safe.h"
21 #include "tla/libarch/editor.h"
22 
23 
24 
25 int
arch_run_editor(t_uchar * name)26 arch_run_editor (t_uchar * name)
27 {
28   int exit_status = 0;
29 #if 0
30   t_uchar * editors[] =  {"/usr/bin/nano",  "/usr/bin/nano-tiny",
31 			  "/usr/bin/emacs", "/usr/bin/vi", 0};
32 #endif
33   t_uchar * editor = 0;
34   t_uchar ** argv = 0;
35   int pid;
36   int x;
37 
38   editor = getenv ("EDITOR");
39 #if 0
40   if (!editor)
41     {
42       t_uchar ** editorp = 0;
43       for (editorp = editors; *editorp != NULL; editorp++)
44 	if (!safe_access (*editorp, X_OK)) {
45 	  editor = *editorp;
46 	  break;
47 	}
48     }
49 #endif
50   if (!editor)
51     {
52       safe_printfmt (2, "arch_run_editor: please set $EDITOR\n");
53       exit (2);
54     }
55 
56   *(t_uchar **)ar_push ((void **)&argv, 0, sizeof (t_uchar *)) = str_save (0, editor);
57   *(t_uchar **)ar_push ((void **)&argv, 0, sizeof (t_uchar *)) = str_save (0, name);
58   *(t_uchar **)ar_push ((void **)&argv, 0, sizeof (t_uchar *)) = 0;
59 
60   pid = fork ();
61   if (pid == -1)
62     panic ("unable to fork for editor");
63 
64   if (pid)
65     {
66       int status;
67       int wait_pid;
68 
69       wait_pid = waitpid (pid, &status, 0);
70       if (wait_pid < 0)
71 	{
72 	  panic_msg ("error waiting for editor subprocess");
73 	  kill (0, SIGKILL);
74 	  panic ("error waiting for editor subprocess");
75 	}
76       if (WIFSIGNALED (status))
77 	{
78 	  safe_printfmt (2, "\n");
79 	  safe_printfmt (2, "arch_run_editor: editor subprocess killed by signal %d\n", WTERMSIG (status));
80 	  safe_printfmt (2, "\n");
81 	  exit (2);
82 	  return -1;
83 	}
84       else if (!WIFEXITED (status))
85 	{
86 	  panic_msg ("waitpid returned for a non-exited editor process");
87 	  kill (0, SIGKILL);
88 	  panic ("waitpid returned for a non-exited editor process and kill failed");
89 	  return -1;
90 	}
91       else
92 	{
93 	  exit_status = WEXITSTATUS (status);
94 	}
95     }
96   else
97     {
98       execvp (editor, (char **)argv);
99       panic ("arch_run_editor: execvp for editor script returned to caller");
100       exit (2);
101     }
102 
103   for (x = 0; x < ar_size ((void *)argv, 0, sizeof (t_uchar *)); ++x)
104       lim_free (0, argv[x]);
105   ar_free ((void **)&argv, 0);
106 
107   return exit_status;
108 }
109 
110 
111 
112 
113 /* tag: Colin Walters Wed, 19 Nov 2003 22:26:51 -0500 (editor.c)
114  */
115