summaryrefslogtreecommitdiff
path: root/mail/echo.c
blob: 9b19d7e0ce65b74e4becd10e1305b2d0ac8e36ca (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2001, 2002 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  */

#include "mail.h"

/*
 * ec[ho] string ...
 */

static int echo (char *s);

int
mail_echo (int argc, char **argv)
{
  int i = 0;
  if (argc > 1)
    {
      for (i = 1; i < argc - 1; i++)
	{
	  echo (argv[i]);
	  fputc (' ', ofile);
	}
      /* Last argument.  */
      if (echo(argv[argc - 1]) == 0)
	fputc ('\n', ofile);
    }
  return 0;
}

/* Cumbersome switch for checking escape char '\'
   if present replace with appropriately.
   Return of 1 means to not print newline.  */
static int
echo (char *s)
{
  int process_escape = 0;
  int c;

  if (s == NULL)
    return 0;

  for (; (c = *s) != 0; s++)
    {
      if (process_escape)
	{
	  switch (c)
	    {
	      /* \a Bell.  */
	    case 'a':
	      c = '\a';
	      break;

	      /* \b Backspace.  */
	    case 'b':
	      c = '\b';
	      break;

	      /* \c means not to print ending newline.  */
	      /* Bail out and tell the caller not to print newline.  */
	    case 'c':
	      return 1;
	      break;

	      /* \f Formfeed.  */
	    case 'f':
	      c = '\f';
	      break;

	      /* \n Newline.  */
	    case 'n':
	      c = '\n';
	      break;

	      /* \r Carriage return.  */
	    case 'r':
	      c = '\r';
	      break;

	      /* \t Tab. */
	    case 't':
	      c = '\t';
	      break;

	      /* \v Vertical Tab.  */
	    case 'v':
	      c = '\v';
	      break;

	      /* Escape sequence.  */
	    case '\\':
	      c = '\\';
	      break;

	      /* \0x99 for example, let strtol() handle it.  */
	      /* WARNING: Side effects because of strtol().  */
	    case '0':
	      {
		long number = strtol (s, &s, 0);
		switch (number)
		  {
		  case LONG_MIN:
		  case LONG_MAX:
		    /* if (errno == ERANGE) */
		    /*  fputc (c, ofile); */
		    break;

		  default:
		    fprintf (ofile, "%ld", number);
		    s--;
		    continue;
		  }
	      }
	      break;

	      /* Can not be here.  */
	    case '\0':
	      return 0;
	      break;

	      /* \\ means \ It was not an escape char.  */
	    default:
	      fputc ('\\', ofile);

	    }
	  process_escape =0;
	}
      else if (c == '\\') /* Find the escape char, go back and process.  */
	{
	  process_escape = 1;
	  continue;
	}
      fputc (c, ofile);
    }
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.