X-Git-Url: http://andersk.mit.edu/gitweb/moira.git/blobdiff_plain/69f63d24d57f9c68690e5dd4534f352c047c48ae..245f8a92a68015a715f4821b4a32b4c39a00d8d7:/server/mr_util.c diff --git a/server/mr_util.c b/server/mr_util.c index 833831ed..9926446e 100644 --- a/server/mr_util.c +++ b/server/mr_util.c @@ -4,36 +4,20 @@ * $Header$ * * Copyright (C) 1987 by the Massachusetts Institute of Technology - * - * $Log$ - * Revision 1.6 1987-07-06 16:09:07 wesommer - * Only print ... if the string is too long.. - * - * Revision 1.5 87/06/30 20:05:52 wesommer - * Added range checking. - * - * Revision 1.4 87/06/21 16:42:19 wesommer - * Performance work, rearrangement of include files. - * - * Revision 1.3 87/06/04 01:35:28 wesommer - * Added better logging routines. - * - * Revision 1.2 87/06/03 16:08:07 wesommer - * Fixes for lint. - * - * Revision 1.1 87/06/02 20:07:32 wesommer - * Initial revision - * + * For copying and distribution information, please see the file + * . */ #ifndef lint -static char *rcsid_sms_util_c = "$Header$"; +static char *rcsid_mr_util_c = "$Header$"; #endif lint -#include "sms_server.h" +#include +#include "mr_server.h" #include #include +#include extern char *whoami; @@ -43,7 +27,7 @@ requote(buf, cp, len) register char *cp; { register int count = 0; - register char c; + register unsigned char c; if (len <= 2) return buf; *buf++ = '"'; count++; len--; for(; (count < 40) && (len > 1) && (c = *cp); @@ -65,14 +49,20 @@ requote(buf, cp, len) return buf; } -log_args(argc, argv) +log_args(tag, version, argc, argv) + char *tag; + int version; int argc; char **argv; { char buf[BUFSIZ]; register int i; - register char *bp = buf; + register char *bp; + i = strlen(tag); + sprintf(buf, "%s[%d]: ", tag, version); + for (bp = buf; *bp; bp++); + for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) { if (i != 0) { *bp++ = ','; @@ -81,49 +71,83 @@ log_args(argc, argv) bp = requote(bp, argv[i], (buf - bp) + 1024); } *bp = '\0'; - com_err(whoami, 0, buf); + com_err(whoami, 0, "%s", buf); } -void sms_com_err(whoami, code, message) +void mr_com_err(whoami, code, fmt, pvar) char *whoami; int code; - char *message; + char *fmt; + caddr_t pvar; { extern char *error_message(); extern client *cur_client; - struct iovec strings[7]; - char buf[32]; - if (cur_client) - (void) sprintf(buf, "[#%d]", cur_client->id); - else buf[0]='\0'; - - strings[1].iov_base = buf; - strings[1].iov_len = strlen(buf); - - strings[0].iov_base = whoami; if (whoami) { - strings[0].iov_len = strlen(whoami); - strings[2].iov_base = ": "; - strings[2].iov_len = 2; - } else { - strings[0].iov_len = 0; - strings[2].iov_base = " "; - strings[2].iov_len = 1; + fputs(whoami, stderr); + if (cur_client) fprintf(stderr, "[#%d]", cur_client->id); + fputs(": ", stderr); } if (code) { - register char *errmsg = error_message(code); - strings[3].iov_base = errmsg; - strings[3].iov_len = strlen(errmsg); - strings[4].iov_base = " "; - strings[4].iov_len = 1; - } else { - strings[3].iov_len = 0; - strings[4].iov_len = 0; + fputs(error_message(code), stderr); + } + if (fmt) { + _doprnt(fmt, pvar, stderr); + } + putc('\n', stderr); +} + + +/* mr_trim_args: passed an argument vector, it will trim any trailing + * spaces on the args by writing a null into the string. If an argument + * appears to be binary instead of ASCII, it will not be trimmed. + */ + +int mr_trim_args(argc, argv) +int argc; +char **argv; +{ + register char **arg; + register unsigned char *p, *lastch; + + for (arg = argv; argc--; arg++) { + for (lastch = p = (unsigned char *) *arg; *p; p++) { + /* If any byte in the string has the high bit set, assume + * that it is binary and we do not want to trim it. + * Setting p = lastch will cause us not to trim the string + * when we break out of this inner loop. + */ + if (*p >= 0x80) { + p = lastch; + break; + } + if (!isspace(*p)) + lastch = p; } - strings[5].iov_base = message; - strings[5].iov_len = strlen(message); - strings[6].iov_base = "\n"; - strings[6].iov_len = 1; - (void) writev(2, strings, 7); + if (p != lastch) { + if (isspace(*lastch)) + *lastch = 0; + else + *(++lastch) = 0; + } + } + return(0); +} + + +/* returns a copy of the argv and all of it's strings */ + +char **mr_copy_args(argv, argc) +char **argv; +int argc; +{ + char **a; + int i; + + a = (char **) malloc(argc * sizeof(char *)); + if (a == 0) + return(a); + for (i = 0; i < argc; i++) + a[i] = strsave(argv[i]); + return(a); }