author | viric@mandarina |
Mon, 05 May 2008 23:14:22 +0200 | |
changeset 216 | b4476e12a717 |
parent 170 | dbe96af9e310 |
child 219 | c24a1f5c1715 |
permissions | -rw-r--r-- |
125 | 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 |
*/ |
|
117 | 7 |
#include <stdio.h> |
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
146 | 10 |
#include <sys/time.h> |
117 | 11 |
#include "main.h" |
12 |
||
120 | 13 |
char * joblistdump_headers() |
14 |
{ |
|
15 |
char * line; |
|
16 |
||
17 |
line = malloc(600); |
|
150 | 18 |
snprintf(line, 600, "#!/bin/sh\n# - task spooler (ts) job dump\n" |
120 | 19 |
"# This file has been created because a SIGTERM killed\n" |
20 |
"# your queue server.\n" |
|
21 |
"# The finished commands are listed first.\n" |
|
22 |
"# The commands running or to be run are stored as you would\n" |
|
23 |
"# probably run them. Take care - some quotes may have got" |
|
24 |
" broken\n\n"); |
|
25 |
||
26 |
return line; |
|
27 |
} |
|
28 |
||
117 | 29 |
char * joblist_headers() |
30 |
{ |
|
31 |
char * line; |
|
32 |
||
33 |
line = malloc(100); |
|
150 | 34 |
snprintf(line, 100, "%-4s %-10s %-20s %-8s %-14s %s\n", |
117 | 35 |
"ID", |
36 |
"State", |
|
37 |
"Output", |
|
38 |
"E-Level", |
|
39 |
"Times(r/u/s)", |
|
40 |
"Command"); |
|
41 |
||
42 |
return line; |
|
43 |
} |
|
44 |
||
45 |
static int max(int a, int b) |
|
46 |
{ |
|
47 |
if (a > b) |
|
48 |
return a; |
|
49 |
return b; |
|
50 |
} |
|
51 |
||
52 |
static const char * ofilename_shown(const struct Job *p) |
|
53 |
{ |
|
54 |
const char * output_filename; |
|
55 |
||
156 | 56 |
if (p->state == SKIPPED) |
57 |
{ |
|
58 |
output_filename = "(no output)"; |
|
59 |
} else if (p->store_output) |
|
117 | 60 |
{ |
61 |
if (p->state == QUEUED) |
|
62 |
{ |
|
63 |
output_filename = "(file)"; |
|
64 |
} else |
|
65 |
{ |
|
66 |
if (p->output_filename == 0) |
|
67 |
/* This may happen due to concurrency |
|
68 |
* problems */ |
|
69 |
output_filename = "(...)"; |
|
70 |
else |
|
71 |
output_filename = p->output_filename; |
|
72 |
} |
|
73 |
} else |
|
74 |
output_filename = "stdout"; |
|
75 |
||
156 | 76 |
|
117 | 77 |
return output_filename; |
78 |
} |
|
79 |
||
80 |
static char * print_noresult(const struct Job *p) |
|
81 |
{ |
|
82 |
const char * jobstate; |
|
83 |
const char * output_filename; |
|
84 |
int maxlen; |
|
85 |
char * line; |
|
86 |
||
87 |
jobstate = jstate2string(p->state); |
|
88 |
output_filename = ofilename_shown(p); |
|
89 |
||
90 |
maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1 |
|
91 |
+ 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
92 |
||
170
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
93 |
if (p->label) |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
94 |
maxlen += 3 + strlen(p->label); |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
95 |
if (p->depend) |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
96 |
maxlen += 3; |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
97 |
|
117 | 98 |
line = (char *) malloc(maxlen); |
99 |
if (line == NULL) |
|
100 |
error("Malloc for %i failed.\n", maxlen); |
|
101 |
||
150 | 102 |
if (p->label) |
155 | 103 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8s %14s %s[%s]%s\n", |
150 | 104 |
p->jobid, |
105 |
jobstate, |
|
106 |
output_filename, |
|
107 |
"", |
|
108 |
"", |
|
156 | 109 |
p->depend?"&& ":"", |
150 | 110 |
p->label, |
111 |
p->command); |
|
112 |
else |
|
155 | 113 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8s %14s %s%s\n", |
150 | 114 |
p->jobid, |
115 |
jobstate, |
|
116 |
output_filename, |
|
117 |
"", |
|
118 |
"", |
|
156 | 119 |
p->depend?"&& ":"", |
150 | 120 |
p->command); |
117 | 121 |
|
122 |
return line; |
|
123 |
} |
|
124 |
||
125 |
static char * print_result(const struct Job *p) |
|
126 |
{ |
|
127 |
const char * jobstate; |
|
128 |
int maxlen; |
|
129 |
char * line; |
|
130 |
const char * output_filename; |
|
131 |
||
132 |
jobstate = jstate2string(p->state); |
|
133 |
output_filename = ofilename_shown(p); |
|
134 |
||
135 |
maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1 |
|
136 |
+ 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
137 |
||
170
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
138 |
if (p->label) |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
139 |
maxlen += 3 + strlen(p->label); |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
140 |
if (p->depend) |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
141 |
maxlen += 3; |
dbe96af9e310
The labels where not considered in size when listing.
lbatlle@npdl268.bpo.hp.com
parents:
156
diff
changeset
|
142 |
|
117 | 143 |
line = (char *) malloc(maxlen); |
144 |
if (line == NULL) |
|
145 |
error("Malloc for %i failed.\n", maxlen); |
|
146 |
||
150 | 147 |
if (p->label) |
155 | 148 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8i %0.2f/%0.2f/%0.2f %s[%s]" |
150 | 149 |
"%s\n", |
150 |
p->jobid, |
|
151 |
jobstate, |
|
152 |
output_filename, |
|
153 |
p->result.errorlevel, |
|
154 |
p->result.real_ms, |
|
155 |
p->result.user_ms, |
|
156 |
p->result.system_ms, |
|
156 | 157 |
p->depend?"&& ":"", |
150 | 158 |
p->label, |
159 |
p->command); |
|
160 |
else |
|
155 | 161 |
snprintf(line, maxlen, "%-4i %-10s %-20s %-8i %0.2f/%0.2f/%0.2f %s%s\n", |
150 | 162 |
p->jobid, |
163 |
jobstate, |
|
164 |
output_filename, |
|
165 |
p->result.errorlevel, |
|
166 |
p->result.real_ms, |
|
167 |
p->result.user_ms, |
|
168 |
p->result.system_ms, |
|
156 | 169 |
p->depend?"&& ":"", |
150 | 170 |
p->command); |
117 | 171 |
|
172 |
return line; |
|
173 |
} |
|
174 |
||
175 |
char * joblist_line(const struct Job *p) |
|
176 |
{ |
|
177 |
char * line; |
|
178 |
||
179 |
if (p->state == FINISHED) |
|
180 |
line = print_result(p); |
|
181 |
else |
|
182 |
line = print_noresult(p); |
|
183 |
||
184 |
return line; |
|
185 |
} |
|
120 | 186 |
|
187 |
char * joblistdump_torun(const struct Job *p) |
|
188 |
{ |
|
189 |
int maxlen; |
|
190 |
char * line; |
|
191 |
||
192 |
maxlen = 10 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
193 |
||
194 |
line = (char *) malloc(maxlen); |
|
195 |
if (line == NULL) |
|
196 |
error("Malloc for %i failed.\n", maxlen); |
|
197 |
||
150 | 198 |
snprintf(line, maxlen, "ts %s\n", p->command); |
120 | 199 |
|
200 |
return line; |
|
201 |
} |