From 6748a2c66f5068043ebc336c57fd9325e3bcad9f Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Sun, 1 Nov 2020 14:54:12 -0800 Subject: [PATCH] SCP: Extend TYPE/CAT command to support offset and line count limit --- scp.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/scp.c b/scp.c index ebaced05..776d5deb 100644 --- a/scp.c +++ b/scp.c @@ -1235,9 +1235,21 @@ static const char simh_help1[] = #define HLP_TYPE "*Commands Displaying_Files TYPE" "3TYPE\n" "++TYPE file display a file contents\n" + "4Switches\n" + " The -O switch start displaying file contents at the specified\n" + " file offset.\n\n" + " The -N switch will display up to a specified number of lines\n" + " from the file.\n\n" + "++TYPE -{O}{N} {offset} {lines} file\n\n" #define HLP_CAT "*Commands Displaying_Files CAT" "3CAT\n" "++CAT file display a file contents\n" + "4Switches\n" + " The -O switch start displaying file contents at the specified\n" + " file offset.\n\n" + " The -N switch will display up to a specified number of lines\n" + " from the file.\n\n" + "++TYPE -{O}{N} {offset} {lines} file\n\n" "2Removing Files\n" #define HLP_DELETE "*Commands Removing_Files DEL" "3DELETE\n" @@ -6877,6 +6889,8 @@ if (r != SCPE_OK) { return r; } +static long sim_type_file_offset; +static long sim_type_line_count; typedef struct { t_stat stat; @@ -6890,6 +6904,7 @@ static void sim_type_entry (const char *directory, { char FullPath[PATH_MAX + 1]; FILE *file; +long lines = 0; char lbuf[4*CBUFSIZE]; sprintf (FullPath, "%s%s", directory, filename); @@ -6899,7 +6914,10 @@ if (file == NULL) /* open failed? */ return; sim_printf ("\n%s\n\n", FullPath); lbuf[sizeof(lbuf)-1] = '\0'; -while (fgets (lbuf, sizeof(lbuf)-1, file)) +if (sim_type_file_offset) + (void)fseek (file, sim_type_file_offset, SEEK_SET); +while ((NULL != fgets (lbuf, sizeof(lbuf)-1, file)) && + (lines++ < sim_type_line_count)) sim_printf ("%s", lbuf); fclose (file); } @@ -6908,11 +6926,36 @@ fclose (file); t_stat type_cmd (int32 flg, CONST char *cptr) { FILE *file; +long lines = 0; char lbuf[4*CBUFSIZE]; +sim_type_file_offset = 0; /* beginning of file */ +sim_type_line_count = 0X7FFFFFFF; /* output many lines */ GET_SWITCHES (cptr); /* get switches */ if ((!cptr) || (*cptr == 0)) return SCPE_2FARG; +if (sim_switches & SWMASK ('O')) { /* Specify Offset in file */ + char gbuf[CBUFSIZE]; + char *eptr; + + cptr = get_glyph (cptr, gbuf, 0); + if ((!cptr) || (*cptr == 0)) + return SCPE_2FARG; + sim_type_file_offset = strtol (gbuf, &eptr, 0); + if ((*eptr) || (sim_type_file_offset < 0)) + return sim_messagef (SCPE_ARG, "Invalid file offset: %s\n", gbuf); + } +if (sim_switches & SWMASK ('N')) { /* Specify Line Count to display */ + char gbuf[CBUFSIZE]; + char *eptr; + + cptr = get_glyph (cptr, gbuf, 0); + if ((!cptr) || (*cptr == 0)) + return SCPE_2FARG; + sim_type_line_count = strtol (gbuf, &eptr, 0); + if ((*eptr) || (sim_type_line_count < 0)) + return sim_messagef (SCPE_ARG, "Invalid file line count: %s\n", gbuf); + } lbuf[sizeof(lbuf)-1] = '\0'; strlcpy (lbuf, cptr, sizeof(lbuf)); sim_trim_endspc(lbuf); @@ -6928,7 +6971,10 @@ if (file == NULL) { /* open failed? */ return sim_messagef (SCPE_OPENERR, "The system cannot find the file specified.\n"); } lbuf[sizeof(lbuf)-1] = '\0'; -while (fgets (lbuf, sizeof(lbuf)-1, file)) +if (sim_type_file_offset) + (void)fseek (file, sim_type_file_offset, SEEK_SET); +while ((NULL != fgets (lbuf, sizeof(lbuf)-1, file)) && + (lines++ < sim_type_line_count)) sim_printf ("%s", lbuf); fclose (file); return SCPE_OK;