1 const char actions_rcs[] = "$Id: actions.c,v 2.0 2002/06/04 14:34:21 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/src/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 2.0 2002/06/04 14:34:21 jongfoster
37 * Moving source files to src/
39 * Revision 1.33 2002/05/26 12:14:19 roro
40 * Change unsigned to unsigned long in actions_name struct. This closes
41 * SourceForge Bug #539284.
43 * Revision 1.32 2002/05/12 21:36:29 jongfoster
44 * Correcting function comments
46 * Revision 1.31 2002/05/06 07:56:50 oes
47 * Made actions_to_html independent of FEATURE_CGI_EDIT_ACTIONS
49 * Revision 1.30 2002/04/30 11:14:52 oes
50 * Made csp the first parameter in *action_to_html
52 * Revision 1.29 2002/04/26 19:30:54 jongfoster
53 * - current_action_to_html(): Adding help link for the "-" form of
55 * - Some actions had "<br>-", some "<br> -" (note the space).
56 * Standardizing on no space.
57 * - Greatly simplifying some of the code by using string_join()
60 * Revision 1.28 2002/04/26 12:53:15 oes
61 * - CGI AF editor now writes action lines split into
62 * single lines with line continuation
63 * - actions_to_html now embeds each action name in
65 * - current_action_to_text is now called current_action_to_html
66 * and acts like actions_to_html
68 * Revision 1.27 2002/04/24 02:10:31 oes
69 * - Jon's patch for multiple AFs:
70 * - split load_actions_file, add load_one_actions_file
71 * - make csp->actions_list files an array
72 * - remember file id with each action
73 * - Copy_action now frees dest action before copying
75 * Revision 1.26 2002/03/26 22:29:54 swa
76 * we have a new homepage!
78 * Revision 1.25 2002/03/24 13:25:43 swa
79 * name change related issues
81 * Revision 1.24 2002/03/16 23:54:06 jongfoster
82 * Adding graceful termination feature, to help look for memory leaks.
83 * If you enable this (which, by design, has to be done by hand
84 * editing config.h) and then go to http://i.j.b/die, then the program
85 * will exit cleanly after the *next* request. It should free all the
86 * memory that was used.
88 * Revision 1.23 2002/03/07 03:46:16 oes
89 * Fixed compiler warnings
91 * Revision 1.22 2002/01/21 00:27:02 jongfoster
92 * Allowing free_action(NULL).
93 * Moving the functions that #include actionlist.h to the end of the file,
94 * because the Visual C++ 97 debugger gets extremely confused if you try
95 * to debug any code that comes after them in the file.
97 * Revision 1.21 2002/01/17 20:54:44 jongfoster
98 * Renaming free_url to free_url_spec, since it frees a struct url_spec.
100 * Revision 1.20 2001/11/22 21:56:49 jongfoster
101 * Making action_spec->flags into an unsigned long rather than just an
103 * Fixing a bug in the display of -add-header and -wafer
105 * Revision 1.19 2001/11/13 00:14:07 jongfoster
106 * Fixing stupid bug now I've figured out what || means.
107 * (It always returns 0 or 1, not one of it's paramaters.)
109 * Revision 1.18 2001/11/07 00:06:06 steudten
110 * Add line number in error output for lineparsing for
113 * Revision 1.17 2001/10/25 03:40:47 david__schmidt
114 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
115 * threads to call select() simultaneously. So, it's time to do a real, live,
116 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
117 * (native). Both versions will work, but using __OS2__ offers multi-threading.
119 * Revision 1.16 2001/10/23 21:30:30 jongfoster
120 * Adding error-checking to selected functions.
122 * Revision 1.15 2001/10/14 21:58:22 jongfoster
123 * Adding support for the CGI-based editor:
124 * - Exported get_actions()
125 * - Added new function free_alias_list()
126 * - Added support for {{settings}} and {{description}} blocks
127 * in the actions file. They are currently ignored.
128 * - Added restriction to only one {{alias}} block which must appear
129 * first in the file, to simplify the editor's rewriting rules.
130 * - Note that load_actions_file() is no longer used by the CGI-based
131 * editor, but some of the other routines in this file are.
133 * Revision 1.14 2001/09/22 16:36:59 jongfoster
134 * Removing unused parameter fs from read_config_line()
136 * Revision 1.13 2001/09/16 15:47:37 jongfoster
137 * First version of CGI-based edit interface. This is very much a
138 * work-in-progress, and you can't actually use it to edit anything
139 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
140 * to have any effect.
142 * Revision 1.12 2001/09/16 13:21:27 jongfoster
143 * Changes to use new list functions.
145 * Revision 1.11 2001/09/14 00:17:32 jongfoster
146 * Tidying up memory allocation. New function init_action().
148 * Revision 1.10 2001/09/10 10:14:34 oes
149 * Removing unused variable
151 * Revision 1.9 2001/07/30 22:08:36 jongfoster
152 * Tidying up #defines:
153 * - All feature #defines are now of the form FEATURE_xxx
154 * - Permanently turned off WIN_GUI_EDIT
155 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
157 * Revision 1.8 2001/06/29 13:19:52 oes
158 * Removed logentry from cancelled commit
160 * Revision 1.7 2001/06/09 10:55:28 jongfoster
161 * Changing BUFSIZ ==> BUFFER_SIZE
163 * Revision 1.6 2001/06/07 23:04:34 jongfoster
164 * Made get_actions() static.
166 * Revision 1.5 2001/06/03 19:11:48 oes
167 * adapted to new enlist_unique arg format
169 * Revision 1.4 2001/06/01 20:03:42 jongfoster
170 * Better memory management - current_action->strings[] now
171 * contains copies of the strings, not the original.
173 * Revision 1.3 2001/06/01 18:49:17 jongfoster
174 * Replaced "list_share" with "list" - the tiny memory gain was not
175 * worth the extra complexity.
177 * Revision 1.2 2001/05/31 21:40:00 jongfoster
178 * Removing some commented out, obsolete blocks of code.
180 * Revision 1.1 2001/05/31 21:16:46 jongfoster
181 * Moved functions to process the action list into this new file.
184 *********************************************************************/
198 #include "miscutil.h"
202 #include "urlmatch.h"
205 const char actions_h_rcs[] = ACTIONS_H_VERSION;
209 * We need the main list of options.
211 * First, we need a way to tell between boolean, string, and multi-string
212 * options. For string and multistring options, we also need to be
213 * able to tell the difference between a "+" and a "-". (For bools,
214 * the "+"/"-" information is encoded in "add" and "mask"). So we use
215 * an enumerated type (well, the preprocessor equivalent). Here are
218 #define AV_NONE 0 /**< Action type: +bool-action or -bool-action */
219 #define AV_ADD_STRING 1 /**< Action type: +string-action{string} */
220 #define AV_REM_STRING 2 /**< Action type: -string-action */
221 #define AV_ADD_MULTI 3 /**< Action type: +multi-action{string} */
222 #define AV_REM_MULTI 4 /**< Action type: -multi-action{string} or -multi-action */
225 * A structure holding information about a single built-in action string.
229 const char * name; /**< Action name */
230 unsigned long mask; /**< A bit set to "0" = remove action */
231 unsigned long add; /**< A bit set to "1" = add action */
232 int takes_value; /**< An AV_... constant */
233 int index; /**< Index into strings[] or multi[] */
237 * The array of all built-in action strings.
239 static const struct action_name action_names[] =
242 * Well actually there's no data here - it's in actionlist.h
243 * This keeps it together to make it easy to change.
245 * Here's the macros used to format it:
247 #define DEFINE_ACTION_MULTI(name,index) \
248 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
249 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
250 #define DEFINE_ACTION_STRING(name,flag,index) \
251 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
252 { "-" name, ~flag, 0, AV_REM_STRING, index },
253 #define DEFINE_ACTION_BOOL(name,flag) \
254 { "+" name, ACTION_MASK_ALL, flag }, \
255 { "-" name, ~flag, 0 },
256 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
258 #include "actionlist.h"
260 #undef DEFINE_ACTION_MULTI
261 #undef DEFINE_ACTION_STRING
262 #undef DEFINE_ACTION_BOOL
263 #undef DEFINE_ACTION_ALIAS
265 { NULL, 0, 0 } /* End marker */
270 * The currently loaded actions files.
272 static struct file_list *current_actions_file[MAX_ACTION_FILES] = {
273 NULL, NULL, NULL, NULL, NULL,
274 NULL, NULL, NULL, NULL, NULL
278 static int load_one_actions_file(struct client_state *csp, int fileid);
281 /*********************************************************************
283 * Function : merge_actions
285 * Description : Merge two actions together.
286 * Similar to "dest += src".
289 * 1 : dest = Actions to modify.
290 * 2 : src = Action to add.
292 * Returns : JB_ERR_OK or JB_ERR_MEMORY
294 *********************************************************************/
295 jb_err merge_actions (struct action_spec *dest,
296 const struct action_spec *src)
301 dest->mask &= src->mask;
302 dest->add &= src->mask;
303 dest->add |= src->add;
305 for (i = 0; i < ACTION_STRING_COUNT; i++)
307 char * str = src->string[i];
310 freez(dest->string[i]);
311 dest->string[i] = strdup(str);
312 if (NULL == dest->string[i])
314 return JB_ERR_MEMORY;
319 for (i = 0; i < ACTION_MULTI_COUNT; i++)
321 if (src->multi_remove_all[i])
323 /* Remove everything from dest */
324 list_remove_all(dest->multi_remove[i]);
325 dest->multi_remove_all[i] = 1;
327 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
329 else if (dest->multi_remove_all[i])
332 * dest already removes everything, so we only need to worry
335 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
336 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
340 /* No "remove all"s to worry about. */
341 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
342 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
343 if (!err) err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
356 /*********************************************************************
358 * Function : copy_action
360 * Description : Copy an action_specs.
361 * Similar to "dest = src".
364 * 1 : dest = Destination of copy.
365 * 2 : src = Source for copy.
369 *********************************************************************/
370 jb_err copy_action (struct action_spec *dest,
371 const struct action_spec *src)
374 jb_err err = JB_ERR_OK;
377 memset(dest, '\0', sizeof(*dest));
379 dest->mask = src->mask;
380 dest->add = src->add;
382 for (i = 0; i < ACTION_STRING_COUNT; i++)
384 char * str = src->string[i];
390 return JB_ERR_MEMORY;
392 dest->string[i] = str;
396 for (i = 0; i < ACTION_MULTI_COUNT; i++)
398 dest->multi_remove_all[i] = src->multi_remove_all[i];
399 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
404 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
414 /*********************************************************************
416 * Function : free_action
418 * Description : Destroy an action_spec. Frees memory used by it,
419 * except for the memory used by the struct action_spec
423 * 1 : src = Source to free.
427 *********************************************************************/
428 void free_action (struct action_spec *src)
437 for (i = 0; i < ACTION_STRING_COUNT; i++)
439 freez(src->string[i]);
442 for (i = 0; i < ACTION_MULTI_COUNT; i++)
444 destroy_list(src->multi_remove[i]);
445 destroy_list(src->multi_add[i]);
448 memset(src, '\0', sizeof(*src));
452 /*********************************************************************
454 * Function : get_action_token
456 * Description : Parses a line for the first action.
457 * Modifies it's input array, doesn't allocate memory.
459 * *line=" +abc{def} -ghi "
466 * 1 : line = [in] The line containing the action.
467 * [out] Start of next action on line, or
468 * NULL if we reached the end of line before
469 * we found an action.
470 * 2 : name = [out] Start of action name, null
471 * terminated. NULL on EOL
472 * 3 : value = [out] Start of action value, null
473 * terminated. NULL if none or EOL.
475 * Returns : JB_ERR_OK => Ok
476 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
478 *********************************************************************/
479 jb_err get_action_token(char **line, char **name, char **value)
484 /* set default returns */
489 /* Eat any leading whitespace */
490 while ((*str == ' ') || (*str == '\t'))
502 /* null name, just value is prohibited */
509 while (((ch = *str) != '\0') &&
510 (ch != ' ') && (ch != '\t') && (ch != '{'))
514 /* error, '}' without '{' */
526 /* EOL - be careful not to run off buffer */
531 /* More to parse next time. */
540 str = strchr(str, '}');
558 /*********************************************************************
560 * Function : get_actions
562 * Description : Parses a list of actions.
565 * 1 : line = The string containing the actions.
566 * Will be written to by this function.
567 * 2 : alias_list = Custom alias list, or NULL for none.
568 * 3 : cur_action = Where to store the action. Caller
571 * Returns : JB_ERR_OK => Ok
572 * JB_ERR_PARSE => Parse error (line was trashed anyway)
573 * nonzero => Out of memory (line was trashed anyway)
575 *********************************************************************/
576 jb_err get_actions(char *line,
577 struct action_alias * alias_list,
578 struct action_spec *cur_action)
581 init_action(cur_action);
582 cur_action->mask = ACTION_MASK_ALL;
586 char * option = NULL;
589 err = get_action_token(&line, &option, &value);
597 /* handle option in 'option' */
599 /* Check for standard action name */
600 const struct action_name * action = action_names;
602 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
606 if (action->name != NULL)
609 cur_action->mask &= action->mask;
610 cur_action->add &= action->mask;
611 cur_action->add |= action->add;
613 switch (action->takes_value)
616 /* ignore any option. */
620 /* add single string. */
622 if ((value == NULL) || (*value == '\0'))
626 /* FIXME: should validate option string here */
627 freez (cur_action->string[action->index]);
628 cur_action->string[action->index] = strdup(value);
629 if (NULL == cur_action->string[action->index])
631 return JB_ERR_MEMORY;
637 /* remove single string. */
639 freez (cur_action->string[action->index]);
644 /* append multi string. */
646 struct list * remove_p = cur_action->multi_remove[action->index];
647 struct list * add_p = cur_action->multi_add[action->index];
649 if ((value == NULL) || (*value == '\0'))
654 list_remove_item(remove_p, value);
655 err = enlist_unique(add_p, value, 0);
664 /* remove multi string. */
666 struct list * remove_p = cur_action->multi_remove[action->index];
667 struct list * add_p = cur_action->multi_add[action->index];
669 if ( (value == NULL) || (*value == '\0')
670 || ((*value == '*') && (value[1] == '\0')) )
673 * no option, or option == "*".
677 list_remove_all(remove_p);
678 list_remove_all(add_p);
679 cur_action->multi_remove_all[action->index] = 1;
683 /* Valid option - remove only 1 option */
685 if ( !cur_action->multi_remove_all[action->index] )
687 /* there isn't a catch-all in the remove list already */
688 err = enlist_unique(remove_p, value, 0);
694 list_remove_item(add_p, value);
699 /* Shouldn't get here unless there's memory corruption. */
706 /* try user aliases. */
707 const struct action_alias * alias = alias_list;
709 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
716 merge_actions(cur_action, alias->action);
720 /* Bad action name */
731 /*********************************************************************
733 * Function : init_current_action
735 * Description : Zero out an action.
738 * 1 : dest = An uninitialized current_action_spec.
742 *********************************************************************/
743 void init_current_action (struct current_action_spec *dest)
745 memset(dest, '\0', sizeof(*dest));
747 dest->flags = ACTION_MOST_COMPATIBLE;
751 /*********************************************************************
753 * Function : init_action
755 * Description : Zero out an action.
758 * 1 : dest = An uninitialized action_spec.
762 *********************************************************************/
763 void init_action (struct action_spec *dest)
765 memset(dest, '\0', sizeof(*dest));
769 /*********************************************************************
771 * Function : merge_current_action
773 * Description : Merge two actions together.
774 * Similar to "dest += src".
775 * Differences between this and merge_actions()
776 * is that this one doesn't allocate memory for
777 * strings (so "src" better be in memory for at least
778 * as long as "dest" is, and you'd better free
779 * "dest" using "free_current_action").
780 * Also, there is no mask or remove lists in dest.
781 * (If we're applying it to a URL, we don't need them)
784 * 1 : dest = Current actions, to modify.
785 * 2 : src = Action to add.
787 * Returns 0 : no error
788 * !=0 : error, probably JB_ERR_MEMORY.
790 *********************************************************************/
791 jb_err merge_current_action (struct current_action_spec *dest,
792 const struct action_spec *src)
795 jb_err err = JB_ERR_OK;
797 dest->flags &= src->mask;
798 dest->flags |= src->add;
800 for (i = 0; i < ACTION_STRING_COUNT; i++)
802 char * str = src->string[i];
808 return JB_ERR_MEMORY;
810 freez(dest->string[i]);
811 dest->string[i] = str;
815 for (i = 0; i < ACTION_MULTI_COUNT; i++)
817 if (src->multi_remove_all[i])
819 /* Remove everything from dest, then add src->multi_add */
820 err = list_duplicate(dest->multi[i], src->multi_add[i]);
828 list_remove_list(dest->multi[i], src->multi_remove[i]);
829 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
840 /*********************************************************************
842 * Function : free_current_action
844 * Description : Free memory used by a current_action_spec.
845 * Does not free the current_action_spec itself.
848 * 1 : src = Source to free.
852 *********************************************************************/
853 void free_current_action (struct current_action_spec *src)
857 for (i = 0; i < ACTION_STRING_COUNT; i++)
859 freez(src->string[i]);
862 for (i = 0; i < ACTION_MULTI_COUNT; i++)
864 destroy_list(src->multi[i]);
867 memset(src, '\0', sizeof(*src));
871 #ifdef FEATURE_GRACEFUL_TERMINATION
872 /*********************************************************************
874 * Function : unload_current_actions_file
876 * Description : Unloads current actions file - reset to state at
877 * beginning of program.
883 *********************************************************************/
884 void unload_current_actions_file(void)
886 if (current_actions_file)
888 current_actions_file->unloader = unload_actions_file;
889 current_actions_file = NULL;
892 #endif /* FEATURE_GRACEFUL_TERMINATION */
895 /*********************************************************************
897 * Function : unload_actions_file
899 * Description : Unloads an actions module.
902 * 1 : file_data = the data structure associated with the
907 *********************************************************************/
908 void unload_actions_file(void *file_data)
910 struct url_actions * next;
911 struct url_actions * cur = (struct url_actions *)file_data;
915 free_url_spec(cur->url);
916 free_action(cur->action);
924 /*********************************************************************
926 * Function : free_alias_list
928 * Description : Free memory used by a list of aliases.
931 * 1 : alias_list = Linked list to free.
935 *********************************************************************/
936 void free_alias_list(struct action_alias *alias_list)
938 while (alias_list != NULL)
940 struct action_alias * next = alias_list->next;
941 alias_list->next = NULL;
942 freez(alias_list->name);
943 free_action(alias_list->action);
950 /*********************************************************************
952 * Function : load_actions_file
954 * Description : Read and parse all the action files and add to files
958 * 1 : csp = Current client state (buffers, headers, etc...)
960 * Returns : 0 => Ok, everything else is an error.
962 *********************************************************************/
963 int load_actions_file(struct client_state *csp)
968 for (i = 0; i < MAX_ACTION_FILES; i++)
970 if (csp->config->actions_file[i])
972 result = load_one_actions_file(csp, i);
978 else if (current_actions_file[i])
980 current_actions_file[i]->unloader = unload_actions_file;
981 current_actions_file[i] = NULL;
989 /** load_one_actions_file() parser status: At start of file. */
990 #define MODE_START_OF_FILE 1
992 /** load_one_actions_file() parser status: In "{{settings}}" block. */
993 #define MODE_SETTINGS 2
995 /** load_one_actions_file() parser status: In "{{description}}" block. */
996 #define MODE_DESCRIPTION 3
998 /** load_one_actions_file() parser status: In "{{alias}}" block. */
1001 /** load_one_actions_file() parser status: In "{+some-actions}" block. */
1002 #define MODE_ACTIONS 5
1005 /*********************************************************************
1007 * Function : load_one_actions_file
1009 * Description : Read and parse a action file and add to files
1013 * 1 : csp = Current client state (buffers, headers, etc...)
1014 * 2 : fileid = File index to load.
1016 * Returns : 0 => Ok, everything else is an error.
1018 *********************************************************************/
1019 static int load_one_actions_file(struct client_state *csp, int fileid)
1024 * Note: Keep these in the order they occur in the file, they are
1025 * sometimes tested with <=
1028 int mode = MODE_START_OF_FILE;
1031 struct url_actions *last_perm;
1032 struct url_actions *perm;
1033 char buf[BUFFER_SIZE];
1034 struct file_list *fs;
1035 struct action_spec * cur_action = NULL;
1036 int cur_action_used = 0;
1037 struct action_alias * alias_list = NULL;
1038 unsigned long linenum = 0;
1040 if (!check_file_changed(current_actions_file[fileid], csp->config->actions_file[fileid], &fs))
1042 /* No need to load */
1043 csp->actions_list[fileid] = current_actions_file[fileid];
1048 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1049 csp->config->actions_file[fileid]);
1050 return 1; /* never get here */
1053 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1054 if (last_perm == NULL)
1056 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1057 csp->config->actions_file[fileid]);
1058 return 1; /* never get here */
1061 if ((fp = fopen(csp->config->actions_file[fileid], "r")) == NULL)
1063 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1064 csp->config->actions_file[fileid]);
1065 return 1; /* never get here */
1068 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1072 /* It's a header block */
1075 /* It's {{settings}} or {{alias}} */
1076 int len = strlen(buf);
1077 char * start = buf + 2;
1078 char * end = buf + len - 1;
1079 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1083 log_error(LOG_LEVEL_FATAL,
1084 "can't load actions file '%s': invalid line (%lu): %s",
1085 csp->config->actions_file[fileid], linenum, buf);
1086 return 1; /* never get here */
1089 /* Trim leading and trailing whitespace. */
1097 log_error(LOG_LEVEL_FATAL,
1098 "can't load actions file '%s': invalid line (%lu): {{ }}",
1099 csp->config->actions_file[fileid], linenum);
1100 return 1; /* never get here */
1104 * An actionsfile can optionally contain the following blocks.
1105 * They *MUST* be in this order, to simplify processing:
1111 * ...free text, format TBD, but no line may start with a '{'...
1116 * The actual actions must be *after* these special blocks.
1117 * None of these special blocks may be repeated.
1120 if (0 == strcmpic(start, "settings"))
1122 /* it's a {{settings}} block */
1123 if (mode >= MODE_SETTINGS)
1125 /* {{settings}} must be first thing in file and must only
1129 log_error(LOG_LEVEL_FATAL,
1130 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1131 csp->config->actions_file[fileid], linenum);
1133 mode = MODE_SETTINGS;
1135 else if (0 == strcmpic(start, "description"))
1137 /* it's a {{description}} block */
1138 if (mode >= MODE_DESCRIPTION)
1140 /* {{description}} is a singleton and only {{settings}} may proceed it
1143 log_error(LOG_LEVEL_FATAL,
1144 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1145 csp->config->actions_file[fileid], linenum);
1147 mode = MODE_DESCRIPTION;
1149 else if (0 == strcmpic(start, "alias"))
1151 /* it's an {{alias}} block */
1152 if (mode >= MODE_ALIAS)
1154 /* {{alias}} must be first thing in file, possibly after
1155 * {{settings}} and {{description}}
1157 * {{alias}} must only appear once.
1159 * Note that these are new restrictions introduced in
1160 * v2.9.10 in order to make actionsfile editing simpler.
1161 * (Otherwise, reordering actionsfile entries without
1162 * completely rewriting the file becomes non-trivial)
1165 log_error(LOG_LEVEL_FATAL,
1166 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1167 csp->config->actions_file[fileid], linenum);
1173 /* invalid {{something}} block */
1175 log_error(LOG_LEVEL_FATAL,
1176 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1177 csp->config->actions_file[fileid], linenum, start);
1178 return 1; /* never get here */
1183 /* It's an actions block */
1185 char actions_buf[BUFFER_SIZE];
1189 mode = MODE_ACTIONS;
1191 /* free old action */
1194 if (!cur_action_used)
1196 free_action(cur_action);
1201 cur_action_used = 0;
1202 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1203 if (cur_action == NULL)
1206 log_error(LOG_LEVEL_FATAL,
1207 "can't load actions file '%s': out of memory",
1208 csp->config->actions_file[fileid]);
1209 return 1; /* never get here */
1211 init_action(cur_action);
1214 strcpy(actions_buf, buf + 1);
1216 /* check we have a trailing } and then trim it */
1217 end = actions_buf + strlen(actions_buf) - 1;
1222 log_error(LOG_LEVEL_FATAL,
1223 "can't load actions file '%s': invalid line (%lu): %s",
1224 csp->config->actions_file[fileid], linenum, buf);
1225 return 1; /* never get here */
1229 /* trim any whitespace immediately inside {} */
1232 if (get_actions(actions_buf, alias_list, cur_action))
1236 log_error(LOG_LEVEL_FATAL,
1237 "can't load actions file '%s': invalid line (%lu): %s",
1238 csp->config->actions_file[fileid], linenum, buf);
1239 return 1; /* never get here */
1243 else if (mode == MODE_SETTINGS)
1246 * Part of the {{settings}} block.
1247 * Ignore for now, but we may want to read & check permissions
1248 * when we go multi-user.
1251 else if (mode == MODE_DESCRIPTION)
1254 * Part of the {{description}} block.
1258 else if (mode == MODE_ALIAS)
1263 char actions_buf[BUFFER_SIZE];
1264 struct action_alias * new_alias;
1266 char * start = strchr(buf, '=');
1269 if ((start == NULL) || (start == buf))
1271 log_error(LOG_LEVEL_FATAL,
1272 "can't load actions file '%s': invalid alias line (%lu): %s",
1273 csp->config->actions_file[fileid], linenum, buf);
1274 return 1; /* never get here */
1277 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1280 log_error(LOG_LEVEL_FATAL,
1281 "can't load actions file '%s': out of memory!",
1282 csp->config->actions_file[fileid]);
1283 return 1; /* never get here */
1286 /* Eat any the whitespace before the '=' */
1288 while ((*end == ' ') || (*end == '\t'))
1291 * we already know we must have at least 1 non-ws char
1292 * at start of buf - no need to check
1298 /* Eat any the whitespace after the '=' */
1300 while ((*start == ' ') || (*start == '\t'))
1306 log_error(LOG_LEVEL_FATAL,
1307 "can't load actions file '%s': invalid alias line (%lu): %s",
1308 csp->config->actions_file[fileid], linenum, buf);
1309 return 1; /* never get here */
1312 new_alias->name = strdup(buf);
1314 strcpy(actions_buf, start);
1316 if (get_actions(actions_buf, alias_list, new_alias->action))
1320 log_error(LOG_LEVEL_FATAL,
1321 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1322 csp->config->actions_file[fileid], linenum, buf, start);
1323 return 1; /* never get here */
1327 new_alias->next = alias_list;
1328 alias_list = new_alias;
1330 else if (mode == MODE_ACTIONS)
1332 /* it's a URL pattern */
1334 /* allocate a new node */
1335 if ((perm = zalloc(sizeof(*perm))) == NULL)
1338 log_error(LOG_LEVEL_FATAL,
1339 "can't load actions file '%s': out of memory!",
1340 csp->config->actions_file[fileid]);
1341 return 1; /* never get here */
1345 copy_action (perm->action, cur_action);
1347 /* Save the URL pattern */
1348 if (create_url_spec(perm->url, buf))
1351 log_error(LOG_LEVEL_FATAL,
1352 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1353 csp->config->actions_file[fileid], linenum, buf);
1354 return 1; /* never get here */
1357 /* add it to the list */
1358 last_perm->next = perm;
1361 else if (mode == MODE_START_OF_FILE)
1363 /* oops - please have a {} line as 1st line in file. */
1365 log_error(LOG_LEVEL_FATAL,
1366 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1367 csp->config->actions_file[fileid], linenum, buf);
1368 return 1; /* never get here */
1372 /* How did we get here? This is impossible! */
1374 log_error(LOG_LEVEL_FATAL,
1375 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1376 csp->config->actions_file[fileid], mode);
1377 return 1; /* never get here */
1383 free_action(cur_action);
1385 free_alias_list(alias_list);
1387 /* the old one is now obsolete */
1388 if (current_actions_file[fileid])
1390 current_actions_file[fileid]->unloader = unload_actions_file;
1393 fs->next = files->next;
1395 current_actions_file[fileid] = fs;
1397 csp->actions_list[fileid] = fs;
1404 /*********************************************************************
1406 * Function : actions_to_text
1408 * Description : Converts a actionsfile entry from the internal
1409 * structurt into a text line. The output is split
1410 * into one line for each action with line continuation.
1413 * 1 : action = The action to format.
1415 * Returns : A string. Caller must free it.
1416 * NULL on out-of-memory error.
1418 *********************************************************************/
1419 char * actions_to_text(struct action_spec *action)
1421 unsigned mask = action->mask;
1422 unsigned add = action->add;
1423 char * result = strdup("");
1424 struct list_entry * lst;
1426 /* sanity - prevents "-feature +feature" */
1430 #define DEFINE_ACTION_BOOL(__name, __bit) \
1431 if (!(mask & __bit)) \
1433 string_append(&result, " -" __name " \\\n"); \
1435 else if (add & __bit) \
1437 string_append(&result, " +" __name " \\\n"); \
1440 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1441 if (!(mask & __bit)) \
1443 string_append(&result, " -" __name " \\\n"); \
1445 else if (add & __bit) \
1447 string_append(&result, " +" __name "{"); \
1448 string_append(&result, action->string[__index]); \
1449 string_append(&result, "} \\\n"); \
1452 #define DEFINE_ACTION_MULTI(__name, __index) \
1453 if (action->multi_remove_all[__index]) \
1455 string_append(&result, " -" __name " \\\n"); \
1459 lst = action->multi_remove[__index]->first; \
1462 string_append(&result, " -" __name "{"); \
1463 string_append(&result, lst->str); \
1464 string_append(&result, "} \\\n"); \
1468 lst = action->multi_add[__index]->first; \
1471 string_append(&result, " +" __name "{"); \
1472 string_append(&result, lst->str); \
1473 string_append(&result, "} \\\n"); \
1477 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1479 #include "actionlist.h"
1481 #undef DEFINE_ACTION_MULTI
1482 #undef DEFINE_ACTION_STRING
1483 #undef DEFINE_ACTION_BOOL
1484 #undef DEFINE_ACTION_ALIAS
1490 /*********************************************************************
1492 * Function : actions_to_html
1494 * Description : Converts a actionsfile entry from numeric form
1495 * ("mask" and "add") to a <br>-seperated HTML string
1496 * in which each action is linked to its chapter in
1500 * 1 : csp = Client state (for config)
1501 * 2 : action = Action spec to be converted
1503 * Returns : A string. Caller must free it.
1504 * NULL on out-of-memory error.
1506 *********************************************************************/
1507 char * actions_to_html(struct client_state *csp,
1508 struct action_spec *action)
1510 unsigned mask = action->mask;
1511 unsigned add = action->add;
1512 char * result = strdup("");
1513 struct list_entry * lst;
1515 /* sanity - prevents "-feature +feature" */
1519 #define DEFINE_ACTION_BOOL(__name, __bit) \
1520 if (!(mask & __bit)) \
1522 string_append(&result, "\n<br>-"); \
1523 string_join(&result, add_help_link(__name, csp->config)); \
1525 else if (add & __bit) \
1527 string_append(&result, "\n<br>+"); \
1528 string_join(&result, add_help_link(__name, csp->config)); \
1531 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1532 if (!(mask & __bit)) \
1534 string_append(&result, "\n<br>-"); \
1535 string_join(&result, add_help_link(__name, csp->config)); \
1537 else if (add & __bit) \
1539 string_append(&result, "\n<br>+"); \
1540 string_join(&result, add_help_link(__name, csp->config)); \
1541 string_append(&result, "{"); \
1542 string_join(&result, html_encode(action->string[__index])); \
1543 string_append(&result, "}"); \
1546 #define DEFINE_ACTION_MULTI(__name, __index) \
1547 if (action->multi_remove_all[__index]) \
1549 string_append(&result, "\n<br>-"); \
1550 string_join(&result, add_help_link(__name, csp->config)); \
1554 lst = action->multi_remove[__index]->first; \
1557 string_append(&result, "\n<br>-"); \
1558 string_join(&result, add_help_link(__name, csp->config)); \
1559 string_append(&result, "{"); \
1560 string_join(&result, html_encode(lst->str)); \
1561 string_append(&result, "}"); \
1565 lst = action->multi_add[__index]->first; \
1568 string_append(&result, "\n<br>+"); \
1569 string_join(&result, add_help_link(__name, csp->config)); \
1570 string_append(&result, "{"); \
1571 string_join(&result, html_encode(lst->str)); \
1572 string_append(&result, "}"); \
1576 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1578 #include "actionlist.h"
1580 #undef DEFINE_ACTION_MULTI
1581 #undef DEFINE_ACTION_STRING
1582 #undef DEFINE_ACTION_BOOL
1583 #undef DEFINE_ACTION_ALIAS
1585 /* trim leading <br> */
1586 if (result && *result)
1589 result = strdup(result + 5);
1597 /*********************************************************************
1599 * Function : current_actions_to_html
1601 * Description : Converts a curren action spec to a <br> seperated HTML
1602 * text in which each action is linked to its chapter in
1606 * 1 : csp = Client state (for config)
1607 * 2 : action = Current action spec to be converted
1609 * Returns : A string. Caller must free it.
1610 * NULL on out-of-memory error.
1612 *********************************************************************/
1613 char *current_action_to_html(struct client_state *csp,
1614 struct current_action_spec *action)
1616 unsigned long flags = action->flags;
1617 char * result = strdup("");
1618 struct list_entry * lst;
1620 #define DEFINE_ACTION_BOOL(__name, __bit) \
1621 if (flags & __bit) \
1623 string_append(&result, "\n<br>+"); \
1624 string_join(&result, add_help_link(__name, csp->config)); \
1628 string_append(&result, "\n<br>-"); \
1629 string_join(&result, add_help_link(__name, csp->config)); \
1632 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
1633 if (flags & __bit) \
1635 string_append(&result, "\n<br>+"); \
1636 string_join(&result, add_help_link(__name, csp->config)); \
1637 string_append(&result, "{"); \
1638 string_join(&result, html_encode(action->string[__index])); \
1639 string_append(&result, "}"); \
1643 string_append(&result, "\n<br>-"); \
1644 string_join(&result, add_help_link(__name, csp->config)); \
1647 #define DEFINE_ACTION_MULTI(__name, __index) \
1648 lst = action->multi[__index]->first; \
1651 string_append(&result, "\n<br>-"); \
1652 string_join(&result, add_help_link(__name, csp->config)); \
1658 string_append(&result, "\n<br>+"); \
1659 string_join(&result, add_help_link(__name, csp->config)); \
1660 string_append(&result, "{"); \
1661 string_join(&result, html_encode(lst->str)); \
1662 string_append(&result, "}"); \
1667 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
1669 #include "actionlist.h"
1671 #undef DEFINE_ACTION_MULTI
1672 #undef DEFINE_ACTION_STRING
1673 #undef DEFINE_ACTION_BOOL
1674 #undef DEFINE_ACTION_ALIAS