Commit 0d950198 0d950198b2895710984dd1b58948cd2993bee4fc by Alain Magloire

implemented "top", "delete", "undelete" "!"(bang) and "edit"

a little cleanup.  Fixme talk to Jakob about the redundoncy.
lib/argcv.c new function argcv_string().
1 parent c4d88d2b
/* argcv.c - simple functions for parsing input based on whitespace
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -36,11 +36,11 @@ argcv_get (const char *command, int *argc, char ***argv)
return 1;
*argc = 1;
for (i = 0; i < len; i++)
if (command[i] == ' ')
(*argc)++;
*argv = malloc ((*argc + 1) * sizeof (char *));
for (i = 0; i <= len; i++)
......@@ -72,3 +72,45 @@ argcv_free (int argc, char **argv)
free (argv);
return 1;
}
/* Take a argv an make string separated by ' '. */
int
argcv_string (int argc, char **argv, char **pstring)
{
int i;
size_t len;
char *buffer;
/* No need. */
if (pstring == NULL)
return 1;
buffer = malloc (1);
if (buffer == NULL)
return 1;
*buffer = '\0';
for (len = i = 0; i < argc; i++)
{
len += strlen (argv[i] + 2);
buffer = realloc (buffer, len);
if (buffer == NULL)
return 1;
if (i != 0)
strcat (buffer, " ");
strcat (buffer, argv[i]);
}
/* Strip off trailing space. */
if (*buffer != '\0')
{
while (buffer[strlen (buffer) - 1] == ' ')
{
buffer[strlen (buffer) - 1] = '\0';
}
}
if (pstring)
*pstring = buffer;
return 0;
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -35,12 +35,12 @@ extern "C" {
# endif
#endif /*__P */
int argcv_get __P((const char *command, int *argc, char ***argv));
int argcv_free __P((int argc, char **argv));
extern int argcv_get __P ((const char *command, int *argc, char ***argv));
extern int argcv_string __P ((int argc, char **argv, char **string));
extern int argcv_free __P ((int argc, char **argv));
#ifdef __cplusplus
}
#endif
#endif /* _ARGCV_H */
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -24,17 +24,40 @@
int
mail_bang (int argc, char **argv)
{
if (!fork ())
int pid = fork ();
if (pid == 0)
{
char *path = getenv ("SHELL");
if (path == NULL)
path = strdup ("/bin/sh");
execv (path, &path);
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 *)));
/* Nuke the ! */
/* FIXME: should they do that upstairs ? */
if (argv[0][0] == '!')
argv[0][0] = ' ';
argcv_string (argc, argv, &buf);
argvec[0] = shell;
argvec[1] = "-c";
argvec[2] = buf;
argvec[3] = NULL;
execv (shell, argvec);
free (buf); /* Being cute, nuke it when finish testing. */
free (argvec);
return 1;
}
else
else if (pid > 0)
{
wait(NULL);
while (waitpid(pid, NULL, 0) == -1)
/* do nothing */;
return 0;
}
return 1;
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -26,7 +26,18 @@ mail_delete (int argc, char **argv)
{
if (argc > 1)
return util_msglist_command (mail_delete, argc, argv);
else if (/* mailbox_delete (mbox, list[cursor]) == */ 0)
return 0;
else
{
message_t msg;
attribute_t attr;
if (mailbox_get_message (mbox, cursor, &msg) != 0)
{
fprintf (stderr, "Meessage %d does not exist\n", cursor);
return 1;
}
message_get_attribute (msg, &attr);
attribute_set_deleted (attr);
return 0;
}
return 1;
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -21,9 +21,65 @@
* e[dit] [msglist]
*/
/* FIXME: Can this be done by simply calling:
copy [msglist] tempfile
! editor tempfile
*/
int
mail_edit (int argc, char **argv)
{
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
return 1;
message_t msg;
mailbox_t mbx;
int *msglist = NULL;
char *filename;
char *cmdbuf;
const char *tmpdir;
const char *editor;
int num, i;
/* Get a temp filename. */
tmpdir = getenv ("TMPDIR");
if (tmpdir == NULL)
tmpdir = "/tmp"; /* FIXME: use _PATH_TMP */
filename = tempnam (tmpdir, "mu");
if (filename == NULL)
{
fprintf (stderr, "Unable to create temp file\n");
return 1;
}
mailbox_create (&mbx, filename, 0);
mailbox_open (mbx, MU_STREAM_WRITE | MU_STREAM_CREAT);
num = util_expand_msglist (argc, argv, &msglist);
if (num > 0)
{
for (i = 0; i < num; i++)
{
mailbox_get_message (mbox, msglist[i], &msg);
mailbox_append_message (mbx, msg);
}
}
else
{
mailbox_get_message (mbox, cursor, &msg);
mailbox_append_message (mbx, msg);
}
/* Build the command buffer: ! " " editor " " filename \0 */
editor = getenv ("EDITOR");
if (editor == NULL)
editor = "/bin/vi";
/* ! + editor + space + filename = len */
cmdbuf = calloc (strlen (editor) + strlen (filename) + 4, sizeof (char));
sprintf (cmdbuf, "!%s %s", editor, filename);
util_do_command (cmdbuf);
mailbox_close (mbx);
mailbox_destroy (&mbx);
remove (filename);
free (filename);
free (cmdbuf);
free (msglist);
return 0;
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -19,11 +19,40 @@
/*
* to[p] [msglist]
* FIXME: Need to check for toplines variable value, how ?
*/
int
mail_top (int argc, char **argv)
{
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
if (argc > 1)
return util_msglist_command (mail_top, argc, argv);
else
{
message_t msg;
stream_t stream;
int toplines = 5; /* FIXME: Use variable TOPLINES. */
off_t off = 0;
size_t n = 0;
char buf[BUFSIZ];
int status;
if (mailbox_get_message (mbox, cursor, &msg) != 0)
{
fprintf (stderr, "Could not read message %d\n", cursor);
return 1;
}
message_get_stream (msg, &stream);
while (toplines--)
{
status = stream_readline (stream, buf, sizeof (buf), off, &n);
if (status != 0 || n == 0)
break;
printf ("%s", buf);
off += n;
}
return 0;
}
return 1;
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -24,6 +24,21 @@
int
mail_undelete (int argc, char **argv)
{
printf ("Function not implemented in %s line %d\n", __FILE__, __LINE__);
if (argc > 1)
return util_msglist_command (mail_undelete, argc, argv);
else
{
message_t msg;
attribute_t attr;
if (mailbox_get_message (mbox, cursor, &msg) != 0)
{
fprintf (stderr, "Meessage %d does not exist\n", cursor);
return 1;
}
message_get_attribute (msg, &attr);
if (attribute_is_deleted (attr))
attribute_unset_deleted (attr);
return 0;
}
return 1;
}
......