changeset 172:e7e0d8c06654

fc-simtool: support output redirection in select command
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 02 Mar 2021 03:18:21 +0000
parents 4d747b86da00
children e70964adcbba
files simtool/dispatch.c simtool/readef.c simtool/select.c
diffstat 3 files changed, 46 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/dispatch.c	Tue Mar 02 03:03:49 2021 +0000
+++ b/simtool/dispatch.c	Tue Mar 02 03:18:21 2021 +0000
@@ -185,7 +185,7 @@
 	{"restore-file", 2, 2, 0, cmd_restore_file},
 	{"savebin", 2, 2, 0, cmd_savebin},
 	{"save-sms-bin", 1, 1, 0, cmd_save_sms_bin},
-	{"select", 1, 1, 0, cmd_select},
+	{"select", 1, 1, 1, cmd_select},
 	{"sim-resp", 0, 0, 1, cmd_sim_resp},
 	{"sms-erase-all", 0, 0, 0, cmd_sms_erase_all},
 	{"sms-erase-one", 1, 1, 0, cmd_sms_erase_one},
--- a/simtool/readef.c	Tue Mar 02 03:03:49 2021 +0000
+++ b/simtool/readef.c	Tue Mar 02 03:18:21 2021 +0000
@@ -38,15 +38,13 @@
 			"error: readef command is only for transparent EFs\n");
 		return(-1);
 	}
-	if (outf == stdout) {
-		printf("Transparent EF of %u byte(s)\n", curfile_total_size);
-		printf("File status: %02X\n", sim_resp_data[11]);
-		show_access_conditions("UPDATE", sim_resp_data[8] & 0xF);
-		show_access_conditions("READ & SEEK", sim_resp_data[8] >> 4);
-		show_access_conditions("INCREASE", sim_resp_data[9] >> 4);
-		show_access_conditions("INVALIDATE", sim_resp_data[10] & 0xF);
-		show_access_conditions("REHABILITATE", sim_resp_data[10] >> 4);
-	}
+	fprintf(outf, "Transparent EF of %u byte(s)\n", curfile_total_size);
+	fprintf(outf, "File status: %02X\n", sim_resp_data[11]);
+	show_access_conditions(outf, "UPDATE", sim_resp_data[8] & 0xF);
+	show_access_conditions(outf, "READ & SEEK", sim_resp_data[8] >> 4);
+	show_access_conditions(outf, "INCREASE", sim_resp_data[9] >> 4);
+	show_access_conditions(outf, "INVALIDATE", sim_resp_data[10] & 0xF);
+	show_access_conditions(outf, "REHABILITATE", sim_resp_data[10] >> 4);
 	if (!curfile_total_size)
 		return(0);
 	readlen = curfile_total_size;
--- a/simtool/select.c	Tue Mar 02 03:03:49 2021 +0000
+++ b/simtool/select.c	Tue Mar 02 03:18:21 2021 +0000
@@ -82,17 +82,19 @@
 }
 
 static void
-show_secret_code_status(name, byte)
+show_secret_code_status(outf, name, byte)
+	FILE *outf;
 	char *name;
 	unsigned byte;
 {
-	printf("Status of %s: %s, %u attempts left\n", name,
+	fprintf(outf, "Status of %s: %s, %u attempts left\n", name,
 		byte & 0x80 ? "initialized" : "not initialized",
 		byte & 0x0F);
 }
 
 void
-show_access_conditions(oper_name, cond_code)
+show_access_conditions(outf, oper_name, cond_code)
+	FILE *outf;
 	char *oper_name;
 	unsigned cond_code;
 {
@@ -102,12 +104,13 @@
 		 "ADM8", "ADM9", "ADM10", "ADM11",
 		 "ADM12", "ADM13", "ADM14", "NEV"};
 
-	printf("Access condition for %s: %s\n", oper_name,
+	fprintf(outf, "Access condition for %s: %s\n", oper_name,
 		cond_names[cond_code]);
 }
 
-cmd_select(argc, argv)
+cmd_select(argc, argv, outf)
 	char **argv;
+	FILE *outf;
 {
 	int file_id, rc;
 
@@ -132,10 +135,10 @@
 	}
 	switch (sim_resp_data[6]) {
 	case 0x01:
-		printf("File type: MF\n");
+		fprintf(outf, "File type: MF\n");
 		goto mf_or_df;
 	case 0x02:
-		printf("File type: DF\n");
+		fprintf(outf, "File type: DF\n");
 	mf_or_df:
 		if (sim_resp_data_len < 22) {
 			fprintf(stderr,
@@ -143,29 +146,31 @@
 				sim_resp_data_len);
 			return(-1);
 		}
-		printf("File characteristics: %02X\n", sim_resp_data[13]);
-		printf("Number of DF children: %u\n", sim_resp_data[14]);
-		printf("Number of EF children: %u\n", sim_resp_data[15]);
-		printf("Number of secret codes: %u\n", sim_resp_data[16]);
-		show_secret_code_status("PIN1", sim_resp_data[18]);
-		show_secret_code_status("PUK1", sim_resp_data[19]);
-		show_secret_code_status("PIN2", sim_resp_data[20]);
-		show_secret_code_status("PUK2", sim_resp_data[21]);
+		fprintf(outf, "File characteristics: %02X\n",
+			sim_resp_data[13]);
+		fprintf(outf, "Number of DF children: %u\n", sim_resp_data[14]);
+		fprintf(outf, "Number of EF children: %u\n", sim_resp_data[15]);
+		fprintf(outf, "Number of secret codes: %u\n",
+			sim_resp_data[16]);
+		show_secret_code_status(outf, "PIN1", sim_resp_data[18]);
+		show_secret_code_status(outf, "PUK1", sim_resp_data[19]);
+		show_secret_code_status(outf, "PIN2", sim_resp_data[20]);
+		show_secret_code_status(outf, "PUK2", sim_resp_data[21]);
 		break;
 	case 0x04:
-		printf("File type: EF\n");
+		fprintf(outf, "File type: EF\n");
 		curfile_total_size = (sim_resp_data[2] << 8) | sim_resp_data[3];
-		printf("File size: %u\n", curfile_total_size);
+		fprintf(outf, "File size: %u\n", curfile_total_size);
 		curfile_structure = sim_resp_data[13];
 		switch (curfile_structure) {
 		case 0x00:
-			printf("Structure: transparent\n");
+			fprintf(outf, "Structure: transparent\n");
 			break;
 		case 0x01:
-			printf("Structure: linear fixed\n");
+			fprintf(outf, "Structure: linear fixed\n");
 			goto ef_record_based;
 		case 0x03:
-			printf("Structure: cyclic\n");
+			fprintf(outf, "Structure: cyclic\n");
 		ef_record_based:
 			if (sim_resp_data_len < 15) {
 				fprintf(stderr,
@@ -173,30 +178,33 @@
 					sim_resp_data_len);
 				return(-1);
 			}
-			printf("Record length: %u\n", sim_resp_data[14]);
+			fprintf(outf, "Record length: %u\n", sim_resp_data[14]);
 			curfile_record_len = sim_resp_data[14];
 			if (curfile_record_len &&
 			    curfile_total_size % curfile_record_len == 0) {
 				curfile_record_count =
 					curfile_total_size / curfile_record_len;
-				printf("Number of records: %u\n",
+				fprintf(outf, "Number of records: %u\n",
 					curfile_record_count);
 			} else
 				curfile_record_count = 0;
 			break;
 		default:
-			printf("Structure: %02X (unknown)\n",
+			fprintf(outf, "Structure: %02X (unknown)\n",
 				curfile_structure);
 		}
-		printf("File status: %02X\n", sim_resp_data[11]);
-		show_access_conditions("UPDATE", sim_resp_data[8] & 0xF);
-		show_access_conditions("READ & SEEK", sim_resp_data[8] >> 4);
-		show_access_conditions("INCREASE", sim_resp_data[9] >> 4);
-		show_access_conditions("INVALIDATE", sim_resp_data[10] & 0xF);
-		show_access_conditions("REHABILITATE", sim_resp_data[10] >> 4);
+		fprintf(outf, "File status: %02X\n", sim_resp_data[11]);
+		show_access_conditions(outf, "UPDATE", sim_resp_data[8] & 0xF);
+		show_access_conditions(outf, "READ & SEEK",
+					sim_resp_data[8] >> 4);
+		show_access_conditions(outf, "INCREASE", sim_resp_data[9] >> 4);
+		show_access_conditions(outf, "INVALIDATE",
+					sim_resp_data[10] & 0xF);
+		show_access_conditions(outf, "REHABILITATE",
+					sim_resp_data[10] >> 4);
 		break;
 	default:
-		printf("File type: %02X (unknown)\n", sim_resp_data[6]);
+		fprintf(outf, "File type: %02X (unknown)\n", sim_resp_data[6]);
 	}
 	return(0);
 }