cvsimport
[fvwm.git] / modules / FvwmForm / ParseCommand.c
blobcd1a1095900b5a1a8139af5b8e3480b40cc55818
1 /* -*-c-*- */
2 /*
3 * ParseComand.c was originally part of FvwmForm.c.
5 * Turned into separate file 02/27/99, Dan Espen.
7 * FvwmForm is original work of Thomas Zuwei Feng.
9 * Copyright Feb 1995, Thomas Zuwei Feng. No guarantees or warantees are
10 * provided or implied in any way whatsoever. Use this program at your own
11 * risk. Permission to use, modify, and redistribute this program is hereby
12 * given, provided that this copyright is kept intact.
15 /* This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "config.h"
31 #include "libs/fvwmlib.h"
32 #include <stdio.h>
33 #include <ctype.h>
34 #include <sys/types.h>
35 #include <FvwmForm.h> /* common FvwmForm stuff */
37 static char *buf;
38 static int N = 8;
40 /* Macro used in following functions */
41 #define AddChar(chr) \
42 { if (dn >= N) {\
43 N *= 2;\
44 buf = (char *)saferealloc(buf, N);\
46 buf[dn++] = (chr);\
49 /* do var substitution for command string */
50 char * ParseCommand (int dn, char *sp, char end, int *dn1, char **sp1)
52 static char var[256];
53 char c, x, *wp, *cp, *vp;
54 int j, dn2;
55 int added_sel;
56 Item *item;
58 if (buf == 0) { /* if no buffer yet */
59 buf = (char *)malloc(N);
61 while (1) {
62 c = *(sp++);
63 if (c == '\0' || c == end) { /* end of substitution */
64 *dn1 = dn;
65 *sp1 = sp;
66 if (end == 0) { /* if end of command reached */
67 AddChar('\0'); /* make sure theres a null */
69 return(buf);
71 if (c == '$') { /* variable */
72 if (*sp != '(')
73 goto normal_char;
74 wp = ++sp;
75 vp = var;
76 while (1) {
77 x = *(sp++);
78 if (x == '\\') {
79 *(vp++) = '\\';
80 *(vp++) = *(sp++);
82 else if (x == ')' || x == '?' || x == '!') {
83 *(vp++) = '\0';
84 break;
86 else if (!isspace(x))
87 *(vp++) = x;
89 for (item = root_item_ptr; item != 0;
90 item = item->header.next) {/* all items */
91 if (strcmp(var, item->header.name) == 0) {
92 switch (item->type) {
93 case I_INPUT:
94 if (x == ')') {
95 for (cp = item->input.value; *cp != '\0'; cp++) {
96 AddChar(*cp);
98 } else {
99 ParseCommand(dn, sp, ')', &dn2, &sp);
100 if ((x == '?' && strlen(item->input.value) > 0) ||
101 (x == '!' && strlen(item->input.value) == 0))
102 dn = dn2;
104 break;
105 case I_CHOICE:
106 if (x == ')') {
107 for (cp = item->choice.value; *cp != '\0'; cp++)
108 AddChar(*cp);
109 } else {
110 ParseCommand(dn, sp, ')', &dn2, &sp);
111 if ((x == '?' && item->choice.on) ||
112 (x == '!' && !item->choice.on))
113 dn = dn2;
115 break;
116 case I_SELECT:
117 if (x != ')')
118 ParseCommand(dn, sp, ')', &dn2, &sp);
119 added_sel=0;
120 for (j = 0; j < item->selection.n; j++) {
121 if (item->selection.choices[j]->choice.on) {
122 if (added_sel) { /* if not first sel added */
123 AddChar(' '); /* insert space before next value */
125 added_sel=1;
126 for (cp = item->selection.choices[j]->choice.value;
127 *cp != '\0'; cp++) {
128 AddChar(*cp);
132 break;
134 goto next_loop;
137 goto next_loop;
138 } /* end char is $, not followed by ( */
139 /* if char is \, followed by ), want to pass thru the paren literally */
140 if (c == '\\' && *sp == ')') {
141 c = *(sp++); /* skip to the paren */
143 normal_char:
144 AddChar(c);
145 next_loop: