"Fossies" - the Fresh Open Source Software Archive

Member "procmeter3-3.6+svn387/gtk1/run.c" (24 Dec 2010, 3693 Bytes) of package /linux/misc/procmeter3-3.6+svn387.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "run.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************
    2   $Header: /home/amb/CVS/procmeter3/gtk1/run.c,v 1.3 2010-02-28 10:21:50 amb Exp $
    3 
    4   ProcMeter - A system monitoring program for Linux - Version 3.5d.
    5 
    6   Run external programs.
    7   ******************/ /******************
    8   Written by Andrew M. Bishop
    9 
   10   This file Copyright 1999-2010 Andrew M. Bishop
   11   It may be distributed under the GNU Public License, version 2, or
   12   any higher version.  See section COPYING of the GNU Public license
   13   for conditions under which this file may be redistributed.
   14   ***************************************/
   15 
   16 
   17 #include <stdlib.h>
   18 #include <stdio.h>
   19 #include <string.h>
   20 #include <ctype.h>
   21 #include <unistd.h>
   22 #include <errno.h>
   23 
   24 #include <gtk/gtkwidget.h>
   25 #include <gdk/gdkx.h>
   26 
   27 #include "procmeterp.h"
   28 
   29 /*+ The toplevel widget. +*/
   30 extern GtkWidget *toplevel;
   31 
   32 
   33 /*++++++++++++++++++++++++++++++++++++++
   34   Parse the command from the config file to be run.
   35 
   36   char *string The string to be parsed.
   37 
   38   RunOption *run Contains the parsed result.
   39   ++++++++++++++++++++++++++++++++++++++*/
   40 
   41 void ParseRunCommand(char *string,RunOption *run)
   42 {
   43  int offset;
   44  char *l,*r;
   45 
   46  run->flag=RUN_NONE;
   47  run->command=NULL;
   48 
   49  if(!string)
   50     return;
   51 
   52  if(!strncmp("XBeep",string,5))
   53    {offset=5; run->flag=RUN_XBELL;}
   54  else if(!strncmp("Shell",string,5))
   55    {offset=5; run->flag=RUN_SHELL;}
   56  else if(!strncmp("XTermWait",string,9))
   57    {offset=9; run->flag=RUN_XTERM_WAIT;}
   58  else if(!strncmp("XTerm",string,5))
   59    {offset=5; run->flag=RUN_XTERM;}
   60  else
   61    {offset=0; run->flag=RUN_SHELL;}
   62 
   63  l=string+offset;
   64  r=string+strlen(string)-1;
   65 
   66  while(isspace(*l))
   67     l++;
   68  if(offset && *l!='(')
   69    {
   70     fprintf(stderr,"ProcMeter: Cannot parse run command '%s'\n",string);
   71     run->flag=RUN_NONE;
   72     return;
   73    }       
   74  else if(offset)
   75     l++;
   76  while(isspace(*l))
   77     l++;
   78 
   79  while(isspace(*r))
   80     r--;
   81  if(offset && *r!=')')
   82    {
   83     fprintf(stderr,"ProcMeter: Cannot parse run command '%s'\n",string);
   84     run->flag=RUN_NONE;
   85     return;
   86    }       
   87  else if(offset)
   88     r--;
   89  while(isspace(*r))
   90     r--;
   91 
   92  if(r<l)
   93     return;
   94 
   95  run->command=(char*)malloc(r-l+2);
   96  strncpy(run->command,l,r-l+1);
   97  *(run->command+(r-l)+1)=0;
   98 }
   99 
  100 
  101 /*++++++++++++++++++++++++++++++++++++++
  102   Run a program.
  103 
  104   RunOption *run The information about the program to run.
  105   ++++++++++++++++++++++++++++++++++++++*/
  106 
  107 void RunProgram(RunOption *run)
  108 {
  109  if(run->flag==RUN_NONE || !run->command)
  110     return;
  111 
  112  if(run->flag==RUN_XBELL)
  113     XBell(GDK_WINDOW_XDISPLAY(toplevel->window),0);
  114  else
  115    {
  116     pid_t pid=fork();
  117 
  118     if(pid==-1)
  119        fprintf(stderr,"ProcMeter: Cannot fork child process %s\n",strerror(errno));
  120     else if(pid==0)
  121       {
  122        char *string,*displayname,*displayenv;
  123 
  124        displayname=XDisplayString(GDK_WINDOW_XDISPLAY(toplevel->window));
  125        displayenv=(char*)malloc(strlen(displayname)+10);
  126        sprintf(displayenv,"DISPLAY=%s",displayname);
  127        putenv(displayenv);
  128 
  129        /* close the X connection */
  130 
  131        close(ConnectionNumber(GDK_WINDOW_XDISPLAY(toplevel->window)));
  132 
  133        switch(run->flag)
  134          {
  135          default:
  136          case RUN_SHELL:
  137           execl("/bin/sh","/bin/sh","-c",run->command,NULL);
  138           break;
  139 
  140          case RUN_XTERM:
  141           execlp("xterm","xterm","-title","ProcMeter3","-e","/bin/sh","-c",run->command,NULL);
  142           break;
  143 
  144          case RUN_XTERM_WAIT:
  145           string=(char*)malloc(strlen(run->command)+64);
  146           sprintf(string,"( %s ) ; echo -n 'Press Return to exit' ; read x",run->command);
  147           execlp("xterm","xterm","-title","ProcMeter3","-e","/bin/sh","-c",string,NULL);
  148           break;
  149          }
  150 
  151        /* Never reached */
  152 
  153        exit(1);
  154       }
  155    }
  156 }