]> andersk Git - moira.git/blobdiff - server/mr_util.c
Command line printer manipulation client, and build goo.
[moira.git] / server / mr_util.c
index 5c5894385766b8d3d0cb136d42ef50605bf0c4a0..2762bd702b938bd302bbccd628ee899a10104b51 100644 (file)
@@ -1,80 +1,83 @@
-/*
- *     $Source$
- *     $Author$
- *     $Header$
+/* $Id$
  *
- *     Copyright (C) 1987 by the Massachusetts Institute of Technology
- *     For copying and distribution information, please see the file
- *     <mit-copyright.h>.
+ * 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_mr_util_c = "$Header$";
-#endif lint
-
 #include <mit-copyright.h>
 #include "mr_server.h"
-#include <com_err.h>
+
 #include <ctype.h>
-#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
+RCSID("$Header$");
+
 extern char *whoami;
 
-char *requote(char *buf, char *cp, int len)
+char *requote(char *cp)
 {
-  int count = 0;
-  unsigned char c;
-  if (len <= 2)
-    return buf;
-  *buf++ = '"';
-  count++;
-  len--;
-  for (; (count < 258) && (len > 1) && (c = *cp); cp++, --len, ++count)
+  int len = 0;
+  char *out = xmalloc(4 * strlen(cp) + 3), *op = out;
+
+  *op++ = '"';
+  len++;
+  while (*cp)
     {
-      if (c == '\\' || c == '"')
-       *buf++ = '\\';
-      if (isprint(c))
-       *buf++ = c;
+      if (*cp == '\\' || *cp == '"')
+       {
+         *op++ = '\\';
+         len++;
+       }
+      if (isprint(*cp))
+       {
+         *op++ = *cp++;
+         len++;
+       }
       else
        {
-         sprintf(buf, "\\%03o", c);
-         buf = strchr(buf, '\0');
+         sprintf(op, "\\%03o", (unsigned char)*cp++);
+         op += 4;
+         len += 4;
        }
     }
-  if (len > 1)
-    {
-      *buf++ = '"';
-      count++;
-      len--;
-    }
-  if (len > 1)
-    *buf = '\0';
-  return buf;
+
+  strcpy(op, "\"");
+  len += 2;
+
+  out = realloc(out, len); /* shrinking, so can't fail */
+  return out;
 }
 
 void log_args(char *tag, int version, int argc, char **argv)
 {
-  char buf[BUFSIZ];
-  int i;
+  char *buf, **qargv;
+  int i, len;
   char *bp;
 
-  i = strlen(tag);
-  sprintf(buf, "%s[%d]: ", tag, version);
-  for (bp = buf; *bp; bp++)
-    ;
+  qargv = xmalloc(argc * sizeof(char *));
 
-  for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++)
+  for (i = len = 0; i < argc; i++)
     {
-      if (i != 0)
-       {
-         *bp++ = ',';
-         *bp++ = ' ';
-       }
-      bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
+      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]);
     }
-  *bp = '\0';
-  com_err(whoami, 0, "%s", buf);
+  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)
@@ -88,17 +91,18 @@ void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
        fprintf(stderr, "[#%d]", cur_client->id);
       fputs(": ", stderr);
     }
-  if (code)
+  if (code) {
     fputs(error_message(code), stderr);
+    fputs(" ", stderr);
+  }
   if (fmt)
-    _doprnt(fmt, pvar, stderr);
+    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.
+ * spaces on the args by writing a null into the string.
  */
 
 int mr_trim_args(int argc, char **argv)
@@ -110,16 +114,6 @@ int mr_trim_args(int argc, char **argv)
     {
       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;
        }
@@ -143,10 +137,43 @@ char **mr_copy_args(char **argv, int argc)
   char **a;
   int i;
 
-  a = malloc(argc * sizeof(char *));
-  if (!a)
-    return a;
+  a = xmalloc(argc * sizeof(char *));
   for (i = 0; i < argc; i++)
-    a[i] = strsave(argv[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);
+}
This page took 0.069111 seconds and 4 git commands to generate.