1/* debug.c
2   UUCP debugging functions.
3
4   Copyright (C) 1991, 1992, 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#include <ctype.h>
28
29#include "uudefs.h"
30
31#if DEBUG > 1
32
33/* The debugging level.  */
34int iDebug;
35
36/* Parse a debugging string.  This may be a simple number, which sets
37   the given number of bits in iDebug, or it may be a series of single
38   letters.  */
39
40static const char * const azDebug_names[] = DEBUG_NAMES;
41
42int
43idebug_parse (z)
44     const char *z;
45{
46  char *zend;
47  int i, iret;
48  char *zcopy, *ztok;
49
50  if (strncasecmp (z, DEBUG_NONE, sizeof DEBUG_NONE - 1) == 0)
51    return 0;
52
53  i = (int) strtol ((char *) z, &zend, 0);
54  if (*zend == '\0')
55    {
56      if (i > 15)
57	i = 15;
58      else if (i < 0)
59	i = 0;
60      return (1 << i) - 1;
61    }
62
63  zcopy = zbufcpy (z);
64
65  iret = 0;
66
67  for (ztok = strtok (zcopy, ", \t");
68       ztok != NULL;
69       ztok = strtok ((char *) NULL, ", \t"))
70    {
71      if (strcasecmp (ztok, "all") == 0)
72	{
73	  iret = DEBUG_MAX;
74	  break;
75	}
76      for (i = 0; azDebug_names[i] != NULL; i++)
77	{
78	  if (strncasecmp (ztok, azDebug_names[i],
79			   strlen (azDebug_names[i])) == 0)
80	    {
81	      iret |= 1 << i;
82	      break;
83	    }
84	}
85      if (azDebug_names[i] == NULL)
86	ulog (LOG_ERROR, "Unrecognized debugging option \"%s\"",
87	      ztok);
88    }
89
90  ubuffree (zcopy);
91
92  return iret;
93}
94
95#endif /* DEBUG > 1 */
96
97/* A debugging routine used when displaying buffers.  */
98
99size_t
100cdebug_char (z, ichar)
101     char *z;
102     int ichar;
103{
104  char b;
105
106  if (isprint (BUCHAR (ichar))
107      && ichar != '\"'
108      && ichar != '\\')
109    {
110      *z++ = (char) ichar;
111      *z = '\0';
112      return 1;
113    }
114
115  *z++ = '\\';
116
117  switch (ichar)
118    {
119    case '\n':
120      b = 'n';
121      break;
122    case '\r':
123      b = 'r';
124      break;
125    case '\"':
126      b = '\"';
127      break;
128    case '\\':
129      b = '\\';
130      break;
131    default:
132      sprintf (z, "%03o", (unsigned int) BUCHAR (ichar));
133      return strlen (z) + 1;
134    }
135
136  *z++ = b;
137  *z = '\0';
138  return 2;
139}
140
141#if DEBUG > 1
142
143/* Display a buffer when debugging.  */
144
145void
146udebug_buffer (zhdr, zbuf, clen)
147     const char *zhdr;
148     const char *zbuf;
149     size_t clen;
150{
151  char *z, *zalc;
152  size_t i;
153
154  zalc = zbufalc (clen * 4 + 1);
155
156  z = zalc;
157  for (i = 0; i < clen && i < 80; i++)
158    z += cdebug_char (z, zbuf[i]);
159  if (i < clen)
160    {
161      *z++ = '.';
162      *z++ = '.';
163      *z++ = '.';
164    }
165  *z = '\0';
166
167  ulog (LOG_DEBUG, "%s %lu \"%s\"", zhdr, (unsigned long) clen, zalc);
168
169  ubuffree (zalc);
170}
171
172#endif
173