Back to index

im-sdk  12.3.91
xaux_ext_help.c
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2003 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 
00043 #pragma ident "$Id$"
00044 
00045 #include <unistd.h>
00046 #include <stdlib.h>
00047 #include <signal.h>
00048 #if defined(USE_CDE_HELP)
00049 #include <X11/Intrinsic.h>
00050 #include <X11/StringDefs.h>
00051 #else /* USE_CDE_HELP */
00052 #include <sys/types.h>
00053 #include <sys/wait.h>
00054 #endif /* USE_CDE_HELP */
00055 
00056 #include "iiimpAux.h"
00057 #include "xaux_ext_help.h"
00058 
00059 #if defined(USE_CDE_HELP)
00060 
00061 #if defined(USE_DTACTIONINVOKE)
00062 #include <Dt/Dt.h>
00063 #include <Dt/Action.h>
00064 
00065 static void DbReloadProc(XtPointer cd);
00066 
00067 #else /* defined(USE_DTACTIONINVOKE) */
00068 
00069 #define DTACTION_CMD    "/usr/dt/bin/dtaction"
00070 
00071 static int help_available = 0;
00072 static char tmp_file[256];
00073 
00074 void
00075 HelpCleanup()
00076 {
00077     if (help_available) {
00078         unlink(tmp_file);
00079     }
00080 }
00081 
00082 void
00083 HelpSinalHandler(int unused)
00084 {
00085     HelpCleanup();
00086     exit(1);
00087 }
00088 
00089 int
00090 HelpXIOErrorHandler(Display * dpy)
00091 {
00092     HelpCleanup();
00093     return 0;
00094 }
00095 #endif /* defined(USE_DTACTIONINVOKE) */
00096 
00097 typedef struct _RmDatabase {
00098     char *help_basepath;
00099 } RmDatabase;
00100 
00101 static RmDatabase rdb;
00102 
00103 static XtResource rdb_items[] = {
00104   {"helpBasepath", "HelpBasepath", XtRString, sizeof(char*),
00105    offsetof(RmDatabase, help_basepath), XtRString,
00106    (XPointer)DEFAULT_HELP_PATH
00107    },
00108 };
00109 
00110 void
00111 HelpInit(
00112        Widget shell,
00113         char *ApplicationClass,
00114        int argc,
00115        char **argv
00116 )
00117 {
00118 #if defined(USE_DTACTIONINVOKE)
00119     if (DtInitialize(XtDisplay(shell), shell, argv[0], ApplicationClass)==False) {
00120         /* DtInitialize() has already logged an appropriate error msg */
00121         exit(-1);
00122     }
00123 
00124     /* Load the filetype/action databases */
00125     DtDbLoad();
00126 
00127     /* Notice changes to the database without needing to restart application */
00128     DtDbReloadNotify(DbReloadProc, NULL);
00129 #else /* defined(USE_DTACTIONINVOKE) */
00130     int i;
00131     char *tmp_dir;
00132     char tmp_tmp_file[256];
00133 
00134     if (help_available) {
00135         return;
00136     }
00137     XtGetApplicationResources(shell,
00138                               &rdb, rdb_items, 1,
00139                               NULL, 0);
00140 
00141     if (access(DTACTION_CMD, X_OK) != 0) {
00142         return;
00143     }
00144     tmp_dir = getenv("HOME");
00145     if (!tmp_dir) {
00146         return;
00147     }
00148 
00149     /* check tmp file exists or not */
00150     for(i=0;i<10;i++){
00151         sprintf(tmp_tmp_file, "%s/.dt/tmp/%s.%s", tmp_dir,
00152             strrchr(ApplicationClass, '.') + 1, "XXXXXX");
00153         mktemp(tmp_tmp_file);
00154         if (access(tmp_tmp_file, F_OK) != 0) {
00155             strcpy(tmp_file, tmp_tmp_file);
00156            help_available = 1;
00157             break;
00158         }
00159     }
00160 
00161     if(!help_available){
00162        return;
00163     }
00164 
00165     /* At shoutdown of user session */
00166     XSetIOErrorHandler(HelpXIOErrorHandler);
00167 
00168     signal(SIGKILL, HelpSinalHandler);
00169     signal(SIGTERM, HelpSinalHandler);
00170     signal(SIGHUP, HelpSinalHandler);
00171 #endif /* defined(USE_DTACTIONINVOKE) */
00172 
00173     XtGetApplicationResources(shell,
00174                                 &rdb, rdb_items, 1,
00175                                 NULL, 0);
00176 
00177 #if defined(DEBUG_XAUX)
00178    printf("basepath = %s\n", rdb.help_basepath);
00179 #endif /* defined(DEBUG_XAUX) */
00180 }
00181 
00182 #if defined(USE_DTACTIONINVOKE)
00183 static void DbReloadProc(XtPointer cd)
00184 {
00185     /* Pick up any dynamic changes to the database files */
00186     DtDbLoad();
00187 }
00188 #endif /* defined(USE_DTACTIONINVOKE) */
00189 
00190 void
00191 HelpInvoke(
00192        Widget toplevel,
00193        char *path_
00194 )
00195 {
00196 #if defined(USE_DTACTIONINVOKE)
00197     DtActionArg *ap = NULL;
00198     int nap = 0;
00199     DtActionInvocationID actionId;
00200     char path[256];
00201 
00202     sprintf(path, "%s%s", rdb.help_basepath, path_);
00203 
00204 #if defined(DEBUG_XAUX)
00205     printf("actual path = %s\n", path);
00206 #endif /* defined(DEBUG_XAUX) */
00207 
00208     if (path != NULL){
00209         ap = (DtActionArg*) XtCalloc(1, sizeof(DtActionArg));
00210 
00211         ap[0].argClass = DtACTION_BUFFER;
00212         ap[0].u.buffer.bp = (void*)path;
00213         ap[0].u.buffer.size = strlen(path);
00214         ap[0].u.buffer.type = NULL;
00215         ap[0].u.buffer.name = NULL;
00216         ap[0].u.buffer.writable = False;
00217 
00218         nap = 1;
00219     }
00220 
00221     /* Invoke the specified action */
00222     actionId = DtActionInvoke(toplevel,"SDtWebClient",ap,nap,NULL,NULL,NULL,True,NULL,NULL);
00223 #else /* defined(USE_DTACTIONINVOKE) */
00224     FILE *fp;
00225     pid_t pid;
00226 
00227     if (!help_available) {
00228         return;
00229     }
00230     if (!path_) {
00231         return;
00232     }
00233     if ((fp = fopen(tmp_file, "w")) == NULL) {
00234         return;
00235     }
00236     fprintf(fp, "%s%s\n", rdb.help_basepath, path_);
00237     fclose(fp);
00238 
00239     pid = fork1();
00240 
00241     if (pid == (pid_t) (-1)) {
00242         return;
00243     } else if (0 == pid) {
00244         execl(DTACTION_CMD, "dtaction",
00245               "SDtWebClient", tmp_file, NULL);
00246         _exit(1);
00247     }
00248     return;
00249 #endif /* defined(USE_DTACTIONINVOKE) */
00250 }
00251 
00252 #else /* USE_CDE_HELP */
00253 
00254 void
00255 HelpInvoke(
00256         char *path
00257 )
00258 {
00259     char *browser;
00260     char *basepath;
00261 
00262     pid_t pid, pid2;
00263 
00264     if (!path) {
00265       return;
00266     }
00267 
00268 #ifdef linux
00269     pid = fork();
00270 #else
00271     pid = fork1();
00272 #endif
00273 
00274 #if defined(USE_GNOME_HELP)
00275     browser = "/usr/lib/iiim/le/sampleja4/help/help-handler";
00276 #else /* USE_GNOME_HELP */
00277     if (getenv("HTT_HELP_BROWSER")){
00278       browser = getenv("HTT_HELP_BROWSER");
00279     } else {
00280       browser = "/usr/bin/netscape";
00281     }
00282 #endif /* USE_GNOME_HELP */
00283 
00284     if (getenv("HTT_HELP_PATH")){
00285       basepath = getenv("HTT_HELP_PATH");
00286     } else {
00287       basepath = DEFAULT_HELP_PATH;
00288     }
00289 
00290     if (pid == (pid_t) (-1)) {
00291         return;
00292     } else if (0 == pid) {
00293         pid2 = fork();
00294         if(pid2 == -1) {
00295             _exit(1);
00296         } else if(pid2 == 0) {
00297            char *help = (char*)malloc(strlen(basepath) + strlen(path) +1 );
00298            sprintf(help, "%s%s", basepath, path);
00299            execl(browser, browser, help, NULL);
00300            free(help);
00301             _exit(1);
00302         } else {
00303             _exit(0);
00304         }
00305     } else {
00306         waitpid(pid, NULL, 0);
00307     }
00308     return;
00309 }
00310 
00311 #endif /* USE_CDE_HELP */