Remove trailing whitespace in recently added dockapps.
[dockapps.git] / washerdryer / wmgeneral / misc.c
blobd9179176de0511282a45dab420f80ea01f316cca
1 /* dock.c- built-in Dock module for WindowMaker
3 * WindowMaker window manager
5 * Copyright (c) 1997 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <stdlib.h>
23 #include <string.h>
24 #include "list.h"
25 #include "misc.h"
28 *----------------------------------------------------------------------
29 * parse_command--
30 * Divides a command line into a argv/argc pair.
31 *----------------------------------------------------------------------
33 #define PRC_ALPHA 0
34 #define PRC_BLANK 1
35 #define PRC_ESCAPE 2
36 #define PRC_DQUOTE 3
37 #define PRC_EOS 4
38 #define PRC_SQUOTE 5
40 typedef struct
42 short nstate;
43 short output;
45 DFA;
48 static DFA mtable[9][6] =
51 {3, 1},
52 {0, 0},
53 {4, 0},
54 {1, 0},
55 {8, 0},
56 {6, 0}},
58 {1, 1},
59 {1, 1},
60 {2, 0},
61 {3, 0},
62 {5, 0},
63 {1, 1}},
65 {1, 1},
66 {1, 1},
67 {1, 1},
68 {1, 1},
69 {5, 0},
70 {1, 1}},
72 {3, 1},
73 {5, 0},
74 {4, 0},
75 {1, 0},
76 {5, 0},
77 {6, 0}},
79 {3, 1},
80 {3, 1},
81 {3, 1},
82 {3, 1},
83 {5, 0},
84 {3, 1}},
86 {-1, -1},
87 {0, 0},
88 {0, 0},
89 {0, 0},
90 {0, 0},
91 {0, 0}}, /* final state */
93 {6, 1},
94 {6, 1},
95 {7, 0},
96 {6, 1},
97 {5, 0},
98 {3, 0}},
100 {6, 1},
101 {6, 1},
102 {6, 1},
103 {6, 1},
104 {5, 0},
105 {6, 1}},
107 {-1, -1},
108 {0, 0},
109 {0, 0},
110 {0, 0},
111 {0, 0},
112 {0, 0}}, /* final state */
115 char * next_token (char *word, char **next)
117 char *ptr;
118 char *ret, *t;
119 int state, ctype;
121 t = ret = malloc (strlen (word) + 1);
122 ptr = word;
124 state = 0;
125 *t = 0;
126 while (1)
128 if (*ptr == 0)
129 ctype = PRC_EOS;
130 else if (*ptr == '\\')
131 ctype = PRC_ESCAPE;
132 else if (*ptr == '"')
133 ctype = PRC_DQUOTE;
134 else if (*ptr == '\'')
135 ctype = PRC_SQUOTE;
136 else if (*ptr == ' ' || *ptr == '\t')
137 ctype = PRC_BLANK;
138 else
139 ctype = PRC_ALPHA;
141 if (mtable[state][ctype].output)
143 *t = *ptr;
144 t++;
145 *t = 0;
147 state = mtable[state][ctype].nstate;
148 ptr++;
149 if (mtable[state][0].output < 0)
151 break;
155 if (*ret == 0)
156 t = NULL;
157 else
158 t = strdup (ret);
160 free (ret);
162 if (ctype == PRC_EOS)
163 *next = NULL;
164 else
165 *next = ptr;
167 return t;
171 extern void parse_command (char *command, char ***argv, int *argc)
173 LinkedList *list = NULL;
174 char *token, *line;
175 int count, i;
177 line = command;
180 token = next_token (line, &line);
181 if (token)
183 list = list_cons (token, list);
186 while (token != NULL && line != NULL);
188 count = list_length (list);
189 *argv = malloc (sizeof (char *) * count);
190 i = count;
191 while (list != NULL)
193 (*argv)[--i] = list->head;
194 list_remove_head (&list);
196 *argc = count;
199 extern pid_t execCommand (char *command)
201 pid_t pid;
202 char **argv;
203 int argc;
205 parse_command (command, &argv, &argc);
207 if (argv == NULL)
209 return 0;
212 if ((pid = fork ()) == 0)
214 char **args;
215 int i;
217 args = malloc (sizeof (char *) * (argc + 1));
218 if (!args)
219 exit (10);
220 for (i = 0; i < argc; i++)
222 args[i] = argv[i];
224 args[argc] = NULL;
225 execvp (argv[0], args);
226 exit (10);
228 return pid;