]> andersk Git - moira.git/blob - server/mr_util.c
argument to sms_auth; trim args before returning them, fix trigger_dcm
[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  *
8  */
9
10 #ifndef lint
11 static char *rcsid_sms_util_c = "$Header$";
12 #endif lint
13
14 #include "sms_server.h"
15
16 #include <ctype.h>
17 #include <strings.h>
18
19 extern char *whoami;
20
21 char *
22 requote(buf, cp, len)
23         char *buf;
24         register char *cp;
25 {
26         register int count = 0;
27         register unsigned char c;
28         if (len <= 2) return buf;
29         *buf++ = '"'; count++; len--;
30         for(; (count < 40) && (len > 1) && (c = *cp);
31             cp++, --len, ++count) {
32                 if (c == '\\' || c == '"') *buf++ = '\\';
33                 if (isprint(c)) *buf++ = c;
34                 else {
35                         sprintf(buf, "\\%03o", c);
36                         buf = index(buf, '\0');
37                 }
38         }
39         if (len > 1) { *buf++ = '"'; count++; len--; }
40         if (len > 3 && count >= 40) {
41                 *buf++ = '.'; count++; len--;
42                 *buf++ = '.'; count++; len--;
43                 *buf++ = '.'; count++; len--;
44         }
45         if (len > 1) *buf = '\0';
46         return buf;
47 }
48
49 log_args(tag, version, argc, argv)
50         char *tag;
51         int version;
52         int argc;
53         char **argv;
54 {
55         char buf[BUFSIZ];
56         register int i;
57         register char *bp;
58         
59         i = strlen(tag);
60         sprintf(buf, "%s[%d]: ", tag, version);
61         for (bp = buf; *bp; bp++);
62        
63         for (i = 0; i < argc && ((buf - bp) + 1024) > 2; i++) {
64                 if (i != 0) {
65                         *bp++ = ',';
66                         *bp++ = ' '; 
67                 }
68                 bp = requote(bp, argv[i], (buf - bp) + 1024);
69         }
70         *bp = '\0';
71         com_err(whoami, 0, "%s", buf);
72 }
73         
74 void sms_com_err(whoami, code, fmt, pvar)
75         char *whoami;
76         int code;
77         char *fmt;
78         caddr_t pvar;
79 {
80         extern char *error_message();
81         extern client *cur_client;
82         
83         if (whoami) {
84                 fputs(whoami, stderr);
85                 if (cur_client) fprintf(stderr, "[#%d]", cur_client->id);
86                 fputs(": ", stderr);
87         }
88         if (code) {
89                 fputs(error_message(code), stderr);
90         }
91         if (fmt) {
92                 _doprnt(fmt, pvar, stderr);
93         }
94         putc('\n', stderr);
95 }
96
97
98 /* sms_trim_args: passed an argument vector, it will trim any trailing
99  * spaces on the args by writing a null into the string.
100  */
101
102 int sms_trim_args(argc, argv)
103 int argc;
104 char **argv;
105 {
106     register char **arg;
107     register char *p, *lastch;
108
109     for (arg = argv; argc--; arg++) {
110         for (lastch = p = *arg; *p; p++)
111           if (!isspace(*p))
112             lastch = p;
113         if (p != lastch) {
114             if (isspace(*lastch))
115               *lastch = 0;
116             else
117               *(++lastch) = 0;
118         }
119     }
120     return(0);
121 }
122
123
124 trim(s)
125 register char *s;
126 {
127     register char *p;
128
129     for (p = s; *s; s++)
130       if (*s != ' ')
131         p = s;
132     if (p != s) {
133         if (*p == ' ')
134           *p = 0;
135         else
136           p[1] = 0;
137     }
138 }
This page took 0.046208 seconds and 5 git commands to generate.