1 const char actions_rcs[] = "$Id: actions.c,v 1.14 2001/09/22 16:36:59 jongfoster 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 * IJBSWA team. http://ijbswa.sourceforge.net
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.14 2001/09/22 16:36:59 jongfoster
37 * Removing unused parameter fs from read_config_line()
39 * Revision 1.13 2001/09/16 15:47:37 jongfoster
40 * First version of CGI-based edit interface. This is very much a
41 * work-in-progress, and you can't actually use it to edit anything
42 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
45 * Revision 1.12 2001/09/16 13:21:27 jongfoster
46 * Changes to use new list functions.
48 * Revision 1.11 2001/09/14 00:17:32 jongfoster
49 * Tidying up memory allocation. New function init_action().
51 * Revision 1.10 2001/09/10 10:14:34 oes
52 * Removing unused variable
54 * Revision 1.9 2001/07/30 22:08:36 jongfoster
55 * Tidying up #defines:
56 * - All feature #defines are now of the form FEATURE_xxx
57 * - Permanently turned off WIN_GUI_EDIT
58 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
60 * Revision 1.8 2001/06/29 13:19:52 oes
61 * Removed logentry from cancelled commit
63 * Revision 1.7 2001/06/09 10:55:28 jongfoster
64 * Changing BUFSIZ ==> BUFFER_SIZE
66 * Revision 1.6 2001/06/07 23:04:34 jongfoster
67 * Made get_actions() static.
69 * Revision 1.5 2001/06/03 19:11:48 oes
70 * adapted to new enlist_unique arg format
72 * Revision 1.4 2001/06/01 20:03:42 jongfoster
73 * Better memory management - current_action->strings[] now
74 * contains copies of the strings, not the original.
76 * Revision 1.3 2001/06/01 18:49:17 jongfoster
77 * Replaced "list_share" with "list" - the tiny memory gain was not
78 * worth the extra complexity.
80 * Revision 1.2 2001/05/31 21:40:00 jongfoster
81 * Removing some commented out, obsolete blocks of code.
83 * Revision 1.1 2001/05/31 21:16:46 jongfoster
84 * Moved functions to process the action list into this new file.
87 *********************************************************************/
102 #ifdef FEATURE_CGI_EDIT_ACTIONS
104 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
106 const char actions_h_rcs[] = ACTIONS_H_VERSION;
110 * We need the main list of options.
112 * First, we need a way to tell between boolean, string, and multi-string
113 * options. For string and multistring options, we also need to be
114 * able to tell the difference between a "+" and a "-". (For bools,
115 * the "+"/"-" information is encoded in "add" and "mask"). So we use
116 * an enumerated type (well, the preprocessor equivalent). Here are
119 #define AV_NONE 0 /* +opt -opt */
120 #define AV_ADD_STRING 1 /* +stropt{string} */
121 #define AV_REM_STRING 2 /* -stropt */
122 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
123 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
126 * We need a structure to hold the name, flag changes,
127 * type, and string index.
132 unsigned mask; /* a bit set to "0" = remove action */
133 unsigned add; /* a bit set to "1" = add action */
134 int takes_value; /* an AV_... constant */
135 int index; /* index into strings[] or multi[] */
139 * And with those building blocks in place, here's the array.
141 static const struct action_name action_names[] =
144 * Well actually there's no data here - it's in actionlist.h
145 * This keeps it together to make it easy to change.
147 * Here's the macros used to format it:
149 #define DEFINE_ACTION_MULTI(name,index) \
150 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
151 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
152 #define DEFINE_ACTION_STRING(name,flag,index) \
153 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
154 { "-" name, ~flag, 0, AV_REM_STRING, index },
155 #define DEFINE_ACTION_BOOL(name,flag) \
156 { "+" name, ACTION_MASK_ALL, flag }, \
157 { "-" name, ~flag, 0 },
158 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
160 #include "actionlist.h"
162 #undef DEFINE_ACTION_MULTI
163 #undef DEFINE_ACTION_STRING
164 #undef DEFINE_ACTION_BOOL
165 #undef DEFINE_ACTION_ALIAS
167 { NULL, 0, 0 } /* End marker */
171 /*********************************************************************
173 * Function : merge_actions
175 * Description : Merge two actions together.
176 * Similar to "cur_action += new_action".
179 * 1 : cur_action = Current actions, to modify.
180 * 2 : new_action = Action to add.
184 *********************************************************************/
185 void merge_actions (struct action_spec *dest,
186 const struct action_spec *src)
190 dest->mask &= src->mask;
191 dest->add &= src->mask;
192 dest->add |= src->add;
194 for (i = 0; i < ACTION_STRING_COUNT; i++)
196 char * str = src->string[i];
199 freez(dest->string[i]);
200 dest->string[i] = strdup(str);
204 for (i = 0; i < ACTION_MULTI_COUNT; i++)
206 if (src->multi_remove_all[i])
208 /* Remove everything from dest */
209 list_remove_all(dest->multi_remove[i]);
210 dest->multi_remove_all[i] = 1;
212 list_duplicate(dest->multi_add[i], src->multi_add[i]);
214 else if (dest->multi_remove_all[i])
217 * dest already removes everything, so we only need to worry
220 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
221 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
225 /* No "remove all"s to worry about. */
226 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
227 list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
228 list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
234 /*********************************************************************
236 * Function : copy_action
238 * Description : Copy an action_specs.
239 * Similar to "cur_action = new_action".
240 * Note that dest better not contain valid data
241 * - it's overwritten, not freed.
244 * 1 : dest = Destination of copy.
245 * 2 : src = Source for copy.
249 *********************************************************************/
250 void copy_action (struct action_spec *dest,
251 const struct action_spec *src)
255 memset(dest, '\0', sizeof(*dest));
257 dest->mask = src->mask;
258 dest->add = src->add;
260 for (i = 0; i < ACTION_STRING_COUNT; i++)
262 char * str = src->string[i];
263 dest->string[i] = (str ? strdup(str) : NULL);
266 for (i = 0; i < ACTION_MULTI_COUNT; i++)
268 dest->multi_remove_all[i] = src->multi_remove_all[i];
269 list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
270 list_duplicate(dest->multi_add[i], src->multi_add[i]);
275 /*********************************************************************
277 * Function : free_action
279 * Description : Destroy an action_spec. Frees memory used by it,
280 * except for the memory used by the struct action_spec
284 * 1 : src = Source to free.
288 *********************************************************************/
289 void free_action (struct action_spec *src)
293 for (i = 0; i < ACTION_STRING_COUNT; i++)
295 freez(src->string[i]);
298 for (i = 0; i < ACTION_MULTI_COUNT; i++)
300 destroy_list(src->multi_remove[i]);
301 destroy_list(src->multi_add[i]);
304 memset(src, '\0', sizeof(*src));
308 /*********************************************************************
310 * Function : get_action_token
312 * Description : Parses a line for the first action.
313 * Modifies it's input array, doesn't allocate memory.
315 * *line=" +abc{def} -ghi "
322 * 1 : line = [in] The line containing the action.
323 * [out] Start of next action on line, or
324 * NULL if we reached the end of line before
325 * we found an action.
326 * 2 : name = [out] Start of action name, null
327 * terminated. NULL on EOL
328 * 3 : value = [out] Start of action value, null
329 * terminated. NULL if none or EOL.
332 * nonzero => Mismatched {} (line was trashed anyway)
334 *********************************************************************/
335 int get_action_token(char **line, char **name, char **value)
340 /* set default returns */
345 /* Eat any leading whitespace */
346 while ((*str == ' ') || (*str == '\t'))
358 /* null name, just value is prohibited */
365 while (((ch = *str) != '\0') &&
366 (ch != ' ') && (ch != '\t') && (ch != '{'))
382 /* EOL - be careful not to run off buffer */
387 /* More to parse next time. */
396 str = strchr(str, '}');
414 /*********************************************************************
416 * Function : get_actions
418 * Description : Parses a list of actions.
421 * 1 : line = The string containing the actions.
422 * Will be written to by this function.
423 * 2 : aliases = Custom alias list, or NULL for none.
424 * 3 : cur_action = Where to store the action. Caller
428 * nonzero => Error (line was trashed anyway)
430 *********************************************************************/
431 int get_actions(char *line,
432 struct action_alias * alias_list,
433 struct action_spec *cur_action)
435 init_action(cur_action);
436 cur_action->mask = ACTION_MASK_ALL;
440 char * option = NULL;
443 if (get_action_token(&line, &option, &value))
450 /* handle option in 'option' */
452 /* Check for standard action name */
453 const struct action_name * action = action_names;
455 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
459 if (action->name != NULL)
462 cur_action->mask &= action->mask;
463 cur_action->add &= action->mask;
464 cur_action->add |= action->add;
466 switch (action->takes_value)
469 /* ignore any option. */
473 /* add single string. */
475 if ((value == NULL) || (*value == '\0'))
479 /* FIXME: should validate option string here */
480 freez (cur_action->string[action->index]);
481 cur_action->string[action->index] = strdup(value);
486 /* remove single string. */
488 freez (cur_action->string[action->index]);
493 /* append multi string. */
495 struct list * remove = cur_action->multi_remove[action->index];
496 struct list * add = cur_action->multi_add[action->index];
498 if ((value == NULL) || (*value == '\0'))
503 list_remove_item(remove, value);
504 enlist_unique(add, value, 0);
509 /* remove multi string. */
511 struct list * remove = cur_action->multi_remove[action->index];
512 struct list * add = cur_action->multi_add[action->index];
514 if ( (value == NULL) || (*value == '\0')
515 || ((*value == '*') && (value[1] == '\0')) )
518 * no option, or option == "*".
522 list_remove_all(remove);
523 list_remove_all(add);
524 cur_action->multi_remove_all[action->index] = 1;
528 /* Valid option - remove only 1 option */
530 if ( !cur_action->multi_remove_all[action->index] )
532 /* there isn't a catch-all in the remove list already */
533 enlist_unique(remove, value, 0);
535 list_remove_item(add, value);
540 /* Shouldn't get here unless there's memory corruption. */
546 /* try user aliases. */
547 const struct action_alias * alias = alias_list;
549 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
556 merge_actions(cur_action, alias->action);
560 /* Bad action name */
571 /*********************************************************************
573 * Function : actions_to_text
575 * Description : Converts a actionsfile entry from numeric form
576 * ("mask" and "add") to text.
579 * 1 : mask = As from struct url_actions
580 * 2 : add = As from struct url_actions
582 * Returns : A string. Caller must free it.
584 *********************************************************************/
585 char * actions_to_text(struct action_spec *action)
587 unsigned mask = action->mask;
588 unsigned add = action->add;
589 char * result = strdup("");
590 struct list_entry * lst;
592 /* sanity - prevents "-feature +feature" */
596 #define DEFINE_ACTION_BOOL(__name, __bit) \
597 if (!(mask & __bit)) \
599 result = strsav(result, " -" __name); \
601 else if (add & __bit) \
603 result = strsav(result, " +" __name); \
606 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
607 if (!(mask & __bit)) \
609 result = strsav(result, " -" __name); \
611 else if (add & __bit) \
613 result = strsav(result, " +" __name "{"); \
614 result = strsav(result, action->string[__index]); \
615 result = strsav(result, "}"); \
618 #define DEFINE_ACTION_MULTI(__name, __index) \
619 if (action->multi_remove_all[__index]) \
621 result = strsav(result, " -" __name "{*}"); \
625 lst = action->multi_remove[__index]->first; \
628 result = strsav(result, " -" __name "{"); \
629 result = strsav(result, lst->str); \
630 result = strsav(result, "}"); \
634 lst = action->multi_add[__index]->first; \
637 result = strsav(result, " +" __name "{"); \
638 result = strsav(result, lst->str); \
639 result = strsav(result, "}"); \
643 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
645 #include "actionlist.h"
647 #undef DEFINE_ACTION_MULTI
648 #undef DEFINE_ACTION_STRING
649 #undef DEFINE_ACTION_BOOL
650 #undef DEFINE_ACTION_ALIAS
656 #ifdef FEATURE_CGI_EDIT_ACTIONS
657 /*********************************************************************
659 * Function : actions_to_html
661 * Description : Converts a actionsfile entry from numeric form
662 * ("mask" and "add") to a <br>-seperated HTML string.
665 * 1 : mask = As from struct url_actions
666 * 2 : add = As from struct url_actions
668 * Returns : A string. Caller must free it.
670 *********************************************************************/
671 char * actions_to_html(struct action_spec *action)
673 unsigned mask = action->mask;
674 unsigned add = action->add;
675 char * result = strdup("");
677 struct list_entry * lst;
679 /* sanity - prevents "-feature +feature" */
683 #define DEFINE_ACTION_BOOL(__name, __bit) \
684 if (!(mask & __bit)) \
686 result = strsav(result, "\n<br>-" __name); \
688 else if (add & __bit) \
690 result = strsav(result, "\n<br>+" __name); \
693 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
694 if (!(mask & __bit)) \
696 result = strsav(result, "\n<br>-" __name); \
698 else if (add & __bit) \
700 result = strsav(result, "\n<br>+" __name "{"); \
701 enc_str = html_encode(action->string[__index]);\
702 result = strsav(result, enc_str); \
704 result = strsav(result, "}"); \
707 #define DEFINE_ACTION_MULTI(__name, __index) \
708 if (action->multi_remove_all[__index]) \
710 result = strsav(result, "\n<br>-" __name "{*}"); \
714 lst = action->multi_remove[__index]->first; \
717 result = strsav(result, "\n<br>-" __name "{");\
718 enc_str = html_encode(lst->str); \
719 result = strsav(result, enc_str); \
721 result = strsav(result, "}"); \
725 lst = action->multi_add[__index]->first; \
728 result = strsav(result, "\n<br>+" __name "{"); \
729 result = strsav(result, lst->str); \
730 result = strsav(result, "}"); \
734 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
736 #include "actionlist.h"
738 #undef DEFINE_ACTION_MULTI
739 #undef DEFINE_ACTION_STRING
740 #undef DEFINE_ACTION_BOOL
741 #undef DEFINE_ACTION_ALIAS
743 /* trim leading <br> */
744 if (result && *result)
747 result = strdup(result + 5);
753 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
756 /*********************************************************************
758 * Function : current_actions_to_text
760 * Description : Converts a actionsfile entry to text.
763 * 1 : action = Action
765 * Returns : A string. Caller must free it.
767 *********************************************************************/
768 char * current_action_to_text(struct current_action_spec *action)
770 unsigned flags = action->flags;
771 char * result = strdup("");
772 struct list_entry * lst;
774 #define DEFINE_ACTION_BOOL(__name, __bit) \
777 result = strsav(result, " +" __name); \
781 result = strsav(result, " -" __name); \
784 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
787 result = strsav(result, " +" __name "{"); \
788 result = strsav(result, action->string[__index]); \
789 result = strsav(result, "}"); \
793 result = strsav(result, " -" __name); \
796 #define DEFINE_ACTION_MULTI(__name, __index) \
797 lst = action->multi[__index]->first; \
800 result = strsav(result, " -" __name); \
806 result = strsav(result, " +" __name "{"); \
807 result = strsav(result, lst->str); \
808 result = strsav(result, "}"); \
813 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
815 #include "actionlist.h"
817 #undef DEFINE_ACTION_MULTI
818 #undef DEFINE_ACTION_STRING
819 #undef DEFINE_ACTION_BOOL
820 #undef DEFINE_ACTION_ALIAS
826 /*********************************************************************
828 * Function : init_current_action
830 * Description : Zero out an action.
833 * 1 : dest = An uninitialized current_action_spec.
837 *********************************************************************/
838 void init_current_action (struct current_action_spec *dest)
840 memset(dest, '\0', sizeof(*dest));
842 dest->flags = ACTION_MOST_COMPATIBLE;
846 /*********************************************************************
848 * Function : init_action
850 * Description : Zero out an action.
853 * 1 : dest = An uninitialized action_spec.
857 *********************************************************************/
858 void init_action (struct action_spec *dest)
860 memset(dest, '\0', sizeof(*dest));
864 /*********************************************************************
866 * Function : merge_current_action
868 * Description : Merge two actions together.
869 * Similar to "dest += src".
870 * Differences between this and merge_actions()
871 * is that this one doesn't allocate memory for
872 * strings (so "src" better be in memory for at least
873 * as long as "dest" is, and you'd better free
874 * "dest" using "free_current_action").
875 * Also, there is no mask or remove lists in dest.
876 * (If we're applying it to a URL, we don't need them)
879 * 1 : dest = Current actions, to modify.
880 * 2 : src = Action to add.
884 *********************************************************************/
885 void merge_current_action (struct current_action_spec *dest,
886 const struct action_spec *src)
890 dest->flags &= src->mask;
891 dest->flags |= src->add;
893 for (i = 0; i < ACTION_STRING_COUNT; i++)
895 char * str = src->string[i];
898 freez(dest->string[i]);
899 dest->string[i] = strdup(str);
903 for (i = 0; i < ACTION_MULTI_COUNT; i++)
905 if (src->multi_remove_all[i])
907 /* Remove everything from dest, then add src->multi_add */
908 list_duplicate(dest->multi[i], src->multi_add[i]);
912 list_remove_list(dest->multi[i], src->multi_remove[i]);
913 list_append_list_unique(dest->multi[i], src->multi_add[i]);
919 /*********************************************************************
921 * Function : free_current_action
923 * Description : Free memory used by a current_action_spec.
924 * Does not free the current_action_spec itself.
927 * 1 : src = Source to free.
931 *********************************************************************/
932 void free_current_action (struct current_action_spec *src)
936 for (i = 0; i < ACTION_STRING_COUNT; i++)
938 freez(src->string[i]);
941 for (i = 0; i < ACTION_MULTI_COUNT; i++)
943 destroy_list(src->multi[i]);
946 memset(src, '\0', sizeof(*src));
950 /*********************************************************************
952 * Function : unload_actions_file
954 * Description : Unloads an actions module.
957 * 1 : file_data = the data structure associated with the
962 *********************************************************************/
963 void unload_actions_file(void *file_data)
965 struct url_actions * next;
966 struct url_actions * cur = (struct url_actions *)file_data;
971 free_action(cur->action);
979 /*********************************************************************
981 * Function : free_alias_list
983 * Description : Free memory used by a list of aliases.
986 * 1 : alias_list = Linked list to free.
990 *********************************************************************/
991 void free_alias_list(struct action_alias *alias_list)
993 while (alias_list != NULL)
995 struct action_alias * next = alias_list->next;
996 alias_list->next = NULL;
997 freez((char *)alias_list->name);
998 free_action(alias_list->action);
1005 /*********************************************************************
1007 * Function : load_actions_file
1009 * Description : Read and parse a action file and add to files
1013 * 1 : csp = Current client state (buffers, headers, etc...)
1015 * Returns : 0 => Ok, everything else is an error.
1017 *********************************************************************/
1018 int load_actions_file(struct client_state *csp)
1020 static struct file_list *current_actions_file = NULL;
1024 * Note: Keep these in the order they occur in the file, they are
1025 * sometimes tested with <=
1027 #define MODE_START_OF_FILE 1
1028 #define MODE_SETTINGS 2
1029 #define MODE_DESCRIPTION 3
1030 #define MODE_ALIAS 4
1031 #define MODE_ACTIONS 5
1033 int mode = MODE_START_OF_FILE;
1036 struct url_actions *last_perm;
1037 struct url_actions *perm;
1038 char buf[BUFFER_SIZE];
1039 struct file_list *fs;
1040 struct action_spec * cur_action = NULL;
1041 int cur_action_used = 0;
1042 struct action_alias * alias_list = NULL;
1044 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
1046 /* No need to load */
1049 csp->actions_list = current_actions_file;
1055 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1056 csp->config->actions_file);
1057 return 1; /* never get here */
1060 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1061 if (last_perm == NULL)
1063 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1064 csp->config->actions_file);
1065 return 1; /* never get here */
1068 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
1070 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1071 csp->config->actions_file);
1072 return 1; /* never get here */
1075 while (read_config_line(buf, sizeof(buf), fp) != NULL)
1079 /* It's a header block */
1082 /* It's {{settings}} or {{alias}} */
1083 int len = strlen(buf);
1084 char * start = buf + 2;
1085 char * end = buf + len - 1;
1086 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1090 log_error(LOG_LEVEL_FATAL,
1091 "can't load actions file '%s': invalid line: %s",
1092 csp->config->actions_file, buf);
1093 return 1; /* never get here */
1096 /* Trim leading and trailing whitespace. */
1104 log_error(LOG_LEVEL_FATAL,
1105 "can't load actions file '%s': invalid line: {{ }}",
1106 csp->config->actions_file);
1107 return 1; /* never get here */
1111 * An actionsfile can optionally contain the following blocks.
1112 * They *MUST* be in this order, to simplify processing:
1118 * ...free text, format TBD, but no line may start with a '{'...
1123 * The actual actions must be *after* these special blocks.
1124 * None of these special blocks may be repeated.
1127 if (0 == strcmpic(start, "settings"))
1129 /* it's a {{settings}} block */
1130 if (mode >= MODE_SETTINGS)
1132 /* {{settings}} must be first thing in file and must only
1136 log_error(LOG_LEVEL_FATAL,
1137 "can't load actions file '%s': {{settings}} must only appear once, and it must be before anything else.",
1138 csp->config->actions_file);
1140 mode = MODE_SETTINGS;
1142 else if (0 == strcmpic(start, "description"))
1144 /* it's a {{description}} block */
1145 if (mode >= MODE_DESCRIPTION)
1147 /* {{description}} is a singleton and only {{settings}} may proceed it
1150 log_error(LOG_LEVEL_FATAL,
1151 "can't load actions file '%s': {{description}} must only appear once, and only a {{settings}} block may be above it.",
1152 csp->config->actions_file);
1154 mode = MODE_DESCRIPTION;
1156 else if (0 == strcmpic(start, "alias"))
1158 /* it's an {{alias}} block */
1159 if (mode >= MODE_ALIAS)
1161 /* {{alias}} must be first thing in file, possibly after
1162 * {{settings}} and {{description}}
1164 * {{alias}} must only appear once.
1166 * Note that these are new restrictions introduced in
1167 * v2.9.10 in order to make actionsfile editing simpler.
1168 * (Otherwise, reordering actionsfile entries without
1169 * completely rewriting the file becomes non-trivial)
1172 log_error(LOG_LEVEL_FATAL,
1173 "can't load actions file '%s': {{alias}} must only appear once, and it must be before all actions.",
1174 csp->config->actions_file, start);
1180 /* invalid {{something}} block */
1182 log_error(LOG_LEVEL_FATAL,
1183 "can't load actions file '%s': invalid line: {{%s}}",
1184 csp->config->actions_file, start);
1185 return 1; /* never get here */
1190 /* It's an actions block */
1192 char actions_buf[BUFFER_SIZE];
1196 mode = MODE_ACTIONS;
1198 /* free old action */
1201 if (!cur_action_used)
1203 free_action(cur_action);
1208 cur_action_used = 0;
1209 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1210 if (cur_action == NULL)
1213 log_error(LOG_LEVEL_FATAL,
1214 "can't load actions file '%s': out of memory",
1215 csp->config->actions_file);
1216 return 1; /* never get here */
1218 init_action(cur_action);
1221 strcpy(actions_buf, buf + 1);
1223 /* check we have a trailing } and then trim it */
1224 end = actions_buf + strlen(actions_buf) - 1;
1229 log_error(LOG_LEVEL_FATAL,
1230 "can't load actions file '%s': invalid line: %s",
1231 csp->config->actions_file, buf);
1232 return 1; /* never get here */
1236 /* trim any whitespace immediately inside {} */
1239 if (*actions_buf == '\0')
1243 log_error(LOG_LEVEL_FATAL,
1244 "can't load actions file '%s': invalid line: %s",
1245 csp->config->actions_file, buf);
1246 return 1; /* never get here */
1249 if (get_actions(actions_buf, alias_list, cur_action))
1253 log_error(LOG_LEVEL_FATAL,
1254 "can't load actions file '%s': invalid line: %s",
1255 csp->config->actions_file, buf);
1256 return 1; /* never get here */
1260 else if (mode == MODE_SETTINGS)
1263 * Part of the {{settings}} block.
1264 * Ignore for now, but we may want to read & check permissions
1265 * when we go multi-user.
1268 else if (mode == MODE_DESCRIPTION)
1271 * Part of the {{description}} block.
1275 else if (mode == MODE_ALIAS)
1280 char actions_buf[BUFFER_SIZE];
1281 struct action_alias * new_alias;
1283 char * start = strchr(buf, '=');
1286 if ((start == NULL) || (start == buf))
1288 log_error(LOG_LEVEL_FATAL,
1289 "can't load actions file '%s': invalid alias line: %s",
1290 csp->config->actions_file, buf);
1291 return 1; /* never get here */
1294 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1297 log_error(LOG_LEVEL_FATAL,
1298 "can't load actions file '%s': out of memory!",
1299 csp->config->actions_file);
1300 return 1; /* never get here */
1303 /* Eat any the whitespace before the '=' */
1305 while ((*end == ' ') || (*end == '\t'))
1308 * we already know we must have at least 1 non-ws char
1309 * at start of buf - no need to check
1315 /* Eat any the whitespace after the '=' */
1317 while ((*start == ' ') || (*start == '\t'))
1323 log_error(LOG_LEVEL_FATAL,
1324 "can't load actions file '%s': invalid alias line: %s",
1325 csp->config->actions_file, buf);
1326 return 1; /* never get here */
1329 new_alias->name = strdup(buf);
1331 strcpy(actions_buf, start);
1333 if (get_actions(actions_buf, alias_list, new_alias->action))
1337 log_error(LOG_LEVEL_FATAL,
1338 "can't load actions file '%s': invalid alias line: %s = %s",
1339 csp->config->actions_file, buf, start);
1340 return 1; /* never get here */
1344 new_alias->next = alias_list;
1345 alias_list = new_alias;
1347 else if (mode == MODE_ACTIONS)
1349 /* it's a URL pattern */
1351 /* allocate a new node */
1352 if ((perm = zalloc(sizeof(*perm))) == NULL)
1355 log_error(LOG_LEVEL_FATAL,
1356 "can't load actions file '%s': out of memory!",
1357 csp->config->actions_file);
1358 return 1; /* never get here */
1362 copy_action (perm->action, cur_action);
1364 /* Save the URL pattern */
1365 if (create_url_spec(perm->url, buf))
1368 log_error(LOG_LEVEL_FATAL,
1369 "can't load actions file '%s': cannot create URL pattern from: %s",
1370 csp->config->actions_file, buf);
1371 return 1; /* never get here */
1374 /* add it to the list */
1375 last_perm->next = perm;
1378 else if (mode == MODE_START_OF_FILE)
1380 /* oops - please have a {} line as 1st line in file. */
1382 log_error(LOG_LEVEL_FATAL,
1383 "can't load actions file '%s': first line is invalid: %s",
1384 csp->config->actions_file, buf);
1385 return 1; /* never get here */
1389 /* How did we get here? This is impossible! */
1391 log_error(LOG_LEVEL_FATAL,
1392 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1393 csp->config->actions_file, mode);
1394 return 1; /* never get here */
1400 free_action(cur_action);
1402 free_alias_list(alias_list);
1404 /* the old one is now obsolete */
1405 if (current_actions_file)
1407 current_actions_file->unloader = unload_actions_file;
1410 fs->next = files->next;
1412 current_actions_file = fs;
1416 csp->actions_list = fs;