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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/*
* queue.c - The queue command
*
* Queues allow for future batch processing
*
* Syntax: /QUEUE -DO -SHOW -LIST -NO_FLUSH -DELETE -FLUSH <name> {commands}
*
* Written by Jeremy Nelson
*
* Copyright (C) 1993. See the copyright file and all that rot
*/
#ifndef lint
static char rcsid[] = "@(#)$Id: queue.c,v 1.2 2001/12/09 02:20:28 toast Exp $";
#endif
#include "irc.h"
#include "alias.h"
#include "ircaux.h"
#include "debug.h"
#include "output.h"
#include "edit.h"
#include "if.h"
#include "queue.h"
typedef struct CmdListT
{
struct CmdListT *next;
char *what;
} CmdList;
typedef struct QueueT
{
struct QueueT *next;
struct CmdListT *first;
char *name;
} Queue;
void queuecmd _((char *, char *, char *));
static Queue *lookup_queue _((Queue *, char *));
static CmdList *walk_commands _((Queue *));
static Queue *make_new_queue _((Queue *, char *));
static int add_commands_to_queue _((Queue *, char *what, char *));
static int delete_commands_from_queue _((Queue *, int));
static Queue *remove_a_queue _((Queue *));
static void flush_queue _((Queue *));
static Queue *do_queue _((Queue *, int));
static void display_all_queues _((Queue *));
static void print_queue _((Queue *));
static int num_entries _((Queue *));
static Queue *Queuelist = (Queue *) 0;
extern void
queuecmd(cmd, args, subargs)
char *cmd,
*args,
*subargs;
{
Queue *tmp;
char *arg = (char *) 0,
*name = (char *) 0,
*startcmds = (char *) 0,
*cmds = (char *) 0;
int noflush = 0,
runit = 0,
list = 0,
flush = 0,
remove_by_number = 0,
commands = 1,
number = 0;
/* If the queue list is empty, make an entry */
if (Queuelist == (Queue *) 0)
Queuelist = make_new_queue((Queue *) 0, "Top");
if ((startcmds = index(args, '{')) == (char *) 0) /* } */
commands = 0;
else
*(startcmds-1) = '\0';
while ((arg = upper(next_arg(args, &args))) != (char *) 0)
{
if (*arg == '-')
{
*arg++ = '\0';
if (!strcmp(arg, "NO_FLUSH"))
noflush = 1;
else
if (!strcmp(arg, "SHOW"))
{
display_all_queues(Queuelist);
return;
}
else
if (!strcmp(arg, "LIST"))
list = 1;
else
if (!strcmp(arg, "DO"))
runit = 1;
else
if (!strcmp(arg, "DELETE"))
remove_by_number = 1;
else
if (!strcmp(arg, "FLUSH"))
flush = 1;
}
else
{
if (name)
number = atoi(arg);
else
name = arg;
}
}
if (name == (char *) 0)
return;
/* Find the queue based upon the previous queue */
tmp = lookup_queue(Queuelist, name);
/* if the next queue is empty, then we need to see if
we should make it or output an error */
if ((tmp->next) == (Queue *) 0)
{
if (commands)
tmp->next = make_new_queue((Queue *) 0, name);
else
{
yell ("QUEUE: (%s) no such queue",name);
return;
}
}
if (remove_by_number == 1)
{
if (delete_commands_from_queue(tmp->next,number))
tmp->next = remove_a_queue(tmp->next);
}
if (list == 1)
{
print_queue(tmp->next);
}
if (runit == 1)
{
tmp->next = do_queue(tmp->next, noflush);
}
if (flush == 1)
{
tmp->next = remove_a_queue(tmp->next);
}
if (startcmds)
{
int booya;
if ((cmds = next_expr(&startcmds, '{')) == (char *) 0) /* } */
{
yell ("QUEUE: missing closing brace");
return;
}
booya = add_commands_to_queue (tmp->next, cmds, subargs);
say ("QUEUED: %s now has %d entries",name, booya);
}
}
/*
* returns the queue BEFORE the queue we are looking for
* returns the last queue if no match
*/
static Queue *
lookup_queue(queue, what)
Queue *queue;
char *what;
{
Queue *tmp = queue;
upper(what);
while (tmp && tmp->next)
{
if (!strcmp(tmp->next->name, what))
return tmp;
else
if (tmp->next)
tmp = tmp->next;
else
break;
}
return tmp;
}
/* returns the last CmdList in a queue, useful for appending commands */
static CmdList *
walk_commands(queue)
Queue *queue;
{
CmdList *ctmp;
if (!queue)
return (CmdList *) 0;
ctmp = queue->first;
if (ctmp)
{
while (ctmp->next)
ctmp = ctmp->next;
return ctmp;
}
return (CmdList *) 0;
}
/*----------------------------------------------------------------*/
/* Make a new queue, link it in, and return it. */
static Queue *
make_new_queue(afterqueue, name)
Queue *afterqueue;
char *name;
{
Queue *tmp;
if (!name)
return (Queue *) 0;
tmp = (Queue *) new_malloc(sizeof(Queue));
tmp->next = afterqueue;
tmp->first = (CmdList *) 0;
tmp->name = (char *) 0;
malloc_strcpy(&tmp->name, name);
upper(tmp->name);
return tmp;
}
/* add a command to a queue, at the end of the list */
/* expands the whole thing once and stores it */
static int
add_commands_to_queue(queue, what, subargs)
Queue *queue;
char *what;
char *subargs;
{
CmdList *ctmp = walk_commands(queue);
char *list = (char *) 0,
*sa;
int args_flag = 0;
sa = subargs ? subargs : " ";
list = expand_alias((char *) 0, what, sa, &args_flag, (char **) 0);
if (!ctmp)
{
queue->first = (CmdList *) new_malloc(sizeof(CmdList));
ctmp = queue->first;
}
else
{
ctmp->next = (CmdList *) new_malloc(sizeof(CmdList));
ctmp = ctmp->next;
}
ctmp->what = (char *) 0;
malloc_strcpy(&ctmp->what, list);
ctmp->next = (CmdList *) 0;
return num_entries(queue);
}
/* remove the Xth command from the queue */
static int
delete_commands_from_queue(queue, which)
Queue *queue;
int which;
{
CmdList *ctmp = queue->first;
CmdList *blah;
int x;
if (which == 1)
queue->first = ctmp->next;
else
{
for (x=1;x<which-1;x++)
{
if (ctmp->next)
ctmp = ctmp->next;
else
return 0;
}
blah = ctmp->next;
ctmp->next = ctmp->next->next;
ctmp = blah;
}
new_free(&ctmp->what);
new_free(&ctmp);
if (queue->first == (CmdList *) 0)
return 1;
else
return 0;
}
/*-------------------------------------------------------------------*/
/* flush a queue, deallocate the memory, and return the next in line */
static Queue *
remove_a_queue(queue)
Queue *queue;
{
Queue *tmp;
tmp = queue->next;
flush_queue(queue);
new_free(&queue);
return tmp;
}
/* walk through a queue, deallocating the entries */
static void
flush_queue(queue)
Queue *queue;
{
CmdList *tmp,
*tmp2;
tmp = queue->first;
while (tmp != (CmdList *) 0)
{
tmp2 = tmp;
tmp = tmp2->next;
if (tmp2->what != (char *) 0)
new_free(&tmp2->what);
if (tmp2)
new_free(&tmp2);
}
}
/*------------------------------------------------------------------------*/
/* run the queue, and if noflush, then return the queue, else return the
next queue */
static Queue *
do_queue(queue, noflush)
Queue *queue;
int noflush;
{
CmdList *tmp;
tmp = queue->first;
do
{
if (tmp->what != (char *) 0)
parse_line((char *) 0, tmp->what, empty_string, 0, 0);
tmp = tmp->next;
}
while (tmp != (CmdList *) 0);
if (!noflush)
return remove_a_queue(queue);
else
return queue;
}
/* ---------------------------------------------------------------------- */
/* output the contents of all the queues to the screen */
static void
display_all_queues(queue)
Queue *queue;
{
Queue *tmp;
if (!queue)
return;
tmp = queue->next;
while (tmp)
{
print_queue(tmp);
if (tmp->next == (Queue *) 0)
return;
else
tmp = tmp->next;
}
yell("QUEUE: No more queues");
}
/* output the contents of a queue to the screen */
static void
print_queue(queue)
Queue *queue;
{
CmdList *tmp;
int x = 0;
tmp = queue->first;
while (tmp != (CmdList *) 0)
{
if (tmp->what)
say ("<%s:%2d> %s",queue->name,++x,tmp->what);
tmp = tmp->next;
}
say ("<%s> End of queue",queue->name);
}
/* return the number of entries in a queue */
static int
num_entries(queue)
Queue *queue;
{
int x = 1;
CmdList *tmp;
if ((tmp = queue->first) == (CmdList *) 0)
return 0;
while (tmp->next)
{
x++;
tmp = tmp->next;
}
return x;
}
|