main.c
author viric@llimona
Tue, 03 Apr 2007 23:50:57 +0200
changeset 81 4735cade8213
parent 78 e4d5907f5aaf
child 87 18dc656315eb
permissions -rw-r--r--
Going to 0.3.1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 48
diff changeset
     1
/*
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 48
diff changeset
     2
    Task Spooler - a task queue system for the unix user
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 48
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 48
diff changeset
     4
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 48
diff changeset
     5
    Please find the license in the provided COPYING file.
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 48
diff changeset
     6
*/
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     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
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
     9
#include <stdlib.h>
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
    10
#include <string.h>
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    11
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    12
#include <stdio.h>
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    13
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    14
#include "main.h"
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    15
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    16
extern char *optarg;
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    17
extern int optind, opterr, optopt;
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    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
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    21
int server_socket;
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    22
76
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
    23
/* Globals for the environment of getopt */
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
    24
static char getopt_env[] = "POSIXLY_CORRECT=YES";
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
    25
static char *old_getopt_env;
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
    26
81
4735cade8213 Going to 0.3.1
viric@llimona
parents: 78
diff changeset
    27
static char version[] = "Task Spooler v0.3.1 - a task queue system for the unix user.\n"
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 48
diff changeset
    28
"Copyright (C) 2007  Lluis Batlle i Rossell";
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    29
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    30
static void default_command_line()
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    31
{
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    32
    command_line.request = c_LIST;
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    33
    command_line.need_server = 0;
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    34
    command_line.store_output = 1;
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    35
    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
    36
    command_line.should_keep_finished = 1;
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 63
diff changeset
    37
    command_line.gzip = 0;
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
    38
    command_line.send_output_by_mail = 0;
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    39
}
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    40
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    41
void get_command(int index, int argc, char **argv)
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    42
{
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
    43
    command_line.command.array = &(argv[index]);
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
    44
    command_line.command.num = argc - index;
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    45
}
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    46
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    47
static int get_two_jobs(const char *str, int *j1, int *j2)
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    48
{
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    49
    char tmp[50];
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    50
    char *tmp2;
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    51
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    52
    if(strlen(str) >= 50)
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    53
        return 0;
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    54
    strcpy(tmp, str);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    55
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    56
    tmp2 = strchr(tmp , '-');
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    57
    if (tmp2 == NULL)
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    58
        return 0;
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    59
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    60
    /* We change the '-' to '\0', so we have a delimiter,
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    61
     * and we can access the two strings for the ids */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    62
    *tmp2 = '\0';
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    63
    /* Skip the '\0', and point tmp2 to the second id */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    64
    ++tmp2;
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    65
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    66
    *j1 = atoi(tmp);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    67
    *j2 = atoi(tmp2);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    68
    return 1;
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    69
}
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    70
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    71
void parse_opts(int argc, char **argv)
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    72
{
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    73
    int c;
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
    74
    int res;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    75
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    76
    /* Parse options */
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    77
    while(1) {
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
    78
        c = getopt(argc, argv, ":VhKgClnfmr:t:c:o:p:w:u:s:U:");
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    79
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    80
        if (c == -1)
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    81
            break;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    82
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    83
        switch(c)
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    84
        {
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    85
            case 'K':
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
    86
                command_line.request = c_KILL_SERVER;
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    87
                break;
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    88
            case 'l':
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
    89
                command_line.request = c_LIST;
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    90
                break;
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    91
            case 'h':
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    92
                command_line.request = c_SHOW_HELP;
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    93
                break;
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
    94
            case 'V':
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
    95
                command_line.request = c_SHOW_VERSION;
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
    96
                break;
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    97
            case 'C':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    98
                command_line.request = c_CLEAR_FINISHED;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    99
                break;
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   100
            case 'c':
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   101
                command_line.request = c_CAT;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   102
                command_line.jobid = atoi(optarg);
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   103
                break;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   104
            case 'o':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   105
                command_line.request = c_SHOW_OUTPUT_FILE;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   106
                command_line.jobid = atoi(optarg);
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   107
                break;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 20
diff changeset
   108
            case 'n':
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   109
                command_line.store_output = 0;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 20
diff changeset
   110
                break;
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 63
diff changeset
   111
            case 'g':
dced0efccc19 Added gzip output.
viric@llimona
parents: 63
diff changeset
   112
                command_line.gzip = 1;
dced0efccc19 Added gzip output.
viric@llimona
parents: 63
diff changeset
   113
                break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   114
            case 'f':
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   115
                command_line.should_go_background = 0;
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   116
                break;
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   117
            case 'm':
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   118
                command_line.send_output_by_mail = 1;
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   119
                break;
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   120
            case 't':
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   121
                command_line.request = c_TAIL;
32
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   122
                command_line.jobid = atoi(optarg);
29
ca47653a4cbc Add an option for electing not to go into background.
viric@llimona
parents: 27
diff changeset
   123
                break;
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   124
            case 'p':
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   125
                command_line.request = c_SHOW_PID;
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   126
                command_line.jobid = atoi(optarg);
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   127
                break;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   128
            case 'r':
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   129
                command_line.request = c_REMOVEJOB;
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   130
                command_line.jobid = atoi(optarg);
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   131
                break;
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   132
            case 'w':
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   133
                command_line.request = c_WAITJOB;
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   134
                command_line.jobid = atoi(optarg);
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   135
                break;
53
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   136
            case 'u':
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   137
                command_line.request = c_URGENT;
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   138
                command_line.jobid = atoi(optarg);
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   139
                break;
63
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   140
            case 's':
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   141
                command_line.request = c_GET_STATE;
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   142
                command_line.jobid = atoi(optarg);
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   143
                break;
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   144
            case 'U':
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   145
                command_line.request = c_SWAP_JOBS;
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   146
                res = get_two_jobs(optarg, &command_line.jobid,
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   147
                        &command_line.jobid2);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   148
                if (!res)
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   149
                {
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   150
                    fprintf(stderr, "Wrong <id-id> for -U.\n");
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   151
                    exit(-1);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   152
                }
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   153
                if (command_line.jobid == command_line.jobid2)
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   154
                {
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   155
                    fprintf(stderr, "Wrong <id-id> for -U. "
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   156
                            "Use different ids.\n");
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   157
                    exit(-1);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   158
                }
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   159
                break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   160
            case ':':
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   161
                switch(optopt)
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   162
                {
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   163
                    case 't':
32
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   164
                        command_line.request = c_TAIL;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   165
                        command_line.jobid = -1; /* This means the 'last' job */
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   166
                        break;
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   167
                    case 'c':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   168
                        command_line.request = c_CAT;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   169
                        command_line.jobid = -1; /* This means the 'last' job */
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   170
                        break;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   171
                    case 'o':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   172
                        command_line.request = c_SHOW_OUTPUT_FILE;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   173
                        command_line.jobid = -1; /* This means the 'last' job */
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   174
                        break;
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   175
                    case 'p':
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   176
                        command_line.request = c_SHOW_PID;
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   177
                        command_line.jobid = -1; /* This means the 'last' job */
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   178
                        break;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   179
                    case 'r':
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   180
                        command_line.request = c_REMOVEJOB;
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   181
                        command_line.jobid = -1; /* This means the 'last'
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   182
                                                    added job */
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   183
                        break;
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   184
                    case 'w':
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   185
                        command_line.request = c_WAITJOB;
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   186
                        command_line.jobid = -1; /* This means the 'last'
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   187
                                                    added job */
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   188
                        break;
53
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   189
                    case 'u':
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   190
                        command_line.request = c_URGENT;
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   191
                        command_line.jobid = -1; /* This means the 'last'
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   192
                                                    added job */
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   193
                        break;
63
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   194
                    case 's':
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   195
                        command_line.request = c_GET_STATE;
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   196
                        command_line.jobid = -1; /* This means the 'last'
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   197
                                                    added job */
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   198
                        break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   199
                    default:
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   200
                        fprintf(stderr, "Option %c missing argument: %s\n",
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   201
                                optopt, optarg);
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   202
                        exit(-1);
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   203
                }
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   204
                break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   205
            case '?':
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   206
                fprintf(stderr, "Wrong option %c.\n", optopt);
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   207
                exit(-1);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   208
        }
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   209
    }
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   210
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   211
    command_line.command.num = 0;
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
   212
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   213
    /* if the request is still the default option... 
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   214
     * (the default values should be centralized) */
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   215
    if (optind < argc && command_line.request == c_LIST)
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   216
    {
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   217
        command_line.request = c_QUEUE;
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
   218
        get_command(optind, argc, argv);
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   219
    }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   220
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   221
    if (command_line.request != c_SHOW_HELP &&
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   222
            command_line.request != c_SHOW_VERSION)
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   223
        command_line.need_server = 1;
55
678ca291d545 Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents: 53
diff changeset
   224
678ca291d545 Now the '-nf' doesn't leave a job in 'finished' state.
viric@llimona
parents: 53
diff changeset
   225
    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
   226
        command_line.should_keep_finished = 0;
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   227
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   228
    if ( command_line.send_output_by_mail && ((! command_line.store_output) ||
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   229
                command_line.gzip) )
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   230
    {
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   231
        fprintf(stderr,
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   232
                "For e-mail, you should store the output (not through gzip)\n");
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   233
        exit(-1);
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   234
    }
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   235
}
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   236
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   237
static void fill_first_3_handles()
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   238
{
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   239
    int tmp_pipe1[2];
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   240
    int tmp_pipe2[2];
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   241
    /* This will fill handles 0 and 1 */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   242
    pipe(tmp_pipe1);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   243
    /* This will fill handles 2 and 3 */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   244
    pipe(tmp_pipe2);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   245
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   246
    close(tmp_pipe2[1]);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   247
}
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   248
44
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
   249
static void go_background()
11
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   250
{
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   251
    int pid;
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   252
    pid = fork();
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   253
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   254
    switch(pid)
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   255
    {
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   256
        case -1:
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   257
            perror("fork failed");
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   258
            exit(-1);
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   259
            break;
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   260
        case 0:
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   261
            close(0);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   262
            close(1);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   263
            close(2);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   264
            /* This is a weird thing. But we will later want to
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   265
             * allocate special files to the 0, 1 or 2 fds. It's
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   266
             * almost impossible, if other important things got
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   267
             * allocated here. */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   268
            fill_first_3_handles();
11
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   269
            break;
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   270
        default:
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   271
            exit(0);
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   272
    }
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   273
}
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   274
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   275
static void print_help(const char *cmd)
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   276
{
52
7c6dc23a2a32 Better 'help'.
viric@llimona
parents: 51
diff changeset
   277
    printf("usage: %s [action] [-n] [-f] [cmd...]\n", cmd);
73
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
   278
    printf("Env vars:\n");
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
   279
    printf("  TS_SOCKET  the path to the unix socket used by the ts command\n");
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
   280
    printf("  TS_MAILTO  where to mail the result (on -m). Local user by default\n");
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
   281
    printf("  TS_MAXFINISHED  maximum finished jobs in the queue\n");
0c03786ff927 Added TS_ONFINISH.
viric@llimona
parents: 71
diff changeset
   282
    printf("  TS_ONFINISH  binary called on job end (passing jobid, error, outfile, command)\n");
52
7c6dc23a2a32 Better 'help'.
viric@llimona
parents: 51
diff changeset
   283
    printf("Actions:\n");
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   284
    printf("  -K       kill the task spooler server\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   285
    printf("  -C       clear the list of finished jobs\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   286
    printf("  -l       show the job list (default action)\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   287
    printf("  -t [id]  tail -f the output of the job. Last if not specified.\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   288
    printf("  -c [id]  cat the output of the job. Last if not specified.\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   289
    printf("  -p [id]  show the pid of the job. Last if not specified.\n");
61
b8ca313cb00c Fixed help for "-o"
viric@llimona
parents: 58
diff changeset
   290
    printf("  -o [id]  show the output file. Of last job run, if not specified.\n");
78
e4d5907f5aaf Added manpage and make install.
viric@llimona
parents: 76
diff changeset
   291
    printf("  -s [id]  show the job state. Of the last added, if not specified.\n");
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   292
    printf("  -r [id]  remove a job. The last added, if not specified.\n");
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   293
    printf("  -w [id]  wait for a job. The last added, if not specified.\n");
53
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   294
    printf("  -u [id]  put that job first. The last added, if not specified.\n");
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   295
    printf("  -U <id-id>  swap two jobs in the queue.\n");
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   296
    printf("  -h       show this help\n");
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   297
    printf("  -V       show the program version\n");
52
7c6dc23a2a32 Better 'help'.
viric@llimona
parents: 51
diff changeset
   298
    printf("Options adding jobs:\n");
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   299
    printf("  -n       don't store the output of the command.\n");
65
dced0efccc19 Added gzip output.
viric@llimona
parents: 63
diff changeset
   300
    printf("  -g       gzip the stored output (if not -n).\n");
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   301
    printf("  -f       don't fork into background.\n");
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   302
    printf("  -m       send the output by e-mail (uses sendmail).\n");
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   303
}
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   304
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   305
static void print_version()
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   306
{
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   307
    puts(version);
44
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
   308
}
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   309
76
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   310
static void set_getopt_env()
48
630e26b09141 A bit better compilation in GNU systems.
viric@llimona
parents: 46
diff changeset
   311
{
76
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   312
    old_getopt_env = getenv("POSIXLY_CORRECT");
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   313
    putenv(getopt_env);
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   314
}
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   315
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   316
static void unset_getopt_env()
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   317
{
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   318
    if (old_getopt_env == NULL)
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   319
        /* Wipe the string from the environment */
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   320
        strcpy(getopt_env, "POSIXLY_CORRECT");
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   321
    else
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   322
        sprintf(getopt_env, "POSIXLY_CORRECT=%s", old_getopt_env);
48
630e26b09141 A bit better compilation in GNU systems.
viric@llimona
parents: 46
diff changeset
   323
}
630e26b09141 A bit better compilation in GNU systems.
viric@llimona
parents: 46
diff changeset
   324
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   325
int main(int argc, char **argv)
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   326
{
38
768d009604b3 The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents: 37
diff changeset
   327
    int errorlevel = 0;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   328
76
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   329
    set_getopt_env();
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   330
    /* 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
   331
    default_command_line();
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   332
    parse_opts(argc, argv);
76
14bc35eee745 Fixed two bugs: POSIXLY_CORRECT=YES no more in the child's env, and a
viric@mandarina
parents: 73
diff changeset
   333
    unset_getopt_env();
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   334
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   335
    if (command_line.need_server)
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   336
        ensure_server_up();
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   337
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   338
    switch(command_line.request)
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   339
    {
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   340
    case c_SHOW_VERSION:
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   341
        print_version(argv[0]);
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   342
        break;
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   343
    case c_SHOW_HELP:
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   344
        print_help(argv[0]);
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   345
        break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   346
    case c_QUEUE:
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   347
        assert(command_line.command.num > 0);
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   348
        assert(command_line.need_server);
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   349
        c_new_job();
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   350
        command_line.jobid = c_wait_newjob_ok();
62
2b29d4666f6c Return the jobid in stdout.
viric@llimona
parents: 61
diff changeset
   351
        if (command_line.store_output)
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   352
        {
71
531666e297d7 Send e-letter implemented.
viric@llimona
parents: 69
diff changeset
   353
            printf("%i\n", command_line.jobid);
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   354
            fflush(stdout);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   355
        }
62
2b29d4666f6c Return the jobid in stdout.
viric@llimona
parents: 61
diff changeset
   356
        if (command_line.should_go_background)
2b29d4666f6c Return the jobid in stdout.
viric@llimona
parents: 61
diff changeset
   357
        {
2b29d4666f6c Return the jobid in stdout.
viric@llimona
parents: 61
diff changeset
   358
            go_background();
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   359
            c_wait_server_commands();
62
2b29d4666f6c Return the jobid in stdout.
viric@llimona
parents: 61
diff changeset
   360
        } else
2b29d4666f6c Return the jobid in stdout.
viric@llimona
parents: 61
diff changeset
   361
        {
68
3ae9b38d7d30 Now the orders don't run under 'bash'. They run as is.
viric@llimona
parents: 65
diff changeset
   362
            errorlevel = c_wait_server_commands();
62
2b29d4666f6c Return the jobid in stdout.
viric@llimona
parents: 61
diff changeset
   363
        }
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   364
        break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   365
    case c_LIST:
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   366
        assert(command_line.need_server);
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   367
        c_list_jobs();
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 3
diff changeset
   368
        c_wait_server_lines();
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   369
        break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   370
    case c_KILL_SERVER:
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   371
        assert(command_line.need_server);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   372
        c_shutdown_server();
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   373
        break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   374
    case c_CLEAR_FINISHED:
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   375
        assert(command_line.need_server);
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   376
        c_clear_finished();
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   377
        break;
32
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   378
    case c_TAIL:
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   379
        assert(command_line.need_server);
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   380
        c_tail();
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   381
        /* This will not return! */
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   382
        break;
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   383
    case c_CAT:
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   384
        assert(command_line.need_server);
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   385
        c_cat();
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   386
        /* This will not return! */
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   387
        break;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   388
    case c_SHOW_OUTPUT_FILE:
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   389
        assert(command_line.need_server);
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   390
        c_show_output_file();
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   391
        break;
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   392
    case c_SHOW_PID:
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   393
        assert(command_line.need_server);
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   394
        c_show_pid();
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   395
        break;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   396
    case c_REMOVEJOB:
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   397
        assert(command_line.need_server);
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   398
        c_remove_job();
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   399
        break;
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   400
    case c_WAITJOB:
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   401
        assert(command_line.need_server);
51
aa6e05d77537 '-w' returns the waited errorlevel. Added a testbench.
viric@llimona
parents: 49
diff changeset
   402
        errorlevel = c_wait_job();
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   403
        break;
53
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   404
    case c_URGENT:
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   405
        assert(command_line.need_server);
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   406
        c_move_urgent();
731c41817842 '-u' (urgency) implemented.
viric@llimona
parents: 52
diff changeset
   407
        break;
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   408
    case c_SWAP_JOBS:
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   409
        assert(command_line.need_server);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   410
        c_swap_jobs();
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 65
diff changeset
   411
        break;
63
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   412
    case c_GET_STATE:
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   413
        assert(command_line.need_server);
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   414
        /* This will also print the state into stdout */
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   415
        c_get_state();
47be318f4cbc Added "-s" for querying the job state.
viric@llimona
parents: 62
diff changeset
   416
        break;
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   417
    }
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   418
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   419
    if (command_line.need_server)
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   420
    {
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   421
        close(server_socket);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   422
    }
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   423
38
768d009604b3 The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents: 37
diff changeset
   424
    return errorlevel;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   425
}