Fix segfault setting MenuFace pixmap style for menus.
[fvwm.git] / modules / FvwmScript / Instructions.c
blob20e48d2cf8660740547daa7ee541c454e8a997e5
1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include "config.h"
19 #include "types.h"
20 #include "libs/fvwmsignal.h"
21 #include "libs/ftime.h"
22 #include "libs/FGettext.h"
23 #include "libs/Bindings.h"
24 #include "libs/charmap.h"
25 #include "libs/wcontext.h"
26 #include "libs/modifiers.h"
27 #include "libs/Module.h"
28 #include "libs/ColorUtils.h"
29 #include "libs/Strings.h"
30 #ifdef HAVE_GETPWUID
31 # include <pwd.h>
32 #endif
34 extern int fd[2];
35 extern Window ref;
37 void (*TabCom[29]) (int NbArg,long *TabArg);
38 char *(*TabFunc[27]) (int *NbArg, long *TabArg);
39 int (*TabComp[7]) (char *arg1,char *arg2);
41 extern Display *dpy;
42 extern int screen;
43 extern X11base *x11base;
44 extern int grab_serve;
45 extern struct XObj *tabxobj[1000];
46 extern Binding *BindingsList;
47 extern void LoadIcon(struct XObj *xobj);
49 extern int nbobj;
50 extern char **TabVVar;
51 extern int TabIdObj[1001];
52 extern char *ScriptName;
53 extern ModuleArgs *module;
54 extern TypeBuffSend BuffSend;
55 extern Atom propriete;
56 extern char *LastString;
57 char *FvwmUserDir = NULL;
58 char *BufCom;
59 char Command[255]="None";
60 time_t TimeCom=0;
63 * Utilities
65 void setFvwmUserDir(void)
67 char *home_dir;
69 if (FvwmUserDir != NULL)
70 return;
72 /* Figure out user's home directory */
73 home_dir = getenv("HOME");
74 #ifdef HAVE_GETPWUID
75 if (home_dir == NULL) {
76 struct passwd* pw = getpwuid(getuid());
77 if (pw != NULL)
78 home_dir = safestrdup(pw->pw_dir);
80 #endif
81 if (home_dir == NULL)
82 home_dir = "/"; /* give up and use root dir */
84 /* Figure out where to read and write user's data files. */
85 FvwmUserDir = getenv("FVWM_USERDIR");
86 if (FvwmUserDir == NULL)
88 FvwmUserDir = safestrdup(CatString2(home_dir, "/.fvwm"));
93 * Ensemble de fonction de comparaison de deux entiers
95 static int Inf(char *arg1,char *arg2)
97 int an1,an2;
98 an1=atoi(arg1);
99 an2=atoi(arg2);
100 return (an1<an2);
103 static int InfEq(char *arg1,char *arg2)
105 int an1,an2;
106 an1=atoi(arg1);
107 an2=atoi(arg2);
108 return (an1<=an2);
111 static int Equal(char *arg1,char *arg2)
113 int an1,an2;
114 an1=atoi(arg1);
115 an2=atoi(arg2);
116 return (strcmp(arg1,arg2)==0);
119 static int SupEq(char *arg1,char *arg2)
121 int an1,an2;
122 an1=atoi(arg1);
123 an2=atoi(arg2);
124 return (an1>=an2);
127 static int Sup(char *arg1,char *arg2)
129 int an1,an2;
130 an1=atoi(arg1);
131 an2=atoi(arg2);
132 return (an1>an2);
135 static int Diff(char *arg1,char *arg2)
137 int an1,an2;
138 an1=atoi(arg1);
139 an2=atoi(arg2);
140 return (strcmp(arg1,arg2)!=0);
144 * Fonction qui retourne la valeur d'un argument
146 static char *CalcArg (long *TabArg,int *Ix)
148 char *TmpStr;
149 int i;
151 if (TabArg[*Ix]>100000) /* Cas du codage d'un nombre */
153 i = (int)TabArg[*Ix] - 200000;
154 TmpStr = (char*)safecalloc(1,sizeof(char)*10);
155 sprintf(TmpStr,"%d",i);
157 else if (TabArg[*Ix] < -200000)/* Cas d'un id de fonction de comparaison */
159 i = TabArg[*Ix]+250000;
160 TmpStr = (char*)safecalloc(1,sizeof(char)*10);
161 sprintf(TmpStr,"%d",i);
163 else if (TabArg[*Ix] < -100000) /* Cas d'un id de fonction */
165 TmpStr = TabFunc[TabArg[*Ix]+150000](Ix,TabArg);
167 else /* Cas d'une variable */
169 TmpStr=safestrdup(TabVVar[TabArg[*Ix]]);
171 return (TmpStr);
175 * Ensemble des fonctions pour recuperer les prop d'un objet
178 /* GetValue */
179 static char *FuncGetValue(int *NbArg, long *TabArg)
181 char *tmp;
182 long Id;
184 (*NbArg)++; /* La fonction GetValue n'a qu'un seul argument */
185 tmp = CalcArg(TabArg,NbArg);
186 Id = atoi(tmp);
187 free(tmp);
188 tmp = (char*)safecalloc(1,sizeof(char)*10);
189 sprintf(tmp,"%d",tabxobj[TabIdObj[Id]]->value);
190 return tmp;
193 /* GetMinValue */
194 static char *FuncGetMinValue(int *NbArg, long *TabArg)
196 char *tmp;
197 long Id;
199 (*NbArg)++; /* La fonction GetValue n'a qu'un seul argument */
200 tmp = CalcArg(TabArg,NbArg);
201 Id = atoi(tmp);
202 free(tmp);
203 tmp = (char*)safecalloc(1,sizeof(char)*10);
204 sprintf(tmp,"%d",tabxobj[TabIdObj[Id]]->value2);
205 return tmp;
208 /* GetMaxValue */
209 static char *FuncGetMaxValue(int *NbArg, long *TabArg)
211 char *tmp;
212 long Id;
214 (*NbArg)++; /* La fonction GetValue n'a qu'un seul argument */
215 tmp = CalcArg(TabArg,NbArg);
216 Id = atoi(tmp);
217 free(tmp);
218 tmp = (char*)safecalloc(1,sizeof(char)*10);
219 sprintf(tmp,"%d",tabxobj[TabIdObj[Id]]->value3);
220 return tmp;
223 /* GetFore */
224 static char *FuncGetFore(int *NbArg, long *TabArg)
226 char *tmp;
227 long Id;
228 XColor color;
230 (*NbArg)++; /* La fonction GetValue n'a qu'un seul argument */
231 tmp = CalcArg(TabArg,NbArg);
232 Id = atoi(tmp);
233 free(tmp);
234 tmp = (char*)safecalloc(1,sizeof(char)*7);
235 color.pixel = tabxobj[TabIdObj[Id]]->TabColor[fore];
236 XQueryColor(dpy, Pcmap, &color);
237 sprintf(tmp, "%02x%02x%02x",
238 color.red >> 8, color.green >> 8, color.blue >> 8);
239 return tmp;
242 /* GetBack */
243 static char *FuncGetBack(int *NbArg, long *TabArg)
245 char *tmp;
246 long Id;
247 XColor color;
249 (*NbArg)++; /* La fonction GetValue n'a qu'un seul argument */
250 tmp = CalcArg(TabArg,NbArg);
251 Id = atoi(tmp);
252 free(tmp);
253 tmp = (char*)safecalloc(1,sizeof(char)*7);
254 color.pixel = tabxobj[TabIdObj[Id]]->TabColor[back];
255 XQueryColor(dpy, Pcmap, &color);
256 sprintf(tmp, "%02x%02x%02x",
257 color.red >> 8, color.green >> 8, color.blue >> 8);
258 return tmp;
261 /* GetHili */
262 static char *FuncGetHili(int *NbArg, long *TabArg)
264 char *tmp;
265 long Id;
266 XColor color;
268 (*NbArg)++; /* La fonction GetValue n'a qu'un seul argument */
269 tmp = CalcArg(TabArg,NbArg);
270 Id = atoi(tmp);
271 free(tmp);
272 tmp = (char*)safecalloc(1,sizeof(char)*7);
273 color.pixel = tabxobj[TabIdObj[Id]]->TabColor[hili];
274 XQueryColor(dpy, Pcmap, &color);
275 sprintf(tmp, "%02x%02x%02x",
276 color.red >> 8, color.green >> 8, color.blue >> 8);
277 return tmp;
280 /* GetShad */
281 static char *FuncGetShad(int *NbArg, long *TabArg)
283 char *tmp;
284 long Id;
285 XColor color;
287 (*NbArg)++; /* La fonction GetValue n'a qu'un seul argument */
288 tmp = CalcArg(TabArg,NbArg);
289 Id = atoi(tmp);
290 free(tmp);
291 tmp = (char*)safecalloc(1,sizeof(char)*7);
292 color.pixel = tabxobj[TabIdObj[Id]]->TabColor[shad];
293 XQueryColor(dpy, Pcmap, &color);
294 sprintf(tmp, "%02x%02x%02x",
295 color.red >> 8, color.green >> 8, color.blue >> 8);
296 return tmp;
299 /* Fonction qui retourne le titre d'un objet */
300 static char *FuncGetTitle(int *NbArg, long *TabArg)
302 char *tmp;
303 long Id;
305 (*NbArg)++;
306 tmp = CalcArg(TabArg,NbArg);
307 Id = atoi(tmp);
308 free(tmp);
309 if (TabIdObj[Id] != -1)
310 tmp=safestrdup(tabxobj[TabIdObj[Id]]->title);
311 else
313 fprintf(stderr,"[%s][GetTitle]: <<WARNING>> Widget %d doesn't exist\n",
314 ScriptName, (int)Id);
315 tmp=(char*)safecalloc(1,sizeof(char));
316 tmp[0]='\0';
318 return tmp;
321 /* Fonction qui retourne la sortie d'une commande */
322 static char *FuncGetOutput(int *NbArg, long *TabArg)
324 char *cmndbuf;
325 char *str;
326 int line,index,i=2,j=0,k,NewWord;
327 FILE *f;
328 int maxsize=32000;
329 int size;
331 (*NbArg)++;
332 cmndbuf=CalcArg(TabArg,NbArg);
333 (*NbArg)++;
334 str=CalcArg(TabArg,NbArg);
335 line=atoi(str);
336 free(str);
337 (*NbArg)++;
338 str=CalcArg(TabArg,NbArg);
339 index=atoi(str);
340 free(str);
342 if ((strcmp(Command,cmndbuf)) || ((time(NULL)-TimeCom) > 1) || (TimeCom == 0))
344 if ((f = popen(cmndbuf,"r")) == NULL)
346 fprintf(stderr,"[%s][GetOutput]: can't run %s\n",ScriptName,cmndbuf);
347 str = (char*)safecalloc(sizeof(char),10);
348 free(cmndbuf);
349 return str;
351 else
353 if (strcmp(Command,"None"))
354 free(BufCom);
355 BufCom = (char*)safecalloc(sizeof(char),maxsize);
356 size = fread(BufCom,1,maxsize,f);
357 pclose(f);
358 strncpy(Command,cmndbuf,255);
359 TimeCom=time(NULL);
363 /* Recherche de la ligne */
364 while ((i <= line) && (BufCom[j] != '\0'))
366 j++;
367 if (BufCom[j] == '\n')
368 i++;
371 /* Recherche du mot */
372 if (index != -1)
374 if (i != 2) j++;
375 i = 1;
376 NewWord = 0;
377 while ((i < index) && (BufCom[j] != '\n') && (BufCom[j] != '\0'))
379 j++;
380 if (BufCom[j] == ' ')
382 if (NewWord)
384 i++;
385 NewWord = 0;
388 else
389 if (!NewWord) NewWord = 1;
391 str = (char*)safecalloc(sizeof(char),255);
392 sscanf(&BufCom[j],"%s",str);
394 else /* Lecture de la ligne complete */
396 if (i != 2) j++;
397 k=j;
398 while ((BufCom[k] != '\n') && (BufCom[k] != '\0'))
399 k++;
400 str = (char*)safecalloc(sizeof(char),k-j+1);
401 memmove(str,&BufCom[j],k-j);
402 str[k-j]='\0';
405 free(cmndbuf);
406 return str;
409 /* Convertion decimal vers hexadecimal */
410 static char *FuncNumToHex(int *NbArg, long *TabArg)
412 char *str;
413 int value,nbchar;
414 int i,j;
416 (*NbArg)++;
417 str = CalcArg(TabArg,NbArg);
418 value = atoi(str);
419 free(str);
420 (*NbArg)++;
421 str = CalcArg(TabArg,NbArg);
422 nbchar = atoi(str);
423 free(str);
425 str = (char*)safecalloc(1,nbchar+10);
426 sprintf(str,"%X",value);
427 j = strlen(str);
428 if (j < nbchar)
430 memmove(&str[nbchar-j],str,j);
431 for (i=0; i < (nbchar-j); i++)
432 str[i]='0';
435 return str;
438 /* Convertion hexadecimal vers decimal */
439 static char *FuncHexToNum(int *NbArg, long *TabArg)
441 char *str,*str2;
442 int k;
444 (*NbArg)++;
445 str = CalcArg(TabArg,NbArg);
446 if (str[0] == '#')
447 memmove(str,&str[1],strlen(str));
448 k = (int)strtol(str,NULL,16);
449 free(str);
451 str2 = (char*)safecalloc(1,20);
452 sprintf(str2,"%d",k);
453 return str2;
456 /* + */
457 static char *FuncAdd(int *NbArg, long *TabArg)
459 char *str;
460 int val1,val2;
462 (*NbArg)++;
463 str = CalcArg(TabArg,NbArg);
464 val1 = atoi(str);
465 free(str);
466 (*NbArg)++;
467 str = CalcArg(TabArg,NbArg);
468 val2 = atoi(str);
469 free(str);
470 str = (char*)safecalloc(1,20);
471 sprintf(str,"%d",val1+val2);
472 return str;
475 /* * */
476 static char *FuncMult(int *NbArg, long *TabArg)
478 char *str;
479 int val1,val2;
481 (*NbArg)++;
482 str = CalcArg(TabArg,NbArg);
483 val1 = atoi(str);
484 free(str);
485 (*NbArg)++;
486 str = CalcArg(TabArg,NbArg);
487 val2 = atoi(str);
488 free(str);
489 str = (char*)safecalloc(1,20);
490 sprintf(str,"%d",val1*val2);
491 return str;
494 /* / */
495 static char *FuncDiv(int *NbArg, long *TabArg)
497 char *str;
498 int val1,val2;
500 (*NbArg)++;
501 str = CalcArg(TabArg,NbArg);
502 val1 = atoi(str);
503 free(str);
504 (*NbArg)++;
505 str = CalcArg(TabArg,NbArg);
506 val2 = atoi(str);
507 free(str);
508 str = (char*)safecalloc(1,20);
509 sprintf(str,"%d",val1/val2);
510 return str;
513 /* % */
514 static char *RemainderOfDiv(int *NbArg, long *TabArg)
516 #ifndef HAVE_DIV
517 return safestrdup("Unsupported function: div");
518 #else
519 char *str;
520 int val1,val2;
521 div_t res;
523 (*NbArg)++;
524 str = CalcArg(TabArg,NbArg);
525 val1 = atoi(str);
526 free(str);
527 (*NbArg)++;
528 str = CalcArg(TabArg,NbArg);
529 val2 = atoi(str);
530 free(str);
531 str = (char*)safecalloc(1,20);
532 res = div(val1,val2);
533 sprintf(str,"%d",res.rem);
534 return str;
535 #endif
539 /* StrCopy */
540 static char *FuncStrCopy(int *NbArg, long *TabArg)
542 char *str,*strsrc;
543 int i1,i2;
545 (*NbArg)++;
546 strsrc = CalcArg(TabArg,NbArg);
547 (*NbArg)++;
548 str = CalcArg(TabArg,NbArg);
549 i1 = atoi(str);
550 if (i1 < 1) i1 = 1;
551 free(str);
552 (*NbArg)++;
553 str = CalcArg(TabArg,NbArg);
554 i2 = atoi(str);
555 if (i2 < 1) i2 = 1;
556 free(str);
558 if ((i1 <= i2) &&( i1 <= strlen(strsrc)))
560 if (i2 > strlen(strsrc))
561 i2=strlen(strsrc);
562 str = (char*)safecalloc(1,i2 - i1 + 2);
563 memmove(str,&strsrc[i1-1],i2 - i1 + 1);
565 else
567 str = (char*)safecalloc(1,1);
570 free(strsrc);
571 return str;
574 /* Lancement d'un script avec pipe */
575 static char *LaunchScript (int *NbArg,long *TabArg)
577 char *arg,*execstr,*str,*scriptarg,*scriptname;
578 unsigned long leng;
579 int i;
580 Atom MyAtom;
582 /* Lecture des arguments */
583 (*NbArg)++;
584 arg = CalcArg(TabArg,NbArg);
586 str = (char*)safecalloc(100,sizeof(char));
588 /* Calcul du nom du script fils */
589 x11base->TabScriptId[x11base->NbChild+2] =
590 (char*)safecalloc(strlen(x11base->TabScriptId[1])+4,sizeof(char));
592 if (x11base->NbChild<98)
594 i=16;
597 sprintf(x11base->TabScriptId[x11base->NbChild + 2],"%s%x",
598 x11base->TabScriptId[1],i);
599 MyAtom = XInternAtom(dpy,x11base->TabScriptId[x11base->NbChild + 2],False);
600 i++;
602 while (XGetSelectionOwner(dpy,MyAtom)!=None);
604 else
606 fprintf(stderr,"[%s][LaunchScript]: Too many launched script\n", ScriptName);
607 sprintf(str,"-1");
608 return str;
611 /* Construction de la commande */
612 execstr = (char*)safecalloc(strlen(module->name) + strlen(arg) +
613 strlen(x11base->TabScriptId[x11base->NbChild + 2]) + 5,sizeof(char));
614 scriptname = (char*)safecalloc(sizeof(char),100);
615 sscanf(arg,"%s",scriptname);
616 scriptarg = (char*)safecalloc(sizeof(char),strlen(arg));
617 scriptarg = (char*)strncpy(scriptarg, &arg[strlen(scriptname)],
618 strlen(arg) - strlen(scriptname));
619 sprintf(execstr,"%s %s %s %s",module->name,scriptname,
620 x11base->TabScriptId[x11base->NbChild + 2],scriptarg);
621 free(scriptname);
622 free(scriptarg);
623 free(arg);
625 /* Envoi de la commande */
626 write(fd[0], &ref, sizeof(Window));
627 leng = strlen(execstr);
628 write(fd[0], &leng, sizeof(unsigned long));
629 write(fd[0], execstr, leng);
630 leng = 1;
631 write(fd[0], &leng, sizeof(unsigned long));
632 free(execstr);
634 /* Retourne l'id du fils */
635 sprintf(str,"%d",x11base->NbChild+2);
636 x11base->NbChild++;
637 return str;
640 /* GetScriptFather */
641 static char *GetScriptFather (int *NbArg,long *TabArg)
643 char *str;
645 str = (char*)safecalloc(10,sizeof(char));
646 sprintf(str,"0");
647 return str;
650 /* GetTime */
651 static char *GetTime (int *NbArg,long *TabArg)
653 char *str;
654 time_t t;
656 str = (char*)safecalloc(20,sizeof(char));
657 t = time(NULL);
658 sprintf(str,"%ld",(long)t-x11base->BeginTime);
659 return str;
662 /* GetScriptArg */
663 static char *GetScriptArg (int *NbArg,long *TabArg)
665 char *str;
666 int val1;
668 (*NbArg)++;
669 str = CalcArg(TabArg,NbArg);
670 val1 = atoi(str);
671 free(str);
673 if (x11base->TabArg[val1] != NULL)
675 str = (char*)safecalloc(strlen(x11base->TabArg[val1])+1,sizeof(char));
676 str = strcpy(str,x11base->TabArg[val1]);
678 else
680 str = (char*)safecalloc(1,sizeof(char));
681 str = strcpy(str,"");
684 return str;
687 /* ReceivFromScript */
688 static char *ReceivFromScript (int *NbArg,long *TabArg)
690 char *arg,*msg;
691 int send;
692 Atom AReceiv,ASend,type;
693 static XEvent event;
694 unsigned long longueur,octets_restant;
695 unsigned char *donnees = (unsigned char *)"";
696 int format;
697 int NbEssai = 0;
699 (*NbArg)++;
700 arg = CalcArg(TabArg,NbArg);
701 send = (int)atoi(arg);
702 free(arg);
704 msg = (char*)safecalloc(256,sizeof(char));
705 sprintf(msg,"No message");
707 /* Recuperation des atomes */
708 AReceiv = XInternAtom(dpy,x11base->TabScriptId[1],True);
709 if (AReceiv == None)
711 fprintf(stderr,"[%s][ReceivFromScript]: <<WARNING>> Error with atome\n",
712 ScriptName);
713 return msg;
716 if ( (send >=0 ) && (send < 99))
718 if (x11base->TabScriptId[send]!=NULL)
720 ASend=XInternAtom(dpy,x11base->TabScriptId[send],True);
721 if (ASend==None)
722 fprintf(stderr,
723 "[%s][ReceivFromScript]: <<WARNING>> Error with atome\n",
724 ScriptName);
726 else
727 return msg;
729 else
730 return msg;
732 /* Recuperation du message */
733 XConvertSelection(dpy,ASend,AReceiv,propriete,x11base->win,CurrentTime);
734 while ((!FCheckTypedEvent(dpy,SelectionNotify,&event)) && (NbEssai < 10))
736 usleep(1);
737 NbEssai++;
739 if (event.xselection.property!=None)
740 if (event.xselection.selection==ASend)
742 XGetWindowProperty(dpy,event.xselection.requestor,
743 event.xselection.property,
744 0L,8192L,False,event.xselection.target,&type,&format,
745 &longueur,&octets_restant,&donnees);
746 if (longueur > 0)
748 msg = (char*)saferealloc((void*)msg, (longueur+1) * sizeof(char));
749 msg = strcpy(msg,(char *)donnees);
750 XDeleteProperty(dpy,event.xselection.requestor,
751 event.xselection.property);
752 XFree(donnees);
755 return msg;
758 /* GetPid */
759 static char *FuncGetPid(int *NbArg,long *TabArg)
761 char *str;
762 pid_t pid;
764 pid = getpid();
765 str = (char*)safecalloc(1,20);
766 sprintf(str,"%d",pid);
767 return str;
770 /* SendMsgAndGet */
771 #define IN_FIFO_NBR_OF_TRY 200
772 #define IN_FIFO_TIMEOUT 100000 /* usec: 0.1 sec (20 sec) */
773 #define OUT_FIFO_NBR_OF_TRY 400
774 #define OUT_FIFO_TIMEOUT 50000 /* usec: 0.05 sec (20 sec) */
775 static char *FuncSendMsgAndGet(int *NbArg,long *TabArg)
777 char *com_name, *cmd, *tmp, *out_fifo, *in_fifo, *str;
778 char *buf = NULL;
779 int maxsize=32000;
780 int err = 0;
781 int filedes = 0;
782 int i,l,j,lock;
783 FILE *f;
784 struct timeval timeout;
786 /* comunication name */
787 (*NbArg)++;
788 com_name=CalcArg(TabArg,NbArg);
789 /* the command send to the receiver */
790 (*NbArg)++;
791 cmd=CalcArg(TabArg,NbArg);
792 /* 0: no answer (so no locking) from the receiver *
793 * 1: real send and get mode */
794 (*NbArg)++;
795 tmp=CalcArg(TabArg,NbArg);
796 lock=atoi(tmp);
797 free(tmp);
799 setFvwmUserDir();
800 in_fifo =
801 (char*)safecalloc(strlen(com_name)+strlen(FvwmUserDir)+14, sizeof(char));
802 sprintf(in_fifo,"%s/.tmp-com-in-%s",FvwmUserDir,com_name);
804 /* unlock the receiver, wait IN_FIFO_TIMEOUT * IN_FIFO_NBR_OF_TRY so that *
805 * the receiver has the time to go in its communication loop at startup *
806 * or if it do an other job than waiting for a cmd */
807 i = 0;
808 while(1)
810 if (access(in_fifo,W_OK) == 0)
812 if ((filedes = open(in_fifo,O_WRONLY)) == 0)
814 fprintf(stderr,
815 "[%s][GetMsgAndGet]: <<WARNING>> cannot open the in fifo %s\n",
816 ScriptName,in_fifo);
817 err = 1;
818 break;
820 i = 0;
821 l = strlen(cmd);
822 while(l > i) {
823 if ((j = write(filedes, cmd+i, l-i)) == -1) {
824 fprintf(stderr,
825 "[%s][GetMsgAndGet]: <<WARNING>> write error on %s\n",
826 ScriptName,in_fifo);
827 err = 1;
829 i += j;
831 close(filedes);
832 break;
834 else
836 timeout.tv_sec = 0;
837 timeout.tv_usec = IN_FIFO_TIMEOUT;
838 select(None,NULL,NULL,NULL,&timeout);
839 i++;
840 if (i > IN_FIFO_NBR_OF_TRY)
842 fprintf(stderr,
843 "[%s][GetMsgAndGet]: <<WARNING>> No in fifo %s for comunication %s\n",
844 ScriptName,in_fifo,com_name);
845 close(filedes);
846 err = 1;
847 break;
851 free(in_fifo);
853 /* no msg to read from the receiver (or an error occur): return */
854 if (!lock || err)
856 free(cmd);
857 free(com_name);
858 str=(char*)safecalloc(2,sizeof(char));
859 sprintf(str,(err) ? "0" : "1");
860 return str;
863 /* get the answer from the receiver. *
864 * we wait OUT_FIFO_TIMEOUT * OUT_FIFO_NBR_OF_TRY for this answer */
865 out_fifo =
866 (char*)safecalloc(strlen(com_name)+strlen(FvwmUserDir)+15,sizeof(char));
867 sprintf(out_fifo,"%s/.tmp-com-out-%s",FvwmUserDir,com_name);
868 i = 0;
869 while(1)
871 if (access(out_fifo,R_OK) == 0)
873 if ((f = fopen (out_fifo, "r")) != NULL)
875 buf=(char*)safecalloc(sizeof(char),maxsize);
876 fread(buf,1,maxsize,f);
877 fclose (f);
879 else
881 fprintf(stderr,
882 "[%s][GetMsgAndGet]: <<WARNING>> cannot open the out fifo %s\n",
883 ScriptName,out_fifo);
884 err = 1;
886 break;
888 else
890 timeout.tv_sec = 0;
891 timeout.tv_usec = OUT_FIFO_TIMEOUT;
892 select(None,NULL,NULL,NULL,&timeout);
893 i++;
894 if (i > OUT_FIFO_NBR_OF_TRY)
896 fprintf(stderr,
897 "[%s][GetMsgAndGet]: <<WARNING>>: No out fifo %s for comunication %s\n",
898 ScriptName,out_fifo,com_name);
899 err = 1;
900 break;
905 free(cmd);
906 free(com_name);
907 free(out_fifo);
909 if (err)
911 if (buf)
912 free(buf);
913 str=(char*)safecalloc(2,sizeof(char));
914 sprintf(str,"0");
915 return str;
917 l = strlen(buf);
918 str=(char*)safecalloc(l+1,sizeof(char));
919 memmove(str,&buf[0],l);
920 free(buf);
921 str[l]='\0';
922 return str;
925 /* Parse */
926 static char *FuncParse(int *NbArg,long *TabArg)
928 char *string,*str;
929 char num[4];
930 int index,toklen,l;
931 int i = 0;
932 int step = 1;
933 int start = 0;
934 int end = 0;
936 /* the string to parse */
937 (*NbArg)++;
938 string=CalcArg(TabArg,NbArg);
939 /* the substring to return */
940 (*NbArg)++;
941 str=CalcArg(TabArg,NbArg);
942 index=atoi(str);
943 free(str);
945 l = strlen(string);
946 while(i < l+4) {
947 memmove(num,&string[i],4);
948 toklen = atoi(num);
949 if (step == index)
951 start = i+4;
952 end = start+toklen;
953 break;
955 step++;
956 i += toklen + 4;
959 if (end > l || end <= start)
961 str=(char*)safecalloc(1,sizeof(char));
962 str[0] ='\0';
964 else
966 str=(char*)safecalloc(end-start+1,sizeof(char));
967 memmove(str,&string[start],end-start);
968 str[end-start] ='\0';
970 free(string);
971 return str;
974 /* GetLastString */
975 static char *FuncGetLastString(int *NbArg,long *TabArg)
977 char *str;
979 if (LastString == NULL) {
980 str = (char*)safecalloc(1,sizeof(char));
981 str[0] ='\0';
983 else
985 CopyString(&str,LastString);
987 return str;
991 * Ensemble des commandes possible pour un obj
994 /* Exec */
995 static void Exec (int NbArg,long *TabArg)
997 unsigned long leng = 0;
998 char *execstr;
999 char *tempstr;
1000 int i;
1002 for (i=0; i < NbArg; i++)
1003 leng += strlen(CalcArg(TabArg,&i));
1005 if (leng >= 998)
1007 fprintf(stderr, "[%s][Do]: too long command %i chars max 998\n",
1008 ScriptName,(int)leng);
1009 return;
1012 execstr = (char*)safecalloc(1,leng+1);
1013 for (i=0; i < NbArg; i++)
1015 tempstr = CalcArg(TabArg,&i);
1016 execstr = strcat(execstr,tempstr);
1017 free(tempstr);
1020 write(fd[0], &ref, sizeof(Window));
1021 leng = strlen(execstr);
1022 write(fd[0], &leng, sizeof(unsigned long));
1023 write(fd[0], execstr, leng);
1024 leng = 1;
1025 write(fd[0], &leng, sizeof(unsigned long));
1026 free(execstr);
1029 /* Hide */
1030 static void HideObj (int NbArg,long *TabArg)
1032 char *arg[1];
1033 int IdItem;
1034 int i = 0;
1036 arg[0] = CalcArg(TabArg,&i);
1037 IdItem = TabIdObj[atoi(arg[0])];
1040 tabxobj[IdItem]->flags[0] = True;
1041 /* On cache la fentre pour la faire disparaitre */
1042 XUnmapWindow(dpy,tabxobj[IdItem]->win);
1043 free(arg[0]);
1046 /* Show */
1047 static void ShowObj (int NbArg,long *TabArg)
1049 char *arg[1];
1050 int IdItem;
1051 int i = 0;
1053 arg[0] = CalcArg(TabArg,&i);
1054 IdItem = TabIdObj[atoi(arg[0])];
1056 tabxobj[IdItem]->flags[0] = False;
1057 XMapWindow(dpy,tabxobj[IdItem]->win);
1058 tabxobj[IdItem]->DrawObj(tabxobj[IdItem], NULL);
1059 free(arg[0]);
1062 /* ChangeValue */
1063 static void ChangeValue (int NbArg,long *TabArg)
1065 int i = 0;
1066 char *arg[2];
1068 arg[0] =CalcArg(TabArg,&i);
1069 i++;
1070 arg[1] =CalcArg(TabArg,&i);
1072 tabxobj[TabIdObj[atoi(arg[0])]]->value = atoi(arg[1]);
1073 /* On redessine l'objet pour le mettre a jour */
1074 if (tabxobj[TabIdObj[atoi(arg[0])]]->TypeWidget != SwallowExec)
1075 XClearWindow(dpy, tabxobj[TabIdObj[atoi(arg[0])]]->win);
1076 tabxobj[TabIdObj[atoi(arg[0])]]->DrawObj(tabxobj[TabIdObj[atoi(arg[0])]],NULL);
1077 free(arg[0]);
1078 free(arg[1]);
1081 /* ChangeValueMax */
1082 static void ChangeValueMax (int NbArg,long *TabArg)
1084 int i=0;
1085 char *arg[2];
1086 int j;
1088 arg[0] = CalcArg(TabArg,&i);
1089 j = atoi(arg[0]);
1090 i++;
1091 arg[1] = CalcArg(TabArg,&i);
1093 tabxobj[TabIdObj[j]]->value3 = atoi(arg[1]);
1094 /* On redessine l'objet pour le mettre a jour */
1095 if (tabxobj[TabIdObj[j]]->value > tabxobj[TabIdObj[j]]->value3)
1097 tabxobj[TabIdObj[j]]->value = atoi(arg[1]);
1098 if (tabxobj[TabIdObj[j]]->TypeWidget != SwallowExec)
1099 XClearWindow(dpy, tabxobj[TabIdObj[j]]->win);
1100 tabxobj[TabIdObj[j]]->DrawObj(tabxobj[TabIdObj[j]],NULL);
1102 free(arg[0]);
1103 free(arg[1]);
1106 /* ChangeValueMin */
1107 static void ChangeValueMin (int NbArg,long *TabArg)
1109 int i=0;
1110 char *arg[2];
1111 int j;
1113 arg[0] = CalcArg(TabArg,&i);
1114 i++;
1115 arg[1] = CalcArg(TabArg,&i);
1116 j = atoi(arg[0]);
1118 tabxobj[TabIdObj[j]]->value2 = atoi(arg[1]);
1119 /* On redessine l'objet pour le mettre a jour */
1120 if (tabxobj[TabIdObj[j]]->value < tabxobj[TabIdObj[j]]->value2)
1122 tabxobj[TabIdObj[j]]->value = atoi(arg[1]);
1123 if (tabxobj[TabIdObj[j]]->TypeWidget != SwallowExec)
1124 XClearWindow(dpy, tabxobj[TabIdObj[j]]->win);
1125 tabxobj[TabIdObj[j]]->DrawObj(tabxobj[TabIdObj[j]],NULL);
1127 free(arg[0]);
1128 free(arg[1]);
1131 /* ChangePosition */
1132 static void ChangePos (int NbArg,long *TabArg)
1134 int i=0;
1135 char *arg[3];
1136 int an[3];
1137 int IdItem;
1139 arg[0] = CalcArg(TabArg,&i);
1140 i++;
1141 arg[1] = CalcArg(TabArg,&i);
1142 i++;
1143 arg[2] = CalcArg(TabArg,&i);
1145 IdItem = TabIdObj[atoi(arg[0])];
1146 for (i=1; i<3; i++)
1147 an[i] = atoi(arg[i]);
1148 tabxobj[IdItem]->x = an[1];
1149 tabxobj[IdItem]->y = an[2];
1150 XMoveWindow(dpy,tabxobj[IdItem]->win,an[1],an[2]);
1152 free(arg[0]);
1153 free(arg[1]);
1154 free(arg[2]);
1157 /* ChangeFont */
1158 static void ChangeFont (int NbArg,long *TabArg)
1160 int i=0;
1161 char *arg[2];
1162 int IdItem;
1163 FlocaleFont *Ffont;
1165 arg[0] = CalcArg(TabArg,&i);
1166 i++;
1167 arg[1] = CalcArg(TabArg,&i);
1168 IdItem = TabIdObj[atoi(arg[0])];
1169 if (tabxobj[IdItem]->font)
1170 free(tabxobj[IdItem]->font);
1171 tabxobj[IdItem]->font = safestrdup(arg[1]);
1173 if ((Ffont =
1174 FlocaleLoadFont(dpy, tabxobj[IdItem]->font, ScriptName)) == NULL) {
1175 fprintf(stderr, "[%s][ChangeFont]: Couldn't load font %s\n",
1176 ScriptName,tabxobj[IdItem]->font);
1178 else
1180 FlocaleUnloadFont(dpy, tabxobj[IdItem]->Ffont);
1181 tabxobj[IdItem]->Ffont = Ffont;
1182 if (Ffont->font != NULL)
1183 XSetFont(dpy,tabxobj[IdItem]->gc,tabxobj[IdItem]->Ffont->font->fid);
1186 if (tabxobj[IdItem]->TypeWidget != SwallowExec)
1187 XClearWindow(dpy, tabxobj[IdItem]->win);
1188 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1189 free(arg[0]);
1190 free(arg[1]);
1193 /* ChangeSize */
1194 static void ChangeSize (int NbArg,long *TabArg)
1196 int i=0;
1197 char *arg[3];
1198 int an[3];
1199 int IdItem;
1201 arg[0] = CalcArg(TabArg,&i);
1202 i++;
1203 arg[1] = CalcArg(TabArg,&i);
1204 i++;
1205 arg[2] = CalcArg(TabArg,&i);
1207 IdItem = TabIdObj[atoi(arg[0])];
1208 for (i=1; i<3; i++) {
1209 an[i] = atoi(arg[i]);
1211 tabxobj[IdItem]->width = an[1];
1212 tabxobj[IdItem]->height = an[2];
1213 XResizeWindow(dpy,tabxobj[IdItem]->win,an[1],an[2]);
1214 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1215 free(arg[0]);
1216 free(arg[1]);
1217 free(arg[2]);
1220 /* ChangeTitle */
1221 static void ChangeTitle (int NbArg,long *TabArg)
1223 int i=0;
1224 char *arg[2];
1225 int IdItem;
1227 arg[0] = CalcArg(TabArg,&i);
1228 i++;
1229 arg[1] = CalcArg(TabArg,&i);
1230 IdItem = TabIdObj[atoi(arg[0])];
1232 if (tabxobj[IdItem]->title)
1233 free(tabxobj[IdItem]->title);
1234 tabxobj[IdItem]->title=safestrdup(arg[1]);
1235 if (tabxobj[IdItem]->TypeWidget != SwallowExec)
1236 XClearWindow(dpy, tabxobj[IdItem]->win);
1237 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1238 free(arg[0]);
1239 free(arg[1]);
1242 /* ChangeLocaleTitle */
1243 static void ChangeLocaleTitle (int NbArg,long *TabArg)
1245 int i=0;
1246 char *arg[2];
1247 int IdItem;
1249 arg[0] = CalcArg(TabArg,&i);
1250 i++;
1251 arg[1] = CalcArg(TabArg,&i);
1252 IdItem = TabIdObj[atoi(arg[0])];
1254 if (tabxobj[IdItem]->title)
1255 free(tabxobj[IdItem]->title);
1256 tabxobj[IdItem]->title=safestrdup(FGettext(arg[1]));
1257 if (tabxobj[IdItem]->TypeWidget != SwallowExec)
1258 XClearWindow(dpy, tabxobj[IdItem]->win);
1259 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1260 free(arg[0]);
1261 free(arg[1]);
1264 /* ChangeIcon */
1265 static void ChangeIcon (int NbArg,long *TabArg)
1267 int i=0;
1268 char *arg[2];
1269 int IdItem;
1271 arg[0] = CalcArg(TabArg,&i);
1272 i++;
1273 arg[1] = CalcArg(TabArg,&i);
1274 IdItem = TabIdObj[atoi(arg[0])];
1275 if (tabxobj[IdItem]->icon)
1277 free(tabxobj[IdItem]->icon);
1279 tabxobj[IdItem]->icon = safestrdup(arg[1]);
1280 LoadIcon(tabxobj[IdItem]);
1281 if (tabxobj[IdItem]->TypeWidget != SwallowExec)
1283 XClearWindow(dpy, tabxobj[IdItem]->win);
1285 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1286 free(arg[0]);
1287 free(arg[1]);
1290 /* ChangeForeColor */
1291 static void ChangeForeColor (int NbArg,long *TabArg)
1293 int i=0;
1294 char *arg[2];
1295 int IdItem;
1297 arg[0] = CalcArg(TabArg,&i);
1298 i++;
1299 arg[1] = CalcArg(TabArg,&i);
1300 IdItem = TabIdObj[atoi(arg[0])];
1302 /* Liberation de la couleur */
1303 if (tabxobj[IdItem]->colorset < 0)
1304 PictureFreeColors(
1305 dpy,Pcmap,(void*)(&(tabxobj[IdItem])->TabColor[fore]),1,0, True);
1307 if (tabxobj[IdItem]->forecolor)
1309 free(tabxobj[IdItem]->forecolor);
1311 tabxobj[IdItem]->forecolor= safestrdup(arg[1]);
1313 tabxobj[IdItem]->TabColor[fore] = GetColor(tabxobj[IdItem]->forecolor);
1314 if (tabxobj[IdItem]->colorset >= 0) {
1315 tabxobj[IdItem]->TabColor[back] = GetColor(tabxobj[IdItem]->backcolor);
1316 tabxobj[IdItem]->TabColor[hili] = GetColor(tabxobj[IdItem]->hilicolor);
1317 tabxobj[IdItem]->TabColor[shad] = GetColor(tabxobj[IdItem]->shadcolor);
1319 tabxobj[IdItem]->colorset = -1;
1321 if (tabxobj[IdItem]->TypeWidget != SwallowExec)
1322 XClearWindow(dpy, tabxobj[IdItem]->win);
1323 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1325 free(arg[0]);
1326 free(arg[1]);
1330 /* ChangeBackColor */
1331 static void ChangeBackColor (int NbArg,long *TabArg)
1333 int i=0;
1334 char *arg[2];
1335 int IdItem;
1337 arg[0] = CalcArg(TabArg,&i);
1338 i++;
1339 arg[1] = CalcArg(TabArg,&i);
1340 IdItem = TabIdObj[atoi(arg[0])];
1342 /* Liberation de la couleur */
1343 if (tabxobj[IdItem]->colorset < 0)
1344 PictureFreeColors(
1345 dpy,Pcmap,(void*)(&(tabxobj[IdItem])->TabColor[back]),1,0,True);
1347 if (tabxobj[IdItem]->backcolor)
1349 free(tabxobj[IdItem]->backcolor);
1351 tabxobj[IdItem]->backcolor= safestrdup(arg[1]);
1353 tabxobj[IdItem]->TabColor[back] = GetColor(tabxobj[IdItem]->backcolor);
1354 if (tabxobj[IdItem]->colorset >= 0) {
1355 tabxobj[IdItem]->TabColor[fore] = GetColor(tabxobj[IdItem]->forecolor);
1356 tabxobj[IdItem]->TabColor[hili] = GetColor(tabxobj[IdItem]->hilicolor);
1357 tabxobj[IdItem]->TabColor[shad] = GetColor(tabxobj[IdItem]->shadcolor);
1359 tabxobj[IdItem]->colorset = -1;
1361 if (tabxobj[IdItem]->TypeWidget != SwallowExec) {
1362 XSetWindowBackground(dpy,
1363 tabxobj[IdItem]->win,
1364 tabxobj[IdItem]->TabColor[back]);
1365 XClearWindow(dpy, tabxobj[IdItem]->win);
1367 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1369 free(arg[0]);
1370 free(arg[1]);
1373 /* ChangeMainColorset */
1374 static void ChangeMainColorset (int i)
1376 /* Liberation de la couleur */
1377 if (x11base->colorset < 0) {
1378 PictureFreeColors(dpy,Pcmap,&x11base->TabColor[fore],1,0,True);
1379 PictureFreeColors(dpy,Pcmap,&x11base->TabColor[back],1,0,True);
1380 PictureFreeColors(dpy,Pcmap,&x11base->TabColor[hili],1,0,True);
1381 PictureFreeColors(dpy,Pcmap,&x11base->TabColor[shad],1,0,True);
1383 x11base->colorset = i;
1384 AllocColorset(i);
1385 x11base->TabColor[fore] = Colorset[i].fg;
1386 x11base->TabColor[back] = Colorset[i].bg;
1387 x11base->TabColor[hili] = Colorset[i].hilite;
1388 x11base->TabColor[shad] = Colorset[i].shadow;
1389 SetWindowBackground(dpy, x11base->win,x11base->size.width,x11base->size.height,
1390 &Colorset[i], Pdepth, x11base->gc, True);
1393 /* ChangeMainColorset */
1394 static void ChangeColorset (int NbArg,long *TabArg)
1396 int i=0;
1397 char *arg[2];
1398 int IdItem;
1400 arg[0] = CalcArg(TabArg,&i);
1401 i++;
1402 arg[1] = CalcArg(TabArg,&i);
1403 if (atoi(arg[0]) == 0) {
1404 ChangeMainColorset(atoi(arg[1]));
1405 free(arg[0]);
1406 free(arg[1]);
1407 return;
1409 IdItem = TabIdObj[atoi(arg[0])];
1411 /* Liberation de la couleur */
1412 if (tabxobj[IdItem]->colorset < 0) {
1413 PictureFreeColors(dpy,Pcmap,&tabxobj[IdItem]->TabColor[fore],1,0,True);
1414 PictureFreeColors(dpy,Pcmap,&tabxobj[IdItem]->TabColor[back],1,0,True);
1415 PictureFreeColors(dpy,Pcmap,&tabxobj[IdItem]->TabColor[hili],1,0,True);
1416 PictureFreeColors(dpy,Pcmap,&tabxobj[IdItem]->TabColor[shad],1,0,True);
1418 sscanf(arg[1], "%d", &i);
1419 tabxobj[IdItem]->colorset = i;
1420 AllocColorset(i);
1421 tabxobj[IdItem]->TabColor[fore] = Colorset[i].fg;
1422 tabxobj[IdItem]->TabColor[back] = Colorset[i].bg;
1423 tabxobj[IdItem]->TabColor[hili] = Colorset[i].hilite;
1424 tabxobj[IdItem]->TabColor[shad] = Colorset[i].shadow;
1426 if (tabxobj[IdItem]->TypeWidget != SwallowExec) {
1427 SetWindowBackground(dpy, tabxobj[IdItem]->win, tabxobj[IdItem]->width,
1428 tabxobj[IdItem]->height, &Colorset[i], Pdepth,
1429 tabxobj[IdItem]->gc, False);
1430 XClearWindow(dpy, tabxobj[IdItem]->win);
1432 tabxobj[IdItem]->DrawObj(tabxobj[IdItem],NULL);
1434 free(arg[0]);
1435 free(arg[1]);
1438 /* ChangeWindowTitle */
1439 static void ChangeWindowTitle(int NbArg,long * TabArg){
1441 char *arg;
1442 int tmpVal=NbArg-1;
1444 arg=CalcArg(TabArg,&tmpVal);
1445 XChangeProperty(
1446 dpy, x11base->win, XA_WM_NAME, XA_STRING, 8, PropModeReplace,
1447 (unsigned char*)arg, strlen(arg));
1448 free(arg);
1452 /* ChangeWindowTitleFromArg */
1453 static void ChangeWindowTitleFromArg(int NbArg,long * TabArg){
1455 char *arg;
1456 int argVal;
1457 int tmpVal=NbArg-1;
1459 arg=CalcArg(TabArg,&tmpVal);
1460 argVal = atoi(arg);
1461 free(arg);
1463 if(x11base->TabArg[argVal]!=NULL){
1464 arg = (char*)safecalloc(strlen(x11base->TabArg[argVal])+1,sizeof(char));
1465 arg = strcpy(arg,x11base->TabArg[argVal]);
1466 }else{
1467 arg = (char*)safecalloc(1,sizeof(char));
1468 arg = strcpy(arg,"");
1471 XChangeProperty(
1472 dpy, x11base->win, XA_WM_NAME, XA_STRING, 8, PropModeReplace,
1473 (unsigned char*)arg, strlen(arg));
1474 free(arg);
1478 /* SetVar */
1479 static void SetVar (int NbArg,long *TabArg)
1481 int i;
1482 char *str,*tempstr;
1484 str=(char*)safecalloc(sizeof(char),1);
1485 for (i=1; i<NbArg; i++)
1487 tempstr = CalcArg(TabArg,&i);
1488 str = (char*)saferealloc((void*)str,
1489 sizeof(char) * (1 + strlen(str) + strlen(tempstr)));
1490 str = strcat(str,tempstr);
1491 free(tempstr);
1494 free(TabVVar[TabArg[0]]);
1495 TabVVar[TabArg[0]] = str;
1498 /* SendSign */
1499 static void SendSign (int NbArg,long *TabArg)
1501 int i=0;
1502 char *arg[2];
1503 int IdItem;
1504 int TypeMsg;
1506 arg[0] = CalcArg(TabArg,&i);
1507 i++;
1508 arg[1] = CalcArg(TabArg,&i);
1509 IdItem = TabIdObj[atoi(arg[0])];
1510 TypeMsg = atoi(arg[1]);
1511 SendMsg(tabxobj[IdItem],TypeMsg);
1512 free(arg[0]);
1513 free(arg[1]);
1516 /* MyWarpPointer */
1517 static void MyWarpPointer(int NbArg,long *TabArg)
1519 int i=0;
1520 char *arg;
1521 int IdItem;
1523 arg=CalcArg(TabArg,&i);
1524 IdItem= TabIdObj[atoi(arg)];
1525 /* Deplacement du pointeur sur l'objet */
1526 FWarpPointer(dpy, None, tabxobj[IdItem]->win, 0, 0, 0, 0,
1527 tabxobj[IdItem]->width / 2, tabxobj[IdItem]->height + 10);
1528 free(arg);
1531 /* Quit */
1532 void Quit (int NbArg,long *TabArg)
1534 exit(0);
1537 /* IfThen */
1538 static void IfThen (int NbArg,long *TabArg)
1540 char *arg[10];
1541 int i,j;
1542 int CurrArg=0;
1543 int IdFuncComp = 0;
1545 /* Verification de la condition */
1546 for (j=0; j<NbArg-2; j++)
1548 if (TabArg[j] > 100000) /* Cas du codage d'un nombre */
1550 i = (int)TabArg[j] - 200000;
1551 arg[CurrArg] = (char*)safecalloc(1,sizeof(char)*10);
1552 sprintf(arg[CurrArg],"%d",i);
1553 CurrArg++;
1555 else if (TabArg[j] < -200000)/* Cas d'un id de fonction de comparaison */
1557 IdFuncComp = TabArg[j] + 250000;
1559 else if (TabArg[j] < -100000) /* Cas d'un id de fonction */
1561 arg[CurrArg] = TabFunc[TabArg[j]+150000](&j,TabArg);
1562 CurrArg++;
1564 else /* Cas d'une variable */
1566 arg[CurrArg] = safestrdup(TabVVar[TabArg[j]]);
1567 CurrArg++;
1571 /* Comparaison des arguments */
1572 if (TabComp[IdFuncComp](arg[0],arg[1]))
1573 ExecBloc((Bloc*)TabArg[NbArg-2]);
1574 else if (TabArg[NbArg-1]!=0)
1575 ExecBloc((Bloc*)TabArg[NbArg-1]);
1577 free(arg[0]);
1578 free(arg[1]);
1581 /* Instruction boucle **/
1582 static void Loop (int NbArg,long *TabArg)
1584 int IdVar;
1585 char *arg[2];
1586 int limit[2];
1587 int i;
1588 int CurrArg=0;
1590 /* le premier argument est une variable */
1591 IdVar = TabArg[0];
1592 /*On ajuste la taille de la var pour contenir un nombre */
1593 TabVVar[TabArg[0]] = (char*)saferealloc(TabVVar[TabArg[0]],sizeof(char)*10);
1594 /* Calcul des 2 autres arguments */
1595 for (i=1; i<NbArg; i++)
1597 if (TabArg[i] > 100000) /* Cas du codage d'un nombre */
1599 int x;
1600 x = (int)TabArg[i] - 200000;
1601 arg[CurrArg] = (char*)safecalloc(1,sizeof(char)*10);
1602 sprintf(arg[CurrArg],"%d",x);
1604 else if (TabArg[i] < -100000) /* Cas d'un id de fonction */
1606 arg[CurrArg] = TabFunc[TabArg[i]+150000](&i,TabArg);
1608 else /* Cas d'une variable */
1610 arg[CurrArg] = safestrdup(TabVVar[TabArg[i]]);
1612 CurrArg++;
1614 limit[0] = atoi(arg[0]);
1615 limit[1] = atoi(arg[1]);
1616 if (limit[0] < limit[1])
1618 for (i=limit[0]; i<=limit[1]; i++)
1620 /* On met a jour la variable */
1621 sprintf(TabVVar[TabArg[0]],"%d",i);
1622 ExecBloc((Bloc*)TabArg[NbArg-1]);
1625 else
1627 for (i=limit[0]; i<=limit[1]; i++)
1629 sprintf(TabVVar[TabArg[0]],"%d",i);
1630 ExecBloc((Bloc*)TabArg[NbArg-1]);
1634 free(arg[0]);
1635 free(arg[1]);
1638 /** Instruction While **/
1639 static void While (int NbArg,long *TabArg)
1641 char *arg[3],*str;
1642 int i;
1643 int Loop=1;
1644 int IdFuncComp;
1646 while (Loop)
1648 i = 0;
1649 arg[0] = CalcArg(TabArg,&i);
1650 i++;
1651 str = CalcArg(TabArg,&i);
1652 IdFuncComp = atoi(str);
1653 free(str);
1654 i++;
1655 arg[1] = CalcArg(TabArg,&i);
1657 Loop=TabComp[IdFuncComp](arg[0],arg[1]);
1658 if (Loop) ExecBloc((Bloc*)TabArg[NbArg-1]);
1659 free(arg[0]);
1660 free(arg[1]);
1664 /* WriteToFile */
1665 static void WriteToFile (int NbArg,long *TabArg)
1667 int i=0;
1668 char *arg[2],str[50],*tempstr,*home,*file;
1669 FILE *f;
1670 char StrBegin[100];
1671 char StrEnd[10];
1672 size_t size;
1673 char *buf;
1674 int maxsize=32000;
1675 int CurrPos=0,CurrPos2;
1676 int OldPID;
1678 arg[0] = CalcArg(TabArg,&i);
1679 arg[1]=(char*)safecalloc(1,256);
1680 for (i=1; i<NbArg; i++)
1682 tempstr = CalcArg(TabArg,&i);
1683 arg[1] = strcat(arg[1],tempstr);
1684 free(tempstr);
1686 if (arg[1][strlen(arg[1])-1] != '\n')
1688 i = strlen(arg[1]);
1689 arg[1] = (char*)saferealloc(arg[1],strlen(arg[1])+2);
1690 arg[1][i] = '\n';
1691 arg[1][i+1] = '\0';
1694 sprintf(StrEnd,"#end\n");
1695 sprintf(StrBegin,"#%s,",ScriptName);
1697 buf=(char*)safecalloc(1,maxsize);
1699 if (arg[0][0] != '/')
1701 file = safestrdup(arg[0]);
1702 home = getenv("HOME");
1703 arg[0] = (char*)saferealloc(arg[0],
1704 sizeof(char) * (strlen(arg[0]) + 4 +strlen(home)));
1705 sprintf(arg[0],"%s/%s",home,file);
1706 free(file);
1708 f = fopen(arg[0],"a+");
1709 fseek(f, 0, SEEK_SET);
1710 size = fread(buf, 1 ,maxsize,f);
1711 while(((strncmp(StrBegin, &buf[CurrPos], strlen(StrBegin))) !=0) &&
1712 (CurrPos<size))
1714 CurrPos++;
1716 if (CurrPos==size)
1718 sprintf(buf,"%s\n%s%d\n",buf,StrBegin,getpid());
1719 sprintf(buf,"%s%s",buf,arg[1]);
1720 sprintf(buf,"%s%s\n",buf,StrEnd);
1722 else
1724 sscanf(&buf[CurrPos+strlen(StrBegin)],"%d",&OldPID);
1725 if (OldPID == getpid())
1727 sprintf(str,"%d\n",OldPID);
1728 while(((strncmp(StrEnd,&buf[CurrPos],strlen(StrEnd))) != 0) &&
1729 (CurrPos<size))
1731 CurrPos++;
1733 memmove(&buf[CurrPos+strlen(arg[1])],&buf[CurrPos],strlen(buf)-CurrPos);
1734 memmove(&buf[CurrPos],arg[1],strlen(arg[1]));
1736 else /* Remplacement des anciennes commandes */
1738 CurrPos = CurrPos+strlen(StrBegin);
1739 CurrPos2 = CurrPos;
1740 while(((strncmp(StrEnd,&buf[CurrPos2],strlen(StrEnd))) != 0) &&
1741 (CurrPos2<size))
1743 CurrPos2++;
1745 sprintf(str,"%d\n%s",getpid(),arg[1]);
1746 memmove(&buf[CurrPos+strlen(str)], &buf[CurrPos2], strlen(buf)-CurrPos2);
1747 buf[strlen(buf)-((CurrPos2-CurrPos)-strlen(str))] = '\0';
1748 memmove(&buf[CurrPos],str,strlen(str));
1752 fclose(f);
1753 f= fopen(arg[0],"w");
1754 if (f == NULL)
1756 fprintf(stderr,"[%s][WriteToFile]: Unable to open file %s\n",
1757 ScriptName,arg[0]);
1758 return;
1760 fwrite(buf,1,strlen(buf),f);
1761 fclose(f);
1763 free(arg[0]);
1764 free(arg[1]);
1767 /* SendToScript */
1768 static void SendToScript (int NbArg,long *TabArg)
1770 char *tempstr,*Msg,*R;
1771 int dest;
1772 int j=0;
1773 Atom myatom;
1775 /* Calcul destinataire */
1776 tempstr=CalcArg(TabArg,&j);
1777 dest=(int)atoi(tempstr);
1778 free(tempstr);
1780 /* Calcul contenu */
1781 Msg=(char*)safecalloc(256,sizeof(char));
1782 for (j=1;j<NbArg;j++)
1784 tempstr=CalcArg(TabArg,&j);
1785 Msg=(char*)saferealloc((void*)Msg,strlen(Msg)+strlen(tempstr)+1);
1786 Msg=strcat(Msg,tempstr);
1787 free(tempstr);
1790 /* Calcul recepteur */
1791 R=(char*)safecalloc(strlen(x11base->TabScriptId[dest])+1,sizeof(char));
1792 sprintf(R,"%s",x11base->TabScriptId[dest]);
1793 myatom=XInternAtom(dpy,R,True);
1795 if ((BuffSend.NbMsg<40)&&(XGetSelectionOwner(dpy,myatom)!=None))
1797 /* Enregistrement dans le buffer du message */
1798 BuffSend.TabMsg[BuffSend.NbMsg].Msg=Msg;
1799 /* Enregistrement dans le buffer du destinataire */
1800 BuffSend.TabMsg[BuffSend.NbMsg].R=R;
1801 /* Enregistrement du message */
1802 BuffSend.NbMsg++;
1804 /* Reveil du destinataire */
1805 XConvertSelection(dpy,
1806 XInternAtom(dpy,x11base->TabScriptId[dest],True),
1807 propriete, propriete, x11base->win, CurrentTime);
1809 else
1811 fprintf(stderr,"[%s][SendToScript]: Too many messages sended\n",
1812 ScriptName);
1813 free(Msg);
1817 /* Key */
1818 static void Key (int NbArg,long *TabArg)
1820 char *key_string,*in_modifier,*action,*str,*tmp,*widget,*sig;
1821 int modifier;
1822 int error = 0;
1823 int i,j=0;
1824 KeySym keysym = NoSymbol;
1826 /* the key */
1827 key_string = CalcArg(TabArg,&j);
1828 /* the modifier */
1829 j++;
1830 in_modifier = CalcArg(TabArg,&j);
1831 /* the widget */
1832 j++;
1833 widget = CalcArg(TabArg,&j);
1834 /* the signal */
1835 j++;
1836 sig = CalcArg(TabArg,&j);
1838 /* the string */
1839 str = (char*)safecalloc(256,sizeof(char));
1840 for (i=j+1; i<NbArg; i++)
1842 tmp = CalcArg(TabArg,&i);
1843 str = (char*)saferealloc((void*)str, strlen(str) + strlen(tmp) + 1);
1844 str = strcat(str, tmp);
1845 free(tmp);
1848 tmp = safestrdup(CatString3(widget, " ", sig));
1849 action = safestrdup(CatString3(tmp, " ", str));
1850 free(sig);
1851 free(widget);
1852 free(str);
1853 free(tmp);
1855 keysym = FvwmStringToKeysym(dpy, key_string);
1856 if (keysym == 0)
1858 fprintf(stderr, "[%s][Key]: No such key: %s", ScriptName, key_string);
1859 error = 1;
1862 if (modifiers_string_to_modmask(in_modifier, &modifier)) {
1863 fprintf(stderr,"[%s][Key]: bad modifier: %s\n",
1864 ScriptName,in_modifier);
1865 error = 1;
1868 if (error) {
1869 free(key_string);
1870 free(in_modifier);
1871 free(action);
1872 return;
1875 AddBinding(
1876 dpy, &BindingsList, BIND_KEYPRESS, STROKE_ARG(0) 0, keysym,
1877 key_string, modifier, C_WINDOW, (void *)action, NULL, NULL);
1878 free(key_string);
1879 free(in_modifier);
1883 * GetText Support
1886 static char *FuncGettext(int *NbArg,long *TabArg)
1888 char *str;
1889 char *string;
1891 (*NbArg)++;
1892 str = CalcArg(TabArg,NbArg);
1894 string = FGettextCopy(str);
1895 if (str && string != str)
1897 free(str);
1899 return (char *)string;
1903 * Fonction d'initialisation de TabCom et TabFunc
1905 void InitCom(void)
1907 /* commande */
1908 TabCom[1]=Exec;
1909 TabCom[2]=HideObj;
1910 TabCom[3]=ShowObj;
1911 TabCom[4]=ChangeValue;
1912 TabCom[5]=ChangePos;
1913 TabCom[6]=ChangeSize;
1914 TabCom[7]=ChangeIcon;
1915 TabCom[8]=ChangeTitle;
1916 TabCom[9]=ChangeFont;
1917 TabCom[10]=ChangeForeColor;
1918 TabCom[11]=SetVar;
1919 TabCom[12]=SendSign;
1920 TabCom[13]=Quit;
1921 TabCom[14]=IfThen;
1922 TabCom[15]=Loop;
1923 TabCom[16]=While;
1924 TabCom[17]=MyWarpPointer;
1925 TabCom[18]=WriteToFile;
1926 TabCom[19]=ChangeBackColor;
1927 TabCom[21]=ChangeValueMax;
1928 TabCom[22]=ChangeValueMin;
1929 TabCom[23]=SendToScript;
1930 TabCom[24]=ChangeColorset;
1931 TabCom[25]=Key;
1932 TabCom[26]=ChangeLocaleTitle;
1933 TabCom[27]=ChangeWindowTitle;
1934 TabCom[28]=ChangeWindowTitleFromArg;
1936 /* Fonction */
1937 TabFunc[1]=FuncGetValue;
1938 TabFunc[2]=FuncGetTitle;
1939 TabFunc[3]=FuncGetOutput;
1940 TabFunc[4]=FuncNumToHex;
1941 TabFunc[5]=FuncHexToNum;
1942 TabFunc[6]=FuncAdd;
1943 TabFunc[7]=FuncMult;
1944 TabFunc[8]=FuncDiv;
1945 TabFunc[9]=FuncStrCopy;
1946 TabFunc[10]=LaunchScript;
1947 TabFunc[11]=GetScriptFather;
1948 TabFunc[12]=ReceivFromScript;
1949 TabFunc[13]=RemainderOfDiv;
1950 TabFunc[14]=GetTime;
1951 TabFunc[15]=GetScriptArg;
1952 TabFunc[16]=FuncGetFore;
1953 TabFunc[17]=FuncGetBack;
1954 TabFunc[18]=FuncGetHili;
1955 TabFunc[19]=FuncGetShad;
1956 TabFunc[20]=FuncGetMinValue;
1957 TabFunc[21]=FuncGetMaxValue;
1958 TabFunc[22]=FuncGetPid;
1959 TabFunc[23]=FuncSendMsgAndGet;
1960 TabFunc[24]=FuncParse;
1961 TabFunc[25]=FuncGetLastString;
1962 TabFunc[26]=FuncGettext;
1964 /* Fonction de comparaison */
1965 TabComp[1]=Inf;
1966 TabComp[2]=InfEq;
1967 TabComp[3]=Equal;
1968 TabComp[4]=SupEq;
1969 TabComp[5]=Sup;
1970 TabComp[6]=Diff;