]>
Commit | Line | Data |
---|---|---|
a3cf6921 | 1 | /* |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
c801de4c | 7 | * For copying and distribution information, please see the file |
8 | * <mit-copyright.h>. | |
a3cf6921 | 9 | */ |
10 | ||
11 | #ifndef lint | |
d548a4e7 | 12 | static char *rcsid_mr_util_c = "$Header$"; |
a3cf6921 | 13 | #endif lint |
14 | ||
c801de4c | 15 | #include <mit-copyright.h> |
d548a4e7 | 16 | #include "mr_server.h" |
a3cf6921 | 17 | |
18 | #include <ctype.h> | |
229feb8a | 19 | #include <strings.h> |
c93d352e | 20 | #include <sys/types.h> |
229feb8a | 21 | |
22 | extern char *whoami; | |
23 | ||
229feb8a | 24 | char * |
5aa981ce | 25 | requote(buf, cp, len) |
229feb8a | 26 | char *buf; |
a3cf6921 | 27 | register char *cp; |
28 | { | |
5aa981ce | 29 | register int count = 0; |
caeb1b25 | 30 | register unsigned char c; |
5aa981ce | 31 | if (len <= 2) return buf; |
32 | *buf++ = '"'; count++; len--; | |
33 | for(; (count < 40) && (len > 1) && (c = *cp); | |
34 | cp++, --len, ++count) { | |
229feb8a | 35 | if (c == '\\' || c == '"') *buf++ = '\\'; |
36 | if (isprint(c)) *buf++ = c; | |
37 | else { | |
38 | sprintf(buf, "\\%03o", c); | |
39 | buf = index(buf, '\0'); | |
40 | } | |
a3cf6921 | 41 | } |
5aa981ce | 42 | if (len > 1) { *buf++ = '"'; count++; len--; } |
69f63d24 | 43 | if (len > 3 && count >= 40) { |
5aa981ce | 44 | *buf++ = '.'; count++; len--; |
45 | *buf++ = '.'; count++; len--; | |
46 | *buf++ = '.'; count++; len--; | |
47 | } | |
48 | if (len > 1) *buf = '\0'; | |
229feb8a | 49 | return buf; |
a3cf6921 | 50 | } |
5aa981ce | 51 | |
2423a5db | 52 | log_args(tag, version, argc, argv) |
ac65c1d1 | 53 | char *tag; |
2423a5db | 54 | int version; |
229feb8a | 55 | int argc; |
56 | char **argv; | |
57 | { | |
58 | char buf[BUFSIZ]; | |
59 | register int i; | |
2423a5db | 60 | register char *bp; |
229feb8a | 61 | |
ac65c1d1 | 62 | i = strlen(tag); |
2423a5db | 63 | sprintf(buf, "%s[%d]: ", tag, version); |
64 | for (bp = buf; *bp; bp++); | |
ac65c1d1 | 65 | |
5aa981ce | 66 | for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) { |
229feb8a | 67 | if (i != 0) { |
68 | *bp++ = ','; | |
69 | *bp++ = ' '; | |
70 | } | |
5aa981ce | 71 | bp = requote(bp, argv[i], (buf - bp) + 1024); |
229feb8a | 72 | } |
73 | *bp = '\0'; | |
5cc7d26c | 74 | com_err(whoami, 0, "%s", buf); |
229feb8a | 75 | } |
76 | ||
d548a4e7 | 77 | void mr_com_err(whoami, code, fmt, pvar) |
a3cf6921 | 78 | char *whoami; |
79 | int code; | |
b4182127 | 80 | char *fmt; |
81 | caddr_t pvar; | |
a3cf6921 | 82 | { |
83 | extern char *error_message(); | |
84 | extern client *cur_client; | |
85 | ||
a3cf6921 | 86 | if (whoami) { |
b4182127 | 87 | fputs(whoami, stderr); |
88 | if (cur_client) fprintf(stderr, "[#%d]", cur_client->id); | |
89 | fputs(": ", stderr); | |
a3cf6921 | 90 | } |
91 | if (code) { | |
b4182127 | 92 | fputs(error_message(code), stderr); |
93 | } | |
94 | if (fmt) { | |
95 | _doprnt(fmt, pvar, stderr); | |
a3cf6921 | 96 | } |
b4182127 | 97 | putc('\n', stderr); |
a3cf6921 | 98 | } |
2423a5db | 99 | |
100 | ||
d548a4e7 | 101 | /* mr_trim_args: passed an argument vector, it will trim any trailing |
e2fe4db4 | 102 | * spaces on the args by writing a null into the string. If an argument |
103 | * appears to be binary instead of ASCII, it will not be trimmed. | |
2423a5db | 104 | */ |
105 | ||
d548a4e7 | 106 | int mr_trim_args(argc, argv) |
2423a5db | 107 | int argc; |
108 | char **argv; | |
109 | { | |
110 | register char **arg; | |
30517356 | 111 | register unsigned char *p, *lastch; |
2423a5db | 112 | |
113 | for (arg = argv; argc--; arg++) { | |
245f8a92 | 114 | for (lastch = p = (unsigned char *) *arg; *p; p++) { |
e2fe4db4 | 115 | /* If any byte in the string has the high bit set, assume |
116 | * that it is binary and we do not want to trim it. | |
117 | * Setting p = lastch will cause us not to trim the string | |
118 | * when we break out of this inner loop. | |
119 | */ | |
120 | if (*p >= 0x80) { | |
121 | p = lastch; | |
122 | break; | |
123 | } | |
124 | if (!isspace(*p)) | |
125 | lastch = p; | |
126 | } | |
2423a5db | 127 | if (p != lastch) { |
128 | if (isspace(*lastch)) | |
129 | *lastch = 0; | |
130 | else | |
131 | *(++lastch) = 0; | |
132 | } | |
133 | } | |
134 | return(0); | |
135 | } | |
71007162 | 136 | |
137 | ||
138 | /* returns a copy of the argv and all of it's strings */ | |
139 | ||
140 | char **mr_copy_args(argv, argc) | |
141 | char **argv; | |
142 | int argc; | |
143 | { | |
144 | char **a; | |
145 | int i; | |
146 | ||
147 | a = (char **) malloc(argc * sizeof(char *)); | |
148 | if (a == 0) | |
149 | return(a); | |
150 | for (i = 0; i < argc; i++) | |
151 | a[i] = strsave(argv[i]); | |
152 | return(a); | |
153 | } |