]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * $Source$ | |
3 | * $Author$ | |
4 | * $Header$ | |
5 | * | |
6 | * Copyright (C) 1987 by the Massachusetts Institute of Technology | |
7 | * | |
8 | */ | |
9 | ||
10 | #ifndef lint | |
11 | static char *rcsid_sms_util_c = "$Header$"; | |
12 | #endif lint | |
13 | ||
14 | #include "sms_server.h" | |
15 | ||
16 | #include <ctype.h> | |
17 | #include <strings.h> | |
18 | #include <sys/types.h> | |
19 | ||
20 | extern char *whoami; | |
21 | ||
22 | char * | |
23 | requote(buf, cp, len) | |
24 | char *buf; | |
25 | register char *cp; | |
26 | { | |
27 | register int count = 0; | |
28 | register unsigned char c; | |
29 | if (len <= 2) return buf; | |
30 | *buf++ = '"'; count++; len--; | |
31 | for(; (count < 40) && (len > 1) && (c = *cp); | |
32 | cp++, --len, ++count) { | |
33 | if (c == '\\' || c == '"') *buf++ = '\\'; | |
34 | if (isprint(c)) *buf++ = c; | |
35 | else { | |
36 | sprintf(buf, "\\%03o", c); | |
37 | buf = index(buf, '\0'); | |
38 | } | |
39 | } | |
40 | if (len > 1) { *buf++ = '"'; count++; len--; } | |
41 | if (len > 3 && count >= 40) { | |
42 | *buf++ = '.'; count++; len--; | |
43 | *buf++ = '.'; count++; len--; | |
44 | *buf++ = '.'; count++; len--; | |
45 | } | |
46 | if (len > 1) *buf = '\0'; | |
47 | return buf; | |
48 | } | |
49 | ||
50 | log_args(tag, version, argc, argv) | |
51 | char *tag; | |
52 | int version; | |
53 | int argc; | |
54 | char **argv; | |
55 | { | |
56 | char buf[BUFSIZ]; | |
57 | register int i; | |
58 | register char *bp; | |
59 | ||
60 | i = strlen(tag); | |
61 | sprintf(buf, "%s[%d]: ", tag, version); | |
62 | for (bp = buf; *bp; bp++); | |
63 | ||
64 | for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) { | |
65 | if (i != 0) { | |
66 | *bp++ = ','; | |
67 | *bp++ = ' '; | |
68 | } | |
69 | bp = requote(bp, argv[i], (buf - bp) + 1024); | |
70 | } | |
71 | *bp = '\0'; | |
72 | com_err(whoami, 0, "%s", buf); | |
73 | } | |
74 | ||
75 | void sms_com_err(whoami, code, fmt, pvar) | |
76 | char *whoami; | |
77 | int code; | |
78 | char *fmt; | |
79 | caddr_t pvar; | |
80 | { | |
81 | extern char *error_message(); | |
82 | extern client *cur_client; | |
83 | ||
84 | if (whoami) { | |
85 | fputs(whoami, stderr); | |
86 | if (cur_client) fprintf(stderr, "[#%d]", cur_client->id); | |
87 | fputs(": ", stderr); | |
88 | } | |
89 | if (code) { | |
90 | fputs(error_message(code), stderr); | |
91 | } | |
92 | if (fmt) { | |
93 | _doprnt(fmt, pvar, stderr); | |
94 | } | |
95 | putc('\n', stderr); | |
96 | } | |
97 | ||
98 | ||
99 | /* sms_trim_args: passed an argument vector, it will trim any trailing | |
100 | * spaces on the args by writing a null into the string. | |
101 | */ | |
102 | ||
103 | int sms_trim_args(argc, argv) | |
104 | int argc; | |
105 | char **argv; | |
106 | { | |
107 | register char **arg; | |
108 | register char *p, *lastch; | |
109 | ||
110 | for (arg = argv; argc--; arg++) { | |
111 | for (lastch = p = *arg; *p; p++) | |
112 | if (!isspace(*p)) | |
113 | lastch = p; | |
114 | if (p != lastch) { | |
115 | if (isspace(*lastch)) | |
116 | *lastch = 0; | |
117 | else | |
118 | *(++lastch) = 0; | |
119 | } | |
120 | } | |
121 | return(0); | |
122 | } | |
123 | ||
124 | ||
125 | trim(s) | |
126 | register char *s; | |
127 | { | |
128 | register char *p; | |
129 | ||
130 | for (p = s; *s; s++) | |
131 | if (*s != ' ') | |
132 | p = s; | |
133 | if (p != s) { | |
134 | if (*p == ' ') | |
135 | *p = 0; | |
136 | else | |
137 | p[1] = 0; | |
138 | } | |
139 | } |