1 const char actions_rcs[] = "$Id: actions.c,v 1.17 2001/10/25 03:40:47 david__schmidt 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.17 2001/10/25 03:40:47 david__schmidt
37 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
38 * threads to call select() simultaneously. So, it's time to do a real, live,
39 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
40 * (native). Both versions will work, but using __OS2__ offers multi-threading.
42 * Revision 1.16 2001/10/23 21:30:30 jongfoster
43 * Adding error-checking to selected functions.
45 * Revision 1.15 2001/10/14 21:58:22 jongfoster
46 * Adding support for the CGI-based editor:
47 * - Exported get_actions()
48 * - Added new function free_alias_list()
49 * - Added support for {{settings}} and {{description}} blocks
50 * in the actions file. They are currently ignored.
51 * - Added restriction to only one {{alias}} block which must appear
52 * first in the file, to simplify the editor's rewriting rules.
53 * - Note that load_actions_file() is no longer used by the CGI-based
54 * editor, but some of the other routines in this file are.
56 * Revision 1.14 2001/09/22 16:36:59 jongfoster
57 * Removing unused parameter fs from read_config_line()
59 * Revision 1.13 2001/09/16 15:47:37 jongfoster
60 * First version of CGI-based edit interface. This is very much a
61 * work-in-progress, and you can't actually use it to edit anything
62 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
65 * Revision 1.12 2001/09/16 13:21:27 jongfoster
66 * Changes to use new list functions.
68 * Revision 1.11 2001/09/14 00:17:32 jongfoster
69 * Tidying up memory allocation. New function init_action().
71 * Revision 1.10 2001/09/10 10:14:34 oes
72 * Removing unused variable
74 * Revision 1.9 2001/07/30 22:08:36 jongfoster
75 * Tidying up #defines:
76 * - All feature #defines are now of the form FEATURE_xxx
77 * - Permanently turned off WIN_GUI_EDIT
78 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
80 * Revision 1.8 2001/06/29 13:19:52 oes
81 * Removed logentry from cancelled commit
83 * Revision 1.7 2001/06/09 10:55:28 jongfoster
84 * Changing BUFSIZ ==> BUFFER_SIZE
86 * Revision 1.6 2001/06/07 23:04:34 jongfoster
87 * Made get_actions() static.
89 * Revision 1.5 2001/06/03 19:11:48 oes
90 * adapted to new enlist_unique arg format
92 * Revision 1.4 2001/06/01 20:03:42 jongfoster
93 * Better memory management - current_action->strings[] now
94 * contains copies of the strings, not the original.
96 * Revision 1.3 2001/06/01 18:49:17 jongfoster
97 * Replaced "list_share" with "list" - the tiny memory gain was not
98 * worth the extra complexity.
100 * Revision 1.2 2001/05/31 21:40:00 jongfoster
101 * Removing some commented out, obsolete blocks of code.
103 * Revision 1.1 2001/05/31 21:16:46 jongfoster
104 * Moved functions to process the action list into this new file.
107 *********************************************************************/
120 #include "miscutil.h"
123 #ifdef FEATURE_CGI_EDIT_ACTIONS
125 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
127 const char actions_h_rcs[] = ACTIONS_H_VERSION;
131 * We need the main list of options.
133 * First, we need a way to tell between boolean, string, and multi-string
134 * options. For string and multistring options, we also need to be
135 * able to tell the difference between a "+" and a "-". (For bools,
136 * the "+"/"-" information is encoded in "add" and "mask"). So we use
137 * an enumerated type (well, the preprocessor equivalent). Here are
140 #define AV_NONE 0 /* +opt -opt */
141 #define AV_ADD_STRING 1 /* +stropt{string} */
142 #define AV_REM_STRING 2 /* -stropt */
143 #define AV_ADD_MULTI 3 /* +multiopt{string} +multiopt{string2} */
144 #define AV_REM_MULTI 4 /* -multiopt{string} -multiopt{*} */
147 * We need a structure to hold the name, flag changes,
148 * type, and string index.
153 unsigned mask; /* a bit set to "0" = remove action */
154 unsigned add; /* a bit set to "1" = add action */
155 int takes_value; /* an AV_... constant */
156 int index; /* index into strings[] or multi[] */
160 * And with those building blocks in place, here's the array.
162 static const struct action_name action_names[] =
165 * Well actually there's no data here - it's in actionlist.h
166 * This keeps it together to make it easy to change.
168 * Here's the macros used to format it:
170 #define DEFINE_ACTION_MULTI(name,index) \
171 { "+" name, ACTION_MASK_ALL, 0, AV_ADD_MULTI, index }, \
172 { "-" name, ACTION_MASK_ALL, 0, AV_REM_MULTI, index },
173 #define DEFINE_ACTION_STRING(name,flag,index) \
174 { "+" name, ACTION_MASK_ALL, flag, AV_ADD_STRING, index }, \
175 { "-" name, ~flag, 0, AV_REM_STRING, index },
176 #define DEFINE_ACTION_BOOL(name,flag) \
177 { "+" name, ACTION_MASK_ALL, flag }, \
178 { "-" name, ~flag, 0 },
179 #define DEFINE_ACTION_ALIAS 1 /* Want aliases please */
181 #include "actionlist.h"
183 #undef DEFINE_ACTION_MULTI
184 #undef DEFINE_ACTION_STRING
185 #undef DEFINE_ACTION_BOOL
186 #undef DEFINE_ACTION_ALIAS
188 { NULL, 0, 0 } /* End marker */
192 /*********************************************************************
194 * Function : merge_actions
196 * Description : Merge two actions together.
197 * Similar to "cur_action += new_action".
200 * 1 : cur_action = Current actions, to modify.
201 * 2 : new_action = Action to add.
203 * Returns : JB_ERR_OK or JB_ERR_MEMORY
205 *********************************************************************/
206 jb_err merge_actions (struct action_spec *dest,
207 const struct action_spec *src)
212 dest->mask &= src->mask;
213 dest->add &= src->mask;
214 dest->add |= src->add;
216 for (i = 0; i < ACTION_STRING_COUNT; i++)
218 char * str = src->string[i];
221 freez(dest->string[i]);
222 dest->string[i] = strdup(str);
223 if (NULL == dest->string[i])
225 return JB_ERR_MEMORY;
230 for (i = 0; i < ACTION_MULTI_COUNT; i++)
232 if (src->multi_remove_all[i])
234 /* Remove everything from dest */
235 list_remove_all(dest->multi_remove[i]);
236 dest->multi_remove_all[i] = 1;
238 err = list_duplicate(dest->multi_add[i], src->multi_add[i]);
240 else if (dest->multi_remove_all[i])
243 * dest already removes everything, so we only need to worry
246 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
247 err = list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
251 /* No "remove all"s to worry about. */
252 list_remove_list(dest->multi_add[i], src->multi_remove[i]);
253 err = list_append_list_unique(dest->multi_remove[i], src->multi_remove[i]);
254 err = err || list_append_list_unique(dest->multi_add[i], src->multi_add[i]);
267 /*********************************************************************
269 * Function : copy_action
271 * Description : Copy an action_specs.
272 * Similar to "cur_action = new_action".
273 * Note that dest better not contain valid data
274 * - it's overwritten, not freed.
277 * 1 : dest = Destination of copy.
278 * 2 : src = Source for copy.
282 *********************************************************************/
283 jb_err copy_action (struct action_spec *dest,
284 const struct action_spec *src)
287 jb_err err = JB_ERR_OK;
289 memset(dest, '\0', sizeof(*dest));
291 dest->mask = src->mask;
292 dest->add = src->add;
294 for (i = 0; i < ACTION_STRING_COUNT; i++)
296 char * str = src->string[i];
302 return JB_ERR_MEMORY;
304 dest->string[i] = str;
308 for (i = 0; i < ACTION_MULTI_COUNT; i++)
310 dest->multi_remove_all[i] = src->multi_remove_all[i];
311 err = list_duplicate(dest->multi_remove[i], src->multi_remove[i]);
312 err = err || list_duplicate(dest->multi_add[i], src->multi_add[i]);
322 /*********************************************************************
324 * Function : free_action
326 * Description : Destroy an action_spec. Frees memory used by it,
327 * except for the memory used by the struct action_spec
331 * 1 : src = Source to free.
335 *********************************************************************/
336 void free_action (struct action_spec *src)
340 for (i = 0; i < ACTION_STRING_COUNT; i++)
342 freez(src->string[i]);
345 for (i = 0; i < ACTION_MULTI_COUNT; i++)
347 destroy_list(src->multi_remove[i]);
348 destroy_list(src->multi_add[i]);
351 memset(src, '\0', sizeof(*src));
355 /*********************************************************************
357 * Function : get_action_token
359 * Description : Parses a line for the first action.
360 * Modifies it's input array, doesn't allocate memory.
362 * *line=" +abc{def} -ghi "
369 * 1 : line = [in] The line containing the action.
370 * [out] Start of next action on line, or
371 * NULL if we reached the end of line before
372 * we found an action.
373 * 2 : name = [out] Start of action name, null
374 * terminated. NULL on EOL
375 * 3 : value = [out] Start of action value, null
376 * terminated. NULL if none or EOL.
378 * Returns : JB_ERR_OK => Ok
379 * JB_ERR_PARSE => Mismatched {} (line was trashed anyway)
381 *********************************************************************/
382 jb_err get_action_token(char **line, char **name, char **value)
387 /* set default returns */
392 /* Eat any leading whitespace */
393 while ((*str == ' ') || (*str == '\t'))
405 /* null name, just value is prohibited */
412 while (((ch = *str) != '\0') &&
413 (ch != ' ') && (ch != '\t') && (ch != '{'))
417 /* error, '}' without '{' */
429 /* EOL - be careful not to run off buffer */
434 /* More to parse next time. */
443 str = strchr(str, '}');
461 /*********************************************************************
463 * Function : get_actions
465 * Description : Parses a list of actions.
468 * 1 : line = The string containing the actions.
469 * Will be written to by this function.
470 * 2 : aliases = Custom alias list, or NULL for none.
471 * 3 : cur_action = Where to store the action. Caller
474 * Returns : JB_ERR_OK => Ok
475 * JB_ERR_PARSE => Parse error (line was trashed anyway)
476 * nonzero => Out of memory (line was trashed anyway)
478 *********************************************************************/
479 jb_err get_actions(char *line,
480 struct action_alias * alias_list,
481 struct action_spec *cur_action)
484 init_action(cur_action);
485 cur_action->mask = ACTION_MASK_ALL;
489 char * option = NULL;
492 err = get_action_token(&line, &option, &value);
500 /* handle option in 'option' */
502 /* Check for standard action name */
503 const struct action_name * action = action_names;
505 while ( (action->name != NULL) && (0 != strcmpic(action->name, option)) )
509 if (action->name != NULL)
512 cur_action->mask &= action->mask;
513 cur_action->add &= action->mask;
514 cur_action->add |= action->add;
516 switch (action->takes_value)
519 /* ignore any option. */
523 /* add single string. */
525 if ((value == NULL) || (*value == '\0'))
529 /* FIXME: should validate option string here */
530 freez (cur_action->string[action->index]);
531 cur_action->string[action->index] = strdup(value);
532 if (NULL == cur_action->string[action->index])
534 return JB_ERR_MEMORY;
540 /* remove single string. */
542 freez (cur_action->string[action->index]);
547 /* append multi string. */
549 struct list * remove = cur_action->multi_remove[action->index];
550 struct list * add = cur_action->multi_add[action->index];
552 if ((value == NULL) || (*value == '\0'))
557 list_remove_item(remove, value);
558 err = enlist_unique(add, value, 0);
567 /* remove multi string. */
569 struct list * remove = cur_action->multi_remove[action->index];
570 struct list * add = cur_action->multi_add[action->index];
572 if ( (value == NULL) || (*value == '\0')
573 || ((*value == '*') && (value[1] == '\0')) )
576 * no option, or option == "*".
580 list_remove_all(remove);
581 list_remove_all(add);
582 cur_action->multi_remove_all[action->index] = 1;
586 /* Valid option - remove only 1 option */
588 if ( !cur_action->multi_remove_all[action->index] )
590 /* there isn't a catch-all in the remove list already */
591 err = enlist_unique(remove, value, 0);
597 list_remove_item(add, value);
602 /* Shouldn't get here unless there's memory corruption. */
609 /* try user aliases. */
610 const struct action_alias * alias = alias_list;
612 while ( (alias != NULL) && (0 != strcmpic(alias->name, option)) )
619 merge_actions(cur_action, alias->action);
623 /* Bad action name */
634 /*********************************************************************
636 * Function : actions_to_text
638 * Description : Converts a actionsfile entry from numeric form
639 * ("mask" and "add") to text.
642 * 1 : mask = As from struct url_actions
643 * 2 : add = As from struct url_actions
645 * Returns : A string. Caller must free it.
646 * NULL on out-of-memory error.
648 *********************************************************************/
649 char * actions_to_text(struct action_spec *action)
651 unsigned mask = action->mask;
652 unsigned add = action->add;
653 char * result = strdup("");
654 struct list_entry * lst;
656 /* sanity - prevents "-feature +feature" */
660 #define DEFINE_ACTION_BOOL(__name, __bit) \
661 if (!(mask & __bit)) \
663 string_append(&result, " -" __name); \
665 else if (add & __bit) \
667 string_append(&result, " +" __name); \
670 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
671 if (!(mask & __bit)) \
673 string_append(&result, " -" __name); \
675 else if (add & __bit) \
677 string_append(&result, " +" __name "{"); \
678 string_append(&result, action->string[__index]); \
679 string_append(&result, "}"); \
682 #define DEFINE_ACTION_MULTI(__name, __index) \
683 if (action->multi_remove_all[__index]) \
685 string_append(&result, " -" __name "{*}"); \
689 lst = action->multi_remove[__index]->first; \
692 string_append(&result, " -" __name "{"); \
693 string_append(&result, lst->str); \
694 string_append(&result, "}"); \
698 lst = action->multi_add[__index]->first; \
701 string_append(&result, " +" __name "{"); \
702 string_append(&result, lst->str); \
703 string_append(&result, "}"); \
707 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
709 #include "actionlist.h"
711 #undef DEFINE_ACTION_MULTI
712 #undef DEFINE_ACTION_STRING
713 #undef DEFINE_ACTION_BOOL
714 #undef DEFINE_ACTION_ALIAS
720 #ifdef FEATURE_CGI_EDIT_ACTIONS
721 /*********************************************************************
723 * Function : actions_to_html
725 * Description : Converts a actionsfile entry from numeric form
726 * ("mask" and "add") to a <br>-seperated HTML string.
729 * 1 : mask = As from struct url_actions
730 * 2 : add = As from struct url_actions
732 * Returns : A string. Caller must free it.
733 * NULL on out-of-memory error.
735 *********************************************************************/
736 char * actions_to_html(struct action_spec *action)
738 unsigned mask = action->mask;
739 unsigned add = action->add;
740 char * result = strdup("");
742 struct list_entry * lst;
744 /* sanity - prevents "-feature +feature" */
748 #define DEFINE_ACTION_BOOL(__name, __bit) \
749 if (!(mask & __bit)) \
751 string_append(&result, "\n<br>-" __name); \
753 else if (add & __bit) \
755 string_append(&result, "\n<br>+" __name); \
758 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
759 if (!(mask & __bit)) \
761 string_append(&result, "\n<br>-" __name); \
763 else if (add & __bit) \
765 string_append(&result, "\n<br>+" __name "{"); \
766 if (NULL == result) \
770 enc_str = html_encode(action->string[__index]);\
771 if (NULL == enc_str) \
776 string_append(&result, enc_str); \
778 string_append(&result, "}"); \
781 #define DEFINE_ACTION_MULTI(__name, __index) \
782 if (action->multi_remove_all[__index]) \
784 string_append(&result, "\n<br>-" __name "{*}"); \
788 lst = action->multi_remove[__index]->first; \
791 string_append(&result, "\n<br>-" __name "{");\
792 if (NULL == result) \
796 enc_str = html_encode(lst->str); \
797 if (NULL == enc_str) \
802 string_append(&result, enc_str); \
804 string_append(&result, "}"); \
808 lst = action->multi_add[__index]->first; \
811 string_append(&result, "\n<br>+" __name "{"); \
812 if (NULL == result) \
816 enc_str = html_encode(lst->str); \
817 if (NULL == enc_str) \
822 string_append(&result, enc_str); \
824 string_append(&result, "}"); \
828 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
830 #include "actionlist.h"
832 #undef DEFINE_ACTION_MULTI
833 #undef DEFINE_ACTION_STRING
834 #undef DEFINE_ACTION_BOOL
835 #undef DEFINE_ACTION_ALIAS
837 /* trim leading <br> */
838 if (result && *result)
841 result = strdup(result + 5);
847 #endif /* def FEATURE_CGI_EDIT_ACTIONS */
850 /*********************************************************************
852 * Function : current_actions_to_text
854 * Description : Converts a actionsfile entry to text.
857 * 1 : action = Action
859 * Returns : A string. Caller must free it.
860 * NULL on out-of-memory error.
862 *********************************************************************/
863 char * current_action_to_text(struct current_action_spec *action)
865 unsigned flags = action->flags;
866 char * result = strdup("");
867 struct list_entry * lst;
869 #define DEFINE_ACTION_BOOL(__name, __bit) \
872 string_append(&result, " +" __name); \
876 string_append(&result, " -" __name); \
879 #define DEFINE_ACTION_STRING(__name, __bit, __index) \
882 string_append(&result, " +" __name "{"); \
883 string_append(&result, action->string[__index]); \
884 string_append(&result, "}"); \
888 string_append(&result, " -" __name); \
891 #define DEFINE_ACTION_MULTI(__name, __index) \
892 lst = action->multi[__index]->first; \
895 string_append(&result, " -" __name); \
901 string_append(&result, " +" __name "{"); \
902 string_append(&result, lst->str); \
903 string_append(&result, "}"); \
908 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
910 #include "actionlist.h"
912 #undef DEFINE_ACTION_MULTI
913 #undef DEFINE_ACTION_STRING
914 #undef DEFINE_ACTION_BOOL
915 #undef DEFINE_ACTION_ALIAS
921 /*********************************************************************
923 * Function : init_current_action
925 * Description : Zero out an action.
928 * 1 : dest = An uninitialized current_action_spec.
932 *********************************************************************/
933 void init_current_action (struct current_action_spec *dest)
935 memset(dest, '\0', sizeof(*dest));
937 dest->flags = ACTION_MOST_COMPATIBLE;
941 /*********************************************************************
943 * Function : init_action
945 * Description : Zero out an action.
948 * 1 : dest = An uninitialized action_spec.
952 *********************************************************************/
953 void init_action (struct action_spec *dest)
955 memset(dest, '\0', sizeof(*dest));
959 /*********************************************************************
961 * Function : merge_current_action
963 * Description : Merge two actions together.
964 * Similar to "dest += src".
965 * Differences between this and merge_actions()
966 * is that this one doesn't allocate memory for
967 * strings (so "src" better be in memory for at least
968 * as long as "dest" is, and you'd better free
969 * "dest" using "free_current_action").
970 * Also, there is no mask or remove lists in dest.
971 * (If we're applying it to a URL, we don't need them)
974 * 1 : dest = Current actions, to modify.
975 * 2 : src = Action to add.
977 * Returns 0 : no error
980 *********************************************************************/
981 jb_err merge_current_action (struct current_action_spec *dest,
982 const struct action_spec *src)
985 jb_err err = JB_ERR_OK;
987 dest->flags &= src->mask;
988 dest->flags |= src->add;
990 for (i = 0; i < ACTION_STRING_COUNT; i++)
992 char * str = src->string[i];
998 return JB_ERR_MEMORY;
1000 freez(dest->string[i]);
1001 dest->string[i] = str;
1005 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1007 if (src->multi_remove_all[i])
1009 /* Remove everything from dest, then add src->multi_add */
1010 err = list_duplicate(dest->multi[i], src->multi_add[i]);
1018 list_remove_list(dest->multi[i], src->multi_remove[i]);
1019 err = list_append_list_unique(dest->multi[i], src->multi_add[i]);
1030 /*********************************************************************
1032 * Function : free_current_action
1034 * Description : Free memory used by a current_action_spec.
1035 * Does not free the current_action_spec itself.
1038 * 1 : src = Source to free.
1042 *********************************************************************/
1043 void free_current_action (struct current_action_spec *src)
1047 for (i = 0; i < ACTION_STRING_COUNT; i++)
1049 freez(src->string[i]);
1052 for (i = 0; i < ACTION_MULTI_COUNT; i++)
1054 destroy_list(src->multi[i]);
1057 memset(src, '\0', sizeof(*src));
1061 /*********************************************************************
1063 * Function : unload_actions_file
1065 * Description : Unloads an actions module.
1068 * 1 : file_data = the data structure associated with the
1073 *********************************************************************/
1074 void unload_actions_file(void *file_data)
1076 struct url_actions * next;
1077 struct url_actions * cur = (struct url_actions *)file_data;
1082 free_action(cur->action);
1090 /*********************************************************************
1092 * Function : free_alias_list
1094 * Description : Free memory used by a list of aliases.
1097 * 1 : alias_list = Linked list to free.
1101 *********************************************************************/
1102 void free_alias_list(struct action_alias *alias_list)
1104 while (alias_list != NULL)
1106 struct action_alias * next = alias_list->next;
1107 alias_list->next = NULL;
1108 freez(alias_list->name);
1109 free_action(alias_list->action);
1116 /*********************************************************************
1118 * Function : load_actions_file
1120 * Description : Read and parse a action file and add to files
1124 * 1 : csp = Current client state (buffers, headers, etc...)
1126 * Returns : 0 => Ok, everything else is an error.
1128 *********************************************************************/
1129 int load_actions_file(struct client_state *csp)
1131 static struct file_list *current_actions_file = NULL;
1135 * Note: Keep these in the order they occur in the file, they are
1136 * sometimes tested with <=
1138 #define MODE_START_OF_FILE 1
1139 #define MODE_SETTINGS 2
1140 #define MODE_DESCRIPTION 3
1141 #define MODE_ALIAS 4
1142 #define MODE_ACTIONS 5
1144 int mode = MODE_START_OF_FILE;
1147 struct url_actions *last_perm;
1148 struct url_actions *perm;
1149 char buf[BUFFER_SIZE];
1150 struct file_list *fs;
1151 struct action_spec * cur_action = NULL;
1152 int cur_action_used = 0;
1153 struct action_alias * alias_list = NULL;
1154 unsigned long linenum = 0;
1156 if (!check_file_changed(current_actions_file, csp->config->actions_file, &fs))
1158 /* No need to load */
1161 csp->actions_list = current_actions_file;
1167 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error finding file: %E",
1168 csp->config->actions_file);
1169 return 1; /* never get here */
1172 fs->f = last_perm = (struct url_actions *)zalloc(sizeof(*last_perm));
1173 if (last_perm == NULL)
1175 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': out of memory!",
1176 csp->config->actions_file);
1177 return 1; /* never get here */
1180 if ((fp = fopen(csp->config->actions_file, "r")) == NULL)
1182 log_error(LOG_LEVEL_FATAL, "can't load actions file '%s': error opening file: %E",
1183 csp->config->actions_file);
1184 return 1; /* never get here */
1187 while (read_config_line(buf, sizeof(buf), fp, &linenum) != NULL)
1191 /* It's a header block */
1194 /* It's {{settings}} or {{alias}} */
1195 int len = strlen(buf);
1196 char * start = buf + 2;
1197 char * end = buf + len - 1;
1198 if ((len < 5) || (*end-- != '}') || (*end-- != '}'))
1202 log_error(LOG_LEVEL_FATAL,
1203 "can't load actions file '%s': invalid line (%lu): %s",
1204 csp->config->actions_file, linenum, buf);
1205 return 1; /* never get here */
1208 /* Trim leading and trailing whitespace. */
1216 log_error(LOG_LEVEL_FATAL,
1217 "can't load actions file '%s': invalid line (%lu): {{ }}",
1218 csp->config->actions_file, linenum);
1219 return 1; /* never get here */
1223 * An actionsfile can optionally contain the following blocks.
1224 * They *MUST* be in this order, to simplify processing:
1230 * ...free text, format TBD, but no line may start with a '{'...
1235 * The actual actions must be *after* these special blocks.
1236 * None of these special blocks may be repeated.
1239 if (0 == strcmpic(start, "settings"))
1241 /* it's a {{settings}} block */
1242 if (mode >= MODE_SETTINGS)
1244 /* {{settings}} must be first thing in file and must only
1248 log_error(LOG_LEVEL_FATAL,
1249 "can't load actions file '%s': line %lu: {{settings}} must only appear once, and it must be before anything else.",
1250 csp->config->actions_file, linenum);
1252 mode = MODE_SETTINGS;
1254 else if (0 == strcmpic(start, "description"))
1256 /* it's a {{description}} block */
1257 if (mode >= MODE_DESCRIPTION)
1259 /* {{description}} is a singleton and only {{settings}} may proceed it
1262 log_error(LOG_LEVEL_FATAL,
1263 "can't load actions file '%s': line %lu: {{description}} must only appear once, and only a {{settings}} block may be above it.",
1264 csp->config->actions_file, linenum);
1266 mode = MODE_DESCRIPTION;
1268 else if (0 == strcmpic(start, "alias"))
1270 /* it's an {{alias}} block */
1271 if (mode >= MODE_ALIAS)
1273 /* {{alias}} must be first thing in file, possibly after
1274 * {{settings}} and {{description}}
1276 * {{alias}} must only appear once.
1278 * Note that these are new restrictions introduced in
1279 * v2.9.10 in order to make actionsfile editing simpler.
1280 * (Otherwise, reordering actionsfile entries without
1281 * completely rewriting the file becomes non-trivial)
1284 log_error(LOG_LEVEL_FATAL,
1285 "can't load actions file '%s': line %lu: {{alias}} must only appear once, and it must be before all actions.",
1286 csp->config->actions_file, linenum);
1292 /* invalid {{something}} block */
1294 log_error(LOG_LEVEL_FATAL,
1295 "can't load actions file '%s': invalid line (%lu): {{%s}}",
1296 csp->config->actions_file, linenum, start);
1297 return 1; /* never get here */
1302 /* It's an actions block */
1304 char actions_buf[BUFFER_SIZE];
1308 mode = MODE_ACTIONS;
1310 /* free old action */
1313 if (!cur_action_used)
1315 free_action(cur_action);
1320 cur_action_used = 0;
1321 cur_action = (struct action_spec *)zalloc(sizeof(*cur_action));
1322 if (cur_action == NULL)
1325 log_error(LOG_LEVEL_FATAL,
1326 "can't load actions file '%s': out of memory",
1327 csp->config->actions_file);
1328 return 1; /* never get here */
1330 init_action(cur_action);
1333 strcpy(actions_buf, buf + 1);
1335 /* check we have a trailing } and then trim it */
1336 end = actions_buf + strlen(actions_buf) - 1;
1341 log_error(LOG_LEVEL_FATAL,
1342 "can't load actions file '%s': invalid line (%lu): %s",
1343 csp->config->actions_file, linenum, buf);
1344 return 1; /* never get here */
1348 /* trim any whitespace immediately inside {} */
1351 if (get_actions(actions_buf, alias_list, cur_action))
1355 log_error(LOG_LEVEL_FATAL,
1356 "can't load actions file '%s': invalid line (%lu): %s",
1357 csp->config->actions_file, linenum, buf);
1358 return 1; /* never get here */
1362 else if (mode == MODE_SETTINGS)
1365 * Part of the {{settings}} block.
1366 * Ignore for now, but we may want to read & check permissions
1367 * when we go multi-user.
1370 else if (mode == MODE_DESCRIPTION)
1373 * Part of the {{description}} block.
1377 else if (mode == MODE_ALIAS)
1382 char actions_buf[BUFFER_SIZE];
1383 struct action_alias * new_alias;
1385 char * start = strchr(buf, '=');
1388 if ((start == NULL) || (start == buf))
1390 log_error(LOG_LEVEL_FATAL,
1391 "can't load actions file '%s': invalid alias line (%lu): %s",
1392 csp->config->actions_file, linenum, buf);
1393 return 1; /* never get here */
1396 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1399 log_error(LOG_LEVEL_FATAL,
1400 "can't load actions file '%s': out of memory!",
1401 csp->config->actions_file);
1402 return 1; /* never get here */
1405 /* Eat any the whitespace before the '=' */
1407 while ((*end == ' ') || (*end == '\t'))
1410 * we already know we must have at least 1 non-ws char
1411 * at start of buf - no need to check
1417 /* Eat any the whitespace after the '=' */
1419 while ((*start == ' ') || (*start == '\t'))
1425 log_error(LOG_LEVEL_FATAL,
1426 "can't load actions file '%s': invalid alias line (%lu): %s",
1427 csp->config->actions_file, linenum, buf);
1428 return 1; /* never get here */
1431 new_alias->name = strdup(buf);
1433 strcpy(actions_buf, start);
1435 if (get_actions(actions_buf, alias_list, new_alias->action))
1439 log_error(LOG_LEVEL_FATAL,
1440 "can't load actions file '%s': invalid alias line (%lu): %s = %s",
1441 csp->config->actions_file, linenum, buf, start);
1442 return 1; /* never get here */
1446 new_alias->next = alias_list;
1447 alias_list = new_alias;
1449 else if (mode == MODE_ACTIONS)
1451 /* it's a URL pattern */
1453 /* allocate a new node */
1454 if ((perm = zalloc(sizeof(*perm))) == NULL)
1457 log_error(LOG_LEVEL_FATAL,
1458 "can't load actions file '%s': out of memory!",
1459 csp->config->actions_file);
1460 return 1; /* never get here */
1464 copy_action (perm->action, cur_action);
1466 /* Save the URL pattern */
1467 if (create_url_spec(perm->url, buf))
1470 log_error(LOG_LEVEL_FATAL,
1471 "can't load actions file '%s': line %lu: cannot create URL pattern from: %s",
1472 csp->config->actions_file, linenum, buf);
1473 return 1; /* never get here */
1476 /* add it to the list */
1477 last_perm->next = perm;
1480 else if (mode == MODE_START_OF_FILE)
1482 /* oops - please have a {} line as 1st line in file. */
1484 log_error(LOG_LEVEL_FATAL,
1485 "can't load actions file '%s': first needed line (%lu) is invalid: %s",
1486 csp->config->actions_file, linenum, buf);
1487 return 1; /* never get here */
1491 /* How did we get here? This is impossible! */
1493 log_error(LOG_LEVEL_FATAL,
1494 "can't load actions file '%s': INTERNAL ERROR - mode = %d",
1495 csp->config->actions_file, mode);
1496 return 1; /* never get here */
1502 free_action(cur_action);
1504 free_alias_list(alias_list);
1506 /* the old one is now obsolete */
1507 if (current_actions_file)
1509 current_actions_file->unloader = unload_actions_file;
1512 fs->next = files->next;
1514 current_actions_file = fs;
1518 csp->actions_list = fs;