Welcome to mirror list, hosted at ThFree Co, Russian Federation.

program_more.cpp « dos « src - github.com/dosbox-staging/dosbox-staging.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 84864d6e18d908423bb7c3d8de30425cbd5e80d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *
 *  Copyright (C) 2022-2022  The DOSBox Staging Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "program_more.h"

#include "../ints/int10.h"
#include "callback.h"
#include "checks.h"
#include "dos_inc.h"
#include "string_utils.h"

#include <algorithm>
#include <array>
#include <cctype>

CHECK_NARROWING();

// ASCII control characters
constexpr char code_ctrl_c = 0x03; // end of text
constexpr char code_lf     = 0x0a; // line feed
constexpr char code_cr     = 0x0d; // carriage return
constexpr char code_esc    = 0x1b; // escape

void MORE::Run()
{
	// Handle command line
	if (HelpRequested()) {
		WriteOut(MSG_Get("PROGRAM_MORE_HELP_LONG"));
		return;
	}
	if (!ParseCommandLine() || shutdown_requested)
		return;

	// Retrieve screen size, prepare limits
	constexpr uint16_t min_lines   = 10;
	constexpr uint16_t min_columns = 40;
	max_lines   = std::max(min_lines, INT10_GetTextRows());
	max_columns = std::max(min_columns, INT10_GetTextColumns());
	// The prompt at the bottom will cause scrolling,
	// so reduce the maximum number of lines accordingly
	max_lines = static_cast<uint16_t>(max_lines - 1);

	line_counter = 0;

	// Show STDIN or input file(s) content
	if (input_files.empty()) {
		DisplayInputStream();
	} else {
		DisplayInputFiles();

		// End message and command prompt is going to appear; ensure the
		// scrolling won't make top lines disappear before user reads them
		const int free_rows_threshold = 2;
		if (max_lines - line_counter < free_rows_threshold)
			PromptUser();

		WriteOut(MSG_Get("PROGRAM_MORE_END"));
		WriteOut("\n");
	}

	WriteOut("\n");
}

bool MORE::ParseCommandLine()
{
	// Put all the parameters into vector
	std::vector<std::string> params;
	cmd->FillVector(params);

	// Check if specified tabulation size
	if (!params.empty()) {
		const auto &param = params[0];
		if ((starts_with("/t", param) || starts_with("/T", param)) &&
		    (param.length() == 3) && (param.back() >= '1') &&
		    (param.back() <= '9')) {
			// FreeDOS extension - custom TAB size
			tab_size = static_cast<uint8_t>(param.back() - '0');
			params.erase(params.begin());
		}
	}

	// Make sure no other switches are supplied
	for (const auto &param : params)
		if (starts_with("/", param)) {
			WriteOut(MSG_Get("SHELL_ILLEGAL_SWITCH"), param.c_str());
			return false;
		}

	// Create list of input files
	return FindInputFiles(params);
}

bool MORE::FindInputFiles(const std::vector<std::string> &params)
{
	input_files.clear();
	if (params.empty())
		return true;

	constexpr auto search_attr = UINT16_MAX & ~DOS_ATTR_DIRECTORY &
	                             ~DOS_ATTR_VOLUME;

	RealPt save_dta = dos.dta();
	dos.dta(dos.tables.tempdta);

	for (const auto &param : params) {
		// Retrieve path to current file/pattern
		char path[DOS_PATHLENGTH];
		if (!DOS_Canonicalize(param.c_str(), path))
			continue;
		char *const end = strrchr(path, '\\') + 1;
		assert(end);
		*end = 0;

		// Search for the first file from pattern
		if (!DOS_FindFirst(param.c_str(),
		                   static_cast<uint16_t>(search_attr))) {
			LOG_WARNING("DOS: MORE.COM - no match for pattern '%s'",
			            param.c_str());
			continue;
		}

		while (!shutdown_requested) {
			CALLBACK_Idle();

			char name[DOS_NAMELENGTH_ASCII];
			uint32_t size = 0;
			uint16_t time = 0;
			uint16_t date = 0;
			uint8_t attr  = 0;

			DOS_DTA dta(dos.dta());
			dta.GetResult(name, size, date, time, attr);
			assert(name);

			input_files.emplace_back();
			auto &entry = input_files.back();

			if (attr & DOS_ATTR_DEVICE) {
				entry.is_device = true;
				entry.name      = std::string(name);
			} else {
				entry.is_device = false;
				entry.name = std::string(path) + std::string(name);
			}

			if (!DOS_FindNext()) {
				break;
			}
		}
	}

	dos.dta(save_dta);

	if (!shutdown_requested && input_files.empty()) {
		WriteOut(MSG_Get("PROGRAM_MORE_NO_FILE"));
		WriteOut("\n");
		return false;
	}

	return true;
}

std::string MORE::GetShortName(const std::string &file_name, const char *msg_id)
{
	assert(msg_id);

	// The shortest name we should be able to display is:
	// - 3 dots
	// - 1 path separator
	// - 8 characters of name
	// - 1 dot
	// - 3 characters of extension
	// This gives 16 characters.
	// We need to keep the last column free (reduces max length by 1).
	// Format string contains '%s' (increases max length by 2).
	constexpr size_t min = 16;
	const auto max_len   = std::max(min, max_columns - std::strlen(MSG_Get(msg_id)) + 1);

	// Nothing to do if file name maches the constraint
	if (file_name.length() <= max_len)
		return file_name;

	// We need to shorten the name - try to strip part of the path
	auto shortened = file_name;
	while (shortened.length() > max_len &&
	       std::count(shortened.begin(), shortened.end(), '\\') > 1) {
		// Strip one level of path at a time
		const auto pos = shortened.find('\\', shortened.find('\\') + 1);
		shortened      = std::string("...") + shortened.substr(pos);
	}

	// If still too long, just cut away the beginning
	const auto len = shortened.length();
	if (len > max_len)
		shortened = std::string("...") + shortened.substr(len - max_len + 3);

	return shortened;
}

void MORE::DisplayInputFiles()
{
	WriteOut("\n");

	bool first = true;
	for (const auto &input_file : input_files) {
		if (!first && Decision::Terminate == PromptUser())
			break;
		first = false;

		if (!DOS_OpenFile(input_file.name.c_str(), 0, &input_handle)) {
			LOG_WARNING("DOS: MORE.COM - could not open '%s'",
			            input_file.name.c_str());
			const auto short_name = GetShortName(input_file.name,
			                                     "PROGRAM_MORE_OPEN_ERROR");
			WriteOut(MSG_Get("PROGRAM_MORE_OPEN_ERROR"), short_name.c_str());
			WriteOut("\n");
			++line_counter;
			continue;
		}

		if (input_file.is_device) {
			const auto short_name = GetShortName(input_file.name,
			                                     "PROGRAM_MORE_NEW_DEVICE");
			WriteOut(MSG_Get("PROGRAM_MORE_NEW_DEVICE"), short_name.c_str());
		} else {
			const auto short_name = GetShortName(input_file.name,
			                                     "PROGRAM_MORE_NEW_FILE");
			WriteOut(MSG_Get("PROGRAM_MORE_NEW_FILE"), short_name.c_str());
		}
		WriteOut("\n");
		++line_counter;

		// If input from a device, CTRL+C shall quit
		ctrl_c_enable = input_file.is_device;

		const auto decision = DisplaySingleStream();
		DOS_CloseFile(input_handle);
		if (decision == Decision::Terminate) {
			break;
		}
	}
}

void MORE::DisplayInputStream()
{
	// We need to be able to read STDIN for key presses, but it is most
	// likely redirected - so clone the handle, and reconstruct real STDIN
	// from STDERR (idea from FreeDOS implementation,
	// https://github.com/FDOS/more/blob/master/src/more.c)
	if (!DOS_DuplicateEntry(STDIN, &input_handle) ||
	    !DOS_ForceDuplicateEntry(STDERR, STDIN)) {
		LOG_ERR("DOS: Unable to prepare handles in MORE.COM");
		return;
	}

	WriteOut("\n");

	// Since this CAN be STDIN input (there is no way to check),
	// CTRL+C shall quit
	ctrl_c_enable = true;
	DisplaySingleStream();
}

MORE::Decision MORE::DisplaySingleStream()
{
	auto previous_column = GetCurrentColumn();

	tabs_remaining = 0;
	skip_next_cr   = false;
	skip_next_lf   = false;

	auto decision = Decision::NextFile;
	while (true) {
		if (shutdown_requested) {
			decision = Decision::Terminate;
			break;
		}

		// Read character
		char code = 0;
		if (!GetCharacter(code)) {
			decision = Decision::NextFile; // end of file
			break;
		}

		// A trick to make it more resistant to ANSI cursor movements
		const auto current_row = GetCurrentRow();
		if (line_counter > current_row)
			line_counter = current_row;

		// Handle new line characters
		bool new_line = false;
		if (code == code_cr) {
			skip_next_lf = true;
			new_line     = true;
		} else if (code == code_lf) {
			skip_next_cr = true;
			new_line     = true;
		} else {
			skip_next_cr = false;
			skip_next_lf = false;
		}

		// Duplicate character on the output
		if (new_line)
			code = '\n';
		WriteOut("%c", code);

		// Detect 'new line' due to character passing the last column
		const auto current_column = GetCurrentColumn();
		if (!current_column && previous_column) {
			new_line = true;
		}
		previous_column = current_column;

		// Update new line counter, decide if pause needed
		if (new_line && current_row) {
			++line_counter;
		}
		if (line_counter < max_lines) {
			continue;
		}

		// New line occured just enough times for a pause
		decision = PromptUser();
		if (decision == Decision::Terminate || decision == Decision::NextFile) {
			break;
		}
	}

	if (GetCurrentColumn()) {
		++line_counter;
		WriteOut("\n");
	}

	return decision;
}

MORE::Decision MORE::PromptUser()
{
	line_counter = 0;
	const bool multiple_files = input_files.size() > 1;

	if (GetCurrentColumn())
		WriteOut("\n");

	if (multiple_files)
		WriteOut(MSG_Get("PROGRAM_MORE_PROMPT_MULTI"));
	else
		WriteOut(MSG_Get("PROGRAM_MORE_PROMPT_SINGLE"));

	auto decision = Decision::Terminate;
	while (!shutdown_requested) {
		CALLBACK_Idle();

		uint16_t count = 1;
		char choice    = 0;
		DOS_ReadFile(STDIN, reinterpret_cast<uint8_t *>(&choice), &count);

		if (count == 0 || choice == code_ctrl_c || choice == code_esc ||
		    choice == 'q' || choice == 'Q') {
			decision = Decision::Terminate;
			break;
		}

		if (choice == code_cr || choice == ' ') {
			decision = Decision::More;
			break;
		}

		if (!multiple_files)
			continue;

		if (choice == 'n' || choice == 'N') {
			decision = Decision::NextFile;
			break;
		}
	}

	if (decision == Decision::Terminate || decision == Decision::NextFile) {
		WriteOut(" ");
		WriteOut(MSG_Get("PROGRAM_MORE_TERMINATE"));
		WriteOut("\n");
		++line_counter;
	} else {
		// We are going to continue - erase the prompt
		WriteOut("\033[M"); // clear line
		auto counter = GetCurrentColumn();
		while (counter--)
			WriteOut("\033[D"); // cursor one position back
	}

	return decision;
}

bool MORE::GetCharacter(char &code)
{
	if (!tabs_remaining) {
		while (true) {
			// Retrieve character from input stream
			uint16_t count = 1;
			DOS_ReadFile(input_handle,
			             reinterpret_cast<uint8_t *>(&code),
			             &count);

			if (!count) {
				return false; // end of stream
			}

			if (ctrl_c_enable && code == code_ctrl_c) {
				if (input_files.empty()) {
					WriteOut("^C");
				}
				return false; // quit by CTRL+C
			}

			// Skip CR/LF characters if requested
			if (skip_next_cr && code == code_cr) {
				skip_next_cr = false;
			} else if (skip_next_lf && code == code_lf) {
				skip_next_lf = false;
			} else {
				break;
			}
		}

		// If TAB found, replace it with given number of spaces
		if (code == '\t') {
			tabs_remaining = tab_size;
		}
	}

	if (tabs_remaining) {
		--tabs_remaining;
		code = ' ';
	}

	return true;
}

uint8_t MORE::GetCurrentColumn()
{
	const auto page = real_readb(BIOSMEM_SEG, BIOSMEM_CURRENT_PAGE);
	return CURSOR_POS_COL(page);
}

uint8_t MORE::GetCurrentRow()
{
	const auto page = real_readb(BIOSMEM_SEG, BIOSMEM_CURRENT_PAGE);
	return CURSOR_POS_ROW(page);
}

void MORE::AddMessages()
{
	MSG_Add("PROGRAM_MORE_HELP_LONG",
	        "Display command output or text file one screen at a time.\n"
	        "\n"
	        "Usage:\n"
	        "  [color=cyan]COMMAND[reset] | [color=green]more[reset] [/t[color=white]n[reset]]\n"
	        "  [color=green]more[reset] [/t[color=white]n[reset]] < [color=cyan]FILE[reset]\n"
	        "  [color=green]more[reset] [/t[color=white]n[reset]] [color=cyan]PATTERN[reset] [[color=cyan]PATTERN[reset] ...]\n"
	        "\n"
	        "Where:\n"
	        "  [color=cyan]COMMAND[reset] is the command to display the output of.\n"
	        "  [color=cyan]FILE[reset]    is an exact name of the file to display, optionally with a path.\n"
	        "  [color=cyan]PATTERN[reset] is either a path to a single file or a path with wildcards,\n"
	        "          which are the asterisk (*) and the question mark (?).\n"
	        "  [color=white]n[reset]       is the tab size, 1-9, default is 8.\n"
	        "\n"
	        "Notes:\n"
	        "  This command is only for viewing text files, not binary files.\n"
	        "\n"
	        "Examples:\n"
	        "  [color=cyan]dir /on[reset] | [color=green]more[reset]             ; displays sorted directory one screen at a time\n"
	        "  [color=green]more[reset] /t[color=white]4[reset] < [color=cyan]A:\\MANUAL.TXT[reset]   ; shows the file's content with tab size 4\n");

	MSG_Add("PROGRAM_MORE_NO_FILE",       "No input file found.");
	MSG_Add("PROGRAM_MORE_END",           "[reset][color=light-yellow]--- end of input ---[reset]");
	MSG_Add("PROGRAM_MORE_NEW_FILE",      "[reset][color=light-yellow]--- file %s ---[reset]");
	MSG_Add("PROGRAM_MORE_NEW_DEVICE",    "[reset][color=light-yellow]--- device %s ---[reset]");
	MSG_Add("PROGRAM_MORE_PROMPT_SINGLE", "[reset][color=light-yellow]--- press SPACE for more ---[reset]");
	MSG_Add("PROGRAM_MORE_PROMPT_MULTI",  "[reset][color=light-yellow]--- press SPACE for more, N for next file ---[reset]");
	MSG_Add("PROGRAM_MORE_OPEN_ERROR",    "[reset][color=red]--- could not open %s ---[reset]");
	MSG_Add("PROGRAM_MORE_TERMINATE",     "[reset][color=light-yellow](terminated)[reset]");
}