1 
2 /*-
3  *
4  * New BSD License 2006
5  *
6  * Copyright (c) 2006, Jorgen Lundman
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  *
13  * 1 Redistributions of source code must retain the above copyright
14  *   notice, this list of conditions and the following disclaimer.
15  * 2 Redistributions in binary form must reproduce the above
16  *   copyright notice, this list of conditions and the following
17  *   disclaimer in the documentation and/or other materials provided
18  *   with the distribution.
19  * 3 Neither the name of the stuff nor the names of its contributors
20  *   may be used to endorse or promote products derived from this
21  *   software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 #include <stdio.h>
38 #if __STDC__
39 #include <stdarg.h>
40 #else
41 #include <varargs.h>
42 #endif
43 
44 #include "lion.h"
45 
46 #include "object.h"
47 
48 #include "say.h"
49 
50 
51 
52 
53 
54 
55 
56 
say_all_sub(object_t * node,void * arg1,void * arg2)57 int say_all_sub( object_t *node, void *arg1, void *arg2 )
58 {
59 	// Out message should be in arg1, and strlen in arg2
60 	static char *line;
61 	static int len;
62 
63 	if (!arg1 || !arg2)
64 		return 0; // 0 to abort iteration
65 
66 	// Only say to registered connections
67 	if (node->type != OBJECT_TYPE_REGISTERED)
68 		return 1; // 1 to keep iterating.
69 
70 
71 	line = (char *) arg1;
72 	len = (int) arg2;
73 
74 	lion_send(node->handle, line, len);
75 
76 	return 1; // 1 to keep iterating.
77 }
78 
79 
80 
81 
82 //
83 // Send a string to a service
84 //
85 #if defined (__STDC__) || defined (WIN32)
say_all(char const * fmt,...)86 int say_all(char const *fmt, ...)
87 #else
88 int say_all(fmt, va_alist)
89      char const *fmt;
90      va_dcl
91 #endif
92 {
93   va_list ap;
94   // Using a static buffer here is not ideal, but would be ok when
95   // using "vsnprintf" - which is included with lion for Windows platforms.
96   // Another option is using "lion_buffersize(YOUR_BUFFER_SIZE);" in your
97   // main, then using that same value here.
98   static char msg[4096];
99   int result;
100 
101 #if __STDC__ || WIN32
102   va_start(ap, fmt);
103 #else
104   va_start(ap);
105 #endif
106 
107   // Buffer overflow! No vsnprintf on many systems :(
108   result = vsprintf(msg, fmt, ap);
109   va_end(ap);
110 
111 
112   result = strlen(msg);
113 
114   // Send it to all registered clients.
115   object_find( say_all_sub, (void *)msg, (void *)result );
116 
117   // Tell console too. But \r\n makes double new line, so to make it
118   // pretty, target and eliminate the "\r"
119   if ((result > 2)  &&
120 	  (msg[ result - 2 ] == '\r'))
121 	  msg[ result - 2 ] = 0;
122 
123   puts(msg);
124 
125   return result;
126 
127 }
128 
129