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