1/* quote.c
2   Quote a UUCP command.
3
4   Copyright (C) 2002 Ian Lance Taylor
5
6   This file is part of the Taylor UUCP package.
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License as
10   published by the Free Software Foundation; either version 2 of the
11   License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
21
22   The author of the program may be contacted at ian@airs.com.
23   */
24
25#include "uucp.h"
26
27#if USE_RCS_ID
28const char quote_rcsid[] = "$Id: quote.c,v 1.2 2002/03/05 19:10:42 ian Rel $";
29#endif
30
31#include "uudefs.h"
32
33/* Local functions.  */
34
35__inline__ static boolean fneeds_quotes P((const char *z));
36
37/* Return whether a string needs quotes.  We want to be conservative
38   here--we don't want to reject a string which would work with an
39   older UUCP version.  */
40
41__inline__
42static boolean
43fneeds_quotes (z)
44     const char *z;
45{
46  return z != NULL && z[strcspn (z, " \t\n")] != '\0';
47}
48
49/* Return whether a command needs quotes.  */
50
51boolean
52fcmd_needs_quotes (qcmd)
53     const struct scmd *qcmd;
54{
55  if (fneeds_quotes (qcmd->zfrom)
56      || fneeds_quotes (qcmd->zto)
57      || fneeds_quotes (qcmd->zuser)
58      || fneeds_quotes (qcmd->znotify))
59    return TRUE;
60
61  /* We don't check qcmd->zcmd.  It is already permitted to have
62     spaces, and uux will never generate a command with an embedded
63     newline.  */
64
65  return FALSE;
66}
67
68/* Quote the strings which appear in a UUCP command string.  Add 'q'
69   to the list of options.  This creates a new command in qnew, with
70   freshly allocated strings.  */
71
72void
73uquote_cmd (qorig, qnew)
74     const struct scmd *qorig;
75     struct scmd *qnew;
76{
77  qnew->bcmd = qorig->bcmd;
78  qnew->bgrade = qorig->bgrade;
79  qnew->pseq = qorig->pseq;
80  qnew->zfrom = zquote_cmd_string (qorig->zfrom, FALSE);
81  qnew->zto = zquote_cmd_string (qorig->zto, FALSE);
82  qnew->zuser = zquote_cmd_string (qorig->zuser, FALSE);
83
84  if (strchr (qorig->zoptions, 'q') != NULL)
85    qnew->zoptions = zbufcpy (qorig->zoptions);
86  else
87    {
88      size_t clen;
89      char *z;
90
91      clen = strlen (qorig->zoptions);
92      z = zbufalc (clen + 2);
93      memcpy (z, qorig->zoptions, clen);
94      z[clen] = 'q';
95      z[clen + 1] = '\0';
96      qnew->zoptions = z;
97    }
98
99  qnew->ztemp = zbufcpy (qorig->ztemp);
100  qnew->imode = qorig->imode;
101  qnew->znotify = zquote_cmd_string (qorig->znotify, FALSE);
102  qnew->cbytes = qorig->cbytes;
103
104  /* The zcmd field is never quoted.  */
105  qnew->zcmd = zbufcpy (qorig->zcmd);
106
107  qnew->ipos = qorig->ipos;
108}
109
110/* Free a command structure created by uquote_cmd.  */
111
112void
113ufree_quoted_cmd (qcmd)
114     struct scmd *qcmd;
115{
116  ubuffree ((char *) qcmd->zfrom);
117  ubuffree ((char *) qcmd->zto);
118  ubuffree ((char *) qcmd->zuser);
119  ubuffree ((char *) qcmd->ztemp);
120  ubuffree ((char *) qcmd->znotify);
121  ubuffree ((char *) qcmd->zcmd);
122  ubuffree ((char *) qcmd->zoptions);
123}
124