X-Git-Url: http://andersk.mit.edu/gitweb/moira.git/blobdiff_plain/6a6547f0a4f97e0dd5ab11c0f95a47014465c598..5eaef52092da48d922a6b37cf48bb7c5104d0841:/server/mr_util.c diff --git a/server/mr_util.c b/server/mr_util.c index 29de3f72..9b9e67d4 100644 --- a/server/mr_util.c +++ b/server/mr_util.c @@ -4,136 +4,149 @@ * $Header$ * * Copyright (C) 1987 by the Massachusetts Institute of Technology - * + * 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 #include +#include extern char *whoami; -char * -requote(buf, cp, len) - char *buf; - register char *cp; +char *requote(char *buf, register char *cp, int len) { - register int count = 0; - register unsigned char c; - if (len <= 2) return buf; - *buf++ = '"'; count++; len--; - for(; (count < 40) && (len > 1) && (c = *cp); - cp++, --len, ++count) { - if (c == '\\' || c == '"') *buf++ = '\\'; - if (isprint(c)) *buf++ = c; - else { - sprintf(buf, "\\%03o", c); - buf = index(buf, '\0'); - } - } - if (len > 1) { *buf++ = '"'; count++; len--; } - if (len > 3 && count >= 40) { - *buf++ = '.'; count++; len--; - *buf++ = '.'; count++; len--; - *buf++ = '.'; count++; len--; + register int count = 0; + register unsigned char c; + if (len <= 2) + return buf; + *buf++ = '"'; + count++; + len--; + for (; (count < 258) && (len > 1) && (c = *cp); cp++, --len, ++count) + { + if (c == '\\' || c == '"') + *buf++ = '\\'; + if (isprint(c)) + *buf++ = c; + else + { + sprintf(buf, "\\%03o", c); + buf = strchr(buf, '\0'); } - if (len > 1) *buf = '\0'; - return buf; + } + if (len > 1) + { + *buf++ = '"'; + count++; + len--; + } + if (len > 1) + *buf = '\0'; + return buf; } -log_args(tag, version, argc, argv) - char *tag; - int version; - int argc; - char **argv; +void log_args(char *tag, int version, int argc, char **argv) { - char buf[BUFSIZ]; - register int i; - 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++ = ','; - *bp++ = ' '; - } - bp = requote(bp, argv[i], (buf - bp) + 1024); + char buf[BUFSIZ]; + register int i; + register char *bp; + + i = strlen(tag); + sprintf(buf, "%s[%d]: ", tag, version); + for (bp = buf; *bp; bp++) + ; + + for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++) + { + if (i != 0) + { + *bp++ = ','; + *bp++ = ' '; } - *bp = '\0'; - com_err(whoami, 0, "%s", buf); + bp = requote(bp, argv[i], (buf - bp) + BUFSIZ); + } + *bp = '\0'; + com_err(whoami, 0, "%s", buf); } - -void sms_com_err(whoami, code, fmt, pvar) - char *whoami; - int code; - char *fmt; - caddr_t pvar; + +void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar) { - extern char *error_message(); - extern client *cur_client; - - if (whoami) { - fputs(whoami, stderr); - if (cur_client) fprintf(stderr, "[#%d]", cur_client->id); - fputs(": ", stderr); - } - if (code) { - fputs(error_message(code), stderr); - } - if (fmt) { - _doprnt(fmt, pvar, stderr); - } - putc('\n', stderr); + extern client *cur_client; + + if (whoami) + { + fputs(whoami, stderr); + if (cur_client) + fprintf(stderr, "[#%d]", cur_client->id); + fputs(": ", stderr); + } + if (code) + fputs(error_message(code), stderr); + if (fmt) + _doprnt(fmt, pvar, stderr); + putc('\n', stderr); } -/* sms_trim_args: passed an argument vector, it will trim any trailing - * spaces on the args by writing a null into the string. +/* 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 sms_trim_args(argc, argv) -int argc; -char **argv; +int mr_trim_args(int argc, char **argv) { - register char **arg; - register char *p, *lastch; + register char **arg; + register unsigned char *p, *lastch; - for (arg = argv; argc--; arg++) { - for (lastch = p = *arg; *p; p++) + 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; - if (p != lastch) { - if (isspace(*lastch)) - *lastch = 0; - else - *(++lastch) = 0; + } + if (p != lastch) + { + if (isspace(*lastch)) + *lastch = '\0'; + else + if (*(++lastch)) + *lastch = '\0'; } } - return(0); + return 0; } -trim(s) -register char *s; +/* returns a copy of the argv and all of it's strings */ + +char **mr_copy_args(char **argv, int argc) { - register char *p; - - for (p = s; *s; s++) - if (*s != ' ') - p = s; - if (p != s) { - if (*p == ' ') - *p = 0; - else - p[1] = 0; - } + char **a; + int i; + + a = malloc(argc * sizeof(char *)); + if (!a) + return a; + for (i = 0; i < argc; i++) + a[i] = strsave(argv[i]); + return a; }