1 #include "wmcliphist.h"
4 #define RC_BUF_SIZE 256
12 STATE_EXPRESSION_START
,
20 GList
*action_list
= NULL
;
23 /* add character to buffer */
24 #define add_to_buff(buf, pos, chr) do { \
27 if (pos + 1 == RC_BUF_SIZE) \
32 #define ismywhite(c) (c == '\t' || c == ' ')
36 * returns config/data file name in user's home
39 rcconfig_get_name(char *append
)
41 static gchar fname
[PATH_MAX
];
44 begin_func("rcconfig_get_name");
46 if (!(home
= g_getenv("HOME")))
49 memset(fname
, 0, PATH_MAX
);
50 snprintf(fname
, PATH_MAX
, "%s/.wmcliphist%s", home
, append
);
59 * appends parsed action to action list
62 action_append(char *expr_buf
, char *action_buf
, char *cmd_buf
)
66 begin_func("action_append");
68 action
= g_new0(ACTION
, 1);
70 if (regcomp(&action
->expression
, expr_buf
,
71 REG_EXTENDED
|REG_ICASE
|REG_NOSUB
) != 0) {
76 if (strcmp(action_buf
, "exec") == 0)
77 action
->action
= ACT_EXEC
;
78 else if (strcmp(action_buf
, "submenu") == 0)
79 action
->action
= ACT_SUBMENU
;
80 else if (strcmp(action_buf
, "ignore") == 0)
81 action
->action
= ACT_IGNORE
;
87 action
->command
= g_strdup(cmd_buf
);
89 action_list
= g_list_append(action_list
, action
);
96 * read and parse rcconfig
99 rcconfig_get(char *fname
)
104 STATE state
= STATE_BEGINING
;
105 char direc_buf
[RC_BUF_SIZE
],
106 expr_buf
[RC_BUF_SIZE
],
107 action_buf
[RC_BUF_SIZE
],
108 cmd_buf
[RC_BUF_SIZE
];
110 int error
= 0, eof
= 0;
115 begin_func("rcconfig_get");
117 close(open(fname
, O_CREAT
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
));
119 if ((f_rc
= open(fname
, O_RDONLY
)) < 0) {
120 fprintf(stderr
, ".wmcliphistrc not found\n");
127 byte_cnt
= read(f_rc
, tmp
, 1024);
128 if (byte_cnt
== -1) {
129 fprintf(stderr
, "cannot read .wmcliphistrc\n");
131 } else if (byte_cnt
< 1024) {
143 state
= STATE_DIRECTIVE
;
145 add_to_buff(direc_buf
, buf_index
, c
);
147 state
= STATE_COMMENT
;
148 else if (ismywhite(c
))
151 state
= STATE_BEGINING
;
157 if (c
== '\n' || c
== '\0')
158 state
= STATE_BEGINING
;
161 case STATE_DIRECTIVE
:
163 if (strcmp(direc_buf
, "action") == 0) {
164 state
= STATE_EXPRESSION_START
;
166 } else if (strcmp(direc_buf
, "menukey") == 0) {
169 } else if (strcmp(direc_buf
, "prev_item_key") == 0) {
172 } else if (strcmp(direc_buf
, "exec_item_key") == 0) {
175 } else if (strcmp(direc_buf
, "keep") == 0) {
178 } else if (strcmp(direc_buf
, "lcolor") == 0) {
181 } else if (strcmp(direc_buf
, "clipboard") == 0) {
184 } else if (strcmp(direc_buf
, "autosave") == 0) {
187 } else if (strcmp(direc_buf
,
188 "confirm_exec") == 0) {
191 } else if (strcmp(direc_buf
, "exec_immediately") == 0) {
194 } else if (strcmp(direc_buf
, "exec_middleclick") == 0) {
197 } else if (strcmp(direc_buf
, "exec_hotkey") == 0) {
200 } else if (strcmp(direc_buf
, "auto_take_up") == 0) {
206 } else if (isalpha(c
) || c
== '_') {
207 add_to_buff(direc_buf
, buf_index
, c
);
214 if (c
== '\n' || ismywhite(c
)) {
215 if (strcmp(direc_buf
, "menukey") == 0) {
216 memset(menukey_str
, 0, 32);
219 } else if (strcmp(direc_buf
, "prev_item_key") == 0) {
220 memset(prev_item_key_str
, 0, 32);
221 strncpy(prev_item_key_str
,
223 } else if (strcmp(direc_buf
, "exec_item_key") == 0) {
224 memset(exec_item_key_str
, 0, 32);
225 strncpy(exec_item_key_str
,
227 } else if (strcmp(direc_buf
, "keep") == 0) {
230 } else if (strcmp(direc_buf
, "lcolor")
232 memset(locked_color_str
, 0, 32);
233 strncpy(locked_color_str
,
235 } else if (strcmp(direc_buf
, "clipboard")
237 memset(clipboard_str
, 0, 32);
238 strncpy(clipboard_str
,
240 } else if (strcmp(direc_buf
, "autosave") == 0) {
243 } else if (strcmp(direc_buf
,
244 "confirm_exec") == 0) {
245 if (strcasecmp(expr_buf
, "yes") == 0) {
248 } else if (strcmp(direc_buf
, "exec_immediately") == 0) {
249 if (strcasecmp(expr_buf
, "no") == 0) {
252 } else if (strcmp(direc_buf
, "exec_middleclick") == 0) {
253 if (strcasecmp(expr_buf
, "no") == 0) {
254 exec_middleclick
= 0;
256 } else if (strcmp(direc_buf
, "exec_hotkey") == 0) {
257 if (strcasecmp(expr_buf
, "no") == 0) {
260 } else if (strcmp(direc_buf
, "auto_take_up") == 0) {
261 if (strcasecmp(expr_buf
, "no") == 0) {
266 } else if (isgraph(c
))
267 add_to_buff(expr_buf
, buf_index
, c
);
272 if (c
== '\n' || c
== '\0') {
274 state
= STATE_BEGINING
;
275 } else if (ismywhite(c
)) {
277 state
= STATE_COMMENT
;
283 case STATE_EXPRESSION_START
:
285 state
= STATE_EXPRESSION
;
290 case STATE_EXPRESSION
:
292 state
= STATE_EXPRESSION_END
;
296 add_to_buff(expr_buf
, buf_index
, c
);
299 case STATE_EXPRESSION_END
:
300 if (c
!= ' ' && c
!= '\t')
302 if (strcmp(direc_buf
, "action") == 0) {
303 state
= STATE_ACTION
;
311 if (c
== ' ' || c
== '\t') {
312 state
= STATE_COMMAND
;
314 } else if (c
== '\0' || c
== '\n') {
315 if (strcmp(action_buf
, "ignore") == 0) {
316 state
= STATE_BEGINING
;
327 } else if (isalpha(c
))
328 add_to_buff(action_buf
, buf_index
, c
);
334 if (c
== '\n' || c
== '\0') {
335 state
= STATE_BEGINING
;
344 add_to_buff(cmd_buf
, buf_index
, c
);
348 if (!error
&& (!eof
|| i
< byte_cnt
))
352 case STATE_DIRECTIVE
:
354 fprintf(stderr
, "Directive is too long "
355 "(line %d)\n", line
);
357 fprintf(stderr
, "Unknown directive "
358 "(line %d)\n", line
);
360 fprintf(stderr
, "Only letters are "
361 "allowed in directive "
366 case STATE_EXPRESSION_START
:
367 case STATE_EXPRESSION
:
369 fprintf(stderr
, "Expression is too long "
370 "(line %d)\n", line
);
372 fprintf(stderr
, "Expression must be "
373 "enclosed with quotes "
374 "\" (line %d)\n", line
);
377 case STATE_EXPRESSION_END
:
378 fprintf(stderr
, "One space/tab and "
379 "action must follow "
381 " (line %d)\n", line
);
386 fprintf(stderr
, "Only letters are "
390 else if (error
== 101)
391 fprintf(stderr
, "Invalid expression "
392 "(line %d)\n", line
);
394 fprintf(stderr
, "Action is too long "
395 "(line %d)\n", line
);
397 fprintf(stderr
, "Invalid action "
398 "(line %d)\n", line
);
403 fprintf(stderr
, "Invalid directive "
404 "(line %d)\n", line
);
406 fprintf(stderr
, "Value is too long "
407 "(line %d)\n", line
);
409 fprintf(stderr
, "Invalid value "
410 "(line %d)\n", line
);
415 fprintf(stderr
, "Invalid expression "
416 "(line %d)\n", line
);
418 fprintf(stderr
, "Command is too long "
419 "(line %d)\n", line
);
421 fprintf(stderr
, "Invalid action "
422 "(line %d)\n", line
);
427 fprintf(stderr
, "Unknown error "
428 "(line %d)\n", line
);
434 /* everything is OK */
456 begin_func("rcconfig_free");
458 list_node
= action_list
;
460 ACTION
*action
= list_node
->data
;
462 g_free(action
->command
);
463 regfree(&action
->expression
);
465 list_node
= list_node
->next
;
467 g_list_free(action_list
);