header.c 10.7 KB
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2007, 2009 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 3, 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., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <mailutils/mailutils.h>

char *file;
mu_header_t header;
mu_iterator_t iterator;

char *ps[] = { "> ", ". " };
int interactive;

static void
prompt(int l)
{
  if (interactive)
    {
      printf ("%s", ps[l]);
      fflush (stdout);
    }
}

static int
load_file (const char *name)
{
  struct stat st;
  size_t nread;
  char *buf;
  FILE *fp;
  int status;
  
  if (stat (name, &st))
    {
      mu_error ("cannot stat %s: %s", name, mu_strerror (errno));
      return 1;
    }

  buf = malloc (st.st_size + 2);
  if (!buf)
    {
      mu_error ("not enough memory");
      return 1;
    }

  fp = fopen (name, "r");
  if (!fp)
    {
      mu_error ("cannot open file %s: %s", name, mu_strerror (errno));
      free (buf);
      return 1;
    }
  
  nread = fread (buf, 1, st.st_size, fp);
  fclose (fp);
  if (nread != st.st_size)
    {
      mu_error ("short read on file %s", name);
      free (buf);
      return 1;
    }

  buf[st.st_size] = '\n';
  buf[st.st_size+1] = 0;
  status = mu_header_create (&header, buf, st.st_size + 1, NULL);
  free (buf);
  if (status)
    {
      mu_error ("cannot create header: %s", mu_strerror (status));
      return 1;
    }
  return 0;
}

unsigned line_num = 0;

static int
check_args (char const *cmdname, int argc, int amin, int amax)
{
  if (argc < amin)
    {
      mu_error ("%u: %s: too few arguments",
	       line_num, cmdname);
      return 1;
    }
  if (amax > 0 && argc > amax)
    {
      mu_error ("%u: %s: too many arguments",
		line_num, cmdname);
      return 1;
    }      
  return 0;
}

void
cmd_quit (int argc, char **argv)
{
  exit (0);
}

void
cmd_load (int argc, char **argv)
{
  if (check_args (argv[0], argc, 2, 2))
    return;
  mu_header_destroy (&header, NULL);
  load_file (argv[1]);
}

void
cmd_free (int argc, char **argv)
{
  if (check_args (argv[0], argc, 1, 1))
    return;
  mu_iterator_destroy (&iterator);
  mu_header_destroy (&header, NULL);
}

void
cmd_print (int argc, char **argv)
{
  char *fn;
  int num = 1;
  int status;
  const char *str;
  
  if (check_args (argv[0], argc, 2, 3))
    return;
  fn = argv[1];
  if (argc == 3)
    num = atoi (argv[2]);

  status = mu_header_sget_value_n (header, fn, num, &str);
  if (status == 0)
    printf ("%s: %s\n", fn, str);
  else
    mu_error ("%u: %s", line_num, mu_strerror (status));
}

void
cmd_dump (int argc, char **argv)
{
  mu_off_t off = 0;
  size_t n;
  mu_stream_t stream;
  char buf[512];
  int status;
  
  if (check_args (argv[0], argc, 1, 2))
    return;

  if (argc == 2)
    off = strtoul (argv[1], NULL, 0);

  status = mu_header_get_stream (header, &stream);
  if (status)
    {
      mu_error ("%u: cannot get stream: %s", line_num, mu_strerror (status));
      return;
    }

  status = mu_stream_seek (stream, off, SEEK_SET);
  if (status)
    {
      mu_error ("%u: cannot seek: %s", line_num, mu_strerror (status));
      return;
    }

  while (mu_stream_sequential_read (stream, buf, sizeof buf, &n) == 0
	 && n > 0)
    {
      fwrite (buf, 1, n, stdout);
    }
}  

void
cmd_remove (int argc, char **argv)
{
  char *fn;
  int num = 1;
  int status;
  
  if (check_args (argv[0], argc, 2, 3))
    return;
  fn = argv[1];
  if (argc == 3)
    num = atoi (argv[2]);
  status = mu_header_remove (header, fn, num);
  if (status)
    mu_error ("%u: %s: %s", line_num, argv[0], mu_strerror (status));
}

/* insert header value [ref [num] [before|after] [replace]] */
void
cmd_insert (int argc, char **argv)
{
  int status;
  int flags = 0;
  char *ref = NULL;
  int num = 1;
  int n;
  
  if (check_args (argv[0], argc, 3, 7))
    return;

  if (argc >= 4)
    {
      ref = argv[3];
      n = 4;
      if (n < argc)
	{
	  char *p;
	  int tmp;
	  
	  tmp = strtoul(argv[4], &p, 0);
	  if (*p == 0)
	    {
	      num = tmp;
	      n++;
	    }

	  for (; n < argc; n++)
	    {
	      if (strcmp(argv[n], "before") == 0)
		flags |= MU_HEADER_BEFORE;
	      else if (strcmp(argv[n], "after") == 0)
		;
	      else if (strcmp(argv[n], "replace") == 0)
		flags |= MU_HEADER_REPLACE;
	      else
		{
		  mu_error("%u: %s: unknown option", line_num, argv[4]);
		  return;
		}
	    }
	}
    }
  status = mu_header_insert (header, argv[1], argv[2],
			     ref, num, flags);
  if (status)
    mu_error ("%u: %s: %s", line_num, argv[0], mu_strerror (status));
}

void
cmd_write (int argc, char **argv)
{
  char buf[512];
  mu_stream_t str;
  int status;
  
  if (check_args (argv[0], argc, 1, 1))
    return;

  status = mu_header_get_stream (header, &str);
  if (status)
    {
      mu_error ("%u: cannot get stream: %s", line_num, mu_strerror (status));
      return;
    }
  printf("[reading headers; end with an empty line]\n");
  mu_stream_seek (str, 0, SEEK_SET);
  while (prompt (1), fgets(buf, sizeof buf, stdin))
    {
      mu_stream_sequential_write (str, buf, strlen (buf));
      if (buf[0] == '\n')
	break;
    }
}

void
cmd_iterate (int argc, char **argv)
{
  if (check_args (argv[0], argc, 1, 2))
    return;
  if (argc == 1)
    {
      mu_iterator_t itr;
      MU_ASSERT (mu_header_get_iterator (header, &itr));
      for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
	   mu_iterator_next (itr))
	{
	  const char *hdr, *val;
	  MU_ASSERT (mu_iterator_current_kv (itr,
					     (const void**)&hdr,
					     (void**)&val));
	  printf ("%s: %s\n", hdr, val);
	}
      mu_iterator_destroy (&itr);
    }
  else
    {
      const char *hdr, *val;

      if (!iterator)
	MU_ASSERT (mu_header_get_iterator (header, &iterator));

      if (strcmp (argv[1], "first") == 0 || strcmp (argv[1], "1") == 0)
	mu_iterator_first (iterator);
      else if (strcmp (argv[1], "next") == 0 || strcmp (argv[1], "n") == 0)
	{
	  mu_iterator_next (iterator);
	  if (mu_iterator_is_done (iterator))
	    {
	      printf ("Past end of headers. Use `itr first'.\n");
	      return;
	    }
	}

      MU_ASSERT (mu_iterator_current_kv (iterator,
					 (const void **)&hdr,
					 (void**)&val));
      printf ("%s: %s\n", hdr, val);
    }
}

void
cmd_readline (int argc, char **argv)
{
  char *buf;
  size_t size;
  mu_stream_t stream;
  size_t nbytes;
  
  if (check_args (argv[0], argc, 1, 2))
    return;
  size = atoi (argv[1]);
  buf = malloc (size);
  if (!buf)
    abort ();
  mu_header_get_stream (header, &stream);
  mu_stream_readline (stream, buf, size, 0, &nbytes);
  printf ("\"%*.*s\"", (int) nbytes, (int) nbytes, buf);
  free (buf);
}
  

struct cmdtab
{
  char *name;
  void (*fun) (int argc, char **argv);
  char *args;
  char *help;
};

static void cmd_help (int argc, char **argv);

static struct cmdtab cmdtab[] = {
  { "quit", cmd_quit, NULL, "quit the program" },
  { "load", cmd_load, "FILE", "read headers from the specified FILE" },
  { "free", cmd_free, NULL, "discard all headers" },
  { "print", cmd_print, "NAME [N]",
    "find and print the Nth (by default, 1st) instance of header named NAME" },
  { "dump", cmd_dump, NULL, "dump all headers on screen" },
  { "itr", cmd_iterate, "[first|1|next|n]", "iterate over headers" },
  { "readline", cmd_readline, "[SIZE]", "read line" },
  { "remove", cmd_remove, "NAME [N]",
    "remove the Nth (by default, 1st) instance of header named NAME" },
  { "insert", cmd_insert, "NAME VALUE [REF [NUM] [before|after] [replace]]",
    "insert new header" },
  { "write", cmd_write, NULL, "accept headers from raw stream" },
  { "help", cmd_help, "[COMMAND]", "print short usage message" },
  { NULL }
};

static struct cmdtab *
find_cmd (const char *name)
{
  struct cmdtab *p;
  for (p = cmdtab; p->name; p++)
    if (strcmp (p->name, name) == 0)
      return p;
  return NULL;
}

static void
format_help_str (int col, char *p)
{
  if (col > 31)
    col = 80;
  while (*p)
    {
      int len;
      char *q;

      if (*p == ' ' || *p == '\t')
	{
	  p++;
	  continue;
	}
      
      q = strchr (p, ' ');
      if (!q)
	len = strlen (p);
      else
	len = q - p;
      
      if (col + len > 80)
	{
	  fputc ('\n', stdout);
	  for (col = 0; col < 30; col++)
	    fputc (' ', stdout);
	}
      for (; len > 0; len--, p++, col++)
	fputc (*p, stdout);

      if (q)
	{
	  if (col < 80)
	    {
	      fputc (' ', stdout);
	      col++;
	    }
	  p++;
	}
    }
  fputc ('\n', stdout);
}
	  
      

void
cmd_help (int argc, char **argv)
{
  struct cmdtab *p;
  
  if (check_args (argv[0], argc, 1, 2))
    return;

  for (p = cmdtab; p->name; p++)
    {
      int col;
      
      col = printf ("%s", p->name);
      for (; col < 10; col++)
	fputc (' ', stdout);
      if (p->args)
	col += printf ("%s", p->args);
      for (; col < 30; col++)
	fputc (' ', stdout);
      format_help_str (col, p->help);
    }
}

int
docmd (int argc, char **argv)
{
  struct cmdtab *cmd = find_cmd (argv[0]);
  if (!cmd)
    {
      mu_error ("%u: unknown command %s", line_num, argv[0]);
      return 1;
    }
  else
    cmd->fun (argc, argv);
  return 0;
}

int
main (int argc, char **argv)
{
  int c;
  char buf[512];
  char **prevv;
  int prevc = 0;
  
  interactive = isatty (0);
  while ((c = getopt (argc, argv, "f:h")) != EOF)
    {
      switch (c)
	{
	case 'f':
	  file = optarg;
	  break;

	case 'h':
	  printf ("usage: header [-f file]\n");
	  exit (0);

	default:
	  exit (1);
	}
    }

  if (file)
    {
      if (load_file (file))
	exit (1);
    }
  else
    {
      int status = mu_header_create (&header, NULL, 0, NULL);
      if (status)
	{
	  mu_error ("cannot create header: %s", mu_strerror (status));
	  exit (1);
	}
    }
  
  while (prompt(0), fgets(buf, sizeof buf, stdin))
    {
      int c;
      char **v;
      int status;

      line_num++;
      status = mu_argcv_get (buf, NULL, "#", &c, &v);
      if (status)
	{
	  mu_error ("%u: cannot parse: %s",
		   line_num, mu_strerror (status));
	  continue;
	}

      if (c == 0)
	{
	  if (prevc)
	    docmd (prevc, prevv);
	  else
	    mu_argcv_free (c, v);
	}
      else
	{
	  docmd (c, v);
	  mu_argcv_free (prevc, prevv);
	  prevc = c;
	  prevv = v;
	}
    }
  exit (0);
}