gnuintfc.red 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. module gnuintfc; % REDUCE-gnuplot interface.
  2. % Authors: Anthony C. Hearn, Herbert Melenk, Arthur C. Norman.
  3. % The file complements the (notionally) generic file "gnuplot.red" to
  4. % provide system-specific interfaces between REDUCE and the gnuplot
  5. % package itself.
  6. fluid '(plotstyle!*);
  7. global '(!*plotinterrupts !*plotpause !*plotusepipe plotheader!*
  8. plotcleanup!* plottmp!*);
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%% PSL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. !#if (member 'psl lispsystem!*)
  11. !#if (member 'unix lispsystem!*)
  12. !*plotusepipe:=t; % pipes: yes
  13. load pipes;
  14. !*plotpause:=nil; % pause: no
  15. plottmp!* := "/tmp/";
  16. if getenv "LOGNAME" then
  17. plottmp!* := bldmsg("%w%w.",plottmp!*,getenv "LOGNAME")
  18. else if getenv "USER" then
  19. plottmp!* := bldmsg("%w%w.",plottmp!*,getenv "USER");
  20. plotdta!* := for i:=1:10 collect
  21. bldmsg("%wplotdta%w*",plottmp!*,i); % scratch data files
  22. plotcmds!* :=bldmsg("%wplotcmds*",plottmp!*); % if pipes not accessible
  23. % select header lines for setting the appropriate GNUPLOT
  24. % terminal type.
  25. if null plotheader!* then
  26. << if null x then
  27. if getenv "DISPLAY" then x := '(nil . "x11")
  28. else x:='(nil."dumb");
  29. plotheader!* := bldmsg("set term %w",cdr x);
  30. >>
  31. where x =
  32. assoc(getenv "TERM",
  33. '(
  34. %% You may want to extend or modify the terminal list above
  35. ("xterm" . "x11")
  36. ("sun-cmd" . "x11") ("sun" . "x11")
  37. ("hpterm" . "x11")
  38. ("vt52" . "tek40xx")
  39. ("vt100" . "tek40xx")
  40. ("vt102" . "tek40xx")
  41. ));
  42. % add $reduce/plot to the path when using X11 (gnuplot will load a child).
  43. plotcommand!*:=
  44. begin scalar p;
  45. if not(p:=getenv "gnuplot") then
  46. p:=bldmsg("%w/plot",getenv "reduce");
  47. return bldmsg("PATH=$PATH:%w;export PATH;gnuplot",p);
  48. end;
  49. plotcleanup!* := % delete scratch files
  50. {"rm /tmp/plotdt*","rm /tmp/plotcmds!*"};
  51. !*plotinterrupts := '(10002);
  52. !#elif (intersection '(dos os2 win32 winnt alphant) lispsystem!*)
  53. fluid '(!*!*windows);
  54. % the following system selection must be done at load time
  55. plottmp!* := bldmsg("%w\",getenv "tmp" or getenv "temp");
  56. if null plottmp!* then
  57. <<prin2t "warning: no TMP directory found. Using >C:<";
  58. plottmp!*:="c:";
  59. >>;
  60. !*plotpause:=nil; % no pause
  61. plotdta!* := for i:=1:10 collect
  62. bldmsg("%wplotdt%w.dat",plottmp!*,i); % scratch data files
  63. if member ('win95,lispsystem!*) then
  64. <<
  65. % for Win95 until we have something better
  66. % pass commands as parameter file
  67. % write command file and data to directory /tmp
  68. !*plotusepipe:=nil; % no pipes
  69. plotcmds!* := bldmsg("%w\plotcmds",plottmp!*);
  70. plotcommand!* :=
  71. % fnexpand bldmsg("$reduce\plot\wgnuplot.exe %w",plotcmds!*);
  72. fnexpand bldmsg("$reduce\wutil\win32\wgnuplot.exe %w",plotcmds!*);
  73. plotheader!* := "";
  74. plotcleanup!* := NIL; % delete scratch files
  75. !*plotpause := -1;
  76. copyd('plot!-exec,'system);
  77. flag('(plot!-exec),'lose);
  78. >> else
  79. if !*!*windows eq 1 or member('winnt,lispsystem!*)
  80. or member('win32 ,lispsystem!*) then
  81. <<
  82. % for windows:
  83. % use plot pipe
  84. load w!-pipes;
  85. !*plotusepipe:=t;
  86. plotcmds!* := bldmsg("%w\plotcmds",plottmp!*);;
  87. plotcommand!* := bldmsg(
  88. % "%w\plot\wgnupl32.exe;wgnuplot_parent;wgnuplot_text",
  89. % "%w\plot\wgnuplot.exe;wgnuplot_parent;wgnuplot_text",
  90. if member('dos,lispsystem!*) then
  91. "%w\wutil\dos386\wgnuplot.exe;wgnuplot_parent;wgnuplot_text"
  92. else if member('alphant,lispsystem!*) then
  93. "%w\wutil\alphant\wgnuplot.exe;wgnuplot_parent;wgnuplot_text"
  94. else
  95. "%w\wutil\win32\wgnuplot.exe;wgnuplot_parent;wgnuplot_text",
  96. getenv("reduce"));
  97. plotheader!* := "";
  98. plotcleanup!* := % delete scratch files
  99. bldmsg("del %w",plotcmds!*) .
  100. for each f in plotdta!* collect bldmsg("del %w",f);
  101. >>
  102. else if member('os2,lispsystem!*) then
  103. <<
  104. !*plotusepipe:=nil; % no pipes
  105. plotcmds!* := bldmsg("%w\plotcmds",plottmp!*);
  106. plotcommand!* := "";
  107. plotheader!* := "";
  108. symbolic procedure plot!-exec u;
  109. <<
  110. prin2 "====> invoke GNUPLOT for file ";
  111. prin2t plotcmds!*;
  112. >>;
  113. loadtime flag('(plot!-exec),'lose);
  114. >>
  115. else
  116. <<
  117. % for dos:
  118. % pass commands as parameter file
  119. % write command file and data to directory /tmp
  120. !*plotusepipe:=nil; % no pipes
  121. plotcmds!* := bldmsg("%w\plotcmds",plottmp!*);
  122. plotcommand!* :=
  123. fnexpand bldmsg("$reduce\wutil\dos386\gnuplot.exe %w",plotcmds!*);
  124. plotheader!* := "set term vga";
  125. plotcleanup!* := % delete scratch files
  126. bldmsg("del %w",plotcmds!*).
  127. for each f in plotdta!* collect bldmsg("del %w",f);
  128. >>;
  129. plotmax!* := 3e36; % IEEE single precision
  130. !*plotinterrupts := '(10002);
  131. !#elif (member 'vms lispsystem!*)
  132. !*plotusepipe:=NIL; % pipes: yes
  133. !*plotpause:=nil; % pause: no
  134. plottmp!* := "SYS$SCRATCH";
  135. plotdta!* := for i:=1:10 collect
  136. bldmsg("%w:plotdt%w",plottmp!*,i); % scratch data files
  137. plotcmds!* :=bldmsg("%w:plotcmds",plottmp!*); % if pipes not accessible
  138. plotcommand!* := bldmsg("gnuplot %w",plotcmds!*);
  139. plotheader!* := "set term x11"; % header: set terminal to X11
  140. plotcleanup!* := % delete scratch files
  141. {"del SYS$SCRATCH:plotdt*;*","del SYS$SCRATCH:plotcmds*;*"};
  142. !*plotinterrupts := '(10002);
  143. !#endif
  144. %%%%%%%%%%%%%%%%%%%%%%%%%%% CSL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  145. !#elif (member 'csl lispsystem!*)
  146. %
  147. % If the user has set environment variables calles "tmp" or "temp" then
  148. % that specify (in that priority order) directories to place temporary data
  149. % in. If neither is set then the current directory will be used. If tmp or temp
  150. % is set but does not name a directory then everything probably fails.
  151. %
  152. %
  153. % In this code opsys!* comes from "assoc('opsys, lispsystem!*)" with
  154. % 'unknown as its default value.
  155. % This means that with CSL 6.0 its value is usually HOST_OS as determined by
  156. % the GNU autoconf utilities. However for some systems I explicitly override
  157. % that and eg on Windows it should be win32. In circumstances that should no
  158. % longer ever arise it could become dos, finder, riscos or unix. These cases
  159. % are no longer supported! In fact the only fuss here will be that for win32
  160. % special action is taken - all other cases go a generic route that was once
  161. % labelled as "unix".
  162. %
  163. %
  164. % If a shell variable "gnuplot" is set then thet must be the directory in
  165. % which the gnuplot binary will be found. On Windows at least a search will
  166. % be made for "wgnuplot" in the directory that REDUCE was launched from, but
  167. % on Unix-like systems it may be necessary either to set the gnuplot shell
  168. % variable or to place the gnuplot binary on your PATH.
  169. %
  170. global '(plotdir!* dirchar!* opsys!* tempdir!*);
  171. symbolic procedure channelflush x;
  172. << x := wrs x; flush(); wrs x >>;
  173. symbolic procedure gtmpnam base;
  174. if null tempdir!* then base
  175. else compress ('!" . append(explodec tempdir!*,
  176. car explodec dirchar!* . cdr explode base));
  177. % In general I want the initialisation actions indicated here to be
  178. % obeyed at load-time. I achieve this by packaging them all inside
  179. % a function, which I then call.
  180. symbolic procedure init_gnuplot();
  181. <<
  182. !*plotpause := -1; % wait for newline from user
  183. % plotcleanup!* is a list of commands to be obeyed after a gnuplot run.
  184. % it is mainly needed to get rid of the data files (used even when pipes
  185. % are available).
  186. plotcleanup!* := {};
  187. tempdir!* := getenv 'tmp;
  188. if null tempdir!* then tempdir!* := getenv 'temp;
  189. dirchar!* := "/";
  190. plotcommand!* := "gnuplot";
  191. opsys!* := assoc('opsys, lispsystem!*);
  192. if null opsys!* then opsys!* := 'unknown
  193. else opsys!* := cdr opsys!*;
  194. % I USED to have special case code for Acorn RISCOS, for DOS, for Win32 and
  195. % "unix", and moaned if none of those where detected. These days it seems
  196. % more sense to make win32 a special case but otherwise assume a "unix-like"
  197. % environment. This will cause pain if you run under Unix but without an
  198. % X session, but in that case maybe the diagnostic about the inability to
  199. % open a window can come from gnuplot rather than me!
  200. if opsys!* neq 'win32 then opsys!* := 'unix;
  201. % If a shell variable "gnuplot" is set it is expected to be the
  202. % directory within which gnuplot binaries can be found.
  203. if getenv "gnuplot" then plotdir!* := getenv "gnuplot"
  204. % Otherwise, and this is what I hope and expect to happen more often, I
  205. % will expect to find gnuplot in the same directory that REDUCE was
  206. % loaded from. This will all work wonderfully under Windows, but under Unix
  207. % there is a much greater chance that I will need to set an environment
  208. % variable "gnuplot". However, I still want to leave open the possibility
  209. % of plotdir!* being set.
  210. else if null plotdir!* and not (opsys!* = 'unix)
  211. then plotdir!* := get!-lisp!-directory();
  212. if opsys!* = 'win32 then <<
  213. % For Microsoft Windows use I try to use "wgnuplot" rather than "gnuplot",
  214. % and the options provided are copied from the PSL case above.
  215. plotcommand!* := "wgnuplot";
  216. plotheader!* := "";
  217. dirchar!* := "\";
  218. plotdta!* := for each n in
  219. {"gnutmp.tm1", "gnutmp.tm2", "gnutmp.tm3", "gnutmp.tm4",
  220. "gnutmp.tm5", "gnutmp.tm6", "gnutmp.tm7", "gnutmp.tm8"}
  221. collect gtmpnam n;
  222. plotcleanup!* := if null tempdir!* then {"erase gnutmp.tm*"}
  223. else {bldmsg("erase %w\gnutmp.tm*", tempdir!*)} >>
  224. else << % Assume Unix with X11.
  225. plotheader!* := "set term x11";
  226. plotdta!* := for i:=1:10 collect tmpnam();
  227. plotcmds!*:= tmpnam();
  228. plotcleanup!* :=
  229. bldmsg("rm %w", plotcmds!*) .
  230. for each f in plotdta!* collect bldmsg("rm %w", f) >>;
  231. % If there are no pipes available gnuplot will need a command-line
  232. % argument indicating where its input file is.
  233. if 'pipes member lispsystem!* then !*plotusepipe:=t
  234. else plotcommand!* := bldmsg("%w %w", plotcommand!*, plotcmds!*);
  235. % If I have a directory to look for gnuplot in I either just give a
  236. % full path or (under Unix) I temporarily switch current directories
  237. % to there.
  238. if plotdir!* then
  239. plotcommand!* := bldmsg("%w%w%w",
  240. plotdir!*, dirchar!*, plotcommand!*);
  241. nil >>;
  242. init_gnuplot();
  243. !#endif
  244. endmodule;
  245. end;