Browse Source

Introduce struct vcmd_param to cleanup struct vcmd_entry.

Georgi Chorbadzhiyski 9 years ago
parent
commit
a42e405e05
3 changed files with 52 additions and 50 deletions
  1. 32
    33
      cmd.c
  2. 7
    4
      cmd.h
  3. 13
    13
      videohubctrl.c

+ 32
- 33
cmd.c View File

378
 	return ret;
378
 	return ret;
379
 }
379
 }
380
 
380
 
381
+static void init_port_number(struct vcmd_param *p, struct port_set *port, const char *port_id) {
382
+	p->port_no = my_atoi(p->param);
383
+	if (p->port_no == 0 || p->port_no > port->num) {
384
+		p->port_no = get_port_by_name(port, p->param);
385
+		if (!p->port_no)
386
+			die("Unknown %s port number/name: %s", port_id, p->param);
387
+	}
388
+}
389
+
381
 void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
390
 void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
382
 	struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
391
 	struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
383
 	struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
392
 	struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
386
 		return;
395
 		return;
387
 
396
 
388
 	// All command types needs parsing of the "source port"
397
 	// All command types needs parsing of the "source port"
389
-	e->port_no1 = my_atoi(e->param1);
390
-	if (e->port_no1 == 0 || e->port_no1 > s_port->num) {
391
-		e->port_no1 = get_port_by_name(s_port, e->param1);
392
-		if (!e->port_no1)
393
-			die("Unknown %s port number/name: %s", e->cmd->port_id1, e->param1);
394
-	}
398
+	init_port_number(&e->p1, s_port, e->cmd->port_id1);
395
 
399
 
396
 	// ROUTE type needs parsing of the "destination port"
400
 	// ROUTE type needs parsing of the "destination port"
397
 	if (e->cmd->type == PARSE_ROUTE) {
401
 	if (e->cmd->type == PARSE_ROUTE) {
398
-		e->port_no2 = my_atoi(e->param2);
399
-		if (e->port_no2 == 0 || e->port_no2 > d_port->num) {
400
-			e->port_no2 = get_port_by_name(d_port, e->param2);
401
-			if (!e->port_no2)
402
-				die("Unknown %s port number/name: %s", e->cmd->port_id2, e->param2);
403
-		}
402
+		init_port_number(&e->p2, d_port, e->cmd->port_id2);
404
 	}
403
 	}
405
 
404
 
406
 	// Allow port_noX to be used as index into ->port[]
405
 	// Allow port_noX to be used as index into ->port[]
407
-	e->port_no1 -= 1;
408
-	e->port_no2 -= 1;
406
+	e->p1.port_no -= 1;
407
+	e->p2.port_no -= 1;
409
 	if (e->clear_port)
408
 	if (e->clear_port)
410
-		e->port_no2 = NO_PORT;
409
+		e->p2.port_no = NO_PORT;
411
 
410
 
412
 	if (e->cmd->type == PARSE_LOCK) {
411
 	if (e->cmd->type == PARSE_LOCK) {
413
-		e->lock = s_port->port[e->port_no1].lock;
412
+		e->lock = s_port->port[e->p1.port_no].lock;
414
 	}
413
 	}
415
 }
414
 }
416
 
415
 
435
 void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
434
 void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
436
 	if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
435
 	if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
437
 		snprintf(buf, bufsz, "%s:\n%s: %s\n\n", videohub_commands_text[e->cmd->cmd],
436
 		snprintf(buf, bufsz, "%s:\n%s: %s\n\n", videohub_commands_text[e->cmd->cmd],
438
-			e->param1, e->param2);
437
+			e->p1.param, e->p2.param);
439
 		return;
438
 		return;
440
 	}
439
 	}
441
 	switch (e->cmd->type) {
440
 	switch (e->cmd->type) {
442
 	case PARSE_LABEL:
441
 	case PARSE_LABEL:
443
 		snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
442
 		snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
444
-			e->port_no1, e->param2);
443
+			e->p1.port_no, e->p2.param);
445
 		break;
444
 		break;
446
 	case PARSE_LOCK:
445
 	case PARSE_LOCK:
447
 		snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
446
 		snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
448
-			e->port_no1, e->do_lock ? "O" : (e->lock == PORT_LOCKED_OTHER ? "F" : "U"));
447
+			e->p1.port_no, e->do_lock ? "O" : (e->lock == PORT_LOCKED_OTHER ? "F" : "U"));
449
 		break;
448
 		break;
450
 	case PARSE_ROUTE:
449
 	case PARSE_ROUTE:
451
 		snprintf(buf, bufsz, "%s:\n%u %d\n\n", videohub_commands_text[e->cmd->cmd],
450
 		snprintf(buf, bufsz, "%s:\n%u %d\n\n", videohub_commands_text[e->cmd->cmd],
452
-			e->port_no1, (e->port_no2 == NO_PORT ? -1 : (int)e->port_no2));
451
+			e->p1.port_no, (e->p2.port_no == NO_PORT ? -1 : (int)e->p2.port_no));
453
 		break;
452
 		break;
454
 	case PARSE_DIR:
453
 	case PARSE_DIR:
455
 		snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
454
 		snprintf(buf, bufsz, "%s:\n%u %s\n\n", videohub_commands_text[e->cmd->cmd],
456
-			e->port_no1, dir2cmd(e->direction));
455
+			e->p1.port_no, dir2cmd(e->direction));
457
 		break;
456
 		break;
458
 	default: break;
457
 	default: break;
459
 	}
458
 	}
466
 	if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
465
 	if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
467
 		printf("%sset device \"%s\" to \"%s\"\n",
466
 		printf("%sset device \"%s\" to \"%s\"\n",
468
 			prefix,
467
 			prefix,
469
-			e->param1,
470
-			e->param2
468
+			e->p1.param,
469
+			e->p2.param
471
 		);
470
 		);
472
 		return;
471
 		return;
473
 	}
472
 	}
476
 		printf("%srename %s %d \"%s\" to \"%s\"\n",
475
 		printf("%srename %s %d \"%s\" to \"%s\"\n",
477
 			prefix,
476
 			prefix,
478
 			e->cmd->port_id1,
477
 			e->cmd->port_id1,
479
-			e->port_no1 + 1, s_port->port[e->port_no1].name,
480
-			e->param2
478
+			e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
479
+			e->p2.param
481
 		);
480
 		);
482
 		break;
481
 		break;
483
 	case PARSE_LOCK:
482
 	case PARSE_LOCK:
485
 			prefix,
484
 			prefix,
486
 			e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
485
 			e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
487
 			e->cmd->port_id1,
486
 			e->cmd->port_id1,
488
-			e->port_no1 + 1, s_port->port[e->port_no1].name
487
+			e->p1.port_no + 1, s_port->port[e->p1.port_no].name
489
 		);
488
 		);
490
 		break;
489
 		break;
491
 	case PARSE_ROUTE:
490
 	case PARSE_ROUTE:
492
-		if (e->port_no2 == NO_PORT) {
491
+		if (e->p2.port_no == NO_PORT) {
493
 			printf("%sdisconnect %s %d \"%s\"\n",
492
 			printf("%sdisconnect %s %d \"%s\"\n",
494
 				prefix,
493
 				prefix,
495
 				e->cmd->port_id1,
494
 				e->cmd->port_id1,
496
-				e->port_no1 + 1, s_port->port[e->port_no1].name
495
+				e->p1.port_no + 1, s_port->port[e->p1.port_no].name
497
 			);
496
 			);
498
 			break;
497
 			break;
499
 		}
498
 		}
501
 			printf("%sconnect %s %d \"%s\" to %s %d \"%s\"\n",
500
 			printf("%sconnect %s %d \"%s\" to %s %d \"%s\"\n",
502
 				prefix,
501
 				prefix,
503
 				e->cmd->port_id1,
502
 				e->cmd->port_id1,
504
-				e->port_no1 + 1, s_port->port[e->port_no1].name,
503
+				e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
505
 				e->cmd->port_id2,
504
 				e->cmd->port_id2,
506
-				e->port_no2 + 1, d_port->port [e->port_no2].name
505
+				e->p2.port_no + 1, d_port->port [e->p2.port_no].name
507
 			);
506
 			);
508
 			break;
507
 			break;
509
 		}
508
 		}
510
 		printf("%sset %s %d \"%s\" to read from %s %d \"%s\"\n",
509
 		printf("%sset %s %d \"%s\" to read from %s %d \"%s\"\n",
511
 			prefix,
510
 			prefix,
512
 			e->cmd->port_id1,
511
 			e->cmd->port_id1,
513
-			e->port_no1 + 1, s_port->port[e->port_no1].name,
512
+			e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
514
 			e->cmd->port_id2,
513
 			e->cmd->port_id2,
515
-			e->port_no2 + 1, d_port->port [e->port_no2].name
514
+			e->p2.port_no + 1, d_port->port [e->p2.port_no].name
516
 		);
515
 		);
517
 		break;
516
 		break;
518
 	case PARSE_DIR:
517
 	case PARSE_DIR:
519
 		printf("%sset %s %d \"%s\" direction to %s\n",
518
 		printf("%sset %s %d \"%s\" direction to %s\n",
520
 			prefix,
519
 			prefix,
521
 			e->cmd->port_id1,
520
 			e->cmd->port_id1,
522
-			e->port_no1 + 1, s_port->port[e->port_no1].name,
521
+			e->p1.port_no + 1, s_port->port[e->p1.port_no].name,
523
 			dir2txt(e->direction)
522
 			dir2txt(e->direction)
524
 		);
523
 		);
525
 		break;
524
 		break;

+ 7
- 4
cmd.h View File

70
 bool parse_command(struct videohub_data *d, char *cmd);
70
 bool parse_command(struct videohub_data *d, char *cmd);
71
 int parse_text_buffer(struct videohub_data *data, char *cmd_buffer);
71
 int parse_text_buffer(struct videohub_data *data, char *cmd_buffer);
72
 
72
 
73
+struct vcmd_param {
74
+	char			*param;
75
+	unsigned int	port_no;
76
+};
77
+
73
 struct vcmd_entry {
78
 struct vcmd_entry {
74
 	struct videohub_commands *cmd;
79
 	struct videohub_commands *cmd;
75
-	char			*param1;
76
-	char			*param2;
77
-	unsigned int	port_no1;
78
-	unsigned int	port_no2;
80
+	struct vcmd_param	p1;
81
+	struct vcmd_param	p2;
79
 	bool			do_lock;
82
 	bool			do_lock;
80
 	enum port_lock	lock;
83
 	enum port_lock	lock;
81
 	enum serial_dir	direction;
84
 	enum serial_dir	direction;

+ 13
- 13
videohubctrl.c View File

197
 		exit(EXIT_FAILURE);
197
 		exit(EXIT_FAILURE);
198
 	}
198
 	}
199
 	c->cmd = &videohub_commands[vcmd];
199
 	c->cmd = &videohub_commands[vcmd];
200
-	c->param1 = argv[optind - 1];
201
-	c->param2 = argv[optind];
200
+	c->p1.param = argv[optind - 1];
201
+	c->p2.param = argv[optind];
202
 	if (vcmd == CMD_SERIAL_PORT_DIRECTIONS) {
202
 	if (vcmd == CMD_SERIAL_PORT_DIRECTIONS) {
203
-		if (strcasecmp("in", c->param2) == 0)        c->direction = DIR_CONTROL;
204
-		else if (strcasecmp("out", c->param2) == 0)  c->direction = DIR_SLAVE;
205
-		else if (strcasecmp("auto", c->param2) == 0) c->direction = DIR_AUTO;
206
-		else die("Invalid serial port direction '%s'. Allowed are: in, out, auto.", c->param2);
203
+		if (strcasecmp("in", c->p2.param) == 0)        c->direction = DIR_CONTROL;
204
+		else if (strcasecmp("out", c->p2.param) == 0)  c->direction = DIR_SLAVE;
205
+		else if (strcasecmp("auto", c->p2.param) == 0) c->direction = DIR_AUTO;
206
+		else die("Invalid serial port direction '%s'. Allowed are: in, out, auto.", c->p2.param);
207
 	}
207
 	}
208
 	num_parsed_cmds++;
208
 	num_parsed_cmds++;
209
 }
209
 }
212
 	check_num_parsed_cmds();
212
 	check_num_parsed_cmds();
213
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
213
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
214
 	c->cmd = &videohub_commands[vcmd];
214
 	c->cmd = &videohub_commands[vcmd];
215
-	c->param1 = optarg;
216
-	c->param2 = "1"; // Fake
215
+	c->p1.param = optarg;
216
+	c->p2.param = "1"; // Fake
217
 	c->clear_port = true;
217
 	c->clear_port = true;
218
 	num_parsed_cmds++;
218
 	num_parsed_cmds++;
219
 }
219
 }
222
 	check_num_parsed_cmds();
222
 	check_num_parsed_cmds();
223
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
223
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
224
 	c->cmd = &videohub_commands[vcmd];
224
 	c->cmd = &videohub_commands[vcmd];
225
-	c->param1 = argv[optind - 1];
225
+	c->p1.param = argv[optind - 1];
226
 	c->do_lock = do_lock;
226
 	c->do_lock = do_lock;
227
 	num_parsed_cmds++;
227
 	num_parsed_cmds++;
228
 }
228
 }
231
 	check_num_parsed_cmds();
231
 	check_num_parsed_cmds();
232
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
232
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
233
 	c->cmd = &videohub_commands[CMD_VIDEOHUB_DEVICE];
233
 	c->cmd = &videohub_commands[CMD_VIDEOHUB_DEVICE];
234
-	c->param1 = setting;
235
-	c->param2 = value;
234
+	c->p1.param = setting;
235
+	c->p2.param = value;
236
 	num_parsed_cmds++;
236
 	num_parsed_cmds++;
237
 }
237
 }
238
 
238
 
430
 		unsigned int i;
430
 		unsigned int i;
431
 		for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
431
 		for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
432
 			struct vcmd_entry *ve = &parsed_cmds.entry[i];
432
 			struct vcmd_entry *ve = &parsed_cmds.entry[i];
433
-			if (!ve->param1)
433
+			if (!ve->p1.param)
434
 				continue;
434
 				continue;
435
 			prepare_cmd_entry(data, &parsed_cmds.entry[i]);
435
 			prepare_cmd_entry(data, &parsed_cmds.entry[i]);
436
 		}
436
 		}
438
 		for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
438
 		for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
439
 			char cmd_buffer[1024];
439
 			char cmd_buffer[1024];
440
 			struct vcmd_entry *ve = &parsed_cmds.entry[i];
440
 			struct vcmd_entry *ve = &parsed_cmds.entry[i];
441
-			if (!ve->param1)
441
+			if (!ve->p1.param)
442
 				continue;
442
 				continue;
443
 			format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
443
 			format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
444
 			if (strlen(cmd_buffer)) {
444
 			if (strlen(cmd_buffer)) {

Loading…
Cancel
Save