1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/*
Stack filter
Copyright (C) 2002 Robert Lipe, robertlipe@usa.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
*/
#include <stdio.h>
#include "defs.h"
#define MYNAME "Stack filter"
extern queue waypt_head;
static char *opt_push = NULL;
static char *opt_copy = NULL;
static char *opt_pop = NULL;
static char *opt_append = NULL;
static char *opt_discard = NULL;
static char *opt_replace = NULL;
static char *opt_swap = NULL;
static char *opt_depth = NULL;
static char *nowarn = NULL;
static int warnings_enabled = 1;
static int swapdepth = 0;
static
arglist_t stackfilt_args[] = {
{"push", &opt_push, "Push waypoint list onto stack", NULL,
ARGTYPE_BOOL},
{"copy", &opt_copy, "Copy waypoint list when pushing", NULL,
ARGTYPE_BOOL},
{"pop", &opt_pop, "Pop waypoint list from stack", NULL,
ARGTYPE_BOOL},
{"append", &opt_append, "Append list when popping", NULL,
ARGTYPE_BOOL},
{"discard", &opt_discard, "Discard top of stack when popping",
NULL, ARGTYPE_BOOL},
{"replace", &opt_replace, "Replace list with top of stack (default)",
NULL, ARGTYPE_BOOL},
{"swap", &opt_swap, "Swap waypoint list with <depth> item on stack",
NULL, ARGTYPE_BOOL},
{"depth", &opt_depth, "Item to use when swapping", NULL, ARGTYPE_INT},
{"nowarn", &nowarn, "Suppress cleanup warning", NULL,
ARGTYPE_INT | ARGTYPE_HIDDEN},
{0, 0, 0, 0, 0}
};
struct stack_elt {
queue waypts;
struct stack_elt *next;
} *stack = NULL;
void
stackfilt_process(void)
{
struct stack_elt *tmp_elt = NULL;
queue *elem = NULL;
queue *tmp = NULL;
queue tmp_queue;
waypoint *wpt_tmp;
if ( opt_push ) {
tmp_elt = (struct stack_elt *)xmalloc(sizeof(struct stack_elt));
QUEUE_MOVE(&(tmp_elt->waypts), &waypt_head);
tmp_elt->next = stack;
stack = tmp_elt;
if ( opt_copy ) {
QUEUE_FOR_EACH( &(stack->waypts), elem, tmp ) {
waypt_add( waypt_dupe((waypoint *)elem));
}
}
}
else if ( opt_pop ) {
tmp_elt = stack;
if ( !tmp_elt ) {
fatal( MYNAME ": stack empty\n");
}
if ( opt_append ) {
QUEUE_FOR_EACH( &(stack->waypts), elem, tmp ) {
waypt_add( (waypoint *)elem);
}
}
else if ( opt_discard ) {
waypt_flush( &(stack->waypts) );
}
else {
waypt_flush( &waypt_head );
QUEUE_MOVE(&(waypt_head), &(stack->waypts) );
}
stack = tmp_elt->next;
xfree( tmp_elt );
}
else if ( opt_swap ) {
tmp_elt = stack;
while ( swapdepth > 1 ) {
if ( !tmp_elt->next ) {
fatal (MYNAME ": swap with nonexistent element\n");
}
tmp_elt = tmp_elt->next;
swapdepth--;
}
QUEUE_MOVE(&tmp_queue, &(tmp_elt->waypts) );
QUEUE_MOVE(&(tmp_elt->waypts), &waypt_head );
QUEUE_MOVE(&waypt_head, &tmp_queue );
}
}
void
stackfilt_init(const char *args) {
int invalid = 0;
if ( nowarn ) {
warnings_enabled = 0;
}
if ( opt_depth ) {
swapdepth = atoi( opt_depth );
}
if ( opt_push ) {
if ( opt_pop || opt_append || opt_discard || opt_replace ||
opt_swap || opt_depth ) {
invalid = 1;
}
}
else if ( opt_pop ) {
if ( opt_push || opt_copy || opt_swap || opt_depth ) {
invalid = 1;
}
if ( !!opt_append + !!opt_discard + !!opt_replace > 1 ) {
invalid = 1;
}
}
else if ( opt_swap ) {
if ( opt_push || opt_copy || opt_pop || opt_append ||
opt_discard || opt_replace ) {
invalid = 1;
}
}
else {
invalid = 1;
}
if ( invalid ) {
fatal (MYNAME ": invalid combination of options\n");
}
}
void
stackfilt_deinit(void) {
swapdepth = 0;
}
void
stackfilt_exit( void ) {
struct stack_elt *tmp_elt = NULL;
if ( warnings_enabled && stack ) {
warning( MYNAME " Warning: leftover stack entries; "
"check command line for mistakes\n" );
}
while ( stack ) {
waypt_flush( &(stack->waypts) );
tmp_elt = stack;
stack = stack->next;
xfree(tmp_elt);
}
}
filter_vecs_t stackfilt_vecs = {
stackfilt_init,
stackfilt_process,
stackfilt_deinit,
stackfilt_exit,
stackfilt_args
};
|