X-Git-Url: http://andersk.mit.edu/gitweb/moira.git/blobdiff_plain/5dbd09a0a1da6890396c5cdc73290d05d853ab82..cf8cdb24351f5749d7f0d526b2cc07de98acd355:/server/mr_util.c diff --git a/server/mr_util.c b/server/mr_util.c index c1bae1fd..8320877d 100644 --- a/server/mr_util.c +++ b/server/mr_util.c @@ -1,83 +1,190 @@ -/* - * $Source$ - * $Author$ - * $Header$ +/* $Id$ * - * Copyright (C) 1987 by the Massachusetts Institute of Technology - * - * $Log$ - * Revision 1.2 1987-06-03 16:08:07 wesommer - * Fixes for lint. - * - * Revision 1.1 87/06/02 20:07:32 wesommer - * Initial revision - * + * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology + * For copying and distribution information, please see the file + * . */ -#ifndef lint -static char *rcsid_sms_util_c = "$Header$"; -#endif lint - -#include "sms_private.h" -#include "sms_server.h" +#include +#include "mr_server.h" #include -#ifdef notdef -frequote(f, cp) - FILE *f; - register char *cp; +#include +#include +#include + +RCSID("$Header$"); + +extern char *whoami; + +char *requote(char *cp) { - register char c; - putc('"', f); - for( ; c= *cp; *cp++){ - if (c == '\\' || c == '"') putc('\\', f); - if (isprint(c)) putc(c, f); - else fprintf(f, "\\%03o", c); + int len = 0; + char *out = xmalloc(4 * strlen(cp) + 3), *op = out; + + *op++ = '"'; + len++; + while (*cp) + { + if (*cp == '\\' || *cp == '"') + { + *op++ = '\\'; + len++; } - putc('"', f); + if (isprint(*cp)) + { + *op++ = *cp++; + len++; + } + else + { + sprintf(op, "\\%03o", (unsigned char)*cp++); + op += 4; + len += 4; + } + } + + strcpy(op, "\""); + len += 2; + + out = realloc(out, len); /* shrinking, so can't fail */ + return out; } -#endif notdef -void sms_com_err(whoami, code, message) - char *whoami; - int code; - char *message; +void log_args(char *tag, int version, int argc, char **argv) { - 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; + char *buf, **qargv; + int i, len; + char *bp; + + qargv = xmalloc(argc * sizeof(char *)); + + for (i = len = 0; i < argc; i++) + { + qargv[i] = requote(argv[i]); + len += strlen(qargv[i]) + 2; + } + + buf = xmalloc(len + 1); + + for (i = 0, *buf = '\0'; i < argc; i++) + { + if (i) + strcat(buf, ", "); + strcat(buf, qargv[i]); + free(qargv[i]); + } + free(qargv); + + com_err(whoami, 0, "%s[%d]: %s", tag, version, buf); + free(buf); +} + +void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar) +{ + 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); + fputs(" ", stderr); + } + if (fmt) + vfprintf(stderr, fmt, pvar); + 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(int argc, char **argv) +{ + char **arg; + 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; } - 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; + if (p != lastch) + { + if (isspace(*lastch)) + *lastch = '\0'; + else + if (*(++lastch)) + *lastch = '\0'; } - 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); + } + return 0; +} + + +/* returns a copy of the argv and all of it's strings */ + +char **mr_copy_args(char **argv, int argc) +{ + char **a; + int i; + + a = xmalloc(argc * sizeof(char *)); + for (i = 0; i < argc; i++) + a[i] = xstrdup(argv[i]); + return a; +} + + +/* malloc or die! */ +void *xmalloc(size_t bytes) +{ + void *buf = malloc(bytes); + + if (buf) + return buf; + + critical_alert("moirad", "Out of memory"); + exit(1); +} + +void *xrealloc(void *ptr, size_t bytes) +{ + void *buf = realloc(ptr, bytes); + + if (buf) + return buf; + + critical_alert("moirad", "Out of memory"); + exit(1); +} + +char *xstrdup(char *str) +{ + char *buf = strdup(str); + + if (buf) + return buf; + + critical_alert("moirad", "Out of memory"); + exit(1); }