main.c
author viric@llimona
Sun, 25 Mar 2007 23:37:42 +0200
changeset 46 620319858e9a
parent 44 4dcf05746ece
child 48 630e26b09141
permissions -rw-r--r--
Preparing for 0.2.1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     1
#include <unistd.h>
11
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
     2
#include <assert.h>
44
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
     3
#include <stdlib.h>
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
     4
#include <string.h>
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     5
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     6
#include <stdio.h>
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     7
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
     8
#include "main.h"
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
     9
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    10
extern char *optarg;
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    11
extern int optind, opterr, optopt;
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    12
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    13
/* Globals */
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    14
struct Command_line command_line;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    15
int server_socket;
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    16
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    17
/* Allocated in get_command() */
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    18
char *new_command;
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    19
46
620319858e9a Preparing for 0.2.1
viric@llimona
parents: 44
diff changeset
    20
static char version[] = "Task Spooler v0.2.1";
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    21
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    22
static void default_command_line()
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    23
{
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    24
    command_line.request = c_LIST;
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    25
    command_line.need_server = 0;
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    26
    command_line.store_output = 1;
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    27
    command_line.should_go_background = 1;
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
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    30
void get_command(int index, int argc, char **argv)
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    31
{
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    32
    int size;
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    33
    int i;
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    34
    
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    35
    size = 0;
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    36
    /* Count bytes needed */
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    37
    for (i = index; i < argc; ++i)
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    38
    {
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    39
        /* The '1' is for spaces, and at the last i,
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    40
         * for the null character */
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    41
        size = size + strlen(argv[i]) + 1;
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    42
    }
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    43
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    44
    /* Alloc */
40
ed74cc289130 Fixed a bug in getopt (:+ should be +:)
viric@llimona
parents: 38
diff changeset
    45
    new_command = (char *) malloc(size);
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    46
    assert(new_command != NULL);
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    47
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    48
    /* Build the command */
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    49
    strcpy(new_command, argv[index]);
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    50
    for (i = index+1; i < argc; ++i)
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    51
    {
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    52
        strcat(new_command, " ");
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    53
        strcat(new_command, argv[i]);
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    54
    }
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
    55
}
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    56
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    57
void parse_opts(int argc, char **argv)
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    58
{
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    59
    int c;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    60
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    61
    /* Parse options */
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    62
    while(1) {
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
    63
        c = getopt(argc, argv, ":VhKClnfr:t:c:o:p:w:");
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    64
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    65
        if (c == -1)
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    66
            break;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    67
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    68
        switch(c)
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    69
        {
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    70
            case 'K':
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
    71
                command_line.request = c_KILL_SERVER;
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    72
                break;
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    73
            case 'l':
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
    74
                command_line.request = c_LIST;
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    75
                break;
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    76
            case 'h':
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    77
                command_line.request = c_SHOW_HELP;
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
    78
                break;
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
    79
            case 'V':
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
    80
                command_line.request = c_SHOW_VERSION;
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
    81
                break;
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    82
            case 'C':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    83
                command_line.request = c_CLEAR_FINISHED;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    84
                break;
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
    85
            case 'c':
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    86
                command_line.request = c_CAT;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    87
                command_line.jobid = atoi(optarg);
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    88
                break;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    89
            case 'o':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    90
                command_line.request = c_SHOW_OUTPUT_FILE;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
    91
                command_line.jobid = atoi(optarg);
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
    92
                break;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 20
diff changeset
    93
            case 'n':
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    94
                command_line.store_output = 0;
22
afdc8410633f Now output can go to filenames.
viric@llimona
parents: 20
diff changeset
    95
                break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
    96
            case 'f':
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    97
                command_line.should_go_background = 0;
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    98
                break;
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
    99
            case 't':
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   100
                command_line.request = c_TAIL;
32
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   101
                command_line.jobid = atoi(optarg);
29
ca47653a4cbc Add an option for electing not to go into background.
viric@llimona
parents: 27
diff changeset
   102
                break;
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   103
            case 'p':
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   104
                command_line.request = c_SHOW_PID;
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   105
                command_line.jobid = atoi(optarg);
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   106
                break;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   107
            case 'r':
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   108
                command_line.request = c_REMOVEJOB;
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   109
                command_line.jobid = atoi(optarg);
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   110
                break;
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   111
            case 'w':
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   112
                command_line.request = c_WAITJOB;
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   113
                command_line.jobid = atoi(optarg);
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   114
                break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   115
            case ':':
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   116
                switch(optopt)
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   117
                {
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   118
                    case 't':
32
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   119
                        command_line.request = c_TAIL;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   120
                        command_line.jobid = -1; /* This means the 'last' job */
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   121
                        break;
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   122
                    case 'c':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   123
                        command_line.request = c_CAT;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   124
                        command_line.jobid = -1; /* This means the 'last' job */
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   125
                        break;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   126
                    case 'o':
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   127
                        command_line.request = c_SHOW_OUTPUT_FILE;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   128
                        command_line.jobid = -1; /* This means the 'last' job */
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   129
                        break;
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   130
                    case 'p':
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   131
                        command_line.request = c_SHOW_PID;
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   132
                        command_line.jobid = -1; /* This means the 'last' job */
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   133
                        break;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   134
                    case 'r':
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   135
                        command_line.request = c_REMOVEJOB;
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   136
                        command_line.jobid = -1; /* This means the 'last'
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   137
                                                    added job */
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   138
                        break;
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   139
                    case 'w':
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   140
                        command_line.request = c_WAITJOB;
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   141
                        command_line.jobid = -1; /* This means the 'last'
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   142
                                                    added job */
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   143
                        break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   144
                    default:
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   145
                        fprintf(stderr, "Option %c missing argument: %s\n",
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   146
                                optopt, optarg);
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   147
                        exit(-1);
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   148
                }
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   149
                break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   150
            case '?':
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   151
                fprintf(stderr, "Wrong option %c.\n", optopt);
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   152
                exit(-1);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   153
        }
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   154
    }
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   155
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
   156
    new_command = 0;
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
   157
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   158
    /* if the request is still the default option... 
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   159
     * (the default values should be centralized) */
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   160
    if (optind < argc && command_line.request == c_LIST)
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   161
    {
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   162
        command_line.request = c_QUEUE;
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
   163
        get_command(optind, argc, argv);
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   164
    }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   165
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   166
    if (command_line.request != c_SHOW_HELP &&
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   167
            command_line.request != c_SHOW_VERSION)
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   168
        command_line.need_server = 1;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   169
}
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   170
44
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
   171
static void go_background()
11
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   172
{
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   173
    int pid;
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   174
    pid = fork();
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   175
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   176
    switch(pid)
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   177
    {
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   178
        case -1:
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   179
            perror("fork failed");
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   180
            exit(-1);
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   181
            break;
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   182
        case 0:
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   183
            break;
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   184
        default:
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   185
            exit(0);
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   186
    }
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   187
}
bb94b9890001 Going to background at for new command queued.
lbatlle@npdl268.bpo.hp.com
parents: 8
diff changeset
   188
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   189
static void print_help(const char *cmd)
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   190
{
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   191
    printf("usage: %s < -K | -C | -l | -t [id] | -c [id] | -p [id] | -r [id] >\n"
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   192
           "       [-n] [ -f ] [cmd...]\n", cmd);
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   193
    printf("  -K       kill the task spooler server\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   194
    printf("  -C       clear the list of finished jobs\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   195
    printf("  -l       show the job list (default action)\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   196
    printf("  -t [id]  tail -f the output of the job. Last if not specified.\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   197
    printf("  -c [id]  cat the output of the job. Last if not specified.\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   198
    printf("  -p [id]  show the pid of the job. Last if not specified.\n");
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   199
    printf("  -r [id]  remove a job. The last added, if not specified.\n");
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   200
    printf("  -w [id]  wait for a job. The last added, if not specified.\n");
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   201
    printf("  -h       show this help\n");
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   202
    printf("  -V       show the program version\n");
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   203
    printf("Adding jobs:\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   204
    printf("  -n       don't store the output of the command.\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   205
    printf("  -f       don't fork into background.\n");
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   206
}
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   207
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   208
static void print_version()
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   209
{
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   210
    puts(version);
44
4dcf05746ece Better include files.
viric@llimona
parents: 42
diff changeset
   211
}
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   212
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   213
int main(int argc, char **argv)
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   214
{
38
768d009604b3 The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents: 37
diff changeset
   215
    int errorlevel = 0;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   216
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   217
    /* This is needed in a gnu system, so getopt works well */
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   218
    setenv("POSIXLY_CORRECT", "YES", 1);
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   219
    default_command_line();
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   220
    parse_opts(argc, argv);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   221
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   222
    if (command_line.need_server)
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   223
        ensure_server_up();
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   224
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   225
    switch(command_line.request)
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   226
    {
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   227
    case c_SHOW_VERSION:
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   228
        print_version(argv[0]);
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   229
        break;
36
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   230
    case c_SHOW_HELP:
f3b164620a3a Added help.
viric@llimona
parents: 35
diff changeset
   231
        print_help(argv[0]);
37
f2f6c6f0e86f Added version.
viric@llimona
parents: 36
diff changeset
   232
        break;
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   233
    case c_QUEUE:
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   234
        assert(new_command != 0);
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   235
        if (command_line.should_go_background)
29
ca47653a4cbc Add an option for electing not to go into background.
viric@llimona
parents: 27
diff changeset
   236
            go_background();
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   237
        assert(command_line.need_server);
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   238
        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
   239
        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
   240
            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
   241
        else
768d009604b3 The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents: 37
diff changeset
   242
            c_wait_server_commands(new_command);
18
af4898956964 Now commands of any-length are accepted.
viric@llimona
parents: 16
diff changeset
   243
        free(new_command);
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   244
        break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   245
    case c_LIST:
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   246
        assert(command_line.need_server);
8
03339adb7014 Some more code for execution.
viric@llimona
parents: 5
diff changeset
   247
        c_list_jobs();
5
bc5e251418f3 The LIST_LINEs are outputed by the client.
viric@llimona
parents: 3
diff changeset
   248
        c_wait_server_lines();
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   249
        break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   250
    case c_KILL_SERVER:
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   251
        assert(command_line.need_server);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   252
        c_shutdown_server();
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   253
        break;
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   254
    case c_CLEAR_FINISHED:
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   255
        assert(command_line.need_server);
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   256
        c_clear_finished();
31
1a6c732b0b71 The command line parsing has been simplified.
viric@llimona
parents: 30
diff changeset
   257
        break;
32
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   258
    case c_TAIL:
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   259
        assert(command_line.need_server);
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   260
        c_tail();
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   261
        /* This will not return! */
3531439f2770 Tail works.
viric@llimona
parents: 31
diff changeset
   262
        break;
33
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   263
    case c_CAT:
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   264
        assert(command_line.need_server);
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   265
        c_cat();
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   266
        /* This will not return! */
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   267
        break;
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   268
    case c_SHOW_OUTPUT_FILE:
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   269
        assert(command_line.need_server);
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   270
        c_show_output_file();
5c2d981f2f8e Added 'cat' and 'show output filename'
viric@llimona
parents: 32
diff changeset
   271
        break;
35
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   272
    case c_SHOW_PID:
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   273
        assert(command_line.need_server);
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   274
        c_show_pid();
83d0da612dc4 Kill the running job works fine.
viric@llimona
parents: 33
diff changeset
   275
        break;
41
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   276
    case c_REMOVEJOB:
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   277
        assert(command_line.need_server);
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   278
        c_remove_job();
cad41574feda Added 'remove job'.
viric@llimona
parents: 40
diff changeset
   279
        break;
42
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   280
    case c_WAITJOB:
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   281
        assert(command_line.need_server);
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   282
        c_wait_job();
f093d0964cf5 Added wait for jobs.
viric@llimona
parents: 41
diff changeset
   283
        break;
20
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   284
    }
d85b4c0745fa "-c" added, for clearing the finished tasks' list.
viric@llimona
parents: 18
diff changeset
   285
30
ab88478a7e9b The commandline parameters are centered in a struct.
viric@llimona
parents: 29
diff changeset
   286
    if (command_line.need_server)
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   287
    {
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   288
        close(server_socket);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   289
    }
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   290
38
768d009604b3 The program returns the errorlevel of the job, if it doesn't go to background.
viric@llimona
parents: 37
diff changeset
   291
    return errorlevel;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   292
}