Making "-l" not to trunc the commands.
authorviric@llimona
Tue, 10 Apr 2007 00:14:45 +0200
changeset 117 db479ef293d3
parent 116 5fe47d692522
child 118 f45aad564b6e
Making "-l" not to trunc the commands.
Makefile
client.c
error.c
execute.c
jobs.c
list.c
main.h
msg.c
msg.h
msgdump.c
server.c
--- a/Makefile	Tue Apr 10 00:08:52 2007 +0200
+++ b/Makefile	Tue Apr 10 00:14:45 2007 +0200
@@ -12,7 +12,8 @@
 	client_run.o \
 	mail.o \
 	error.o \
-	signals.o
+	signals.o \
+	list.o
 INSTALL=/usr/bin/install -c
 
 all: ts
@@ -33,6 +34,7 @@
 mail.o: mail.c main.h
 error.o: error.c main.h msg.h
 signals.o: signals.c main.h
+list.o: list.c main.h
 
 clean:
 	rm -f *.o ts
--- a/client.c	Tue Apr 10 00:08:52 2007 +0200
+++ b/client.c	Tue Apr 10 00:14:45 2007 +0200
@@ -9,7 +9,6 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include "msg.h"
 #include "main.h"
 
 static void c_end_of_job(const struct Result *res);
--- a/error.c	Tue Apr 10 00:08:52 2007 +0200
+++ b/error.c	Tue Apr 10 00:14:45 2007 +0200
@@ -16,7 +16,6 @@
 #include <stdlib.h>
 
 #include "main.h"
-#include "msg.h"
 
 enum Etype
 {
--- a/execute.c	Tue Apr 10 00:08:52 2007 +0200
+++ b/execute.c	Tue Apr 10 00:14:45 2007 +0200
@@ -16,7 +16,6 @@
 #include <sys/times.h>
 #include <sys/time.h>
 
-#include "msg.h"
 #include "main.h"
 
 /* Returns errorlevel */
--- a/jobs.c	Tue Apr 10 00:08:52 2007 +0200
+++ b/jobs.c	Tue Apr 10 00:14:45 2007 +0200
@@ -8,7 +8,6 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
-#include "msg.h"
 #include "main.h"
 
 static enum
@@ -17,19 +16,6 @@
     WAITING /* A task is running, and the server is waiting */
 } state = FREE;
 
-struct Job
-{
-    struct Job *next;
-    int jobid;
-    char *command;
-    enum Jobstate state;
-    struct Result result; /* Defined in msg.h */
-    char *output_filename;
-    int store_output;
-    int pid;
-    int should_keep_finished;
-};
-
 struct Notify
 {
     int socket;
@@ -152,55 +138,22 @@
 void s_list(int s)
 {
     struct Job *p;
-    char buffer[500];
+    char *buffer;
 
     /* We limit to 100 bytes for output and 200 for the command.
      * We also put spaces between the data, for assuring parseability. */
     /* Times:   0.00/0.00/0.00 - 4+4+4+2 = 14*/ 
-    sprintf(buffer, "%-4s %-10s %-20.100s %-8s %-14s %.200s\n",
-            "ID",
-            "State",
-            "Output",
-            "E-Level",
-            "Times(r/u/s)",
-            "Command");
+    buffer = joblist_headers();
     send_list_line(s,buffer);
+    free(buffer);
 
     /* Show Queued or Running jobs */
     p = firstjob;
     while(p != 0)
     {
-        const char * jobstate;
-        const char * output_filename;
-        jobstate = jstate2string(p->state);
-        if (p->store_output)
-        {
-            if (p->state == RUNNING)
-            {
-                if (p->output_filename == 0)
-                    /* This may happen due to concurrency
-                     * problems */
-                    output_filename = "(...)";
-                else
-                    output_filename = p->output_filename;
-            } else
-                output_filename = "(file)";
-        } else
-            output_filename = "stdout";
-
-           
-        /* We limit to 100 bytes for output and 200 for the command.
-         * We also put spaces between the data, for assuring parseability. */
-        sprintf(buffer, "%-4i %-10s %-20.100s%s %-8s %14s %.200s%s\n",
-                p->jobid,
-                jobstate,
-                output_filename,
-                (strlen(output_filename) > 100) ? "..." : "",
-                "",
-                "",
-                p->command,
-                (strlen(p->command) > 200) ? "..." : "");
+        buffer = joblist_line(p);
         send_list_line(s,buffer);
+        free(buffer);
         p = p->next;
     }
 
@@ -211,28 +164,9 @@
         /* Show Finished jobs */
         while(p != 0)
         {
-            const char * jobstate;
-            const char * output_filename;
-            jobstate = jstate2string(p->state);
-            if (p->output_filename == 0)
-                output_filename = "stdout";
-            else
-                output_filename = p->output_filename;
-            /* We limit to 100 bytes for output and 200 for the command.
-             * We also put spaces between the data, for assuring
-             * parseability. */
-            sprintf(buffer, "%-4i %-10s %-20.100s%s %-8i %0.2f/%0.2f/%0.2f %.200s%s\n",
-                    p->jobid,
-                    jobstate,
-                    output_filename,
-                    (strlen(output_filename) > 100) ? "..." : "",
-                    p->result.errorlevel,
-                    p->result.real_ms,
-                    p->result.user_ms,
-                    p->result.system_ms,
-                    p->command,
-                    (strlen(p->command) > 200) ? "..." : "");
+            buffer = joblist_line(p);
             send_list_line(s,buffer);
+            free(buffer);
             p = p->next;
         }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/list.c	Tue Apr 10 00:14:45 2007 +0200
@@ -0,0 +1,124 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "main.h"
+
+char * joblist_headers()
+{
+    char * line;
+
+    line = malloc(100);
+    /* We limit to 100 bytes for output and 200 for the command.
+     * We also put spaces between the data, for assuring parseability. */
+    /* Times:   0.00/0.00/0.00 - 4+4+4+2 = 14*/ 
+    sprintf(line, "%-4s %-10s %-20.100s %-8s %-14s %.200s\n",
+            "ID",
+            "State",
+            "Output",
+            "E-Level",
+            "Times(r/u/s)",
+            "Command");
+
+    return line;
+}
+
+static int max(int a, int b)
+{
+    if (a > b)
+        return a;
+    return b;
+}
+
+static const char * ofilename_shown(const struct Job *p)
+{
+    const char * output_filename;
+
+    if (p->store_output)
+    {
+        if (p->state == QUEUED)
+        {
+            output_filename = "(file)";
+        } else
+        {
+            if (p->output_filename == 0)
+                /* This may happen due to concurrency
+                 * problems */
+                output_filename = "(...)";
+            else
+                output_filename = p->output_filename;
+        }
+    } else
+        output_filename = "stdout";
+
+    return output_filename;
+}
+
+static char * print_noresult(const struct Job *p)
+{
+    const char * jobstate;
+    const char * output_filename;
+    int maxlen;
+    char * line;
+
+    jobstate = jstate2string(p->state);
+    output_filename = ofilename_shown(p);
+
+    maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1
+        + 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */
+
+    line = (char *) malloc(maxlen);
+    if (line == NULL)
+        error("Malloc for %i failed.\n", maxlen);
+
+    sprintf(line, "%-4i %-10s %-20s %-8s %14s %s\n",
+            p->jobid,
+            jobstate,
+            output_filename,
+            "",
+            "",
+            p->command);
+
+    return line;
+}
+
+static char * print_result(const struct Job *p)
+{
+    const char * jobstate;
+    int maxlen;
+    char * line;
+    const char * output_filename;
+
+    jobstate = jstate2string(p->state);
+    output_filename = ofilename_shown(p);
+
+    maxlen = 4 + 1 + 10 + 1 + max(20, strlen(output_filename)) + 1 + 8 + 1
+        + 14 + 1 + strlen(p->command) + 20; /* 20 is the margin for errors */
+
+    line = (char *) malloc(maxlen);
+    if (line == NULL)
+        error("Malloc for %i failed.\n", maxlen);
+
+    sprintf(line, "%-4i %-10s %-20s %-8i %0.2f/%0.2f/%0.2f %s\n",
+            p->jobid,
+            jobstate,
+            output_filename,
+            p->result.errorlevel,
+            p->result.real_ms,
+            p->result.user_ms,
+            p->result.system_ms,
+            p->command);
+
+    return line;
+}
+
+char * joblist_line(const struct Job *p)
+{
+    char * line;
+
+    if (p->state == FINISHED)
+        line = print_result(p);
+    else
+        line = print_noresult(p);
+
+    return line;
+}
--- a/main.h	Tue Apr 10 00:08:52 2007 +0200
+++ b/main.h	Tue Apr 10 00:14:45 2007 +0200
@@ -1,3 +1,33 @@
+enum
+{
+    CMD_LEN=500
+};
+
+enum msg_types
+{
+    KILL_SERVER,
+    NEWJOB,
+    NEWJOB_OK,
+    RUNJOB,
+    RUNJOB_OK,
+    ENDJOB,
+    LIST,
+    LIST_LINE,
+    CLEAR_FINISHED,
+    ASK_OUTPUT,
+    ANSWER_OUTPUT,
+    REMOVEJOB,
+    REMOVEJOB_OK,
+    WAITJOB,
+    WAITJOB_OK,
+    URGENT,
+    URGENT_OK,
+    GET_STATE,
+    ANSWER_STATE,
+    SWAP_JOBS,
+    SWAP_JOBS_OK
+};
+
 enum Request
 {
     c_QUEUE,
@@ -17,8 +47,6 @@
     c_SWAP_JOBS
 };
 
-struct Result; /* Defined in msg.h */
-
 struct Command_line {
     enum Request request;
     int need_server;
@@ -44,9 +72,63 @@
 extern struct Command_line command_line;
 extern int server_socket;
 extern enum Process_type process_type;
+extern int server_socket; /* Used in the client */
 
 struct msg;
 
+enum Jobstate
+{
+    QUEUED,
+    RUNNING,
+    FINISHED
+};
+
+struct msg
+{
+    enum msg_types type;
+
+    union
+    {
+        struct {
+            int command_size;
+            int store_output;
+            int should_keep_finished;
+        } newjob;
+        struct {
+            int ofilename_size;
+            int store_output;
+            int pid;
+        } output;
+        int jobid;
+        struct Result {
+            int errorlevel;
+            float user_ms;
+            float system_ms;
+            float real_ms;
+        } result;
+        int line_size;
+        enum Jobstate state;
+        struct {
+            int jobid1;
+            int jobid2;
+        } swap;
+    } u;
+};
+
+struct Job
+{
+    struct Job *next;
+    int jobid;
+    char *command;
+    enum Jobstate state;
+    struct Result result; /* Defined in msg.h */
+    char *output_filename;
+    int store_output;
+    int pid;
+    int should_keep_finished;
+};
+
+
 /* client.c */
 void c_new_job();
 void c_list_jobs();
@@ -116,3 +198,23 @@
 /* signals.c */
 void ignore_sigpipe();
 void restore_sigmask();
+
+/* msg.c */
+void send_bytes(const int fd, const char *data, const int bytes);
+int recv_bytes(const int fd, char *data, const int bytes);
+void send_msg(const int fd, const struct msg *m);
+int recv_msg(const int fd, struct msg *m);
+
+/* jobs.c */
+const char * jstate2string(enum Jobstate s);
+
+/* msgdump.c */
+void msgdump(FILE *, const struct msg *m);
+
+/* error.c */
+void error_msg(const struct msg *m, const char *str, ...);
+void warning_msg(const struct msg *m, const char *str, ...);
+
+/* list.c */
+char * joblist_headers();
+char * joblist_line(const struct Job *p);
--- a/msg.c	Tue Apr 10 00:08:52 2007 +0200
+++ b/msg.c	Tue Apr 10 00:14:45 2007 +0200
@@ -8,7 +8,6 @@
 #include <sys/socket.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include "msg.h"
 #include "main.h"
 
 void send_bytes(const int fd, const char *data, const int bytes)
--- a/msg.h	Tue Apr 10 00:08:52 2007 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,86 +0,0 @@
-extern int server_socket;
-
-enum
-{
-    CMD_LEN=500
-};
-
-enum msg_types
-{
-    KILL_SERVER,
-    NEWJOB,
-    NEWJOB_OK,
-    RUNJOB,
-    RUNJOB_OK,
-    ENDJOB,
-    LIST,
-    LIST_LINE,
-    CLEAR_FINISHED,
-    ASK_OUTPUT,
-    ANSWER_OUTPUT,
-    REMOVEJOB,
-    REMOVEJOB_OK,
-    WAITJOB,
-    WAITJOB_OK,
-    URGENT,
-    URGENT_OK,
-    GET_STATE,
-    ANSWER_STATE,
-    SWAP_JOBS,
-    SWAP_JOBS_OK
-};
-
-enum Jobstate
-{
-    QUEUED,
-    RUNNING,
-    FINISHED
-};
-
-struct msg
-{
-    enum msg_types type;
-
-    union
-    {
-        struct {
-            int command_size;
-            int store_output;
-            int should_keep_finished;
-        } newjob;
-        struct {
-            int ofilename_size;
-            int store_output;
-            int pid;
-        } output;
-        int jobid;
-        struct Result {
-            int errorlevel;
-            float user_ms;
-            float system_ms;
-            float real_ms;
-        } result;
-        int line_size;
-        enum Jobstate state;
-        struct {
-            int jobid1;
-            int jobid2;
-        } swap;
-    } u;
-};
-
-/* msg.c */
-void send_bytes(const int fd, const char *data, const int bytes);
-int recv_bytes(const int fd, char *data, const int bytes);
-void send_msg(const int fd, const struct msg *m);
-int recv_msg(const int fd, struct msg *m);
-
-/* jobs.c */
-const char * jstate2string(enum Jobstate s);
-
-/* msgdump.c */
-void msgdump(FILE *, const struct msg *m);
-
-/* error.c */
-void error_msg(const struct msg *m, const char *str, ...);
-void warning_msg(const struct msg *m, const char *str, ...);
--- a/msgdump.c	Tue Apr 10 00:08:52 2007 +0200
+++ b/msgdump.c	Tue Apr 10 00:14:45 2007 +0200
@@ -5,7 +5,6 @@
     Please find the license in the provided COPYING file.
 */
 #include <stdio.h>
-#include "msg.h"
 #include "main.h"
 
 void msgdump(FILE *f, const struct msg *m)
--- a/server.c	Tue Apr 10 00:08:52 2007 +0200
+++ b/server.c	Tue Apr 10 00:14:45 2007 +0200
@@ -19,7 +19,6 @@
 
 #include <stdio.h>
 
-#include "msg.h"
 #include "main.h"
 
 enum