Commit 1152383e 1152383ee7848d07d6ce262ff05738fe9fa78be6 by Jakob Kaivo

Begin real implementation of mailx mail command

make sure none of the necessary environment variables will be unset in mailx
1 parent 2633d263
......@@ -29,11 +29,8 @@ mail_edit (int argc, char **argv)
else
{
char *file = tempnam(getenv("TMPDIR"), "mu");
char *editor = getenv ("EDITOR");
if (!editor)
editor = strdup ("ed");
util_do_command ("copy %s", file);
util_do_command ("shell %s %s", editor, file);
util_do_command ("shell %s %s", getenv("EDITOR"), file);
remove (file);
free (file);
return 0;
......
......@@ -64,11 +64,6 @@ parse_opt (int key, char *arg, struct argp_state *state)
{
int len;
char *home = getenv("HOME");
if (home == NULL)
{
fprintf (stderr, "No Home!\n");
home = (char *)"";
}
len = strlen (home) + strlen ("/mbox") + 1;
args->file = malloc(len * sizeof (char));
strcpy (args->file, home);
......@@ -129,6 +124,20 @@ main (int argc, char **argv)
signal (SIGPIPE, SIG_IGN);
/* set up the default environment */
if (!getenv ("HOME"))
exit (1); /* FIXME: how to start with no $HOME ? */
setenv ("DEAD", "~/dead.letter", 0); /* FIXME: expand ~ */
setenv ("EDITOR", "ed", 0);
setenv ("LISTER", "ls", 0);
setenv ("MAILRC", "~/.mailrc", 0); /* FIXME: expand ~ */
setenv ("MBOX", "~/mbox", 0); /* FIXME: expand ~ */
setenv ("PAGER", "more", 0);
setenv ("SHELL", "sh", 0);
setenv ("VISUAL", "vi", 0);
setenv ("COLUMNS", "80", 0);
setenv ("LINES", "24", 0);
/* set defaults for execution */
util_do_command ("set noallnet");
util_do_command ("set noappend");
......
......@@ -43,6 +43,7 @@
#include <mailutils/body.h>
#include <argcv.h>
#include <getline.h>
#ifdef __cplusplus
extern "C" {
......@@ -106,10 +107,8 @@ int mail_mbox __P((int argc, char **argv));
int mail_next __P((int argc, char **argv));
int mail_pipe __P((int argc, char **argv));
int mail_previous __P((int argc, char **argv));
int mail_printall __P((int argc, char **argv)); /* command Print */
int mail_print __P((int argc, char **argv));
int mail_quit __P((int argc, char **argv));
int mail_relist __P((int argc, char **argv)); /* command Reply */
int mail_reply __P((int argc, char **argv));
int mail_retain __P((int argc, char **argv));
int mail_save __P((int argc, char **argv));
......@@ -126,7 +125,6 @@ int mail_visual __P((int argc, char **argv));
int mail_write __P((int argc, char **argv));
int mail_z __P((int argc, char **argv));
int mail_bang __P((int argc, char **argv)); /* command ! */
int mail_eq __P((int argc, char **argv)); /* command = */
int util_expand_msglist __P((const int argc, char **argv, int **list));
......
......@@ -31,10 +31,13 @@ mail_next (int argc, char **argv)
realcursor++;
return 0;
}
else if (argc == 2)
else
{
cursor = strtol (argv[1], NULL, 10);
int *list = NULL;
util_expand_msglist (argc, argv, &list);
cursor = list[0];
realcursor = cursor;
free (list);
return 0;
}
return 1;
......
......@@ -60,7 +60,7 @@ mail_pipe (int argc, char **argv)
off =+ n;
}
if ((util_find_env("page"))->set)
fprintf (pipe, "\f");
fprintf (pipe, "\f"); /* FIXME: is this formfeed ? */
}
}
}
......
......@@ -31,10 +31,13 @@ mail_previous (int argc, char **argv)
realcursor--;
return 0;
}
else if ( argc == 2)
else
{
cursor = strtol (argv[1], NULL, 10);
int *list = NULL;
util_expand_msglist (argc, argv, &list);
cursor = list[0];
realcursor = cursor;
free (list);
return 0;
}
return 1;
......
......@@ -50,13 +50,7 @@ mail_print (int argc, char **argv)
message_lines (mesg, &lines);
if ((util_find_env("crt"))->set && lines > util_getlines ())
{
char *pager = getenv ("PAGER");
if (pager)
out = popen (pager, "w");
else
out = popen ("more", "w");
}
out = popen (getenv("PAGER"), "w");
if (islower (argv[0][0]))
{
......@@ -64,13 +58,13 @@ mail_print (int argc, char **argv)
if (header_get_value (hdr, MU_HEADER_FROM, buffer, sizeof (buffer),
NULL) == 0)
{
printf ("From: %s\n", buffer);
fprintf (out, "From: %s\n", buffer);
/* free (buf); */
}
if (header_get_value (hdr, MU_HEADER_SUBJECT, buffer,
sizeof (buffer), NULL) == 0)
{
printf ("Subject: %s\n", buffer);
fprintf (out, "Subject: %s\n", buffer);
/* free (buf); */
}
......
......@@ -25,6 +25,10 @@ int
mail_send (int argc, char **argv)
{
char *to = NULL, *cc = NULL, *bcc = NULL, *subj = NULL;
char *filename = tempnam (getenv ("TMPDIR"), "mu");
FILE *file = fopen (filename, "w");
char *buf = NULL;
size_t n;
if (argc < 2)
to = readline ("To: ");
......@@ -42,7 +46,143 @@ mail_send (int argc, char **argv)
subj = readline ("Subject: ");
else
subj = (util_find_env ("subject"))->value;
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
while (getline (&buf, &n, stdin) != -1)
{
if (buf[0] == (util_find_env("escape"))->value[0])
{
FILE *ostdout = stdout;
stdout = file;
buf[strlen(buf)-1] = '\0';
switch (buf[1])
{
case '!':
util_do_command ("!%s", &buf[3]);
break;
case '.':
/* eof */
break;
case ':':
case '-':
util_do_command ("%s", &buf[3]);
break;
case '?':
/* escape help */
break;
case 'A':
printf ("%s", (util_find_env("Sign"))->value);
break;
case 'a':
printf ("%s", (util_find_env("sign"))->value);
break;
case 'b':
bcc = realloc (bcc, (strlen(bcc) + strlen(buf) - 1) *
sizeof(char));
strcat (bcc, ", ");
strcat (bcc, &buf[3]);
break;
case 'c':
cc = realloc (cc, (strlen(cc) + strlen(buf) - 1) *
sizeof (char));
strcat (cc, ", ");
strcat (cc, &buf[3]);
break;
case 'd':
{
FILE *dead = fopen (getenv("DEAD"), "r");
char c;
while ((c = fgetc(dead)))
fputc (c, file);
fclose (dead);
}
break;
case 'e':
fclose (file);
stdout = ostdout;
util_do_command ("!%s %s", getenv("EDITOR"), filename);
file = fopen (filename, "a");
stdout = file;
break;
case 'f':
util_do_command ("print %s", &buf[3]);
break;
case 'F':
util_do_command ("Print %s", &buf[3]);
break;
case 'h':
/* reget Bcc, Cc, To, and Subject */
break;
case 'i':
fprintf (file, "%s", (util_find_env(&buf[3]))->value);
break;
case 'm':
/* quote messages */
break;
case 'M':
/* same as m with no headers ignored */
break;
case 'p':
fclose (file);
stdout = ostdout;
if (/* numlines (filename) > */ util_getlines())
util_do_command ("!%s %s", getenv("PAGER"), filename);
else
/* dump filename */;
file = fopen (filename, "a");
stdout = file;
break;
case 'q':
fclose (file);
rename (filename, getenv("DEAD"));
util_do_command ("quit");
break;
case 'r':
case '<':
/* read in a file */
break;
case 's':
free (subj);
subj = strdup (&buf[3]);
break;
case 't':
to = realloc (to, (strlen(to) + strlen(buf) - 1) *
sizeof (char));
strcat (to, ", ");
strcat (to, &buf[3]);
break;
case 'v':
fclose (file);
stdout = ostdout;
util_do_command ("!%s %s", getenv("VISUAL"), filename);
file = fopen (filename, "a");
stdout = file;
break;
case 'w':
{
FILE *f2 = fopen (&buf[3], "a");
/* read this file and output to f2 */
fclose (f2);
}
break;
case 'x':
util_do_command ("quit");
break;
case '|':
/* pipe to &buf[3] */
break;
default:
fprintf (stderr, "Unknown escape %c\n", buf[0]);
break;
}
stdout = ostdout;
}
else
fprintf (file, "%s", buf);
fflush (file);
/* free (buf); */
}
/* read in filename, send it */
return 1;
}
......
......@@ -44,26 +44,22 @@ mail_shell (int argc, char **argv)
int pid = fork ();
if (pid == 0)
{
const char *shell = getenv ("SHELL");
const char **argvec;
char *buf = NULL;
if (shell == NULL)
shell = "/bin/sh";
/* 1(shell) + 1 (-c) + 1(arg) + 1 (null) = 4 */
argvec = malloc (4 * (sizeof (char *)));
argcv_string (argc-1, &argv[1], &buf);
argvec[0] = shell;
argvec[0] = getenv("SHELL");
argvec[1] = "-c";
argvec[2] = buf;
argvec[3] = NULL;
/* why does this complain if argvec[2] is in the path but not
fully qualified ? */
execvp (shell, argvec);
execvp (argvec[0], argvec);
free (buf); /* Being cute, nuke it when finish testing. */
free (argvec);
return 1;
......@@ -78,10 +74,7 @@ mail_shell (int argc, char **argv)
}
else
{
char *shell = getenv ("SHELL");
if (!shell)
shell = strdup ("/bin/sh");
return util_do_command ("shell %s", shell);
return util_do_command ("shell %s", getenv("SHELL"));
}
return 1;
}
......
......@@ -90,5 +90,9 @@ const struct mail_command_entry mail_command_table[] = {
{ "+", "+", mail_next, "+ [message]" },
{ "|", "|", mail_pipe, "| [[msglist] command]" },
{ "-", "-", mail_previous, "- [message]" },
{ 0, 0, 0, 0,}
{ 0, 0, 0, 0}
};
const struct mail_command_entry mail_escape_table[] = {
{0, 0, 0, 0}
};
......
......@@ -382,11 +382,7 @@ util_stripwhite (char *string)
int
util_getcols (void)
{
int columns = 80;
char *col = getenv ("COLUMNS");
if (col)
columns = strtol (col, NULL, 10);
return columns;
return strtol (getenv("COLUMNS"), NULL, 10);
}
/*
......@@ -395,11 +391,7 @@ util_getcols (void)
int
util_getlines (void)
{
int lines = 24;
char *lin = getenv ("LINES");
if (lin)
lines = strtol (lin, NULL, 10);
return lines;
return strtol (getenv("LINES"), NULL, 10);
}
/*
......
......@@ -29,11 +29,8 @@ mail_visual (int argc, char **argv)
else
{
char *file = tempnam(getenv("TMPDIR"), "mu");
char *editor = getenv ("VISUAL");
if (!editor)
editor = strdup ("vi");
util_do_command ("copy %s", file);
util_do_command ("shell %s %s", editor, file);
util_do_command ("shell %s %s", getenv("VISUAL"), file);
remove (file);
free (file);
return 0;
......