1 const char actions_rcs[] = "$Id: actions.c,v 1.26 2002/03/26 22:29:54 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.26 2002/03/26 22:29:54 swa
37 * we have a new homepage!
39 * Revision 1.25 2002/03/24 13:25:43 swa
40 * name change related issues
42 * Revision 1.24 2002/03/16 23:54:06 jongfoster
43 * Adding graceful termination feature, to help look for memory leaks.
44 * If you enable this (which, by design, has to be done by hand
45 * editing config.h) and then go to http://i.j.b/die, then the program
46 * will exit cleanly after the *next* request. It should free all the
47 * memory that was used.
49 * Revision 1.23 2002/03/07 03:46:16 oes
50 * Fixed compiler warnings
52 * Revision 1.22 2002/01/21 00:27:02 jongfoster
53 * Allowing free_action(NULL).
54 * Moving the functions that #include actionlist.h to the end of the file,
55 * because the Visual C++ 97 debugger gets extremely confused if you try
56 * to debug any code that comes after them in the file.
58 * Revision 1.21 2002/01/17 20:54:44 jongfoster
59 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
61 * Revision 1.20 2001/11/22 21:56:49 jongfoster
62 * Making action_spec->flags into an unsigned long rather than just an
64 * Fixing a bug in the display of -add-header and -wafer
66 * Revision 1.19 2001/11/13 00:14:07 jongfoster
67 * Fixing stupid bug now I've figured out what || means.
68 * (It always returns 0 or 1, not one of it's paramaters.)
70 * Revision 1.18 2001/11/07 00:06:06 steudten
71 * Add line number in error output for lineparsing for
74 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
75 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
76 * threads to call select() simultaneously. So, it's time to do a real, live,
77 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
78 * (native). Both versions will work, but using __OS2__ offers multi-threading.
80 * Revision 1.16 2001/10/23 21:30:30 jongfoster
81 * Adding error-checking to selected functions.
83 * Revision 1.15 2001/10/14 21:58:22 jongfoster
84 * Adding support for the CGI-based editor:
85 * - Exported get_actions()
86 * - Added new function free_alias_list()
87 * - Added support for {{settings}} and {{description}} blocks
88 * in the actions file. They are currently ignored.
89 * - Added restriction to only one {{alias}} block which must appear
90 * first in the file, to simplify the editor's rewriting rules.
91 * - Note that load_actions_file() is no longer used by the CGI-based
92 * editor, but some of the other routines in this file are.
94 * Revision 1.14 2001/09/22 16:36:59 jongfoster
95 * Removing unused parameter fs from read_config_line()
97 * Revision 1.13 2001/09/16 15:47:37 jongfoster
98 * First version of CGI-based edit interface. This is very much a
99 * work-in-progress, and you can't actually use it to edit anything
100 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
101 * to have any effect.
103 * Revision 1.12 2001/09/16 13:21:27 jongfoster
104 * Changes to use new list functions.
106 * Revision 1.11 2001/09/14 00:17:32 jongfoster
107 * Tidying up memory allocation. New function init_action().
109 * Revision 1.10 2001/09/10 10:14:34 oes
110 * Removing unused variable
112 * Revision 1.9 2001/07/30 22:08:36 jongfoster
113 * Tidying up #defines:
114 * - All feature #defines are now of the form FEATURE_xxx
115 * - Permanently turned off WIN_GUI_EDIT
116 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
118 * Revision 1.8 2001/06/29 13:19:52 oes
119 * Removed logentry from cancelled commit
121 * Revision 1.7 2001/06/09 10:55:28 jongfoster
122 * Changing BUFSIZ ==> BUFFER_SIZE
124 * Revision 1.6 2001/06/07 23:04:34 jongfoster
125 * Made get_actions() static.
127 * Revision 1.5 2001/06/03 19:11:48 oes
128 * adapted to new enlist_unique arg format
130 * Revision 1.4 2001/06/01 20:03:42 jongfoster
131 * Better memory management - current_action->strings[] now
132 * contains copies of the strings, not the original.
134 * Revision 1.3 2001/06/01 18:49:17 jongfoster
135 * Replaced "list_share" with "list" - the tiny memory gain was not
136 * worth the extra complexity.
138 * Revision 1.2 2001/05/31 21:40:00 jongfoster
139 * Removing some commented out, obsolete blocks of code.
141 * Revision 1.1 2001/05/31 21:16:46 jongfoster
142 * Moved functions to process the action list into this new file.
145 *********************************************************************/
159 #include "miscutil.h"
162 #ifdef FEATURE_CGI_EDIT_ACTIONS
164 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
165 #include "urlmatch.h"
167 const char actions_h_rcs[] = ACTIONS_H_VERSION;
171 * We need the main list of options.
173 * First, we need a way to tell between boolean, string, and multi-string
174 * options. For string and multistring options, we also need to be
175 * able to tell the difference between a "+" and a "-". (For bools,
176 * the "+"/"-" information is encoded in "add" and "mask"). So we use
177 * an enumerated type (well, the preprocessor equivalent). Here are
180 #define AV_NONE 0 /* +opt -opt */
181 #define AV_ADD_STRING 1 /* +stropt{string} */
182 #define AV_REM_STRING 2 /* -stropt */
183 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
184 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
187 * We need a structure to hold the name, flag changes,
188 * type, and string index.
193 unsigned mask; /* a bit set to "0" = remove action */
194 unsigned add; /* a bit set to "1" = add action */
195 int takes_value; /* an AV_... constant */
196 int index; /* index into strings[] or multi[] */
200 * And with those building blocks in place, here's the array.
202 static const struct action_name action_names[] =
205 * Well actually there's no data here - it's in actionlist.h
206 * This keeps it together to make it easy to change.
208 * Here's the macros used to format it:
210 #define DEFINE_ACTION_MULTI(name,index) \
211 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
212 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
213 #define DEFINE_ACTION_STRING(name,flag,index) \
214 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
215 { "-" name, ~flag, 0, AV_REM_STRING, index },
216 #define DEFINE_ACTION_BOOL(name,flag) \
217 { "+" name, ACTION_MASK_ALL, flag }, \
218 { "-" name, ~flag, 0 },
219 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
221 #include "actionlist.h"
223 #undef DEFINE_ACTION_MULTI
224 #undef DEFINE_ACTION_STRING
225 #undef DEFINE_ACTION_BOOL
226 #undef DEFINE_ACTION_ALIAS
228 { NULL, 0, 0 } /* End marker */
232 static int load_one_actions_file(struct client_state *csp, int fileid);
235 /*********************************************************************
237 * Function : merge_actions
239 * Description : Merge two actions together.
240 * Similar to "cur_action += new_action".
243 * 1 : cur_action = Current actions, to modify.
244 * 2 : new_action = Action to add.
246 * Returns : JB_ERR_OK or JB_ERR_MEMORY
248 *********************************************************************/
249 jb_err merge_actions (struct action_spec *dest,
250 const struct action_spec *src)
255 dest->mask &= src->mask;
256 dest->add &= src->mask;
257 dest->add |= src->add;
259 for (i = 0; i < ACTION_STRING_COUNT; i++)
261 char * str = src->string[i];
264 freez(dest->string[i]);
265 dest->string[i] = strdup(str);
266 if (NULL == dest->string[i])
268 return JB_ERR_MEMORY;
273 for (i = 0; i < ACTION_MULTI_COUNT; i++)
275 if (src->multi_remove_all[i])
277 /* Remove everything from dest */
278 list_remove_all(dest->multi_remove[i]);
279 dest->multi_remove_all[i] = 1;
281 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
283 else if (dest->multi_remove_all[i])
286 * dest already removes everything, so we only need to worry
289 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
290 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
294 /* No "remove all"s to worry about. */
295 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
296 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
297 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
310 /*********************************************************************
312 * Function : copy_action
314 * Description : Copy an action_specs.
315 * Similar to "cur_action = new_action".
318 * 1 : dest = Destination of copy.
319 * 2 : src = Source for copy.
323 *********************************************************************/
324 jb_err copy_action (struct action_spec *dest,
325 const struct action_spec *src)
328 jb_err err = JB_ERR_OK;
331 memset(dest, '\0', sizeof(*dest));
333 dest->mask = src->mask;
334 dest->add = src->add;
336 for (i = 0; i < ACTION_STRING_COUNT; i++)
338 char * str = src->string[i];
344 return JB_ERR_MEMORY;
346 dest->string[i] = str;
350 for (i = 0; i < ACTION_MULTI_COUNT; i++)
352 dest->multi_remove_all[i] = src->multi_remove_all[i];
353 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
358 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
368 /*********************************************************************
370 * Function : free_action
372 * Description : Destroy an action_spec. Frees memory used by it,
373 * except for the memory used by the struct action_spec
377 * 1 : src = Source to free.
381 *********************************************************************/
382 void free_action (struct action_spec *src)
391 for (i = 0; i < ACTION_STRING_COUNT; i++)
393 freez(src->string[i]);
396 for (i = 0; i < ACTION_MULTI_COUNT; i++)
398 destroy_list(src->multi_remove[i]);
399 destroy_list(src->multi_add[i]);
402 memset(src, '\0', sizeof(*src));
406 /*********************************************************************
408 * Function : get_action_token
410 * Description : Parses a line for the first action.
411 * Modifies it's input array, doesn't allocate memory.
413 * *line=" +abc{def} -ghi "
420 * 1 : line = [in] The line containing the action.
421 * [out] Start of next action on line, or
422 * NULL if we reached the end of line before
423 * we found an action.
424 * 2 : name = [out] Start of action name, null
425 * terminated. NULL on EOL
426 * 3 : value = [out] Start of action value, null
427 * terminated. NULL if none or EOL.
429 * Returns : JB_ERR_OK => Ok
430 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
432 *********************************************************************/
433 jb_err get_action_token(char **line, char **name, char **value)
438 /* set default returns */
443 /* Eat any leading whitespace */
444 while ((*str == ' ') || (*str == '\t'))
456 /* null name, just value is prohibited */
463 while (((ch = *str) != '\0') &&
464 (ch != ' ') && (ch != '\t') && (ch != '{'))
468 /* error, '}' without '{' */
480 /* EOL - be careful not to run off buffer */
485 /* More to parse next time. */
494 str = strchr(str, '}');
512 /*********************************************************************
514 * Function : get_actions
516 * Description : Parses a list of actions.
519 * 1 : line = The string containing the actions.
520 * Will be written to by this function.
521 * 2 : aliases = Custom alias list, or NULL for none.
522 * 3 : cur_action = Where to store the action. Caller
525 * Returns : JB_ERR_OK => Ok
526 * JB_ERR_PARSE => Parse error (line was trashed anyway)
527 * nonzero => Out of memory (line was trashed anyway)
529 *********************************************************************/
530 jb_err get_actions(char *line,
531 struct action_alias * alias_list,
532 struct action_spec *cur_action)
535 init_action(cur_action);
536 cur_action->mask = ACTION_MASK_ALL;
540 char * option = NULL;
543 err = get_action_token(&line, &option, &value);
551 /* handle option in 'option' */
553 /* Check for standard action name */
554 const struct action_name * action = action_names;
556 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
560 if (action->name != NULL)
563 cur_action->mask &= action->mask;
564 cur_action->add &= action->mask;
565 cur_action->add |= action->add;
567 switch (action->takes_value)
570 /* ignore any option. */
574 /* add single string. */
576 if ((value == NULL) || (*value == '\0'))
580 /* FIXME: should validate option string here */
581 freez (cur_action->string[action->index]);
582 cur_action->string[action->index] = strdup(value);
583 if (NULL == cur_action->string[action->index])
585 return JB_ERR_MEMORY;
591 /* remove single string. */
593 freez (cur_action->string[action->index]);
598 /* append multi string. */
600 struct list * remove_p = cur_action->multi_remove[action->index];
601 struct list * add_p = cur_action->multi_add[action->index];
603 if ((value == NULL) || (*value == '\0'))
608 list_remove_item(remove_p, value);
609 err = enlist_unique(add_p, value, 0);
618 /* remove multi string. */
620 struct list * remove_p = cur_action->multi_remove[action->index];
621 struct list * add_p = cur_action->multi_add[action->index];
623 if ( (value == NULL) || (*value == '\0')
624 || ((*value == '*') && (value[1] == '\0')) )
627 * no option, or option == "*".
631 list_remove_all(remove_p);
632 list_remove_all(add_p);
633 cur_action->multi_remove_all[action->index] = 1;
637 /* Valid option - remove only 1 option */
639 if ( !cur_action->multi_remove_all[action->index] )
641 /* there isn't a catch-all in the remove list already */
642 err = enlist_unique(remove_p, value, 0);
648 list_remove_item(add_p, value);
653 /* Shouldn't get here unless there's memory corruption. */
660 /* try user aliases. */
661 const struct action_alias * alias = alias_list;
663 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
670 merge_actions(cur_action, alias->action);
674 /* Bad action name */
685 /*********************************************************************
687 * Function : init_current_action
689 * Description : Zero out an action.
692 * 1 : dest = An uninitialized current_action_spec.
696 *********************************************************************/
697 void init_current_action (struct current_action_spec *dest)
699 memset(dest, '\0', sizeof(*dest));
701 dest->flags = ACTION_MOST_COMPATIBLE;
705 /*********************************************************************
707 * Function : init_action
709 * Description : Zero out an action.
712 * 1 : dest = An uninitialized action_spec.
716 *********************************************************************/
717 void init_action (struct action_spec *dest)
719 memset(dest, '\0', sizeof(*dest));
723 /*********************************************************************
725 * Function : merge_current_action
727 * Description : Merge two actions together.
728 * Similar to "dest += src".
729 * Differences between this and merge_actions()
730 * is that this one doesn't allocate memory for
731 * strings (so "src" better be in memory for at least
732 * as long as "dest" is, and you'd better free
733 * "dest" using "free_current_action").
734 * Also, there is no mask or remove lists in dest.
735 * (If we're applying it to a URL, we don't need them)
738 * 1 : dest = Current actions, to modify.
739 * 2 : src = Action to add.
741 * Returns 0 : no error
742 * !=0 : error, probably JB_ERR_MEMORY.
744 *********************************************************************/
745 jb_err merge_current_action (struct current_action_spec *dest,
746 const struct action_spec *src)
749 jb_err err = JB_ERR_OK;
751 dest->flags &= src->mask;
752 dest->flags |= src->add;
754 for (i = 0; i < ACTION_STRING_COUNT; i++)
756 char * str = src->string[i];
762 return JB_ERR_MEMORY;
764 freez(dest->string[i]);
765 dest->string[i] = str;
769 for (i = 0; i < ACTION_MULTI_COUNT; i++)
771 if (src->multi_remove_all[i])
773 /* Remove everything from dest, then add src->multi_add */
774 err = list_duplicate(dest->multi[i], src->multi_add[i]);
782 list_remove_list(dest->multi[i], src->multi_remove[i]);
783 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
794 /*********************************************************************
796 * Function : free_current_action
798 * Description : Free memory used by a current_action_spec.
799 * Does not free the current_action_spec itself.
802 * 1 : src = Source to free.
806 *********************************************************************/
807 void free_current_action (struct current_action_spec *src)
811 for (i = 0; i < ACTION_STRING_COUNT; i++)
813 freez(src->string[i]);
816 for (i = 0; i < ACTION_MULTI_COUNT; i++)
818 destroy_list(src->multi[i]);
821 memset(src, '\0', sizeof(*src));
825 static struct file_list *current_actions_file[MAX_ACTION_FILES] = {
826 NULL, NULL, NULL, NULL, NULL,
827 NULL, NULL, NULL, NULL, NULL
831 #ifdef FEATURE_GRACEFUL_TERMINATION
832 /*********************************************************************
834 * Function : unload_current_actions_file
836 * Description : Unloads current actions file - reset to state at
837 * beginning of program.
843 *********************************************************************/
844 void unload_current_actions_file(void)
846 if (current_actions_file)
848 current_actions_file->unloader = unload_actions_file;
849 current_actions_file = NULL;
852 #endif /* FEATURE_GRACEFUL_TERMINATION */
855 /*********************************************************************
857 * Function : unload_actions_file
859 * Description : Unloads an actions module.
862 * 1 : file_data = the data structure associated with the
867 *********************************************************************/
868 void unload_actions_file(void *file_data)
870 struct url_actions * next;
871 struct url_actions * cur = (struct url_actions *)file_data;
875 free_url_spec(cur->url);
876 free_action(cur->action);
884 /*********************************************************************
886 * Function : free_alias_list
888 * Description : Free memory used by a list of aliases.
891 * 1 : alias_list = Linked list to free.
895 *********************************************************************/
896 void free_alias_list(struct action_alias *alias_list)
898 while (alias_list != NULL)
900 struct action_alias * next = alias_list->next;
901 alias_list->next = NULL;
902 freez(alias_list->name);
903 free_action(alias_list->action);
910 /*********************************************************************
912 * Function : load_actions_file
914 * Description : Read and parse all the action files and add to files
918 * 1 : csp = Current client state (buffers, headers, etc...)
920 * Returns : 0 => Ok, everything else is an error.
922 *********************************************************************/
923 int load_actions_file(struct client_state *csp)
928 for (i = 0; i < MAX_ACTION_FILES; i++)
930 if (csp->config->actions_file[i])
932 result = load_one_actions_file(csp, i);
938 else if (current_actions_file[i])
940 current_actions_file[i]->unloader = unload_actions_file;
941 current_actions_file[i] = NULL;
948 /*********************************************************************
950 * Function : load_one_actions_file
952 * Description : Read and parse a action file and add to files
956 * 1 : csp = Current client state (buffers, headers, etc...)
957 * 2 : fileid = File index to load.
959 * Returns : 0 => Ok, everything else is an error.
961 *********************************************************************/
962 static int load_one_actions_file(struct client_state *csp, int fileid)
967 * Note: Keep these in the order they occur in the file, they are
968 * sometimes tested with <=
970 #define MODE_START_OF_FILE 1
971 #define MODE_SETTINGS 2
972 #define MODE_DESCRIPTION 3
974 #define MODE_ACTIONS 5
976 int mode = MODE_START_OF_FILE;
979 struct url_actions *last_perm;
980 struct url_actions *perm;
981 char buf[BUFFER_SIZE];
982 struct file_list *fs;
983 struct action_spec * cur_action = NULL;
984 int cur_action_used = 0;
985 struct action_alias * alias_list = NULL;
986 unsigned long linenum = 0;
988 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
990 /* No need to load */
991 csp->actions_list[fileid] = current_actions_file[fileid];
996 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
997 csp->config->actions_file[fileid]);
998 return 1; /* never get here */
1001 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1002 if (last_perm == NULL)
1004 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1005 csp->config->actions_file[fileid]);
1006 return 1; /* never get here */
1009 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1011 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1012 csp->config->actions_file[fileid]);
1013 return 1; /* never get here */
1016 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1020 /* It's a header block */
1023 /* It's {{settings}} or {{alias}} */
1024 int len = strlen(buf);
1025 char * start = buf + 2;
1026 char * end = buf + len - 1;
1027 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1031 log_error(LOG_LEVEL_FATAL,
1032 "can't load actions file '%s': invalid line (%lu): %s",
1033 csp->config->actions_file[fileid], linenum, buf);
1034 return 1; /* never get here */
1037 /* Trim leading and trailing whitespace. */
1045 log_error(LOG_LEVEL_FATAL,
1046 "can't load actions file '%s': invalid line (%lu): {{ }}",
1047 csp->config->actions_file[fileid], linenum);
1048 return 1; /* never get here */
1052 * An actionsfile can optionally contain the following blocks.
1053 * They *MUST* be in this order, to simplify processing:
1059 * ...free text, format TBD, but no line may start with a '{'...
1064 * The actual actions must be *after* these special blocks.
1065 * None of these special blocks may be repeated.
1068 if (0 == strcmpic(start, "settings"))
1070 /* it's a {{settings}} block */
1071 if (mode >= MODE_SETTINGS)
1073 /* {{settings}} must be first thing in file and must only
1077 log_error(LOG_LEVEL_FATAL,
1078 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1079 csp->config->actions_file[fileid], linenum);
1081 mode = MODE_SETTINGS;
1083 else if (0 == strcmpic(start, "description"))
1085 /* it's a {{description}} block */
1086 if (mode >= MODE_DESCRIPTION)
1088 /* {{description}} is a singleton and only {{settings}} may proceed it
1091 log_error(LOG_LEVEL_FATAL,
1092 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1093 csp->config->actions_file[fileid], linenum);
1095 mode = MODE_DESCRIPTION;
1097 else if (0 == strcmpic(start, "alias"))
1099 /* it's an {{alias}} block */
1100 if (mode >= MODE_ALIAS)
1102 /* {{alias}} must be first thing in file, possibly after
1103 * {{settings}} and {{description}}
1105 * {{alias}} must only appear once.
1107 * Note that these are new restrictions introduced in
1108 * v2.9.10 in order to make actionsfile editing simpler.
1109 * (Otherwise, reordering actionsfile entries without
1110 * completely rewriting the file becomes non-trivial)
1113 log_error(LOG_LEVEL_FATAL,
1114 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1115 csp->config->actions_file[fileid], linenum);
1121 /* invalid {{something}} block */
1123 log_error(LOG_LEVEL_FATAL,
1124 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1125 csp->config->actions_file[fileid], linenum, start);
1126 return 1; /* never get here */
1131 /* It's an actions block */
1133 char actions_buf[BUFFER_SIZE];
1137 mode = MODE_ACTIONS;
1139 /* free old action */
1142 if (!cur_action_used)
1144 free_action(cur_action);
1149 cur_action_used = 0;
1150 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1151 if (cur_action == NULL)
1154 log_error(LOG_LEVEL_FATAL,
1155 "can't load actions file '%s': out of memory",
1156 csp->config->actions_file[fileid]);
1157 return 1; /* never get here */
1159 init_action(cur_action);
1162 strcpy(actions_buf, buf + 1);
1164 /* check we have a trailing } and then trim it */
1165 end = actions_buf + strlen(actions_buf) - 1;
1170 log_error(LOG_LEVEL_FATAL,
1171 "can't load actions file '%s': invalid line (%lu): %s",
1172 csp->config->actions_file[fileid], linenum, buf);
1173 return 1; /* never get here */
1177 /* trim any whitespace immediately inside {} */
1180 if (get_actions(actions_buf, alias_list, cur_action))
1184 log_error(LOG_LEVEL_FATAL,
1185 "can't load actions file '%s': invalid line (%lu): %s",
1186 csp->config->actions_file[fileid], linenum, buf);
1187 return 1; /* never get here */
1191 else if (mode == MODE_SETTINGS)
1194 * Part of the {{settings}} block.
1195 * Ignore for now, but we may want to read & check permissions
1196 * when we go multi-user.
1199 else if (mode == MODE_DESCRIPTION)
1202 * Part of the {{description}} block.
1206 else if (mode == MODE_ALIAS)
1211 char actions_buf[BUFFER_SIZE];
1212 struct action_alias * new_alias;
1214 char * start = strchr(buf, '=');
1217 if ((start == NULL) || (start == buf))
1219 log_error(LOG_LEVEL_FATAL,
1220 "can't load actions file '%s': invalid alias line (%lu): %s",
1221 csp->config->actions_file[fileid], linenum, buf);
1222 return 1; /* never get here */
1225 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1228 log_error(LOG_LEVEL_FATAL,
1229 "can't load actions file '%s': out of memory!",
1230 csp->config->actions_file[fileid]);
1231 return 1; /* never get here */
1234 /* Eat any the whitespace before the '=' */
1236 while ((*end == ' ') || (*end == '\t'))
1239 * we already know we must have at least 1 non-ws char
1240 * at start of buf - no need to check
1246 /* Eat any the whitespace after the '=' */
1248 while ((*start == ' ') || (*start == '\t'))
1254 log_error(LOG_LEVEL_FATAL,
1255 "can't load actions file '%s': invalid alias line (%lu): %s",
1256 csp->config->actions_file[fileid], linenum, buf);
1257 return 1; /* never get here */
1260 new_alias->name = strdup(buf);
1262 strcpy(actions_buf, start);
1264 if (get_actions(actions_buf, alias_list, new_alias->action))
1268 log_error(LOG_LEVEL_FATAL,
1269 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1270 csp->config->actions_file[fileid], linenum, buf, start);
1271 return 1; /* never get here */
1275 new_alias->next = alias_list;
1276 alias_list = new_alias;
1278 else if (mode == MODE_ACTIONS)
1280 /* it's a URL pattern */
1282 /* allocate a new node */
1283 if ((perm = zalloc(sizeof(*perm))) == NULL)
1286 log_error(LOG_LEVEL_FATAL,
1287 "can't load actions file '%s': out of memory!",
1288 csp->config->actions_file[fileid]);
1289 return 1; /* never get here */
1293 copy_action (perm->action, cur_action);
1295 /* Save the URL pattern */
1296 if (create_url_spec(perm->url, buf))
1299 log_error(LOG_LEVEL_FATAL,
1300 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1301 csp->config->actions_file[fileid], linenum, buf);
1302 return 1; /* never get here */
1305 /* add it to the list */
1306 last_perm->next = perm;
1309 else if (mode == MODE_START_OF_FILE)
1311 /* oops - please have a {} line as 1st line in file. */
1313 log_error(LOG_LEVEL_FATAL,
1314 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1315 csp->config->actions_file[fileid], linenum, buf);
1316 return 1; /* never get here */
1320 /* How did we get here? This is impossible! */
1322 log_error(LOG_LEVEL_FATAL,
1323 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1324 csp->config->actions_file[fileid], mode);
1325 return 1; /* never get here */
1331 free_action(cur_action);
1333 free_alias_list(alias_list);
1335 /* the old one is now obsolete */
1336 if (current_actions_file[fileid])
1338 current_actions_file[fileid]->unloader = unload_actions_file;
1341 fs->next = files->next;
1343 current_actions_file[fileid] = fs;
1345 csp->actions_list[fileid] = fs;
1352 /*********************************************************************
1354 * Function : actions_to_text
1356 * Description : Converts a actionsfile entry from numeric form
1357 * ("mask" and "add") to text.
1360 * 1 : mask = As from struct url_actions
1361 * 2 : add = As from struct url_actions
1363 * Returns : A string. Caller must free it.
1364 * NULL on out-of-memory error.
1366 *********************************************************************/
1367 char * actions_to_text(struct action_spec *action)
1369 unsigned mask = action->mask;
1370 unsigned add = action->add;
1371 char * result = strdup("");
1372 struct list_entry * lst;
1374 /* sanity - prevents "-feature +feature" */
1378 #define DEFINE_ACTION_BOOL(__name, __bit) \
1379 if (!(mask & __bit)) \
1381 string_append(&result, " -" __name); \
1383 else if (add & __bit) \
1385 string_append(&result, " +" __name); \
1388 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1389 if (!(mask & __bit)) \
1391 string_append(&result, " -" __name); \
1393 else if (add & __bit) \
1395 string_append(&result, " +" __name "{"); \
1396 string_append(&result, action->string[__index]); \
1397 string_append(&result, "}"); \
1400 #define DEFINE_ACTION_MULTI(__name, __index) \
1401 if (action->multi_remove_all[__index]) \
1403 string_append(&result, " -" __name); \
1407 lst = action->multi_remove[__index]->first; \
1410 string_append(&result, " -" __name "{"); \
1411 string_append(&result, lst->str); \
1412 string_append(&result, "}"); \
1416 lst = action->multi_add[__index]->first; \
1419 string_append(&result, " +" __name "{"); \
1420 string_append(&result, lst->str); \
1421 string_append(&result, "}"); \
1425 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1427 #include "actionlist.h"
1429 #undef DEFINE_ACTION_MULTI
1430 #undef DEFINE_ACTION_STRING
1431 #undef DEFINE_ACTION_BOOL
1432 #undef DEFINE_ACTION_ALIAS
1438 #ifdef FEATURE_CGI_EDIT_ACTIONS
1439 /*********************************************************************
1441 * Function : actions_to_html
1443 * Description : Converts a actionsfile entry from numeric form
1444 * ("mask" and "add") to a <br>-seperated HTML string.
1447 * 1 : mask = As from struct url_actions
1448 * 2 : add = As from struct url_actions
1450 * Returns : A string. Caller must free it.
1451 * NULL on out-of-memory error.
1453 *********************************************************************/
1454 char * actions_to_html(struct action_spec *action)
1456 unsigned mask = action->mask;
1457 unsigned add = action->add;
1458 char * result = strdup("");
1460 struct list_entry * lst;
1462 /* sanity - prevents "-feature +feature" */
1466 #define DEFINE_ACTION_BOOL(__name, __bit) \
1467 if (!(mask & __bit)) \
1469 string_append(&result, "\n<br>-" __name); \
1471 else if (add & __bit) \
1473 string_append(&result, "\n<br>+" __name); \
1476 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1477 if (!(mask & __bit)) \
1479 string_append(&result, "\n<br>-" __name); \
1481 else if (add & __bit) \
1483 string_append(&result, "\n<br>+" __name "{"); \
1484 if (NULL == result) \
1488 enc_str = html_encode(action->string[__index]);\
1489 if (NULL == enc_str) \
1494 string_append(&result, enc_str); \
1496 string_append(&result, "}"); \
1499 #define DEFINE_ACTION_MULTI(__name, __index) \
1500 if (action->multi_remove_all[__index]) \
1502 string_append(&result, "\n<br>-" __name); \
1506 lst = action->multi_remove[__index]->first; \
1509 string_append(&result, "\n<br>-" __name "{");\
1510 if (NULL == result) \
1514 enc_str = html_encode(lst->str); \
1515 if (NULL == enc_str) \
1520 string_append(&result, enc_str); \
1522 string_append(&result, "}"); \
1526 lst = action->multi_add[__index]->first; \
1529 string_append(&result, "\n<br>+" __name "{"); \
1530 if (NULL == result) \
1534 enc_str = html_encode(lst->str); \
1535 if (NULL == enc_str) \
1540 string_append(&result, enc_str); \
1542 string_append(&result, "}"); \
1546 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1548 #include "actionlist.h"
1550 #undef DEFINE_ACTION_MULTI
1551 #undef DEFINE_ACTION_STRING
1552 #undef DEFINE_ACTION_BOOL
1553 #undef DEFINE_ACTION_ALIAS
1555 /* trim leading <br> */
1556 if (result && *result)
1559 result = strdup(result + 5);
1565 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1568 /*********************************************************************
1570 * Function : current_actions_to_text
1572 * Description : Converts a actionsfile entry to text.
1575 * 1 : action = Action
1577 * Returns : A string. Caller must free it.
1578 * NULL on out-of-memory error.
1580 *********************************************************************/
1581 char * current_action_to_text(struct current_action_spec *action)
1583 unsigned long flags = action->flags;
1584 char * result = strdup("");
1585 struct list_entry * lst;
1587 #define DEFINE_ACTION_BOOL(__name, __bit) \
1588 if (flags & __bit) \
1590 string_append(&result, " +" __name); \
1594 string_append(&result, " -" __name); \
1597 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1598 if (flags & __bit) \
1600 string_append(&result, " +" __name "{"); \
1601 string_append(&result, action->string[__index]); \
1602 string_append(&result, "}"); \
1606 string_append(&result, " -" __name); \
1609 #define DEFINE_ACTION_MULTI(__name, __index) \
1610 lst = action->multi[__index]->first; \
1613 string_append(&result, " -" __name); \
1619 string_append(&result, " +" __name "{"); \
1620 string_append(&result, lst->str); \
1621 string_append(&result, "}"); \
1626 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1628 #include "actionlist.h"
1630 #undef DEFINE_ACTION_MULTI
1631 #undef DEFINE_ACTION_STRING
1632 #undef DEFINE_ACTION_BOOL
1633 #undef DEFINE_ACTION_ALIAS