wmsun: Fix need to restart for Daylight Savings change.
[dockapps.git] / wmitime / wmgeneral / misc.c
blobfb36fd1bfc73de739baea30dda223440f4e94c96
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 #define _POSIX_C_SOURCE 200809L
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include "list.h"
28 #include "misc.h"
31 *----------------------------------------------------------------------
32 * parse_command--
33 * Divides a command line into a argv/argc pair.
34 *----------------------------------------------------------------------
36 #define PRC_ALPHA 0
37 #define PRC_BLANK 1
38 #define PRC_ESCAPE 2
39 #define PRC_DQUOTE 3
40 #define PRC_EOS 4
41 #define PRC_SQUOTE 5
43 typedef struct {
44 short nstate;
45 short output;
46 } DFA;
49 static DFA mtable[9][6] = {
50 {{3,1},{0,0},{4,0},{1,0},{8,0},{6,0}},
51 {{1,1},{1,1},{2,0},{3,0},{5,0},{1,1}},
52 {{1,1},{1,1},{1,1},{1,1},{5,0},{1,1}},
53 {{3,1},{5,0},{4,0},{1,0},{5,0},{6,0}},
54 {{3,1},{3,1},{3,1},{3,1},{5,0},{3,1}},
55 {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
56 {{6,1},{6,1},{7,0},{6,1},{5,0},{3,0}},
57 {{6,1},{6,1},{6,1},{6,1},{5,0},{6,1}},
58 {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
61 char*
62 next_token(char *word, char **next)
64 char *ptr;
65 char *ret, *t;
66 int state, ctype;
68 t = ret = malloc(strlen(word)+1);
69 if (ret == NULL) {
70 fprintf(stderr, "Insufficient memory.\n");
71 exit(EXIT_FAILURE);
73 ptr = word;
75 state = 0;
76 *t = 0;
77 while (1) {
78 if (*ptr==0)
79 ctype = PRC_EOS;
80 else if (*ptr=='\\')
81 ctype = PRC_ESCAPE;
82 else if (*ptr=='"')
83 ctype = PRC_DQUOTE;
84 else if (*ptr=='\'')
85 ctype = PRC_SQUOTE;
86 else if (*ptr==' ' || *ptr=='\t')
87 ctype = PRC_BLANK;
88 else
89 ctype = PRC_ALPHA;
91 if (mtable[state][ctype].output) {
92 *t = *ptr; t++;
93 *t = 0;
95 state = mtable[state][ctype].nstate;
96 ptr++;
97 if (mtable[state][0].output<0) {
98 break;
102 if (*ret==0)
103 t = NULL;
104 else
105 t = strdup(ret);
107 free(ret);
109 if (ctype==PRC_EOS)
110 *next = NULL;
111 else
112 *next = ptr;
114 return t;
118 extern void
119 parse_command(char *command, char ***argv, int *argc)
121 LinkedList *list = NULL;
122 char *token, *line;
123 int count, i;
125 line = command;
126 do {
127 token = next_token(line, &line);
128 if (token) {
129 list = list_cons(token, list);
131 } while (token!=NULL && line!=NULL);
133 count = list_length(list);
134 *argv = malloc(sizeof(char*)*count);
135 i = count;
136 while (list!=NULL) {
137 (*argv)[--i] = list->head;
138 list_remove_head(&list);
140 *argc = count;
143 extern pid_t
144 execCommand(char *command)
146 pid_t pid;
147 char **argv;
148 int argc;
150 parse_command(command, &argv, &argc);
152 if (argv==NULL) {
153 return 0;
156 if ((pid=fork())==0) {
157 char **args;
158 int i;
160 args = malloc(sizeof(char*)*(argc+1));
161 if (!args)
162 exit(10);
163 for (i=0; i<argc; i++) {
164 args[i] = argv[i];
166 args[argc] = NULL;
167 execvp(argv[0], args);
168 exit(10);
170 free(argv);
171 return pid;