aboutsummaryrefslogtreecommitdiff
path: root/OSC/sendOSC.c
blob: c039c862bd7fde1db3c708c906ddca008dacd7e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
/*
Copyright (c) 1996,1997.  The Regents of the University of California (Regents).
All Rights Reserved.

Permission to use, copy, modify, and distribute this software and its
documentation for educational, research, and not-for-profit purposes, without
fee and without a signed licensing agreement, is hereby granted, provided that
the above copyright notice, this paragraph and the following two paragraphs
appear in all copies, modifications, and distributions.  Contact The Office of
Technology Licensing, UC Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley,
CA 94720-1620, (510) 643-7201, for commercial licensing opportunities.

Written by Matt Wright, The Center for New Music and Audio Technologies,
University of California, Berkeley.

     IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
     SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
     ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
     REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

     REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
     DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
     REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
     ENHANCEMENTS, OR MODIFICATIONS.
*/

/* sendOSC.c

    Matt Wright, 6/3/97
   based on sendOSC.c, which was based on a version by Adrian Freed

    Text-based OpenSoundControl client.  User can enter messages via command
    line arguments or standard input.

    Version 0.1: "play" feature
   Version 0.2: Message type tags.
   


   pd
   -------------
   -- added bundle stuff to send. jdl 20020416
   -- tweaks for Win32    www.zeggz.com/raf	13-April-2002
   -- ost_at_test.at + i22_at_test.at, 2000-2002
      modified to compile as pd externel

*/

//#define VERSION "http://cnmat.berkeley.edu/OpenSoundControl/sendOSC-0.1.html"
#define MAX_ARGS 2000
#define SC_BUFFER_SIZE 64000

/*
compiling:
        cc -o sendOSC sendOSC.c htmsocket.c OpenSoundControl.c OSC_timeTag.c
*/

#ifdef WIN32
	#include "m_pd.h"
	#include "OSC-client.h"
	#include "htmsocket.h"
	#include "OSC-common.h"
	#include <winsock2.h>	
	#include <io.h>    
	#include <errno.h>
	#include <fcntl.h>
	#include <sys/stat.h>
	#include <stdlib.h>
	#include <string.h>
	#include <sys/types.h>
#else
	#include "m_pd.h"
	//#include "x_osc.h"
	#include "OSC-client.h"
	#include "htmsocket.h"

	#include <stdio.h>
	#include <stdlib.h>
	#include <string.h>
	#include <sys/types.h>
	#include <netinet/in.h>
	#include <netdb.h>
#endif

///////////////////////
// from sendOSC

typedef struct {
    //enum {INT, FLOAT, STRING} type;
    enum {INT_osc, FLOAT_osc, STRING_osc} type;
    union {
        int i;
        float f;
        char *s;
    } datum;
} typedArg;

void CommandLineMode(int argc, char *argv[], void *htmsocket);
//void InteractiveMode(void *htmsocket);
OSCTimeTag ParseTimeTag(char *s);
void ParseInteractiveLine(OSCbuf *buf, char *mesg);
typedArg ParseToken(char *token);
int WriteMessage(OSCbuf *buf, char *messageName, int numArgs, typedArg *args);
void SendBuffer(void *htmsocket, OSCbuf *buf);
void SendData(void *htmsocket, int size, char *data);
void fatal_error(char *s);
void send_complain(char *s, ...);

//static void *htmsocket;
static int exitStatus = 0;  
static int useTypeTags = 0;

static char bufferForOSCbuf[SC_BUFFER_SIZE];


/////////
// end from sendOSC

static t_class *sendOSC_class;

typedef struct _sendOSC
{
  t_object x_obj;
  int x_protocol;      // UDP/TCP (udp only atm)
  t_int x_typetags;    // typetag flag
  void *x_htmsocket;   // sending socket
  int x_bundle;        // bundle open flag
  OSCbuf x_oscbuf[1];  // OSCbuffer
  t_outlet *x_bdpthout;// bundle-depth floatoutlet
} t_sendOSC;

static void *sendOSC_new(t_floatarg udpflag)
{
    t_sendOSC *x = (t_sendOSC *)pd_new(sendOSC_class);
    outlet_new(&x->x_obj, &s_float);
    x->x_htmsocket = 0;		// {{raf}}
    // set udp
    x->x_protocol = SOCK_STREAM;
    // set typetags to 1 by default
    x->x_typetags = 1;
    // bunlde is closed
    x->x_bundle   = 0;
    OSC_initBuffer(x->x_oscbuf, SC_BUFFER_SIZE, bufferForOSCbuf);
    x->x_bdpthout = outlet_new(&x->x_obj, 0); // outlet_float();
    //x->x_oscbuf   =
    return (x);
}


void sendOSC_openbundle(t_sendOSC *x)
{
  if (x->x_oscbuf->bundleDepth + 1 >= MAX_BUNDLE_NESTING ||
      OSC_openBundle(x->x_oscbuf, OSCTT_Immediately()))
    {
    send_complain("Problem opening bundle: %s\n", OSC_errorMessage);
    return;
  }
  x->x_bundle = 1;
  outlet_float(x->x_bdpthout, (float)x->x_oscbuf->bundleDepth);
}

static void sendOSC_closebundle(t_sendOSC *x)
{
  if (OSC_closeBundle(x->x_oscbuf)) {
    send_complain("Problem closing bundle: %s\n", OSC_errorMessage);
    return;
  }
  outlet_float(x->x_bdpthout, (float)x->x_oscbuf->bundleDepth);
  // in bundle mode we send when bundle is closed?
  if(!OSC_isBufferEmpty(x->x_oscbuf) > 0 && OSC_isBufferDone(x->x_oscbuf)) {
    // post("x_oscbuf: something inside me?");
    if (x->x_htmsocket) {
      SendBuffer(x->x_htmsocket, x->x_oscbuf);
    } else {
      post("sendOSC: not connected");
    }
    OSC_initBuffer(x->x_oscbuf, SC_BUFFER_SIZE, bufferForOSCbuf);
    x->x_bundle = 0;
    return;
  }
  // post("x_oscbuf: something went wrong");
}

static void sendOSC_settypetags(t_sendOSC *x, t_float *f)
 {
   x->x_typetags = (int)f;
   post("sendOSC.c: setting typetags %d",x->x_typetags);
 }


static void sendOSC_connect(t_sendOSC *x, t_symbol *hostname,
			    t_floatarg fportno)
{
	int portno = fportno;
	/* create a socket */

	//	make sure handle is available
	if(x->x_htmsocket == 0) {
		//
		x->x_htmsocket = OpenHTMSocket(hostname->s_name, portno);
		if (!x->x_htmsocket)
			post("Couldn't open socket: ");
		else {
			post("connected to port %s:%d (hSock=%d)",  hostname->s_name, portno, x->x_htmsocket);
			outlet_float(x->x_obj.ob_outlet, 1);
		}
	}
	else 
		perror("call to sendOSC_connect() against UNavailable socket handle");
}

void sendOSC_disconnect(t_sendOSC *x)
{
  if (x->x_htmsocket)
    {
      post("disconnecting htmsock (hSock=%d)...", x->x_htmsocket);
      CloseHTMSocket(x->x_htmsocket);
	  x->x_htmsocket = 0;	// {{raf}}  semi-quasi-semaphorize this
      outlet_float(x->x_obj.ob_outlet, 0);
    }
  else {
	perror("call to sendOSC_disconnect() against unused socket handle");
  }
}

void sendOSC_senduntyped(t_sendOSC *x, t_symbol *s, int argc, t_atom *argv)
{
  char* targv[MAXPDARG];
  char tmparg[MAXPDSTRING];
  char* tmp = tmparg;
  //char testarg[MAXPDSTRING];
  int c;

  post("sendOSC: use typetags 0/1 message and plain send method so send untypetagged...");
  return;

  //atom_string(argv,testarg, MAXPDSTRING);
  for (c=0;c<argc;c++) {
    atom_string(argv+c,tmp, 80);
    //    post ("sendOSC: %d, %s",c, tmp);
    targv[c] = tmp;
    tmp += strlen(tmp)+1;
    //post ("sendOSC: %d, %s",c, targv[c]);
  }
  // this sock needs to be larger than 0, not >= ..
  if (x->x_htmsocket)
    {
      CommandLineMode(argc, targv, x->x_htmsocket);
      //      post("test %d", c);
    }
  else {
    post("sendOSC: not connected");
    //    exit(3);
  }
}

//////////////////////////////////////////////////////////////////////
// this is the real and only sending routine now, for both typed and undtyped mode.

static void sendOSC_sendtyped(t_sendOSC *x, t_symbol *s, int argc, t_atom *argv)
{
  char* targv[MAX_ARGS];
  char tmparg[MAXPDSTRING];
  char* tmp = tmparg;
  int c;

  char *messageName;
  char *token;
  typedArg args[MAX_ARGS];
  int i,j;
  int numArgs = 0;

  messageName = "";
#ifdef DEBUG
  post ("sendOSC: messageName: %s", messageName);
#endif


  
  for (c=0;c<argc;c++) {
    atom_string(argv+c,tmp, 80);

#ifdef DEBUG
    // post ("sendOSC: %d, %s",c, tmp);
#endif

    targv[c] = tmp;
    tmp += strlen(tmp)+1;

#ifdef DEBUG
    // post ("sendOSC: %d, %s",c, targv[c]);
#endif
  }

  // this sock needs to be larger than 0, not >= ..
  if (x->x_htmsocket > 0)
    { 
#ifdef DEBUG
    post ("sendOSC: type tags? %d", useTypeTags);
#endif 

      messageName = strtok(targv[0], ",");
      j = 1;
      for (i = j; i < argc; i++) {
	token = strtok(targv[i],",");
	args[i-j] = ParseToken(token);
#ifdef DEBUG
	printf("cell-cont: %s\n", targv[i]);
	printf("  type-id: %d\n", args[i-j]);
#endif
	numArgs = i;
      }
      

      if(WriteMessage(x->x_oscbuf, messageName, numArgs, args)) {
	post("sendOSC: usage error, write-msg failed: %s", OSC_errorMessage);
	return;
      }
      
      if(!x->x_bundle) {
/* 	// post("sendOSC: accumulating bundle, not sending things ...");	 */
/*       } else { */
	// post("sendOSC: yeah and OUT!");
	SendBuffer(x->x_htmsocket, x->x_oscbuf);
	OSC_initBuffer(x->x_oscbuf, SC_BUFFER_SIZE, bufferForOSCbuf);
      }
      
      //CommandLineMode(argc, targv, x->x_htmsocket);
      //useTypeTags = 0;
    }
  else {
    post("sendOSC: not connected");
    //    exit(3);
  }
}

void sendOSC_send(t_sendOSC *x, t_symbol *s, int argc, t_atom *argv) 
{
  if(!argc) {
    post("not sending empty message.");
    return;
  }
  if(x->x_typetags) {
    useTypeTags = 1;
    sendOSC_sendtyped(x,s,argc,argv);
    useTypeTags = 0;
  } else {
    sendOSC_sendtyped(x,s,argc,argv);
  }
}

static void sendOSC_free(t_sendOSC *x)
{
    sendOSC_disconnect(x);
}

#ifdef WIN32
	OSC_API void sendOSC_setup(void) { 
#else
	void sendOSC_setup(void) {
#endif
    sendOSC_class = class_new(gensym("sendOSC"), (t_newmethod)sendOSC_new,
			      (t_method)sendOSC_free,
			      sizeof(t_sendOSC), 0, A_DEFFLOAT, 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_connect,
		    gensym("connect"), A_SYMBOL, A_FLOAT, 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_disconnect,
		    gensym("disconnect"), 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_settypetags,
		    gensym("typetags"),
		    A_FLOAT, 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_send,
		    gensym("send"),
		    A_GIMME, 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_send,
		    gensym("senduntyped"),
		    A_GIMME, 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_send,
		    gensym("sendtyped"),
		    A_GIMME, 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_openbundle,
		    gensym("["),
		    0, 0);
    class_addmethod(sendOSC_class, (t_method)sendOSC_closebundle,
		    gensym("]"),
		    0, 0);
    class_sethelpsymbol(sendOSC_class, gensym("OSC/sendOSC-help.pd"));
}





/* Exit status codes:
    0: successful
    2: Message(s) dropped because of buffer overflow
    3: Socket error
    4: Usage error
    5: Internal error
*/

void CommandLineMode(int argc, char *argv[], void *htmsocket) {
    char *messageName;
    char *token;
    typedArg args[MAX_ARGS];
    int i,j, numArgs;
    OSCbuf buf[1];

  OSC_initBuffer(buf, SC_BUFFER_SIZE, bufferForOSCbuf);

  if (argc > 1) {
    post("argc (%d) > 1", argc);
/* 	if (OSC_openBundle(buf, OSCTT_Immediately())) { */
/* 	    send_complain("Problem opening bundle: %s\n", OSC_errorMessage); */
/* 	    return; */
/* 	} */
    }

  //    ParseInteractiveLine(buf, argv);
  messageName = strtok(argv[0], ",");

    j = 1;
    for (i = j; i < argc; i++) {
      token = strtok(argv[i],",");
      args[i-j] = ParseToken(token);
#ifdef DEBUG
      printf("cell-cont: %s\n", argv[i]);
      printf("  type-id: %d\n", args[i-j]);
#endif
      numArgs = i;
    }

    if(WriteMessage(buf, messageName, numArgs, args)) {
      post("sendOSC: usage error. write-msg failed: %s", OSC_errorMessage);
      return;
    }

/*     for (i = 0; i < argc; i++) { */
/*         messageName = strtok(argv[i], ","); */
/* 	//send_complain ("commandlinemode: count: %d %s\n",i, messageName); */
/*         if (messageName == NULL) { */
/*             break; */
/*         } */

/*         j = 0; */
/*         while ((token = strtok(NULL, ",")) != NULL) { */
/*             args[j] = ParseToken(token); */
/*             j++; */
/* 	    if (j >= MAX_ARGS) { */
/* 		send_complain("Sorry; your message has more than MAX_ARGS (%d) arguments; ignoring the rest.\n", */
/* 			 MAX_ARGS); */
/* 		break; */
/* 	    } */
/*         } */
/*         numArgs = j; */

/*         WriteMessage(buf, messageName, numArgs, args); */

/*     } */

/*     if (argc > 1) { */
/* 	if (OSC_closeBundle(buf)) { */
/* 	    send_complain("Problem closing bundle: %s\n", OSC_errorMessage); */
/* 	    return; */
/* 	} */
/*     } */

    SendBuffer(htmsocket, buf);
}

#define MAXMESG 2048

void InteractiveMode(void *htmsocket) {
    char mesg[MAXMESG];
    OSCbuf buf[1];
    int bundleDepth = 0;    /* At first, we haven't seen "[". */

    OSC_initBuffer(buf, SC_BUFFER_SIZE, bufferForOSCbuf);

    while (fgets(mesg, MAXMESG, stdin) != NULL) {
        if (mesg[0] == '\n') {
	  if (bundleDepth > 0) {
	    /* Ignore blank lines inside a group. */
	  } else {
            /* blank line => repeat previous send */
            SendBuffer(htmsocket, buf);
	  }
	  continue;
        }

	if (bundleDepth == 0) {
	    OSC_resetBuffer(buf);
	}

	if (mesg[0] == '[') {
	    OSCTimeTag tt = ParseTimeTag(mesg+1);
	    if (OSC_openBundle(buf, tt)) {
		send_complain("Problem opening bundle: %s\n", OSC_errorMessage);
		OSC_resetBuffer(buf);
		bundleDepth = 0;
		continue;
	    }
	    bundleDepth++;
        } else if (mesg[0] == ']' && mesg[1] == '\n' && mesg[2] == '\0') {
            if (bundleDepth == 0) {
                send_complain("Unexpected ']': not currently in a bundle.\n");
            } else {
		if (OSC_closeBundle(buf)) {
		    send_complain("Problem closing bundle: %s\n", OSC_errorMessage);
		    OSC_resetBuffer(buf);
		    bundleDepth = 0;
		    continue;
		}

		bundleDepth--;
		if (bundleDepth == 0) {
		    SendBuffer(htmsocket, buf);
		}
            }
        } else {
            ParseInteractiveLine(buf, mesg);
            if (bundleDepth != 0) {
                /* Don't send anything until we close all bundles */
            } else {
                SendBuffer(htmsocket, buf);
            }
        }
    }
}

OSCTimeTag ParseTimeTag(char *s) {
    char *p, *newline;
    typedArg arg;

    p = s;
    while (isspace(*p)) p++;
    if (*p == '\0') return OSCTT_Immediately();

    if (*p == '+') {
	/* Time tag is for some time in the future.  It should be a
           number of seconds as an int or float */

	newline = strchr(s, '\n');
	if (newline != NULL) *newline = '\0';

	p++; /* Skip '+' */
	while (isspace(*p)) p++;

	arg = ParseToken(p);
	if (arg.type == STRING_osc) {
	    send_complain("warning: inscrutable time tag request: %s\n", s);
	    return OSCTT_Immediately();
	} else if (arg.type == INT_osc) {
	    return OSCTT_PlusSeconds(OSCTT_CurrentTime(),
				     (float) arg.datum.i);
	} else if (arg.type == FLOAT_osc) {
	    return OSCTT_PlusSeconds(OSCTT_CurrentTime(), arg.datum.f);
	} else {
	    fatal_error("This can't happen!");
	}
    }

    if (isdigit(*p) || (*p >= 'a' && *p <='f') || (*p >= 'A' && *p <='F')) {
	/* They specified the 8-byte tag in hex */
	OSCTimeTag tt;
	if (sscanf(p, "%llx", &tt) != 1) {
	    send_complain("warning: couldn't parse time tag %s\n", s);
	    return OSCTT_Immediately();
	}
#ifndef	HAS8BYTEINT
	if (ntohl(1) != 1) {
	    /* tt is a struct of seconds and fractional part,
	       and this machine is little-endian, so sscanf
	       wrote each half of the time tag in the wrong half
	       of the struct. */
	    uint32 temp;
	    temp = tt.seconds;
	    tt.seconds = tt.fraction ;
	    tt.fraction = temp;
	}
#endif
	return tt;
    }

    send_complain("warning: invalid time tag: %s\n", s);
    return OSCTT_Immediately();
}
	    

void ParseInteractiveLine(OSCbuf *buf, char *mesg) {
    char *messageName, *token, *p;
    typedArg args[MAX_ARGS];
    int thisArg;

    p = mesg;
    while (isspace(*p)) p++;
    if (*p == '\0') return;

    messageName = p;

    if (strcmp(messageName, "play\n") == 0) {
	/* Special kludge feature to save typing */
	typedArg arg;

	if (OSC_openBundle(buf, OSCTT_Immediately())) {
	    send_complain("Problem opening bundle: %s\n", OSC_errorMessage);
	    return;
	}

	arg.type = INT_osc;
	arg.datum.i = 0;
	WriteMessage(buf, "/voices/0/tp/timbre_index", 1, &arg);

	arg.type = FLOAT_osc;
	arg.datum.i = 0.0f;
	WriteMessage(buf, "/voices/0/tm/goto", 1, &arg);

	if (OSC_closeBundle(buf)) {
	    send_complain("Problem closing bundle: %s\n", OSC_errorMessage);
	}

	return;
    }

    while (!isspace(*p) && *p != '\0') p++;
    if (isspace(*p)) {
        *p = '\0';
        p++;
    }

    thisArg = 0;
    while (*p != '\0') {
        /* flush leading whitespace */
        while (isspace(*p)) p++;
        if (*p == '\0') break;

        if (*p == '"') {
            /* A string argument: scan for close quotes */
            p++;
            args[thisArg].type = STRING_osc;
            args[thisArg].datum.s = p;

            while (*p != '"') {
                if (*p == '\0') {
                    send_complain("Unterminated quote mark: ignoring line\n");
                    return;
                }
                p++;
            }
            *p = '\0';
            p++;
        } else {
            token = p;
            while (!isspace(*p) && (*p != '\0')) p++;
            if (isspace(*p)) {
                *p = '\0';
                p++;
            }
            args[thisArg] = ParseToken(token);
        }
        thisArg++;
	if (thisArg >= MAX_ARGS) {
	  send_complain("Sorry, your message has more than MAX_ARGS (%d) arguments; ignoring the rest.\n",
		   MAX_ARGS);
	  break;
	}
    }

    if (WriteMessage(buf, messageName, thisArg, args) != 0)  {
	send_complain("Problem sending message: %s\n", OSC_errorMessage);
    }
}

typedArg ParseToken(char *token) {
    char *p = token;
    typedArg returnVal;

    /* It might be an int, a float, or a string */

    if (*p == '-') p++;

    if (isdigit(*p) || *p == '.') {
        while (isdigit(*p)) p++;
        if (*p == '\0') {
            returnVal.type = INT_osc;
            returnVal.datum.i = atoi(token);
            return returnVal;
        }
        if (*p == '.') {
            p++;
            while (isdigit(*p)) p++;
            if (*p == '\0') {
                returnVal.type = FLOAT_osc;
                returnVal.datum.f = atof(token);
                return returnVal;
            }
        }
    }

    returnVal.type = STRING_osc;
    returnVal.datum.s = token;
    return returnVal;
}

int WriteMessage(OSCbuf *buf, char *messageName, int numArgs, typedArg *args) {
  int j, returnVal;
  const wmERROR = -1;

  returnVal = 0;

#ifdef DEBUG
  printf("WriteMessage: %s ", messageName);

  for (j = 0; j < numArgs; j++) {
    switch (args[j].type) {
    case INT_osc:
      printf("%d ", args[j].datum.i);
      break;
      
    case FLOAT_osc:
      printf("%f ", args[j].datum.f);
      break;
      
    case STRING_osc:
      printf("%s ", args[j].datum.s);
      break;
      
    default:
      fatal_error("Unrecognized arg type, (not exiting)");
      return(wmERROR);
      // exit(5);
    }
  }
  printf("\n");
#endif
  
  if (!useTypeTags) {
    returnVal = OSC_writeAddress(buf, messageName);
    if (returnVal) {
      send_complain("Problem writing address: %s\n", OSC_errorMessage);
    }
  } else {
    /* First figure out the type tags */
    char typeTags[MAX_ARGS+2];
    int i;
    
    typeTags[0] = ',';
    
    for (i = 0; i < numArgs; ++i) {
      switch (args[i].type) {
      case INT_osc:
	typeTags[i+1] = 'i';
	break;
	
      case FLOAT_osc:
	typeTags[i+1] = 'f';
	break;
	
      case STRING_osc:
	typeTags[i+1] = 's';
	break;
	
      default:
	fatal_error("Unrecognized arg type (not exiting)");
	return(wmERROR);
	// exit(5);
      }
    }
    typeTags[i+1] = '\0';
    
    returnVal = OSC_writeAddressAndTypes(buf, messageName, typeTags);
    if (returnVal) {
      send_complain("Problem writing address: %s\n", OSC_errorMessage);
    }
  }

  for (j = 0; j < numArgs; j++) {
    switch (args[j].type) {
    case INT_osc:
      if ((returnVal = OSC_writeIntArg(buf, args[j].datum.i)) != 0) {
	return returnVal;
      }
      break;
      
    case FLOAT_osc:
      if ((returnVal = OSC_writeFloatArg(buf, args[j].datum.f)) != 0) {
	return returnVal;
      }
      break;
      
    case STRING_osc:
      if ((returnVal = OSC_writeStringArg(buf, args[j].datum.s)) != 0) {
	return returnVal;
      }
      break;
      
    default:
      fatal_error("Unrecognized arg type (not exiting)");
      returnVal = wmERROR;
      // exit(5);
    }
  }
  return returnVal;
}

void SendBuffer(void *htmsocket, OSCbuf *buf) {
#ifdef DEBUG
  printf("Sending buffer...\n");
#endif
  if (OSC_isBufferEmpty(buf)) {
		post("SendBuffer() called but buffer empty");
		return;
  }
  if (!OSC_isBufferDone(buf)) {
		fatal_error("SendBuffer() called but buffer not ready!, not exiting");
		// exit(5); 
		return;	//{{raf}}
  }
  SendData(htmsocket, OSC_packetSize(buf), OSC_getPacket(buf));
}

void SendData(void *htmsocket, int size, char *data) {
  if (!SendHTMSocket(htmsocket, size, data)) {
    post("SendData::SendHTMSocket()failure -- not connected");
    CloseHTMSocket(htmsocket);
    //    sendOSC_disconnect();
    //exit(3);
    //    return;
  }
}

void fatal_error(char *s) {
    fprintf(stderr, "FATAL ERROR: %s\n", s);
    post("fatal error, not exiting ...");
    //exit(4);
}

#include <stdarg.h>
void send_complain(char *s, ...) {
    va_list ap;
    va_start(ap, s);
    vfprintf(stderr, s, ap);
    va_end(ap);
}


#ifdef COMPUTE_MESSAGE_SIZE
    /* Unused code to find the size of a message */

    /* Compute size */
    size = SynthControl_effectiveStringLength(messageName);

    for (j = 0; j < numArgs; j++) {
        switch (args[j].type) {
            case INT_osc: case FLOAT_osc:
            size += 4;
            break;

            case STRING_osc:
            size += SynthControl_effectiveStringLength(args[j].datum.s);
            break;

            default:
            fatal_error("Unrecognized token type ( not exiting)");
            // exit(4);
        }
    }

    if (!SynthControl_willMessageFit(buf, size)) {
        send_complain("Message \"%s\" won't fit in buffer: dropping.", messageName);
        return;
    }
#endif