]>
Commit | Line | Data |
---|---|---|
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 *cp) | |
21 | { | |
22 | int len = 0; | |
23 | char *out = xmalloc(4 * strlen(cp) + 3), *op = out; | |
24 | ||
25 | *op++ = '"'; | |
26 | len++; | |
27 | while (*cp) | |
28 | { | |
29 | if (*cp == '\\' || *cp == '"') | |
30 | { | |
31 | *op++ = '\\'; | |
32 | len++; | |
33 | } | |
34 | if (isprint(*cp)) | |
35 | { | |
36 | *op++ = *cp++; | |
37 | len++; | |
38 | } | |
39 | else | |
40 | { | |
41 | sprintf(op, "\\%03o", (unsigned char)*cp++); | |
42 | op += 4; | |
43 | len += 4; | |
44 | } | |
45 | } | |
46 | ||
47 | strcpy(op, "\""); | |
48 | len += 2; | |
49 | ||
50 | out = realloc(out, len); /* shrinking, so can't fail */ | |
51 | return out; | |
52 | } | |
53 | ||
54 | void log_args(char *tag, int version, int argc, char **argv) | |
55 | { | |
56 | char *buf, **qargv; | |
57 | int i, len; | |
58 | char *bp; | |
59 | ||
60 | qargv = xmalloc(argc * sizeof(char *)); | |
61 | ||
62 | for (i = len = 0; i < argc; i++) | |
63 | { | |
64 | qargv[i] = requote(argv[i]); | |
65 | len += strlen(qargv[i]) + 2; | |
66 | } | |
67 | ||
68 | buf = xmalloc(len + 1); | |
69 | ||
70 | for (i = 0, *buf = '\0'; i < argc; i++) | |
71 | { | |
72 | if (i) | |
73 | strcat(buf, ", "); | |
74 | strcat(buf, qargv[i]); | |
75 | free(qargv[i]); | |
76 | } | |
77 | free(qargv); | |
78 | ||
79 | com_err(whoami, 0, "%s[%d]: %s", tag, version, buf); | |
80 | free(buf); | |
81 | } | |
82 | ||
83 | void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar) | |
84 | { | |
85 | extern client *cur_client; | |
86 | ||
87 | if (whoami) | |
88 | { | |
89 | fputs(whoami, stderr); | |
90 | if (cur_client) | |
91 | fprintf(stderr, "[#%d]", cur_client->id); | |
92 | fputs(": ", stderr); | |
93 | } | |
94 | if (code) { | |
95 | fputs(error_message(code), stderr); | |
96 | fputs(" ", stderr); | |
97 | } | |
98 | if (fmt) | |
99 | vfprintf(stderr, fmt, pvar); | |
100 | putc('\n', stderr); | |
101 | } | |
102 | ||
103 | ||
104 | /* mr_trim_args: passed an argument vector, it will trim any trailing | |
105 | * spaces on the args by writing a null into the string. If an argument | |
106 | * appears to be binary instead of ASCII, it will not be trimmed. | |
107 | */ | |
108 | ||
109 | int mr_trim_args(int argc, char **argv) | |
110 | { | |
111 | char **arg; | |
112 | unsigned char *p, *lastch; | |
113 | ||
114 | for (arg = argv; argc--; arg++) | |
115 | { | |
116 | for (lastch = p = (unsigned char *) *arg; *p; p++) | |
117 | { | |
118 | /* If any byte in the string has the high bit set, assume | |
119 | * that it is binary and we do not want to trim it. | |
120 | * Setting p = lastch will cause us not to trim the string | |
121 | * when we break out of this inner loop. | |
122 | */ | |
123 | if (*p >= 0x80) | |
124 | { | |
125 | p = lastch; | |
126 | break; | |
127 | } | |
128 | if (!isspace(*p)) | |
129 | lastch = p; | |
130 | } | |
131 | if (p != lastch) | |
132 | { | |
133 | if (isspace(*lastch)) | |
134 | *lastch = '\0'; | |
135 | else | |
136 | if (*(++lastch)) | |
137 | *lastch = '\0'; | |
138 | } | |
139 | } | |
140 | return 0; | |
141 | } | |
142 | ||
143 | ||
144 | /* returns a copy of the argv and all of it's strings */ | |
145 | ||
146 | char **mr_copy_args(char **argv, int argc) | |
147 | { | |
148 | char **a; | |
149 | int i; | |
150 | ||
151 | a = xmalloc(argc * sizeof(char *)); | |
152 | for (i = 0; i < argc; i++) | |
153 | a[i] = xstrdup(argv[i]); | |
154 | return a; | |
155 | } | |
156 | ||
157 | ||
158 | /* malloc or die! */ | |
159 | void *xmalloc(size_t bytes) | |
160 | { | |
161 | void *buf = malloc(bytes); | |
162 | ||
163 | if (buf) | |
164 | return buf; | |
165 | ||
166 | critical_alert("moirad", "Out of memory"); | |
167 | exit(1); | |
168 | } | |
169 | ||
170 | void *xrealloc(void *ptr, size_t bytes) | |
171 | { | |
172 | void *buf = realloc(ptr, bytes); | |
173 | ||
174 | if (buf) | |
175 | return buf; | |
176 | ||
177 | critical_alert("moirad", "Out of memory"); | |
178 | exit(1); | |
179 | } | |
180 | ||
181 | char *xstrdup(char *str) | |
182 | { | |
183 | char *buf = strdup(str); | |
184 | ||
185 | if (buf) | |
186 | return buf; | |
187 | ||
188 | critical_alert("moirad", "Out of memory"); | |
189 | exit(1); | |
190 | } |