1 /*----------------------------------------------------------------------------
2 --
3 --  Module:           xtmLocate
4 --
5 --  Project:          XDiary
6 --  System:           xtm - X Desktop Calendar
7 --    Subsystem:      <>
8 --    Function block: <>
9 --
10 --  Description:
11 --    Locate a calendar.
12 --
13 --  Filename:         xtmLocate.c
14 --
15 --  Authors:          Roger Larsson, Ulrika Bornetun
16 --  Creation date:    1992-10-27
17 --
18 --
19 --  (C) Copyright Ulrika Bornetun, Roger Larsson (1995)
20 --      All rights reserved
21 --
22 --  Permission to use, copy, modify, and distribute this software and its
23 --  documentation for any purpose and without fee is hereby granted,
24 --  provided that the above copyright notice appear in all copies. Ulrika
25 --  Bornetun and Roger Larsson make no representations about the usability
26 --  of this software for any purpose. It is provided "as is" without express
27 --  or implied warranty.
28 ----------------------------------------------------------------------------*/
29 
30 /* SCCS module identifier. */
31 static char SCCSID[] = "@(#) Module: xtmLocate.c, Version: 1.1, Date: 95/02/18 15:52:26";
32 
33 
34 /*----------------------------------------------------------------------------
35 --  Include files
36 ----------------------------------------------------------------------------*/
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include <X11/Intrinsic.h>
44 #include <X11/cursorfont.h>
45 
46 #include <Xm/Xm.h>
47 
48 #include "System.h"
49 #include "Message.h"
50 
51 #include "msgXdiary.h"
52 #include "xitError.h"
53 #include "xtmLocate.h"
54 
55 
56 /*----------------------------------------------------------------------------
57 --  Macro definitions
58 ----------------------------------------------------------------------------*/
59 
60 
61 /*----------------------------------------------------------------------------
62 --  Type declarations
63 ----------------------------------------------------------------------------*/
64 
65 
66 /*----------------------------------------------------------------------------
67 --  Global definitions
68 ----------------------------------------------------------------------------*/
69 
70 /* Name of module. */
71 static char  *module_name = "xtmLocate";
72 
73 
74 /*----------------------------------------------------------------------------
75 --  Function prototypes
76 ----------------------------------------------------------------------------*/
77 
78 
79 
80 /*----------------------------------------------------------------------------
81 --  Functions
82 ----------------------------------------------------------------------------*/
83 
84 Boolean
xtmLcLocateCalendar(Widget parent,char * calendar,char * locate_script,char * result,int max_result)85   xtmLcLocateCalendar( Widget  parent,
86                        char    *calendar,
87                        char    *locate_script,
88                        char    *result,
89                        int     max_result )
90 {
91 
92   /* Variables. */
93   int      status;
94   char     buffer[ 500 ];
95   char     *char_ref;
96   Cursor   wait_cursor;
97   Display  *display = NULL;
98   Window   window = None;
99   FILE     *pipe_ref;
100 
101 
102   /* Code. */
103 
104   *result = '\0';
105 
106   if( parent != NULL ) {
107     display = XtDisplay( parent );
108     window  = XtWindow(  parent );
109   }
110 
111 
112   /* Does the executable file exist? */
113   status = access( locate_script, (R_OK | X_OK) );
114 
115   if( status != 0 ) {
116     xitErMessage( parent, XIT_ER_ERROR,
117                   module_name, "xtmLcLocateCalendar",
118                   msgGetText( MXDI_PROCESS_NO_FILE ) );
119     return( False );
120   }
121 
122 
123   /* Waiting cursor. */
124   if( parent != NULL ) {
125     wait_cursor = XCreateFontCursor( display, XC_watch );
126     XDefineCursor( display, window, wait_cursor );
127 
128     XFlush( display );
129   }
130 
131 
132   /* The search command. */
133   char_ref = (char *) SysMalloc( strlen( locate_script ) +
134                                  strlen( calendar ) + 50 );
135 
136   sprintf( char_ref, "%s %s", locate_script, calendar );
137 
138 
139   /* Open a pipe to the locate procedure. */
140   pipe_ref = (FILE *) popen( char_ref, "r" );
141   SysFree( char_ref );
142 
143   if( pipe_ref == NULL ) {
144     xitErMessage( parent, XIT_ER_ERROR,
145                   module_name, "xtmLcLocateCalendar",
146                   msgGetText( MXDI_PROCESS_CANNOT_FORK ) );
147 
148     return( False );
149   }
150 
151 
152   /* Wait for the result (a single line). If empty, not found. */
153   char_ref = fgets( buffer, sizeof( buffer ), pipe_ref );
154 
155   /* Close the pipe. */
156 #ifdef XD_HAS_NO_WAITPID
157   fclose( pipe_ref );
158 #else
159   pclose( pipe_ref );
160 #endif
161 
162   /* Restore cursor. */
163   if( parent != NULL )
164     XUndefineCursor( display, window );
165 
166 
167   /* Check the result. */
168   if( char_ref == NULL )
169     return( False );
170 
171   buffer[ strlen( buffer ) -1 ] = '\0';
172   strncpy( result, buffer, max_result );
173   *(result + max_result - 1) = '\0';
174 
175 
176   return( True );
177 
178 } /* xtmLcLocateCalendar */
179