]> andersk Git - moira.git/blob - server/mr_util.c
punt mrgdb
[moira.git] / server / mr_util.c
1 /* $Id$
2  *
3  * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
4  * For copying and distribution information, please see the file
5  * <mit-copyright.h>.
6  */
7
8 #include <mit-copyright.h>
9 #include "mr_server.h"
10
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 RCSID("$Header$");
17
18 extern char *whoami;
19
20 char *requote(char *buf, char *cp, int len)
21 {
22   int count = 0;
23   unsigned char c;
24   if (len <= 2)
25     return buf;
26   *buf++ = '"';
27   count++;
28   len--;
29   for (; (count < 258) && (len > 1) && (c = *cp); cp++, --len, ++count)
30     {
31       if (c == '\\' || c == '"')
32         *buf++ = '\\';
33       if (isprint(c))
34         *buf++ = c;
35       else
36         {
37           sprintf(buf, "\\%03o", c);
38           buf = strchr(buf, '\0');
39         }
40     }
41   if (len > 1)
42     {
43       *buf++ = '"';
44       count++;
45       len--;
46     }
47   if (len > 1)
48     *buf = '\0';
49   return buf;
50 }
51
52 void log_args(char *tag, int version, int argc, char **argv)
53 {
54   char buf[BUFSIZ];
55   int i;
56   char *bp;
57
58   i = strlen(tag);
59   sprintf(buf, "%s[%d]: ", tag, version);
60   for (bp = buf; *bp; bp++)
61     ;
62
63   for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++)
64     {
65       if (i != 0)
66         {
67           *bp++ = ',';
68           *bp++ = ' ';
69         }
70       bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
71     }
72   *bp = '\0';
73   com_err(whoami, 0, "%s", buf);
74 }
75
76 void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
77 {
78   extern client *cur_client;
79
80   if (whoami)
81     {
82       fputs(whoami, stderr);
83       if (cur_client)
84         fprintf(stderr, "[#%d]", cur_client->id);
85       fputs(": ", stderr);
86     }
87   if (code) {
88     fputs(error_message(code), stderr);
89     fputs(" ", stderr);
90   }
91   if (fmt)
92     vfprintf(stderr, fmt, pvar);
93   putc('\n', stderr);
94 }
95
96
97 /* mr_trim_args: passed an argument vector, it will trim any trailing
98  * spaces on the args by writing a null into the string.  If an argument
99  * appears to be binary instead of ASCII, it will not be trimmed.
100  */
101
102 int mr_trim_args(int argc, char **argv)
103 {
104   char **arg;
105   unsigned char *p, *lastch;
106
107   for (arg = argv; argc--; arg++)
108     {
109       for (lastch = p = (unsigned char *) *arg; *p; p++)
110         {
111           /* If any byte in the string has the high bit set, assume
112            * that it is binary and we do not want to trim it.
113            * Setting p = lastch will cause us not to trim the string
114            * when we break out of this inner loop.
115            */
116           if (*p >= 0x80)
117             {
118               p = lastch;
119               break;
120             }
121           if (!isspace(*p))
122             lastch = p;
123         }
124       if (p != lastch)
125         {
126           if (isspace(*lastch))
127             *lastch = '\0';
128           else
129             if (*(++lastch))
130               *lastch = '\0';
131         }
132     }
133   return 0;
134 }
135
136
137 /* returns a copy of the argv and all of it's strings */
138
139 char **mr_copy_args(char **argv, int argc)
140 {
141   char **a;
142   int i;
143
144   a = malloc(argc * sizeof(char *));
145   if (!a)
146     return a;
147   for (i = 0; i < argc; i++)
148     a[i] = strdup(argv[i]);
149   return a;
150 }
151
152
153 /* malloc or die! */
154 void *xmalloc(size_t bytes)
155 {
156   void *buf = malloc(bytes);
157
158   if (buf)
159     return buf;
160
161   critical_alert("moirad", "Out of memory");
162   exit(1);
163 }
164
165 void *xrealloc(void *ptr, size_t bytes)
166 {
167   void *buf = realloc(ptr, bytes);
168
169   if (buf)
170     return buf;
171
172   critical_alert("moirad", "Out of memory");
173   exit(1);
174 }
This page took 0.054105 seconds and 5 git commands to generate.