author | viric@llimona |
Sun, 25 Mar 2007 04:55:18 +0200 | |
changeset 23 | 96fcebb68510 |
parent 22 | afdc8410633f |
child 27 | 886bdb2f4632 |
permissions | -rw-r--r-- |
1 | 1 |
#include <unistd.h> |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
2 |
#include <assert.h> |
1 | 3 |
|
4 |
#include <stdio.h> |
|
5 |
||
3 | 6 |
#include "main.h" |
7 |
||
1 | 8 |
extern char *optarg; |
9 |
extern int optind, opterr, optopt; |
|
10 |
||
11 |
int kill_server = 0; |
|
12 |
int need_server = 0; |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
13 |
int clear_finished = 0; |
3 | 14 |
int list_jobs = 0; |
22 | 15 |
int store_output = 1; |
1 | 16 |
|
17 |
int server_socket; |
|
18 |
||
18 | 19 |
/* Allocated in get_command() */ |
20 |
char *new_command; |
|
21 |
||
22 |
void get_command(int index, int argc, char **argv) |
|
23 |
{ |
|
24 |
int size; |
|
25 |
int i; |
|
26 |
||
27 |
size = 0; |
|
28 |
/* Count bytes needed */ |
|
29 |
for (i = index; i < argc; ++i) |
|
30 |
{ |
|
31 |
/* The '1' is for spaces, and at the last i, |
|
32 |
* for the null character */ |
|
33 |
size = size + strlen(argv[i]) + 1; |
|
34 |
} |
|
35 |
||
36 |
/* Alloc */ |
|
37 |
new_command = malloc(size); |
|
38 |
assert(new_command != NULL); |
|
39 |
||
40 |
/* Build the command */ |
|
41 |
strcpy(new_command, argv[index]); |
|
42 |
for (i = index+1; i < argc; ++i) |
|
43 |
{ |
|
44 |
strcat(new_command, " "); |
|
45 |
strcat(new_command, argv[i]); |
|
46 |
} |
|
47 |
} |
|
3 | 48 |
|
1 | 49 |
void parse_opts(int argc, char **argv) |
50 |
{ |
|
2 | 51 |
int c; |
1 | 52 |
|
2 | 53 |
/* Parse options */ |
54 |
while(1) { |
|
22 | 55 |
c = getopt(argc, argv, "klcn"); |
1 | 56 |
|
2 | 57 |
if (c == -1) |
58 |
break; |
|
1 | 59 |
|
2 | 60 |
switch(c) |
61 |
{ |
|
62 |
case 'k': |
|
63 |
kill_server = 1; |
|
64 |
break; |
|
3 | 65 |
case 'l': |
66 |
list_jobs = 1; |
|
67 |
break; |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
68 |
case 'c': |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
69 |
clear_finished = 1; |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
70 |
break; |
22 | 71 |
case 'n': |
72 |
store_output = 0; |
|
73 |
break; |
|
2 | 74 |
} |
75 |
} |
|
1 | 76 |
|
18 | 77 |
new_command = 0; |
78 |
||
16 | 79 |
if (optind < argc) |
18 | 80 |
get_command(optind, argc, argv); |
3 | 81 |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
82 |
if (list_jobs || kill_server || (new_command != 0) |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
83 |
|| clear_finished) |
2 | 84 |
need_server = 1; |
1 | 85 |
} |
86 |
||
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
87 |
static int go_background() |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
88 |
{ |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
89 |
int pid; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
90 |
pid = fork(); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
91 |
|
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
92 |
switch(pid) |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
93 |
{ |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
94 |
case -1: |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
95 |
perror("fork failed"); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
96 |
exit(-1); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
97 |
break; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
98 |
case 0: |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
99 |
break; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
100 |
default: |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
101 |
exit(0); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
102 |
} |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
103 |
} |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
104 |
|
1 | 105 |
int main(int argc, char **argv) |
106 |
{ |
|
2 | 107 |
parse_opts(argc, argv); |
1 | 108 |
|
3 | 109 |
if (need_server) |
110 |
ensure_server_up(); |
|
111 |
||
18 | 112 |
if (new_command != 0) |
3 | 113 |
{ |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
114 |
go_background(); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
115 |
assert(need_server); |
3 | 116 |
c_new_job(new_command); |
22 | 117 |
c_wait_server_commands(new_command, store_output); |
18 | 118 |
free(new_command); |
3 | 119 |
} |
120 |
||
121 |
if (list_jobs != 0) |
|
5 | 122 |
{ |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
123 |
assert(need_server); |
8 | 124 |
c_list_jobs(); |
5 | 125 |
c_wait_server_lines(); |
126 |
} |
|
3 | 127 |
|
128 |
if (kill_server) |
|
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
129 |
{ |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
130 |
assert(need_server); |
3 | 131 |
c_shutdown_server(); |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
132 |
} |
1 | 133 |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
134 |
if (clear_finished) |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
135 |
{ |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
136 |
assert(need_server); |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
137 |
c_clear_finished(); |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
138 |
} |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
139 |
|
2 | 140 |
if (need_server) |
141 |
{ |
|
3 | 142 |
close(server_socket); |
2 | 143 |
} |
1 | 144 |
|
2 | 145 |
return 0; |
1 | 146 |
} |