1 const char actions_rcs[] = "$Id: actions.c,v 1.25 2002/03/24 13:25:43 swa Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/actions.c,v $
6 * Purpose : Declares functions to work with actions files
7 * Functions declared include: FIXME
9 * Copyright : Written by and Copyright (C) 2001 the SourceForge
10 * Privoxy team. http://www.privoxy.org/
12 * Based on the Internet Junkbuster originally written
13 * by and Copyright (C) 1997 Anonymous Coders and
14 * Junkbusters Corporation. http://www.junkbusters.com
16 * This program is free software; you can redistribute it
17 * and/or modify it under the terms of the GNU General
18 * Public License as published by the Free Software
19 * Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * This program is distributed in the hope that it will
23 * be useful, but WITHOUT ANY WARRANTY; without even the
24 * implied warranty of MERCHANTABILITY or FITNESS FOR A
25 * PARTICULAR PURPOSE. See the GNU General Public
26 * License for more details.
28 * The GNU General Public License should be included with
29 * this file. If not, you can view it at
30 * http://www.gnu.org/copyleft/gpl.html
31 * or write to the Free Software Foundation, Inc., 59
32 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 * Revision 1.25 2002/03/24 13:25:43 swa
37 * name change related issues
39 * Revision 1.24 2002/03/16 23:54:06 jongfoster
40 * Adding graceful termination feature, to help look for memory leaks.
41 * If you enable this (which, by design, has to be done by hand
42 * editing config.h) and then go to http://i.j.b/die, then the program
43 * will exit cleanly after the *next* request. It should free all the
44 * memory that was used.
46 * Revision 1.23 2002/03/07 03:46:16 oes
47 * Fixed compiler warnings
49 * Revision 1.22 2002/01/21 00:27:02 jongfoster
50 * Allowing free_action(NULL).
51 * Moving the functions that #include actionlist.h to the end of the file,
52 * because the Visual C++ 97 debugger gets extremely confused if you try
53 * to debug any code that comes after them in the file.
55 * Revision 1.21 2002/01/17 20:54:44 jongfoster
56 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
58 * Revision 1.20 2001/11/22 21:56:49 jongfoster
59 * Making action_spec->flags into an unsigned long rather than just an
61 * Fixing a bug in the display of -add-header and -wafer
63 * Revision 1.19 2001/11/13 00:14:07 jongfoster
64 * Fixing stupid bug now I've figured out what || means.
65 * (It always returns 0 or 1, not one of it's paramaters.)
67 * Revision 1.18 2001/11/07 00:06:06 steudten
68 * Add line number in error output for lineparsing for
71 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
72 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
73 * threads to call select() simultaneously. So, it's time to do a real, live,
74 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
75 * (native). Both versions will work, but using __OS2__ offers multi-threading.
77 * Revision 1.16 2001/10/23 21:30:30 jongfoster
78 * Adding error-checking to selected functions.
80 * Revision 1.15 2001/10/14 21:58:22 jongfoster
81 * Adding support for the CGI-based editor:
82 * - Exported get_actions()
83 * - Added new function free_alias_list()
84 * - Added support for {{settings}} and {{description}} blocks
85 * in the actions file. They are currently ignored.
86 * - Added restriction to only one {{alias}} block which must appear
87 * first in the file, to simplify the editor's rewriting rules.
88 * - Note that load_actions_file() is no longer used by the CGI-based
89 * editor, but some of the other routines in this file are.
91 * Revision 1.14 2001/09/22 16:36:59 jongfoster
92 * Removing unused parameter fs from read_config_line()
94 * Revision 1.13 2001/09/16 15:47:37 jongfoster
95 * First version of CGI-based edit interface. This is very much a
96 * work-in-progress, and you can't actually use it to edit anything
97 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
100 * Revision 1.12 2001/09/16 13:21:27 jongfoster
101 * Changes to use new list functions.
103 * Revision 1.11 2001/09/14 00:17:32 jongfoster
104 * Tidying up memory allocation. New function init_action().
106 * Revision 1.10 2001/09/10 10:14:34 oes
107 * Removing unused variable
109 * Revision 1.9 2001/07/30 22:08:36 jongfoster
110 * Tidying up #defines:
111 * - All feature #defines are now of the form FEATURE_xxx
112 * - Permanently turned off WIN_GUI_EDIT
113 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
115 * Revision 1.8 2001/06/29 13:19:52 oes
116 * Removed logentry from cancelled commit
118 * Revision 1.7 2001/06/09 10:55:28 jongfoster
119 * Changing BUFSIZ ==> BUFFER_SIZE
121 * Revision 1.6 2001/06/07 23:04:34 jongfoster
122 * Made get_actions() static.
124 * Revision 1.5 2001/06/03 19:11:48 oes
125 * adapted to new enlist_unique arg format
127 * Revision 1.4 2001/06/01 20:03:42 jongfoster
128 * Better memory management - current_action->strings[] now
129 * contains copies of the strings, not the original.
131 * Revision 1.3 2001/06/01 18:49:17 jongfoster
132 * Replaced "list_share" with "list" - the tiny memory gain was not
133 * worth the extra complexity.
135 * Revision 1.2 2001/05/31 21:40:00 jongfoster
136 * Removing some commented out, obsolete blocks of code.
138 * Revision 1.1 2001/05/31 21:16:46 jongfoster
139 * Moved functions to process the action list into this new file.
142 *********************************************************************/
156 #include "miscutil.h"
159 #ifdef FEATURE_CGI_EDIT_ACTIONS
161 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
162 #include "urlmatch.h"
164 const char actions_h_rcs[] = ACTIONS_H_VERSION;
168 * We need the main list of options.
170 * First, we need a way to tell between boolean, string, and multi-string
171 * options. For string and multistring options, we also need to be
172 * able to tell the difference between a "+" and a "-". (For bools,
173 * the "+"/"-" information is encoded in "add" and "mask"). So we use
174 * an enumerated type (well, the preprocessor equivalent). Here are
177 #define AV_NONE 0 /* +opt -opt */
178 #define AV_ADD_STRING 1 /* +stropt{string} */
179 #define AV_REM_STRING 2 /* -stropt */
180 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
181 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
184 * We need a structure to hold the name, flag changes,
185 * type, and string index.
190 unsigned mask; /* a bit set to "0" = remove action */
191 unsigned add; /* a bit set to "1" = add action */
192 int takes_value; /* an AV_... constant */
193 int index; /* index into strings[] or multi[] */
197 * And with those building blocks in place, here's the array.
199 static const struct action_name action_names[] =
202 * Well actually there's no data here - it's in actionlist.h
203 * This keeps it together to make it easy to change.
205 * Here's the macros used to format it:
207 #define DEFINE_ACTION_MULTI(name,index) \
208 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
209 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
210 #define DEFINE_ACTION_STRING(name,flag,index) \
211 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
212 { "-" name, ~flag, 0, AV_REM_STRING, index },
213 #define DEFINE_ACTION_BOOL(name,flag) \
214 { "+" name, ACTION_MASK_ALL, flag }, \
215 { "-" name, ~flag, 0 },
216 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
218 #include "actionlist.h"
220 #undef DEFINE_ACTION_MULTI
221 #undef DEFINE_ACTION_STRING
222 #undef DEFINE_ACTION_BOOL
223 #undef DEFINE_ACTION_ALIAS
225 { NULL, 0, 0 } /* End marker */
229 /*********************************************************************
231 * Function : merge_actions
233 * Description : Merge two actions together.
234 * Similar to "cur_action += new_action".
237 * 1 : cur_action = Current actions, to modify.
238 * 2 : new_action = Action to add.
240 * Returns : JB_ERR_OK or JB_ERR_MEMORY
242 *********************************************************************/
243 jb_err merge_actions (struct action_spec *dest,
244 const struct action_spec *src)
249 dest->mask &= src->mask;
250 dest->add &= src->mask;
251 dest->add |= src->add;
253 for (i = 0; i < ACTION_STRING_COUNT; i++)
255 char * str = src->string[i];
258 freez(dest->string[i]);
259 dest->string[i] = strdup(str);
260 if (NULL == dest->string[i])
262 return JB_ERR_MEMORY;
267 for (i = 0; i < ACTION_MULTI_COUNT; i++)
269 if (src->multi_remove_all[i])
271 /* Remove everything from dest */
272 list_remove_all(dest->multi_remove[i]);
273 dest->multi_remove_all[i] = 1;
275 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
277 else if (dest->multi_remove_all[i])
280 * dest already removes everything, so we only need to worry
283 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
284 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
288 /* No "remove all"s to worry about. */
289 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
290 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
291 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
304 /*********************************************************************
306 * Function : copy_action
308 * Description : Copy an action_specs.
309 * Similar to "cur_action = new_action".
310 * Note that dest better not contain valid data
311 * - it's overwritten, not freed.
314 * 1 : dest = Destination of copy.
315 * 2 : src = Source for copy.
319 *********************************************************************/
320 jb_err copy_action (struct action_spec *dest,
321 const struct action_spec *src)
324 jb_err err = JB_ERR_OK;
326 memset(dest, '\0', sizeof(*dest));
328 dest->mask = src->mask;
329 dest->add = src->add;
331 for (i = 0; i < ACTION_STRING_COUNT; i++)
333 char * str = src->string[i];
339 return JB_ERR_MEMORY;
341 dest->string[i] = str;
345 for (i = 0; i < ACTION_MULTI_COUNT; i++)
347 dest->multi_remove_all[i] = src->multi_remove_all[i];
348 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
353 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
363 /*********************************************************************
365 * Function : free_action
367 * Description : Destroy an action_spec. Frees memory used by it,
368 * except for the memory used by the struct action_spec
372 * 1 : src = Source to free.
376 *********************************************************************/
377 void free_action (struct action_spec *src)
386 for (i = 0; i < ACTION_STRING_COUNT; i++)
388 freez(src->string[i]);
391 for (i = 0; i < ACTION_MULTI_COUNT; i++)
393 destroy_list(src->multi_remove[i]);
394 destroy_list(src->multi_add[i]);
397 memset(src, '\0', sizeof(*src));
401 /*********************************************************************
403 * Function : get_action_token
405 * Description : Parses a line for the first action.
406 * Modifies it's input array, doesn't allocate memory.
408 * *line=" +abc{def} -ghi "
415 * 1 : line = [in] The line containing the action.
416 * [out] Start of next action on line, or
417 * NULL if we reached the end of line before
418 * we found an action.
419 * 2 : name = [out] Start of action name, null
420 * terminated. NULL on EOL
421 * 3 : value = [out] Start of action value, null
422 * terminated. NULL if none or EOL.
424 * Returns : JB_ERR_OK => Ok
425 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
427 *********************************************************************/
428 jb_err get_action_token(char **line, char **name, char **value)
433 /* set default returns */
438 /* Eat any leading whitespace */
439 while ((*str == ' ') || (*str == '\t'))
451 /* null name, just value is prohibited */
458 while (((ch = *str) != '\0') &&
459 (ch != ' ') && (ch != '\t') && (ch != '{'))
463 /* error, '}' without '{' */
475 /* EOL - be careful not to run off buffer */
480 /* More to parse next time. */
489 str = strchr(str, '}');
507 /*********************************************************************
509 * Function : get_actions
511 * Description : Parses a list of actions.
514 * 1 : line = The string containing the actions.
515 * Will be written to by this function.
516 * 2 : aliases = Custom alias list, or NULL for none.
517 * 3 : cur_action = Where to store the action. Caller
520 * Returns : JB_ERR_OK => Ok
521 * JB_ERR_PARSE => Parse error (line was trashed anyway)
522 * nonzero => Out of memory (line was trashed anyway)
524 *********************************************************************/
525 jb_err get_actions(char *line,
526 struct action_alias * alias_list,
527 struct action_spec *cur_action)
530 init_action(cur_action);
531 cur_action->mask = ACTION_MASK_ALL;
535 char * option = NULL;
538 err = get_action_token(&line, &option, &value);
546 /* handle option in 'option' */
548 /* Check for standard action name */
549 const struct action_name * action = action_names;
551 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
555 if (action->name != NULL)
558 cur_action->mask &= action->mask;
559 cur_action->add &= action->mask;
560 cur_action->add |= action->add;
562 switch (action->takes_value)
565 /* ignore any option. */
569 /* add single string. */
571 if ((value == NULL) || (*value == '\0'))
575 /* FIXME: should validate option string here */
576 freez (cur_action->string[action->index]);
577 cur_action->string[action->index] = strdup(value);
578 if (NULL == cur_action->string[action->index])
580 return JB_ERR_MEMORY;
586 /* remove single string. */
588 freez (cur_action->string[action->index]);
593 /* append multi string. */
595 struct list * remove_p = cur_action->multi_remove[action->index];
596 struct list * add_p = cur_action->multi_add[action->index];
598 if ((value == NULL) || (*value == '\0'))
603 list_remove_item(remove_p, value);
604 err = enlist_unique(add_p, value, 0);
613 /* remove multi string. */
615 struct list * remove_p = cur_action->multi_remove[action->index];
616 struct list * add_p = cur_action->multi_add[action->index];
618 if ( (value == NULL) || (*value == '\0')
619 || ((*value == '*') && (value[1] == '\0')) )
622 * no option, or option == "*".
626 list_remove_all(remove_p);
627 list_remove_all(add_p);
628 cur_action->multi_remove_all[action->index] = 1;
632 /* Valid option - remove only 1 option */
634 if ( !cur_action->multi_remove_all[action->index] )
636 /* there isn't a catch-all in the remove list already */
637 err = enlist_unique(remove_p, value, 0);
643 list_remove_item(add_p, value);
648 /* Shouldn't get here unless there's memory corruption. */
655 /* try user aliases. */
656 const struct action_alias * alias = alias_list;
658 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
665 merge_actions(cur_action, alias->action);
669 /* Bad action name */
680 /*********************************************************************
682 * Function : init_current_action
684 * Description : Zero out an action.
687 * 1 : dest = An uninitialized current_action_spec.
691 *********************************************************************/
692 void init_current_action (struct current_action_spec *dest)
694 memset(dest, '\0', sizeof(*dest));
696 dest->flags = ACTION_MOST_COMPATIBLE;
700 /*********************************************************************
702 * Function : init_action
704 * Description : Zero out an action.
707 * 1 : dest = An uninitialized action_spec.
711 *********************************************************************/
712 void init_action (struct action_spec *dest)
714 memset(dest, '\0', sizeof(*dest));
718 /*********************************************************************
720 * Function : merge_current_action
722 * Description : Merge two actions together.
723 * Similar to "dest += src".
724 * Differences between this and merge_actions()
725 * is that this one doesn't allocate memory for
726 * strings (so "src" better be in memory for at least
727 * as long as "dest" is, and you'd better free
728 * "dest" using "free_current_action").
729 * Also, there is no mask or remove lists in dest.
730 * (If we're applying it to a URL, we don't need them)
733 * 1 : dest = Current actions, to modify.
734 * 2 : src = Action to add.
736 * Returns 0 : no error
737 * !=0 : error, probably JB_ERR_MEMORY.
739 *********************************************************************/
740 jb_err merge_current_action (struct current_action_spec *dest,
741 const struct action_spec *src)
744 jb_err err = JB_ERR_OK;
746 dest->flags &= src->mask;
747 dest->flags |= src->add;
749 for (i = 0; i < ACTION_STRING_COUNT; i++)
751 char * str = src->string[i];
757 return JB_ERR_MEMORY;
759 freez(dest->string[i]);
760 dest->string[i] = str;
764 for (i = 0; i < ACTION_MULTI_COUNT; i++)
766 if (src->multi_remove_all[i])
768 /* Remove everything from dest, then add src->multi_add */
769 err = list_duplicate(dest->multi[i], src->multi_add[i]);
777 list_remove_list(dest->multi[i], src->multi_remove[i]);
778 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
789 /*********************************************************************
791 * Function : free_current_action
793 * Description : Free memory used by a current_action_spec.
794 * Does not free the current_action_spec itself.
797 * 1 : src = Source to free.
801 *********************************************************************/
802 void free_current_action (struct current_action_spec *src)
806 for (i = 0; i < ACTION_STRING_COUNT; i++)
808 freez(src->string[i]);
811 for (i = 0; i < ACTION_MULTI_COUNT; i++)
813 destroy_list(src->multi[i]);
816 memset(src, '\0', sizeof(*src));
820 static struct file_list *current_actions_file = NULL;
823 #ifdef FEATURE_GRACEFUL_TERMINATION
824 /*********************************************************************
826 * Function : unload_current_actions_file
828 * Description : Unloads current actions file - reset to state at
829 * beginning of program.
835 *********************************************************************/
836 void unload_current_actions_file(void)
838 if (current_actions_file)
840 current_actions_file->unloader = unload_actions_file;
841 current_actions_file = NULL;
844 #endif /* FEATURE_GRACEFUL_TERMINATION */
847 /*********************************************************************
849 * Function : unload_actions_file
851 * Description : Unloads an actions module.
854 * 1 : file_data = the data structure associated with the
859 *********************************************************************/
860 void unload_actions_file(void *file_data)
862 struct url_actions * next;
863 struct url_actions * cur = (struct url_actions *)file_data;
867 free_url_spec(cur->url);
868 free_action(cur->action);
876 /*********************************************************************
878 * Function : free_alias_list
880 * Description : Free memory used by a list of aliases.
883 * 1 : alias_list = Linked list to free.
887 *********************************************************************/
888 void free_alias_list(struct action_alias *alias_list)
890 while (alias_list != NULL)
892 struct action_alias * next = alias_list->next;
893 alias_list->next = NULL;
894 freez(alias_list->name);
895 free_action(alias_list->action);
902 /*********************************************************************
904 * Function : load_actions_file
906 * Description : Read and parse a action file and add to files
910 * 1 : csp = Current client state (buffers, headers, etc...)
912 * Returns : 0 => Ok, everything else is an error.
914 *********************************************************************/
915 int load_actions_file(struct client_state *csp)
920 * Note: Keep these in the order they occur in the file, they are
921 * sometimes tested with <=
923 #define MODE_START_OF_FILE 1
924 #define MODE_SETTINGS 2
925 #define MODE_DESCRIPTION 3
927 #define MODE_ACTIONS 5
929 int mode = MODE_START_OF_FILE;
932 struct url_actions *last_perm;
933 struct url_actions *perm;
934 char buf[BUFFER_SIZE];
935 struct file_list *fs;
936 struct action_spec * cur_action = NULL;
937 int cur_action_used = 0;
938 struct action_alias * alias_list = NULL;
939 unsigned long linenum = 0;
941 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
943 /* No need to load */
946 csp->actions_list = current_actions_file;
952 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
953 csp->config->actions_file);
954 return 1; /* never get here */
957 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
958 if (last_perm == NULL)
960 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
961 csp->config->actions_file);
962 return 1; /* never get here */
965 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
967 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
968 csp->config->actions_file);
969 return 1; /* never get here */
972 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
976 /* It's a header block */
979 /* It's {{settings}} or {{alias}} */
980 int len = strlen(buf);
981 char * start = buf + 2;
982 char * end = buf + len - 1;
983 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
987 log_error(LOG_LEVEL_FATAL,
988 "can't load actions file '%s': invalid line (%lu): %s",
989 csp->config->actions_file, linenum, buf);
990 return 1; /* never get here */
993 /* Trim leading and trailing whitespace. */
1001 log_error(LOG_LEVEL_FATAL,
1002 "can't load actions file '%s': invalid line (%lu): {{ }}",
1003 csp->config->actions_file, linenum);
1004 return 1; /* never get here */
1008 * An actionsfile can optionally contain the following blocks.
1009 * They *MUST* be in this order, to simplify processing:
1015 * ...free text, format TBD, but no line may start with a '{'...
1020 * The actual actions must be *after* these special blocks.
1021 * None of these special blocks may be repeated.
1024 if (0 == strcmpic(start, "settings"))
1026 /* it's a {{settings}} block */
1027 if (mode >= MODE_SETTINGS)
1029 /* {{settings}} must be first thing in file and must only
1033 log_error(LOG_LEVEL_FATAL,
1034 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1035 csp->config->actions_file, linenum);
1037 mode = MODE_SETTINGS;
1039 else if (0 == strcmpic(start, "description"))
1041 /* it's a {{description}} block */
1042 if (mode >= MODE_DESCRIPTION)
1044 /* {{description}} is a singleton and only {{settings}} may proceed it
1047 log_error(LOG_LEVEL_FATAL,
1048 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1049 csp->config->actions_file, linenum);
1051 mode = MODE_DESCRIPTION;
1053 else if (0 == strcmpic(start, "alias"))
1055 /* it's an {{alias}} block */
1056 if (mode >= MODE_ALIAS)
1058 /* {{alias}} must be first thing in file, possibly after
1059 * {{settings}} and {{description}}
1061 * {{alias}} must only appear once.
1063 * Note that these are new restrictions introduced in
1064 * v2.9.10 in order to make actionsfile editing simpler.
1065 * (Otherwise, reordering actionsfile entries without
1066 * completely rewriting the file becomes non-trivial)
1069 log_error(LOG_LEVEL_FATAL,
1070 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1071 csp->config->actions_file, linenum);
1077 /* invalid {{something}} block */
1079 log_error(LOG_LEVEL_FATAL,
1080 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1081 csp->config->actions_file, linenum, start);
1082 return 1; /* never get here */
1087 /* It's an actions block */
1089 char actions_buf[BUFFER_SIZE];
1093 mode = MODE_ACTIONS;
1095 /* free old action */
1098 if (!cur_action_used)
1100 free_action(cur_action);
1105 cur_action_used = 0;
1106 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1107 if (cur_action == NULL)
1110 log_error(LOG_LEVEL_FATAL,
1111 "can't load actions file '%s': out of memory",
1112 csp->config->actions_file);
1113 return 1; /* never get here */
1115 init_action(cur_action);
1118 strcpy(actions_buf, buf + 1);
1120 /* check we have a trailing } and then trim it */
1121 end = actions_buf + strlen(actions_buf) - 1;
1126 log_error(LOG_LEVEL_FATAL,
1127 "can't load actions file '%s': invalid line (%lu): %s",
1128 csp->config->actions_file, linenum, buf);
1129 return 1; /* never get here */
1133 /* trim any whitespace immediately inside {} */
1136 if (get_actions(actions_buf, alias_list, cur_action))
1140 log_error(LOG_LEVEL_FATAL,
1141 "can't load actions file '%s': invalid line (%lu): %s",
1142 csp->config->actions_file, linenum, buf);
1143 return 1; /* never get here */
1147 else if (mode == MODE_SETTINGS)
1150 * Part of the {{settings}} block.
1151 * Ignore for now, but we may want to read & check permissions
1152 * when we go multi-user.
1155 else if (mode == MODE_DESCRIPTION)
1158 * Part of the {{description}} block.
1162 else if (mode == MODE_ALIAS)
1167 char actions_buf[BUFFER_SIZE];
1168 struct action_alias * new_alias;
1170 char * start = strchr(buf, '=');
1173 if ((start == NULL) || (start == buf))
1175 log_error(LOG_LEVEL_FATAL,
1176 "can't load actions file '%s': invalid alias line (%lu): %s",
1177 csp->config->actions_file, linenum, buf);
1178 return 1; /* never get here */
1181 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1184 log_error(LOG_LEVEL_FATAL,
1185 "can't load actions file '%s': out of memory!",
1186 csp->config->actions_file);
1187 return 1; /* never get here */
1190 /* Eat any the whitespace before the '=' */
1192 while ((*end == ' ') || (*end == '\t'))
1195 * we already know we must have at least 1 non-ws char
1196 * at start of buf - no need to check
1202 /* Eat any the whitespace after the '=' */
1204 while ((*start == ' ') || (*start == '\t'))
1210 log_error(LOG_LEVEL_FATAL,
1211 "can't load actions file '%s': invalid alias line (%lu): %s",
1212 csp->config->actions_file, linenum, buf);
1213 return 1; /* never get here */
1216 new_alias->name = strdup(buf);
1218 strcpy(actions_buf, start);
1220 if (get_actions(actions_buf, alias_list, new_alias->action))
1224 log_error(LOG_LEVEL_FATAL,
1225 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1226 csp->config->actions_file, linenum, buf, start);
1227 return 1; /* never get here */
1231 new_alias->next = alias_list;
1232 alias_list = new_alias;
1234 else if (mode == MODE_ACTIONS)
1236 /* it's a URL pattern */
1238 /* allocate a new node */
1239 if ((perm = zalloc(sizeof(*perm))) == NULL)
1242 log_error(LOG_LEVEL_FATAL,
1243 "can't load actions file '%s': out of memory!",
1244 csp->config->actions_file);
1245 return 1; /* never get here */
1249 copy_action (perm->action, cur_action);
1251 /* Save the URL pattern */
1252 if (create_url_spec(perm->url, buf))
1255 log_error(LOG_LEVEL_FATAL,
1256 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1257 csp->config->actions_file, linenum, buf);
1258 return 1; /* never get here */
1261 /* add it to the list */
1262 last_perm->next = perm;
1265 else if (mode == MODE_START_OF_FILE)
1267 /* oops - please have a {} line as 1st line in file. */
1269 log_error(LOG_LEVEL_FATAL,
1270 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1271 csp->config->actions_file, linenum, buf);
1272 return 1; /* never get here */
1276 /* How did we get here? This is impossible! */
1278 log_error(LOG_LEVEL_FATAL,
1279 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1280 csp->config->actions_file, mode);
1281 return 1; /* never get here */
1287 free_action(cur_action);
1289 free_alias_list(alias_list);
1291 /* the old one is now obsolete */
1292 if (current_actions_file)
1294 current_actions_file->unloader = unload_actions_file;
1297 fs->next = files->next;
1299 current_actions_file = fs;
1303 csp->actions_list = fs;
1311 /*********************************************************************
1313 * Function : actions_to_text
1315 * Description : Converts a actionsfile entry from numeric form
1316 * ("mask" and "add") to text.
1319 * 1 : mask = As from struct url_actions
1320 * 2 : add = As from struct url_actions
1322 * Returns : A string. Caller must free it.
1323 * NULL on out-of-memory error.
1325 *********************************************************************/
1326 char * actions_to_text(struct action_spec *action)
1328 unsigned mask = action->mask;
1329 unsigned add = action->add;
1330 char * result = strdup("");
1331 struct list_entry * lst;
1333 /* sanity - prevents "-feature +feature" */
1337 #define DEFINE_ACTION_BOOL(__name, __bit) \
1338 if (!(mask & __bit)) \
1340 string_append(&result, " -" __name); \
1342 else if (add & __bit) \
1344 string_append(&result, " +" __name); \
1347 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1348 if (!(mask & __bit)) \
1350 string_append(&result, " -" __name); \
1352 else if (add & __bit) \
1354 string_append(&result, " +" __name "{"); \
1355 string_append(&result, action->string[__index]); \
1356 string_append(&result, "}"); \
1359 #define DEFINE_ACTION_MULTI(__name, __index) \
1360 if (action->multi_remove_all[__index]) \
1362 string_append(&result, " -" __name); \
1366 lst = action->multi_remove[__index]->first; \
1369 string_append(&result, " -" __name "{"); \
1370 string_append(&result, lst->str); \
1371 string_append(&result, "}"); \
1375 lst = action->multi_add[__index]->first; \
1378 string_append(&result, " +" __name "{"); \
1379 string_append(&result, lst->str); \
1380 string_append(&result, "}"); \
1384 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1386 #include "actionlist.h"
1388 #undef DEFINE_ACTION_MULTI
1389 #undef DEFINE_ACTION_STRING
1390 #undef DEFINE_ACTION_BOOL
1391 #undef DEFINE_ACTION_ALIAS
1397 #ifdef FEATURE_CGI_EDIT_ACTIONS
1398 /*********************************************************************
1400 * Function : actions_to_html
1402 * Description : Converts a actionsfile entry from numeric form
1403 * ("mask" and "add") to a <br>-seperated HTML string.
1406 * 1 : mask = As from struct url_actions
1407 * 2 : add = As from struct url_actions
1409 * Returns : A string. Caller must free it.
1410 * NULL on out-of-memory error.
1412 *********************************************************************/
1413 char * actions_to_html(struct action_spec *action)
1415 unsigned mask = action->mask;
1416 unsigned add = action->add;
1417 char * result = strdup("");
1419 struct list_entry * lst;
1421 /* sanity - prevents "-feature +feature" */
1425 #define DEFINE_ACTION_BOOL(__name, __bit) \
1426 if (!(mask & __bit)) \
1428 string_append(&result, "\n<br>-" __name); \
1430 else if (add & __bit) \
1432 string_append(&result, "\n<br>+" __name); \
1435 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1436 if (!(mask & __bit)) \
1438 string_append(&result, "\n<br>-" __name); \
1440 else if (add & __bit) \
1442 string_append(&result, "\n<br>+" __name "{"); \
1443 if (NULL == result) \
1447 enc_str = html_encode(action->string[__index]);\
1448 if (NULL == enc_str) \
1453 string_append(&result, enc_str); \
1455 string_append(&result, "}"); \
1458 #define DEFINE_ACTION_MULTI(__name, __index) \
1459 if (action->multi_remove_all[__index]) \
1461 string_append(&result, "\n<br>-" __name); \
1465 lst = action->multi_remove[__index]->first; \
1468 string_append(&result, "\n<br>-" __name "{");\
1469 if (NULL == result) \
1473 enc_str = html_encode(lst->str); \
1474 if (NULL == enc_str) \
1479 string_append(&result, enc_str); \
1481 string_append(&result, "}"); \
1485 lst = action->multi_add[__index]->first; \
1488 string_append(&result, "\n<br>+" __name "{"); \
1489 if (NULL == result) \
1493 enc_str = html_encode(lst->str); \
1494 if (NULL == enc_str) \
1499 string_append(&result, enc_str); \
1501 string_append(&result, "}"); \
1505 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1507 #include "actionlist.h"
1509 #undef DEFINE_ACTION_MULTI
1510 #undef DEFINE_ACTION_STRING
1511 #undef DEFINE_ACTION_BOOL
1512 #undef DEFINE_ACTION_ALIAS
1514 /* trim leading <br> */
1515 if (result && *result)
1518 result = strdup(result + 5);
1524 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1527 /*********************************************************************
1529 * Function : current_actions_to_text
1531 * Description : Converts a actionsfile entry to text.
1534 * 1 : action = Action
1536 * Returns : A string. Caller must free it.
1537 * NULL on out-of-memory error.
1539 *********************************************************************/
1540 char * current_action_to_text(struct current_action_spec *action)
1542 unsigned long flags = action->flags;
1543 char * result = strdup("");
1544 struct list_entry * lst;
1546 #define DEFINE_ACTION_BOOL(__name, __bit) \
1547 if (flags & __bit) \
1549 string_append(&result, " +" __name); \
1553 string_append(&result, " -" __name); \
1556 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1557 if (flags & __bit) \
1559 string_append(&result, " +" __name "{"); \
1560 string_append(&result, action->string[__index]); \
1561 string_append(&result, "}"); \
1565 string_append(&result, " -" __name); \
1568 #define DEFINE_ACTION_MULTI(__name, __index) \
1569 lst = action->multi[__index]->first; \
1572 string_append(&result, " -" __name); \
1578 string_append(&result, " +" __name "{"); \
1579 string_append(&result, lst->str); \
1580 string_append(&result, "}"); \
1585 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1587 #include "actionlist.h"
1589 #undef DEFINE_ACTION_MULTI
1590 #undef DEFINE_ACTION_STRING
1591 #undef DEFINE_ACTION_BOOL
1592 #undef DEFINE_ACTION_ALIAS