2
0

CoEd.C 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
  24. //%% (c) Copyright 1993, 1994 International Business Machines Corp.
  25. //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  26. //%% (c) Copyright 1993, 1994 Novell, Inc.
  27. //%% $XConsortium: CoEd.C /main/3 1995/10/20 17:05:50 rswiston $
  28. /*
  29. * CoEd.cc
  30. *
  31. * Copyright (c) 1991 by Sun Microsystems. All Rights Reserved.
  32. *
  33. * Permission to use, copy, modify, distribute, and sell this software
  34. * and its documentation for any purpose is hereby granted without
  35. * fee, provided that the above copyright notice appear in all copies
  36. * and that both that copyright notice and this permission notice
  37. * appear in supporting documentation, and that the names of Sun
  38. * Microsystems and its subsidiaries not be used in advertising or
  39. * publicity pertaining to distribution of the software without
  40. * specific, written prior permission. Sun Microsystems and its
  41. * subsidiaries make no representations about the suitability of this
  42. * software for any purpose. It is provided "as is" without express
  43. * or implied warranty.
  44. *
  45. * Sun Microsystems and its subsidiaries disclaim all warranties with
  46. * regard to this software, including all implied warranties of
  47. * merchantability and fitness. In no event shall Sun Microsystems or
  48. * its subsidiaries be liable for any special, indirect or
  49. * consequential damages or any damages whatsoever resulting from loss
  50. * of use, data or profits, whether in an action of contract,
  51. * negligence or other tortious action, arising out of or in
  52. * connection with the use or performance of this software.
  53. */
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <sys/time.h>
  57. #include <desktop/tt_c.h>
  58. #define InLibCoEd
  59. #include "CoEd.h"
  60. #include "CoEdFile.h"
  61. #include "CoEdGlobals.h"
  62. //#include "../libticccm/eclipse.h"
  63. char *coEdProcID = 0;
  64. CoEdSiteID *coEdSiteID = 0;
  65. int coEdTtFd = -1;
  66. int coEdTtStackMark = 0;
  67. CoEdFileList *coEdFiles = 0;
  68. const char *
  69. coEdStatusMessage(
  70. CoEdStatus status
  71. )
  72. {
  73. switch (status) {
  74. case CoEdWarnTimeout:
  75. return "Timed out";
  76. case CoEdErrFile:
  77. return "Invalid file";
  78. case CoEdErrNoMem:
  79. return "Out of memory";
  80. case CoEdErrBadPointer:
  81. return "Invalid pointer";
  82. case CoEdErrXDR:
  83. return "XDR failed";
  84. case CoEdErrBadMsg:
  85. return "Invalid message";
  86. case CoEdErrFailure:
  87. return "System error";
  88. default:
  89. if ((status >= TT_OK) && (status < TT_ERR_APPFIRST)) {
  90. return tt_status_message( (Tt_status)status );
  91. } else {
  92. return "Invalid CoEdStatus";
  93. }
  94. }
  95. }
  96. CoEdStatus
  97. coEdInit( char* &returnProcID, int &pFd2Watch )
  98. {
  99. if (coEdProcID != 0) {
  100. return CoEdOK;
  101. }
  102. coEdFiles = new CoEdFileList;
  103. if (coEdFiles == 0) {
  104. return CoEdErrNoMem;
  105. }
  106. //
  107. // Initialize ToolTalk
  108. //
  109. coEdProcID = tt_open();
  110. Tt_status err = tt_ptr_error( coEdProcID );
  111. if (tt_is_err( err )) {
  112. fprintf( stderr, "libCoEd: tt_open(): %s\n",
  113. tt_status_message( err ));
  114. return (CoEdStatus)err;
  115. }
  116. returnProcID = strdup(coEdProcID);
  117. coEdSiteID = new CoEdSiteID( coEdProcID );
  118. if (coEdSiteID == 0) {
  119. return CoEdErrNoMem;
  120. }
  121. //
  122. // Get the file descriptor that ToolTalk will use to tell us
  123. // of new messages.
  124. //
  125. coEdTtFd = tt_fd();
  126. err = tt_int_error( coEdTtFd );
  127. if (tt_is_err( err )) {
  128. fprintf( stderr, "libCoEd: tt_fd(): %s\n",
  129. tt_status_message( err ));
  130. return (CoEdStatus)err;
  131. }
  132. if (pFd2Watch == 0) {
  133. return CoEdErrBadPointer;
  134. }
  135. pFd2Watch = coEdTtFd;
  136. //
  137. // Register a file-scoped pattern for the notices in the
  138. // CoEd protocol.
  139. //
  140. Tt_pattern pat = tt_pattern_create();
  141. err = tt_ptr_error( pat );
  142. if (tt_is_err( err )) {
  143. fprintf( stderr, "libCoEd: tt_pattern_create(): %s\n",
  144. tt_status_message( err ));
  145. return (CoEdStatus)err;
  146. }
  147. err = tt_pattern_op_add( pat, "Text_File_Changed" );
  148. if (tt_is_err( err )) {
  149. fprintf( stderr, "libCoEd: tt_pattern_op_add(): %s\n",
  150. tt_status_message( err ));
  151. return (CoEdStatus)err;
  152. }
  153. err = tt_pattern_op_add( pat, "Text_File_Poll_Version" );
  154. if (tt_is_err( err )) {
  155. fprintf( stderr, "libCoEd: tt_pattern_op_add(): %s\n",
  156. tt_status_message( err ));
  157. return (CoEdStatus)err;
  158. }
  159. err = tt_pattern_op_add( pat, "Text_File_Quiesce" );
  160. if (tt_is_err( err )) {
  161. fprintf( stderr, "libCoEd: tt_pattern_op_add(): %s\n",
  162. tt_status_message( err ));
  163. return (CoEdStatus)err;
  164. }
  165. err = tt_pattern_op_add( pat, "Text_File_Saved" );
  166. if (tt_is_err( err )) {
  167. fprintf( stderr, "libCoEd: tt_pattern_op_add(): %s\n",
  168. tt_status_message( err ));
  169. return (CoEdStatus)err;
  170. }
  171. err = tt_pattern_op_add( pat, "Text_File_Reverted" );
  172. if (tt_is_err( err )) {
  173. fprintf( stderr, "libCoEd: tt_pattern_op_add(): %s\n",
  174. tt_status_message( err ));
  175. return (CoEdStatus)err;
  176. }
  177. err = tt_pattern_op_add( pat, "Text_File_Renamed" );
  178. if (tt_is_err( err )) {
  179. fprintf( stderr, "libCoEd: tt_pattern_op_add(): %s\n",
  180. tt_status_message( err ));
  181. return (CoEdStatus)err;
  182. }
  183. err = tt_pattern_scope_add( pat, TT_FILE );
  184. if (tt_is_err( err )) {
  185. fprintf( stderr, "libCoEd: tt_pattern_scope_add(): %s\n",
  186. tt_status_message( err ));
  187. return (CoEdStatus)err;
  188. }
  189. err = tt_pattern_category_set( pat, TT_OBSERVE );
  190. if (tt_is_err( err )) {
  191. fprintf( stderr, "libCoEd: tt_pattern_category_set(): %s\n",
  192. tt_status_message( err ));
  193. return (CoEdStatus)err;
  194. }
  195. err = tt_pattern_register( pat );
  196. if (tt_is_err( err )) {
  197. fprintf( stderr, "libCoEd: tt_pattern_register(): %s\n",
  198. tt_status_message( err ));
  199. return (CoEdStatus)err;
  200. }
  201. //
  202. // Register a file-scoped pattern for the requests in the
  203. // CoEd protocol.
  204. //
  205. Tt_pattern pat2 = tt_pattern_create();
  206. err = tt_ptr_error( pat2 );
  207. if (tt_is_err( err )) {
  208. fprintf( stderr, "libCoEd: tt_pattern_create(): %s\n",
  209. tt_status_message( err ));
  210. return (CoEdStatus)err;
  211. }
  212. err = tt_pattern_op_add( pat2, "Text_File_Version_Vote" );
  213. if (tt_is_err( err )) {
  214. fprintf( stderr, "libCoEd: tt_pattern_op_add(): %s\n",
  215. tt_status_message( err ));
  216. return (CoEdStatus)err;
  217. }
  218. err = tt_pattern_scope_add( pat2, TT_FILE );
  219. if (tt_is_err( err )) {
  220. fprintf( stderr, "libCoEd: tt_pattern_scope_add(): %s\n",
  221. tt_status_message( err ));
  222. return (CoEdStatus)err;
  223. }
  224. err = tt_pattern_category_set( pat2, TT_HANDLE );
  225. if (tt_is_err( err )) {
  226. fprintf( stderr, "libCoEd: tt_pattern_category_set(): %s\n",
  227. tt_status_message( err ));
  228. return (CoEdStatus)err;
  229. }
  230. err = tt_pattern_register( pat2 );
  231. if (tt_is_err( err )) {
  232. fprintf( stderr, "libCoEd: tt_pattern_register(): %s\n",
  233. tt_status_message( err ));
  234. return (CoEdStatus)err;
  235. }
  236. return CoEdOK;
  237. }
  238. CoEdStatus
  239. coEdFini()
  240. {
  241. delete coEdFiles;
  242. tt_close();
  243. return CoEdOK;
  244. }
  245. CoEdStatus
  246. coEdHandleActiveFD( int )
  247. {
  248. Tt_status status;
  249. // TtDtProcIDEclipse eclipse( coEdProcID, &status );
  250. // if (status != TT_OK) {
  251. // return (CoEdStatus)status;
  252. // }
  253. Tt_message msg = tt_message_receive();
  254. if (msg == 0) {
  255. return CoEdOK;
  256. }
  257. status = tt_ptr_error( msg );
  258. if (tt_is_err( status )) {
  259. fprintf( stderr, "libCoEd: tt_message_receive(): %s\n",
  260. tt_status_message( status ));
  261. return (CoEdStatus)CoEdOK;
  262. }
  263. char *msgFile = tt_message_file( msg );
  264. status = tt_ptr_error( msgFile );
  265. if (tt_is_err( status )) {
  266. fprintf( stderr, "libCoEd: tt_message_file(): %s\n",
  267. tt_status_message( status ));
  268. return (CoEdStatus)CoEdOK;
  269. }
  270. if (msgFile == 0) {
  271. return CoEdOK;
  272. }
  273. CoEdStatus retVal = coEdFiles->handleMsg( msgFile, msg );
  274. tt_free( msgFile );
  275. return retVal;
  276. }