Commit b7c6e7ca b7c6e7ca285ab9d2db395d32d14fdab462e54a3a by Jakob Kaivo

update to mailx msglist parser (parts of it now work!)

tweak some mailx commands
1 parent 86506722
......@@ -24,6 +24,19 @@
int
mail_bang (int argc, char **argv)
{
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
int pid = fork ();
if (pid == 0)
{
free (argv[0]);
argv[0] = strdup ("/bin/sh");
execv ("/bin/sh", argv);
return 1;
}
else if (pid > 0)
{
while (waitpid(pid, NULL, 0) == -1)
/* do nothing */;
return 0;
}
return 1;
}
......
......@@ -20,6 +20,6 @@
int
mail_exit (int argc, char **argv)
{
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
return 1;
mailbox_close (mbox);
exit (0);
}
......
......@@ -32,12 +32,8 @@ mail_from (int argc, char **argv)
header_t hdr;
char *from, *subj;
int froml, subjl;
int columns = 74;
char format[64];
char *col = getenv("COLUMNS");
if (col)
columns = strtol (col, NULL, 10) - 5;
int cols = util_getcols () - 6;
if (mailbox_get_message (mbox, cursor, &msg) != 0)
{
......@@ -45,9 +41,9 @@ mail_from (int argc, char **argv)
return 1;
}
froml = columns / 3;
subjl = columns * 2 / 3;
if (froml + subjl > columns)
froml = cols / 3;
subjl = cols * 2 / 3;
if (froml + subjl > cols)
subjl--;
from = malloc (froml * sizeof (char));
......
......@@ -24,34 +24,30 @@
int
mail_headers (int argc, char **argv)
{
if (argc > 1)
return util_msglist_command (mail_headers, argc, argv);
else
char buf[64];
int low = 1, high = total, middle = cursor;
int lines = util_getlines () - 2;
if (argc > 2)
return 1;
if (argc == 2)
middle = strtol (argv[1], NULL, 10);
if (lines < total)
{
message_t msg;
header_t hdr;
stream_t is;
char buffer[BUFSIZ];
off_t off = 0;
size_t n = 0;
if (mailbox_get_message (mbox, cursor, &msg) != 0)
{
printf ("Could not read message %d\n", cursor);
return 1;
}
message_get_header (msg, &hdr);
header_get_stream (hdr, &is);
while (stream_read (is, buffer, sizeof (buffer) - 1, off, &n) == 0
&& n != 0)
low = middle - (lines / 2);
if (low < 1)
low = 1;
high = low + lines;
if (high > total)
{
buffer[n] = '\0';
printf ("%s", buffer);
off += n;
high = total;
low = high - lines;
}
return 0;
}
return 1;
memset (buf, '\0', 64);
snprintf (buf, 64, "from %d-%d", low, high);
return util_do_command (buf);
}
......
......@@ -25,13 +25,10 @@
int
mail_list (int argc, char **argv)
{
int i = 0, pos = 0, columns = 80, len = 0;
char *cmd;
char *col = getenv ("COLUMNS");
char *cmd = NULL;
int i = 0, pos = 0, len = 0;
int cols = util_getcols () - 5;
if (col)
columns = strtol (col, NULL, 10);
for (i=0; mail_command_table[i].shortname != 0; i++)
{
len = strlen (mail_command_table[i].longname);
......@@ -45,7 +42,7 @@ mail_list (int argc, char **argv)
pos += len + 1;
if (pos >= columns)
if (pos >= cols)
{
pos = 0;
printf ("\n");
......
......@@ -119,7 +119,6 @@ int
main (int argc, char **argv)
{
char *command = NULL, *cmd = NULL;
char *from[] = { "from", "*" };
struct arguments args;
cursor = 1;
......@@ -162,11 +161,7 @@ main (int argc, char **argv)
exit (0);
}
/* mail_from (2, from); */
/* FIXME: this is bad form */
for (cursor = 1; cursor <= total; cursor++)
mail_from (1, from);
cursor = realcursor;
util_do_command ("from *");
/* Initialize readline */
rl_readline_name = "mail";
......
......@@ -40,6 +40,8 @@
#include <mailutils/header.h>
#include <mailutils/body.h>
#include <argcv.h>
#ifdef __cplusplus
extern "C" {
#endif
......@@ -53,14 +55,14 @@ extern "C" {
#endif /*__P */
/* Type definitions */
#ifndef Function
typedef int Function ();
#ifndef function_t
typedef int function_t ();
#endif
struct mail_command_entry {
char *shortname;
char *longname;
Function *func;
function_t *func;
char *synopsis;
};
......@@ -121,12 +123,14 @@ int mail_eq __P((int argc, char **argv)); /* command = */
int util_expand_msglist __P((const int argc, char **argv, int **list));
int util_do_command __P((const char *cmd));
int util_msglist_command __P((int (*func)(int, char**), int argc, char **argv));
Function* util_command_get __P((char *cmd));
int util_msglist_command __P((function_t *func, int argc, char **argv));
function_t* util_command_get __P((char *cmd));
char **util_command_completion __P((char *cmd, int start, int end));
char *util_command_generator __P((char *text, int state));
char *util_stripwhite __P((char *string));
struct mail_command_entry util_find_entry __P((char *cmd));
int util_getcols __P((void));
int util_getlines __P((void));
#ifdef __cplusplus
}
......
......@@ -26,13 +26,12 @@ mail_shell (int argc, char **argv)
{
if (argc > 1)
return 1;
else if (fork ())
else if (!fork ())
{
char *path = getenv ("SHELL");
if (path == NULL)
path = strdup ("sh");
execl (path, path);
free (path);
path = strdup ("/bin/sh");
execv (path, &path);
return 1;
}
else
......
......@@ -17,6 +17,32 @@
#include "mail.h"
typedef struct _node {
int data;
struct _node *next;
} node;
static node *
util_ll_add (node *c, int data)
{
c->next = malloc (sizeof (node));
c->data = data;
c->next->next = NULL;
return c->next;
}
static void
util_ll_free (node *c)
{
node *t = c;
while (t != NULL)
{
c = t;
t = t->next;
free (c);
}
}
/*
* expands a standard message list into an array of numbers
* argc is the number of elements being passed in
......@@ -30,98 +56,93 @@ util_expand_msglist (const int argc, char **argv, int **list)
{
int i = 0, lc = 0;
int undelete = 0;
int hyphen = 0;
int *ret = NULL;
/* let's try a linked list */
node *first = malloc (sizeof (node));
node *current = first;
first->next = NULL;
if (util_command_get (argv[0]) == util_command_get ("undelete"))
undelete = 1;
*list = malloc (argc * sizeof (int));
for (i = 1; i < argc; i++)
{
if (!strcmp (argv[i], "+"))
{
*list[lc++] = i; /* FIXME: next [un]deleted message */
/* FIXME: next [un]deleted message */
current = util_ll_add (current, realcursor + 1);
}
else if (!strcmp (argv[i], "-"))
{
hyphen = 1; /* FIXME: previous [un]deleted message */
/* FIXME: prev [un]deleted message */
current = util_ll_add (current, realcursor - 1);
}
else if (!strcmp (argv[i], "."))
{
*list[lc++] = realcursor;
/* the current cursor location */
current = util_ll_add (current, realcursor);
}
else if (!strcmp (argv[i], "^"))
{
*list[lc++] = i; /* FIXME: first [un]deleted message */
/* FIXME: first [un]deleted message */
current =util_ll_add (current, 1);
}
else if (!strcmp (argv[i], "$"))
{
*list[lc++] = i; /* FIXME: the last message */
/* FIXME: should this be last [un]deleted? */
current = util_ll_add (current, total);
}
else if (!strcmp (argv[i], "*"))
{
free (*list);
*list = malloc (total * sizeof(int));
for (i = 0; i < total; i++)
*list[i] = i + 1;
return total;
util_ll_free (first);
current = first = malloc (sizeof (node));
for (i = 1; i <= total; i++)
current = util_ll_add (current, i);
i = argc + 1;
}
else if (argv[i][0] == '/')
{
*list[lc++] = i; /* FIXME: all messages with pattern following / in
the subject line, case insensitive */
/* FIXME: all messages with pattern following / in
the subject line, case insensitive */
}
else if (argv[i][0] == ':')
{
*list[lc++] = i; /* FIXME: all messages of type argv[i][1] */
/* FIXME: all messages of type argv[i][1] */
}
else if (isalpha(argv[i][0]))
{
*list[lc++] = i; /* FIXME: all messages from argv[i] */
/* FIXME: all messages from argv[i] */
}
else if (strchr (argv[i], '-') != NULL)
{
int j, x, y;
char *arg = strdup (argv[i]);
for (j=0; j < strlen (arg); j++)
if (arg[j] == '-')
break;
arg[j] = '\0';
x = strtol (arg, NULL, 10);
y = strtol (&(arg[j + 1]), NULL, 10);
for (; x <= y; x++)
current = util_ll_add (current, x);
free (arg);
}
else
{
int j;
int len = strlen (argv[i]);
for (j = 0; j < len; j++)
if (argv[i][j] == '-')
{
hyphen = 1;
break;
}
if (hyphen)
{
if (j != len) /* One argument "x-y". */
{
int x, y;
char *arg = strdup (argv[i]);
arg[j] = '\0';
x = atoi (arg);
y = atoi (&(arg[j + 1]));
/* In this case, we also have to realloc() the list. */
*list = realloc (*list, (argc + 2) * sizeof (int));
for (; x <= y; x++, lc++)
*list[lc] = x;
free (arg);
}
else if (i == 3) /* 3 arguments "x" "-" "y". */
{
int x, y;
x = *list[lc - 1];
y = atoi (argv[i]);
for (; x <= y; x++, lc++)
*list[lc] = x;
}
else /* Badly form. */
*list[lc++] = atoi (argv[i]);
hyphen = 0;
}
else
*list[lc++] = atoi(argv[i]);
current = util_ll_add (current, strtol (argv[i], NULL, 10));
}
}
return lc;
for (current = first; current != NULL; current = current->next)
lc++;
ret = malloc (lc * sizeof (int));
lc = 0;
for (current = first; current != NULL; current = current->next)
ret [lc++] = current->data;
util_ll_free (first);
*list = ret;
return lc-1;
}
/*
......@@ -135,7 +156,7 @@ util_do_command (const char *cmd)
int argc = 0;
char **argv = NULL;
int status = 0;
Function *command;
function_t *command;
if (cmd[0] == '#')
return 0;
......@@ -169,7 +190,7 @@ util_do_command (const char *cmd)
*/
int
util_msglist_command (int (*func)(int, char**), int argc, char **argv)
util_msglist_command (function_t *func, int argc, char **argv)
{
int i;
int *list = NULL;
......@@ -192,7 +213,7 @@ util_msglist_command (int (*func)(int, char**), int argc, char **argv)
/*
* returns the function to run for command
*/
Function *
function_t *
util_command_get (char *cmd)
{
struct mail_command_entry entry = util_find_entry (cmd);
......@@ -279,4 +300,28 @@ util_stripwhite (char *string)
return s;
}
/*
* get the number of columns on the screen
*/
int
util_getcols (void)
{
int columns = 80;
char *col = getenv ("COLUMNS");
if (col)
columns = strtol (col, NULL, 10);
return columns;
}
/*
* get the number of lines on the screen
*/
int
util_getlines (void)
{
int lines = 24;
char *lin = getenv ("LINES");
if (lin)
lines = strtol (lin, NULL, 10);
return lines;
}
......