16 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
17 |
|
|
18 |
#include "guimb.h" |
#include "guimb.h" |
19 |
#include "getopt.h" |
#include <mu_argp.h> |
|
|
|
|
static char short_options[] = "c:de:f:g:hM:m:s:u::v{"; |
|
|
static struct option long_options[] = { |
|
|
{"code", required_argument, 0, 'c'}, |
|
|
{"debug", no_argument, 0, 'd'}, |
|
|
{"expression", required_argument, 0, 'e'}, |
|
|
{"file", required_argument, 0, 'f'}, |
|
|
{"help", no_argument, 0, 'h'}, |
|
|
{"guile-arg", required_argument, 0, 'g'}, |
|
|
{"maildir", required_argument, 0, 'm'}, |
|
|
{"mailbox", required_argument, 0, 'M'}, |
|
|
{"source", required_argument, 0, 's'}, |
|
|
{"user", optional_argument, 0, 'u'}, |
|
|
{"version", no_argument, 0, 'v'}, |
|
|
{0, 0, 0, 0} |
|
|
}; |
|
20 |
|
|
21 |
char *program_file; |
char *program_file; |
22 |
char *program_expr; |
char *program_expr; |
23 |
int debug_guile; |
int debug_guile; |
24 |
char *user_name; |
char *user_name; |
25 |
char *default_mailbox; |
char *default_mailbox; |
|
char *maildir = MU_PATH_MAILDIR; |
|
26 |
|
|
|
static void usage (void); |
|
27 |
char * who_am_i (); |
char * who_am_i (); |
28 |
|
|
29 |
static int g_size; |
static int g_size; |
48 |
g_argv[g_argc++] = arg; |
g_argv[g_argc++] = arg; |
49 |
} |
} |
50 |
|
|
51 |
|
static struct argp_option options[] = { |
52 |
|
{NULL, 0, NULL, 0, |
53 |
|
"The following switches stop argument processing, and pass all\n" |
54 |
|
"remaining arguments as the value of (command-line):", 1}, |
55 |
|
{"code", 'c', "EXPR", 0, "Execute given scheme expression.", 1}, |
56 |
|
{"source", 's', "PROGFILE", 0, |
57 |
|
"Load Scheme source code from PROGFILE, and exit", 1}, |
58 |
|
{NULL, 0, NULL, 0, |
59 |
|
"The following options do not change the order of options parsing:", 2}, |
60 |
|
{"expression", 'e', "EXPR", 0, "Execute given scheme expression.", 2}, |
61 |
|
{"file", 'f', "PROGFILE", 0, |
62 |
|
"Load Scheme source code from PROGFILE, and exit", 2}, |
63 |
|
{NULL, 0, NULL, 0, "Other options:", 3}, |
64 |
|
{"debug", 'd', NULL, 0, "Start with debugging evaluator and backtraces.", 3}, |
65 |
|
{"guile-arg", 'g', "ARG", 0, |
66 |
|
"Append ARG to the command line passed to Guile", 3}, |
67 |
|
{"mailbox", 'M', "NAME", 0, "Set default mailbox name", 3}, |
68 |
|
{"user", 'u', "NAME", OPTION_ARG_OPTIONAL, |
69 |
|
"Act as local MDA for user NAME", 3}, |
70 |
|
{0, 0, 0, 0} |
71 |
|
}; |
72 |
|
|
73 |
|
static error_t |
74 |
|
parse_opt (int key, char *arg, struct argp_state *state) |
75 |
|
{ |
76 |
|
switch (key) |
77 |
|
{ |
78 |
|
case 'c': |
79 |
|
program_expr = optarg; |
80 |
|
state->next = state->argc; |
81 |
|
break; |
82 |
|
|
83 |
|
case 's': |
84 |
|
program_file = optarg; |
85 |
|
state->next = state->argc; |
86 |
|
break; |
87 |
|
|
88 |
|
case 'f': |
89 |
|
program_file = optarg; |
90 |
|
break; |
91 |
|
|
92 |
|
case 'e': |
93 |
|
program_expr = optarg; |
94 |
|
break; |
95 |
|
|
96 |
|
case 'd': |
97 |
|
debug_guile = 1; |
98 |
|
break; |
99 |
|
|
100 |
|
case 'g': |
101 |
|
append_arg (optarg); |
102 |
|
break; |
103 |
|
|
104 |
|
case 'M': |
105 |
|
default_mailbox = optarg; |
106 |
|
break; |
107 |
|
|
108 |
|
case 'u': |
109 |
|
user_name = optarg ? optarg : who_am_i (); |
110 |
|
break; |
111 |
|
|
112 |
|
default: |
113 |
|
return ARGP_ERR_UNKNOWN; |
114 |
|
} |
115 |
|
return 0; |
116 |
|
} |
117 |
|
|
118 |
|
const char *argp_program_version = "guimb (" PACKAGE ") " VERSION; |
119 |
|
const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; |
120 |
|
static char doc[] = |
121 |
|
"GNU messages -- Process the contents of the specified mailboxes\n" |
122 |
|
"using a Scheme program or expression."; |
123 |
|
static char args_doc[] = "[mailbox...]"; |
124 |
|
|
125 |
|
static struct argp argp = { |
126 |
|
options, |
127 |
|
parse_opt, |
128 |
|
args_doc, |
129 |
|
doc, |
130 |
|
mu_common_argp_child, |
131 |
|
NULL, NULL |
132 |
|
}; |
133 |
|
|
134 |
|
|
135 |
int |
int |
136 |
main (int argc, char *argv[]) |
main (int argc, char *argv[]) |
137 |
{ |
{ |
138 |
int c; |
int c; |
|
int stop = 0; |
|
139 |
guimb_param_t param; |
guimb_param_t param; |
140 |
struct guimb_data gd; |
struct guimb_data gd; |
141 |
|
|
142 |
append_arg (""); |
append_arg (""); |
143 |
while (!stop |
mu_create_argcv (argc, argv, &argc, &argv); |
144 |
&& (c = getopt_long (argc, argv, short_options, long_options, NULL)) |
argp_parse (&argp, argc, argv, 0, &c, NULL); |
145 |
!= -1) |
|
146 |
switch (c) |
for (; c < argc; c++) |
147 |
{ |
append_arg (argv[c]); |
|
case 'c': |
|
|
program_expr = optarg; |
|
|
stop = 1; |
|
|
break; |
|
|
case 'd': |
|
|
debug_guile = 1; |
|
|
break; |
|
|
case 'f': |
|
|
program_file = optarg; |
|
|
break; |
|
|
case 'e': |
|
|
program_expr = optarg; |
|
|
break; |
|
|
case 'g': |
|
|
append_arg (optarg); |
|
|
break; |
|
|
case 'h': |
|
|
usage (); |
|
|
exit (0); |
|
|
case 'M': |
|
|
default_mailbox = optarg; |
|
|
break; |
|
|
case 'm': |
|
|
maildir = optarg; |
|
|
break; |
|
|
case 'u': |
|
|
user_name = optarg ? optarg : who_am_i (); |
|
|
break; |
|
|
case 's': |
|
|
program_file = optarg; |
|
|
stop = 1; |
|
|
break; |
|
|
case 'v': |
|
|
printf ("guimb (" PACKAGE " " VERSION ")\n"); |
|
|
exit (0); |
|
|
case '{': |
|
|
for (;; optind++) |
|
|
{ |
|
|
if (optind == argc) |
|
|
{ |
|
|
fprintf (stderr, "guimb: missing -}\n"); |
|
|
exit (1); |
|
|
} |
|
|
if (strcmp (argv[optind], "-}") == 0) |
|
|
break; |
|
|
append_arg (argv[optind]); |
|
|
} |
|
|
optind++; |
|
|
break; |
|
|
default: |
|
|
exit (1); |
|
|
} |
|
|
|
|
|
if (stop) |
|
|
for (; optind < argc; optind++) |
|
|
append_arg (argv[optind]); |
|
148 |
|
|
149 |
if (!user_name) |
if (!user_name) |
150 |
user_name = who_am_i (); |
user_name = who_am_i (); |
151 |
|
|
|
maildir = mu_normalize_maildir (maildir); |
|
|
if (!maildir) |
|
|
{ |
|
|
util_error ("Badly formed maildir: %s", maildir); |
|
|
exit (1); |
|
|
} |
|
|
|
|
152 |
if (program_file) |
if (program_file) |
153 |
g_argv[0] = program_file; |
g_argv[0] = program_file; |
154 |
else if (!program_expr) |
else if (!program_expr) |
155 |
{ |
{ |
156 |
usage (); |
mu_error ("At least one of -fecs must be used. Try guimb --help for more info"); |
157 |
exit (0); |
exit (0); |
158 |
} |
} |
159 |
|
|
228 |
return name; |
return name; |
229 |
} |
} |
230 |
|
|
|
static char usage_str[] = |
|
|
"Usage: guimb [OPTIONS] [-{ SCRIPT-OPTIONS -}] [MBOX ...]\n" |
|
|
"Process the contents of the specified mailboxes using a Scheme program\n" |
|
|
"or expression.\n" |
|
|
"Options are:\n\n" |
|
|
" -c, --code EXPR Execute given scheme expression.\n" |
|
|
" -s, --source PROGFILE Load Scheme source code from PROGFILE, and exit\n" |
|
|
"\nThe above switches stop argument processing, and pass all\n" |
|
|
"remaining arguments as the value of (command-line).\n" |
|
|
"\n" |
|
|
" -d, --debug Start with debugging evaluator and backtraces.\n" |
|
|
" -e, --expression EXPR Execute scheme expression.\n" |
|
|
" -f, --file PROGFILE Read program from PROGFILE.\n" |
|
|
" -g, --guile-arg ARG Append ARG to the command line passed to scheme\n" |
|
|
" program.\n" |
|
|
" -m, --mailbox MBOX Set default mailbox name.\n" |
|
|
" -u, --user NAME Act as local MDA for user NAME.\n" |
|
|
" -h, --help Display help message.\n" |
|
|
" -v, --version Display program version.\n" |
|
|
"\n" |
|
|
"Any arguments between -{ and -} are passed to the Scheme program verbatim.\n" |
|
|
"When both --file and --expression are specified, file is evaluated first.\n" |
|
|
"If no mailboxes are specified, the system mailbox of the current user is read.\n\n" |
|
|
"The semantics of the default mailbox depends on whether more mailbox\n" |
|
|
"arguments are specified in the command line. If they are, any messages\n" |
|
|
"that are not deleted after executing the script are appended to the default\n" |
|
|
"mailbox. Otherwise its contents is read, processed and *replaced* by\n" |
|
|
"messages that remain undeleted after executing the script.\n"; |
|
|
|
|
|
void |
|
|
usage () |
|
|
{ |
|
|
printf ("%s\n" "Report bugs to <bug-mailutils@gnu.org>\n", usage_str); |
|
|
} |
|