1 const char actions_rcs[] = "$Id: actions.c,v 1.30 2002/04/30 11:14:52 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.30 2002/04/30 11:14:52 oes
37 * Made csp the first parameter in *action_to_html
39 * Revision 1.29 2002/04/26 19:30:54 jongfoster
40 * - current_action_to_html(): Adding help link for the "-" form of
42 * - Some actions had "<br>-", some "<br> -" (note the space).
43 * Standardizing on no space.
44 * - Greatly simplifying some of the code by using string_join()
47 * Revision 1.28 2002/04/26 12:53:15 oes
48 * - CGI AF editor now writes action lines split into
49 * single lines with line continuation
50 * - actions_to_html now embeds each action name in
52 * - current_action_to_text is now called current_action_to_html
53 * and acts like actions_to_html
55 * Revision 1.27 2002/04/24 02:10:31 oes
56 * - Jon's patch for multiple AFs:
57 * - split load_actions_file, add load_one_actions_file
58 * - make csp->actions_list files an array
59 * - remember file id with each action
60 * - Copy_action now frees dest action before copying
62 * Revision 1.26 2002/03/26 22:29:54 swa
63 * we have a new homepage!
65 * Revision 1.25 2002/03/24 13:25:43 swa
66 * name change related issues
68 * Revision 1.24 2002/03/16 23:54:06 jongfoster
69 * Adding graceful termination feature, to help look for memory leaks.
70 * If you enable this (which, by design, has to be done by hand
71 * editing config.h) and then go to http://i.j.b/die, then the program
72 * will exit cleanly after the *next* request. It should free all the
73 * memory that was used.
75 * Revision 1.23 2002/03/07 03:46:16 oes
76 * Fixed compiler warnings
78 * Revision 1.22 2002/01/21 00:27:02 jongfoster
79 * Allowing free_action(NULL).
80 * Moving the functions that #include actionlist.h to the end of the file,
81 * because the Visual C++ 97 debugger gets extremely confused if you try
82 * to debug any code that comes after them in the file.
84 * Revision 1.21 2002/01/17 20:54:44 jongfoster
85 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
87 * Revision 1.20 2001/11/22 21:56:49 jongfoster
88 * Making action_spec->flags into an unsigned long rather than just an
90 * Fixing a bug in the display of -add-header and -wafer
92 * Revision 1.19 2001/11/13 00:14:07 jongfoster
93 * Fixing stupid bug now I've figured out what || means.
94 * (It always returns 0 or 1, not one of it's paramaters.)
96 * Revision 1.18 2001/11/07 00:06:06 steudten
97 * Add line number in error output for lineparsing for
100 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
101 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
102 * threads to call select() simultaneously. So, it's time to do a real, live,
103 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
104 * (native). Both versions will work, but using __OS2__ offers multi-threading.
106 * Revision 1.16 2001/10/23 21:30:30 jongfoster
107 * Adding error-checking to selected functions.
109 * Revision 1.15 2001/10/14 21:58:22 jongfoster
110 * Adding support for the CGI-based editor:
111 * - Exported get_actions()
112 * - Added new function free_alias_list()
113 * - Added support for {{settings}} and {{description}} blocks
114 * in the actions file. They are currently ignored.
115 * - Added restriction to only one {{alias}} block which must appear
116 * first in the file, to simplify the editor's rewriting rules.
117 * - Note that load_actions_file() is no longer used by the CGI-based
118 * editor, but some of the other routines in this file are.
120 * Revision 1.14 2001/09/22 16:36:59 jongfoster
121 * Removing unused parameter fs from read_config_line()
123 * Revision 1.13 2001/09/16 15:47:37 jongfoster
124 * First version of CGI-based edit interface. This is very much a
125 * work-in-progress, and you can't actually use it to edit anything
126 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
127 * to have any effect.
129 * Revision 1.12 2001/09/16 13:21:27 jongfoster
130 * Changes to use new list functions.
132 * Revision 1.11 2001/09/14 00:17:32 jongfoster
133 * Tidying up memory allocation. New function init_action().
135 * Revision 1.10 2001/09/10 10:14:34 oes
136 * Removing unused variable
138 * Revision 1.9 2001/07/30 22:08:36 jongfoster
139 * Tidying up #defines:
140 * - All feature #defines are now of the form FEATURE_xxx
141 * - Permanently turned off WIN_GUI_EDIT
142 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
144 * Revision 1.8 2001/06/29 13:19:52 oes
145 * Removed logentry from cancelled commit
147 * Revision 1.7 2001/06/09 10:55:28 jongfoster
148 * Changing BUFSIZ ==> BUFFER_SIZE
150 * Revision 1.6 2001/06/07 23:04:34 jongfoster
151 * Made get_actions() static.
153 * Revision 1.5 2001/06/03 19:11:48 oes
154 * adapted to new enlist_unique arg format
156 * Revision 1.4 2001/06/01 20:03:42 jongfoster
157 * Better memory management - current_action->strings[] now
158 * contains copies of the strings, not the original.
160 * Revision 1.3 2001/06/01 18:49:17 jongfoster
161 * Replaced "list_share" with "list" - the tiny memory gain was not
162 * worth the extra complexity.
164 * Revision 1.2 2001/05/31 21:40:00 jongfoster
165 * Removing some commented out, obsolete blocks of code.
167 * Revision 1.1 2001/05/31 21:16:46 jongfoster
168 * Moved functions to process the action list into this new file.
171 *********************************************************************/
185 #include "miscutil.h"
189 #include "urlmatch.h"
192 const char actions_h_rcs[] = ACTIONS_H_VERSION;
196 * We need the main list of options.
198 * First, we need a way to tell between boolean, string, and multi-string
199 * options. For string and multistring options, we also need to be
200 * able to tell the difference between a "+" and a "-". (For bools,
201 * the "+"/"-" information is encoded in "add" and "mask"). So we use
202 * an enumerated type (well, the preprocessor equivalent). Here are
205 #define AV_NONE 0 /* +opt -opt */
206 #define AV_ADD_STRING 1 /* +stropt{string} */
207 #define AV_REM_STRING 2 /* -stropt */
208 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
209 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt */
212 * We need a structure to hold the name, flag changes,
213 * type, and string index.
218 unsigned mask; /* a bit set to "0" = remove action */
219 unsigned add; /* a bit set to "1" = add action */
220 int takes_value; /* an AV_... constant */
221 int index; /* index into strings[] or multi[] */
225 * And with those building blocks in place, here's the array.
227 static const struct action_name action_names[] =
230 * Well actually there's no data here - it's in actionlist.h
231 * This keeps it together to make it easy to change.
233 * Here's the macros used to format it:
235 #define DEFINE_ACTION_MULTI(name,index) \
236 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
237 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
238 #define DEFINE_ACTION_STRING(name,flag,index) \
239 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
240 { "-" name, ~flag, 0, AV_REM_STRING, index },
241 #define DEFINE_ACTION_BOOL(name,flag) \
242 { "+" name, ACTION_MASK_ALL, flag }, \
243 { "-" name, ~flag, 0 },
244 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
246 #include "actionlist.h"
248 #undef DEFINE_ACTION_MULTI
249 #undef DEFINE_ACTION_STRING
250 #undef DEFINE_ACTION_BOOL
251 #undef DEFINE_ACTION_ALIAS
253 { NULL, 0, 0 } /* End marker */
257 static int load_one_actions_file(struct client_state *csp, int fileid);
260 /*********************************************************************
262 * Function : merge_actions
264 * Description : Merge two actions together.
265 * Similar to "cur_action += new_action".
268 * 1 : cur_action = Current actions, to modify.
269 * 2 : new_action = Action to add.
271 * Returns : JB_ERR_OK or JB_ERR_MEMORY
273 *********************************************************************/
274 jb_err merge_actions (struct action_spec *dest,
275 const struct action_spec *src)
280 dest->mask &= src->mask;
281 dest->add &= src->mask;
282 dest->add |= src->add;
284 for (i = 0; i < ACTION_STRING_COUNT; i++)
286 char * str = src->string[i];
289 freez(dest->string[i]);
290 dest->string[i] = strdup(str);
291 if (NULL == dest->string[i])
293 return JB_ERR_MEMORY;
298 for (i = 0; i < ACTION_MULTI_COUNT; i++)
300 if (src->multi_remove_all[i])
302 /* Remove everything from dest */
303 list_remove_all(dest->multi_remove[i]);
304 dest->multi_remove_all[i] = 1;
306 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
308 else if (dest->multi_remove_all[i])
311 * dest already removes everything, so we only need to worry
314 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
315 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
319 /* No "remove all"s to worry about. */
320 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
321 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
322 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
335 /*********************************************************************
337 * Function : copy_action
339 * Description : Copy an action_specs.
340 * Similar to "cur_action = new_action".
343 * 1 : dest = Destination of copy.
344 * 2 : src = Source for copy.
348 *********************************************************************/
349 jb_err copy_action (struct action_spec *dest,
350 const struct action_spec *src)
353 jb_err err = JB_ERR_OK;
356 memset(dest, '\0', sizeof(*dest));
358 dest->mask = src->mask;
359 dest->add = src->add;
361 for (i = 0; i < ACTION_STRING_COUNT; i++)
363 char * str = src->string[i];
369 return JB_ERR_MEMORY;
371 dest->string[i] = str;
375 for (i = 0; i < ACTION_MULTI_COUNT; i++)
377 dest->multi_remove_all[i] = src->multi_remove_all[i];
378 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
383 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
393 /*********************************************************************
395 * Function : free_action
397 * Description : Destroy an action_spec. Frees memory used by it,
398 * except for the memory used by the struct action_spec
402 * 1 : src = Source to free.
406 *********************************************************************/
407 void free_action (struct action_spec *src)
416 for (i = 0; i < ACTION_STRING_COUNT; i++)
418 freez(src->string[i]);
421 for (i = 0; i < ACTION_MULTI_COUNT; i++)
423 destroy_list(src->multi_remove[i]);
424 destroy_list(src->multi_add[i]);
427 memset(src, '\0', sizeof(*src));
431 /*********************************************************************
433 * Function : get_action_token
435 * Description : Parses a line for the first action.
436 * Modifies it's input array, doesn't allocate memory.
438 * *line=" +abc{def} -ghi "
445 * 1 : line = [in] The line containing the action.
446 * [out] Start of next action on line, or
447 * NULL if we reached the end of line before
448 * we found an action.
449 * 2 : name = [out] Start of action name, null
450 * terminated. NULL on EOL
451 * 3 : value = [out] Start of action value, null
452 * terminated. NULL if none or EOL.
454 * Returns : JB_ERR_OK => Ok
455 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
457 *********************************************************************/
458 jb_err get_action_token(char **line, char **name, char **value)
463 /* set default returns */
468 /* Eat any leading whitespace */
469 while ((*str == ' ') || (*str == '\t'))
481 /* null name, just value is prohibited */
488 while (((ch = *str) != '\0') &&
489 (ch != ' ') && (ch != '\t') && (ch != '{'))
493 /* error, '}' without '{' */
505 /* EOL - be careful not to run off buffer */
510 /* More to parse next time. */
519 str = strchr(str, '}');
537 /*********************************************************************
539 * Function : get_actions
541 * Description : Parses a list of actions.
544 * 1 : line = The string containing the actions.
545 * Will be written to by this function.
546 * 2 : aliases = Custom alias list, or NULL for none.
547 * 3 : cur_action = Where to store the action. Caller
550 * Returns : JB_ERR_OK => Ok
551 * JB_ERR_PARSE => Parse error (line was trashed anyway)
552 * nonzero => Out of memory (line was trashed anyway)
554 *********************************************************************/
555 jb_err get_actions(char *line,
556 struct action_alias * alias_list,
557 struct action_spec *cur_action)
560 init_action(cur_action);
561 cur_action->mask = ACTION_MASK_ALL;
565 char * option = NULL;
568 err = get_action_token(&line, &option, &value);
576 /* handle option in 'option' */
578 /* Check for standard action name */
579 const struct action_name * action = action_names;
581 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
585 if (action->name != NULL)
588 cur_action->mask &= action->mask;
589 cur_action->add &= action->mask;
590 cur_action->add |= action->add;
592 switch (action->takes_value)
595 /* ignore any option. */
599 /* add single string. */
601 if ((value == NULL) || (*value == '\0'))
605 /* FIXME: should validate option string here */
606 freez (cur_action->string[action->index]);
607 cur_action->string[action->index] = strdup(value);
608 if (NULL == cur_action->string[action->index])
610 return JB_ERR_MEMORY;
616 /* remove single string. */
618 freez (cur_action->string[action->index]);
623 /* append multi string. */
625 struct list * remove_p = cur_action->multi_remove[action->index];
626 struct list * add_p = cur_action->multi_add[action->index];
628 if ((value == NULL) || (*value == '\0'))
633 list_remove_item(remove_p, value);
634 err = enlist_unique(add_p, value, 0);
643 /* remove multi string. */
645 struct list * remove_p = cur_action->multi_remove[action->index];
646 struct list * add_p = cur_action->multi_add[action->index];
648 if ( (value == NULL) || (*value == '\0')
649 || ((*value == '*') && (value[1] == '\0')) )
652 * no option, or option == "*".
656 list_remove_all(remove_p);
657 list_remove_all(add_p);
658 cur_action->multi_remove_all[action->index] = 1;
662 /* Valid option - remove only 1 option */
664 if ( !cur_action->multi_remove_all[action->index] )
666 /* there isn't a catch-all in the remove list already */
667 err = enlist_unique(remove_p, value, 0);
673 list_remove_item(add_p, value);
678 /* Shouldn't get here unless there's memory corruption. */
685 /* try user aliases. */
686 const struct action_alias * alias = alias_list;
688 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
695 merge_actions(cur_action, alias->action);
699 /* Bad action name */
710 /*********************************************************************
712 * Function : init_current_action
714 * Description : Zero out an action.
717 * 1 : dest = An uninitialized current_action_spec.
721 *********************************************************************/
722 void init_current_action (struct current_action_spec *dest)
724 memset(dest, '\0', sizeof(*dest));
726 dest->flags = ACTION_MOST_COMPATIBLE;
730 /*********************************************************************
732 * Function : init_action
734 * Description : Zero out an action.
737 * 1 : dest = An uninitialized action_spec.
741 *********************************************************************/
742 void init_action (struct action_spec *dest)
744 memset(dest, '\0', sizeof(*dest));
748 /*********************************************************************
750 * Function : merge_current_action
752 * Description : Merge two actions together.
753 * Similar to "dest += src".
754 * Differences between this and merge_actions()
755 * is that this one doesn't allocate memory for
756 * strings (so "src" better be in memory for at least
757 * as long as "dest" is, and you'd better free
758 * "dest" using "free_current_action").
759 * Also, there is no mask or remove lists in dest.
760 * (If we're applying it to a URL, we don't need them)
763 * 1 : dest = Current actions, to modify.
764 * 2 : src = Action to add.
766 * Returns 0 : no error
767 * !=0 : error, probably JB_ERR_MEMORY.
769 *********************************************************************/
770 jb_err merge_current_action (struct current_action_spec *dest,
771 const struct action_spec *src)
774 jb_err err = JB_ERR_OK;
776 dest->flags &= src->mask;
777 dest->flags |= src->add;
779 for (i = 0; i < ACTION_STRING_COUNT; i++)
781 char * str = src->string[i];
787 return JB_ERR_MEMORY;
789 freez(dest->string[i]);
790 dest->string[i] = str;
794 for (i = 0; i < ACTION_MULTI_COUNT; i++)
796 if (src->multi_remove_all[i])
798 /* Remove everything from dest, then add src->multi_add */
799 err = list_duplicate(dest->multi[i], src->multi_add[i]);
807 list_remove_list(dest->multi[i], src->multi_remove[i]);
808 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
819 /*********************************************************************
821 * Function : free_current_action
823 * Description : Free memory used by a current_action_spec.
824 * Does not free the current_action_spec itself.
827 * 1 : src = Source to free.
831 *********************************************************************/
832 void free_current_action (struct current_action_spec *src)
836 for (i = 0; i < ACTION_STRING_COUNT; i++)
838 freez(src->string[i]);
841 for (i = 0; i < ACTION_MULTI_COUNT; i++)
843 destroy_list(src->multi[i]);
846 memset(src, '\0', sizeof(*src));
850 static struct file_list *current_actions_file[MAX_ACTION_FILES] = {
851 NULL, NULL, NULL, NULL, NULL,
852 NULL, NULL, NULL, NULL, NULL
856 #ifdef FEATURE_GRACEFUL_TERMINATION
857 /*********************************************************************
859 * Function : unload_current_actions_file
861 * Description : Unloads current actions file - reset to state at
862 * beginning of program.
868 *********************************************************************/
869 void unload_current_actions_file(void)
871 if (current_actions_file)
873 current_actions_file->unloader = unload_actions_file;
874 current_actions_file = NULL;
877 #endif /* FEATURE_GRACEFUL_TERMINATION */
880 /*********************************************************************
882 * Function : unload_actions_file
884 * Description : Unloads an actions module.
887 * 1 : file_data = the data structure associated with the
892 *********************************************************************/
893 void unload_actions_file(void *file_data)
895 struct url_actions * next;
896 struct url_actions * cur = (struct url_actions *)file_data;
900 free_url_spec(cur->url);
901 free_action(cur->action);
909 /*********************************************************************
911 * Function : free_alias_list
913 * Description : Free memory used by a list of aliases.
916 * 1 : alias_list = Linked list to free.
920 *********************************************************************/
921 void free_alias_list(struct action_alias *alias_list)
923 while (alias_list != NULL)
925 struct action_alias * next = alias_list->next;
926 alias_list->next = NULL;
927 freez(alias_list->name);
928 free_action(alias_list->action);
935 /*********************************************************************
937 * Function : load_actions_file
939 * Description : Read and parse all the action files and add to files
943 * 1 : csp = Current client state (buffers, headers, etc...)
945 * Returns : 0 => Ok, everything else is an error.
947 *********************************************************************/
948 int load_actions_file(struct client_state *csp)
953 for (i = 0; i < MAX_ACTION_FILES; i++)
955 if (csp->config->actions_file[i])
957 result = load_one_actions_file(csp, i);
963 else if (current_actions_file[i])
965 current_actions_file[i]->unloader = unload_actions_file;
966 current_actions_file[i] = NULL;
973 /*********************************************************************
975 * Function : load_one_actions_file
977 * Description : Read and parse a action file and add to files
981 * 1 : csp = Current client state (buffers, headers, etc...)
982 * 2 : fileid = File index to load.
984 * Returns : 0 => Ok, everything else is an error.
986 *********************************************************************/
987 static int load_one_actions_file(struct client_state *csp, int fileid)
992 * Note: Keep these in the order they occur in the file, they are
993 * sometimes tested with <=
995 #define MODE_START_OF_FILE 1
996 #define MODE_SETTINGS 2
997 #define MODE_DESCRIPTION 3
999 #define MODE_ACTIONS 5
1001 int mode = MODE_START_OF_FILE;
1004 struct url_actions *last_perm;
1005 struct url_actions *perm;
1006 char buf[BUFFER_SIZE];
1007 struct file_list *fs;
1008 struct action_spec * cur_action = NULL;
1009 int cur_action_used = 0;
1010 struct action_alias * alias_list = NULL;
1011 unsigned long linenum = 0;
1013 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1015 /* No need to load */
1016 csp->actions_list[fileid] = current_actions_file[fileid];
1021 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1022 csp->config->actions_file[fileid]);
1023 return 1; /* never get here */
1026 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1027 if (last_perm == NULL)
1029 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1030 csp->config->actions_file[fileid]);
1031 return 1; /* never get here */
1034 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1036 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1037 csp->config->actions_file[fileid]);
1038 return 1; /* never get here */
1041 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1045 /* It's a header block */
1048 /* It's {{settings}} or {{alias}} */
1049 int len = strlen(buf);
1050 char * start = buf + 2;
1051 char * end = buf + len - 1;
1052 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1056 log_error(LOG_LEVEL_FATAL,
1057 "can't load actions file '%s': invalid line (%lu): %s",
1058 csp->config->actions_file[fileid], linenum, buf);
1059 return 1; /* never get here */
1062 /* Trim leading and trailing whitespace. */
1070 log_error(LOG_LEVEL_FATAL,
1071 "can't load actions file '%s': invalid line (%lu): {{ }}",
1072 csp->config->actions_file[fileid], linenum);
1073 return 1; /* never get here */
1077 * An actionsfile can optionally contain the following blocks.
1078 * They *MUST* be in this order, to simplify processing:
1084 * ...free text, format TBD, but no line may start with a '{'...
1089 * The actual actions must be *after* these special blocks.
1090 * None of these special blocks may be repeated.
1093 if (0 == strcmpic(start, "settings"))
1095 /* it's a {{settings}} block */
1096 if (mode >= MODE_SETTINGS)
1098 /* {{settings}} must be first thing in file and must only
1102 log_error(LOG_LEVEL_FATAL,
1103 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1104 csp->config->actions_file[fileid], linenum);
1106 mode = MODE_SETTINGS;
1108 else if (0 == strcmpic(start, "description"))
1110 /* it's a {{description}} block */
1111 if (mode >= MODE_DESCRIPTION)
1113 /* {{description}} is a singleton and only {{settings}} may proceed it
1116 log_error(LOG_LEVEL_FATAL,
1117 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1118 csp->config->actions_file[fileid], linenum);
1120 mode = MODE_DESCRIPTION;
1122 else if (0 == strcmpic(start, "alias"))
1124 /* it's an {{alias}} block */
1125 if (mode >= MODE_ALIAS)
1127 /* {{alias}} must be first thing in file, possibly after
1128 * {{settings}} and {{description}}
1130 * {{alias}} must only appear once.
1132 * Note that these are new restrictions introduced in
1133 * v2.9.10 in order to make actionsfile editing simpler.
1134 * (Otherwise, reordering actionsfile entries without
1135 * completely rewriting the file becomes non-trivial)
1138 log_error(LOG_LEVEL_FATAL,
1139 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1140 csp->config->actions_file[fileid], linenum);
1146 /* invalid {{something}} block */
1148 log_error(LOG_LEVEL_FATAL,
1149 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1150 csp->config->actions_file[fileid], linenum, start);
1151 return 1; /* never get here */
1156 /* It's an actions block */
1158 char actions_buf[BUFFER_SIZE];
1162 mode = MODE_ACTIONS;
1164 /* free old action */
1167 if (!cur_action_used)
1169 free_action(cur_action);
1174 cur_action_used = 0;
1175 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1176 if (cur_action == NULL)
1179 log_error(LOG_LEVEL_FATAL,
1180 "can't load actions file '%s': out of memory",
1181 csp->config->actions_file[fileid]);
1182 return 1; /* never get here */
1184 init_action(cur_action);
1187 strcpy(actions_buf, buf + 1);
1189 /* check we have a trailing } and then trim it */
1190 end = actions_buf + strlen(actions_buf) - 1;
1195 log_error(LOG_LEVEL_FATAL,
1196 "can't load actions file '%s': invalid line (%lu): %s",
1197 csp->config->actions_file[fileid], linenum, buf);
1198 return 1; /* never get here */
1202 /* trim any whitespace immediately inside {} */
1205 if (get_actions(actions_buf, alias_list, cur_action))
1209 log_error(LOG_LEVEL_FATAL,
1210 "can't load actions file '%s': invalid line (%lu): %s",
1211 csp->config->actions_file[fileid], linenum, buf);
1212 return 1; /* never get here */
1216 else if (mode == MODE_SETTINGS)
1219 * Part of the {{settings}} block.
1220 * Ignore for now, but we may want to read & check permissions
1221 * when we go multi-user.
1224 else if (mode == MODE_DESCRIPTION)
1227 * Part of the {{description}} block.
1231 else if (mode == MODE_ALIAS)
1236 char actions_buf[BUFFER_SIZE];
1237 struct action_alias * new_alias;
1239 char * start = strchr(buf, '=');
1242 if ((start == NULL) || (start == buf))
1244 log_error(LOG_LEVEL_FATAL,
1245 "can't load actions file '%s': invalid alias line (%lu): %s",
1246 csp->config->actions_file[fileid], linenum, buf);
1247 return 1; /* never get here */
1250 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1253 log_error(LOG_LEVEL_FATAL,
1254 "can't load actions file '%s': out of memory!",
1255 csp->config->actions_file[fileid]);
1256 return 1; /* never get here */
1259 /* Eat any the whitespace before the '=' */
1261 while ((*end == ' ') || (*end == '\t'))
1264 * we already know we must have at least 1 non-ws char
1265 * at start of buf - no need to check
1271 /* Eat any the whitespace after the '=' */
1273 while ((*start == ' ') || (*start == '\t'))
1279 log_error(LOG_LEVEL_FATAL,
1280 "can't load actions file '%s': invalid alias line (%lu): %s",
1281 csp->config->actions_file[fileid], linenum, buf);
1282 return 1; /* never get here */
1285 new_alias->name = strdup(buf);
1287 strcpy(actions_buf, start);
1289 if (get_actions(actions_buf, alias_list, new_alias->action))
1293 log_error(LOG_LEVEL_FATAL,
1294 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1295 csp->config->actions_file[fileid], linenum, buf, start);
1296 return 1; /* never get here */
1300 new_alias->next = alias_list;
1301 alias_list = new_alias;
1303 else if (mode == MODE_ACTIONS)
1305 /* it's a URL pattern */
1307 /* allocate a new node */
1308 if ((perm = zalloc(sizeof(*perm))) == NULL)
1311 log_error(LOG_LEVEL_FATAL,
1312 "can't load actions file '%s': out of memory!",
1313 csp->config->actions_file[fileid]);
1314 return 1; /* never get here */
1318 copy_action (perm->action, cur_action);
1320 /* Save the URL pattern */
1321 if (create_url_spec(perm->url, buf))
1324 log_error(LOG_LEVEL_FATAL,
1325 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1326 csp->config->actions_file[fileid], linenum, buf);
1327 return 1; /* never get here */
1330 /* add it to the list */
1331 last_perm->next = perm;
1334 else if (mode == MODE_START_OF_FILE)
1336 /* oops - please have a {} line as 1st line in file. */
1338 log_error(LOG_LEVEL_FATAL,
1339 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1340 csp->config->actions_file[fileid], linenum, buf);
1341 return 1; /* never get here */
1345 /* How did we get here? This is impossible! */
1347 log_error(LOG_LEVEL_FATAL,
1348 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1349 csp->config->actions_file[fileid], mode);
1350 return 1; /* never get here */
1356 free_action(cur_action);
1358 free_alias_list(alias_list);
1360 /* the old one is now obsolete */
1361 if (current_actions_file[fileid])
1363 current_actions_file[fileid]->unloader = unload_actions_file;
1366 fs->next = files->next;
1368 current_actions_file[fileid] = fs;
1370 csp->actions_list[fileid] = fs;
1377 /*********************************************************************
1379 * Function : actions_to_text
1381 * Description : Converts a actionsfile entry from numeric form
1382 * ("mask" and "add") to a text line which is split
1383 * into one line for each action with line continuation.
1386 * 1 : mask = As from struct url_actions
1387 * 2 : add = As from struct url_actions
1389 * Returns : A string. Caller must free it.
1390 * NULL on out-of-memory error.
1392 *********************************************************************/
1393 char * actions_to_text(struct action_spec *action)
1395 unsigned mask = action->mask;
1396 unsigned add = action->add;
1397 char * result = strdup("");
1398 struct list_entry * lst;
1400 /* sanity - prevents "-feature +feature" */
1404 #define DEFINE_ACTION_BOOL(__name, __bit) \
1405 if (!(mask & __bit)) \
1407 string_append(&result, " -" __name " \\\n"); \
1409 else if (add & __bit) \
1411 string_append(&result, " +" __name " \\\n"); \
1414 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1415 if (!(mask & __bit)) \
1417 string_append(&result, " -" __name " \\\n"); \
1419 else if (add & __bit) \
1421 string_append(&result, " +" __name "{"); \
1422 string_append(&result, action->string[__index]); \
1423 string_append(&result, "} \\\n"); \
1426 #define DEFINE_ACTION_MULTI(__name, __index) \
1427 if (action->multi_remove_all[__index]) \
1429 string_append(&result, " -" __name " \\\n"); \
1433 lst = action->multi_remove[__index]->first; \
1436 string_append(&result, " -" __name "{"); \
1437 string_append(&result, lst->str); \
1438 string_append(&result, "} \\\n"); \
1442 lst = action->multi_add[__index]->first; \
1445 string_append(&result, " +" __name "{"); \
1446 string_append(&result, lst->str); \
1447 string_append(&result, "} \\\n"); \
1451 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1453 #include "actionlist.h"
1455 #undef DEFINE_ACTION_MULTI
1456 #undef DEFINE_ACTION_STRING
1457 #undef DEFINE_ACTION_BOOL
1458 #undef DEFINE_ACTION_ALIAS
1464 /*********************************************************************
1466 * Function : actions_to_html
1468 * Description : Converts a actionsfile entry from numeric form
1469 * ("mask" and "add") to a <br>-seperated HTML string
1470 * in which each action is linked to its chapter in
1474 * 1 : csp = Client state (for config)
1475 * 2 : action = Action spec to be converted
1477 * Returns : A string. Caller must free it.
1478 * NULL on out-of-memory error.
1480 *********************************************************************/
1481 char * actions_to_html(struct client_state *csp,
1482 struct action_spec *action)
1484 unsigned mask = action->mask;
1485 unsigned add = action->add;
1486 char * result = strdup("");
1487 struct list_entry * lst;
1489 /* sanity - prevents "-feature +feature" */
1493 #define DEFINE_ACTION_BOOL(__name, __bit) \
1494 if (!(mask & __bit)) \
1496 string_append(&result, "\n<br>-"); \
1497 string_join(&result, add_help_link(__name, csp->config)); \
1499 else if (add & __bit) \
1501 string_append(&result, "\n<br>+"); \
1502 string_join(&result, add_help_link(__name, csp->config)); \
1505 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1506 if (!(mask & __bit)) \
1508 string_append(&result, "\n<br>-"); \
1509 string_join(&result, add_help_link(__name, csp->config)); \
1511 else if (add & __bit) \
1513 string_append(&result, "\n<br>+"); \
1514 string_join(&result, add_help_link(__name, csp->config)); \
1515 string_append(&result, "{"); \
1516 string_join(&result, html_encode(action->string[__index])); \
1517 string_append(&result, "}"); \
1520 #define DEFINE_ACTION_MULTI(__name, __index) \
1521 if (action->multi_remove_all[__index]) \
1523 string_append(&result, "\n<br>-"); \
1524 string_join(&result, add_help_link(__name, csp->config)); \
1528 lst = action->multi_remove[__index]->first; \
1531 string_append(&result, "\n<br>-"); \
1532 string_join(&result, add_help_link(__name, csp->config)); \
1533 string_append(&result, "{"); \
1534 string_join(&result, html_encode(lst->str)); \
1535 string_append(&result, "}"); \
1539 lst = action->multi_add[__index]->first; \
1542 string_append(&result, "\n<br>+"); \
1543 string_join(&result, add_help_link(__name, csp->config)); \
1544 string_append(&result, "{"); \
1545 string_join(&result, html_encode(lst->str)); \
1546 string_append(&result, "}"); \
1550 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1552 #include "actionlist.h"
1554 #undef DEFINE_ACTION_MULTI
1555 #undef DEFINE_ACTION_STRING
1556 #undef DEFINE_ACTION_BOOL
1557 #undef DEFINE_ACTION_ALIAS
1559 /* trim leading <br> */
1560 if (result && *result)
1563 result = strdup(result + 5);
1571 /*********************************************************************
1573 * Function : current_actions_to_html
1575 * Description : Converts a curren action spec to a <br> seperated HTML
1576 * text in which each action is linked to its chapter in
1580 * 1 : csp = Client state (for config)
1581 * 2 : action = Current action spec to be converted
1583 * Returns : A string. Caller must free it.
1584 * NULL on out-of-memory error.
1586 *********************************************************************/
1587 char *current_action_to_html(struct client_state *csp,
1588 struct current_action_spec *action)
1590 unsigned long flags = action->flags;
1591 char * result = strdup("");
1592 struct list_entry * lst;
1594 #define DEFINE_ACTION_BOOL(__name, __bit) \
1595 if (flags & __bit) \
1597 string_append(&result, "\n<br>+"); \
1598 string_join(&result, add_help_link(__name, csp->config)); \
1602 string_append(&result, "\n<br>-"); \
1603 string_join(&result, add_help_link(__name, csp->config)); \
1606 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1607 if (flags & __bit) \
1609 string_append(&result, "\n<br>+"); \
1610 string_join(&result, add_help_link(__name, csp->config)); \
1611 string_append(&result, "{"); \
1612 string_join(&result, html_encode(action->string[__index])); \
1613 string_append(&result, "}"); \
1617 string_append(&result, "\n<br>-"); \
1618 string_join(&result, add_help_link(__name, csp->config)); \
1621 #define DEFINE_ACTION_MULTI(__name, __index) \
1622 lst = action->multi[__index]->first; \
1625 string_append(&result, "\n<br>-"); \
1626 string_join(&result, add_help_link(__name, csp->config)); \
1632 string_append(&result, "\n<br>+"); \
1633 string_join(&result, add_help_link(__name, csp->config)); \
1634 string_append(&result, "{"); \
1635 string_join(&result, html_encode(lst->str)); \
1636 string_append(&result, "}"); \
1641 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1643 #include "actionlist.h"
1645 #undef DEFINE_ACTION_MULTI
1646 #undef DEFINE_ACTION_STRING
1647 #undef DEFINE_ACTION_BOOL
1648 #undef DEFINE_ACTION_ALIAS