summaryrefslogtreecommitdiff
path: root/mh/ali.c
blob: abb0b28cbf0e700c8779fd41b5e50ca6b2354662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2003, 2004 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */

/* MH ali command */

#include <mh.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif
#include <sys/ioctl.h>
#include <sys/stat.h>

const char *program_version = "ali (" PACKAGE_STRING ")";
static char doc[] = N_("GNU MH ali\v"
"Options marked with `*' are not yet implemented.\n"
"Use -help to obtain the list of traditional MH options.");
static char args_doc[] = N_("aliases ...");

/* GNU options */
static struct argp_option options[] = {
  {"alias",  ARG_ALIAS, N_("FILE"), 0,
   N_("Use the additional alias FILE")},
  {"noalias", ARG_NOALIAS, NULL, 0,
   N_("Do not read the system alias file") },
  {"list", ARG_LIST, N_("BOOL"),  OPTION_ARG_OPTIONAL,
   N_("List each address on a separate line") },
  {"normalize", ARG_NORMALIZE, N_("BOOL"),  OPTION_ARG_OPTIONAL,
   N_("Try to determine the official hostname for each address") },
  {"user", ARG_USER, N_("BOOL"),  OPTION_ARG_OPTIONAL,
   N_("List the aliases that expand to given addresses") },
  {"license", ARG_LICENSE, 0,      0,
   N_("Display software license"), -1},
  { 0 }
};

/* Traditional MH options */
struct mh_option mh_option[] = {
  { "alias", 1, MH_OPT_ARG, "aliasfile" },
  { "noalias", 3, },
  { "list", 1, MH_OPT_BOOL },
  { "normalize", 3, MH_OPT_BOOL },
  { "user", 1, MH_OPT_BOOL },
  { 0 }
};

static int list_mode;
static int user_mode;
static int normalize_mode;
static int nolist_mode;

static int
opt_handler (int key, char *arg, void *unused, struct argp_state *state)
{
  switch (key)
    {
    case ARG_LICENSE:
      mh_license (argp_program_version);
      break;

    case ARG_ALIAS:
      mh_alias_read (arg, 1);
      break;

    case ARG_NOALIAS:
      nolist_mode = 1;
      break;
      
    case ARG_LIST:
      list_mode = is_true (arg);
      break;
	
    case ARG_NORMALIZE:
      normalize_mode = is_true (arg);
      break;

    case ARG_USER:
      user_mode = is_true (arg);
      break;

    default:
      return 1;
    }
  return 0;
}

static int
getcols ()
{
  struct winsize ws;

  ws.ws_col = ws.ws_row = 0;
  if ((ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) || ws.ws_row == 0)
    {
      const char *columns = getenv ("COLUMNS");
      if (columns)
	ws.ws_col = strtol (columns, NULL, 10);
    }

  if (ws.ws_col == 0)
    ws.ws_col = 80;
  return ws.ws_col;
}

static void
ali_print_name_list (list_t list, int off)
{
  iterator_t itr;
  char *item;
  
  list_get_iterator (list, &itr);
  
  if (list_mode)
    {
      iterator_first (itr);
      iterator_current (itr, (void **)&item);
      printf ("%s\n", item);
      for (iterator_next (itr); !iterator_is_done (itr); iterator_next (itr))
	{
	  int len;
	  iterator_current (itr, (void **)&item);
	  len = off + strlen (item);
	  printf ("%*.*s\n", len, len, item);
	}
    }
  else
    {
      int ncol = getcols ();
      int n = off;
      
      iterator_first (itr);

      for (;;)
	{
	  int len;

	  iterator_current (itr, (void **)&item);
	  len = strlen (item) + 2;
	  if (n + len > ncol)
	    n = printf ("\n ");

	  len = printf ("%s", item);
	  iterator_next (itr);
	  if (!iterator_is_done (itr))
	    len += printf (", ");
	  else
	    break;
	  n += len;
	}
      printf ("\n");
    }
  iterator_destroy (&itr);
}

static int
ali_print_alias (char *name, list_t alias, void *data ARG_UNUSED)
{
  int n;
  
  if (name)
    n = printf ("%s: ", name);
  else
    n = 0;

  ali_print_name_list (alias, n);
  return 0;
}

static void
ali_print_name (char *name)
{
  printf ("%s\n", name);
}

int
main (int argc, char **argv)
{
  int index;
  
  /* Native Language Support */
  mu_init_nls ();

  mu_argp_init (program_version, NULL);
  mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
		 opt_handler, NULL, &index);

  argc -= index;
  argv += index;

  if (!nolist_mode)
    mh_read_aliases ();

  if (!user_mode)
    {
      if (argc == 0)
	mh_alias_enumerate (ali_print_alias, NULL);
      else
	{
	  int i;
	  for (i = 0; i < argc; i++)
	    {
	      list_t al = NULL;
	      
	      if (mh_alias_get (argv[i], &al) == 0)
		{
		  ali_print_alias (NULL, al, NULL);
		  list_destroy (&al);
		}
	      else
		ali_print_name (argv[i]);
	    }
	}
    }
  else
    {
      if (argc == 0)
	{
	  mh_error ("List of addresses is not given");
	  exit (1);
	}
      else
	{
	  int i;
	  for (i = 0; i < argc; i++)
	    {
	      list_t nl = NULL;

	      if (mh_alias_get_alias (argv[i], &nl) == 0)
		{
		  ali_print_name_list (nl, 0);
		  list_destroy (&nl);
		}
	      else
		printf ("%s\n", argv[i]);
	    }
	}
    }
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.