author | viric@llimona |
Tue, 27 Mar 2007 23:29:56 +0200 | |
changeset 58 | 3c492266923e |
parent 55 | 678ca291d545 |
child 61 | b8ca313cb00c |
permissions | -rw-r--r-- |
49 | 1 |
/* |
2 |
Task Spooler - a task queue system for the unix user |
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
|
4 |
||
5 |
Please find the license in the provided COPYING file. |
|
6 |
*/ |
|
1 | 7 |
#include <unistd.h> |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
8 |
#include <assert.h> |
44 | 9 |
#include <stdlib.h> |
10 |
#include <string.h> |
|
1 | 11 |
|
12 |
#include <stdio.h> |
|
13 |
||
3 | 14 |
#include "main.h" |
15 |
||
1 | 16 |
extern char *optarg; |
17 |
extern int optind, opterr, optopt; |
|
18 |
||
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
19 |
/* Globals */ |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
20 |
struct Command_line command_line; |
1 | 21 |
int server_socket; |
22 |
||
18 | 23 |
/* Allocated in get_command() */ |
24 |
char *new_command; |
|
25 |
||
58 | 26 |
static char version[] = "Task Spooler v0.2.3 - a task queue system for the unix user.\n" |
49 | 27 |
"Copyright (C) 2007 Lluis Batlle i Rossell"; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
28 |
|
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
29 |
static void default_command_line() |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
30 |
{ |
36 | 31 |
command_line.request = c_LIST; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
32 |
command_line.need_server = 0; |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
33 |
command_line.store_output = 1; |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
34 |
command_line.should_go_background = 1; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
35 |
command_line.should_keep_finished = 1; |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
36 |
} |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
37 |
|
18 | 38 |
void get_command(int index, int argc, char **argv) |
39 |
{ |
|
40 |
int size; |
|
41 |
int i; |
|
42 |
||
43 |
size = 0; |
|
44 |
/* Count bytes needed */ |
|
45 |
for (i = index; i < argc; ++i) |
|
46 |
{ |
|
47 |
/* The '1' is for spaces, and at the last i, |
|
48 |
* for the null character */ |
|
49 |
size = size + strlen(argv[i]) + 1; |
|
50 |
} |
|
51 |
||
52 |
/* Alloc */ |
|
40 | 53 |
new_command = (char *) malloc(size); |
18 | 54 |
assert(new_command != NULL); |
55 |
||
56 |
/* Build the command */ |
|
57 |
strcpy(new_command, argv[index]); |
|
58 |
for (i = index+1; i < argc; ++i) |
|
59 |
{ |
|
60 |
strcat(new_command, " "); |
|
61 |
strcat(new_command, argv[i]); |
|
62 |
} |
|
63 |
} |
|
3 | 64 |
|
1 | 65 |
void parse_opts(int argc, char **argv) |
66 |
{ |
|
2 | 67 |
int c; |
1 | 68 |
|
2 | 69 |
/* Parse options */ |
70 |
while(1) { |
|
53 | 71 |
c = getopt(argc, argv, ":VhKClnfr:t:c:o:p:w:u:"); |
1 | 72 |
|
2 | 73 |
if (c == -1) |
74 |
break; |
|
1 | 75 |
|
2 | 76 |
switch(c) |
77 |
{ |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
78 |
case 'K': |
31 | 79 |
command_line.request = c_KILL_SERVER; |
2 | 80 |
break; |
3 | 81 |
case 'l': |
31 | 82 |
command_line.request = c_LIST; |
3 | 83 |
break; |
36 | 84 |
case 'h': |
85 |
command_line.request = c_SHOW_HELP; |
|
86 |
break; |
|
37 | 87 |
case 'V': |
88 |
command_line.request = c_SHOW_VERSION; |
|
89 |
break; |
|
33 | 90 |
case 'C': |
91 |
command_line.request = c_CLEAR_FINISHED; |
|
92 |
break; |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
93 |
case 'c': |
33 | 94 |
command_line.request = c_CAT; |
95 |
command_line.jobid = atoi(optarg); |
|
96 |
break; |
|
97 |
case 'o': |
|
98 |
command_line.request = c_SHOW_OUTPUT_FILE; |
|
99 |
command_line.jobid = atoi(optarg); |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
100 |
break; |
22 | 101 |
case 'n': |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
102 |
command_line.store_output = 0; |
22 | 103 |
break; |
31 | 104 |
case 'f': |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
105 |
command_line.should_go_background = 0; |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
106 |
break; |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
107 |
case 't': |
31 | 108 |
command_line.request = c_TAIL; |
32 | 109 |
command_line.jobid = atoi(optarg); |
29
ca47653a4cbc
Add an option for electing not to go into background.
viric@llimona
parents:
27
diff
changeset
|
110 |
break; |
35 | 111 |
case 'p': |
112 |
command_line.request = c_SHOW_PID; |
|
113 |
command_line.jobid = atoi(optarg); |
|
114 |
break; |
|
41 | 115 |
case 'r': |
116 |
command_line.request = c_REMOVEJOB; |
|
117 |
command_line.jobid = atoi(optarg); |
|
118 |
break; |
|
42 | 119 |
case 'w': |
120 |
command_line.request = c_WAITJOB; |
|
121 |
command_line.jobid = atoi(optarg); |
|
122 |
break; |
|
53 | 123 |
case 'u': |
124 |
command_line.request = c_URGENT; |
|
125 |
command_line.jobid = atoi(optarg); |
|
126 |
break; |
|
31 | 127 |
case ':': |
128 |
switch(optopt) |
|
129 |
{ |
|
130 |
case 't': |
|
32 | 131 |
command_line.request = c_TAIL; |
31 | 132 |
command_line.jobid = -1; /* This means the 'last' job */ |
133 |
break; |
|
33 | 134 |
case 'c': |
135 |
command_line.request = c_CAT; |
|
136 |
command_line.jobid = -1; /* This means the 'last' job */ |
|
137 |
break; |
|
138 |
case 'o': |
|
139 |
command_line.request = c_SHOW_OUTPUT_FILE; |
|
140 |
command_line.jobid = -1; /* This means the 'last' job */ |
|
141 |
break; |
|
35 | 142 |
case 'p': |
143 |
command_line.request = c_SHOW_PID; |
|
144 |
command_line.jobid = -1; /* This means the 'last' job */ |
|
145 |
break; |
|
41 | 146 |
case 'r': |
147 |
command_line.request = c_REMOVEJOB; |
|
148 |
command_line.jobid = -1; /* This means the 'last' |
|
149 |
added job */ |
|
150 |
break; |
|
42 | 151 |
case 'w': |
152 |
command_line.request = c_WAITJOB; |
|
153 |
command_line.jobid = -1; /* This means the 'last' |
|
154 |
added job */ |
|
155 |
break; |
|
53 | 156 |
case 'u': |
157 |
command_line.request = c_URGENT; |
|
158 |
command_line.jobid = -1; /* This means the 'last' |
|
159 |
added job */ |
|
160 |
break; |
|
31 | 161 |
default: |
162 |
fprintf(stderr, "Option %c missing argument: %s\n", |
|
163 |
optopt, optarg); |
|
164 |
exit(-1); |
|
165 |
} |
|
166 |
break; |
|
167 |
case '?': |
|
168 |
fprintf(stderr, "Wrong option %c.\n", optopt); |
|
169 |
exit(-1); |
|
2 | 170 |
} |
171 |
} |
|
1 | 172 |
|
18 | 173 |
new_command = 0; |
174 |
||
37 | 175 |
/* if the request is still the default option... |
176 |
* (the default values should be centralized) */ |
|
177 |
if (optind < argc && command_line.request == c_LIST) |
|
31 | 178 |
{ |
179 |
command_line.request = c_QUEUE; |
|
18 | 180 |
get_command(optind, argc, argv); |
31 | 181 |
} |
3 | 182 |
|
37 | 183 |
if (command_line.request != c_SHOW_HELP && |
184 |
command_line.request != c_SHOW_VERSION) |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
185 |
command_line.need_server = 1; |
55
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
186 |
|
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
187 |
if ( ! command_line.store_output && ! command_line.should_go_background ) |
678ca291d545
Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents:
53
diff
changeset
|
188 |
command_line.should_keep_finished = 0; |
1 | 189 |
} |
190 |
||
44 | 191 |
static void go_background() |
11
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
192 |
{ |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
193 |
int pid; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
194 |
pid = fork(); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
195 |
|
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
196 |
switch(pid) |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
197 |
{ |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
198 |
case -1: |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
199 |
perror("fork failed"); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
200 |
exit(-1); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
201 |
break; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
202 |
case 0: |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
203 |
break; |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
204 |
default: |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
205 |
exit(0); |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
206 |
} |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
207 |
} |
bb94b9890001
Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents:
8
diff
changeset
|
208 |
|
37 | 209 |
static void print_help(const char *cmd) |
36 | 210 |
{ |
52 | 211 |
printf("usage: %s [action] [-n] [-f] [cmd...]\n", cmd); |
212 |
printf("Actions:\n"); |
|
36 | 213 |
printf(" -K kill the task spooler server\n"); |
214 |
printf(" -C clear the list of finished jobs\n"); |
|
215 |
printf(" -l show the job list (default action)\n"); |
|
216 |
printf(" -t [id] tail -f the output of the job. Last if not specified.\n"); |
|
217 |
printf(" -c [id] cat the output of the job. Last if not specified.\n"); |
|
218 |
printf(" -p [id] show the pid of the job. Last if not specified.\n"); |
|
42 | 219 |
printf(" -r [id] remove a job. The last added, if not specified.\n"); |
220 |
printf(" -w [id] wait for a job. The last added, if not specified.\n"); |
|
53 | 221 |
printf(" -u [id] put that job first. The last added, if not specified.\n"); |
37 | 222 |
printf(" -h show this help\n"); |
223 |
printf(" -V show the program version\n"); |
|
52 | 224 |
printf("Options adding jobs:\n"); |
36 | 225 |
printf(" -n don't store the output of the command.\n"); |
226 |
printf(" -f don't fork into background.\n"); |
|
227 |
} |
|
228 |
||
37 | 229 |
static void print_version() |
230 |
{ |
|
231 |
puts(version); |
|
44 | 232 |
} |
37 | 233 |
|
48 | 234 |
static void set_my_env() |
235 |
{ |
|
58 | 236 |
static char tmp[] = "POSIXLY_CORRECT=YES"; |
48 | 237 |
putenv(tmp); |
238 |
} |
|
239 |
||
1 | 240 |
int main(int argc, char **argv) |
241 |
{ |
|
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
242 |
int errorlevel = 0; |
41 | 243 |
|
48 | 244 |
set_my_env(); |
41 | 245 |
/* This is needed in a gnu system, so getopt works well */ |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
246 |
default_command_line(); |
2 | 247 |
parse_opts(argc, argv); |
1 | 248 |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
249 |
if (command_line.need_server) |
3 | 250 |
ensure_server_up(); |
251 |
||
31 | 252 |
switch(command_line.request) |
3 | 253 |
{ |
37 | 254 |
case c_SHOW_VERSION: |
255 |
print_version(argv[0]); |
|
256 |
break; |
|
36 | 257 |
case c_SHOW_HELP: |
258 |
print_help(argv[0]); |
|
37 | 259 |
break; |
31 | 260 |
case c_QUEUE: |
261 |
assert(new_command != 0); |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
262 |
if (command_line.should_go_background) |
29
ca47653a4cbc
Add an option for electing not to go into background.
viric@llimona
parents:
27
diff
changeset
|
263 |
go_background(); |
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
264 |
assert(command_line.need_server); |
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
265 |
c_new_job(new_command); |
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
266 |
if (!command_line.should_go_background) |
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
267 |
errorlevel = c_wait_server_commands(new_command); |
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
268 |
else |
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
269 |
c_wait_server_commands(new_command); |
18 | 270 |
free(new_command); |
31 | 271 |
break; |
272 |
case c_LIST: |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
273 |
assert(command_line.need_server); |
8 | 274 |
c_list_jobs(); |
5 | 275 |
c_wait_server_lines(); |
31 | 276 |
break; |
277 |
case c_KILL_SERVER: |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
278 |
assert(command_line.need_server); |
3 | 279 |
c_shutdown_server(); |
31 | 280 |
break; |
281 |
case c_CLEAR_FINISHED: |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
282 |
assert(command_line.need_server); |
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
283 |
c_clear_finished(); |
31 | 284 |
break; |
32 | 285 |
case c_TAIL: |
286 |
assert(command_line.need_server); |
|
287 |
c_tail(); |
|
288 |
/* This will not return! */ |
|
289 |
break; |
|
33 | 290 |
case c_CAT: |
291 |
assert(command_line.need_server); |
|
292 |
c_cat(); |
|
293 |
/* This will not return! */ |
|
294 |
break; |
|
295 |
case c_SHOW_OUTPUT_FILE: |
|
296 |
assert(command_line.need_server); |
|
297 |
c_show_output_file(); |
|
298 |
break; |
|
35 | 299 |
case c_SHOW_PID: |
300 |
assert(command_line.need_server); |
|
301 |
c_show_pid(); |
|
302 |
break; |
|
41 | 303 |
case c_REMOVEJOB: |
304 |
assert(command_line.need_server); |
|
305 |
c_remove_job(); |
|
306 |
break; |
|
42 | 307 |
case c_WAITJOB: |
308 |
assert(command_line.need_server); |
|
51
aa6e05d77537
'-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents:
49
diff
changeset
|
309 |
errorlevel = c_wait_job(); |
42 | 310 |
break; |
53 | 311 |
case c_URGENT: |
312 |
assert(command_line.need_server); |
|
313 |
c_move_urgent(); |
|
314 |
break; |
|
20
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
315 |
} |
d85b4c0745fa
"-c" added, for clearing the finished tasks' list.
viric@llimona
parents:
18
diff
changeset
|
316 |
|
30
ab88478a7e9b
The commandline parameters are centered in a struct.
viric@llimona
parents:
29
diff
changeset
|
317 |
if (command_line.need_server) |
2 | 318 |
{ |
3 | 319 |
close(server_socket); |
2 | 320 |
} |
1 | 321 |
|
38
768d009604b3
The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents:
37
diff
changeset
|
322 |
return errorlevel; |
1 | 323 |
} |