File: commands.c

package info (click to toggle)
ips 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 664 kB
  • sloc: ansic: 10,438; makefile: 25
file content (410 lines) | stat: -rw-r--r-- 7,232 bytes parent folder | download
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
/*
 * Configurable ps-like program.
 * Routines to sleep while reading commands while ips is in loop mode.
 *
 * Copyright (c) 2014 David I. Bell
 * Permission is granted to use, distribute, or modify this source,
 * provided that this copyright notice remains intact.
 */

#include "ips.h"


/*
 * Some state for numeric argument parsing.
 */
static	double	numberValue;
static	double	scaleValue;
static	BOOL	isNumberSeen;
static	BOOL	isPeriodSeen;


/*
 * Local routines.
 */
static	void	ClearParsedNumber(void);
static	double	GetParsedNumber(double);


/*
 * Sleep for the required amount of time while handling any commands.
 * Returns only when the sleep time is used up or a command was read
 * which wants us to start the next loop immediately.
 */
void
WaitForCommands(long milliSeconds)
{
	long		remainingMilliSeconds;
	long		elapsedMilliSeconds;
	struct	timeval	beginTime;

	/*
	 * If there is no sleep then just collect any keyboard events
	 * that are ready right now and execute commands for them.
	 */
	if (milliSeconds <= 0)
	{
		DpyEventWait(0);
		ReadCommands();

		return;
	}

	/*
	 * We want to sleep.  Get the beginning time so we can tell
	 * when our sleep has finished.
	 */
	GetTimeOfDay(&beginTime);

	remainingMilliSeconds = milliSeconds;

	/*
	 * Loop until either the remaining time has elapsed, the display
	 * needs redrawing, or until a  command was executed which wants
	 * the sleep to terminate early.
	 */
	do
	{
		/*
		 * Handle any events for the display device while sleeping
		 * for the remaining amount of time.  If the routine says
		 * that an update is needed now, then arrange to leave
		 * the loop (after checking for commands).
		 */
		if (DpyEventWait(remainingMilliSeconds))
			milliSeconds = 0;

		/*
		 * Handle any commands that may have been typed while we
		 * were sleeping.  If the command is one which wants us to
		 * return early, then do so.
		 */
		if (ReadCommands())
			break;

		/*
		 * No command that needed to terminate the sleep was read.
		 * Get the elapsed time since the beginning time so we can
		 * tell how long we slept for.
		 */
		elapsedMilliSeconds = ElapsedMilliSeconds(&beginTime, NULL);

		/*
		 * If the time went negative (e.g., was reset) then leave.
		 * The time should behave better on the next call.
		 */
		if (elapsedMilliSeconds < 0)
			break;

		/*
		 * Calculate the amount of time left to sleep.
		 */
		remainingMilliSeconds = milliSeconds - elapsedMilliSeconds;
	}
	while (remainingMilliSeconds > 0);
}


/*
 * Read any commands that the user may have typed while we were sleeping.
 * Reading is done in character mode so that multi-character commands
 * might not be complete in one call.  Also, reading is non-blocking.
 * Returns TRUE if the command is one which requires our sleep to terminate.
 */
BOOL
ReadCommands(void)
{
	BOOL	isWakeup;
	BOOL	isClear;
	BOOL	isError;
	int	ch;

	isWakeup = FALSE;
	isClear = FALSE;
	isError = FALSE;

	/*
	 * Loop reading commands until there are no more characters.
	 */
	while (DpyInputReady())
	{
		ch = DpyReadChar();

		if (ch == EOF)
			break;

		switch (ch)
		{
			case 'q':
			case '\033':
				/*
				 * Quit from the program.
				 */
				isRunning = FALSE;
				isWakeup = TRUE;
				isClear = TRUE;

				break;

			case '\r':
			case '\n':
			case ' ':
				/*
				 * Do an immediate update.
				 */
				isWakeup = TRUE;
				isUpdateForced = TRUE;
				isClear = TRUE;

				break;

			case 'a':
				/*
				 * Set autoscroll time.
				 */
				scrollSeconds = (int)
					GetParsedNumber(DEFAULT_SCROLL_SEC);

				if (isInfoShown)
					isWakeup = TRUE;

				break;

			case 't':
				/*
				 * Move to top page.
				 */
				TopPage();
				isWakeup = TRUE;
				isClear = TRUE;

				break;

			case 'h':
				/*
				 * Turn on or off the header line.
				 */
				noHeader = (GetParsedNumber(noHeader) == 0);
				isWakeup = TRUE;

				break;

			case 'f':
				/*
				 * Freeze or unfreeze the display.
				 */
				isFrozen = (GetParsedNumber(!isFrozen) != 0);
				ResetScrollTime();
				isWakeup = TRUE;

				break;

			case 'i':
				/*
				 * Turn on or off the info line.
				 */
				isInfoShown = (GetParsedNumber(!isInfoShown) != 0);
				isWakeup = TRUE;

				break;

			case 'b':
				/*
				 * Move to bottom page.
				 */
				BottomPage();
				isWakeup = TRUE;
				isClear = TRUE;

				break;

			case 'M':
				/*
				 * Set the myprocs option.
				 */
				myProcs = (GetParsedNumber(!myProcs) != 0);
				isWakeup = TRUE;
				TopPage();

				break;

			case 'n':
				/*
				 * Move to next page of data.
				 */
				NextPage();
				isWakeup = TRUE;
				isClear = TRUE;

				break;

			case 'o':
				/*
				 * Set overlap lines between screens.
				 */
				overlapLines = (int)
					GetParsedNumber(DEFAULT_OVERLAP_LINES);

				isWakeup = TRUE;

				break;

			case 'p':
				/*
				 * Move to previous page of data.
				 */
				PreviousPage();
				isWakeup = TRUE;
				isClear = TRUE;

				break;

			case 's':
				/*
				 * Set sleep time.
				 */
				sleepTimeMs = (int) (1000.0 *
					GetParsedNumber(DEFAULT_SLEEP_SEC));

				isWakeup = TRUE;

				break;

			case 'S':
				/*
				 * Set the no self option.
				 */
				noSelf = (GetParsedNumber(!noSelf) != 0);
				isWakeup = TRUE;
				TopPage();

				break;

			case '\177':
			case '\b':
				/*
				 * Clear the numeric argument.
				 */
				isClear = TRUE;

				break;

			case 'r':
				/*
				 * Refresh the screen.
				 */
				isRefreshNeeded = TRUE;
				isWakeup = TRUE;
				isClear = TRUE;

				break;

			case 'R':
				/*
				 * Set the no root option.
				 */
				noRoot = (GetParsedNumber(!noRoot) != 0);
				isWakeup = TRUE;
				TopPage();

				break;

			case '.':
				/*
				 * Parse part of a floating point argument.
				 */
				if (isPeriodSeen)
					isError = TRUE;

				isPeriodSeen = TRUE;

				if (!isNumberSeen)
				{
					isNumberSeen = TRUE;
					numberValue = 0.0;
					scaleValue = 1.0;
				}

				break;

			default:
				/*
				 * If the character isn't a digit then it is
				 * an error.
				 */
				if ((ch < '0') || (ch > '9'))
				{
					isError = TRUE;
					break;
				}

				/*
				 * Parse part of a floating point value.
				 */
				if (!isNumberSeen)
				{
					isNumberSeen = TRUE;
					numberValue = 0.0;
					scaleValue = 1.0;
				}

				numberValue = numberValue * 10.0 + (ch - '0');

				if (isPeriodSeen)
					scaleValue *= 10.0;

				break;
		}
	}

	/*
	 * If there was a command error then ring the bell.
	 */
	if (isError)
		DpyRingBell();

	/*
	 * Clear any parsed number if we need to.
	 */
	if (isError || isClear)
		ClearParsedNumber();

	/*
	 * Return indication of whether we need to wake up.
	 */
	return isWakeup;
}


/*
 * Get the numeric value (if any) that had previously been parsed and saved,
 * defaulting its value to the specifed one if it hadn't been entered,
 * and clearing the saved value so that a new number can be entered for
 * the next command.
 */
static double
GetParsedNumber(double value)
{
	if (isNumberSeen)
		value = numberValue / scaleValue;

	ClearParsedNumber();

	return value;	
}


/*
 * Clear any parsed number.
 */
static void
ClearParsedNumber(void)
{
	numberValue = 0;
	scaleValue = 0;
	isNumberSeen = FALSE;
	isPeriodSeen = FALSE;
}


/* END CODE */