1 const char actions_rcs[] = "$Id: actions.c,v 1.27 2002/04/24 02:10:31 oes 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.27 2002/04/24 02:10:31 oes
37 * - Jon's patch for multiple AFs:
38 * - split load_actions_file, add load_one_actions_file
39 * - make csp->actions_list files an array
40 * - remember file id with each action
41 * - Copy_action now frees dest action before copying
43 * Revision 1.26 2002/03/26 22:29:54 swa
44 * we have a new homepage!
46 * Revision 1.25 2002/03/24 13:25:43 swa
47 * name change related issues
49 * Revision 1.24 2002/03/16 23:54:06 jongfoster
50 * Adding graceful termination feature, to help look for memory leaks.
51 * If you enable this (which, by design, has to be done by hand
52 * editing config.h) and then go to http://i.j.b/die, then the program
53 * will exit cleanly after the *next* request. It should free all the
54 * memory that was used.
56 * Revision 1.23 2002/03/07 03:46:16 oes
57 * Fixed compiler warnings
59 * Revision 1.22 2002/01/21 00:27:02 jongfoster
60 * Allowing free_action(NULL).
61 * Moving the functions that #include actionlist.h to the end of the file,
62 * because the Visual C++ 97 debugger gets extremely confused if you try
63 * to debug any code that comes after them in the file.
65 * Revision 1.21 2002/01/17 20:54:44 jongfoster
66 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
68 * Revision 1.20 2001/11/22 21:56:49 jongfoster
69 * Making action_spec->flags into an unsigned long rather than just an
71 * Fixing a bug in the display of -add-header and -wafer
73 * Revision 1.19 2001/11/13 00:14:07 jongfoster
74 * Fixing stupid bug now I've figured out what || means.
75 * (It always returns 0 or 1, not one of it's paramaters.)
77 * Revision 1.18 2001/11/07 00:06:06 steudten
78 * Add line number in error output for lineparsing for
81 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
82 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
83 * threads to call select() simultaneously. So, it's time to do a real, live,
84 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
85 * (native). Both versions will work, but using __OS2__ offers multi-threading.
87 * Revision 1.16 2001/10/23 21:30:30 jongfoster
88 * Adding error-checking to selected functions.
90 * Revision 1.15 2001/10/14 21:58:22 jongfoster
91 * Adding support for the CGI-based editor:
92 * - Exported get_actions()
93 * - Added new function free_alias_list()
94 * - Added support for {{settings}} and {{description}} blocks
95 * in the actions file. They are currently ignored.
96 * - Added restriction to only one {{alias}} block which must appear
97 * first in the file, to simplify the editor's rewriting rules.
98 * - Note that load_actions_file() is no longer used by the CGI-based
99 * editor, but some of the other routines in this file are.
101 * Revision 1.14 2001/09/22 16:36:59 jongfoster
102 * Removing unused parameter fs from read_config_line()
104 * Revision 1.13 2001/09/16 15:47:37 jongfoster
105 * First version of CGI-based edit interface. This is very much a
106 * work-in-progress, and you can't actually use it to edit anything
107 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
108 * to have any effect.
110 * Revision 1.12 2001/09/16 13:21:27 jongfoster
111 * Changes to use new list functions.
113 * Revision 1.11 2001/09/14 00:17:32 jongfoster
114 * Tidying up memory allocation. New function init_action().
116 * Revision 1.10 2001/09/10 10:14:34 oes
117 * Removing unused variable
119 * Revision 1.9 2001/07/30 22:08:36 jongfoster
120 * Tidying up #defines:
121 * - All feature #defines are now of the form FEATURE_xxx
122 * - Permanently turned off WIN_GUI_EDIT
123 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
125 * Revision 1.8 2001/06/29 13:19:52 oes
126 * Removed logentry from cancelled commit
128 * Revision 1.7 2001/06/09 10:55:28 jongfoster
129 * Changing BUFSIZ ==> BUFFER_SIZE
131 * Revision 1.6 2001/06/07 23:04:34 jongfoster
132 * Made get_actions() static.
134 * Revision 1.5 2001/06/03 19:11:48 oes
135 * adapted to new enlist_unique arg format
137 * Revision 1.4 2001/06/01 20:03:42 jongfoster
138 * Better memory management - current_action->strings[] now
139 * contains copies of the strings, not the original.
141 * Revision 1.3 2001/06/01 18:49:17 jongfoster
142 * Replaced "list_share" with "list" - the tiny memory gain was not
143 * worth the extra complexity.
145 * Revision 1.2 2001/05/31 21:40:00 jongfoster
146 * Removing some commented out, obsolete blocks of code.
148 * Revision 1.1 2001/05/31 21:16:46 jongfoster
149 * Moved functions to process the action list into this new file.
152 *********************************************************************/
166 #include "miscutil.h"
169 #ifdef FEATURE_CGI_EDIT_ACTIONS
171 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
172 #include "urlmatch.h"
175 const char actions_h_rcs[] = ACTIONS_H_VERSION;
179 * We need the main list of options.
181 * First, we need a way to tell between boolean, string, and multi-string
182 * options. For string and multistring options, we also need to be
183 * able to tell the difference between a "+" and a "-". (For bools,
184 * the "+"/"-" information is encoded in "add" and "mask"). So we use
185 * an enumerated type (well, the preprocessor equivalent). Here are
188 #define AV_NONE 0 /* +opt -opt */
189 #define AV_ADD_STRING 1 /* +stropt{string} */
190 #define AV_REM_STRING 2 /* -stropt */
191 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
192 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
195 * We need a structure to hold the name, flag changes,
196 * type, and string index.
201 unsigned mask; /* a bit set to "0" = remove action */
202 unsigned add; /* a bit set to "1" = add action */
203 int takes_value; /* an AV_... constant */
204 int index; /* index into strings[] or multi[] */
208 * And with those building blocks in place, here's the array.
210 static const struct action_name action_names[] =
213 * Well actually there's no data here - it's in actionlist.h
214 * This keeps it together to make it easy to change.
216 * Here's the macros used to format it:
218 #define DEFINE_ACTION_MULTI(name,index) \
219 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
220 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
221 #define DEFINE_ACTION_STRING(name,flag,index) \
222 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
223 { "-" name, ~flag, 0, AV_REM_STRING, index },
224 #define DEFINE_ACTION_BOOL(name,flag) \
225 { "+" name, ACTION_MASK_ALL, flag }, \
226 { "-" name, ~flag, 0 },
227 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
229 #include "actionlist.h"
231 #undef DEFINE_ACTION_MULTI
232 #undef DEFINE_ACTION_STRING
233 #undef DEFINE_ACTION_BOOL
234 #undef DEFINE_ACTION_ALIAS
236 { NULL, 0, 0 } /* End marker */
240 static int load_one_actions_file(struct client_state *csp, int fileid);
243 /*********************************************************************
245 * Function : merge_actions
247 * Description : Merge two actions together.
248 * Similar to "cur_action += new_action".
251 * 1 : cur_action = Current actions, to modify.
252 * 2 : new_action = Action to add.
254 * Returns : JB_ERR_OK or JB_ERR_MEMORY
256 *********************************************************************/
257 jb_err merge_actions (struct action_spec *dest,
258 const struct action_spec *src)
263 dest->mask &= src->mask;
264 dest->add &= src->mask;
265 dest->add |= src->add;
267 for (i = 0; i < ACTION_STRING_COUNT; i++)
269 char * str = src->string[i];
272 freez(dest->string[i]);
273 dest->string[i] = strdup(str);
274 if (NULL == dest->string[i])
276 return JB_ERR_MEMORY;
281 for (i = 0; i < ACTION_MULTI_COUNT; i++)
283 if (src->multi_remove_all[i])
285 /* Remove everything from dest */
286 list_remove_all(dest->multi_remove[i]);
287 dest->multi_remove_all[i] = 1;
289 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
291 else if (dest->multi_remove_all[i])
294 * dest already removes everything, so we only need to worry
297 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
298 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
302 /* No "remove all"s to worry about. */
303 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
304 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
305 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
318 /*********************************************************************
320 * Function : copy_action
322 * Description : Copy an action_specs.
323 * Similar to "cur_action = new_action".
326 * 1 : dest = Destination of copy.
327 * 2 : src = Source for copy.
331 *********************************************************************/
332 jb_err copy_action (struct action_spec *dest,
333 const struct action_spec *src)
336 jb_err err = JB_ERR_OK;
339 memset(dest, '\0', sizeof(*dest));
341 dest->mask = src->mask;
342 dest->add = src->add;
344 for (i = 0; i < ACTION_STRING_COUNT; i++)
346 char * str = src->string[i];
352 return JB_ERR_MEMORY;
354 dest->string[i] = str;
358 for (i = 0; i < ACTION_MULTI_COUNT; i++)
360 dest->multi_remove_all[i] = src->multi_remove_all[i];
361 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
366 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
376 /*********************************************************************
378 * Function : free_action
380 * Description : Destroy an action_spec. Frees memory used by it,
381 * except for the memory used by the struct action_spec
385 * 1 : src = Source to free.
389 *********************************************************************/
390 void free_action (struct action_spec *src)
399 for (i = 0; i < ACTION_STRING_COUNT; i++)
401 freez(src->string[i]);
404 for (i = 0; i < ACTION_MULTI_COUNT; i++)
406 destroy_list(src->multi_remove[i]);
407 destroy_list(src->multi_add[i]);
410 memset(src, '\0', sizeof(*src));
414 /*********************************************************************
416 * Function : get_action_token
418 * Description : Parses a line for the first action.
419 * Modifies it's input array, doesn't allocate memory.
421 * *line=" +abc{def} -ghi "
428 * 1 : line = [in] The line containing the action.
429 * [out] Start of next action on line, or
430 * NULL if we reached the end of line before
431 * we found an action.
432 * 2 : name = [out] Start of action name, null
433 * terminated. NULL on EOL
434 * 3 : value = [out] Start of action value, null
435 * terminated. NULL if none or EOL.
437 * Returns : JB_ERR_OK => Ok
438 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
440 *********************************************************************/
441 jb_err get_action_token(char **line, char **name, char **value)
446 /* set default returns */
451 /* Eat any leading whitespace */
452 while ((*str == ' ') || (*str == '\t'))
464 /* null name, just value is prohibited */
471 while (((ch = *str) != '\0') &&
472 (ch != ' ') && (ch != '\t') && (ch != '{'))
476 /* error, '}' without '{' */
488 /* EOL - be careful not to run off buffer */
493 /* More to parse next time. */
502 str = strchr(str, '}');
520 /*********************************************************************
522 * Function : get_actions
524 * Description : Parses a list of actions.
527 * 1 : line = The string containing the actions.
528 * Will be written to by this function.
529 * 2 : aliases = Custom alias list, or NULL for none.
530 * 3 : cur_action = Where to store the action. Caller
533 * Returns : JB_ERR_OK => Ok
534 * JB_ERR_PARSE => Parse error (line was trashed anyway)
535 * nonzero => Out of memory (line was trashed anyway)
537 *********************************************************************/
538 jb_err get_actions(char *line,
539 struct action_alias * alias_list,
540 struct action_spec *cur_action)
543 init_action(cur_action);
544 cur_action->mask = ACTION_MASK_ALL;
548 char * option = NULL;
551 err = get_action_token(&line, &option, &value);
559 /* handle option in 'option' */
561 /* Check for standard action name */
562 const struct action_name * action = action_names;
564 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
568 if (action->name != NULL)
571 cur_action->mask &= action->mask;
572 cur_action->add &= action->mask;
573 cur_action->add |= action->add;
575 switch (action->takes_value)
578 /* ignore any option. */
582 /* add single string. */
584 if ((value == NULL) || (*value == '\0'))
588 /* FIXME: should validate option string here */
589 freez (cur_action->string[action->index]);
590 cur_action->string[action->index] = strdup(value);
591 if (NULL == cur_action->string[action->index])
593 return JB_ERR_MEMORY;
599 /* remove single string. */
601 freez (cur_action->string[action->index]);
606 /* append multi string. */
608 struct list * remove_p = cur_action->multi_remove[action->index];
609 struct list * add_p = cur_action->multi_add[action->index];
611 if ((value == NULL) || (*value == '\0'))
616 list_remove_item(remove_p, value);
617 err = enlist_unique(add_p, value, 0);
626 /* remove multi string. */
628 struct list * remove_p = cur_action->multi_remove[action->index];
629 struct list * add_p = cur_action->multi_add[action->index];
631 if ( (value == NULL) || (*value == '\0')
632 || ((*value == '*') && (value[1] == '\0')) )
635 * no option, or option == "*".
639 list_remove_all(remove_p);
640 list_remove_all(add_p);
641 cur_action->multi_remove_all[action->index] = 1;
645 /* Valid option - remove only 1 option */
647 if ( !cur_action->multi_remove_all[action->index] )
649 /* there isn't a catch-all in the remove list already */
650 err = enlist_unique(remove_p, value, 0);
656 list_remove_item(add_p, value);
661 /* Shouldn't get here unless there's memory corruption. */
668 /* try user aliases. */
669 const struct action_alias * alias = alias_list;
671 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
678 merge_actions(cur_action, alias->action);
682 /* Bad action name */
693 /*********************************************************************
695 * Function : init_current_action
697 * Description : Zero out an action.
700 * 1 : dest = An uninitialized current_action_spec.
704 *********************************************************************/
705 void init_current_action (struct current_action_spec *dest)
707 memset(dest, '\0', sizeof(*dest));
709 dest->flags = ACTION_MOST_COMPATIBLE;
713 /*********************************************************************
715 * Function : init_action
717 * Description : Zero out an action.
720 * 1 : dest = An uninitialized action_spec.
724 *********************************************************************/
725 void init_action (struct action_spec *dest)
727 memset(dest, '\0', sizeof(*dest));
731 /*********************************************************************
733 * Function : merge_current_action
735 * Description : Merge two actions together.
736 * Similar to "dest += src".
737 * Differences between this and merge_actions()
738 * is that this one doesn't allocate memory for
739 * strings (so "src" better be in memory for at least
740 * as long as "dest" is, and you'd better free
741 * "dest" using "free_current_action").
742 * Also, there is no mask or remove lists in dest.
743 * (If we're applying it to a URL, we don't need them)
746 * 1 : dest = Current actions, to modify.
747 * 2 : src = Action to add.
749 * Returns 0 : no error
750 * !=0 : error, probably JB_ERR_MEMORY.
752 *********************************************************************/
753 jb_err merge_current_action (struct current_action_spec *dest,
754 const struct action_spec *src)
757 jb_err err = JB_ERR_OK;
759 dest->flags &= src->mask;
760 dest->flags |= src->add;
762 for (i = 0; i < ACTION_STRING_COUNT; i++)
764 char * str = src->string[i];
770 return JB_ERR_MEMORY;
772 freez(dest->string[i]);
773 dest->string[i] = str;
777 for (i = 0; i < ACTION_MULTI_COUNT; i++)
779 if (src->multi_remove_all[i])
781 /* Remove everything from dest, then add src->multi_add */
782 err = list_duplicate(dest->multi[i], src->multi_add[i]);
790 list_remove_list(dest->multi[i], src->multi_remove[i]);
791 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
802 /*********************************************************************
804 * Function : free_current_action
806 * Description : Free memory used by a current_action_spec.
807 * Does not free the current_action_spec itself.
810 * 1 : src = Source to free.
814 *********************************************************************/
815 void free_current_action (struct current_action_spec *src)
819 for (i = 0; i < ACTION_STRING_COUNT; i++)
821 freez(src->string[i]);
824 for (i = 0; i < ACTION_MULTI_COUNT; i++)
826 destroy_list(src->multi[i]);
829 memset(src, '\0', sizeof(*src));
833 static struct file_list *current_actions_file[MAX_ACTION_FILES] = {
834 NULL, NULL, NULL, NULL, NULL,
835 NULL, NULL, NULL, NULL, NULL
839 #ifdef FEATURE_GRACEFUL_TERMINATION
840 /*********************************************************************
842 * Function : unload_current_actions_file
844 * Description : Unloads current actions file - reset to state at
845 * beginning of program.
851 *********************************************************************/
852 void unload_current_actions_file(void)
854 if (current_actions_file)
856 current_actions_file->unloader = unload_actions_file;
857 current_actions_file = NULL;
860 #endif /* FEATURE_GRACEFUL_TERMINATION */
863 /*********************************************************************
865 * Function : unload_actions_file
867 * Description : Unloads an actions module.
870 * 1 : file_data = the data structure associated with the
875 *********************************************************************/
876 void unload_actions_file(void *file_data)
878 struct url_actions * next;
879 struct url_actions * cur = (struct url_actions *)file_data;
883 free_url_spec(cur->url);
884 free_action(cur->action);
892 /*********************************************************************
894 * Function : free_alias_list
896 * Description : Free memory used by a list of aliases.
899 * 1 : alias_list = Linked list to free.
903 *********************************************************************/
904 void free_alias_list(struct action_alias *alias_list)
906 while (alias_list != NULL)
908 struct action_alias * next = alias_list->next;
909 alias_list->next = NULL;
910 freez(alias_list->name);
911 free_action(alias_list->action);
918 /*********************************************************************
920 * Function : load_actions_file
922 * Description : Read and parse all the action files and add to files
926 * 1 : csp = Current client state (buffers, headers, etc...)
928 * Returns : 0 => Ok, everything else is an error.
930 *********************************************************************/
931 int load_actions_file(struct client_state *csp)
936 for (i = 0; i < MAX_ACTION_FILES; i++)
938 if (csp->config->actions_file[i])
940 result = load_one_actions_file(csp, i);
946 else if (current_actions_file[i])
948 current_actions_file[i]->unloader = unload_actions_file;
949 current_actions_file[i] = NULL;
956 /*********************************************************************
958 * Function : load_one_actions_file
960 * Description : Read and parse a action file and add to files
964 * 1 : csp = Current client state (buffers, headers, etc...)
965 * 2 : fileid = File index to load.
967 * Returns : 0 => Ok, everything else is an error.
969 *********************************************************************/
970 static int load_one_actions_file(struct client_state *csp, int fileid)
975 * Note: Keep these in the order they occur in the file, they are
976 * sometimes tested with <=
978 #define MODE_START_OF_FILE 1
979 #define MODE_SETTINGS 2
980 #define MODE_DESCRIPTION 3
982 #define MODE_ACTIONS 5
984 int mode = MODE_START_OF_FILE;
987 struct url_actions *last_perm;
988 struct url_actions *perm;
989 char buf[BUFFER_SIZE];
990 struct file_list *fs;
991 struct action_spec * cur_action = NULL;
992 int cur_action_used = 0;
993 struct action_alias * alias_list = NULL;
994 unsigned long linenum = 0;
996 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
998 /* No need to load */
999 csp->actions_list[fileid] = current_actions_file[fileid];
1004 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1005 csp->config->actions_file[fileid]);
1006 return 1; /* never get here */
1009 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1010 if (last_perm == NULL)
1012 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1013 csp->config->actions_file[fileid]);
1014 return 1; /* never get here */
1017 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1019 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1020 csp->config->actions_file[fileid]);
1021 return 1; /* never get here */
1024 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1028 /* It's a header block */
1031 /* It's {{settings}} or {{alias}} */
1032 int len = strlen(buf);
1033 char * start = buf + 2;
1034 char * end = buf + len - 1;
1035 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1039 log_error(LOG_LEVEL_FATAL,
1040 "can't load actions file '%s': invalid line (%lu): %s",
1041 csp->config->actions_file[fileid], linenum, buf);
1042 return 1; /* never get here */
1045 /* Trim leading and trailing whitespace. */
1053 log_error(LOG_LEVEL_FATAL,
1054 "can't load actions file '%s': invalid line (%lu): {{ }}",
1055 csp->config->actions_file[fileid], linenum);
1056 return 1; /* never get here */
1060 * An actionsfile can optionally contain the following blocks.
1061 * They *MUST* be in this order, to simplify processing:
1067 * ...free text, format TBD, but no line may start with a '{'...
1072 * The actual actions must be *after* these special blocks.
1073 * None of these special blocks may be repeated.
1076 if (0 == strcmpic(start, "settings"))
1078 /* it's a {{settings}} block */
1079 if (mode >= MODE_SETTINGS)
1081 /* {{settings}} must be first thing in file and must only
1085 log_error(LOG_LEVEL_FATAL,
1086 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1087 csp->config->actions_file[fileid], linenum);
1089 mode = MODE_SETTINGS;
1091 else if (0 == strcmpic(start, "description"))
1093 /* it's a {{description}} block */
1094 if (mode >= MODE_DESCRIPTION)
1096 /* {{description}} is a singleton and only {{settings}} may proceed it
1099 log_error(LOG_LEVEL_FATAL,
1100 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1101 csp->config->actions_file[fileid], linenum);
1103 mode = MODE_DESCRIPTION;
1105 else if (0 == strcmpic(start, "alias"))
1107 /* it's an {{alias}} block */
1108 if (mode >= MODE_ALIAS)
1110 /* {{alias}} must be first thing in file, possibly after
1111 * {{settings}} and {{description}}
1113 * {{alias}} must only appear once.
1115 * Note that these are new restrictions introduced in
1116 * v2.9.10 in order to make actionsfile editing simpler.
1117 * (Otherwise, reordering actionsfile entries without
1118 * completely rewriting the file becomes non-trivial)
1121 log_error(LOG_LEVEL_FATAL,
1122 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1123 csp->config->actions_file[fileid], linenum);
1129 /* invalid {{something}} block */
1131 log_error(LOG_LEVEL_FATAL,
1132 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1133 csp->config->actions_file[fileid], linenum, start);
1134 return 1; /* never get here */
1139 /* It's an actions block */
1141 char actions_buf[BUFFER_SIZE];
1145 mode = MODE_ACTIONS;
1147 /* free old action */
1150 if (!cur_action_used)
1152 free_action(cur_action);
1157 cur_action_used = 0;
1158 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1159 if (cur_action == NULL)
1162 log_error(LOG_LEVEL_FATAL,
1163 "can't load actions file '%s': out of memory",
1164 csp->config->actions_file[fileid]);
1165 return 1; /* never get here */
1167 init_action(cur_action);
1170 strcpy(actions_buf, buf + 1);
1172 /* check we have a trailing } and then trim it */
1173 end = actions_buf + strlen(actions_buf) - 1;
1178 log_error(LOG_LEVEL_FATAL,
1179 "can't load actions file '%s': invalid line (%lu): %s",
1180 csp->config->actions_file[fileid], linenum, buf);
1181 return 1; /* never get here */
1185 /* trim any whitespace immediately inside {} */
1188 if (get_actions(actions_buf, alias_list, cur_action))
1192 log_error(LOG_LEVEL_FATAL,
1193 "can't load actions file '%s': invalid line (%lu): %s",
1194 csp->config->actions_file[fileid], linenum, buf);
1195 return 1; /* never get here */
1199 else if (mode == MODE_SETTINGS)
1202 * Part of the {{settings}} block.
1203 * Ignore for now, but we may want to read & check permissions
1204 * when we go multi-user.
1207 else if (mode == MODE_DESCRIPTION)
1210 * Part of the {{description}} block.
1214 else if (mode == MODE_ALIAS)
1219 char actions_buf[BUFFER_SIZE];
1220 struct action_alias * new_alias;
1222 char * start = strchr(buf, '=');
1225 if ((start == NULL) || (start == buf))
1227 log_error(LOG_LEVEL_FATAL,
1228 "can't load actions file '%s': invalid alias line (%lu): %s",
1229 csp->config->actions_file[fileid], linenum, buf);
1230 return 1; /* never get here */
1233 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1236 log_error(LOG_LEVEL_FATAL,
1237 "can't load actions file '%s': out of memory!",
1238 csp->config->actions_file[fileid]);
1239 return 1; /* never get here */
1242 /* Eat any the whitespace before the '=' */
1244 while ((*end == ' ') || (*end == '\t'))
1247 * we already know we must have at least 1 non-ws char
1248 * at start of buf - no need to check
1254 /* Eat any the whitespace after the '=' */
1256 while ((*start == ' ') || (*start == '\t'))
1262 log_error(LOG_LEVEL_FATAL,
1263 "can't load actions file '%s': invalid alias line (%lu): %s",
1264 csp->config->actions_file[fileid], linenum, buf);
1265 return 1; /* never get here */
1268 new_alias->name = strdup(buf);
1270 strcpy(actions_buf, start);
1272 if (get_actions(actions_buf, alias_list, new_alias->action))
1276 log_error(LOG_LEVEL_FATAL,
1277 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1278 csp->config->actions_file[fileid], linenum, buf, start);
1279 return 1; /* never get here */
1283 new_alias->next = alias_list;
1284 alias_list = new_alias;
1286 else if (mode == MODE_ACTIONS)
1288 /* it's a URL pattern */
1290 /* allocate a new node */
1291 if ((perm = zalloc(sizeof(*perm))) == NULL)
1294 log_error(LOG_LEVEL_FATAL,
1295 "can't load actions file '%s': out of memory!",
1296 csp->config->actions_file[fileid]);
1297 return 1; /* never get here */
1301 copy_action (perm->action, cur_action);
1303 /* Save the URL pattern */
1304 if (create_url_spec(perm->url, buf))
1307 log_error(LOG_LEVEL_FATAL,
1308 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1309 csp->config->actions_file[fileid], linenum, buf);
1310 return 1; /* never get here */
1313 /* add it to the list */
1314 last_perm->next = perm;
1317 else if (mode == MODE_START_OF_FILE)
1319 /* oops - please have a {} line as 1st line in file. */
1321 log_error(LOG_LEVEL_FATAL,
1322 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1323 csp->config->actions_file[fileid], linenum, buf);
1324 return 1; /* never get here */
1328 /* How did we get here? This is impossible! */
1330 log_error(LOG_LEVEL_FATAL,
1331 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1332 csp->config->actions_file[fileid], mode);
1333 return 1; /* never get here */
1339 free_action(cur_action);
1341 free_alias_list(alias_list);
1343 /* the old one is now obsolete */
1344 if (current_actions_file[fileid])
1346 current_actions_file[fileid]->unloader = unload_actions_file;
1349 fs->next = files->next;
1351 current_actions_file[fileid] = fs;
1353 csp->actions_list[fileid] = fs;
1360 /*********************************************************************
1362 * Function : actions_to_text
1364 * Description : Converts a actionsfile entry from numeric form
1365 * ("mask" and "add") to a text line which is split
1366 * into one line for each action with line continuation.
1369 * 1 : mask = As from struct url_actions
1370 * 2 : add = As from struct url_actions
1372 * Returns : A string. Caller must free it.
1373 * NULL on out-of-memory error.
1375 *********************************************************************/
1376 char * actions_to_text(struct action_spec *action)
1378 unsigned mask = action->mask;
1379 unsigned add = action->add;
1380 char * result = strdup("");
1381 struct list_entry * lst;
1383 /* sanity - prevents "-feature +feature" */
1387 #define DEFINE_ACTION_BOOL(__name, __bit) \
1388 if (!(mask & __bit)) \
1390 string_append(&result, " -" __name " \\\n"); \
1392 else if (add & __bit) \
1394 string_append(&result, " +" __name " \\\n"); \
1397 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1398 if (!(mask & __bit)) \
1400 string_append(&result, " -" __name " \\\n"); \
1402 else if (add & __bit) \
1404 string_append(&result, " +" __name "{"); \
1405 string_append(&result, action->string[__index]); \
1406 string_append(&result, "} \\\n"); \
1409 #define DEFINE_ACTION_MULTI(__name, __index) \
1410 if (action->multi_remove_all[__index]) \
1412 string_append(&result, " -" __name " \\\n"); \
1416 lst = action->multi_remove[__index]->first; \
1419 string_append(&result, " -" __name "{"); \
1420 string_append(&result, lst->str); \
1421 string_append(&result, "} \\\n"); \
1425 lst = action->multi_add[__index]->first; \
1428 string_append(&result, " +" __name "{"); \
1429 string_append(&result, lst->str); \
1430 string_append(&result, "} \\\n"); \
1434 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1436 #include "actionlist.h"
1438 #undef DEFINE_ACTION_MULTI
1439 #undef DEFINE_ACTION_STRING
1440 #undef DEFINE_ACTION_BOOL
1441 #undef DEFINE_ACTION_ALIAS
1447 #ifdef FEATURE_CGI_EDIT_ACTIONS
1448 /*********************************************************************
1450 * Function : actions_to_html
1452 * Description : Converts a actionsfile entry from numeric form
1453 * ("mask" and "add") to a <br>-seperated HTML string
1454 * in which each action is linked to its chapter in
1458 * 1 : mask = As from struct url_actions
1459 * 2 : add = As from struct url_actions
1461 * Returns : A string. Caller must free it.
1462 * NULL on out-of-memory error.
1464 *********************************************************************/
1465 char * actions_to_html(struct action_spec *action,
1466 struct client_state *csp)
1468 unsigned mask = action->mask;
1469 unsigned add = action->add;
1470 char * result = strdup("");
1472 struct list_entry * lst;
1474 /* sanity - prevents "-feature +feature" */
1478 #define DEFINE_ACTION_BOOL(__name, __bit) \
1479 if (!(mask & __bit)) \
1481 string_append(&result, "\n<br>-"); \
1482 string_join(&result, add_help_link(__name, csp->config)); \
1484 else if (add & __bit) \
1486 string_append(&result, "\n<br>+"); \
1487 string_join(&result, add_help_link(__name, csp->config)); \
1490 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1491 if (!(mask & __bit)) \
1493 string_append(&result, "\n<br>-"); \
1494 string_join(&result, add_help_link(__name, csp->config)); \
1496 else if (add & __bit) \
1498 string_append(&result, "\n<br>+"); \
1499 string_join(&result, add_help_link(__name, csp->config)); \
1500 string_append(&result, "{"); \
1501 if (NULL == result) \
1505 enc_str = html_encode(action->string[__index]);\
1506 if (NULL == enc_str) \
1511 string_append(&result, enc_str); \
1513 string_append(&result, "}"); \
1516 #define DEFINE_ACTION_MULTI(__name, __index) \
1517 if (action->multi_remove_all[__index]) \
1519 string_append(&result, "\n<br>-"); \
1520 string_join(&result, add_help_link(__name, csp->config)); \
1524 lst = action->multi_remove[__index]->first; \
1527 string_append(&result, "\n<br>-"); \
1528 string_join(&result, add_help_link(__name, csp->config)); \
1529 string_append(&result, "{"); \
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 lst = action->multi_add[__index]->first; \
1549 string_append(&result, "\n<br>+"); \
1550 string_join(&result, add_help_link(__name, csp->config)); \
1551 string_append(&result, "{"); \
1552 if (NULL == result) \
1556 enc_str = html_encode(lst->str); \
1557 if (NULL == enc_str) \
1562 string_append(&result, enc_str); \
1564 string_append(&result, "}"); \
1568 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1570 #include "actionlist.h"
1572 #undef DEFINE_ACTION_MULTI
1573 #undef DEFINE_ACTION_STRING
1574 #undef DEFINE_ACTION_BOOL
1575 #undef DEFINE_ACTION_ALIAS
1577 /* trim leading <br> */
1578 if (result && *result)
1581 result = strdup(result + 5);
1587 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
1590 /*********************************************************************
1592 * Function : current_actions_to_html
1594 * Description : Converts a curren action spec to a <br> seperated HTML
1595 * text in which each action is linked to its chapter in
1599 * 1 : action = Action
1601 * Returns : A string. Caller must free it.
1602 * NULL on out-of-memory error.
1604 *********************************************************************/
1605 char *current_action_to_html(struct current_action_spec *action,
1606 struct client_state *csp)
1608 unsigned long flags = action->flags;
1609 char * result = strdup("");
1611 struct list_entry * lst;
1613 #define DEFINE_ACTION_BOOL(__name, __bit) \
1614 if (flags & __bit) \
1616 string_append(&result, "\n<br>+"); \
1617 string_join(&result, add_help_link(__name, csp->config)); \
1621 string_append(&result, "\n<br>-"); \
1622 string_join(&result, add_help_link(__name, csp->config)); \
1625 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1626 if (flags & __bit) \
1628 string_append(&result, "\n<br>+"); \
1629 string_join(&result, add_help_link(__name, csp->config)); \
1630 string_append(&result, "{"); \
1631 enc_str = html_encode(action->string[__index]); \
1632 if (NULL == enc_str) \
1637 string_append(&result, enc_str); \
1639 string_append(&result, "}"); \
1643 string_append(&result, "\n<br>-" __name); \
1646 #define DEFINE_ACTION_MULTI(__name, __index) \
1647 lst = action->multi[__index]->first; \
1650 string_append(&result, "\n<br> -"); \
1651 string_join(&result, add_help_link(__name, csp->config)); \
1657 string_append(&result, "\n<br> +"); \
1658 string_join(&result, add_help_link(__name, csp->config)); \
1659 string_append(&result, "{"); \
1660 enc_str = html_encode(lst->str); \
1661 if (NULL == enc_str) \
1666 string_append(&result, enc_str); \
1668 string_append(&result, "}"); \
1673 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1675 #include "actionlist.h"
1677 #undef DEFINE_ACTION_MULTI
1678 #undef DEFINE_ACTION_STRING
1679 #undef DEFINE_ACTION_BOOL
1680 #undef DEFINE_ACTION_ALIAS