]> andersk Git - moira.git/blob - server/mr_util.c
af7e266f24b6a474ab560166762189e25a6ba218
[moira.git] / server / mr_util.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  */
10
11 #ifndef lint
12 static char *rcsid_mr_util_c = "$Header$";
13 #endif lint
14
15 #include <mit-copyright.h>
16 #include "mr_server.h"
17 #include <com_err.h>
18 #include <ctype.h>
19 #include <sys/types.h>
20 #include <string.h>
21
22 extern char *whoami;
23
24 char *
25 requote(buf, cp, len)
26         char *buf;
27         register char *cp;
28 {
29         register int count = 0;
30         register unsigned char c;
31         if (len <= 2) return buf;
32         *buf++ = '"'; count++; len--;
33         for(; (count < 258) && (len > 1) && (c = *cp);
34             cp++, --len, ++count) {
35                 if (c == '\\' || c == '"') *buf++ = '\\';
36                 if (isprint(c)) *buf++ = c;
37                 else {
38                         sprintf(buf, "\\%03o", c);
39                         buf = strchr(buf, '\0');
40                 }
41         }
42         if (len > 1) { *buf++ = '"'; count++; len--; }
43         if (len > 1) *buf = '\0';
44         return buf;
45 }
46
47 void log_args(tag, version, argc, argv)
48         char *tag;
49         int version;
50         int argc;
51         char **argv;
52 {
53         char buf[BUFSIZ];
54         register int i;
55         register char *bp;
56         
57         i = strlen(tag);
58         sprintf(buf, "%s[%d]: ", tag, version);
59         for (bp = buf; *bp; bp++);
60        
61         for (i = 0; i < argc && ((buf - bp) + BUFSIZ) > 2; i++) {
62                 if (i != 0) {
63                         *bp++ = ',';
64                         *bp++ = ' '; 
65                 }
66                 bp = requote(bp, argv[i], (buf - bp) + BUFSIZ);
67         }
68         *bp = '\0';
69         com_err(whoami, 0, "%s", buf);
70 }
71         
72 void mr_com_err(whoami, code, fmt, pvar)
73         char *whoami;
74         int code;
75         char *fmt;
76         caddr_t pvar;
77 {
78         extern client *cur_client;
79         
80         if (whoami) {
81                 fputs(whoami, stderr);
82                 if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
83                 fputs(": ", stderr);
84         }
85         if (code) {
86                 fputs(error_message(code), stderr);
87         }
88         if (fmt) {
89                 _doprnt(fmt, pvar, stderr);
90         }
91         putc('\n', stderr);
92 }
93
94
95 /* mr_trim_args: passed an argument vector, it will trim any trailing
96  * spaces on the args by writing a null into the string.  If an argument
97  * appears to be binary instead of ASCII, it will not be trimmed.
98  */
99
100 int mr_trim_args(argc, argv)
101 int argc;
102 char **argv;
103 {
104     register char **arg;
105     register unsigned char *p, *lastch;
106
107     for (arg = argv; argc--; arg++) {
108         for (lastch = p = (unsigned char *) *arg; *p; p++) {
109             /* If any byte in the string has the high bit set, assume
110              * that it is binary and we do not want to trim it.
111              * Setting p = lastch will cause us not to trim the string
112              * when we break out of this inner loop.
113              */
114             if (*p >= 0x80) {
115                 p = lastch;
116                 break;
117             }
118             if (!isspace(*p))
119               lastch = p;
120         }
121         if (p != lastch) {
122             if (isspace(*lastch))
123               *lastch = 0;
124             else
125               *(++lastch) = 0;
126         }
127     }
128     return(0);
129 }
130
131
132 /* returns a copy of the argv and all of it's strings */
133
134 char **mr_copy_args(argv, argc)
135 char **argv;
136 int argc;
137 {
138     char **a;
139     int i;
140
141     a = (char **) malloc(argc * sizeof(char *));
142     if (a == 0)
143       return(a);
144     for (i = 0; i < argc; i++)
145       a[i] = strsave(argv[i]);
146     return(a);
147 }
This page took 0.03261 seconds and 3 git commands to generate.