]> andersk Git - moira.git/blame - server/mr_util.c
Command line printer manipulation client, and build goo.
[moira.git] / server / mr_util.c
CommitLineData
7ac48069 1/* $Id$
a3cf6921 2 *
7ac48069 3 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
4 * For copying and distribution information, please see the file
5 * <mit-copyright.h>.
a3cf6921 6 */
7
c801de4c 8#include <mit-copyright.h>
d548a4e7 9#include "mr_server.h"
7ac48069 10
a3cf6921 11#include <ctype.h>
85330553 12#include <stdio.h>
7ac48069 13#include <stdlib.h>
03c05291 14#include <string.h>
229feb8a 15
7ac48069 16RCSID("$Header$");
17
229feb8a 18extern char *whoami;
19
e688520a 20char *requote(char *cp)
a3cf6921 21{
e688520a 22 int len = 0;
23 char *out = xmalloc(4 * strlen(cp) + 3), *op = out;
24
25 *op++ = '"';
26 len++;
27 while (*cp)
5eaef520 28 {
e688520a 29 if (*cp == '\\' || *cp == '"')
30 {
31 *op++ = '\\';
32 len++;
33 }
34 if (isprint(*cp))
35 {
36 *op++ = *cp++;
37 len++;
38 }
5eaef520 39 else
40 {
8192daa7 41 sprintf(op, "\\%03o", (unsigned char)*cp++);
e688520a 42 op += 4;
43 len += 4;
a3cf6921 44 }
5eaef520 45 }
e688520a 46
47 strcpy(op, "\"");
48 len += 2;
49
50 out = realloc(out, len); /* shrinking, so can't fail */
51 return out;
a3cf6921 52}
5aa981ce 53
5eaef520 54void log_args(char *tag, int version, int argc, char **argv)
229feb8a 55{
e688520a 56 char *buf, **qargv;
57 int i, len;
44d12d58 58 char *bp;
5eaef520 59
e688520a 60 qargv = xmalloc(argc * sizeof(char *));
5eaef520 61
e688520a 62 for (i = len = 0; i < argc; i++)
5eaef520 63 {
e688520a 64 qargv[i] = requote(argv[i]);
65 len += strlen(qargv[i]) + 2;
5eaef520 66 }
e688520a 67
68 buf = xmalloc(len + 1);
69
70 for (i = 0, *buf = '\0'; i < argc; i++)
71 {
72 if (i)
73 strcat(buf, ", ");
74 strcat(buf, qargv[i]);
75 free(qargv[i]);
76 }
77 free(qargv);
78
79 com_err(whoami, 0, "%s[%d]: %s", tag, version, buf);
80 free(buf);
229feb8a 81}
5eaef520 82
83void mr_com_err(const char *whoami, long code, const char *fmt, va_list pvar)
a3cf6921 84{
5eaef520 85 extern client *cur_client;
86
87 if (whoami)
88 {
89 fputs(whoami, stderr);
90 if (cur_client)
91 fprintf(stderr, "[#%d]", cur_client->id);
92 fputs(": ", stderr);
93 }
85330553 94 if (code) {
5eaef520 95 fputs(error_message(code), stderr);
85330553 96 fputs(" ", stderr);
97 }
5eaef520 98 if (fmt)
7ac48069 99 vfprintf(stderr, fmt, pvar);
5eaef520 100 putc('\n', stderr);
a3cf6921 101}
2423a5db 102
103
d548a4e7 104/* mr_trim_args: passed an argument vector, it will trim any trailing
788b1426 105 * spaces on the args by writing a null into the string.
2423a5db 106 */
107
5eaef520 108int mr_trim_args(int argc, char **argv)
2423a5db 109{
44d12d58 110 char **arg;
111 unsigned char *p, *lastch;
5eaef520 112
113 for (arg = argv; argc--; arg++)
114 {
115 for (lastch = p = (unsigned char *) *arg; *p; p++)
116 {
5eaef520 117 if (!isspace(*p))
118 lastch = p;
e2fe4db4 119 }
5eaef520 120 if (p != lastch)
121 {
122 if (isspace(*lastch))
123 *lastch = '\0';
124 else
125 if (*(++lastch))
126 *lastch = '\0';
2423a5db 127 }
128 }
5eaef520 129 return 0;
2423a5db 130}
71007162 131
132
133/* returns a copy of the argv and all of it's strings */
134
5eaef520 135char **mr_copy_args(char **argv, int argc)
71007162 136{
5eaef520 137 char **a;
138 int i;
139
e688520a 140 a = xmalloc(argc * sizeof(char *));
5eaef520 141 for (i = 0; i < argc; i++)
e688520a 142 a[i] = xstrdup(argv[i]);
5eaef520 143 return a;
71007162 144}
85330553 145
146
147/* malloc or die! */
148void *xmalloc(size_t bytes)
149{
150 void *buf = malloc(bytes);
151
152 if (buf)
153 return buf;
154
a816420b 155 critical_alert(whoami, "moirad", "Out of memory");
85330553 156 exit(1);
157}
158
159void *xrealloc(void *ptr, size_t bytes)
160{
161 void *buf = realloc(ptr, bytes);
162
163 if (buf)
164 return buf;
165
a816420b 166 critical_alert(whoami, "moirad", "Out of memory");
85330553 167 exit(1);
168}
e688520a 169
170char *xstrdup(char *str)
171{
172 char *buf = strdup(str);
173
174 if (buf)
175 return buf;
176
a816420b 177 critical_alert(whoami, "moirad", "Out of memory");
e688520a 178 exit(1);
179}
This page took 3.188725 seconds and 5 git commands to generate.