summaryrefslogtreecommitdiff
path: root/AppPkg/Applications/OrderedCollectionTest/OrderedCollectionTest.c
blob: 6d371f90cbf29b0b48f9ad479064115c88132c3f (plain)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/** @file
  A simple "fuzzer" application for OrderedCollectionLib, reading commands from
  the standard input, and writing results to the standard output.

  Make sure you configure your platform so that the console stderr device is
  visible to the user (or else run the program from wherever stderr is visible
  per default, eg. serial line).

  Copyright (C) 2014, Red Hat, Inc.
  Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>

  This program and the accompanying materials are licensed and made available
  under the terms and conditions of the BSD License which accompanies this
  distribution. The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include <assert.h> // assert()
#include <errno.h>  // errno
#include <limits.h> // INT_MIN
#include <stdio.h>  // fgets()
#include <stdlib.h> // EXIT_FAILURE
#include <string.h> // strerror()
#include <unistd.h> // getopt()

#include <Library/OrderedCollectionLib.h>

//
// We allow the user to select between stdin+stdout and regular input+output
// files via command line options. We don't rely on shell redirection for two
// reasons:
//
// - The "old shell" doesn't support input redirection (<a, <);
//
// - The "new shell" supports input redirection (<a, <), but those redirections
//   break fgets(stdin). Both when redirecting stdin from an ASCII file (<a),
//   and when redirecting stdin from a UCS-2 file (<), the very first fgets()
//   spirals into an infinite loop, spewing ^@ on the serial console.
//
// Performing fopen() manually (made available to the user with the -i option),
// and reading from that stream with fgets() work under both old and new shell.
// Only ASCII encoded files are supported.
//
static FILE *Input, *Output;


//
// Define a (potentially aggregate) key type.
//
typedef struct {
  int Value;
} USER_KEY;

//
// The user structure includes the key as one of its fields. (There can be
// several, optionally differently typed, keys, if we link user structures into
// several collections, with different comparators.)
//
typedef struct {
  unsigned char  Supplementary1[4];
  USER_KEY       Key;
  unsigned short Supplementary2[2];
} USER_STRUCT;


/**
  Compare a standalone key against a user structure containing an embedded key.

  @param[in] StandaloneKey  Pointer to the bare key.

  @param[in] UserStruct     Pointer to the user structure with the embedded
                            key.

  @retval <0  If StandaloneKey compares less than UserStruct's key.

  @retval  0  If StandaloneKey compares equal to UserStruct's key.

  @retval >0  If StandaloneKey compares greater than UserStruct's key.
**/
static
INTN
EFIAPI
KeyCompare (
  IN CONST VOID *StandaloneKey,
  IN CONST VOID *UserStruct
  )
{
  const USER_KEY    *CmpKey;
  const USER_STRUCT *CmpStruct;

  CmpKey    = StandaloneKey;
  CmpStruct = UserStruct;

  return CmpKey->Value < CmpStruct->Key.Value ? -1 :
         CmpKey->Value > CmpStruct->Key.Value ?  1 :
         0;
}


/**
  Comparator function type for two user structures.

  @param[in] UserStruct1  Pointer to the first user structure.

  @param[in] UserStruct2  Pointer to the second user structure.

  @retval <0  If UserStruct1 compares less than UserStruct2.

  @retval  0  If UserStruct1 compares equal to UserStruct2.

  @retval >0  If UserStruct1 compares greater than UserStruct2.
**/
static
INTN
EFIAPI
UserStructCompare (
  IN CONST VOID *UserStruct1,
  IN CONST VOID *UserStruct2
  )
{
  const USER_STRUCT *CmpStruct1;

  CmpStruct1 = UserStruct1;
  return KeyCompare (&CmpStruct1->Key, UserStruct2);
}


/**
  Empty the collection by iterating forward through its entries.

  This function demonstrates that iterators different from the one being
  removed remain valid.

  @param[in,out] Collection  The collection to empty.
**/
static void
CmdForwardEmpty (
  IN OUT ORDERED_COLLECTION *Collection
  )
{
  ORDERED_COLLECTION_ENTRY *Entry;

  Entry = OrderedCollectionMin (Collection);
  while (Entry != NULL) {
    ORDERED_COLLECTION_ENTRY *Next;
    void                     *Ptr;
    USER_STRUCT              *UserStruct;

    Next = OrderedCollectionNext (Entry);
    OrderedCollectionDelete (Collection, Entry, &Ptr);

    UserStruct = Ptr;
    fprintf (Output, "%s: %d: removed\n", __FUNCTION__, UserStruct->Key.Value);
    free (UserStruct);

    Entry = Next;
  }
}


/**
  Empty the collection by iterating backward through its entries.

  This function demonstrates that iterators different from the one being
  removed remain valid.

  @param[in,out] Collection  The collection to empty.
**/
static void
CmdBackwardEmpty (
  IN OUT ORDERED_COLLECTION *Collection
  )
{
  ORDERED_COLLECTION_ENTRY *Entry;

  Entry = OrderedCollectionMax (Collection);
  while (Entry != NULL) {
    ORDERED_COLLECTION_ENTRY *Prev;
    void                     *Ptr;
    USER_STRUCT              *UserStruct;

    Prev = OrderedCollectionPrev (Entry);
    OrderedCollectionDelete (Collection, Entry, &Ptr);

    UserStruct = Ptr;
    fprintf (Output, "%s: %d: removed\n", __FUNCTION__, UserStruct->Key.Value);
    free (UserStruct);

    Entry = Prev;
  }
}


/**
  List the user structures linked into the collection, in increasing order.

  @param[in] Collection  The collection to list.
**/
static void
CmdForwardList (
  IN ORDERED_COLLECTION *Collection
  )
{
  ORDERED_COLLECTION_ENTRY *Entry;

  for (Entry = OrderedCollectionMin (Collection); Entry != NULL;
       Entry = OrderedCollectionNext (Entry)) {
    USER_STRUCT  *UserStruct;

    UserStruct = OrderedCollectionUserStruct (Entry);
    fprintf (Output, "%s: %d\n", __FUNCTION__, UserStruct->Key.Value);
  }
}


/**
  List the user structures linked into the collection, in decreasing order.

  @param[in] Collection  The collection to list.
**/
static void
CmdBackwardList (
  IN ORDERED_COLLECTION *Collection
  )
{
  ORDERED_COLLECTION_ENTRY *Entry;

  for (Entry = OrderedCollectionMax (Collection); Entry != NULL;
       Entry = OrderedCollectionPrev (Entry)) {
    USER_STRUCT  *UserStruct;

    UserStruct = OrderedCollectionUserStruct (Entry);
    fprintf (Output, "%s: %d\n", __FUNCTION__, UserStruct->Key.Value);
  }
}


/**
  Create a new user structure and attempt to insert it into the collection.

  @param[in]     Value       The key value of the user structure to create.

  @param[in,out] Collection  The collection to insert the new user structure
                             into.
**/
static void
CmdInsert (
  IN     int     Value,
  IN OUT ORDERED_COLLECTION *Collection
  )
{
  USER_STRUCT              *UserStruct, *UserStruct2;
  RETURN_STATUS            Status;
  ORDERED_COLLECTION_ENTRY *Entry;

  UserStruct = calloc (1, sizeof *UserStruct);
  if (UserStruct == NULL) {
    fprintf (Output, "%s: %d: calloc(): out of memory\n", __FUNCTION__, Value);
    return;
  }

  UserStruct->Key.Value = Value;
  Status = OrderedCollectionInsert (Collection, &Entry, UserStruct);

  switch (Status) {
  case RETURN_OUT_OF_RESOURCES:
    fprintf (Output, "%s: %d: OrderedCollectionInsert(): out of memory\n",
      __FUNCTION__, Value);
    goto ReleaseUserStruct;

  case RETURN_ALREADY_STARTED:
    UserStruct2 = OrderedCollectionUserStruct (Entry);
    assert (UserStruct != UserStruct2);
    assert (UserStruct2->Key.Value == Value);
    fprintf (Output, "%s: %d: already exists\n", __FUNCTION__,
      UserStruct2->Key.Value);
    goto ReleaseUserStruct;

  default:
    assert (Status == RETURN_SUCCESS);
    break;
  }

  assert (OrderedCollectionUserStruct (Entry) == UserStruct);
  fprintf (Output, "%s: %d: inserted\n", __FUNCTION__, Value);
  return;

ReleaseUserStruct:
  free (UserStruct);
}


/**
  Look up a user structure by key in the collection and print it.

  @param[in] Value       The key of the user structure to find.

  @param[in] Collection  The collection to search for the user structure with
                         the key.
**/
static void
CmdFind (
  IN int                Value,
  IN ORDERED_COLLECTION *Collection
  )
{
  USER_KEY                 StandaloneKey;
  ORDERED_COLLECTION_ENTRY *Entry;
  USER_STRUCT              *UserStruct;

  StandaloneKey.Value = Value;
  Entry = OrderedCollectionFind (Collection, &StandaloneKey);

  if (Entry == NULL) {
    fprintf (Output, "%s: %d: not found\n", __FUNCTION__, Value);
    return;
  }

  UserStruct = OrderedCollectionUserStruct (Entry);
  assert (UserStruct->Key.Value == StandaloneKey.Value);
  fprintf (Output, "%s: %d: found\n", __FUNCTION__, UserStruct->Key.Value);
}


/**
  Look up a user structure by key in the collection and delete it.

  @param[in] Value       The key of the user structure to find.

  @param[in] Collection  The collection to search for the user structure with
                         the key.
**/
static void
CmdDelete (
  IN int                Value,
  IN ORDERED_COLLECTION *Collection
  )
{
  USER_KEY                 StandaloneKey;
  ORDERED_COLLECTION_ENTRY *Entry;
  void                     *Ptr;
  USER_STRUCT              *UserStruct;

  StandaloneKey.Value = Value;
  Entry = OrderedCollectionFind (Collection, &StandaloneKey);

  if (Entry == NULL) {
    fprintf (Output, "%s: %d: not found\n", __FUNCTION__, Value);
    return;
  }

  OrderedCollectionDelete (Collection, Entry, &Ptr);

  UserStruct = Ptr;
  assert (UserStruct->Key.Value == StandaloneKey.Value);
  fprintf (Output, "%s: %d: removed\n", __FUNCTION__, UserStruct->Key.Value);
  free (UserStruct);
}


typedef struct {
  const char *Command;
  void       (*Function) (ORDERED_COLLECTION *Collection);
  const char *Description;
} KEYLESS_COMMAND;

typedef struct {
  const char *Command;
  void       (*Function) (int Value, ORDERED_COLLECTION *Collection);
  const char *Description;
} KEYED_COMMAND;

static const KEYLESS_COMMAND KeylessCommands[] = {
  { "forward-empty",  CmdForwardEmpty,
                      "empty the collection iterating forward" },
  { "fe",             CmdForwardEmpty,
                      "shorthand for forward-empty" },
  { "backward-empty", CmdBackwardEmpty,
                      "empty the collection iterating backward" },
  { "be",             CmdBackwardEmpty,
                      "shorthand for backward-empty" },
  { "forward-list",   CmdForwardList,
                      "list contents, iterating forward" },
  { "fl",             CmdForwardList,
                      "shorthand for forward-list" },
  { "backward-list",  CmdBackwardList,
                      "list contents, iterating backward" },
  { "bl",             CmdBackwardList,
                      "shorthand for backward-list" },
  { NULL }
};

static const KEYED_COMMAND KeyedCommands[] = {
  { "insert ", CmdInsert, "insert value into collection" },
  { "i ",      CmdInsert, "shorthand for insert"         },
  { "find ",   CmdFind,   "find value in collection"     },
  { "f ",      CmdFind,   "shorthand for find"           },
  { "delete ", CmdDelete, "delete value from collection" },
  { "d ",      CmdDelete, "shorthand for delete"         },
  { NULL }
};


/**
  List the supported commands on stderr.
**/
static void
ListCommands (
  void
  )
{
  const KEYLESS_COMMAND *KeylessCmd;
  const KEYED_COMMAND   *KeyedCmd;

  fprintf (stderr, "Supported commands:\n\n");
  for (KeylessCmd = KeylessCommands; KeylessCmd->Command != NULL;
       ++KeylessCmd) {
    fprintf (stderr, "%-14s: %s\n", KeylessCmd->Command,
      KeylessCmd->Description);
  }
  for (KeyedCmd = KeyedCommands; KeyedCmd->Command != NULL; ++KeyedCmd) {
    fprintf (stderr, "%-9s<int>: %s\n", KeyedCmd->Command,
      KeyedCmd->Description);
  }
}


/**
  Configure stdio FILEs that we'll use for input and output.

  @param[in] ArgC  The number of elements in ArgV, from main(). The environment
                   is required to ensure ArgC >= 1 (ie. that the program name,
                   ArgV[0], is available).

  @param[in] ArgV  Command line argument list, from main().
**/
static void
SetupInputOutput (
  IN int  ArgC,
  IN char **ArgV
  )
{
  char *InputName, *OutputName;
  int  Loop;

  assert (ArgC >= 1);

  InputName  = NULL;
  OutputName = NULL;
  Loop       = 1;

  while (Loop) {
    switch (getopt (ArgC, ArgV, ":i:o:h")) {
    case 'i':
      InputName = optarg;
      break;

    case 'o':
      OutputName = optarg;
      break;

    case 'h':
      fprintf (stderr,
        "%1$s: simple OrderedCollectionLib tester\n"
        "\n"
        "Usage: 1. %1$s [-i InputFile] [-o OutputFile]\n"
        "       2. %1$s -h\n"
        "\n"
        "Options:\n"
        "  -i InputFile : read commands from InputFile\n"
        "                 (will read from stdin if absent)\n"
        "  -o OutputFile: write command responses to OutputFile\n"
        "                 (will write to stdout if absent)\n"
        "  -h           : print this help and exit\n"
        "\n", ArgV[0]);
      ListCommands ();
      exit (EXIT_SUCCESS);

//
// The current "compatibility" getopt() implementation doesn't support optopt,
// but it gracefully degrades these branches to the others (one of the optarg
// ones or the excess operands one).
//
#if 0
    case ':':
      fprintf (stderr, "%s: option -%c requires an argument; pass -h for "
        "help\n", ArgV[0], optopt);
      exit (EXIT_FAILURE);

    case '?':
      fprintf (stderr, "%s: unknown option -%c; pass -h for help\n", ArgV[0],
        optopt);
      exit (EXIT_FAILURE);
#endif

    case -1:
      if (optind != ArgC) {
        fprintf (stderr, "%s: excess operands on command line; pass -h for "
          "help\n", ArgV[0]);
        exit (EXIT_FAILURE);
      }
      Loop = 0;
      break;

    default:
      assert (0);
    }
  }

  if (InputName == NULL) {
    Input = stdin;
  } else {
    Input = fopen (InputName, "r");
    if (Input == NULL) {
      fprintf (stderr, "%s: fopen(\"%s\", \"r\"): %s\n", ArgV[0], InputName,
        strerror (errno));
      exit (EXIT_FAILURE);
    }
  }

  if (OutputName == NULL) {
    Output = stdout;
  } else {
    Output = fopen (OutputName, "w");
    if (Output == NULL) {
      fprintf (stderr, "%s: fopen(\"%s\", \"w\"): %s\n", ArgV[0], OutputName,
        strerror (errno));
      exit (EXIT_FAILURE);
    }
  }

  //
  // When reading commands from the standard input, assume interactive mode,
  // and list the supported commands. However, delay this until both streams
  // are set up.
  //
  if (InputName == NULL) {
    ListCommands ();
  }
}


int
main (
  IN int  ArgC,
  IN char **ArgV
  )
{
  int                RetVal;
  ORDERED_COLLECTION *Collection;
  char               Line[256];

  SetupInputOutput (ArgC, ArgV);

  Collection = OrderedCollectionInit (UserStructCompare, KeyCompare);
  if (Collection == NULL) {
    fprintf (stderr, "%s: OrderedCollectionInit(): out of memory\n",
      __FUNCTION__);
      return EXIT_FAILURE;
  }

  RetVal = EXIT_SUCCESS;
  while (fgets (Line, sizeof Line, Input) != NULL) {
    size_t                Length;
    const KEYLESS_COMMAND *KeylessCmd;
    const KEYED_COMMAND   *KeyedCmd;

    Length = strlen (Line);
    assert (Length > 0);
    if (Line[Length - 1] != '\n') {
      fprintf (stderr, "%s: overlong line\n", __FUNCTION__);
      RetVal = EXIT_FAILURE;
      break;
    }

    //
    // Strip [\r]\n.
    //
    Line[Length - 1] = '\0';
    if (Length >= 2 && Line[Length - 2] == '\r') {
      Line[Length - 2] = '\0';
    }
    //
    // Ignore empty lines and comments.
    //
    if (Line[0] == '\0' || Line[0] == '#') {
      if (Input != stdin) {
        //
        // ... but echo them back in non-interactive mode.
        //
        fprintf (Output, "%s\n", Line);
      }
      continue;
    }

    //
    // Ironically, this is the kind of loop that should be replaced with an
    // ORDERED_COLLECTION.
    //
    for (KeylessCmd = KeylessCommands; KeylessCmd->Command != NULL;
         ++KeylessCmd) {
      if (strcmp (KeylessCmd->Command, Line) == 0) {
        KeylessCmd->Function (Collection);
        break;
      }
    }
    if (KeylessCmd->Command != NULL) {
      continue;
    }

    for (KeyedCmd = KeyedCommands; KeyedCmd->Command != NULL; ++KeyedCmd) {
      size_t CmdLength;

      CmdLength = strlen (KeyedCmd->Command);
      assert (CmdLength >= 2);
      if (strncmp (KeyedCmd->Command, Line, CmdLength) == 0) {
        char *CommandArg, *EndPtr;
        long Value;

        CommandArg = Line + CmdLength;
        errno = 0;
        Value = strtol (CommandArg, &EndPtr, 10);
        if (EndPtr == CommandArg ||            // no conversion performed
            errno != 0 ||                      // not in long's range, etc
            *EndPtr != '\0' ||                 // final string not empty
            Value < INT_MIN || Value > INT_MAX // parsed long not in int range
            ) {
          fprintf (stderr, "%s: %.*s: \"%s\": not an int\n", __FUNCTION__,
            (int)(CmdLength - 1), Line, CommandArg);
        } else {
          KeyedCmd->Function (Value, Collection);
        }

        break;
      }
    }
    if (KeyedCmd->Command != NULL) {
      continue;
    }

    fprintf (stderr, "%s: \"%s\": unknown command\n", __FUNCTION__, Line);
  }

  if (RetVal == EXIT_SUCCESS && ferror (Input)) {
    fprintf (stderr, "%s: fgets(): %s\n", __FUNCTION__, strerror (errno));
    RetVal = EXIT_FAILURE;
  }

  CmdForwardEmpty (Collection);
  OrderedCollectionUninit (Collection);
  return RetVal;
}