author | viric@llimona |
Mon, 12 Nov 2007 09:57:46 +0100 | |
changeset 140 | 0b99d94818d1 |
parent 125 | b60f173b1489 |
child 146 | 5e689cb593aa |
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> |
|
10 |
#include "main.h" |
|
11 |
||
120 | 12 |
char * joblistdump_headers() |
13 |
{ |
|
14 |
char * line; |
|
15 |
||
16 |
line = malloc(600); |
|
17 |
sprintf(line, "#!/bin/sh\n# - task spooler (ts) job dump\n" |
|
18 |
"# This file has been created because a SIGTERM killed\n" |
|
19 |
"# your queue server.\n" |
|
20 |
"# The finished commands are listed first.\n" |
|
21 |
"# The commands running or to be run are stored as you would\n" |
|
22 |
"# probably run them. Take care - some quotes may have got" |
|
23 |
" broken\n\n"); |
|
24 |
||
25 |
return line; |
|
26 |
} |
|
27 |
||
117 | 28 |
char * joblist_headers() |
29 |
{ |
|
30 |
char * line; |
|
31 |
||
32 |
line = malloc(100); |
|
120 | 33 |
sprintf(line, "%-4s %-10s %-20s %-8s %-14s %s\n", |
117 | 34 |
"ID", |
35 |
"State", |
|
36 |
"Output", |
|
37 |
"E-Level", |
|
38 |
"Times(r/u/s)", |
|
39 |
"Command"); |
|
40 |
||
41 |
return line; |
|
42 |
} |
|
43 |
||
44 |
static int max(int a, int b) |
|
45 |
{ |
|
46 |
if (a > b) |
|
47 |
return a; |
|
48 |
return b; |
|
49 |
} |
|
50 |
||
51 |
static const char * ofilename_shown(const struct Job *p) |
|
52 |
{ |
|
53 |
const char * output_filename; |
|
54 |
||
55 |
if (p->store_output) |
|
56 |
{ |
|
57 |
if (p->state == QUEUED) |
|
58 |
{ |
|
59 |
output_filename = "(file)"; |
|
60 |
} else |
|
61 |
{ |
|
62 |
if (p->output_filename == 0) |
|
63 |
/* This may happen due to concurrency |
|
64 |
* problems */ |
|
65 |
output_filename = "(...)"; |
|
66 |
else |
|
67 |
output_filename = p->output_filename; |
|
68 |
} |
|
69 |
} else |
|
70 |
output_filename = "stdout"; |
|
71 |
||
72 |
return output_filename; |
|
73 |
} |
|
74 |
||
75 |
static char * print_noresult(const struct Job *p) |
|
76 |
{ |
|
77 |
const char * jobstate; |
|
78 |
const char * output_filename; |
|
79 |
int maxlen; |
|
80 |
char * line; |
|
81 |
||
82 |
jobstate = jstate2string(p->state); |
|
83 |
output_filename = ofilename_shown(p); |
|
84 |
||
85 |
maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1 |
|
86 |
+ 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
87 |
||
88 |
line = (char *) malloc(maxlen); |
|
89 |
if (line == NULL) |
|
90 |
error("Malloc for %i failed.\n", maxlen); |
|
91 |
||
92 |
sprintf(line, "%-4i %-10s %-20s %-8s %14s %s\n", |
|
93 |
p->jobid, |
|
94 |
jobstate, |
|
95 |
output_filename, |
|
96 |
"", |
|
97 |
"", |
|
98 |
p->command); |
|
99 |
||
100 |
return line; |
|
101 |
} |
|
102 |
||
103 |
static char * print_result(const struct Job *p) |
|
104 |
{ |
|
105 |
const char * jobstate; |
|
106 |
int maxlen; |
|
107 |
char * line; |
|
108 |
const char * output_filename; |
|
109 |
||
110 |
jobstate = jstate2string(p->state); |
|
111 |
output_filename = ofilename_shown(p); |
|
112 |
||
113 |
maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1 |
|
114 |
+ 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
115 |
||
116 |
line = (char *) malloc(maxlen); |
|
117 |
if (line == NULL) |
|
118 |
error("Malloc for %i failed.\n", maxlen); |
|
119 |
||
120 |
sprintf(line, "%-4i %-10s %-20s %-8i %0.2f/%0.2f/%0.2f %s\n", |
|
121 |
p->jobid, |
|
122 |
jobstate, |
|
123 |
output_filename, |
|
124 |
p->result.errorlevel, |
|
125 |
p->result.real_ms, |
|
126 |
p->result.user_ms, |
|
127 |
p->result.system_ms, |
|
128 |
p->command); |
|
129 |
||
130 |
return line; |
|
131 |
} |
|
132 |
||
133 |
char * joblist_line(const struct Job *p) |
|
134 |
{ |
|
135 |
char * line; |
|
136 |
||
137 |
if (p->state == FINISHED) |
|
138 |
line = print_result(p); |
|
139 |
else |
|
140 |
line = print_noresult(p); |
|
141 |
||
142 |
return line; |
|
143 |
} |
|
120 | 144 |
|
145 |
char * joblistdump_torun(const struct Job *p) |
|
146 |
{ |
|
147 |
int maxlen; |
|
148 |
char * line; |
|
149 |
||
150 |
maxlen = 10 + strlen(p->command) + 20; /* 20 is the margin for errors */ |
|
151 |
||
152 |
line = (char *) malloc(maxlen); |
|
153 |
if (line == NULL) |
|
154 |
error("Malloc for %i failed.\n", maxlen); |
|
155 |
||
122
e6d6ca823d78
Fixed a \n 'mankantan' in the TS_SAVEFILE dump.
viric@llimona
parents:
120
diff
changeset
|
156 |
sprintf(line, "ts %s\n", p->command); |
120 | 157 |
|
158 |
return line; |
|
159 |
} |