ObjFW  Check-in [be9bad7ce0]

Overview
Comment:utils/ofatalkcfg: Add support for showing config
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: be9bad7ce029ed121a4fd3eeea69937de67bf4baa39997bddc9fa0a525aac111
User & Date: js on 2023-04-16 13:42:35
Other Links: manifest | tags
Context
2023-04-16
13:43
GitHub Actions: Add missing GnuTLS on NetBSD check-in: 9006b9d6fd user: js tags: trunk
13:42
utils/ofatalkcfg: Add support for showing config check-in: be9bad7ce0 user: js tags: trunk
13:11
Add GCC on NetBSD to GitHub Actions check-in: 8cb5b89234 user: js tags: trunk
Changes

Modified utils/ofatalkcfg/OFATalkCfg.m from [c079b4e165] to [9d2924bd2f].

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
#import "OFDDPSocket.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFOptionsParser.h"
#import "OFPair.h"
#import "OFStdIOStream.h"


#import "OFInvalidFormatException.h"


@interface OFATalkCfg: OFObject <OFApplicationDelegate>
@end

OF_APPLICATION_DELEGATE(OFATalkCfg)










































































































































@implementation OFATalkCfg
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{
	OFMutableDictionary *config = [OFMutableDictionary dictionary];
	OFString *nodeString = nil, *networkString = nil, *phaseString = nil;
	OFString *rangeString = nil;
	const OFOptionsParserOption options[] = {
		{ '\0', @"network", 1, NULL, &networkString },
		{ '\0', @"node", 1, NULL, &nodeString },
		{ '\0', @"phase", 1, NULL, &phaseString },
		{ '\0', @"range", 1, NULL, &rangeString },
		{ '\0', nil, 0, NULL, NULL }
	};
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: options];
	OFUnichar option;
	unsigned long long node, network, phase, rangeStart, rangeEnd;
	OFArray OF_GENERIC(OFString *) *rangeArray;

	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case ':':
			if (optionsParser.lastLongOption != nil)
				[OFStdErr writeFormat:
				    @"%@: Argument for option --%@ missing\n",







>

>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




<












<
<







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
#import "OFDDPSocket.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFOptionsParser.h"
#import "OFPair.h"
#import "OFStdIOStream.h"

#import "OFGetOptionFailedException.h"
#import "OFInvalidFormatException.h"
#import "OFSetOptionFailedException.h"

@interface OFATalkCfg: OFObject <OFApplicationDelegate>
@end

OF_APPLICATION_DELEGATE(OFATalkCfg)

static void
showError(OFString *error)
{
	[OFStdErr writeFormat: @"%@: %@\n", [OFApplication programName], error];
	[OFApplication terminateWithStatus: 1];
}

static void
showConfiguration(OFArray OF_GENERIC(OFString *) *arguments)
{
	OFAppleTalkInterfaceConfiguration config;
	OFNumber *network, *node, *phase;
	OFPair OF_GENERIC(OFNumber *, OFNumber *) *range;

	if (arguments.count == 0)
		showError(@"No interface specified!");
	if (arguments.count > 1)
		showError(@"More than one interface specified!");

	config = [OFDDPSocket configurationForInterface: arguments.firstObject];
	network = [config
	    objectForKey: OFAppleTalkInterfaceConfigurationNetwork];
	node = [config objectForKey: OFAppleTalkInterfaceConfigurationNode];
	phase = [config objectForKey: OFAppleTalkInterfaceConfigurationPhase];
	range = [config
	    objectForKey: OFAppleTalkInterfaceConfigurationNetworkRange];

	if (network == nil || node == nil)
		[OFApplication terminateWithStatus: 1];

	[OFStdOut writeLine: arguments.firstObject];
	[OFStdOut writeFormat: @"\tNetwork:   %04X\n",
			       network.unsignedShortValue];
	[OFStdOut writeFormat: @"\tNode:      %02X\n", node.unsignedCharValue];
	if (phase != nil)
		[OFStdOut writeFormat: @"\tPhase:     %@\n", phase];
	if (range != nil) {
		unsigned short start = [range.firstObject unsignedShortValue];
		unsigned short end = [range.secondObject unsignedShortValue];
		[OFStdOut writeFormat: @"\tNet range: %04X-%04X", start, end];
	}
}

static void
setConfiguration(OFArray OF_GENERIC(OFString *) *arguments,
    OFString *networkString, OFString *nodeString, OFString *phaseString,
    OFString *rangeString)
{
	OFMutableDictionary *config = [OFMutableDictionary dictionary];
	unsigned long long node, network, phase, rangeStart, rangeEnd;
	OFArray OF_GENERIC(OFString *) *rangeArray;

	if (arguments.count == 0)
		showError(@"No interface specified!");
	if (arguments.count > 1)
		showError(@"More than one interface specified!");

	if (networkString == nil)
		showError(@"--network not specified!");
	@try {
		network = [networkString unsignedLongLongValueWithBase: 0];
	} @catch (OFInvalidFormatException *e) {
		showError(@"Invalid format for --network!");
		return;
	}
	if (network > UINT16_MAX)
		showError(@"--network out of range!");
	[config setObject: [OFNumber numberWithUnsignedShort: (uint16_t)network]
		   forKey: OFAppleTalkInterfaceConfigurationNetwork];

	if (nodeString == nil)
		showError(@"--node not specified!");
	@try {
		node = [nodeString unsignedLongLongValueWithBase: 0];
	} @catch (OFInvalidFormatException *e) {
		showError(@"Invalid format for --node!");
		return;
	}
	if (node > UINT8_MAX)
		showError(@"--node out of range!");
	[config setObject: [OFNumber numberWithUnsignedChar: (uint8_t)node]
		   forKey: OFAppleTalkInterfaceConfigurationNode];

	if (phaseString != nil) {
		@try {
			phase = [phaseString unsignedLongLongValueWithBase: 0];
		} @catch (OFInvalidFormatException *e) {
			showError(@"Invalid format for "@"--phase!");
			return;
		}

		if (phase > 2)
			showError(@"--phase out of range!");

		[config setObject: [OFNumber
				       numberWithUnsignedChar: (uint8_t)phase]
			   forKey: OFAppleTalkInterfaceConfigurationPhase];
	}

	if (rangeString != nil) {
		const OFAppleTalkInterfaceConfigurationKey key =
		    OFAppleTalkInterfaceConfigurationNetworkRange;
		OFPair *range;

		rangeArray = [rangeString componentsSeparatedByString: @"-"];
		if (rangeArray.count != 2)
			showError(@"Invalid format for --range!");

		@try {
			rangeStart = [[rangeArray objectAtIndex: 0]
			    unsignedLongLongValueWithBase: 0];
			rangeEnd = [[rangeArray objectAtIndex: 1]
			    unsignedLongLongValueWithBase: 0];
		} @catch (OFInvalidFormatException *e) {
			showError(@"Invalid format for --range!");
			return;
		}
		if (rangeStart > UINT16_MAX || rangeEnd > UINT16_MAX)
			showError(@"--range out of range!");

		range = [OFPair
		    pairWithFirstObject: [OFNumber numberWithUnsignedShort:
					     (uint16_t)rangeStart]
			   secondObject: [OFNumber numberWithUnsignedShort:
					     (uint16_t)rangeEnd]];
		[config setObject: range forKey: key];
	}

	@try {
		[OFDDPSocket setConfiguration: config
				 forInterface: arguments.firstObject];
	} @catch (OFSetOptionFailedException *e) {
		showError([OFString stringWithFormat:
		    @"Setting configuration failed: %@", e]);
	}
}

@implementation OFATalkCfg
- (void)applicationDidFinishLaunching: (OFNotification *)notification
{

	OFString *nodeString = nil, *networkString = nil, *phaseString = nil;
	OFString *rangeString = nil;
	const OFOptionsParserOption options[] = {
		{ '\0', @"network", 1, NULL, &networkString },
		{ '\0', @"node", 1, NULL, &nodeString },
		{ '\0', @"phase", 1, NULL, &phaseString },
		{ '\0', @"range", 1, NULL, &rangeString },
		{ '\0', nil, 0, NULL, NULL }
	};
	OFOptionsParser *optionsParser =
	    [OFOptionsParser parserWithOptions: options];
	OFUnichar option;



	while ((option = [optionsParser nextOption]) != '\0') {
		switch (option) {
		case ':':
			if (optionsParser.lastLongOption != nil)
				[OFStdErr writeFormat:
				    @"%@: Argument for option --%@ missing\n",
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
				    optionsParser.lastOption];

			[OFApplication terminateWithStatus: 1];
			break;
		}
	}

	if (optionsParser.remainingArguments.count == 0) {
		[OFStdErr writeFormat: @"%@: No interface specified!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	if (optionsParser.remainingArguments.count > 1) {
		[OFStdErr writeFormat: @"%@: More than one interface "
				       @"specified!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}

	if (networkString == nil) {
		[OFStdErr writeFormat: @"%@: --network not specified!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	@try {
		network = [networkString unsignedLongLongValueWithBase: 0];
	} @catch (OFInvalidFormatException *e) {
		[OFStdErr writeFormat: @"%@: Invalid format for --network!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
		return;
	}
	if (network > UINT16_MAX) {
		[OFStdErr writeFormat: @"%@: --network out of range!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	[config setObject: [OFNumber numberWithUnsignedShort: (uint16_t)network]
		   forKey: OFAppleTalkInterfaceConfigurationNetwork];

	if (nodeString == nil) {
		[OFStdErr writeFormat: @"%@: --node not specified!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	@try {
		node = [nodeString unsignedLongLongValueWithBase: 0];
	} @catch (OFInvalidFormatException *e) {
		[OFStdErr writeFormat: @"%@: Invalid format for --node!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
		return;
	}
	if (node > UINT8_MAX) {
		[OFStdErr writeFormat: @"%@: --node out of range!\n",
				       [OFApplication programName]];
		[OFApplication terminateWithStatus: 1];
	}
	[config setObject: [OFNumber numberWithUnsignedChar: (uint8_t)node]
		   forKey: OFAppleTalkInterfaceConfigurationNode];

	if (phaseString != nil) {
		@try {
			phase = [phaseString unsignedLongLongValueWithBase: 0];
		} @catch (OFInvalidFormatException *e) {
			[OFStdErr writeFormat:
			    @"%@: Invalid format for "@"--phase!\n",
			    [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
			return;
		}

		if (phase > 2) {
			[OFStdErr writeFormat: @"%@: --phase out of range!\n",
					       [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}

		[config setObject: [OFNumber
				       numberWithUnsignedChar: (uint8_t)phase]
			   forKey: OFAppleTalkInterfaceConfigurationPhase];
	}

	if (rangeString != nil) {
		const OFAppleTalkInterfaceConfigurationKey key =
		    OFAppleTalkInterfaceConfigurationNetworkRange;
		OFPair *range;

		rangeArray = [rangeString componentsSeparatedByString: @"-"];
		if (rangeArray.count != 2) {
			[OFStdErr writeFormat:
			    @"%@: Invalid format for --range!\n",
			    [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}

		@try {
			rangeStart = [[rangeArray objectAtIndex: 0]
			    unsignedLongLongValueWithBase: 0];
			rangeEnd = [[rangeArray objectAtIndex: 1]
			    unsignedLongLongValueWithBase: 0];
		} @catch (OFInvalidFormatException *e) {
			[OFStdErr writeFormat:
			    @"%@: Invalid format for --range!\n",
			    [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
			return;
		}

		if (rangeStart > UINT16_MAX || rangeEnd > UINT16_MAX) {
			[OFStdErr writeFormat: @"%@: --range out of range!\n",
					       [OFApplication programName]];
			[OFApplication terminateWithStatus: 1];
		}

		range = [OFPair
		    pairWithFirstObject: [OFNumber numberWithUnsignedShort:
					     (uint16_t)rangeStart]
			   secondObject: [OFNumber numberWithUnsignedShort:
					     (uint16_t)rangeEnd]];
		[config setObject: range forKey: key];
	}

	[OFDDPSocket setConfiguration: config
			 forInterface: optionsParser.remainingArguments
					   .firstObject];

	[OFApplication terminate];
}
@end







<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




217
218
219
220
221
222
223












224




















225



















226










227





228




229











































230
231
232
233
				    optionsParser.lastOption];

			[OFApplication terminateWithStatus: 1];
			break;
		}
	}













	if (networkString == nil && nodeString == nil && phaseString == nil &&




















	    rangeString == nil)



















		showConfiguration(optionsParser.remainingArguments);










	else





		setConfiguration(optionsParser.remainingArguments,




		    networkString, nodeString, phaseString, rangeString);












































	[OFApplication terminate];
}
@end