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