File: exec.c

package info (click to toggle)
flin 0.5.1-9.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 276 kB
  • ctags: 164
  • sloc: ansic: 1,642; makefile: 105; sh: 71
file content (155 lines) | stat: -rw-r--r-- 3,193 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>

#include "exec.h"
#include "misc.h"
#include "screen.h"
#include "menu.h"
#include "additional.h"

extern int exec_logging;

/* Finds the named menu */
static int find_menu(struct menu **menu, char *name)
{
   struct menu *q = *menu;

   while (q->prev)
      q = q->prev;

   while (q && (strcmp(q->name, name))) {
      q = q->next;
   }

   if (q) {
      *menu = q;
      return 0;
   } else
      return -1;
}

void display_file(const char *filen)
{
   pid_t pid_child;
   int child_stat;

   clear_scr();
   close_scr();

   if ((pid_child = fork()) == -1)
      error("No more pids\n", 0);
   else if (pid_child > 0) {
      waitpid(pid_child, &child_stat, WUNTRACED);
   } else {
      char *cmd_line = malloc(strlen(PAGER) + strlen(filen) + 2);

      sprintf(cmd_line, "%s %s", PAGER, filen);
      system(cmd_line);
      exit(0);
   }

   printf("[----Hit the ENTER key when you're ready----]\n");
   fflush(stdout);
   while (getchar() != '\n');
   fflush(stdin);
   init_scr();
   clear_scr();
}

/* Interface to screen module
   Execute menu item
   item == item to execute
   *menu == menu list ptr, for use with MENU_SUBs */
void exec_item(struct menu_items item, struct menu **menu)
{
   pid_t pid_child;
   int child_stat;
   char args[255];
   char exec[255];
   char *argv[128];
   int argc;

   switch (item.type) {
   case MENU_SUB:
      if (!find_menu(menu, item.args)) {
	 display_list(*menu);
      } else
	 fprintf(stderr, "No menu named %s\n", item.name);
      break;

   case MENU_ARGS:
      if (get_args(args, item.prompt) == 0) {
	 sprintf(exec, "%s %s", item.args, args);

	 clear_scr(); close_scr();
	 if (prep_for_exec(exec, &argc, argv, 127) > 0)
	    do_exec(exec, argc, argv);
      
	 printf("[----Hit ENTER when you're ready----]\n");
	 fflush(stdout);
	 while (getchar() != '\n');
	 fflush(stdin);
      }

      init_scr(); clear_scr();

      break;

   case MENU_EXEC:
      clear_scr();
      close_scr();

      if (prep_for_exec(item.args, &argc, argv, 127) > 0)
	 do_exec(item.args, argc, argv);
      
      printf("[----Hit ENTER when you're ready----]\n");
      fflush(stdout);
      while (getchar() != '\n');
      fflush(stdin);
      init_scr();
      clear_scr();
      break;
   }
}

int prep_for_exec(char *exec_line, int *argc, char **argv, int maxargc) {
   int ret;

   ret = parseline(exec_line, argc, argv, maxargc);

   if (ret >= 0)
      *(argv + *argc) = NULL;

   return ret;
}

void do_exec(char *exec_line, int argc, char **argv) {
   pid_t pid_child;
   int child_stat;

   if ((pid_child = fork()) == -1)
      error("No more pids\n", 0);
   else if (pid_child > 0) {
      if ((exec_logging == 1) && (exec_line != (char *) NULL))
	 syslog(LOG_LOCAL1 | LOG_INFO, "%d: %s (%d)", getpid(), exec_line, pid_child);

      waitpid(pid_child, &child_stat, WUNTRACED);

      for (; argc; --argc)
	 free((void *) *(argv + argc));
   } else {
      execvp(*argv, argv);
      exit(0);
   }
}