home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bricon20.zip / bricons / action.c < prev    next >
C/C++ Source or Header  |  1992-07-23  |  5KB  |  220 lines

  1. #include "gen.h"
  2. #include "widgets.h"
  3. #include <signal.h>
  4. #include <sys/wait.h>
  5. #include <stdio.h>
  6.  
  7. extern    XtAppContext    app_context;
  8.  
  9. static void     catch_child();
  10. static void    ResetButton PROTO((XtPointer,XtIntervalId *));
  11. extern  int     ProcessFile PROTO((Info*, char*));
  12. extern  void    PositionButtons PROTO((int,int));
  13. extern    void    ManageButtons();
  14.  
  15. extern void 
  16. Quit(widget, client_data,call_data)
  17. Widget          widget;
  18. XtPointer       client_data;
  19. XtPointer       call_data;
  20. {
  21.     /* exit graciously */
  22.     XtDestroyWidget(toplevel);
  23.         exit(0);
  24. }
  25.  
  26.  
  27. extern void 
  28. MultiAction(widget,client_data,call_data)
  29. Widget          widget;
  30. XtPointer       client_data;
  31. XtPointer       call_data;
  32. {
  33.     char    *command = (char*) client_data;
  34.     int    pid;
  35.     static    bool_t first_time = TRUE;
  36.     char    *exec_command;    /* for "exec " + command */
  37.  
  38. #define PREPEND "exec "
  39.  
  40.         if (first_time) 
  41.     {
  42.             first_time = FALSE;
  43.         if ((int) signal(SIGCHLD, catch_child) == -1) 
  44.         {
  45.             (void) fprintf(stderr,"unable to install child catcher");
  46.             exit(1);
  47.         }
  48.     }
  49.  
  50.     switch(pid = fork())
  51.     {
  52.         case -1:
  53.             (void) fprintf(stderr,"ERROR: Process cannot fork\n");
  54.             (void) fprintf(stderr,"Sorry unable to execute command\n");
  55.             break;
  56.         case 0:
  57.             /* child */
  58.             exec_command = malloc((unsigned) sizeof(PREPEND) +
  59.                 strlen(command) + 1);
  60.             if(exec_command == NULL)
  61.             {
  62.                 (void) fprintf(stderr,"ERROR: out of memory\n");
  63.                 exit(1);
  64.             }
  65.             strcpy(exec_command,PREPEND);
  66.             strcat(exec_command,command);
  67.             (void)execlp("/bin/sh","sh","-c",exec_command,(char *) 0);
  68.             exit(127);
  69.             break;
  70.     }
  71. }
  72.  
  73.  
  74. extern void 
  75. SingleAction(widget,client_data,call_data)
  76. Widget          widget;
  77. XtPointer       client_data;
  78. XtPointer       call_data;
  79. {
  80.     char    *command = (char*) client_data;
  81.     int    pid;
  82.     int    i = 0;
  83.     static    bool_t first_time = TRUE;
  84.     char    *exec_command;    /* for "exec " + command */
  85.  
  86. #define PREPEND "exec "
  87.  
  88. /*
  89.     This section of code was taken from xalias written by Godfrey Paul
  90.         First time only, arrange for SIGCHLD to be called.
  91. */
  92.         if (first_time) 
  93.     {
  94.             first_time = FALSE;
  95.         if ((int) signal(SIGCHLD, catch_child) == -1) 
  96.         {
  97.             (void) fprintf(stderr,"unable to install child catcher");
  98.             exit(1);
  99.         }
  100.     }
  101.  
  102.     switch(pid = fork())
  103.     {
  104.         case -1:
  105.             (void) fprintf(stderr,"ERROR: Process cannot fork\n");
  106.             (void) fprintf(stderr,"Sorry unable to execute command\n");
  107.             break;
  108.         case 0:
  109.             /* child */
  110.             exec_command = malloc((unsigned) sizeof(PREPEND) +
  111.                 strlen(command) + 1);
  112.             if(exec_command == NULL)
  113.             {
  114.                 (void) fprintf(stderr,"ERROR: out of memory\n");
  115.                 exit(1);
  116.             }
  117.             strcpy(exec_command,PREPEND);
  118.             strcat(exec_command,command);
  119.             (void)execlp("/bin/sh","sh","-c",exec_command,(char *) 0);
  120.             exit(127);
  121.             break;
  122.     }
  123.     while((ci_ptr[i].child_id != 0) && (i < MAXPROCS))
  124.             i++;
  125.     if(i < MAXPROCS)
  126.     {
  127.         ci_ptr[i].child_id = pid;
  128.         ci_ptr[i].w_id = widget;
  129.         XtSetSensitive(widget,False);
  130.     }
  131.     else
  132.         (void) printf("\nWarning: unable to deactivate selected button\n");
  133.  
  134. }
  135.  
  136.  
  137. /*
  138.     This section of code was taken from xalias written by Godfrey Paul
  139. */
  140. static void
  141. catch_child()
  142. {
  143.         int         pid;
  144.     int        i = 0;
  145.         union wait      status;
  146.  
  147.  
  148.         /*
  149.          * wait3() returns -1 if the are no children left, or 0
  150.          * if there are no exited children.
  151.          */
  152.         while ((pid = wait3(&status, WNOHANG, (struct rusage *) NULL)) > 0)
  153.     {
  154.  
  155.         while((ci_ptr[i].child_id != pid) && (i < MAXPROCS))
  156.                 i++;
  157.         if(ci_ptr[i].child_id == pid)
  158.         {
  159.             XtAppAddTimeOut(app_context,50,ResetButton,pid);
  160.         }
  161.     }
  162.         return;
  163. }
  164.  
  165. extern void
  166. Source(widget,client_data,call_data)
  167. Widget          widget;
  168. XtPointer       client_data;
  169. XtPointer       call_data;
  170. {
  171.     Info    *data = (Info*) client_data;
  172.  
  173.     int        x = 0;
  174.     WidgetList    w_list;
  175.     Cardinal    num_childern, i;
  176.     if(data->no_of_buttons >= 0)
  177.     {
  178.         while(x < MAXPROCS)
  179.         {
  180.             ci_ptr[x].child_id = 0;
  181.             ci_ptr[x].w_id = NULL;
  182.             x++;
  183.         }
  184.         XtUnmanageChild(box);
  185.         XtVaGetValues(
  186.             box,
  187.             XtNchildren,
  188.             &w_list,
  189.             XtNnumChildren,
  190.             &num_childern,
  191.             NULL);
  192.  
  193.         for(i = 0; i < num_childern; i++)
  194.         {
  195.             XtDestroyWidget(w_list[i]);
  196.         }
  197.         data->no_of_buttons = 0;
  198.         data->no_of_buttons = ProcessFile(data, "bricons");
  199.         PositionButtons(data->no_of_buttons,data->no_of_columns);
  200.         ManageButtons();
  201.         XtManageChild(box);
  202.     }
  203. }
  204.  
  205. static void
  206. ResetButton(client_data,id)
  207. XtPointer       client_data;
  208. XtIntervalId    *id;
  209. {
  210.     int    c_pid = (int) client_data;
  211.     int    i = 0;
  212.     while((ci_ptr[i].child_id != c_pid) && (i < MAXPROCS))
  213.             i++;
  214.     if(ci_ptr[i].child_id == c_pid)
  215.     {
  216.         XtSetSensitive(ci_ptr[i].w_id,True);
  217.         ci_ptr[i].child_id = 0;
  218.     }
  219. }
  220.