1/**
2 * \file
3 * \brief Simple test program to test terminal input.
4 **/
5
6/*
7 * Copyright (c) 2012, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, CAB F.78, Universitaetstr. 6, CH-8092 Zurich,
13 * Attn: Systems Group.
14 */
15
16#include <stdio.h>
17#include <barrelfish/barrelfish.h>
18#include <barrelfish/terminal.h>
19
20#define SIZE   128
21
22static void test_getchar(void)
23{
24    printf("--- TEST GETCHAR ---\n");
25    printf("Please enter a character: ");
26    fflush(stdout);
27
28    int c = getchar();
29    printf("\n");
30    fflush(stdout);
31
32    if (c == EOF) {
33        fprintf(stderr, "Received EOF.\n");
34    } else {
35        printf("Received character %c (hex %#x).\n", c, c);
36    }
37    printf("\n");
38    fflush(stdout);
39}
40
41static void test_gets(void)
42{
43    printf("--- TEST GETS ---\n");
44    printf("Please enter a string of maximum length %d: ", SIZE);
45    fflush(stdout);
46
47    char buffer[SIZE];
48    char *ret = NULL;
49    ret = gets(buffer);
50    printf("\n");
51    fflush(stdout);
52
53    if (ret == NULL) {
54        fprintf(stderr, "Error during gets().\n");
55    } else {
56        printf("Received the following string: %s\n", buffer);
57    }
58    printf("\n");
59    fflush(stdout);
60}
61
62static void test_scanf(void)
63{
64    printf("--- TEST SCANF ---\n");
65    printf("Please enter an integer followed by a string of maximum length %d: ",
66           SIZE);
67    fflush(stdout);
68
69    int i = 0;
70    char buffer[SIZE];
71    int num = scanf("%i %s", &i, buffer);
72    printf("\n");
73    fflush(stdout);
74
75    if (num == EOF) {
76        fprintf(stderr, "Error during scanf().\n");
77    } else if (num != 2) {
78        fprintf(stderr, "Could not read an interger and a string.\n");
79    } else {
80        printf("Received the number: %i\n", i);
81        printf("Received the string: %s\n", buffer);
82    }
83    printf("\n");
84    fflush(stdout);
85}
86
87int main(int argc, char **argv)
88{
89    printf("--- TERMINAL INPUT TESTS ---\n");
90
91    test_getchar();
92    test_gets();
93    test_scanf();
94
95    return EXIT_SUCCESS;
96}
97