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 417 418 419 420 421 422 423 424 425 426 427 428
|
/*
* Configurable ps-like program.
* Sorting support routines.
*
* Copyright (c) 2010 David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*/
#include "ips.h"
#include "expr.h"
#define MAX_SORT 50 /* maximum items to allow sorting by */
#define SORT_ALLOC_SIZE 100 /* reallocation size for sorting */
/*
* Types of values to sort on.
*/
typedef int SORT_TYPE;
#define SORT_NORMAL 1 /* normal sort on column value */
#define SORT_REVERSE 2 /* reverse sort on column value */
#define SORT_NORMAL_EXPR 3 /* sort on expression evaluation */
#define SORT_REVERSE_EXPR 4 /* reverse sort on expression */
/*
* One item of sorting.
*/
typedef struct
{
SORT_TYPE type; /* type of sort */
COLUMN * column; /* column to sort by */
TREE * tree; /* expression tree to sort by */
} SORT_ITEM;
/*
* Data to specify sorting conditions.
*/
static int sortCount; /* number of items in table */
static SORT_ITEM sortTable[MAX_SORT]; /* table of sorting items */
/*
* Data used during the actual sorting.
*/
static int procSortSize; /* entries in sort table */
static PROC ** procSortTable; /* proc sorting table */
/*
* Local procedures.
*/
static int SortFunction(const void * e1, const void * e2);
static int CompareSortingItems(const PROC * proc1, const PROC * proc2);
/*
* Get the use flags for all the columns that we sort by.
* This just means find all references to columns, and OR together
* the use flags for each column.
*/
USEFLAG
GetSortingUseFlags(void)
{
const SORT_ITEM * item;
USEFLAG flags;
flags = USE_NONE;
for (item = sortTable; item < &sortTable[sortCount]; item++)
{
switch (item->type)
{
case SORT_NORMAL:
case SORT_REVERSE:
flags |= item->column->useFlag;
break;
case SORT_NORMAL_EXPR:
case SORT_REVERSE_EXPR:
flags |= GetNodeUseFlags(item->tree->root);
break;
default:
break;
}
}
return flags;
}
/*
* Clear all sorting.
*/
void
ClearSorting(void)
{
sortCount = 0;
}
/*
* Append items to do sorting according to the indicated column names.
* The sorting can be the normal forwards sort, or else a reverse sort.
* The arguments are column names, no expressions are allowed here.
*/
BOOL
AppendColumnSort(ARGS * ap, BOOL reverse)
{
SORT_TYPE type;
SORT_ITEM * item;
const char * name;
COLUMN * column;
int count;
int i;
char * table[MAX_WORDS];
type = (reverse ? SORT_REVERSE : SORT_NORMAL);
count = ExpandArguments(ap, table, MAX_WORDS);
if (count < 0)
return FALSE;
for (i = 0; i < count; i++)
{
if (sortCount >= MAX_SORT)
{
fprintf(stderr, "Too many sort items\n");
return FALSE;
}
name = table[i];
column = FindColumn(name);
if (column == NULL)
{
fprintf(stderr, "Bad sorting column name %s\n", name);
return FALSE;
}
item = &sortTable[sortCount];
item->type = type;
item->column = column;
item->tree = NULL;
sortCount++;
}
return TRUE;
}
/*
* Routine to append a sort expression to the sorting list.
* The sorting can be the normal forwards sort, or else a reverse sort.
*/
BOOL
AppendExpressionSort(ARGS * ap, BOOL reverse)
{
SORT_TYPE type;
SORT_ITEM * item;
char * str;
type = (reverse ? SORT_REVERSE_EXPR : SORT_NORMAL_EXPR);
if (ap->count <= 0)
{
fprintf(stderr, "Missing sort expression\n");
return FALSE;
}
ap->count--;
str = *ap->table++;
if (sortCount >= MAX_SORT)
{
fprintf(stderr, "Too many sort items\n");
return FALSE;
}
item = &sortTable[sortCount];
item->type = type;
item->column = NULL;
item->tree = (TREE *) malloc(sizeof(TREE));
if (item->tree == NULL)
{
fprintf(stderr, "Cannot allocate memory\n");
return FALSE;
}
if (!ParseTree(item->tree, str, 0))
return FALSE;
sortCount++;
return TRUE;
}
/*
* Sort the process list.
*/
void
SortProcesses(void)
{
int count;
PROC * proc;
PROC ** procPtr;
/*
* If we need to grow the sorting table to include new processes,
* then do so.
*/
if (procAllocCount >= procSortSize)
{
procSortSize = procAllocCount + SORT_ALLOC_SIZE;
procSortTable = (PROC **) realloc(procSortTable,
(sizeof(PROC *) * (procSortSize + 1)));
if (procSortTable == NULL)
{
fprintf(stderr, "Cannot allocate memory\n");
exit(1);
}
}
/*
* Put the process entries into the sort table.
*/
count = 0;
for (proc = processList; proc; proc = proc->next)
procSortTable[count++] = proc;
procSortTable[count] = NULL_PROC;
/*
* Sort the entries in the table.
*/
qsort((void *) procSortTable, count, sizeof(PROC *), SortFunction);
/*
* Relink the processes into a new list according to the sorted
* table pointers.
*/
procPtr = procSortTable;
processList = *procPtr;
while (*procPtr)
{
procPtr[0]->next = procPtr[1];
procPtr++;
}
}
/*
* Function called by qsort to compare two processes for the correct
* sorting order. Returns -1 if the first process is first, 1 if the
* second process is first, or 0 if they are equal. (But there should
* be no case where two different processes actually sort as equal.)
*/
static int
SortFunction(const void * e1, const void * e2)
{
const PROC ** procPtr1;
const PROC ** procPtr2;
const PROC * proc1;
const PROC * proc2;
int status;
procPtr1 = (const PROC **) e1;
procPtr2 = (const PROC **) e2;
proc1 = *procPtr1;
proc2 = *procPtr2;
/*
* Check for processes which are not to be shown so as to
* avoid any hard work comparing them to sort them.
*/
if (!proc1->isShown || !proc2->isShown)
{
if (proc1->isShown)
return -1;
if (proc2->isShown)
return 1;
return 0;
}
/*
* Do sorting based on any specified sorting items,
* which can be column names or arbitrary expressions.
*/
status = CompareSortingItems(proc1, proc2);
if (status != 0)
return status;
/*
* They appear equal for all sorting items, so do a sort on
* their process ids.
*/
if (proc1->pid < proc2->pid)
return -1;
if (proc1->pid > proc2->pid)
return 1;
/*
* The process ids match, so sort on their thread ids.
* Always put the main thread first.
*/
if ((proc1->tid == NO_THREAD_ID) && (proc2->tid != NO_THREAD_ID))
return -1;
if ((proc1->tid != NO_THREAD_ID) && (proc2->tid == NO_THREAD_ID))
return 1;
if ((proc1->tid == proc1->pid) && (proc2->tid != proc2->pid))
return -1;
if ((proc1->tid != proc1->pid) && (proc2->tid == proc2->pid))
return 1;
if (proc1->tid < proc2->tid)
return -1;
if (proc1->tid > proc2->tid)
return 1;
/*
* There are multiple processes with the same ids.
* Sort on their start times (newer processes first).
*/
if (proc1->startTimeTicks > proc2->startTimeTicks)
return -1;
if (proc1->startTimeTicks < proc2->startTimeTicks)
return -1;
return 0;
}
/*
* Compare two processes using the defined list of sorting items.
* The comparison can be on column values or on arbitrary expressions.
* Returns -1 if the first process is less, 1 if the first process is
* more, or 0 if they are identical.
*/
static int
CompareSortingItems(const PROC * proc1, const PROC * proc2)
{
SORT_ITEM * item;
TREE * tree;
int status;
VALUE value1;
VALUE value2;
if (proc1 == proc2)
return 0;
for (item = sortTable; item < &sortTable[sortCount]; item++)
{
switch (item->type)
{
case SORT_NORMAL:
status = item->column->sortFunc(proc1, proc2);
break;
case SORT_REVERSE:
status = -item->column->sortFunc(proc1, proc2);
break;
case SORT_NORMAL_EXPR:
case SORT_REVERSE_EXPR:
tree = item->tree;
tree->proc = proc1;
value1 = EvaluateNode(tree, tree->root);
tree->proc = proc2;
value2 = EvaluateNode(tree, tree->root);
if (!CompareValues(value1, value2, &status))
return 0;
if (item->type == SORT_REVERSE_EXPR)
status = -status;
break;
default:
status = 0;
break;
}
if (status != 0)
return status;
}
return 0;
}
/* END CODE */
|