-/*
- * $Source$
- * $Author$
- * $Header$
+/* $Id$
*
- * Copyright (C) 1987 by the Massachusetts Institute of Technology
- *
- * $Log$
- * Revision 1.10 1987-08-04 01:54:47 wesommer
- * Changed messages.
- *
- * Revision 1.9 87/08/04 01:50:13 wesommer
- * Rearranged messages.
- *
- * Revision 1.8 87/07/29 16:02:48 wesommer
- * Use unsigned char rather than char to prevent sign extension
- * problem.
- *
- * Revision 1.7 87/07/14 00:39:47 wesommer
- * Changed interface to log_args.
- *
- * Revision 1.6 87/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
- *
+ * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
*/
-#ifndef lint
-static char *rcsid_sms_util_c = "$Header$";
-#endif lint
-
-#include "sms_server.h"
+#include <mit-copyright.h>
+#include "mr_server.h"
#include <ctype.h>
-#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+RCSID("$Header$");
extern char *whoami;
-char *
-requote(buf, cp, len)
- char *buf;
- register char *cp;
+char *requote(char *cp)
{
- 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');
- }
+ int len = 0;
+ char *out = xmalloc(4 * strlen(cp) + 3), *op = out;
+
+ *op++ = '"';
+ len++;
+ while (*cp)
+ {
+ if (*cp == '\\' || *cp == '"')
+ {
+ *op++ = '\\';
+ len++;
+ }
+ if (isprint(*cp))
+ {
+ *op++ = *cp++;
+ len++;
}
- if (len > 1) { *buf++ = '"'; count++; len--; }
- if (len > 3 && count >= 40) {
- *buf++ = '.'; count++; len--;
- *buf++ = '.'; count++; len--;
- *buf++ = '.'; count++; len--;
+ else
+ {
+ sprintf(op, "\\%03o", (unsigned char)*cp++);
+ op += 4;
+ len += 4;
}
- if (len > 1) *buf = '\0';
- return buf;
+ }
+
+ strcpy(op, "\"");
+ len += 2;
+
+ out = realloc(out, len); /* shrinking, so can't fail */
+ return out;
}
-log_args(tag, argc, argv)
- char *tag;
- int argc;
- char **argv;
+void log_args(char *tag, int version, int argc, char **argv)
{
- char buf[BUFSIZ];
- register int i;
- register char *bp = buf;
-
- i = strlen(tag);
- bcopy(tag, bp, i+1);
- bp += i;
- *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);
- }
- *bp = '\0';
- com_err(whoami, 0, "%s", buf);
+ 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 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;
-
- struct iovec strings[7];
-
- if (whoami) {
- fputs(whoami, stderr);
- if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
- fputs(": ", stderr);
- }
- if (code) {
- fputs(error_message(code), 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);
+ 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.
+ */
+
+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 (!isspace(*p))
+ lastch = p;
}
- if (fmt) {
- _doprnt(fmt, pvar, stderr);
+ if (p != lastch)
+ {
+ if (isspace(*lastch))
+ *lastch = '\0';
+ else
+ if (*(++lastch))
+ *lastch = '\0';
}
- putc('\n', stderr);
+ }
+ 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(whoami, "moirad", "Out of memory");
+ exit(1);
+}
+
+void *xrealloc(void *ptr, size_t bytes)
+{
+ void *buf = realloc(ptr, bytes);
+
+ if (buf)
+ return buf;
+
+ critical_alert(whoami, "moirad", "Out of memory");
+ exit(1);
+}
+
+char *xstrdup(char *str)
+{
+ char *buf = strdup(str);
+
+ if (buf)
+ return buf;
+
+ critical_alert(whoami, "moirad", "Out of memory");
+ exit(1);
}