+ Fixes
[opsoft.git] / silentbob / plugins / plugin_editor.cxx
blob8627d32268b7d8924d46403f29d58752b171093b
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <gclib/gclib.h>
12 #include <sys/wait.h>
13 #include <mod.h>
14 #include <head.h>
15 #include <dbg.h>
17 #define TMP_FILE "/tmp/.silent_bob.tmp.cpp"
18 extern "C" DArray * plugin_init (struct env_t *env);
19 struct env_t *ENV;
20 char * editor;
22 void try_editor ()
24 int pid;
25 int fd;
26 int status;
27 char m_buf[1024];
29 pid = fork ();
30 if (pid < 0)
31 return;
33 if (pid == 0) {
34 unlink (TMP_FILE);
35 fd = open (TMP_FILE, O_WRONLY | O_CREAT);
36 fchmod (fd, S_IREAD | S_IWRITE);
37 if (fd == -1) {
38 perror ("open");
39 exit (1);
41 dup2 (fd, fileno (stdout));
42 return;
43 } else {
44 if ((waitpid (pid, &status, 0) < 0) ||
45 status != 0)
46 exit (status);
48 sprintf (m_buf, "%s %s", editor, TMP_FILE);
49 system (m_buf);
50 exit (0);
54 char editor_opt (DArray * d_opts, int * i)
56 char * S;
57 if (! d_opts || ! i)
58 return 0;
60 if (editor != NULL)
61 return 1;
63 S = d_opts->get (*i);
64 if (EQ(S, "-g") || EQ(S, "--gvim"))
65 editor = (char *) "gvim";
67 if (EQ(S, "--emacs"))
68 editor = (char *) "emacs";
70 if (EQ(S, "--nano"))
71 editor = (char *) "nano";
73 if (editor != NULL) {
74 try_editor ();
75 return 1;
78 return 0;
81 void short_info ()
83 printf ("Editors.");
86 void long_info ()
88 printf ("Editors.\n");
89 printf ("Version: 1.0\n");
90 printf ("Few editors for SilentBob:\n"
91 "\t-g --gvim\t-\tGVim\n"
92 "\t--emacs\t\t-\tEmacs\n"
93 "\t--nano\t\t-\tnano\n");
96 DArray * plugin_init (struct env_t *env)
98 DArray * Ret;
99 mod_t * plug;
101 plug = CNEW (mod_t, 1);
102 memset (plug, 0, sizeof (mod_t));
103 plug->Version = strdup ("1.0");
104 plug->opt = editor_opt;
105 plug->Name = strdup ("Editors");
106 plug->short_info = short_info;
107 plug->long_info = long_info;
109 ENV->listOptions->add ( "-g");
110 ENV->listOptions->add ( "--gvim");
111 ENV->listOptions->add ( "--emacs");
112 ENV->listOptions->add ( "--nano");
114 editor = NULL;
115 Ret = new DArray (1);
116 Ret->add (LPCHAR (plug));
117 return Ret;