wmgeneral: Add proper copyright headers; fix changelog formatting.
[dockapps.git] / wmtz / wmgeneral / misc.c
blobfa7f69dcf488b1d9ca7c13e0dcf62005a806322e
1 /* wmgeneral miscellaneous functions
3 * from dock.c - built-in Dock module for 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 * USA.
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include "list.h"
27 #include "misc.h"
30 *----------------------------------------------------------------------
31 * parse_command--
32 * Divides a command line into a argv/argc pair.
33 *----------------------------------------------------------------------
35 #define PRC_ALPHA 0
36 #define PRC_BLANK 1
37 #define PRC_ESCAPE 2
38 #define PRC_DQUOTE 3
39 #define PRC_EOS 4
40 #define PRC_SQUOTE 5
42 typedef struct {
43 short nstate;
44 short output;
45 } DFA;
48 static DFA mtable[9][6] = {
49 {{3,1},{0,0},{4,0},{1,0},{8,0},{6,0}},
50 {{1,1},{1,1},{2,0},{3,0},{5,0},{1,1}},
51 {{1,1},{1,1},{1,1},{1,1},{5,0},{1,1}},
52 {{3,1},{5,0},{4,0},{1,0},{5,0},{6,0}},
53 {{3,1},{3,1},{3,1},{3,1},{5,0},{3,1}},
54 {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
55 {{6,1},{6,1},{7,0},{6,1},{5,0},{3,0}},
56 {{6,1},{6,1},{6,1},{6,1},{5,0},{6,1}},
57 {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
60 char*
61 next_token(char *word, char **next)
63 char *ptr;
64 char *ret, *t;
65 int state, ctype;
67 t = ret = malloc(strlen(word)+1);
68 if (ret == NULL) {
69 fprintf(stderr, "Insufficient memory.\n");
70 exit(EXIT_FAILURE);
72 ptr = word;
74 state = 0;
75 *t = 0;
76 while (1) {
77 if (*ptr==0)
78 ctype = PRC_EOS;
79 else if (*ptr=='\\')
80 ctype = PRC_ESCAPE;
81 else if (*ptr=='"')
82 ctype = PRC_DQUOTE;
83 else if (*ptr=='\'')
84 ctype = PRC_SQUOTE;
85 else if (*ptr==' ' || *ptr=='\t')
86 ctype = PRC_BLANK;
87 else
88 ctype = PRC_ALPHA;
90 if (mtable[state][ctype].output) {
91 *t = *ptr; t++;
92 *t = 0;
94 state = mtable[state][ctype].nstate;
95 ptr++;
96 if (mtable[state][0].output<0) {
97 break;
101 if (*ret==0)
102 t = NULL;
103 else
104 t = strdup(ret);
106 free(ret);
108 if (ctype==PRC_EOS)
109 *next = NULL;
110 else
111 *next = ptr;
113 return t;
117 extern void
118 parse_command(char *command, char ***argv, int *argc)
120 LinkedList *list = NULL;
121 char *token, *line;
122 int count, i;
124 line = command;
125 do {
126 token = next_token(line, &line);
127 if (token) {
128 list = list_cons(token, list);
130 } while (token!=NULL && line!=NULL);
132 count = list_length(list);
133 *argv = malloc(sizeof(char*)*count);
134 i = count;
135 while (list!=NULL) {
136 (*argv)[--i] = list->head;
137 list_remove_head(&list);
139 *argc = count;
142 extern pid_t
143 execCommand(char *command)
145 pid_t pid;
146 char **argv;
147 int argc;
149 parse_command(command, &argv, &argc);
151 if (argv==NULL) {
152 return 0;
155 if ((pid=fork())==0) {
156 char **args;
157 int i;
159 args = malloc(sizeof(char*)*(argc+1));
160 if (!args)
161 exit(10);
162 for (i=0; i<argc; i++) {
163 args[i] = argv[i];
165 args[argc] = NULL;
166 execvp(argv[0], args);
167 exit(10);
169 free(argv);
170 return pid;