"Fossies" - the Fresh Open Source Software Archive

Member "procmeter3-3.6+svn387/xaw/run.c" (24 Dec 2010, 3602 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/xaw/run.c,v 1.5 2010-02-28 10:22:21 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 <X11/X.h>
   25 #include <X11/Xlib.h>
   26 
   27 #include "procmeterp.h"
   28 
   29 
   30 /*+ The display that we are using. +*/
   31 extern Display *display;
   32 
   33 
   34 /*++++++++++++++++++++++++++++++++++++++
   35   Parse the command from the config file to be run.
   36 
   37   char *string The string to be parsed.
   38 
   39   RunOption *run Contains the parsed result.
   40   ++++++++++++++++++++++++++++++++++++++*/
   41 
   42 void ParseRunCommand(char *string,RunOption *run)
   43 {
   44  int offset;
   45  char *l,*r;
   46 
   47  run->flag=RUN_NONE;
   48  run->command=NULL;
   49 
   50  if(!string)
   51     return;
   52 
   53  if(!strncmp("XBeep",string,5))
   54    {offset=5; run->flag=RUN_XBELL;}
   55  else if(!strncmp("Shell",string,5))
   56    {offset=5; run->flag=RUN_SHELL;}
   57  else if(!strncmp("XTermWait",string,9))
   58    {offset=9; run->flag=RUN_XTERM_WAIT;}
   59  else if(!strncmp("XTerm",string,5))
   60    {offset=5; run->flag=RUN_XTERM;}
   61  else
   62    {offset=0; run->flag=RUN_SHELL;}
   63 
   64  l=string+offset;
   65  r=string+strlen(string)-1;
   66 
   67  while(isspace(*l))
   68     l++;
   69  if(offset && *l!='(')
   70    {
   71     fprintf(stderr,"ProcMeter: Cannot parse run command '%s'\n",string);
   72     run->flag=RUN_NONE;
   73     return;
   74    }       
   75  else if(offset)
   76     l++;
   77  while(isspace(*l))
   78     l++;
   79 
   80  while(isspace(*r))
   81     r--;
   82  if(offset && *r!=')')
   83    {
   84     fprintf(stderr,"ProcMeter: Cannot parse run command '%s'\n",string);
   85     run->flag=RUN_NONE;
   86     return;
   87    }       
   88  else if(offset)
   89     r--;
   90  while(isspace(*r))
   91     r--;
   92 
   93  if(r<l)
   94     return;
   95 
   96  run->command=(char*)malloc(r-l+2);
   97  strncpy(run->command,l,r-l+1);
   98  *(run->command+(r-l)+1)=0;
   99 }
  100 
  101 
  102 /*++++++++++++++++++++++++++++++++++++++
  103   Run a program.
  104 
  105   RunOption *run The information about the program to run.
  106   ++++++++++++++++++++++++++++++++++++++*/
  107 
  108 void RunProgram(RunOption *run)
  109 {
  110  if(run->flag==RUN_NONE || !run->command)
  111     return;
  112 
  113  if(run->flag==RUN_XBELL)
  114     XBell(display,0);
  115  else
  116    {
  117     pid_t pid=fork();
  118 
  119     if(pid==-1)
  120        fprintf(stderr,"ProcMeter: Cannot fork child process %s\n",strerror(errno));
  121     else if(pid==0)
  122       {
  123        char *string,*displayname,*displayenv;
  124 
  125        displayname=XDisplayString(display);
  126        displayenv=(char*)malloc(strlen(displayname)+10);
  127        sprintf(displayenv,"DISPLAY=%s",displayname);
  128        putenv(displayenv);
  129 
  130        /* close the X connection */
  131 
  132        close(ConnectionNumber(display));
  133 
  134        switch(run->flag)
  135          {
  136          default:
  137          case RUN_SHELL:
  138           execl("/bin/sh","/bin/sh","-c",run->command,NULL);
  139           break;
  140 
  141          case RUN_XTERM:
  142           execlp("xterm","xterm","-title","ProcMeter3","-e","/bin/sh","-c",run->command,NULL);
  143           break;
  144 
  145          case RUN_XTERM_WAIT:
  146           string=(char*)malloc(strlen(run->command)+64);
  147           sprintf(string,"( %s ) ; echo -n 'Press Return to exit' ; read x",run->command);
  148           execlp("xterm","xterm","-title","ProcMeter3","-e","/bin/sh","-c",string,NULL);
  149           break;
  150          }
  151 
  152        /* Never reached */
  153 
  154        exit(1);
  155       }
  156    }
  157 }