function vp_prints splited in vp_prints and vp_prints_value
[freeradius.git] / src / lib / print.c
1 /*
2  * print.c      Routines to print stuff.
3  *
4  * Version:     $Id$
5  *
6  */
7
8 static const char rcsid[] = "$Id$";
9
10 #include        "autoconf.h"
11
12 #include        <stdio.h>
13 #include        <stdlib.h>
14 #include        <sys/types.h>
15 #include        <time.h>
16 #include        <pwd.h>
17 #include        <ctype.h>
18 #include        <string.h>
19
20 #include        "libradius.h"
21
22 /*
23  *      Convert a string to something printable.
24  *      The output string has to be _at least_ 4x the size
25  *      of the input string!
26  */
27 void librad_safeprint(char *in, int inlen, char *out, int outlen)
28 {
29         unsigned char   *str = (unsigned char *)in;
30         int             done = 0;
31         int             sp = 0;
32
33         if (inlen < 0) inlen = strlen(str);
34
35         while (inlen-- > 0 && (done + 3) < outlen) {
36                 /*
37                  *      Hack: never print trailing zero.
38                  *      Some clients send strings with an off-by-one
39                  *      length (confused with strings in C).
40                  */
41                 if (inlen == 0 && *str == 0)
42                         break;
43
44                 sp = 0;
45
46                 switch (*str) {
47                         case '\\':
48                                 sp = '\\';
49                                 break;
50                         case '\r':
51                                 sp = 'r';
52                                 break;
53                         case '\n':
54                                 sp = 'n';
55                                 break;
56                         case '\t':
57                                 sp = 't';
58                                 break;
59                         default:
60                                 if (*str < 32 || (*str >= 128)){
61                                         sprintf(out, "\\%03o", *str);
62                                         done += 4;
63                                         out  += 4;
64                                 } else {
65                                         *out++ = *str;
66                                         done++;
67                                 }
68                 }
69                 if (sp) {
70                         *out++ = '\\';
71                         *out++ = sp;
72                         done += 2;
73                 }
74                 str++;
75         }
76         *out = 0;
77 }
78
79
80 /*
81  *  Print one value into a string.
82  *  delimitst will define if strings and dates will be delimited by '"'
83  */
84 int vp_prints_value(char * out,int outlen, VALUE_PAIR *vp,int delimitst)
85 {
86     DICT_VALUE  *v;
87     char        buf[1024];
88     char        *a;
89     time_t      t;
90
91     out[0] = 0;
92     if (!vp) return 0;
93
94     switch (vp->type) {
95         case PW_TYPE_STRING:
96             if (vp->attribute == PW_NAS_PORT_ID)
97                 a = vp->strvalue;
98             else {
99                 if (delimitst) {
100                   buf[0] = '"';
101                   librad_safeprint(vp->strvalue, vp->length,
102                       buf + 1, sizeof(buf) - 2);
103                   strcat(buf, "\"");
104                 } else {
105                   librad_safeprint(vp->strvalue, vp->length,
106                       buf, sizeof(buf));
107                 }
108
109                 a = buf;
110             }
111             break;
112         case PW_TYPE_INTEGER:
113             if ((v = dict_valbyattr(vp->attribute, vp->lvalue))
114                 != NULL)
115                 a = v->name;
116             else {
117                 sprintf(buf, "%d", vp->lvalue);
118                 a = buf;
119             }
120             break;
121         case PW_TYPE_DATE:
122             t = vp->lvalue;
123             if (delimitst) {
124               strftime(buf, sizeof(buf), "\"%b %e %Y\"",gmtime(&t));
125             } else {
126               strftime(buf, sizeof(buf), "%b %e %Y",gmtime(&t));
127             }
128             a = buf;
129             break;
130         case PW_TYPE_IPADDR:
131             if (vp->strvalue[0])
132                 a = vp->strvalue;
133             else
134                 a = ip_ntoa(NULL, vp->lvalue);
135             break;
136         case PW_TYPE_ABINARY:
137 #ifdef ASCEND_BINARY
138           a = buf;
139           print_abinary(vp, buf, sizeof(buf));
140           break;
141 #endif
142         case PW_TYPE_OCTETS:
143           strcpy(buf, "0x");
144           a = buf + 2;
145           for (t = 0; t < vp->length; t++) {
146             sprintf(a, "%02x", vp->strvalue[t]);
147             a += 2;
148           }
149           a = buf;
150           break;
151
152         default:
153             a = "UNKNOWN-TYPE";
154             break;
155     }
156     strncpy(out, a, outlen);
157     out[outlen - 1] = 0;
158
159     return strlen(out);
160 }
161
162
163 /*
164  *      Print one attribute and value into a string.
165  */
166 int vp_prints(char *out, int outlen, VALUE_PAIR *vp)
167 {
168         int             len;
169
170     out[0] = 0;
171     if (!vp) return 0;
172
173         if (strlen(vp->name) + 3 > outlen) {
174                 return 0;
175         }
176
177         sprintf(out, "%s = ", vp->name);
178         len = strlen(out);
179     vp_prints_value(out+len,outlen-len,vp,1);
180
181     return strlen(out);
182 }
183
184
185 /*
186  *      Print one attribute and value.
187  */
188 void vp_print(FILE *fp, VALUE_PAIR *vp)
189 {
190         char    buf[1024];
191
192         vp_prints(buf, sizeof(buf), vp);
193         fputs(buf, fp);
194 }
195
196
197 /*
198  *      Print a whole list of attributes, indented by a TAB
199  *      and with a newline at the end.
200  */
201 void vp_printlist(FILE *fp, VALUE_PAIR *vp)
202 {
203         for (; vp; vp = vp->next) {
204                 fprintf(fp, "\t");
205                 vp_print(fp, vp);
206                 fprintf(fp, "\n");
207         }
208 }
209