applauncher._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  * Copyright (C) 2003-2004 Martin Baehr
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: applauncher.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
19  */
20 class applauncher {
21 public:
22 
23 
24 
25 //before using this file, patch the paths for watchforchanges.vim and golden_ratio.vim
26 object newfileobj;
27 string content;
28 int i=1;
29 int j=1;
30 int count=0;
31 int set=0;
32 string dir;
33 string debugfile;
34 
35 void upload(object editor, string file, int last_mtime, object obj, object xslobj, function|void exit_callback, string|void olderrors)
36 {
37  int exit_status = editor->status();
38  object new_stat = file_stat(file);
39  int new_mtime;
40  string newcontent;
41  string oldcontent = obj->get_content(); //currently changing
42 
43  if((content!=oldcontent)&&(oldcontent!=("sTeam connection lost."||""))&&obj&&(i==1))
44  {
45  i=0;
46  send_message("File changed on server.\n");
47 //Not needed - Stdio.write_file(file, oldcontent||"", 0600);
48  last_mtime = new_stat->mtime;
49  }
50  if (!new_stat)
51  send_message(sprintf("%s is gone!", file));
52 
53  if (new_stat && new_stat->mtime > last_mtime)
54  {
55  new_mtime = new_stat->mtime;
56  newcontent = Stdio.read_file(file);
57  if (!stringp(newcontent))
58  send_message(sprintf("failed to read %s", file));
59  }
60 
61  if (stringp(newcontent) && newcontent != content && oldcontent!="sTeam connection lost.")
62  {
63  last_mtime=new_mtime;
64  content = newcontent; //update initial content to new after saving
65  mixed result=obj->set_content(newcontent);
66  string message=sprintf("File saved - upload: %O\n", result);
67  olderrors = UNDEFINED;
68  send_message(message);
69  count=0; //so that compile status can be rewritten for newfile
70  if (xslobj)
71  {
72  result=xslobj->load_xml_structure();
73  message=sprintf("%O: load xml struct: %O", xslobj, result);
74  send_message(message);
75  }
76  }
77  if(oldcontent=="sTeam connection lost.")
78  {
79  if(j==1){
80  send_message("Disconnected\n");
81  j--;
82  }
83  if(newfileobj)
84  {
85  send_message("Connected back\n");
86  obj = newfileobj;
87  }
88  }
89 
90  if (exit_status != 2)
91  {
92  if(obj->get_class()=="DocLpc") //if pike script .
93  {
94  array errors = obj->get_errors();
95  string newerrors = sprintf("%O", errors);
96  if (newerrors != olderrors)
97  {
98  olderrors = newerrors;
99  send_message("-----------------------------------------\n");
100  if(errors==({}))
101  send_message("Compiled successfully\n");
102  else
103  {
104  foreach(errors, string err)
105  send_message(err);
106  send_message("Compilation failed\n");
107  }
108  send_message("-----------------------------------------\n");
109  }
110  }
111  call_out(upload, 1, editor, file, new_mtime, obj, xslobj, exit_callback, olderrors);
112  }
113  else if (exit_callback)
114  {
115  exit_callback(editor->wait());
116 // exit(1);
117  }
118 }
119 
120 
121 void update(object obj)
122 {
123  newfileobj = obj;
124 }
125 
126 array edit(object obj)
127 {
128 #if constant(Crypto.Random)
129  dir="/tmp/"+(MIME.encode_base64(Crypto.Random.random_string(10), 1)-("/"))+System.getpid();
130 #else
131  dir="/tmp/"+(MIME.encode_base64(Crypto.randomness.pike_random()->read(10), 1)-("/"))+System.getpid();
132 #endif
133  string filename=obj->get_object_id()+"-"+obj->get_identifier();
134 
135  debugfile = filename+"-disp";
136  mkdir(dir, 0700);
137  content=obj->get_content(); //made content global, this is content when vim starts and remains same. oldcontent keeps changing in upload function.
138  //werror("%O\n", content);
139  Stdio.write_file(dir+"/"+filename, content||"", 0600);
140 
141  Stdio.write_file(dir+"/"+debugfile, "This is your log window\n", 0600);
142  array command;
143  //array command=({ "screen", "-X", "screen", "vi", dir+"/"+filename });
144  //array command=({ "vim", "--servername", "VIM", "--remote-wait", dir+"/"+filename });
145  string enveditor = getenv("EDITOR");
146  string name = dir+"/"+debugfile;
147  if((enveditor=="VIM")||(enveditor=="vim")) //full path to .vim files to be mentioned
148  command=({ "vim","-S", "/usr/local/lib/steam/tools/watchforchanges.vim", "-S", "/usr/local/lib/steam/tools/golden_ratio.vim", dir+"/"+filename, "-c","set splitbelow", "-c" ,sprintf("split|view %s",name), "-c", "wincmd w"});
149  else if(enveditor=="emacs")
150  command=({ "emacs", "--eval","(add-hook 'emacs-startup-hook 'toggle-window-spt)", "--eval", "(global-auto-revert-mode t)", dir+"/"+filename, dir+"/"+debugfile, "--eval", "(setq buffer-read-only t)", "--eval", sprintf("(setq frame-title-format \"%s\")",obj->get_identifier()) , "--eval", "(windmove-up)", "--eval", "(enlarge-window 5)"});
151  else
152  command=({ "vi","-S", "/usr/local/lib/steam/tools/watchforchanges.vim", "-S", "/usr/local/lib/steam/tools/golden_ratio.vim", dir+"/"+filename, "-c","set splitbelow", "-c" ,sprintf("split|view %s",name), "-c", "wincmd w"});
153 
154  object editor=Process.create_process(command,
155  ([ "cwd":getenv("HOME"), "env":getenv(), "stdin":Stdio.stdin, "stdout":Stdio.stdout, "stderr":Stdio.stderr ]));
156  return ({ editor, dir+"/"+filename });
157 }
158 
159 int send_message(string message)
160 {
161 /*
162  if (getenv("STY"))
163  Process.create_process(({ "screen", "-X", "wall", message }));
164  else if (getenv("TMUX"))
165  Process.create_process(({ "tmux", "display-message", message }));
166  else
167  werror("%s\n", message);
168 */
169  Stdio.append_file(dir+"/"+debugfile, message||"", 0600); //result buffer
170 }
171 
172 int applaunch(object obj, function exit_callback)
173 {
174  object xslobj;
175  if(obj->get_identifier()[sizeof(obj->get_identifier())-8..]==".xsl.xml")
176  {
177  string xslname=
178  obj->get_identifier()[..sizeof(obj->get_identifier())-9]+ ".xsl";
179  xslobj=obj->get_environment()->get_object_byname(xslname);
180  }
181 
182  object editor;
183  string file;
184  [editor, file]=edit(obj);
185  mixed status;
186  //while(!(status=editor->status()))
187 
188  send_message(sprintf("(opened %O %s)\n", obj, file));
189  call_out(upload, 1, editor, file, file_stat(file)->mtime, obj, xslobj, exit_callback);
190 
191 // signal(signum("SIGINT"), prompt);
192  return -1;
193 }
194 
195 
196 };