]> andersk Git - moira.git/blob - server/mr_util.c
e5d2d8e7f63696680323f8053a34a1101396fec6
[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
18 #include <ctype.h>
19 #include <strings.h>
20 #include <sys/types.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 < 40) && (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 = index(buf, '\0');
40                 }
41         }
42         if (len > 1) { *buf++ = '"'; count++; len--; }
43         if (len > 3 && count >= 40) {
44                 *buf++ = '.'; count++; len--;
45                 *buf++ = '.'; count++; len--;
46                 *buf++ = '.'; count++; len--;
47         }
48         if (len > 1) *buf = '\0';
49         return buf;
50 }
51
52 log_args(tag, version, argc, argv)
53         char *tag;
54         int version;
55         int argc;
56         char **argv;
57 {
58         char buf[BUFSIZ];
59         register int i;
60         register char *bp;
61         
62         i = strlen(tag);
63         sprintf(buf, "%s[%d]: ", tag, version);
64         for (bp = buf; *bp; bp++);
65        
66         for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) {
67                 if (i != 0) {
68                         *bp++ = ',';
69                         *bp++ = ' '; 
70                 }
71                 bp = requote(bp, argv[i], (buf - bp) + 1024);
72         }
73         *bp = '\0';
74         com_err(whoami, 0, "%s", buf);
75 }
76         
77 void mr_com_err(whoami, code, fmt, pvar)
78         char *whoami;
79         int code;
80         char *fmt;
81         caddr_t pvar;
82 {
83         extern char *error_message();
84         extern client *cur_client;
85         
86         if (whoami) {
87                 fputs(whoami, stderr);
88                 if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
89                 fputs(": ", stderr);
90         }
91         if (code) {
92                 fputs(error_message(code), stderr);
93         }
94         if (fmt) {
95                 _doprnt(fmt, pvar, stderr);
96         }
97         putc('\n', stderr);
98 }
99
100
101 /* mr_trim_args: passed an argument vector, it will trim any trailing
102  * spaces on the args by writing a null into the string.
103  */
104
105 int mr_trim_args(argc, argv)
106 int argc;
107 char **argv;
108 {
109     register char **arg;
110     register char *p, *lastch;
111
112     for (arg = argv; argc--; arg++) {
113         for (lastch = p = *arg; *p; p++)
114           if (!isspace(*p))
115             lastch = p;
116         if (p != lastch) {
117             if (isspace(*lastch))
118               *lastch = 0;
119             else
120               *(++lastch) = 0;
121         }
122     }
123     return(0);
124 }
125
126
127 /* returns a copy of the argv and all of it's strings */
128
129 char **mr_copy_args(argv, argc)
130 char **argv;
131 int argc;
132 {
133     char **a;
134     int i;
135
136     a = (char **) malloc(argc * sizeof(char *));
137     if (a == 0)
138       return(a);
139     for (i = 0; i < argc; i++)
140       a[i] = strsave(argv[i]);
141     return(a);
142 }
This page took 0.04139 seconds and 3 git commands to generate.