1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // configs/parsebools.cfa
|
---|
8 | // Testing parsing of boolean arguments
|
---|
9 | //
|
---|
10 | // Author : Thierry Delisle
|
---|
11 | // Created On : Wed Oct 12 15:28:01 2022
|
---|
12 | // Last Modified By : Peter A. Buhr
|
---|
13 | // Last Modified On : Sun Feb 11 09:26:13 2024
|
---|
14 | // Update Count : 2
|
---|
15 | //
|
---|
16 |
|
---|
17 | #include <fstream.hfa>
|
---|
18 | #include "../meta/fork+exec.hfa"
|
---|
19 |
|
---|
20 | // last as a work around to a parse bug
|
---|
21 | #include <parseargs.hfa>
|
---|
22 |
|
---|
23 | int main( int argc, char * argv[] ) {
|
---|
24 | if ( argc == 0 ) abort( "Test requires a command-line argument" );
|
---|
25 | check_main( argv[0] );
|
---|
26 |
|
---|
27 | bool YN = false;
|
---|
28 | bool Yn = false;
|
---|
29 | bool yn = false;
|
---|
30 | bool tf = false;
|
---|
31 | bool st = false;
|
---|
32 | bool sf = true;
|
---|
33 |
|
---|
34 | array( cfa_option, 6 ) options;
|
---|
35 | options[0] = (cfa_option){'e', "yesno", "test yes/no", YN, parse_yesno};
|
---|
36 | options[1] = (cfa_option){'y', "YN", "test yes/no", Yn, parse_yesno};
|
---|
37 | options[2] = (cfa_option){'n', "yn", "test yes/no", yn, parse_yesno};
|
---|
38 | options[3] = (cfa_option){'t', "truefalse", "test true/false", tf, parse_truefalse};
|
---|
39 | options[4] = (cfa_option){'s', "settrue", "test set true", st, parse_settrue};
|
---|
40 | options[5] = (cfa_option){'u', "setfalse", "test set false", sf, parse_setfalse};
|
---|
41 |
|
---|
42 | char **left;
|
---|
43 | parse_args( options, "[OPTIONS]...\ntesting bool parameters", left );
|
---|
44 |
|
---|
45 | sout | "yes/no :" | YN;
|
---|
46 | sout | "Y/N :" | Yn;
|
---|
47 | sout | "y/n :" | yn;
|
---|
48 | sout | "true/false :" | tf;
|
---|
49 | sout | "set true :" | st;
|
---|
50 | sout | "set false :" | sf;
|
---|
51 | }
|
---|
52 |
|
---|
53 | int true_main( const char * path, const char * env[] ) {
|
---|
54 | printf( "no arg:\n" );
|
---|
55 | if ( pid_t child = strict_fork(); child == 0 ) {
|
---|
56 | int ret = execle( path, "parsebools", (const char*)0p, env );
|
---|
57 | if ( ret < 0 ) {
|
---|
58 | fprintf( stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror( errno ) );
|
---|
59 | exit( 1 );
|
---|
60 | }
|
---|
61 | } else {
|
---|
62 | int status = do_wait( child );
|
---|
63 | print_status( status );
|
---|
64 | } // if
|
---|
65 |
|
---|
66 | printf( "all true/set arg:\n" );
|
---|
67 | if ( pid_t child = strict_fork(); child == 0 ) {
|
---|
68 | int ret = execle( path, "parsebools", "-e=yes", "-y=Y", "-n=y", "-t=true", "-s", "-u", (const char*)0p, env );
|
---|
69 | if ( ret < 0 ) {
|
---|
70 | fprintf( stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror( errno ) );
|
---|
71 | exit( 1 );
|
---|
72 | } // if
|
---|
73 | } else {
|
---|
74 | int status = do_wait( child );
|
---|
75 | print_status( status );
|
---|
76 | } // if
|
---|
77 |
|
---|
78 | printf( "all false/unset arg:\n" );
|
---|
79 | if ( pid_t child = strict_fork(); child == 0 ) {
|
---|
80 | int ret = execle( path, "parsebools", "-e=no", "-y=N", "-n=n", "-t=false", (const char*)0p, env );
|
---|
81 | if ( ret < 0 ) {
|
---|
82 | fprintf( stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror( errno ) );
|
---|
83 | exit( 1 );
|
---|
84 | } // if
|
---|
85 | } else {
|
---|
86 | int status = do_wait( child );
|
---|
87 | print_status( status );
|
---|
88 | } // if
|
---|
89 |
|
---|
90 | printf( "gibberish arg 1:\n" );
|
---|
91 | if ( pid_t child = strict_fork(); child == 0 ) {
|
---|
92 | int ret = execle( path, "parsebools", "-y=true", (const char*)0p, env );
|
---|
93 | if ( ret < 0 ) {
|
---|
94 | fprintf( stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror( errno ) );
|
---|
95 | exit( 1 );
|
---|
96 | } // if
|
---|
97 | } else {
|
---|
98 | int status = do_wait( child );
|
---|
99 | print_status( status );
|
---|
100 | } // if
|
---|
101 |
|
---|
102 | printf( "gibberish arg 2:\n" );
|
---|
103 | if ( pid_t child = strict_fork(); child == 0 ) {
|
---|
104 | int ret = execle( path, "parsebools", "-t=yes", (const char*)0p, env );
|
---|
105 | if ( ret < 0 ) {
|
---|
106 | fprintf( stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror( errno ) );
|
---|
107 | exit( 1 );
|
---|
108 | } // if
|
---|
109 | } else {
|
---|
110 | int status = do_wait( child );
|
---|
111 | print_status( status );
|
---|
112 | } // if
|
---|
113 |
|
---|
114 | printf( "gibberish arg 3:\n" );
|
---|
115 | if ( pid_t child = strict_fork(); child == 0 ) {
|
---|
116 | int ret = execle( path, "parsebools", "-s=yes", (const char*)0p, env );
|
---|
117 | if ( ret < 0 ) {
|
---|
118 | fprintf( stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror( errno ) );
|
---|
119 | exit( 1 );
|
---|
120 | } // if
|
---|
121 | } else {
|
---|
122 | int status = do_wait( child );
|
---|
123 | print_status( status );
|
---|
124 | } // if
|
---|
125 |
|
---|
126 | printf( "gibberish arg 4:\n" );
|
---|
127 | if ( pid_t child = strict_fork(); child == 0 ) {
|
---|
128 | int ret = execle( path, "parsebools", "-u=yes", ( const char*)0p, env );
|
---|
129 | if ( ret < 0 ) {
|
---|
130 | fprintf( stderr, "Execl 2 returned with error: %d '%s'\n", errno, strerror( errno ) );
|
---|
131 | exit( 1 );
|
---|
132 | } // if
|
---|
133 | } else {
|
---|
134 | int status = do_wait( child );
|
---|
135 | print_status( status );
|
---|
136 | } // if
|
---|
137 |
|
---|
138 | printf( "All Done!\n" );
|
---|
139 |
|
---|
140 | return 0;
|
---|
141 | }
|
---|