m_binbuf.c 60.3 KB
Newer Older
Miller Puckette's avatar
Miller Puckette committed
1 2 3 4
/* Copyright (c) 1997-1999 Miller Puckette.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
5
#include "config.h"
Miller Puckette's avatar
Miller Puckette committed
6 7 8 9

#include <stdlib.h>
#include "m_pd.h"
#include "s_stuff.h"
Jonathan Wilkes's avatar
Jonathan Wilkes committed
10
#include "g_canvas.h"
Miller Puckette's avatar
Miller Puckette committed
11
#include <stdio.h>
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
12 13

#ifdef HAVE_UNISTD_H
Miller Puckette's avatar
Miller Puckette committed
14 15
#include <unistd.h>
#endif
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
16 17

#ifdef HAVE_IO_H
Miller Puckette's avatar
Miller Puckette committed
18 19
#include <io.h>
#endif
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
20

Miller Puckette's avatar
Miller Puckette committed
21 22 23 24
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>

25 26
#define DOLLARALL -0x7fffffff /* sentinel value for "$@" dollar arg */

Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/* escape characters for saving */
static char* strnescape(char *dest, const char *src, size_t outlen)
{
    int ptin = 0;
    unsigned ptout = 0;
    for(; ptout < outlen; ptin++, ptout++)
    {
        int c = src[ptin];
        if (c == ' ' || c=='\t')
            dest[ptout++] = '\\';
        dest[ptout] = src[ptin];
        if (c==0) break;
    }

    if(ptout < outlen)
        dest[ptout]=0;
    else
        dest[outlen-1]=0;

    return dest;
}


Miller Puckette's avatar
Miller Puckette committed
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
struct _binbuf
{
    int b_n;
    t_atom *b_vec;
};

t_binbuf *binbuf_new(void)
{
    t_binbuf *x = (t_binbuf *)t_getbytes(sizeof(*x));
    x->b_n = 0;
    x->b_vec = t_getbytes(0);
    return (x);
}

void binbuf_free(t_binbuf *x)
{
    t_freebytes(x->b_vec, x->b_n * sizeof(*x->b_vec));
    t_freebytes(x,  sizeof(*x));
}

t_binbuf *binbuf_duplicate(t_binbuf *y)
{
    t_binbuf *x = (t_binbuf *)t_getbytes(sizeof(*x));
    x->b_n = y->b_n;
    x->b_vec = t_getbytes(x->b_n * sizeof(*x->b_vec));
    memcpy(x->b_vec, y->b_vec, x->b_n * sizeof(*x->b_vec));
    return (x);
}

void binbuf_clear(t_binbuf *x)
{
    x->b_vec = t_resizebytes(x->b_vec, x->b_n * sizeof(*x->b_vec), 0);
    x->b_n = 0;
}

    /* convert text to a binbuf */
void binbuf_text(t_binbuf *x, char *text, size_t size)
{
88
    //fprintf(stderr, "current text: %.*s\n", size, text);
Miller Puckette's avatar
Miller Puckette committed
89 90 91 92 93 94 95 96 97 98
    char buf[MAXPDSTRING+1], *bufp, *ebuf = buf+MAXPDSTRING;
    const char *textp = text, *etext = text+size;
    t_atom *ap;
    int nalloc = 16, natom = 0;
    t_freebytes(x->b_vec, x->b_n * sizeof(*x->b_vec));
    x->b_vec = t_getbytes(nalloc * sizeof(*x->b_vec));
    ap = x->b_vec;
    x->b_n = 0;
    while (1)
    {
Ivica Bukvic's avatar
Ivica Bukvic committed
99
        //int type;
Miller Puckette's avatar
Miller Puckette committed
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
            /* skip leading space */
        while ((textp != etext) && (*textp == ' ' || *textp == '\n'
            || *textp == '\r' || *textp == '\t')) textp++;
        if (textp == etext) break;
        if (*textp == ';') SETSEMI(ap), textp++;
        else if (*textp == ',') SETCOMMA(ap), textp++;
        else
        {
                /* it's an atom other than a comma or semi */
            char c;
            int floatstate = 0, slash = 0, lastslash = 0, dollar = 0;
            bufp = buf;
            do
            {
                c = *bufp = *textp++;
                lastslash = slash;
                slash = (c == '\\');

                if (floatstate >= 0)
                {
                    int digit = (c >= '0' && c <= '9'),
                        dot = (c == '.'), minus = (c == '-'),
                        plusminus = (minus || (c == '+')),
                        expon = (c == 'e' || c == 'E');
                    if (floatstate == 0)    /* beginning */
                    {
                        if (minus) floatstate = 1;
                        else if (digit) floatstate = 2;
                        else if (dot) floatstate = 3;
                        else floatstate = -1;
                    }
                    else if (floatstate == 1)   /* got minus */
                    {
                        if (digit) floatstate = 2;
                        else if (dot) floatstate = 3;
                        else floatstate = -1;
                    }
                    else if (floatstate == 2)   /* got digits */
                    {
                        if (dot) floatstate = 4;
                        else if (expon) floatstate = 6;
                        else if (!digit) floatstate = -1;
                    }
                    else if (floatstate == 3)   /* got '.' without digits */
                    {
                        if (digit) floatstate = 5;
                        else floatstate = -1;
                    }
                    else if (floatstate == 4)   /* got '.' after digits */
                    {
                        if (digit) floatstate = 5;
                        else if (expon) floatstate = 6;
                        else floatstate = -1;
                    }
                    else if (floatstate == 5)   /* got digits after . */
                    {
                        if (expon) floatstate = 6;
                        else if (!digit) floatstate = -1;
                    }
                    else if (floatstate == 6)   /* got 'e' */
                    {
                        if (plusminus) floatstate = 7;
                        else if (digit) floatstate = 8;
                        else floatstate = -1;
                    }
                    else if (floatstate == 7)   /* got plus or minus */
                    {
                        if (digit) floatstate = 8;
                        else floatstate = -1;
                    }
                    else if (floatstate == 8)   /* got digits */
                    {
                        if (!digit) floatstate = -1;
                    }
                }
                if (!lastslash && c == '$' && (textp != etext && 
176 177
                    ((textp[0] >= '0' && textp[0] <= '9')||
                    textp[0]=='@'))) /* JMZ: $@ and $# expansion */
Miller Puckette's avatar
Miller Puckette committed
178 179
                        dollar = 1;
                if (!slash) bufp++;
180 181 182 183 184
                else if (lastslash)
                {
                    bufp++;
                    slash = 0;
                }
Miller Puckette's avatar
Miller Puckette committed
185 186 187 188 189 190
            }
            while (textp != etext && bufp != ebuf && 
                (slash || (*textp != ' ' && *textp != '\n' && *textp != '\r'
                    && *textp != '\t' &&*textp != ',' && *textp != ';')));
            *bufp = 0;
#if 0
191
            post("binbuf_text: buf %s, dollar=%d", buf, dollar);
Miller Puckette's avatar
Miller Puckette committed
192 193 194 195 196 197 198 199 200 201
#endif
            if (floatstate == 2 || floatstate == 4 || floatstate == 5 ||
                floatstate == 8)
                    SETFLOAT(ap, atof(buf));
                /* LATER try to figure out how to mix "$" and "\$" correctly;
                here, the backslashes were already stripped so we assume all
                "$" chars are real dollars.  In fact, we only know at least one
                was. */
            else if (dollar)
            {
202 203
                if(buf[1]=='@') /* JMZ: $@ expansion */
                {
204
                    if(buf[2]==0) /* only expand A_DOLLAR $@ */
205
                    {
206
                        ap->a_type = A_DOLLAR;
207
                        ap->a_w.w_index = DOLLARALL;
208
                    } 
209
                    else /* there is no A_DOLLSYM $@ */
210
                    {
211
                        SETSYMBOL(ap, gensym(buf));
212
                    }
213 214 215 216 217 218 219 220 221 222 223 224
                }
                else
                {
                    if (buf[0] != '$') 
                        dollar = 0;
                    for (bufp = buf+1; *bufp; bufp++)
                        if (*bufp < '0' || *bufp > '9')
                            dollar = 0;
                    if (dollar)
                        SETDOLLAR(ap, atoi(buf+1));
                    else SETDOLLSYM(ap, gensym(buf));
                }
Miller Puckette's avatar
Miller Puckette committed
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
            }
            else SETSYMBOL(ap, gensym(buf));
        }
        ap++;
        natom++;
        if (natom == nalloc)
        {
            x->b_vec = t_resizebytes(x->b_vec, nalloc * sizeof(*x->b_vec),
                nalloc * (2*sizeof(*x->b_vec)));
            nalloc = nalloc * 2;
            ap = x->b_vec + natom;
        }
        if (textp == etext) break;
    }
    /* reallocate the vector to exactly the right size */
    x->b_vec = t_resizebytes(x->b_vec, nalloc * sizeof(*x->b_vec),
        natom * sizeof(*x->b_vec));
    x->b_n = natom;
}

    /* convert a binbuf to text; no null termination. */
void binbuf_gettext(t_binbuf *x, char **bufp, int *lengthp)
{
    char *buf = getbytes(0), *newbuf;
    int length = 0;
    char string[MAXPDSTRING];
    t_atom *ap;
    int indx;
253
    int newlength;
Miller Puckette's avatar
Miller Puckette committed
254 255 256

    for (ap = x->b_vec, indx = x->b_n; indx--; ap++)
    {
257
        //fprintf(stderr,"=====\n");
Miller Puckette's avatar
Miller Puckette committed
258
        if ((ap->a_type == A_SEMI || ap->a_type == A_COMMA) &&
259 260 261 262 263
                length && buf[length-1] == ' ') 
        {
            //fprintf(stderr, "subtracting length\n");
            length--;
        }
Miller Puckette's avatar
Miller Puckette committed
264 265 266 267
        atom_string(ap, string, MAXPDSTRING);
        newlength = length + strlen(string) + 1;
        if (!(newbuf = resizebytes(buf, length, newlength))) break;
        buf = newbuf;
268
        //fprintf(stderr,"string=<%s> buf=<%s> length=%d\n", string, buf, length);
Miller Puckette's avatar
Miller Puckette committed
269 270 271 272
        strcpy(buf + length, string);
        length = newlength;
        if (ap->a_type == A_SEMI) buf[length-1] = '\n';
        else buf[length-1] = ' ';
273
        //if (ap->a_type == A_COMMA) length--;
Miller Puckette's avatar
Miller Puckette committed
274 275 276 277 278 279 280 281 282
    }
    if (length && buf[length-1] == ' ')
    {
        if (newbuf = t_resizebytes(buf, length, length-1))
        {
            buf = newbuf;
            length--;
        }
    }
283
    //fprintf(stderr,"binbuf_gettext: <%s>\n", buf);
Miller Puckette's avatar
Miller Puckette committed
284 285 286 287 288 289 290 291 292
    *bufp = buf;
    *lengthp = length;
}

/* LATER improve the out-of-space behavior below.  Also fix this so that
writing to file doesn't buffer everything together. */

void binbuf_add(t_binbuf *x, int argc, t_atom *argv)
{
293
    //fprintf(stderr,"binbuf_add_started %d\n", argc);
Miller Puckette's avatar
Miller Puckette committed
294 295 296 297 298 299 300 301 302 303
    int newsize = x->b_n + argc, i;
    t_atom *ap;
    if (ap = t_resizebytes(x->b_vec, x->b_n * sizeof(*x->b_vec),
        newsize * sizeof(*x->b_vec)))
            x->b_vec = ap;
    else
    {
        error("binbuf_addmessage: out of space");
        return;
    }
304 305
    /*
    for (i=0; i < argc; i++) {
306 307 308 309 310 311 312 313
        if (argv[i].a_type == A_SYMBOL)
        {
            fprintf(stderr, "binbuf_add: %s\n", argv[i].a_w.w_symbol->s_name);
        }
        else if (argv[i].a_type == A_FLOAT)
        {
            fprintf(stderr, "binbuf_add: %g\n", argv[i].a_w.w_float);
        }
314 315
    }
    */
Miller Puckette's avatar
Miller Puckette committed
316 317 318 319 320 321 322 323
#if 0
    startpost("binbuf_add: ");
    postatom(argc, argv);
    endpost();
#endif
    for (ap = x->b_vec + x->b_n, i = argc; i--; ap++)
        *ap = *(argv++);
    x->b_n = newsize;
324
    //fprintf(stderr,"done binbuf_add\n");
Miller Puckette's avatar
Miller Puckette committed
325 326 327 328 329
}

#define MAXADDMESSV 100
void binbuf_addv(t_binbuf *x, char *fmt, ...)
{
330
    //fprintf(stderr,"binbuf_addv started\n");
Miller Puckette's avatar
Miller Puckette committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
    va_list ap;
    t_atom arg[MAXADDMESSV], *at =arg;
    int nargs = 0;
    char *fp = fmt;

    va_start(ap, fmt);
    while (1)
    {
        if (nargs >= MAXADDMESSV)
        {
            error("binbuf_addmessv: only %d allowed", MAXADDMESSV);
            break;
        }
        switch(*fp++)
        {
346
        case 'i': SETFLOAT(at, va_arg(ap, int));
347 348
                    //fprintf(stderr, "i = %f\n", at->a_w.w_float);
                    break;
349
        case 'f': SETFLOAT(at, va_arg(ap, double)); 
350 351
                    //fprintf(stderr, "f = %f\n", at->a_w.w_float);
                    break;
352
        case 's': SETSYMBOL(at, va_arg(ap, t_symbol *)); 
353 354
                    //fprintf(stderr, "s = %s\n", at->a_w.w_symbol->s_name);
                    break;
Miller Puckette's avatar
Miller Puckette committed
355 356 357 358 359 360 361 362 363
        case ';': SETSEMI(at); break;
        case ',': SETCOMMA(at); break;
        default: goto done;
        }
        at++;
        nargs++;
    }
done:
    va_end(ap);
364
    //fprintf(stderr,"done binbuf_addv\n");
Miller Puckette's avatar
Miller Puckette committed
365 366 367 368
    binbuf_add(x, nargs, arg);
}

/* add a binbuf to another one for saving.  Semicolons and commas go to
369 370
symbols ";", "'",; We assume here (probably incorrectly) that there's
no symbol whose name is ";" - should we be escaping those?. */
Miller Puckette's avatar
Miller Puckette committed
371 372 373

void binbuf_addbinbuf(t_binbuf *x, t_binbuf *y)
{
374
    //fprintf(stderr,"starting binbuf_addbinbuf\n");
Miller Puckette's avatar
Miller Puckette committed
375 376 377 378 379 380 381 382 383 384
    t_binbuf *z = binbuf_new();
    int i;
    t_atom *ap;
    binbuf_add(z, y->b_n, y->b_vec);
    for (i = 0, ap = z->b_vec; i < z->b_n; i++, ap++)
    {
        char tbuf[MAXPDSTRING];
        switch (ap->a_type)
        {
        case A_FLOAT:
385
            //fprintf(stderr,"addbinbuf: float\n");
Miller Puckette's avatar
Miller Puckette committed
386 387
            break;
        case A_SEMI:
388
            //fprintf(stderr,"addbinbuf: semi\n");
Miller Puckette's avatar
Miller Puckette committed
389 390 391
            SETSYMBOL(ap, gensym(";"));
            break;
        case A_COMMA:
392
            //fprintf(stderr,"addbinbuf: comma\n");
Miller Puckette's avatar
Miller Puckette committed
393 394 395
            SETSYMBOL(ap, gensym(","));
            break;
        case A_DOLLAR:
396
            //fprintf(stderr,"addbinbuf: dollar\n");
397
            if(ap->a_w.w_index==DOLLARALL){ /* JMZ: $@ expansion */
398
                SETSYMBOL(ap, gensym("$@"));
399
            } else {
400 401
                sprintf(tbuf, "$%d", ap->a_w.w_index);
                SETSYMBOL(ap, gensym(tbuf));
402
            }
Miller Puckette's avatar
Miller Puckette committed
403 404
            break;
        case A_DOLLSYM:
405
            //fprintf(stderr,"addbinbuf: dollsym\n");
Miller Puckette's avatar
Miller Puckette committed
406 407 408 409
            atom_string(ap, tbuf, MAXPDSTRING);
            SETSYMBOL(ap, gensym(tbuf));
            break;
        case A_SYMBOL:
410
            //fprintf(stderr,"addbinbuf: symbol\n");
Miller Puckette's avatar
Miller Puckette committed
411
                /* FIXME make this general */
412
            /*if (!strcmp(ap->a_w.w_symbol->s_name, ";"))
Miller Puckette's avatar
Miller Puckette committed
413 414
                SETSYMBOL(ap, gensym(";"));
            else if (!strcmp(ap->a_w.w_symbol->s_name, ","))
415
                SETSYMBOL(ap, gensym(","));*/
Miller Puckette's avatar
Miller Puckette committed
416 417 418 419 420 421 422
            break;
        default:
            bug("binbuf_addbinbuf");
        }
    }
    
    binbuf_add(x, z->b_n, z->b_vec);
423
    binbuf_free(z);
424
    //fprintf(stderr,"done binbuf_addbinbuf\n");
Miller Puckette's avatar
Miller Puckette committed
425 426 427 428 429 430 431
}

void binbuf_addsemi(t_binbuf *x)
{
    t_atom a;
    SETSEMI(&a);
    binbuf_add(x, 1, &a);
432
    //binbuf_add(x, 1, '\0');
Miller Puckette's avatar
Miller Puckette committed
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
}

/* Supply atoms to a binbuf from a message, making the opposite changes
from binbuf_addbinbuf.  The symbol ";" goes to a semicolon, etc. */

void binbuf_restore(t_binbuf *x, int argc, t_atom *argv)
{
    int newsize = x->b_n + argc, i;
    t_atom *ap;
    if (ap = t_resizebytes(x->b_vec, x->b_n * sizeof(*x->b_vec),
        newsize * sizeof(*x->b_vec)))
            x->b_vec = ap;
    else
    {
        error("binbuf_addmessage: out of space");
        return;
    }

    for (ap = x->b_vec + x->b_n, i = argc; i--; ap++)
    {
        if (argv->a_type == A_SYMBOL)
        {
            char *str = argv->a_w.w_symbol->s_name, *str2;
            if (!strcmp(str, ";")) SETSEMI(ap);
            else if (!strcmp(str, ",")) SETCOMMA(ap);
458 459 460
            else if (!strcmp(str, "$@")) /* JMZ: $@ expansion */
            {
                ap->a_type = A_DOLLAR;
461
                ap->a_w.w_index = DOLLARALL;
462
            }
Miller Puckette's avatar
Miller Puckette committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
            else if ((str2 = strchr(str, '$')) && str2[1] >= '0'
                && str2[1] <= '9')
            {
                int dollsym = 0;
                if (*str != '$')
                    dollsym = 1;
                else for (str2 = str + 1; *str2; str2++)
                    if (*str2 < '0' || *str2 > '9')
                {
                    dollsym = 1;
                    break;
                }
                if (dollsym)
                    SETDOLLSYM(ap, gensym(str));
                else
                {
                    int dollar = 0;
                    sscanf(argv->a_w.w_symbol->s_name + 1, "%d", &dollar);
                    SETDOLLAR(ap, dollar);
                }
            }
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
            else if (strchr(argv->a_w.w_symbol->s_name, '\\'))
            {
                char buf[MAXPDSTRING], *sp1, *sp2;
                int slashed = 0;
                for (sp1 = buf, sp2 = argv->a_w.w_symbol->s_name;
                    *sp2 && sp1 < buf + (MAXPDSTRING-1);
                        sp2++)
                {
                    if (slashed)
                        *sp1++ = *sp2;
                    else if (*sp2 == '\\')
                        slashed = 1;
                    else *sp1++ = *sp2, slashed = 0;
                }
                *sp1 = 0;
                SETSYMBOL(ap, gensym(buf));
            }
Miller Puckette's avatar
Miller Puckette committed
501 502 503 504 505 506 507 508
            else *ap = *argv;
            argv++;
        }
        else *ap = *(argv++);
    }
    x->b_n = newsize;
}

509 510 511
#define ISSYMBOL(a, b) ((a)->a_type == A_SYMBOL && \
    !strcmp((a)->a_w.w_symbol->s_name, (b)))

Miller Puckette's avatar
Miller Puckette committed
512 513 514 515 516 517 518 519 520 521 522
void binbuf_print(t_binbuf *x)
{
    int i, startedpost = 0, newline = 1;
    for (i = 0; i < x->b_n; i++)
    {
        if (newline)
        {
            if (startedpost) endpost();
            startpost("");
            startedpost = 1;
        }
523 524
        if (ISSYMBOL(x->b_vec + i, "%hidden%"))
            i = x->b_n - 1;
Miller Puckette's avatar
Miller Puckette committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
        postatom(1, x->b_vec + i);
        if (x->b_vec[i].a_type == A_SEMI)
            newline = 1;
        else newline = 0; 
    }
    if (startedpost) endpost();
}

int binbuf_getnatom(t_binbuf *x)
{
    return (x->b_n);
}

t_atom *binbuf_getvec(t_binbuf *x)
{
    return (x->b_vec);
}

543 544 545 546 547 548 549 550 551
int binbuf_resize(t_binbuf *x, int newsize)
{
    t_atom *new = t_resizebytes(x->b_vec,
        x->b_n * sizeof(*x->b_vec), newsize * sizeof(*x->b_vec));
    if (new)
        x->b_vec = new, x->b_n = newsize;
    return (new != 0);
}

Miller Puckette's avatar
Miller Puckette committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
int canvas_getdollarzero( void);

/* JMZ:
 * s points to the first character after the $
 * (e.g. if the org.symbol is "$1-bla", then s will point to "1-bla")
 * (e.g. org.symbol="hu-$1mu", s="1mu")
 * LATER: think about more complex $args, like ${$1+3}
 *
 * the return value holds the length of the $arg (in most cases: 1)
 * buf holds the expanded $arg
 *
 * if some error occured, "-1" is returned
 *
 * e.g. "$1-bla" with list "10 20 30"
 * s="1-bla"
 * buf="10"
 * return value = 1; (s+1=="-bla")
 */
570 571
int binbuf_expanddollsym(char*s, char*buf,t_atom dollar0, int ac, t_atom *av,
    int tonew)
Miller Puckette's avatar
Miller Puckette committed
572 573 574 575 576 577 578
{
  int argno=atol(s);
  int arglen=0;
  char*cs=s;
  char c=*cs;
  *buf=0;

579 580 581
  while(c && (c >= '0') && (c <= '9'))
  {
    c = *cs++;
Miller Puckette's avatar
Miller Puckette committed
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
    arglen++;
  }

  if (cs==s) { /* invalid $-expansion (like "$bla") */
    sprintf(buf, "$");
    return 0;
  }
  else if (argno < 0 || argno > ac) /* undefined argument */
    {
      if(!tonew)return 0;
      sprintf(buf, "$%d", argno);
    }
  else if (argno == 0){ /* $0 */
    atom_string(&dollar0, buf, MAXPDSTRING/2-1);
  }
  else{ /* fine! */
    atom_string(av+(argno-1), buf, MAXPDSTRING/2-1);
  }
  return (arglen-1);
}

/* LATER remove the dependence on the current canvas for $0; should be another
argument. */
t_symbol *binbuf_realizedollsym(t_symbol *s, int ac, t_atom *av, int tonew)
{
    char buf[MAXPDSTRING];
    char buf2[MAXPDSTRING];
    char*str=s->s_name;
    char*substr;
    int next=0, i=MAXPDSTRING;
    t_atom dollarnull;
    SETFLOAT(&dollarnull, canvas_getdollarzero());
    while(i--)buf2[i]=0;

#if 1
617 618
    /* JMZ: currently, a symbol is detected to be
     * A_DOLLSYM if it starts with '$'
Miller Puckette's avatar
Miller Puckette committed
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
     * the leading $ is stripped and the rest stored in "s"
     * i would suggest to NOT strip the leading $
     * and make everything a A_DOLLSYM that contains(!) a $
     *
     * whenever this happened, enable this code
     */
    substr=strchr(str, '$');
    if (!substr || substr-str >= MAXPDSTRING)
        return (s);

    strncat(buf2, str, (substr-str));
    str=substr+1;

#endif

    while((next=binbuf_expanddollsym(str, buf, dollarnull, ac, av, tonew))>=0)
    {
        /*
        * JMZ: i am not sure what this means, so i might have broken it
        * it seems like that if "tonew" is set and the $arg cannot be expanded
        * (or the dollarsym is in reality a A_DOLLAR)
        * 0 is returned from binbuf_realizedollsym
        * this happens, when expanding in a message-box, but does not happen
        * when the A_DOLLSYM is the name of a subpatch
        */
        if(!tonew&&(0==next)&&(0==*buf))
        {
            return 0; /* JMZ: this should mimick the original behaviour */
        }

        strncat(buf2, buf, MAXPDSTRING/2-1);
        str+=next;
        substr=strchr(str, '$');
        if(substr)
        {
            strncat(buf2, str, (substr-str));
            str=substr+1;
        } 
        else
        {
            strcat(buf2, str);
            goto done;
        }
    }
done:
    return (gensym(buf2));
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
#define SMALLMSG 5
#define HUGEMSG 1000
#ifdef MSW
#include <malloc.h>
#else
#include <alloca.h>
#endif
#if HAVE_ALLOCA
#define ATOMS_ALLOCA(x, n) ((x) = (t_atom *)((n) < HUGEMSG ?  \
        alloca((n) * sizeof(t_atom)) : getbytes((n) * sizeof(t_atom))))
#define ATOMS_FREEA(x, n) ( \
    ((n) < HUGEMSG || (freebytes((x), (n) * sizeof(t_atom)), 0)))
#else
#define ATOMS_ALLOCA(x, n) ((x) = (t_atom *)getbytes((n) * sizeof(t_atom)))
#define ATOMS_FREEA(x, n) (freebytes((x), (n) * sizeof(t_atom)))
#endif

Miller Puckette's avatar
Miller Puckette committed
684 685
void binbuf_eval(t_binbuf *x, t_pd *target, int argc, t_atom *argv)
{
686
    t_atom smallstack[SMALLMSG], *mstack, *msp;
Miller Puckette's avatar
Miller Puckette committed
687 688
    t_atom *at = x->b_vec;
    int ac = x->b_n;
689
    int nargs, maxnargs = 0;
690 691 692 693
    int at_arg = 0;

    //first we need to check if the list of arguments has $@
    //fprintf(stderr,"=========\nbinbuf_eval argc:%d ac:%d\n", argc, (int)ac);
694 695
    int count, old_ac = ac;
    for (count = 0; count < old_ac; count++)
696 697 698
    {
        //fprintf(stderr, "count %d\n", count);
        if (at[count].a_type == A_DOLLAR &&
699
            at[count].a_w.w_index==DOLLARALL)
700 701 702 703 704 705 706
        {
            //fprintf(stderr,"found @ count:%d ac:%d argc:%d ac+argc-1:%d\n",
            //    count, ac, argc, ac+argc-1);
            ac = ac + argc;
            maxnargs = ac;
        }
    }
707 708

    if (ac <= SMALLMSG)
709 710 711 712 713 714 715 716 717 718 719
        mstack = smallstack;
    else
    {
#if 1
            /* count number of args in biggest message.  The wierd
            treatment of "pd_objectmaker" is because when the message
            goes out to objectmaker, commas and semis are passed
            on as regular args (see below).  We're tacitly assuming here
            that the pd_objectmaker target can't come up via a named
            destination in the message, only because the original "target"
            points there. */
720
        if (target == &pd_objectmaker) {
721
            maxnargs = ac;
722 723 724
            //if (at && x->b_n) fprintf(stderr,"pd_objectmaker %s %d %d %d\n",
            //    at[0].a_w.w_symbol->s_name, ac, argc, maxnargs);
        }
725
        else if (!maxnargs) //we set maxnargs=ac if we encounter $@ arg (see above) so we avoid this place because A_SEMI and A_COMMA tend to be detected at location 220. Why? No idea. Perhaps it is a line delimiter for one line in the saved file?
726 727
        {
            int i, j = (target ? 0 : -1);
728
            for (i = 0; i < ac; i++)
729
            {
730
                if (at[i].a_type == A_SEMI) {
731
                    j = -1;
732
                    //fprintf(stderr,"A_SEMI %d\n", i);
733
                } else if (at[i].a_type == A_COMMA) {
734
                    j = 0;
735
                    //fprintf(stderr,"A_COMMA %d\n", i);
736
                } else if (++j > maxnargs) {
737
                    maxnargs = j;
738
                }
739
            }
740
            //fprintf(stderr,"maxnargs = %d\n", maxnargs);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
        }
        if (maxnargs <= SMALLMSG)
            mstack = smallstack;
        else ATOMS_ALLOCA(mstack, maxnargs);
#else
            /* just pessimistically allocate enough to hold everything
            at once.  This turned out to run slower in a simple benchmark
            I tried, perhaps because the extra memory allocation
            hurt the cache hit rate. */
        maxnargs = ac;
        ATOMS_ALLOCA(mstack, maxnargs);
#endif

    }
    msp = mstack;
756 757
    //fprintf(stderr,"maxnargs=%d ac=%d argc=%d\n", maxnargs, ac, argc);
    //static t_atom *ems = mstack+MSTACKSIZE;
Miller Puckette's avatar
Miller Puckette committed
758 759 760 761 762 763 764 765 766 767 768 769
    while (1)
    {
        t_pd *nexttarget;
            /* get a target. */
        while (!target)
        {
            t_symbol *s;
            while (ac && (at->a_type == A_SEMI || at->a_type == A_COMMA))
                ac--,  at++;
            if (!ac) break;
            if (at->a_type == A_DOLLAR)
            {
770
                /* would it make sense to consider $@ here? */
Miller Puckette's avatar
Miller Puckette committed
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
                if (at->a_w.w_index <= 0 || at->a_w.w_index > argc)
                {
                    error("$%d: not enough arguments supplied",
                            at->a_w.w_index);
                    goto cleanup; 
                }
                else if (argv[at->a_w.w_index-1].a_type != A_SYMBOL)
                {
                    error("$%d: symbol needed as message destination",
                        at->a_w.w_index);
                    goto cleanup; 
                }
                else s = argv[at->a_w.w_index-1].a_w.w_symbol;
            }
            else if (at->a_type == A_DOLLSYM)
            {
                if (!(s = binbuf_realizedollsym(at->a_w.w_symbol,
                    argc, argv, 0)))
                {
                    error("$%s: not enough arguments supplied",
                        at->a_w.w_symbol->s_name);
                    goto cleanup;
                }
            }
            else s = atom_getsymbol(at);
            if (!(target = s->s_thing))
            {
                error("%s: no such object", s->s_name);
            cleanup:
                do at++, ac--;
                while (ac && at->a_type != A_SEMI);
                    /* LATER eat args until semicolon and continue */
                continue;
            }
            else
            {
                at++, ac--;
                break;
            }
        }
        if (!ac) break;
        nargs = 0;
        nexttarget = target;
        while (1)
        {
            t_symbol *s9;
            if (!ac) goto gotmess;
            switch (at->a_type)
            {
            case A_SEMI:
                    /* semis and commas in new message just get bashed to
                    a symbol.  This is needed so you can pass them to "expr." */
                if (target == &pd_objectmaker)
                {
                    SETSYMBOL(msp, gensym(";"));
                    break;
                }
                else
                {
                    nexttarget = 0;
                    goto gotmess;
                }
            case A_COMMA:
                if (target == &pd_objectmaker)
                {
                    SETSYMBOL(msp, gensym(","));
                    break;
                }
                else goto gotmess;
            case A_FLOAT:
            case A_SYMBOL:
                *msp = *at;
                break;
            case A_DOLLAR:
845
                if (at->a_w.w_index==DOLLARALL)
846 847 848 849 850 851 852
                { /* JMZ: $@ expansion */
                    int i;
                    //if(msp+argc >= ems) 
                    //{
                    //    error("message stack overflow");
                    //    goto broken;
                    //}
853 854 855 856 857 858 859
                    for (i=0; i<argc; i++)
                    {
                        //fprintf(stderr, "@: %d %d\n", i, maxnargs);
                        *msp++=argv[i];
                        nargs++;
                        ac--;
                    }
860 861
                    msp--;
                    nargs--;
862 863 864
                    //fprintf(stderr,"x->b_n=%d ac=%d maxnargs=%d "
                    //               "nargs=%d argc=%d\n",
                    //    x->b_n, ac, maxnargs, nargs, argc);
865 866
                }
                else if (at->a_w.w_index > 0 && at->a_w.w_index <= argc)
867 868 869
                    *msp = argv[at->a_w.w_index-1];
                else if (at->a_w.w_index == 0)
                    SETFLOAT(msp, canvas_getdollarzero());
Miller Puckette's avatar
Miller Puckette committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
                else
                {
                    if (target == &pd_objectmaker)
                        SETFLOAT(msp, 0);
                    else
                    {
                        error("$%d: argument number out of range",
                            at->a_w.w_index);
                        SETFLOAT(msp, 0);
                    }
                }
                break;
            case A_DOLLSYM:
                s9 = binbuf_realizedollsym(at->a_w.w_symbol, argc, argv,
                    target == &pd_objectmaker);
                if (!s9)
                {
887 888
                    error("%s: argument number out of range",
                        at->a_w.w_symbol->s_name);
Miller Puckette's avatar
Miller Puckette committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
                    SETSYMBOL(msp, at->a_w.w_symbol);
                }
                else SETSYMBOL(msp, s9);
                break;
            default:
                bug("bad item in binbuf");
                goto broken;
            }
            msp++;
            ac--;
            at++;
            nargs++;
        }
    gotmess:
        if (nargs)
        {
905
            switch (mstack->a_type)
Miller Puckette's avatar
Miller Puckette committed
906 907
            {
            case A_SYMBOL:
908
                typedmess(target, mstack->a_w.w_symbol, nargs-1, mstack+1);
Miller Puckette's avatar
Miller Puckette committed
909 910
                break;
            case A_FLOAT:
911 912
                if (nargs == 1) pd_float(target, mstack->a_w.w_float);
                else pd_list(target, 0, nargs, mstack);
Miller Puckette's avatar
Miller Puckette committed
913
                break;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
914 915 916 917
            case A_BLOB: /* MP 20070106 blob type */
                if (nargs == 1) pd_blob(target, mstack->a_w.w_blob);
                else pd_list(target, 0, nargs, mstack);
                break;
Miller Puckette's avatar
Miller Puckette committed
918 919
            }
        }
920
        msp = mstack;
Miller Puckette's avatar
Miller Puckette committed
921 922 923 924 925
        if (!ac) break;
        target = nexttarget;
        at++;
        ac--;
    }
926 927 928
broken: 
    if (maxnargs > SMALLMSG)
         ATOMS_FREEA(mstack, maxnargs);
Miller Puckette's avatar
Miller Puckette committed
929 930 931 932
}

static int binbuf_doopen(char *s, int mode)
{
933
    char namebuf[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
934 935 936 937 938 939 940 941 942
#ifdef MSW
    mode |= O_BINARY;
#endif
    sys_bashfilename(s, namebuf);
    return (open(namebuf, mode));
}

static FILE *binbuf_dofopen(char *s, char *mode)
{
943
    char namebuf[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
944 945 946 947 948 949 950 951 952 953
    sys_bashfilename(s, namebuf);
    return (fopen(namebuf, mode));
}

int binbuf_read(t_binbuf *b, char *filename, char *dirname, int crflag)
{
    long length;
    int fd;
    int readret;
    char *buf;
954
    char namebuf[MAXPDSTRING];
Miller Puckette's avatar
Miller Puckette committed
955 956
    
    if (*dirname)
957 958 959 960
        snprintf(namebuf, MAXPDSTRING-1, "%s/%s", dirname, filename);
    else
        snprintf(namebuf, MAXPDSTRING-1, "%s", filename);
    namebuf[MAXPDSTRING-1] = 0;
Miller Puckette's avatar
Miller Puckette committed
961 962 963
    
    if ((fd = binbuf_doopen(namebuf, 0)) < 0)
    {
964
        //fprintf(stderr, "open: ");
Miller Puckette's avatar
Miller Puckette committed
965 966 967 968 969 970
        perror(namebuf);
        return (1);
    }
    if ((length = lseek(fd, 0, SEEK_END)) < 0 || lseek(fd, 0, SEEK_SET) < 0 
        || !(buf = t_getbytes(length)))
    {
971
        //fprintf(stderr, "lseek: ");
Miller Puckette's avatar
Miller Puckette committed
972 973 974 975 976 977
        perror(namebuf);
        close(fd);
        return(1);
    }
    if ((readret = read(fd, buf, length)) < length)
    {
978
        //fprintf(stderr, "read (%d %ld) -> %d\n", fd, length, readret);
Miller Puckette's avatar
Miller Puckette committed
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
        perror(namebuf);
        close(fd);
        t_freebytes(buf, length);
        return(1);
    }
        /* optionally map carriage return to semicolon */
    if (crflag)
    {
        int i;
        for (i = 0; i < length; i++)
            if (buf[i] == '\n')
                buf[i] = ';';
    }
    binbuf_text(b, buf, length);

#if 0
    startpost("binbuf_read "); postatom(b->b_n, b->b_vec); endpost();
#endif

    t_freebytes(buf, length);
    close(fd);
    return (0);
}

    /* read a binbuf from a file, via the search patch of a canvas */
int binbuf_read_via_canvas(t_binbuf *b, char *filename, t_canvas *canvas,
    int crflag)
{
    int filedesc;
1008
    char buf[MAXPDSTRING], *bufptr;
Miller Puckette's avatar
Miller Puckette committed
1009
    if ((filedesc = canvas_open(canvas, filename, "",
1010
        buf, &bufptr, MAXPDSTRING, 0)) < 0)
Miller Puckette's avatar
Miller Puckette committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    {
        error("%s: can't open", filename);
        return (1);
    }
    else close (filedesc);
    if (binbuf_read(b, bufptr, buf, crflag))
        return (1);
    else return (0);
}

    /* old version */
int binbuf_read_via_path(t_binbuf *b, char *filename, char *dirname,
    int crflag)
{
    int filedesc;
1026
    char buf[MAXPDSTRING], *bufptr;
Miller Puckette's avatar
Miller Puckette committed
1027
    if ((filedesc = open_via_path(
1028
        dirname, filename, "", buf, &bufptr, MAXPDSTRING, 0)) < 0)
Miller Puckette's avatar
Miller Puckette committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    {
        error("%s: can't open", filename);
        return (1);
    }
    else close (filedesc);
    if (binbuf_read(b, bufptr, buf, crflag))
        return (1);
    else return (0);
}

#define WBUFSIZE 4096
static t_binbuf *binbuf_convert(t_binbuf *oldb, int maxtopd);

    /* write a binbuf to a text file.  If "crflag" is set we suppress
    semicolons. */
int binbuf_write(t_binbuf *x, char *filename, char *dir, int crflag)
{
    FILE *f = 0;
1047
    char sbuf[WBUFSIZE], fbuf[MAXPDSTRING], *bp = sbuf, *ep = sbuf + WBUFSIZE;
Miller Puckette's avatar
Miller Puckette committed
1048 1049 1050 1051 1052
    t_atom *ap;
    int indx, deleteit = 0;
    int ncolumn = 0;

    if (*dir)
1053 1054 1055 1056 1057
        snprintf(fbuf, MAXPDSTRING-1, "%s/%s", dir, filename);
    else
        snprintf(fbuf, MAXPDSTRING-1, "%s", filename);
    fbuf[MAXPDSTRING-1] = 0;

Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1058 1059
    if (!strcmp(filename + strlen(filename) - 4, ".pat") ||
        !strcmp(filename + strlen(filename) - 4, ".mxt"))
Miller Puckette's avatar
Miller Puckette committed
1060 1061 1062 1063 1064 1065 1066
    {
        x = binbuf_convert(x, 0);
        deleteit = 1;
    }
    
    if (!(f = binbuf_dofopen(fbuf, "w")))
    {
1067
        //fprintf(stderr, "open: ");
Miller Puckette's avatar
Miller Puckette committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
        sys_unixerror(fbuf);
        goto fail;
    }
    for (ap = x->b_vec, indx = x->b_n; indx--; ap++)
    {
        int length;
            /* estimate how many characters will be needed.  Printing out
            symbols may need extra characters for inserting backslashes. */
        if (ap->a_type == A_SYMBOL || ap->a_type == A_DOLLSYM)
            length = 80 + strlen(ap->a_w.w_symbol->s_name);
        else length = 40;
        if (ep - bp < length)
        {
            if (fwrite(sbuf, bp-sbuf, 1, f) < 1)
            {
                sys_unixerror(fbuf);
                goto fail;
            }
            bp = sbuf;
        }
        if ((ap->a_type == A_SEMI || ap->a_type == A_COMMA) &&
            bp > sbuf && bp[-1] == ' ') bp--;
        if (!crflag || ap->a_type != A_SEMI)
        {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
1092 1093 1094
            char bp2[WBUFSIZE];
            atom_string(ap, bp2, WBUFSIZE);
            strnescape(bp, bp2, (ep-bp)-2);
1095
            //atom_string(ap, bp, (ep-bp)-2);
Miller Puckette's avatar
Miller Puckette committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
            length = strlen(bp);
            bp += length;
            ncolumn += length;
        }
        if (ap->a_type == A_SEMI || (!crflag && ncolumn > 65))
        {
            *bp++ = '\n';
            ncolumn = 0;
        }
        else
        {
            *bp++ = ' ';
            ncolumn++;
        }
    }
    if (fwrite(sbuf, bp-sbuf, 1, f) < 1)
    {
        sys_unixerror(fbuf);
        goto fail;
    }
1116 1117 1118 1119 1120 1121 1122 1123


    if (fflush(f) != 0) 
    {
        sys_unixerror(fbuf);
        goto fail;
    }

Miller Puckette's avatar
Miller Puckette committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    if (deleteit)
        binbuf_free(x);
    fclose(f);
    return (0);
fail:
    if (deleteit)
        binbuf_free(x);
    if (f)
        fclose(f);
    return (1);
}

/* The following routine attempts to convert from max to pd or back.  The
max to pd direction is working OK but you will need to make lots of 
abstractions for objects like "gate" which don't exist in Pd.  conversion
from Pd to Max hasn't been tested for patches with subpatches yet!  */

#define MAXSTACK 1000

static t_binbuf *binbuf_convert(t_binbuf *oldb, int maxtopd)
{
    t_binbuf *newb = binbuf_new();
    t_atom *vec = oldb->b_vec;
1147
    t_int n = oldb->b_n, nextindex, stackdepth = 0, stack[MAXSTACK] = {0},
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1148
        nobj = 0, i, gotfontsize = 0;
Miller Puckette's avatar
Miller Puckette committed
1149
    t_atom outmess[MAXSTACK], *nextmess;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1150
    t_float fontsize = 10;
Miller Puckette's avatar
Miller Puckette committed
1151 1152 1153 1154 1155
    if (!maxtopd)
        binbuf_addv(newb, "ss;", gensym("max"), gensym("v2"));
    for (nextindex = 0; nextindex < n; )
    {
        int endmess, natom;
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1156
        char *first, *second, *third;
Miller Puckette's avatar
Miller Puckette committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
        for (endmess = nextindex; endmess < n && vec[endmess].a_type != A_SEMI;
            endmess++)
                ;
        if (endmess == n) break;
        if (endmess == nextindex || endmess == nextindex + 1
            || vec[nextindex].a_type != A_SYMBOL ||
                vec[nextindex+1].a_type != A_SYMBOL)
        {
            nextindex = endmess + 1;
            continue;
        }
        natom = endmess - nextindex;
        if (natom > MAXSTACK-10) natom = MAXSTACK-10;
        nextmess = vec + nextindex;
        first = nextmess->a_w.w_symbol->s_name;
        second = (nextmess+1)->a_w.w_symbol->s_name;
        if (maxtopd)
        {
                /* case 1: importing a ".pat" file into Pd. */
                
                /* dollar signs in file translate to symbols */
            for (i = 0; i < natom; i++)
            {
                if (nextmess[i].a_type == A_DOLLAR)
                {
                    char buf[100];
                    sprintf(buf, "$%d", nextmess[i].a_w.w_index);
                    SETSYMBOL(nextmess+i, gensym(buf));
                }
                else if (nextmess[i].a_type == A_DOLLSYM)
                {
                    char buf[100];
                    sprintf(buf, "%s", nextmess[i].a_w.w_symbol->s_name);
                    SETSYMBOL(nextmess+i, gensym(buf));
                }
            }
            if (!strcmp(first, "#N"))
            {
                if (!strcmp(second, "vpatcher"))
                {
                    if (stackdepth >= MAXSTACK)
                    {
1199
                        error("stack depth exceeded: too many embedded patches");
Miller Puckette's avatar
Miller Puckette committed
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
                        return (newb);
                    }
                    stack[stackdepth] = nobj;
                    stackdepth++;
                    nobj = 0;
                    binbuf_addv(newb, "ssfffff;", 
                        gensym("#N"), gensym("canvas"),
                            atom_getfloatarg(2, natom, nextmess),
                            atom_getfloatarg(3, natom, nextmess),
                            atom_getfloatarg(4, natom, nextmess) -
                                atom_getfloatarg(2, natom, nextmess),
                            atom_getfloatarg(5, natom, nextmess) -
                                atom_getfloatarg(3, natom, nextmess),
1213
                            (t_float)sys_defaultfont);
Miller Puckette's avatar
Miller Puckette committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
                }
            }
            if (!strcmp(first, "#P"))
            {
                    /* drop initial "hidden" flag */
                if (!strcmp(second, "hidden"))
                {
                    nextmess++;
                    natom--;
                    second = (nextmess+1)->a_w.w_symbol->s_name;
                }
                if (natom >= 7 && !strcmp(second, "newobj")
                    && (ISSYMBOL(&nextmess[6], "patcher") ||
                        ISSYMBOL(&nextmess[6], "p")))
                {
                    binbuf_addv(newb, "ssffss;",
                        gensym("#X"), gensym("restore"),
                        atom_getfloatarg(2, natom, nextmess),
                        atom_getfloatarg(3, natom, nextmess),
                        gensym("pd"), atom_getsymbolarg(7, natom, nextmess));
                    if (stackdepth) stackdepth--;
                    nobj = stack[stackdepth];
                    nobj++;
                }
                else if (!strcmp(second, "newex") || !strcmp(second, "newobj"))
                {
                    t_symbol *classname =
                        atom_getsymbolarg(6, natom, nextmess);
                    if (classname == gensym("trigger") ||
                        classname == gensym("t"))
                    {
                        for (i = 7; i < natom; i++)
                            if (nextmess[i].a_type == A_SYMBOL &&
                                nextmess[i].a_w.w_symbol == gensym("i"))
                                    nextmess[i].a_w.w_symbol = gensym("f");
                    }
                    if (classname == gensym("table"))
                        classname = gensym("TABLE");
                    SETSYMBOL(outmess, gensym("#X"));
                    SETSYMBOL(outmess + 1, gensym("obj"));
                    outmess[2] = nextmess[2];
                    outmess[3] = nextmess[3];
                    SETSYMBOL(outmess+4, classname);
                    for (i = 7; i < natom; i++)
                        outmess[i-2] = nextmess[i];
                    SETSEMI(outmess + natom - 2);
                    binbuf_add(newb, natom - 1, outmess);
                    nobj++;
                }
                else if (!strcmp(second, "message") || 
                    !strcmp(second, "comment"))
                {
                    SETSYMBOL(outmess, gensym("#X"));
                    SETSYMBOL(outmess + 1, gensym(
                        (strcmp(second, "message") ? "text" : "msg")));
                    outmess[2] = nextmess[2];
                    outmess[3] = nextmess[3];
                    for (i = 6; i < natom; i++)
                        outmess[i-2] = nextmess[i];
                    SETSEMI(outmess + natom - 2);
                    binbuf_add(newb, natom - 1, outmess);
                    nobj++;
                }
                else if (!strcmp(second, "button"))
                {
                    binbuf_addv(newb, "ssffs;",
                        gensym("#X"), gensym("obj"),
                        atom_getfloatarg(2, natom, nextmess),
                        atom_getfloatarg(3, natom, nextmess),
                        gensym("bng"));
                    nobj++;
                }
                else if (!strcmp(second, "number") || !strcmp(second, "flonum"))
                {
                    binbuf_addv(newb, "ssff;",
                        gensym("#X"), gensym("floatatom"),
                        atom_getfloatarg(2, natom, nextmess),
                        atom_getfloatarg(3, natom, nextmess));
                    nobj++;
                }
                else if (!strcmp(second, "slider"))
                {
1296
                    t_float inc = atom_getfloatarg(7, natom, nextmess);
Miller Puckette's avatar
Miller Puckette committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
                    if (inc <= 0)
                        inc = 1;
                    binbuf_addv(newb, "ssffsffffffsssfffffffff;",
                        gensym("#X"), gensym("obj"),
                        atom_getfloatarg(2, natom, nextmess),
                        atom_getfloatarg(3, natom, nextmess),
                        gensym("vsl"),
                        atom_getfloatarg(4, natom, nextmess),
                        atom_getfloatarg(5, natom, nextmess),
                        atom_getfloatarg(6, natom, nextmess),
                        atom_getfloatarg(6, natom, nextmess)
                            + (atom_getfloatarg(5, natom, nextmess) - 1) * inc,
                        0., 0.,
                        gensym("empty"), gensym("empty"), gensym("empty"),
                        0., -8., 0., 8., -262144., -1., -1., 0., 1.);
                    nobj++;
                }
                else if (!strcmp(second, "toggle"))
                {
                    binbuf_addv(newb, "ssffs;",
                        gensym("#X"), gensym("obj"),
                        atom_getfloatarg(2, natom, nextmess),
                        atom_getfloatarg(3, natom, nextmess),
                        gensym("tgl"));
                    nobj++;
                }
                else if (!strcmp(second, "inlet"))
                {
                    binbuf_addv(newb, "ssffs;",
                        gensym("#X"), gensym("obj"),
                        atom_getfloatarg(2, natom, nextmess),
                        atom_getfloatarg(3, natom, nextmess),
                        gensym((natom > 5 ? "inlet~" : "inlet"))); 
                    nobj++;
                }
                else if (!strcmp(second, "outlet"))
                {
                    binbuf_addv(newb, "ssffs;",
                        gensym("#X"), gensym("obj"),
                        atom_getfloatarg(2, natom, nextmess),
                        atom_getfloatarg(3, natom, nextmess),
                        gensym((natom > 5 ? "outlet~" : "outlet"))); 
                    nobj++;
                }
                else if (!strcmp(second, "user"))
                {
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1343 1344 1345 1346
                    third = (nextmess+2)->a_w.w_symbol->s_name;
                    if (!strcmp(third, "hslider"))
                    {
                        t_float range = atom_getfloatarg(7, natom, nextmess);
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
1347
                        //t_float multiplier = atom_getfloatarg(8, natom, nextmess);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
                        t_float offset = atom_getfloatarg(9, natom, nextmess);
                        binbuf_addv(newb, "ssffsffffffsssfffffffff;",
                                    gensym("#X"), gensym("obj"),
                                    atom_getfloatarg(3, natom, nextmess),
                                    atom_getfloatarg(4, natom, nextmess),
                                    gensym("hsl"),
                                    atom_getfloatarg(6, natom, nextmess),
                                    atom_getfloatarg(5, natom, nextmess),
                                    offset,
                                    range + offset,
                                    0., 0.,
                                    gensym("empty"), gensym("empty"), gensym("empty"),
                                    0., -8., 0., 8., -262144., -1., -1., 0., 1.); 
                   }
                    else if (!strcmp(third, "uslider"))
                    {
                        t_float range = atom_getfloatarg(7, natom, nextmess);
Mathieu L Bouchard's avatar
Mathieu L Bouchard committed
1365
                        //t_float multiplier = atom_getfloatarg(8, natom, nextmess);
Hans-Christoph Steiner's avatar
Hans-Christoph Steiner committed
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
                        t_float offset = atom_getfloatarg(9, natom, nextmess);
                        binbuf_addv(newb, "ssffsffffffsssfffffffff;",
                                    gensym("#X"), gensym("obj"),
                                    atom_getfloatarg(3, natom, nextmess),
                                    atom_getfloatarg(4, natom, nextmess),
                                    gensym("vsl"),
                                    atom_getfloatarg(5, natom, nextmess),
                                    atom_getfloatarg(6, natom, nextmess),
                                    offset,
                                    range + offset,
                                    0., 0.,
                                    gensym("empty"), gensym("empty"), gensym("empty"),
                                    0., -8., 0., 8., -262144., -1., -1., 0., 1.);
                    }
                    else
                        binbuf_addv(newb, "ssffs;",
                                    gensym("#X"), gensym("obj"),
                                    atom_getfloatarg(3, natom, nextmess),
                                    atom_getfloatarg(4, natom, nextmess),
                                    atom_getsymbolarg(2, natom, nextmess));
Miller Puckette's avatar
Miller Puckette committed
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 </