1 const char actions_rcs[] = "$Id: actions.c,v 1.15 2001/10/14 21:58:22 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.15 2001/10/14 21:58:22 jongfoster
37 * Adding support for the CGI-based editor:
38 * - Exported get_actions()
39 * - Added new function free_alias_list()
40 * - Added support for {{settings}} and {{description}} blocks
41 * in the actions file. They are currently ignored.
42 * - Added restriction to only one {{alias}} block which must appear
43 * first in the file, to simplify the editor's rewriting rules.
44 * - Note that load_actions_file() is no longer used by the CGI-based
45 * editor, but some of the other routines in this file are.
47 * Revision 1.14 2001/09/22 16:36:59 jongfoster
48 * Removing unused parameter fs from read_config_line()
50 * Revision 1.13 2001/09/16 15:47:37 jongfoster
51 * First version of CGI-based edit interface. This is very much a
52 * work-in-progress, and you can't actually use it to edit anything
53 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
56 * Revision 1.12 2001/09/16 13:21:27 jongfoster
57 * Changes to use new list functions.
59 * Revision 1.11 2001/09/14 00:17:32 jongfoster
60 * Tidying up memory allocation. New function init_action().
62 * Revision 1.10 2001/09/10 10:14:34 oes
63 * Removing unused variable
65 * Revision 1.9 2001/07/30 22:08:36 jongfoster
66 * Tidying up #defines:
67 * - All feature #defines are now of the form FEATURE_xxx
68 * - Permanently turned off WIN_GUI_EDIT
69 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
71 * Revision 1.8 2001/06/29 13:19:52 oes
72 * Removed logentry from cancelled commit
74 * Revision 1.7 2001/06/09 10:55:28 jongfoster
75 * Changing BUFSIZ ==> BUFFER_SIZE
77 * Revision 1.6 2001/06/07 23:04:34 jongfoster
78 * Made get_actions() static.
80 * Revision 1.5 2001/06/03 19:11:48 oes
81 * adapted to new enlist_unique arg format
83 * Revision 1.4 2001/06/01 20:03:42 jongfoster
84 * Better memory management - current_action->strings[] now
85 * contains copies of the strings, not the original.
87 * Revision 1.3 2001/06/01 18:49:17 jongfoster
88 * Replaced "list_share" with "list" - the tiny memory gain was not
89 * worth the extra complexity.
91 * Revision 1.2 2001/05/31 21:40:00 jongfoster
92 * Removing some commented out, obsolete blocks of code.
94 * Revision 1.1 2001/05/31 21:16:46 jongfoster
95 * Moved functions to process the action list into this new file.
98 *********************************************************************/
111 #include "miscutil.h"
114 #ifdef FEATURE_CGI_EDIT_ACTIONS
116 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
118 const char actions_h_rcs[] = ACTIONS_H_VERSION;
122 * We need the main list of options.
124 * First, we need a way to tell between boolean, string, and multi-string
125 * options. For string and multistring options, we also need to be
126 * able to tell the difference between a "+" and a "-". (For bools,
127 * the "+"/"-" information is encoded in "add" and "mask"). So we use
128 * an enumerated type (well, the preprocessor equivalent). Here are
131 #define AV_NONE 0 /* +opt -opt */
132 #define AV_ADD_STRING 1 /* +stropt{string} */
133 #define AV_REM_STRING 2 /* -stropt */
134 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
135 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
138 * We need a structure to hold the name, flag changes,
139 * type, and string index.
144 unsigned mask; /* a bit set to "0" = remove action */
145 unsigned add; /* a bit set to "1" = add action */
146 int takes_value; /* an AV_... constant */
147 int index; /* index into strings[] or multi[] */
151 * And with those building blocks in place, here's the array.
153 static const struct action_name action_names[] =
156 * Well actually there's no data here - it's in actionlist.h
157 * This keeps it together to make it easy to change.
159 * Here's the macros used to format it:
161 #define DEFINE_ACTION_MULTI(name,index) \
162 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
163 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
164 #define DEFINE_ACTION_STRING(name,flag,index) \
165 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
166 { "-" name, ~flag, 0, AV_REM_STRING, index },
167 #define DEFINE_ACTION_BOOL(name,flag) \
168 { "+" name, ACTION_MASK_ALL, flag }, \
169 { "-" name, ~flag, 0 },
170 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
172 #include "actionlist.h"
174 #undef DEFINE_ACTION_MULTI
175 #undef DEFINE_ACTION_STRING
176 #undef DEFINE_ACTION_BOOL
177 #undef DEFINE_ACTION_ALIAS
179 { NULL, 0, 0 } /* End marker */
183 /*********************************************************************
185 * Function : merge_actions
187 * Description : Merge two actions together.
188 * Similar to "cur_action += new_action".
191 * 1 : cur_action = Current actions, to modify.
192 * 2 : new_action = Action to add.
194 * Returns : JB_ERR_OK or JB_ERR_MEMORY
196 *********************************************************************/
197 jb_err merge_actions (struct action_spec *dest,
198 const struct action_spec *src)
203 dest->mask &= src->mask;
204 dest->add &= src->mask;
205 dest->add |= src->add;
207 for (i = 0; i < ACTION_STRING_COUNT; i++)
209 char * str = src->string[i];
212 freez(dest->string[i]);
213 dest->string[i] = strdup(str);
214 if (NULL == dest->string[i])
216 return JB_ERR_MEMORY;
221 for (i = 0; i < ACTION_MULTI_COUNT; i++)
223 if (src->multi_remove_all[i])
225 /* Remove everything from dest */
226 list_remove_all(dest->multi_remove[i]);
227 dest->multi_remove_all[i] = 1;
229 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
231 else if (dest->multi_remove_all[i])
234 * dest already removes everything, so we only need to worry
237 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
238 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
242 /* No "remove all"s to worry about. */
243 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
244 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
245 err = err || list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
258 /*********************************************************************
260 * Function : copy_action
262 * Description : Copy an action_specs.
263 * Similar to "cur_action = new_action".
264 * Note that dest better not contain valid data
265 * - it's overwritten, not freed.
268 * 1 : dest = Destination of copy.
269 * 2 : src = Source for copy.
273 *********************************************************************/
274 jb_err copy_action (struct action_spec *dest,
275 const struct action_spec *src)
280 memset(dest, '\0', sizeof(*dest));
282 dest->mask = src->mask;
283 dest->add = src->add;
285 for (i = 0; i < ACTION_STRING_COUNT; i++)
287 char * str = src->string[i];
293 return JB_ERR_MEMORY;
295 dest->string[i] = str;
299 for (i = 0; i < ACTION_MULTI_COUNT; i++)
301 dest->multi_remove_all[i] = src->multi_remove_all[i];
302 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
303 err = err || list_duplicate(dest->multi_add[i], src->multi_add[i]);
312 /*********************************************************************
314 * Function : free_action
316 * Description : Destroy an action_spec. Frees memory used by it,
317 * except for the memory used by the struct action_spec
321 * 1 : src = Source to free.
325 *********************************************************************/
326 void free_action (struct action_spec *src)
330 for (i = 0; i < ACTION_STRING_COUNT; i++)
332 freez(src->string[i]);
335 for (i = 0; i < ACTION_MULTI_COUNT; i++)
337 destroy_list(src->multi_remove[i]);
338 destroy_list(src->multi_add[i]);
341 memset(src, '\0', sizeof(*src));
345 /*********************************************************************
347 * Function : get_action_token
349 * Description : Parses a line for the first action.
350 * Modifies it's input array, doesn't allocate memory.
352 * *line=" +abc{def} -ghi "
359 * 1 : line = [in] The line containing the action.
360 * [out] Start of next action on line, or
361 * NULL if we reached the end of line before
362 * we found an action.
363 * 2 : name = [out] Start of action name, null
364 * terminated. NULL on EOL
365 * 3 : value = [out] Start of action value, null
366 * terminated. NULL if none or EOL.
368 * Returns : JB_ERR_OK => Ok
369 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
371 *********************************************************************/
372 jb_err get_action_token(char **line, char **name, char **value)
377 /* set default returns */
382 /* Eat any leading whitespace */
383 while ((*str == ' ') || (*str == '\t'))
395 /* null name, just value is prohibited */
402 while (((ch = *str) != '\0') &&
403 (ch != ' ') && (ch != '\t') && (ch != '{'))
407 /* error, '}' without '{' */
419 /* EOL - be careful not to run off buffer */
424 /* More to parse next time. */
433 str = strchr(str, '}');
451 /*********************************************************************
453 * Function : get_actions
455 * Description : Parses a list of actions.
458 * 1 : line = The string containing the actions.
459 * Will be written to by this function.
460 * 2 : aliases = Custom alias list, or NULL for none.
461 * 3 : cur_action = Where to store the action. Caller
464 * Returns : JB_ERR_OK => Ok
465 * JB_ERR_PARSE => Parse error (line was trashed anyway)
466 * nonzero => Out of memory (line was trashed anyway)
468 *********************************************************************/
469 jb_err get_actions(char *line,
470 struct action_alias * alias_list,
471 struct action_spec *cur_action)
474 init_action(cur_action);
475 cur_action->mask = ACTION_MASK_ALL;
479 char * option = NULL;
482 err = get_action_token(&line, &option, &value);
490 /* handle option in 'option' */
492 /* Check for standard action name */
493 const struct action_name * action = action_names;
495 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
499 if (action->name != NULL)
502 cur_action->mask &= action->mask;
503 cur_action->add &= action->mask;
504 cur_action->add |= action->add;
506 switch (action->takes_value)
509 /* ignore any option. */
513 /* add single string. */
515 if ((value == NULL) || (*value == '\0'))
519 /* FIXME: should validate option string here */
520 freez (cur_action->string[action->index]);
521 cur_action->string[action->index] = strdup(value);
522 if (NULL == cur_action->string[action->index])
524 return JB_ERR_MEMORY;
530 /* remove single string. */
532 freez (cur_action->string[action->index]);
537 /* append multi string. */
539 struct list * remove = cur_action->multi_remove[action->index];
540 struct list * add = cur_action->multi_add[action->index];
542 if ((value == NULL) || (*value == '\0'))
547 list_remove_item(remove, value);
548 err = enlist_unique(add, value, 0);
557 /* remove multi string. */
559 struct list * remove = cur_action->multi_remove[action->index];
560 struct list * add = cur_action->multi_add[action->index];
562 if ( (value == NULL) || (*value == '\0')
563 || ((*value == '*') && (value[1] == '\0')) )
566 * no option, or option == "*".
570 list_remove_all(remove);
571 list_remove_all(add);
572 cur_action->multi_remove_all[action->index] = 1;
576 /* Valid option - remove only 1 option */
578 if ( !cur_action->multi_remove_all[action->index] )
580 /* there isn't a catch-all in the remove list already */
581 err = enlist_unique(remove, value, 0);
587 list_remove_item(add, value);
592 /* Shouldn't get here unless there's memory corruption. */
599 /* try user aliases. */
600 const struct action_alias * alias = alias_list;
602 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
609 merge_actions(cur_action, alias->action);
613 /* Bad action name */
624 /*********************************************************************
626 * Function : actions_to_text
628 * Description : Converts a actionsfile entry from numeric form
629 * ("mask" and "add") to text.
632 * 1 : mask = As from struct url_actions
633 * 2 : add = As from struct url_actions
635 * Returns : A string. Caller must free it.
636 * NULL on out-of-memory error.
638 *********************************************************************/
639 char * actions_to_text(struct action_spec *action)
641 unsigned mask = action->mask;
642 unsigned add = action->add;
643 char * result = strdup("");
644 struct list_entry * lst;
646 /* sanity - prevents "-feature +feature" */
650 #define DEFINE_ACTION_BOOL(__name, __bit) \
651 if (!(mask & __bit)) \
653 string_append(&result, " -" __name); \
655 else if (add & __bit) \
657 string_append(&result, " +" __name); \
660 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
661 if (!(mask & __bit)) \
663 string_append(&result, " -" __name); \
665 else if (add & __bit) \
667 string_append(&result, " +" __name "{"); \
668 string_append(&result, action->string[__index]); \
669 string_append(&result, "}"); \
672 #define DEFINE_ACTION_MULTI(__name, __index) \
673 if (action->multi_remove_all[__index]) \
675 string_append(&result, " -" __name "{*}"); \
679 lst = action->multi_remove[__index]->first; \
682 string_append(&result, " -" __name "{"); \
683 string_append(&result, lst->str); \
684 string_append(&result, "}"); \
688 lst = action->multi_add[__index]->first; \
691 string_append(&result, " +" __name "{"); \
692 string_append(&result, lst->str); \
693 string_append(&result, "}"); \
697 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
699 #include "actionlist.h"
701 #undef DEFINE_ACTION_MULTI
702 #undef DEFINE_ACTION_STRING
703 #undef DEFINE_ACTION_BOOL
704 #undef DEFINE_ACTION_ALIAS
710 #ifdef FEATURE_CGI_EDIT_ACTIONS
711 /*********************************************************************
713 * Function : actions_to_html
715 * Description : Converts a actionsfile entry from numeric form
716 * ("mask" and "add") to a <br>-seperated HTML string.
719 * 1 : mask = As from struct url_actions
720 * 2 : add = As from struct url_actions
722 * Returns : A string. Caller must free it.
723 * NULL on out-of-memory error.
725 *********************************************************************/
726 char * actions_to_html(struct action_spec *action)
728 unsigned mask = action->mask;
729 unsigned add = action->add;
730 char * result = strdup("");
732 struct list_entry * lst;
734 /* sanity - prevents "-feature +feature" */
738 #define DEFINE_ACTION_BOOL(__name, __bit) \
739 if (!(mask & __bit)) \
741 string_append(&result, "\n<br>-" __name); \
743 else if (add & __bit) \
745 string_append(&result, "\n<br>+" __name); \
748 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
749 if (!(mask & __bit)) \
751 string_append(&result, "\n<br>-" __name); \
753 else if (add & __bit) \
755 string_append(&result, "\n<br>+" __name "{"); \
756 if (NULL == result) \
760 enc_str = html_encode(action->string[__index]);\
761 if (NULL == enc_str) \
766 string_append(&result, enc_str); \
768 string_append(&result, "}"); \
771 #define DEFINE_ACTION_MULTI(__name, __index) \
772 if (action->multi_remove_all[__index]) \
774 string_append(&result, "\n<br>-" __name "{*}"); \
778 lst = action->multi_remove[__index]->first; \
781 string_append(&result, "\n<br>-" __name "{");\
782 if (NULL == result) \
786 enc_str = html_encode(lst->str); \
787 if (NULL == enc_str) \
792 string_append(&result, enc_str); \
794 string_append(&result, "}"); \
798 lst = action->multi_add[__index]->first; \
801 string_append(&result, "\n<br>+" __name "{"); \
802 if (NULL == result) \
806 enc_str = html_encode(lst->str); \
807 if (NULL == enc_str) \
812 string_append(&result, enc_str); \
814 string_append(&result, "}"); \
818 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
820 #include "actionlist.h"
822 #undef DEFINE_ACTION_MULTI
823 #undef DEFINE_ACTION_STRING
824 #undef DEFINE_ACTION_BOOL
825 #undef DEFINE_ACTION_ALIAS
827 /* trim leading <br> */
828 if (result && *result)
831 result = strdup(result + 5);
837 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
840 /*********************************************************************
842 * Function : current_actions_to_text
844 * Description : Converts a actionsfile entry to text.
847 * 1 : action = Action
849 * Returns : A string. Caller must free it.
850 * NULL on out-of-memory error.
852 *********************************************************************/
853 char * current_action_to_text(struct current_action_spec *action)
855 unsigned flags = action->flags;
856 char * result = strdup("");
857 struct list_entry * lst;
859 #define DEFINE_ACTION_BOOL(__name, __bit) \
862 string_append(&result, " +" __name); \
866 string_append(&result, " -" __name); \
869 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
872 string_append(&result, " +" __name "{"); \
873 string_append(&result, action->string[__index]); \
874 string_append(&result, "}"); \
878 string_append(&result, " -" __name); \
881 #define DEFINE_ACTION_MULTI(__name, __index) \
882 lst = action->multi[__index]->first; \
885 string_append(&result, " -" __name); \
891 string_append(&result, " +" __name "{"); \
892 string_append(&result, lst->str); \
893 string_append(&result, "}"); \
898 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
900 #include "actionlist.h"
902 #undef DEFINE_ACTION_MULTI
903 #undef DEFINE_ACTION_STRING
904 #undef DEFINE_ACTION_BOOL
905 #undef DEFINE_ACTION_ALIAS
911 /*********************************************************************
913 * Function : init_current_action
915 * Description : Zero out an action.
918 * 1 : dest = An uninitialized current_action_spec.
922 *********************************************************************/
923 void init_current_action (struct current_action_spec *dest)
925 memset(dest, '\0', sizeof(*dest));
927 dest->flags = ACTION_MOST_COMPATIBLE;
931 /*********************************************************************
933 * Function : init_action
935 * Description : Zero out an action.
938 * 1 : dest = An uninitialized action_spec.
942 *********************************************************************/
943 void init_action (struct action_spec *dest)
945 memset(dest, '\0', sizeof(*dest));
949 /*********************************************************************
951 * Function : merge_current_action
953 * Description : Merge two actions together.
954 * Similar to "dest += src".
955 * Differences between this and merge_actions()
956 * is that this one doesn't allocate memory for
957 * strings (so "src" better be in memory for at least
958 * as long as "dest" is, and you'd better free
959 * "dest" using "free_current_action").
960 * Also, there is no mask or remove lists in dest.
961 * (If we're applying it to a URL, we don't need them)
964 * 1 : dest = Current actions, to modify.
965 * 2 : src = Action to add.
969 *********************************************************************/
970 jb_err merge_current_action (struct current_action_spec *dest,
971 const struct action_spec *src)
976 dest->flags &= src->mask;
977 dest->flags |= src->add;
979 for (i = 0; i < ACTION_STRING_COUNT; i++)
981 char * str = src->string[i];
987 return JB_ERR_MEMORY;
989 freez(dest->string[i]);
990 dest->string[i] = str;
994 for (i = 0; i < ACTION_MULTI_COUNT; i++)
996 if (src->multi_remove_all[i])
998 /* Remove everything from dest, then add src->multi_add */
999 err = list_duplicate(dest->multi[i], src->multi_add[i]);
1007 list_remove_list(dest->multi[i], src->multi_remove[i]);
1008 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
1018 /*********************************************************************
1020 * Function : free_current_action
1022 * Description : Free memory used by a current_action_spec.
1023 * Does not free the current_action_spec itself.
1026 * 1 : src = Source to free.
1030 *********************************************************************/
1031 void free_current_action (struct current_action_spec *src)
1035 for (i = 0; i < ACTION_STRING_COUNT; i++)
1037 freez(src->string[i]);
1040 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1042 destroy_list(src->multi[i]);
1045 memset(src, '\0', sizeof(*src));
1049 /*********************************************************************
1051 * Function : unload_actions_file
1053 * Description : Unloads an actions module.
1056 * 1 : file_data = the data structure associated with the
1061 *********************************************************************/
1062 void unload_actions_file(void *file_data)
1064 struct url_actions * next;
1065 struct url_actions * cur = (struct url_actions *)file_data;
1070 free_action(cur->action);
1078 /*********************************************************************
1080 * Function : free_alias_list
1082 * Description : Free memory used by a list of aliases.
1085 * 1 : alias_list = Linked list to free.
1089 *********************************************************************/
1090 void free_alias_list(struct action_alias *alias_list)
1092 while (alias_list != NULL)
1094 struct action_alias * next = alias_list->next;
1095 alias_list->next = NULL;
1096 freez((char *)alias_list->name);
1097 free_action(alias_list->action);
1104 /*********************************************************************
1106 * Function : load_actions_file
1108 * Description : Read and parse a action file and add to files
1112 * 1 : csp = Current client state (buffers, headers, etc...)
1114 * Returns : 0 => Ok, everything else is an error.
1116 *********************************************************************/
1117 int load_actions_file(struct client_state *csp)
1119 static struct file_list *current_actions_file = NULL;
1123 * Note: Keep these in the order they occur in the file, they are
1124 * sometimes tested with <=
1126 #define MODE_START_OF_FILE 1
1127 #define MODE_SETTINGS 2
1128 #define MODE_DESCRIPTION 3
1129 #define MODE_ALIAS 4
1130 #define MODE_ACTIONS 5
1132 int mode = MODE_START_OF_FILE;
1135 struct url_actions *last_perm;
1136 struct url_actions *perm;
1137 char buf[BUFFER_SIZE];
1138 struct file_list *fs;
1139 struct action_spec * cur_action = NULL;
1140 int cur_action_used = 0;
1141 struct action_alias * alias_list = NULL;
1143 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
1145 /* No need to load */
1148 csp->actions_list = current_actions_file;
1154 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1155 csp->config->actions_file);
1156 return 1; /* never get here */
1159 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1160 if (last_perm == NULL)
1162 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1163 csp->config->actions_file);
1164 return 1; /* never get here */
1167 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
1169 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1170 csp->config->actions_file);
1171 return 1; /* never get here */
1174 while (read_config_line(buf, sizeof(buf), fp) != NULL)
1178 /* It's a header block */
1181 /* It's {{settings}} or {{alias}} */
1182 int len = strlen(buf);
1183 char * start = buf + 2;
1184 char * end = buf + len - 1;
1185 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1189 log_error(LOG_LEVEL_FATAL,
1190 "can't load actions file '%s': invalid line: %s",
1191 csp->config->actions_file, buf);
1192 return 1; /* never get here */
1195 /* Trim leading and trailing whitespace. */
1203 log_error(LOG_LEVEL_FATAL,
1204 "can't load actions file '%s': invalid line: {{ }}",
1205 csp->config->actions_file);
1206 return 1; /* never get here */
1210 * An actionsfile can optionally contain the following blocks.
1211 * They *MUST* be in this order, to simplify processing:
1217 * ...free text, format TBD, but no line may start with a '{'...
1222 * The actual actions must be *after* these special blocks.
1223 * None of these special blocks may be repeated.
1226 if (0 == strcmpic(start, "settings"))
1228 /* it's a {{settings}} block */
1229 if (mode >= MODE_SETTINGS)
1231 /* {{settings}} must be first thing in file and must only
1235 log_error(LOG_LEVEL_FATAL,
1236 "can't load actions file '%s': {{settings}} must only appear once, and it must be before anything else.",
1237 csp->config->actions_file);
1239 mode = MODE_SETTINGS;
1241 else if (0 == strcmpic(start, "description"))
1243 /* it's a {{description}} block */
1244 if (mode >= MODE_DESCRIPTION)
1246 /* {{description}} is a singleton and only {{settings}} may proceed it
1249 log_error(LOG_LEVEL_FATAL,
1250 "can't load actions file '%s': {{description}} must only appear once, and only a {{settings}} block may be above it.",
1251 csp->config->actions_file);
1253 mode = MODE_DESCRIPTION;
1255 else if (0 == strcmpic(start, "alias"))
1257 /* it's an {{alias}} block */
1258 if (mode >= MODE_ALIAS)
1260 /* {{alias}} must be first thing in file, possibly after
1261 * {{settings}} and {{description}}
1263 * {{alias}} must only appear once.
1265 * Note that these are new restrictions introduced in
1266 * v2.9.10 in order to make actionsfile editing simpler.
1267 * (Otherwise, reordering actionsfile entries without
1268 * completely rewriting the file becomes non-trivial)
1271 log_error(LOG_LEVEL_FATAL,
1272 "can't load actions file '%s': {{alias}} must only appear once, and it must be before all actions.",
1273 csp->config->actions_file, start);
1279 /* invalid {{something}} block */
1281 log_error(LOG_LEVEL_FATAL,
1282 "can't load actions file '%s': invalid line: {{%s}}",
1283 csp->config->actions_file, start);
1284 return 1; /* never get here */
1289 /* It's an actions block */
1291 char actions_buf[BUFFER_SIZE];
1295 mode = MODE_ACTIONS;
1297 /* free old action */
1300 if (!cur_action_used)
1302 free_action(cur_action);
1307 cur_action_used = 0;
1308 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1309 if (cur_action == NULL)
1312 log_error(LOG_LEVEL_FATAL,
1313 "can't load actions file '%s': out of memory",
1314 csp->config->actions_file);
1315 return 1; /* never get here */
1317 init_action(cur_action);
1320 strcpy(actions_buf, buf + 1);
1322 /* check we have a trailing } and then trim it */
1323 end = actions_buf + strlen(actions_buf) - 1;
1328 log_error(LOG_LEVEL_FATAL,
1329 "can't load actions file '%s': invalid line: %s",
1330 csp->config->actions_file, buf);
1331 return 1; /* never get here */
1335 /* trim any whitespace immediately inside {} */
1338 if (get_actions(actions_buf, alias_list, cur_action))
1342 log_error(LOG_LEVEL_FATAL,
1343 "can't load actions file '%s': invalid line: %s",
1344 csp->config->actions_file, buf);
1345 return 1; /* never get here */
1349 else if (mode == MODE_SETTINGS)
1352 * Part of the {{settings}} block.
1353 * Ignore for now, but we may want to read & check permissions
1354 * when we go multi-user.
1357 else if (mode == MODE_DESCRIPTION)
1360 * Part of the {{description}} block.
1364 else if (mode == MODE_ALIAS)
1369 char actions_buf[BUFFER_SIZE];
1370 struct action_alias * new_alias;
1372 char * start = strchr(buf, '=');
1375 if ((start == NULL) || (start == buf))
1377 log_error(LOG_LEVEL_FATAL,
1378 "can't load actions file '%s': invalid alias line: %s",
1379 csp->config->actions_file, buf);
1380 return 1; /* never get here */
1383 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1386 log_error(LOG_LEVEL_FATAL,
1387 "can't load actions file '%s': out of memory!",
1388 csp->config->actions_file);
1389 return 1; /* never get here */
1392 /* Eat any the whitespace before the '=' */
1394 while ((*end == ' ') || (*end == '\t'))
1397 * we already know we must have at least 1 non-ws char
1398 * at start of buf - no need to check
1404 /* Eat any the whitespace after the '=' */
1406 while ((*start == ' ') || (*start == '\t'))
1412 log_error(LOG_LEVEL_FATAL,
1413 "can't load actions file '%s': invalid alias line: %s",
1414 csp->config->actions_file, buf);
1415 return 1; /* never get here */
1418 new_alias->name = strdup(buf);
1420 strcpy(actions_buf, start);
1422 if (get_actions(actions_buf, alias_list, new_alias->action))
1426 log_error(LOG_LEVEL_FATAL,
1427 "can't load actions file '%s': invalid alias line: %s = %s",
1428 csp->config->actions_file, buf, start);
1429 return 1; /* never get here */
1433 new_alias->next = alias_list;
1434 alias_list = new_alias;
1436 else if (mode == MODE_ACTIONS)
1438 /* it's a URL pattern */
1440 /* allocate a new node */
1441 if ((perm = zalloc(sizeof(*perm))) == NULL)
1444 log_error(LOG_LEVEL_FATAL,
1445 "can't load actions file '%s': out of memory!",
1446 csp->config->actions_file);
1447 return 1; /* never get here */
1451 copy_action (perm->action, cur_action);
1453 /* Save the URL pattern */
1454 if (create_url_spec(perm->url, buf))
1457 log_error(LOG_LEVEL_FATAL,
1458 "can't load actions file '%s': cannot create URL pattern from: %s",
1459 csp->config->actions_file, buf);
1460 return 1; /* never get here */
1463 /* add it to the list */
1464 last_perm->next = perm;
1467 else if (mode == MODE_START_OF_FILE)
1469 /* oops - please have a {} line as 1st line in file. */
1471 log_error(LOG_LEVEL_FATAL,
1472 "can't load actions file '%s': first line is invalid: %s",
1473 csp->config->actions_file, buf);
1474 return 1; /* never get here */
1478 /* How did we get here? This is impossible! */
1480 log_error(LOG_LEVEL_FATAL,
1481 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1482 csp->config->actions_file, mode);
1483 return 1; /* never get here */
1489 free_action(cur_action);
1491 free_alias_list(alias_list);
1493 /* the old one is now obsolete */
1494 if (current_actions_file)
1496 current_actions_file->unloader = unload_actions_file;
1499 fs->next = files->next;
1501 current_actions_file = fs;
1505 csp->actions_list = fs;