From 02593d170a7163a141acf6f2f72686a7bdbfb828 Mon Sep 17 00:00:00 2001 From: Ole Loots Date: Mon, 27 Aug 2012 20:05:35 +0200 Subject: Added is_process_running Now the source code viewer is launched when not already running (ShelWrite wasn't handling this correctly) --- atari/ctxmenu.c | 34 +++++++++++++++-------- atari/misc.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ atari/misc.h | 5 +++- 3 files changed, 111 insertions(+), 13 deletions(-) (limited to 'atari') diff --git a/atari/ctxmenu.c b/atari/ctxmenu.c index 79395ca1b..1a1755b12 100644 --- a/atari/ctxmenu.c +++ b/atari/ctxmenu.c @@ -23,7 +23,8 @@ #include #include #include -#include +#include +#include #include "desktop/gui.h" #include "desktop/netsurf.h" @@ -122,8 +123,8 @@ void context_popup( struct gui_window * gw, short x, short y ) FILE * fp_tmpfile; char * tempfile; int err = 0; - char * editor; - char cmdline[128]; + char * editor, *lastslash; + char cmdline[PATH_MAX]; pop = get_tree( POP_CTX ); if (pop == NULL) @@ -239,15 +240,24 @@ void context_popup( struct gui_window * gw, short x, short y ) fp_tmpfile = fopen( tempfile, "w" ); if (fp_tmpfile != NULL){ fwrite(data, size, 1, fp_tmpfile); - fclose(fp_tmpfile ); - - // TODO: check if app is runnin, if not, use pexec or such. - /* - sprintf((char*)&cmdline, "%s \"%s\"", nsoption_charp(atari_editor), tempfile ); - system( (char*)&cmdline ); - */ - err = ShelWrite( editor, tempfile , editor, 1, 0); - LOG(("Launched: %s %s (%d)\n", editor, tempfile, err )); + fclose(fp_tmpfile ); + lastslash = strrchr(editor, '/'); + if (lastslash == NULL) + lastslash = strrchr(editor, '\\'); + if (lastslash == NULL) + lastslash = editor; + else + lastslash++; + if(is_process_running(lastslash)){ + err = ShelWrite( editor, tempfile , editor, 1, 0); + } else { + /* check for max length of simple commandline param: */ + if(strlen(tempfile)<=125){ + sprintf(cmdline, "%c%s", (char)strlen(tempfile), + tempfile); + Pexec(100, editor, cmdline, NULL); + } + } } else { printf("Could not open temp file: %s!\n", tempfile ); } diff --git a/atari/misc.c b/atari/misc.c index 372184785..054684a82 100755 --- a/atari/misc.c +++ b/atari/misc.c @@ -45,6 +45,11 @@ #include "cflib.h" extern void * h_gem_rsrc; + +struct is_process_running_callback_data { + const char * fname; + bool found; +}; void warn_user(const char *warning, const char *detail) { @@ -152,6 +157,86 @@ struct gui_window * find_cmp_window( COMPONENT * c ) gw = gw->next; } return( NULL ); +} + +static int scan_process_list(scan_process_callback cb, void *data) +{ + int pid, count = 0; + DIR *dir; + char*dirname; + struct dirent *de; + + if (( dir = opendir("U:/kern")) == NULL) + return(0); + + while ((de = readdir( dir)) != NULL) { + dirname = de->d_name; + + if( dirname[0] != '1' && dirname[0] != '2' && dirname[0] != '3' && dirname[0] != '4' && dirname[0] != '5' + && dirname[0] != '6' && dirname[0] != '7' && dirname[0] != '8' && dirname[0] != '9') + continue; + + count++; + if (cb != NULL) { + /* when callback returns negative value, we stop scanning: */ + pid = atoi(dirname); + if (cb(pid, data)<0) { + break; + } + } + } + + closedir(dir); + + return(count); +} + +static int proc_running_callback(int pid, void * arg) +{ + char buf[PATH_MAX], fnamepath[256]; + FILE *fp; + int nread; + struct is_process_running_callback_data *data; + + data = (struct is_process_running_callback_data *)arg; + + sprintf(fnamepath, "U:\\kern\\%d\\fname", pid); + + fp = fopen(fnamepath, "r"); + if(!fp) + return(0); + + nread = fread(buf, 1, PATH_MAX-1, fp); + fclose(fp); + + if (nread > 0) { + buf[nread] = 0; + + char *lastslash = strrchr(buf, '/'); + + if(lastslash == NULL) + lastslash = strrchr(buf, '\\'); + + if(lastslash==NULL) + lastslash = buf; + else + lastslash++; + + if(strcasecmp(lastslash, data->fname)==0){ + data->found = true; + return(-1); + } + } + return(0); +} + +bool is_process_running(const char * name) +{ + struct is_process_running_callback_data data = {name, false}; + + scan_process_list(proc_running_callback, &data); + + return( (data.found==1) ? true : false ); } diff --git a/atari/misc.h b/atari/misc.h index edd026ba4..612113fc1 100755 --- a/atari/misc.h +++ b/atari/misc.h @@ -39,9 +39,12 @@ /* Modes for find_gui_window: */ #define BY_WINDOM_HANDLE 0x0 #define BY_GEM_HANDLE 0x1 + +typedef int (*scan_process_callback)(int pid, void *data); struct gui_window * find_gui_window( unsigned long, short mode ); -struct gui_window * find_cmp_window( COMPONENT * c ); +struct gui_window * find_cmp_window( COMPONENT * c ); +bool is_process_running(const char * name); OBJECT *get_tree( int idx ); char *get_rsc_string( int idx ); void gem_set_cursor( MFORM_EX * cursor ); -- cgit v1.2.3