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
|
/*
FvwmButtons v2.0.41-plural-Z-alpha, copyright 1996, Jarl Totland
* This module, and the entire GoodStuff program, and the concept for
* interfacing this module to the Window Manager, are all original work
* by Robert Nation
*
* Copyright 1993, Robert Nation. No guarantees or warantees or anything
* are provided or implied in any way whatsoever. Use this program at your
* own risk. Permission to use this program for any purpose is given,
* as long as the copyright is kept intact.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
/*
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xproto.h>
*/
#include "FvwmButtons.h"
#include "button.h"
extern int w,h,x,y,xneg,yneg;
extern char *config_file;
/* ----------------------------------- macros ------------------------------ */
#define trimleft(s) {while(*s==' '||*s=='\t'||*s=='\n')(s)++;}
#define trimright(s) { \
char *t=(s);while(*t!=0) t++; \
if(t>s) while(*(t-1)==' '||*(t-1)=='\t'||*(t-1)=='\n') t--; \
*t=0;}
/**
*** seekright()
***
*** split off the first continous section of the string into a new allocated
*** string and move the old pointer forward. Accepts and strips quoting with
*** '," or `, and the current quote q can be escaped inside the string with \q.
**/
char *seekright(char **s)
{
char *b,*t,*r,q;
trimleft(*s);
q=**s;
if(q=='"' || q=='`' || q=='\'')
{
b=t=(*s)++;
while(**s && (**s!=q || (*(*s-1)=='\\' && t--)))
*t++=*((*s)++);
if(!**s)
fprintf(stderr,"%s: Unterminated %c quote found\n",MyName,q);
else (*s)++;
}
else
{
b=t=*s;
while(**s && !isspace((unsigned char)**s) && **s!=')' && **s!=',')
(*s)++;
t=*s;
}
if(t<=b)
return NULL;
r=mymalloc(t-b+1);
strncpy(r,b,t-b);
r[t-b]=0;
return r;
}
/**
*** MatchSeveralLines()
***
*** Returns which, if any, of the strings in o[] matches start of s, t is moved
*** after the end of the match.
**/
int MatchSeveralLines(char *s,char **o,char **t)
{
int i=0,j;
if(s && o && o[0])
while(o[i])
{
j=0;while(o[i][j] && tolower(s[j])==tolower(o[i][j]))j++;
if(!o[i][j])
{
*t=&s[j];
return i;
}
i++;
}
*t=s;
return -1;
}
/**
*** ParseBack()
*** Parses the options possible to Back
**/
int ParseBack(char **ss)
{
char *opts[]={"icon",NULL};
char *t,*s=*ss;
int r=0;
while(*s && *s!=')')
{
trimleft(s);
if(*s==',')
s++;
else switch(MatchSeveralLines(s,opts,&s))
{
case 0: /* Icon */
r=1;
fprintf(stderr,"%s: Back(Icon) not supported yet\n",MyName);
break;
default:
t=seekright(&s);
fprintf(stderr,"%s: Illegal back option \"%s\"\n",MyName,t);
free(t);
}
}
if(*s) s++;
*ss=s;
return r;
}
/**
*** ParseTitle()
*** Parses the options possible to Title
**/
void ParseTitle(char **ss,byte *flags,byte *mask)
{
char *titleopts[]={"left","right","center","side",NULL};
char *t,*s=*ss;
while(*s && *s!=')')
{
trimleft(s);
if(*s==',')
s++;
else switch(MatchSeveralLines(s,titleopts,&s))
{
case 0: /* Left */
*flags&=~b_TitleHoriz;
*mask|=b_TitleHoriz;
break;
case 1: /* Right */
*flags&=~b_TitleHoriz;
*flags|=2;
*mask|=b_TitleHoriz;
break;
case 2: /* Center */
*flags&=~b_TitleHoriz;
*flags|=1;
*mask|=b_TitleHoriz;
break;
case 3: /* Side */
*flags|=b_Horizontal;
*mask|=b_Horizontal;
break;
default:
t=seekright(&s);
fprintf(stderr,"%s: Illegal title option \"%s\"\n",MyName,t);
free(t);
}
}
if(*s) s++;
*ss=s;
}
/**
*** ParseSwallow()
*** Parses the options possible to Swallow
**/
void ParseSwallow(char **ss,byte *flags,byte *mask)
{
char *swallowopts[]={"nohints","hints","nokill","kill","noclose","close",
"respawn","norespawn","useold",
"noold","usetitle","notitle",NULL};
char *t,*s=*ss;
while(*s && *s!=')')
{
trimleft(s);
if(*s==',')
s++;
else switch(MatchSeveralLines(s,swallowopts,&s))
{
case 0: /* NoHints */
*flags|=b_NoHints;
*mask|=b_NoHints;
break;
case 1: /* Hints */
*flags&=~b_NoHints;
*mask|=b_NoHints;
break;
case 2: /* NoKill */
*flags&=~b_Kill;
*mask|=b_Kill;
break;
case 3: /* Kill */
*flags|=b_Kill;
*mask|=b_Kill;
break;
case 4: /* NoClose */
*flags|=b_NoClose;
*mask|=b_NoClose;
break;
case 5: /* Close */
*flags&=~b_NoClose;
*mask|=b_NoClose;
break;
case 6: /* Respawn */
*flags|=b_Respawn;
*mask|=b_Respawn;
break;
case 7: /* NoRespawn */
*flags&=~b_Respawn;
*mask|=b_Respawn;
break;
case 8: /* UseOld */
*flags|=b_UseOld;
*mask|=b_UseOld;
break;
case 9: /* NoOld */
*flags&=~b_UseOld;
*mask|=b_UseOld;
break;
case 10: /* UseTitle */
*flags|=b_UseTitle;
*mask|=b_UseTitle;
break;
case 11: /* NoTitle */
*flags&=~b_UseTitle;
*mask|=b_UseTitle;
break;
default:
t=seekright(&s);
fprintf(stderr,"%s: Illegal swallow option \"%s\"\n",MyName,t);
free(t);
}
}
if(*s) s++;
*ss=s;
}
/**
*** ParseContainer()
*** Parses the options possible to Container
**/
void ParseContainer(char **ss,button_info *b)
{
char *conts[]={"columns","rows","font","frame","back","fore",
"padding","title","swallow","nosize","size",NULL};
char *t,*o,*s=*ss;
int i,j;
while(*s && *s!=')')
{
trimleft(s);
if(*s==',')
s++;
else switch(MatchSeveralLines(s,conts,&s))
{
case 0: /* Columns */
b->c->num_columns=max(1,strtol(s,&t,10));
s=t;
break;
case 1: /* Rows */
b->c->num_rows=max(1,strtol(s,&t,10));
s=t;
break;
case 2: /* Font */
b->c->font_string=seekright(&s);
b->c->flags|=b_Font;
break;
case 3: /* Frame */
b->c->framew=strtol(s,&t,10);
b->c->flags|=b_Frame;
s=t;
break;
case 4: /* Back */
trimleft(s);
if(*s=='(' && s++)
if(ParseBack(&s))
b->c->flags|=b_IconBack;
b->c->back=seekright(&s);
if(b->c->back)
b->c->flags|=b_Back;
else
b->c->flags&=~b_IconBack;
break;
case 5: /* Fore */
b->c->fore=seekright(&s);
b->c->flags|=b_Fore;
break;
case 6: /* Padding */
i=strtol(s,&t,10);
if(t>s)
{
b->c->xpad=b->c->ypad=i;
s=t;
i=strtol(s,&t,10);
if(t>s)
{
b->c->ypad=i;
s=t;
}
b->c->flags|=b_Padding;
}
else
fprintf(stderr,"%s: Illegal padding argument\n",MyName);
break;
case 7: /* Title - flags */
trimleft(s);
if(*s=='(' && s++)
{
b->c->justify=0;
b->c->justify_mask=0;
ParseTitle(&s,&b->c->justify,&b->c->justify_mask);
if(b->c->justify_mask)
b->c->flags|=b_Justify;
}
else
{
fprintf(stderr,"%s: Illegal title in container options\n",
MyName);
free(seekright(&s));
}
break;
case 8: /* Swallow - flags */
trimleft(s);
if(*s=='(' && s++)
{
b->c->swallow=0;
b->c->swallow_mask=0;
ParseSwallow(&s,&b->c->swallow,&b->c->swallow_mask);
if(b->c->swallow_mask)
b->c->flags|=b_Swallow;
}
else
{
fprintf(stderr,"%s: Illegal swallow in container options\n",
MyName);
free(seekright(&s));
}
break;
case 9: /* NoSize */
b->c->flags|=b_Size;
b->c->minx=b->c->miny=0;
break;
case 10: /* Size */
i=strtol(s,&t,10);
j=strtol(t,&o,10);
if(t>s && o>t)
{
b->c->minx=i;
b->c->miny=j;
b->c->flags|=b_Size;
s=o;
}
else
fprintf(stderr,"%s: Illegal size arguments\n",MyName);
break;
default:
t=seekright(&s);
fprintf(stderr,"%s: Illegal container option \"%s\"\n",MyName,t);
free(t);
}
}
if(*s) s++;
*ss=s;
}
/**
*** match_string()
***
*** parse a buttonconfig string.
*** *FvwmButtons(option[ options]) title iconname command
**/
/*#define DEBUG_PARSER*/
void match_string(button_info **uberb,char *s)
{
button_info *b,*ub=*uberb;
int i,j;
char *t,*o;
b=alloc_button(ub,(ub->c->num_buttons)++);
trimleft(s);
if(*s=='(' && s++)
{
char *opts[]={"back","fore","font","title","icon","frame","padding",
"swallow","action","container","end","nosize","size",
NULL};
trimleft(s);
while(*s && *s!=')')
{
if(*s>='0' && *s<='9')
{
sscanf(s,"%dx%d",&i,&j);
if(i>0) b->BWidth=i;
if(j>0) b->BHeight=j;
while(*s!=' ' && *s!='\t' && *s!=')' && *s!=',') s++;
trimleft(s);
continue;
}
if(*s==',' && s++)
trimleft(s);
switch(MatchSeveralLines(s,opts,&s))
{
case 0: /* Back */
trimleft(s);
if(*s=='(' && s++)
if(ParseBack(&s))
b->flags|=b_IconBack;
if(b->flags&b_Back) free(b->back);
b->back=seekright(&s);
if(b->back)
b->flags|=b_Back;
else
b->flags&=~b_IconBack;
break;
case 1: /* Fore */
if(b->flags&b_Fore) free(b->fore);
b->fore=seekright(&s);
b->flags|=b_Fore;
break;
case 2: /* Font */
if(b->flags&b_Font) free(b->font_string);
b->font_string=seekright(&s);
b->flags|=b_Font;
break;
/* --------------------------- Title ------------------------- */
case 3: /* Title */
trimleft(s);
if(*s=='(' && s++)
{
b->justify=0;
b->justify_mask=0;
ParseTitle(&s,&b->justify,&b->justify_mask);
if(b->justify_mask)
b->flags|=b_Justify;
}
t=seekright(&s);
if(t && (t[0]!='-' || t[1]!=0))
{
b->title=t;
#ifdef DEBUG_PARSER
fprintf(stderr,"PARSE: Title \"%s\"\n",b->title);
#endif
b->flags|=b_Title;
}
else
{
fprintf(stderr,"%s: Missing title argument\n",MyName);
if(t)free(t);
}
break;
/* ---------------------------- icon ------------------------- */
case 4: /* Icon */
t=seekright(&s);
if(t && (t[0]!='-' && t[1]!=0))
{
b->icon_file=t;
b->IconWin=None;
b->flags|=b_Icon;
}
else
{
fprintf(stderr,"%s: Missing icon argument\n",MyName);
if(t)free(t);
}
break;
/* --------------------------- frame ------------------------- */
case 5: /* Frame */
i=strtol(s,&t,10);
if(t>s)
{
b->flags|=b_Frame;
b->framew=i;
s=t;
}
else
fprintf(stderr,"%s: Illegal frame argument\n",MyName);
break;
/* -------------------------- padding ------------------------ */
case 6: /* Padding */
i=strtol(s,&t,10);
if(t>s)
{
b->xpad=b->ypad=i;
b->flags |= b_Padding;
s=t;
i=strtol(s,&t,10);
if(t>s)
{
b->ypad=i;
s=t;
}
}
else
fprintf(stderr,"%s: Illegal padding argument\n",MyName);
break;
/* -------------------------- swallow ------------------------ */
case 7: /* Swallow */
trimleft(s);
b->swallow=0;
b->swallow_mask=0;
if(*s=='(' && s++)
ParseSwallow(&s,&b->swallow,&b->swallow_mask);
t=seekright(&s);
o=seekright(&s);
if(t)
{
b->hangon=t;
b->flags|=b_Hangon;
b->flags|=b_Swallow;
b->swallow|=1;
if(!(b->swallow&b_NoHints))
b->hints=(XSizeHints*)mymalloc(sizeof(XSizeHints));
if(o)
{
if(!(buttonSwallow(b)&b_UseOld))
SendText(fd,o,0);
b->spawn=o; /* Might be needed if respawning sometime */
}
}
else
{
fprintf(stderr,"%s: Missing swallow argument\n",MyName);
if(t)free(t);
if(o)free(o);
}
break;
/* --------------------------- action ------------------------ */
case 8: /* Action */
trimleft(s);
i=0;
if(*s=='(')
{
s++;
if(strncasecmp(s,"mouse",5)!=0)
{
fprintf(stderr,"%s: Couldn't parse action\n",MyName);
}
s+=5;
i=strtol(s,&t,10);
s=t;
while(*s && *s!=')')
s++;
if(*s==')')s++;
}
t=seekright(&s);
if(t)
AddButtonAction(b,i,strdup(t));
else
fprintf(stderr,"%s: Missing action argument\n",MyName);
break;
/* -------------------------- container ---------------------- */
case 9: /* Container */
b->flags&=b_Frame|b_Back|b_Fore|b_Padding|b_Action;
MakeContainer(b);
*uberb=b;
trimleft(s);
if(*s=='(' && s++)
ParseContainer(&s,b);
break;
case 10: /* End */
*uberb=ub->parent;
ub->c->buttons[--(ub->c->num_buttons)]=NULL;
if(!ub->parent)
{
fprintf(stderr,"%s: Unmatched END in config file\n",MyName);
exit(1);
}
break;
case 11: /* NoSize */
b->flags|=b_Size;
b->minx=b->miny=0;
break;
case 12: /* Size */
i=strtol(s,&t,10);
j=strtol(t,&o,10);
if(t>s && o>t)
{
b->minx=i;
b->miny=j;
b->flags|=b_Size;
s=o;
}
else
fprintf(stderr,"%s: Illegal size arguments\n",MyName);
break;
default:
t=seekright(&s);
fprintf(stderr,"%s: Illegal button option \"%s\"\n",MyName,t);
free(t);
break;
}
trimleft(s);
}
s++;
trimleft(s);
}
/* get title and iconname */
if(!(b->flags&b_Title))
{
b->title=seekright(&s);
if(b->title && ((b->title)[0]!='-'||(b->title)[1]!=0))
b->flags |= b_Title;
else
if(b->title)free(b->title);
}
else
free(seekright(&s));
if(!(b->flags&b_Icon))
{
b->icon_file=seekright(&s);
if(b->icon_file && ((b->icon_file)[0]!='-'||(b->icon_file)[1]!=0))
{
b->flags|=b_Icon;
b->IconWin=None;
}
else
if(b->icon_file)free(b->icon_file);
}
else
free(seekright(&s));
trimleft(s);
/* Swallow hangon command */
if(strncasecmp(s,"swallow",7)==0)
{
if(b->flags&b_Swallow)
{
fprintf(stderr,"%s: Illegal with both old and new swallow!\n",
MyName);
exit(1);
}
s+=7;
b->hangon=seekright(&s);
b->flags|=(b_Swallow|b_Hangon);
b->swallow|=1;
trimleft(s);
if(!(b->swallow&b_NoHints))
b->hints=(XSizeHints*)mymalloc(sizeof(XSizeHints));
if(*s)
{
if(!(buttonSwallow(b)&b_UseOld))
SendText(fd,s,0);
b->spawn=strdup(s);
}
}
else if(*s)
AddButtonAction(b,0,strdup(s));
return;
}
/**
*** ParseConfigLine
**/
void ParseConfigLine(button_info **ubb,char *s)
{
button_info *ub=*ubb;
char *opts[]={"geometry","font","padding","columns","rows","back","fore",
"frame","file",NULL};
int i,j,k;
switch(MatchSeveralLines(s,opts,&s))
{
case 0:/* Geometry */
{
char geom[64];
int flags,g_x,g_y,width,height;
i=sscanf(s,"%63s",geom);
if(i==1)
{
flags=XParseGeometry(geom,&g_x,&g_y,&width,&height);
if(flags&WidthValue) w=width;
if(flags&HeightValue) h=height;
if(flags&XValue) x=g_x;
if(flags&YValue) y=g_y;
if(flags&XNegative) xneg=1;
if(flags&YNegative) yneg=1;
}
break;
}
case 1:/* Font */
CopyString(&ub->c->font_string,s);
break;
case 2:/* Padding */
i=sscanf(s,"%d %d",&j,&k);
if(i>0) ub->c->xpad=ub->c->ypad=j;
if(i>1) ub->c->ypad=k;
break;
case 3:/* Columns */
i=sscanf(s,"%d",&j);
if(i>0) ub->c->num_columns=j;
break;
case 4:/* Rows */
i=sscanf(s,"%d",&j);
if(i>0) ub->c->num_rows=j;
break;
case 5:/* Back */
CopyString(&(ub->c->back),s);
break;
case 6:/* Fore */
CopyString(&(ub->c->fore),s);
break;
case 7:/* Frame */
i=sscanf(s,"%d",&j);
if(i>0) ub->c->framew=j;
break;
case 8:/* File */
trimleft(s);
config_file=seekright(&s);
break;
default:
trimleft(s);
match_string(ubb,s);
break;
}
}
/**
*** ParseConfigFile()
*** Parses optional separate configuration file for FvwmButtons
**/
void ParseConfigFile(button_info *ub)
{
char s[1024],*t;
FILE *f=fopen(config_file,"r");
int l;
if(!f)
{
fprintf(stderr,"%s: Couldn't open config file %s\n",MyName,config_file);
return;
}
while(fgets(s,1023,f))
{
/* Got to do some preprocessing here... Line continuation: */
while((l=strlen(s))<sizeof(s) && s[l-1]=='\n' && s[l-2]=='\\')
fgets(s+l-2,sizeof(s)-l,f);
/* And comments: */
t=s;
while(*t)
{
if(*t=='#' && (t==s || *(t-1)!='\\'))
{
*t=0;
break;
}
t++;
}
t=s;trimleft(t);
if(*t)ParseConfigLine(&ub,t);
}
fclose(f);
}
/**
*** ParseOptions()
**/
void ParseOptions(button_info *ub)
{
char *s;
char *items[]={"iconpath","pixmappath",NULL,NULL};
items[2]=mymalloc(strlen(MyName)+2);
sprintf(items[2],"*%s",MyName);
GetConfigLine(fd,&s);
while(s && s[0])
{
switch(MatchSeveralLines(s,items,&s))
{
case -1:
break;
case 0:
CopyString(&iconPath,s);
break;
case 1:
CopyString(&pixmapPath,s);
break;
case 2:
if(s && s[0] && !config_file)
ParseConfigLine(&ub,s);
}
GetConfigLine(fd,&s);
}
if(config_file)
ParseConfigFile(ub);
return;
}
|