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,6 +378,15 @@ static unsigned int my_atoi(char *txt) {
378 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 390
 void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
382 391
 	struct port_set *s_port = !e->cmd->ports1 ? NULL : (void *)d + e->cmd->ports1;
383 392
 	struct port_set *d_port = !e->cmd->ports2 ? NULL : (void *)d + e->cmd->ports2;
@@ -386,31 +395,21 @@ void prepare_cmd_entry(struct videohub_data *d, struct vcmd_entry *e) {
386 395
 		return;
387 396
 
388 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 400
 	// ROUTE type needs parsing of the "destination port"
397 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 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 408
 	if (e->clear_port)
410
-		e->port_no2 = NO_PORT;
409
+		e->p2.port_no = NO_PORT;
411 410
 
412 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,25 +434,25 @@ static char *dir2txt(enum serial_dir dir) {
435 434
 void format_cmd_text(struct vcmd_entry *e, char *buf, unsigned int bufsz) {
436 435
 	if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
437 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 438
 		return;
440 439
 	}
441 440
 	switch (e->cmd->type) {
442 441
 	case PARSE_LABEL:
443 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 444
 		break;
446 445
 	case PARSE_LOCK:
447 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 448
 		break;
450 449
 	case PARSE_ROUTE:
451 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 452
 		break;
454 453
 	case PARSE_DIR:
455 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 456
 		break;
458 457
 	default: break;
459 458
 	}
@@ -466,8 +465,8 @@ void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
466 465
 	if (e->cmd->cmd == CMD_VIDEOHUB_DEVICE) {
467 466
 		printf("%sset device \"%s\" to \"%s\"\n",
468 467
 			prefix,
469
-			e->param1,
470
-			e->param2
468
+			e->p1.param,
469
+			e->p2.param
471 470
 		);
472 471
 		return;
473 472
 	}
@@ -476,8 +475,8 @@ void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
476 475
 		printf("%srename %s %d \"%s\" to \"%s\"\n",
477 476
 			prefix,
478 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 481
 		break;
483 482
 	case PARSE_LOCK:
@@ -485,15 +484,15 @@ void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
485 484
 			prefix,
486 485
 			e->do_lock ? "lock" : (e->lock == PORT_LOCKED_OTHER ? "force unlock" : "unlock"),
487 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 489
 		break;
491 490
 	case PARSE_ROUTE:
492
-		if (e->port_no2 == NO_PORT) {
491
+		if (e->p2.port_no == NO_PORT) {
493 492
 			printf("%sdisconnect %s %d \"%s\"\n",
494 493
 				prefix,
495 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 497
 			break;
499 498
 		}
@@ -501,25 +500,25 @@ void show_cmd(struct videohub_data *d, struct vcmd_entry *e) {
501 500
 			printf("%sconnect %s %d \"%s\" to %s %d \"%s\"\n",
502 501
 				prefix,
503 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 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 507
 			break;
509 508
 		}
510 509
 		printf("%sset %s %d \"%s\" to read from %s %d \"%s\"\n",
511 510
 			prefix,
512 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 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 516
 		break;
518 517
 	case PARSE_DIR:
519 518
 		printf("%sset %s %d \"%s\" direction to %s\n",
520 519
 			prefix,
521 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 522
 			dir2txt(e->direction)
524 523
 		);
525 524
 		break;

+ 7
- 4
cmd.h View File

@@ -70,12 +70,15 @@ extern struct videohub_commands videohub_commands[NUM_COMMANDS];
70 70
 bool parse_command(struct videohub_data *d, char *cmd);
71 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 78
 struct vcmd_entry {
74 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 82
 	bool			do_lock;
80 83
 	enum port_lock	lock;
81 84
 	enum serial_dir	direction;

+ 13
- 13
videohubctrl.c View File

@@ -197,13 +197,13 @@ static void parse_cmd2(int argc, char **argv, enum vcmd vcmd) {
197 197
 		exit(EXIT_FAILURE);
198 198
 	}
199 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 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 208
 	num_parsed_cmds++;
209 209
 }
@@ -212,8 +212,8 @@ static void parse_cmd2s(int argc, char **argv, enum vcmd vcmd) {
212 212
 	check_num_parsed_cmds();
213 213
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
214 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 217
 	c->clear_port = true;
218 218
 	num_parsed_cmds++;
219 219
 }
@@ -222,7 +222,7 @@ static void parse_cmd1(int argc, char **argv, enum vcmd vcmd, bool do_lock) {
222 222
 	check_num_parsed_cmds();
223 223
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
224 224
 	c->cmd = &videohub_commands[vcmd];
225
-	c->param1 = argv[optind - 1];
225
+	c->p1.param = argv[optind - 1];
226 226
 	c->do_lock = do_lock;
227 227
 	num_parsed_cmds++;
228 228
 }
@@ -231,8 +231,8 @@ static void set_device_option(char *setting, char *value) {
231 231
 	check_num_parsed_cmds();
232 232
 	struct vcmd_entry *c = &parsed_cmds.entry[num_parsed_cmds];
233 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 236
 	num_parsed_cmds++;
237 237
 }
238 238
 
@@ -430,7 +430,7 @@ int main(int argc, char **argv) {
430 430
 		unsigned int i;
431 431
 		for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
432 432
 			struct vcmd_entry *ve = &parsed_cmds.entry[i];
433
-			if (!ve->param1)
433
+			if (!ve->p1.param)
434 434
 				continue;
435 435
 			prepare_cmd_entry(data, &parsed_cmds.entry[i]);
436 436
 		}
@@ -438,7 +438,7 @@ int main(int argc, char **argv) {
438 438
 		for (i = 0; i < ARRAY_SIZE(parsed_cmds.entry); i++) {
439 439
 			char cmd_buffer[1024];
440 440
 			struct vcmd_entry *ve = &parsed_cmds.entry[i];
441
-			if (!ve->param1)
441
+			if (!ve->p1.param)
442 442
 				continue;
443 443
 			format_cmd_text(ve, cmd_buffer, sizeof(cmd_buffer));
444 444
 			if (strlen(cmd_buffer)) {

Loading…
Cancel
Save