Blame view

mail/source.c 1.99 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2001, 2002, 2007, 2010 Free Software Foundation,
   Inc.
4

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

10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12 13 14 15
   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
16
   along with GNU Mailutils; if not, write to the Free Software
17 18
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */
19 20 21

#include "mail.h"

22
static char *
23
source_readline (void *closure, int cont MU_ARG_UNUSED)
24 25 26 27
{
  FILE *fp = closure;
  size_t s = 0;
  char *buf = NULL;
28 29
  mu_debug_t debug;
  struct mu_debug_locus locus;
30 31 32
  
  if (getline (&buf, &s, fp) >= 0)
    {
33 34 35 36 37
      mu_rtrim_class (buf, MU_CTYPE_SPACE);

      mu_diag_get_debug (&debug);
      mu_debug_get_locus (debug, &locus);
      mu_debug_set_locus (debug, locus.file, locus.line + 1);
38 39
      return buf;
    }
40
  
41 42 43
  return NULL;
}
  
44 45 46 47 48 49 50
/*
 * so[urce] file
 */

int
mail_source (int argc, char **argv)
{
51 52
  FILE *fp;
  int save_term;
53
  mu_debug_t debug;
54 55 56
  
  if (argc != 2)
    {
57
      /* TRANSLATORS: 'source' is a command name. Do not translate it! */
58
      util_error (_("source requires a single argument"));
59 60 61 62 63
      return 1;
    }
  
  fp = fopen (argv[1], "r");
  if (!fp)
Jakob Kaivo authored
64
    {
65
      if (errno != ENOENT)
66
	util_error(_("Cannot open `%s': %s"), argv[1], strerror(errno));
67
      return 1;
Jakob Kaivo authored
68
    }
69

70 71
  save_term = interactive;
  interactive = 0;
72 73 74
  mu_diag_get_debug (&debug);
  mu_debug_set_locus (debug, argv[1], 0);
  mail_mainloop (source_readline, fp, 0);
75
  interactive = save_term;
76
  mu_debug_set_locus (debug, NULL, 0);
77 78
  fclose (fp);
  return 0;
79
}