~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to misc-utils/setterm.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* setterm.c, set terminal attributes.
2
 
 *
3
 
 * Copyright (C) 1990 Gordon Irlam (gordoni@cs.ua.oz.au).  Conditions of use,
4
 
 * modification, and redistribution are contained in the file COPYRIGHT that
5
 
 * forms part of this distribution.
6
 
 * 
7
 
 * Adaption to Linux by Peter MacDonald.
8
 
 *
9
 
 * Enhancements by Mika Liljeberg (liljeber@cs.Helsinki.FI)
10
 
 *
11
 
 * Beep modifications by Christophe Jolif (cjolif@storm.gatelink.fr.net)
12
 
 *
13
 
 * Sanity increases by Cafeine Addict [sic].
14
 
 *
15
 
 * Powersave features by todd j. derr <tjd@wordsmith.org>
16
 
 *
17
 
 * Converted to terminfo by Kars de Jong (jongk@cs.utwente.nl)
18
 
 *
19
 
 * 1999-02-22 Arkadiusz Miļæ½kiewicz <misiek@pld.ORG.PL>
20
 
 * - added Native Language Support
21
 
 *
22
 
 *
23
 
 * Syntax:
24
 
 *
25
 
 * setterm
26
 
 *   [ -term terminal_name ]
27
 
 *   [ -reset ]
28
 
 *   [ -initialize ]
29
 
 *   [ -cursor [on|off] ]
30
 
 *   [ -repeat [on|off] ]
31
 
 *   [ -appcursorkeys [on|off] ]
32
 
 *   [ -linewrap [on|off] ]
33
 
 *   [ -snow [on|off] ]
34
 
 *   [ -softscroll [on|off] ]
35
 
 *   [ -defaults ]
36
 
 *   [ -foreground black|red|green|yellow|blue|magenta|cyan|white|default ]
37
 
 *   [ -background black|red|green|yellow|blue|magenta|cyan|white|default ]
38
 
 *   [ -ulcolor black|grey|red|green|yellow|blue|magenta|cyan|white ]
39
 
 *   [ -ulcolor bright red|green|yellow|blue|magenta|cyan|white ]
40
 
 *   [ -hbcolor black|grey|red|green|yellow|blue|magenta|cyan|white ]
41
 
 *   [ -hbcolor bright red|green|yellow|blue|magenta|cyan|white ]
42
 
 *   [ -inversescreen [on|off] ]
43
 
 *   [ -bold [on|off] ]
44
 
 *   [ -half-bright [on|off] ]
45
 
 *   [ -blink [on|off] ]
46
 
 *   [ -reverse [on|off] ]
47
 
 *   [ -underline [on|off] ]
48
 
 *   [ -store ]
49
 
 *   [ -clear [ all|rest ] ]
50
 
 *   [ -tabs [tab1 tab2 tab3 ... ] ]     (tabn = 1-160)
51
 
 *   [ -clrtabs [ tab1 tab2 tab3 ... ]   (tabn = 1-160)
52
 
 *   [ -regtabs [1-160] ]
53
 
 *   [ -blank [0-60|force|poke|] ]
54
 
 *   [ -dump   [1-NR_CONS ] ]
55
 
 *   [ -append [1-NR_CONS ] ]
56
 
 *   [ -file dumpfilename ]
57
 
 *   [ -standout [attr] ]
58
 
 *   [ -msg [on|off] ]
59
 
 *   [ -msglevel [0-8] ]
60
 
 *   [ -powersave [on|vsync|hsync|powerdown|off] ]
61
 
 *   [ -powerdown [0-60] ]
62
 
 *   [ -blength [0-2000] ]
63
 
 *   [ -bfreq freq ]
64
 
 *
65
 
 *
66
 
 * Semantics:
67
 
 *
68
 
 * Setterm writes to standard output a character string that will
69
 
 * invoke the specified terminal capabilities.  Where possibile
70
 
 * terminfo is consulted to find the string to use.  Some options
71
 
 * however do not correspond to a terminfo capability.  In this case if
72
 
 * the terminal type is "con*", or "linux*" the string that invokes
73
 
 * the specified capabilities on the PC Linux virtual console driver
74
 
 * is output.  Options that are not implemented by the terminal are
75
 
 * ignored.
76
 
 *
77
 
 * The following options are non-obvious.
78
 
 *
79
 
 *   -term can be used to override the TERM environment variable.
80
 
 *
81
 
 *   -reset displays the terminal reset string, which typically resets the
82
 
 *      terminal to its power on state.
83
 
 *
84
 
 *   -initialize displays the terminal initialization string, which typically
85
 
 *      sets the terminal's rendering options, and other attributes to the
86
 
 *      default values.
87
 
 *
88
 
 *   -default sets the terminal's rendering options to the default values.
89
 
 *
90
 
 *   -store stores the terminal's current rendering options as the default
91
 
 *      values.  */
92
 
 
93
 
#include <stdio.h>
94
 
#include <stdlib.h>
95
 
#include <errno.h>
96
 
#include <ctype.h>
97
 
#include <unistd.h>
98
 
#include <termios.h>
99
 
#include <string.h>
100
 
#include <fcntl.h>
101
 
#ifndef NCURSES_CONST
102
 
#define NCURSES_CONST const     /* define before including term.h */
103
 
#endif
104
 
#include <term.h>
105
 
 
106
 
#ifdef HAVE_NCURSES_H
107
 
#include <ncurses.h>
108
 
#elif defined(HAVE_NCURSES_NCURSES_H)
109
 
#include <ncurses/ncurses.h>
110
 
#endif
111
 
 
112
 
#include <sys/param.h>          /* for MAXPATHLEN */
113
 
#include <sys/ioctl.h>
114
 
#include <sys/time.h>
115
 
#ifdef HAVE_LINUX_TIOCL_H
116
 
#include <linux/tiocl.h>
117
 
#endif
118
 
 
119
 
#include "c.h"
120
 
#include "xalloc.h"
121
 
#include "nls.h"
122
 
 
123
 
#if __GNU_LIBRARY__ < 5
124
 
#ifndef __alpha__
125
 
# include <linux/unistd.h>
126
 
#define __NR_klogctl __NR_syslog
127
 
_syscall3(int, klogctl, int, type, char*, buf, int, len);
128
 
#else /* __alpha__ */
129
 
#define klogctl syslog
130
 
#endif
131
 
#endif
132
 
extern int klogctl(int type, char *buf, int len);
133
 
 
134
 
/* Constants. */
135
 
 
136
 
/* Keyboard types. */
137
 
#define PC       0
138
 
#define OLIVETTI 1
139
 
#define DUTCH    2
140
 
#define EXTENDED 3
141
 
 
142
 
/* Colors. */
143
 
#define BLACK   0
144
 
#define RED     1
145
 
#define GREEN   2
146
 
#define YELLOW  3
147
 
#define BLUE    4
148
 
#define MAGENTA 5
149
 
#define CYAN    6
150
 
#define WHITE   7
151
 
#define GREY    8
152
 
#define DEFAULT 9
153
 
 
154
 
/* Blank commands */
155
 
#define BLANKSCREEN     -1
156
 
#define UNBLANKSCREEN   -2
157
 
#define BLANKEDSCREEN   -3
158
 
 
159
 
/* <linux/tiocl.h> fallback */
160
 
#ifndef TIOCL_BLANKSCREEN
161
 
# define TIOCL_UNBLANKSCREEN    4       /* unblank screen */
162
 
# define TIOCL_SETVESABLANK     10      /* set vesa blanking mode */
163
 
# define TIOCL_BLANKSCREEN      14      /* keep screen blank even if a key is pressed */
164
 
# define TIOCL_BLANKEDSCREEN    15      /* return which vt was blanked */
165
 
#endif
166
 
 
167
 
/* Control sequences. */
168
 
#define ESC "\033"
169
 
#define DCS "\033P"
170
 
#define ST  "\033\\"
171
 
 
172
 
/* Static variables. */
173
 
 
174
 
/* Option flags.  Set if the option is to be invoked. */
175
 
int opt_term, opt_reset, opt_initialize, opt_cursor;
176
 
int opt_linewrap, opt_snow, opt_softscroll, opt_default, opt_foreground;
177
 
int opt_background, opt_bold, opt_blink, opt_reverse, opt_underline;
178
 
int opt_store, opt_clear, opt_blank, opt_snap, opt_snapfile, opt_standout;
179
 
int opt_append, opt_ulcolor, opt_hbcolor, opt_halfbright, opt_repeat;
180
 
int opt_tabs, opt_clrtabs, opt_regtabs, opt_appcursorkeys, opt_inversescreen;
181
 
int opt_msg, opt_msglevel, opt_powersave, opt_powerdown;
182
 
int opt_blength, opt_bfreq;
183
 
 
184
 
/* Option controls.  The variable names have been contracted to ensure
185
 
 * uniqueness.
186
 
 */
187
 
char *opt_te_terminal_name;     /* Terminal name. */
188
 
int opt_cu_on, opt_li_on, opt_sn_on, opt_so_on, opt_bo_on, opt_hb_on, opt_bl_on;
189
 
int opt_re_on, opt_un_on, opt_rep_on, opt_appck_on, opt_invsc_on;
190
 
int opt_msg_on;                 /* Boolean switches. */
191
 
int opt_ke_type;                /* Keyboard type. */
192
 
int opt_fo_color, opt_ba_color; /* Colors. */
193
 
int opt_ul_color, opt_hb_color;
194
 
int opt_cl_all;                 /* Clear all or rest. */
195
 
int opt_bl_min;                 /* Blank screen. */
196
 
int opt_blength_l;
197
 
int opt_bfreq_f;
198
 
int opt_sn_num;                 /* Snap screen. */
199
 
int opt_st_attr;
200
 
int opt_rt_len;                 /* regular tab length */
201
 
int opt_tb_array[161];          /* Array for tab list */
202
 
int opt_msglevel_num;
203
 
int opt_ps_mode, opt_pd_min;    /* powersave mode/powerdown time */
204
 
 
205
 
char opt_sn_name[PATH_MAX + 1] = "screen.dump";
206
 
 
207
 
static void screendump(int vcnum, FILE *F);
208
 
 
209
 
/* Command line parsing routines.
210
 
 *
211
 
 * Note that it is an error for a given option to be invoked more than once.
212
 
 */
213
 
 
214
 
static void
215
 
parse_term(int argc, char **argv, int *option, char **opt_term, int *bad_arg) {
216
 
        /* argc: Number of arguments for this option. */
217
 
        /* argv: Arguments for this option. */
218
 
        /* option: Term flag to set. */
219
 
        /* opt_term: Terminal name to set. */
220
 
        /* bad_arg: Set to true if an error is detected. */
221
 
 
222
 
/* Parse a -term specification. */
223
 
 
224
 
        if (argc != 1 || *option)
225
 
                *bad_arg = TRUE;
226
 
        *option = TRUE;
227
 
        if (argc == 1)
228
 
                *opt_term = argv[0];
229
 
}
230
 
 
231
 
static void
232
 
parse_none(int argc, char **argv, int *option, int *bad_arg) {
233
 
        /* argc: Number of arguments for this option. */
234
 
        /* argv: Arguments for this option. */
235
 
        /* option: Term flag to set. */
236
 
        /* bad_arg: Set to true if an error is detected. */
237
 
 
238
 
/* Parse a parameterless specification. */
239
 
 
240
 
        if (argc != 0 || *option)
241
 
                *bad_arg = TRUE;
242
 
        *option = TRUE;
243
 
}
244
 
 
245
 
static void
246
 
parse_switch(int argc, char **argv, int *option, int *opt_on, int *bad_arg) {
247
 
        /* argc: Number of arguments for this option. */
248
 
        /* argv: Arguments for this option. */
249
 
        /* option: Option flag to set. */
250
 
        /* opt_on: Boolean option switch to set or reset. */
251
 
        /* bad_arg: Set to true if an error is detected. */
252
 
 
253
 
/* Parse a boolean (on/off) specification. */
254
 
 
255
 
        if (argc > 1 || *option)
256
 
                *bad_arg = TRUE;
257
 
        *option = TRUE;
258
 
        if (argc == 1) {
259
 
                if (strcmp(argv[0], "on") == 0)
260
 
                        *opt_on = TRUE;
261
 
                else if (strcmp(argv[0], "off") == 0)
262
 
                        *opt_on = FALSE;
263
 
                else
264
 
                        *bad_arg = TRUE;
265
 
        } else {
266
 
                *opt_on = TRUE;
267
 
        }
268
 
}
269
 
 
270
 
static void
271
 
par_color(int argc, char **argv, int *option, int *opt_color, int *bad_arg) {
272
 
        /* argc: Number of arguments for this option. */
273
 
        /* argv: Arguments for this option. */
274
 
        /* option: Color flag to set. */
275
 
        /* opt_color: Color to set. */
276
 
        /* bad_arg: Set to true if an error is detected. */
277
 
 
278
 
/* Parse a -foreground or -background specification. */
279
 
 
280
 
        if (argc != 1 || *option)
281
 
                *bad_arg = TRUE;
282
 
        *option = TRUE;
283
 
        if (argc == 1) {
284
 
                if (strcmp(argv[0], "black") == 0)
285
 
                        *opt_color = BLACK;
286
 
                else if (strcmp(argv[0], "red") == 0)
287
 
                        *opt_color = RED;
288
 
                else if (strcmp(argv[0], "green") == 0)
289
 
                        *opt_color = GREEN;
290
 
                else if (strcmp(argv[0], "yellow") == 0)
291
 
                        *opt_color = YELLOW;
292
 
                else if (strcmp(argv[0], "blue") == 0)
293
 
                        *opt_color = BLUE;
294
 
                else if (strcmp(argv[0], "magenta") == 0)
295
 
                        *opt_color = MAGENTA;
296
 
                else if (strcmp(argv[0], "cyan") == 0)
297
 
                        *opt_color = CYAN;
298
 
                else if (strcmp(argv[0], "white") == 0)
299
 
                        *opt_color = WHITE;
300
 
                else if (strcmp(argv[0], "default") == 0)
301
 
                        *opt_color = DEFAULT;
302
 
                else if (isdigit(argv[0][0]))
303
 
                        *opt_color = atoi(argv[0]);
304
 
                else
305
 
                        *bad_arg = TRUE;
306
 
 
307
 
                if(*opt_color < 0 || *opt_color > 9 || *opt_color == 8)
308
 
                        *bad_arg = TRUE;
309
 
        }
310
 
}
311
 
 
312
 
static void
313
 
par_color2(int argc, char **argv, int *option, int *opt_color, int *bad_arg) {
314
 
        /* argc: Number of arguments for this option. */
315
 
        /* argv: Arguments for this option. */
316
 
        /* option: Color flag to set. */
317
 
        /* opt_color: Color to set. */
318
 
        /* bad_arg: Set to true if an error is detected. */
319
 
 
320
 
/* Parse a -ulcolor or -hbcolor specification. */
321
 
 
322
 
        if (!argc || argc > 2 || *option)
323
 
                *bad_arg = TRUE;
324
 
        *option = TRUE;
325
 
        *opt_color = 0;
326
 
        if (argc == 2) {
327
 
                if (strcmp(argv[0], "bright") == 0)
328
 
                        *opt_color = 8;
329
 
                else {
330
 
                        *bad_arg = TRUE;
331
 
                        return;
332
 
                }
333
 
        }
334
 
        if (argc) {
335
 
                if (strcmp(argv[argc-1], "black") == 0) {
336
 
                        if(*opt_color)
337
 
                                *bad_arg = TRUE;
338
 
                        else
339
 
                                *opt_color = BLACK;
340
 
                } else if (strcmp(argv[argc-1], "grey") == 0) {
341
 
                        if(*opt_color)
342
 
                                *bad_arg = TRUE;
343
 
                        else
344
 
                                *opt_color = GREY;
345
 
                } else if (strcmp(argv[argc-1], "red") == 0)
346
 
                        *opt_color |= RED;
347
 
                else if (strcmp(argv[argc-1], "green") == 0)
348
 
                        *opt_color |= GREEN;
349
 
                else if (strcmp(argv[argc-1], "yellow") == 0)
350
 
                        *opt_color |= YELLOW;
351
 
                else if (strcmp(argv[argc-1], "blue") == 0)
352
 
                        *opt_color |= BLUE;
353
 
                else if (strcmp(argv[argc-1], "magenta") == 0)
354
 
                        *opt_color |= MAGENTA;
355
 
                else if (strcmp(argv[argc-1], "cyan") == 0)
356
 
                        *opt_color |= CYAN;
357
 
                else if (strcmp(argv[argc-1], "white") == 0)
358
 
                        *opt_color |= WHITE;
359
 
                else if (isdigit(argv[argc-1][0]))
360
 
                        *opt_color = atoi(argv[argc-1]);
361
 
                else
362
 
                        *bad_arg = TRUE;
363
 
                if(*opt_color < 0 || *opt_color > 15)
364
 
                        *bad_arg = TRUE;
365
 
        }
366
 
}
367
 
 
368
 
static void
369
 
parse_clear(int argc, char **argv, int *option, int *opt_all, int *bad_arg) {
370
 
        /* argc: Number of arguments for this option. */
371
 
        /* argv: Arguments for this option. */
372
 
        /* option: Clear flag to set. */
373
 
        /* opt_all: Clear all switch to set or reset. */
374
 
        /* bad_arg: Set to true if an error is detected. */
375
 
 
376
 
/* Parse a -clear specification. */
377
 
 
378
 
        if (argc > 1 || *option)
379
 
                *bad_arg = TRUE;
380
 
        *option = TRUE;
381
 
        if (argc == 1) {
382
 
                if (strcmp(argv[0], "all") == 0)
383
 
                        *opt_all = TRUE;
384
 
                else if (strcmp(argv[0], "rest") == 0)
385
 
                        *opt_all = FALSE;
386
 
                else
387
 
                        *bad_arg = TRUE;
388
 
        } else {
389
 
                *opt_all = TRUE;
390
 
        }
391
 
}
392
 
 
393
 
static void
394
 
parse_blank(int argc, char **argv, int *option, int *opt_all, int *bad_arg) {
395
 
        /* argc: Number of arguments for this option. */
396
 
        /* argv: Arguments for this option. */
397
 
        /* option: Clear flag to set. */
398
 
        /* opt_all: Clear all switch to set or reset. */
399
 
        /* bad_arg: Set to true if an error is detected. */
400
 
 
401
 
/* Parse a -blank specification. */
402
 
 
403
 
        if (argc > 1 || *option)
404
 
                *bad_arg = TRUE;
405
 
        *option = TRUE;
406
 
        if (argc == 1) {
407
 
                if (!strcmp(argv[0], "force"))
408
 
                        *opt_all = BLANKSCREEN;
409
 
                else if (!strcmp(argv[0], "poke"))
410
 
                        *opt_all = UNBLANKSCREEN;
411
 
                else {
412
 
                        *opt_all = atoi(argv[0]);
413
 
                        if ((*opt_all > 60) || (*opt_all < 0))
414
 
                                *bad_arg = TRUE;
415
 
                }
416
 
        } else {
417
 
                *opt_all = BLANKEDSCREEN;
418
 
        }
419
 
}
420
 
 
421
 
static void
422
 
parse_powersave(int argc, char **argv, int *option, int *opt_mode, int *bad_arg) {
423
 
        /* argc: Number of arguments for this option. */
424
 
        /* argv: Arguments for this option. */
425
 
        /* option: powersave flag to set. */
426
 
        /* opt_mode: Powersaving mode, defined in vesa_blank.c */
427
 
        /* bad_arg: Set to true if an error is detected. */
428
 
 
429
 
/* Parse a -powersave mode specification. */
430
 
 
431
 
        if (argc > 1 || *option)
432
 
                *bad_arg = TRUE;
433
 
        *option = TRUE;
434
 
        if (argc == 1) {
435
 
                if (strcmp(argv[0], "on") == 0)
436
 
                        *opt_mode = 1;
437
 
                else if (strcmp(argv[0], "vsync") == 0)
438
 
                        *opt_mode = 1;
439
 
                else if (strcmp(argv[0], "hsync") == 0)
440
 
                        *opt_mode = 2;
441
 
                else if (strcmp(argv[0], "powerdown") == 0)
442
 
                        *opt_mode = 3;
443
 
                else if (strcmp(argv[0], "off") == 0)
444
 
                        *opt_mode = 0;
445
 
                else
446
 
                        *bad_arg = TRUE;
447
 
        } else {
448
 
                *opt_mode = 0;
449
 
        }
450
 
}
451
 
 
452
 
#if 0
453
 
static void
454
 
parse_standout(int argc, char *argv, int *option, int *opt_all, int *bad_arg) {
455
 
        /* argc: Number of arguments for this option. */
456
 
        /* argv: Arguments for this option. */
457
 
        /* option: Clear flag to set. */
458
 
        /* opt_all: Clear all switch to set or reset. */
459
 
        /* bad_arg: Set to true if an error is detected. */
460
 
 
461
 
/* Parse a -standout specification. */
462
 
 
463
 
        if (argc > 1 || *option)
464
 
                *bad_arg = TRUE;
465
 
        *option = TRUE;
466
 
        if (argc == 1)
467
 
                *opt_all = atoi(argv[0]);
468
 
        else
469
 
                *opt_all = -1;
470
 
}
471
 
#endif
472
 
 
473
 
static void
474
 
parse_msglevel(int argc, char **argv, int *option, int *opt_all, int *bad_arg) {
475
 
        /* argc: Number of arguments for this option. */
476
 
        /* argv: Arguments for this option. */
477
 
        /* option: Clear flag to set. */
478
 
        /* opt_all: Clear all switch to set or reset. */
479
 
        /* bad_arg: Set to true if an error is detected. */
480
 
 
481
 
        if (argc > 1 || *option)
482
 
                *bad_arg = TRUE;
483
 
        *option = TRUE;
484
 
        if (argc == 1) {
485
 
                *opt_all = atoi(argv[0]);
486
 
                if (*opt_all < 0 || *opt_all > 8)
487
 
                        *bad_arg = TRUE;
488
 
        } else {
489
 
                *opt_all = -1;
490
 
        }
491
 
}
492
 
 
493
 
static void
494
 
parse_snap(int argc, char **argv, int *option, int *opt_all, int *bad_arg) {
495
 
        /* argc: Number of arguments for this option. */
496
 
        /* argv: Arguments for this option. */
497
 
        /* option: Clear flag to set. */
498
 
        /* opt_all: Clear all switch to set or reset. */
499
 
        /* bad_arg: Set to true if an error is detected. */
500
 
 
501
 
/* Parse a -dump or -append specification. */
502
 
 
503
 
        if (argc > 1 || *option)
504
 
                *bad_arg = TRUE;
505
 
        *option = TRUE;
506
 
        if (argc == 1) {
507
 
                *opt_all = atoi(argv[0]);
508
 
                if ((*opt_all <= 0))
509
 
                        *bad_arg = TRUE;
510
 
        } else {
511
 
                *opt_all = 0;
512
 
        }
513
 
}
514
 
 
515
 
static void
516
 
parse_snapfile(int argc, char **argv, int *option, int *opt_all, int *bad_arg) {
517
 
        /* argc: Number of arguments for this option. */
518
 
        /* argv: Arguments for this option. */
519
 
        /* option: Clear flag to set. */
520
 
        /* opt_all: Clear all switch to set or reset. */
521
 
        /* bad_arg: Set to true if an error is detected. */
522
 
 
523
 
/* Parse a -file specification. */
524
 
 
525
 
        if (argc != 1 || *option)
526
 
                *bad_arg = TRUE;
527
 
        *option = TRUE;
528
 
        memset(opt_all, 0, PATH_MAX + 1);
529
 
        if (argc == 1)
530
 
                strncpy((char *)opt_all, argv[0], PATH_MAX);
531
 
}
532
 
 
533
 
static void
534
 
parse_tabs(int argc, char **argv, int *option, int *tab_array, int *bad_arg) {
535
 
        /* argc: Number of arguments for this option. */
536
 
        /* argv: Arguments for this option. */
537
 
        /* option: Clear flag to set. */
538
 
        /* tab_array: Array of tabs */
539
 
        /* bad_arg: Set to true if an error is detected. */
540
 
 
541
 
        if (*option || argc > 160)
542
 
                *bad_arg = TRUE;
543
 
        *option = TRUE;
544
 
        tab_array[argc] = -1;
545
 
        while(argc--) {
546
 
                tab_array[argc] = atoi(argv[argc]);
547
 
                if(tab_array[argc] < 1 || tab_array[argc] > 160) {
548
 
                        *bad_arg = TRUE;
549
 
                        return;
550
 
                }
551
 
        }
552
 
}
553
 
 
554
 
static void
555
 
parse_clrtabs(int argc, char **argv, int *option, int *tab_array, int *bad_arg) {
556
 
        /* argc: Number of arguments for this option. */
557
 
        /* argv: Arguments for this option. */
558
 
        /* option: Clear flag to set. */
559
 
        /* tab_array: Array of tabs */
560
 
        /* bad_arg: Set to true if an error is detected. */
561
 
 
562
 
        if (*option || argc > 160)
563
 
                *bad_arg = TRUE;
564
 
        *option = TRUE;
565
 
        if(argc == 0) {
566
 
                tab_array[0] = -1;
567
 
                return;
568
 
        }
569
 
        tab_array[argc] = -1;
570
 
        while(argc--) {
571
 
                tab_array[argc] = atoi(argv[argc]);
572
 
                if(tab_array[argc] < 1 || tab_array[argc] > 160) {
573
 
                        *bad_arg = TRUE;
574
 
                        return;
575
 
                }
576
 
        }
577
 
}
578
 
 
579
 
static void
580
 
parse_regtabs(int argc, char **argv, int *option, int *opt_len, int *bad_arg) {
581
 
        /* argc: Number of arguments for this option. */
582
 
        /* argv: Arguments for this option. */
583
 
        /* option: Clear flag to set. */
584
 
        /* opt_len: Regular tab length. */
585
 
        /* bad_arg: Set to true if an error is detected. */
586
 
 
587
 
        if (*option || argc > 1)
588
 
                *bad_arg = TRUE;
589
 
        *option = TRUE;
590
 
        if(argc == 0) {
591
 
                *opt_len = 8;
592
 
                return;
593
 
        }
594
 
        *opt_len = atoi(argv[0]);
595
 
        if(*opt_len < 1 || *opt_len > 160) {
596
 
                *bad_arg = TRUE;
597
 
                return;
598
 
        }
599
 
}
600
 
 
601
 
 
602
 
static void
603
 
parse_blength(int argc, char **argv, int *option, int *opt_all, int *bad_arg) {
604
 
        /* argc: Number of arguments for this option. */
605
 
        /* argv: Arguments for this option. */
606
 
        /* option: Clear flag to set. */
607
 
        /* opt_all */
608
 
        /* bad_arg: Set to true if an error is detected. */
609
 
 
610
 
/* Parse  -blength specification. */
611
 
 
612
 
        if (argc > 1 || *option)
613
 
                *bad_arg = TRUE;
614
 
        *option = TRUE;
615
 
        if (argc == 1) {
616
 
                *opt_all = atoi(argv[0]);
617
 
                if (*opt_all > 2000)
618
 
                        *bad_arg = TRUE;
619
 
        } else {
620
 
                *opt_all = 0;
621
 
        }
622
 
}
623
 
 
624
 
static void
625
 
parse_bfreq(int argc, char **argv, int *option, int *opt_all, int *bad_arg) {
626
 
        /* argc: Number of arguments for this option. */
627
 
        /* argv: Arguments for this option. */
628
 
        /* option: Clear flag to set. */
629
 
        /* opt_all */
630
 
        /* bad_arg: Set to true if an error is detected. */
631
 
 
632
 
/* Parse  -bfreq specification. */
633
 
 
634
 
        if (argc > 1 || *option)
635
 
                *bad_arg = TRUE;
636
 
        *option = TRUE;
637
 
        if (argc == 1) {
638
 
                *opt_all = atoi(argv[0]);
639
 
        } else {
640
 
                *opt_all = 0;
641
 
        }
642
 
}
643
 
 
644
 
 
645
 
static void
646
 
show_tabs(void) {
647
 
        int i, co = tigetnum("cols");
648
 
 
649
 
        if(co > 0) {
650
 
                printf("\r         ");
651
 
                for(i = 10; i < co-2; i+=10)
652
 
                        printf("%-10d", i);
653
 
                putchar('\n');
654
 
                for(i = 1; i <= co; i++)
655
 
                        putchar(i%10+'0');
656
 
                putchar('\n');
657
 
                for(i = 1; i < co; i++)
658
 
                        printf("\tT\b");
659
 
                putchar('\n');
660
 
        }
661
 
}
662
 
 
663
 
 
664
 
#define STRCMP(str1,str2) strncmp(str1,str2,strlen(str1))
665
 
 
666
 
static void
667
 
parse_option(char *option, int argc, char **argv, int *bad_arg) {
668
 
        /* option: Option with leading '-' removed. */
669
 
        /* argc: Number of arguments for this option. */
670
 
        /* argv: Arguments for this option. */
671
 
        /* bad_arg: Set to true if an error is detected. */
672
 
 
673
 
/* Parse a single specification. */
674
 
 
675
 
        if (STRCMP(option, "term") == 0)
676
 
                parse_term(argc, argv, &opt_term, &opt_te_terminal_name, bad_arg);
677
 
        else if (STRCMP(option, "reset") == 0)
678
 
                parse_none(argc, argv, &opt_reset, bad_arg);
679
 
        else if (STRCMP(option, "initialize") == 0)
680
 
                parse_none(argc, argv, &opt_initialize, bad_arg);
681
 
        else if (STRCMP(option, "cursor") == 0)
682
 
                parse_switch(argc, argv, &opt_cursor, &opt_cu_on, bad_arg);
683
 
        else if (STRCMP(option, "repeat") == 0)
684
 
                parse_switch(argc, argv, &opt_repeat, &opt_rep_on, bad_arg);
685
 
        else if (STRCMP(option, "appcursorkeys") == 0)
686
 
                parse_switch(argc, argv, &opt_appcursorkeys, &opt_appck_on, bad_arg);
687
 
        else if (STRCMP(option, "linewrap") == 0)
688
 
                parse_switch(argc, argv, &opt_linewrap, &opt_li_on, bad_arg);
689
 
#if 0
690
 
        else if (STRCMP(option, "snow") == 0)
691
 
                parse_switch(argc, argv, &opt_snow, &opt_sn_on, bad_arg);
692
 
        else if (STRCMP(option, "softscroll") == 0)
693
 
                parse_switch(argc, argv, &opt_softscroll, &opt_so_on, bad_arg);
694
 
#endif
695
 
        else if (STRCMP(option, "default") == 0)
696
 
                parse_none(argc, argv, &opt_default, bad_arg);
697
 
        else if (STRCMP(option, "foreground") == 0)
698
 
                par_color(argc, argv, &opt_foreground, &opt_fo_color, bad_arg);
699
 
        else if (STRCMP(option, "background") == 0)
700
 
                par_color(argc, argv, &opt_background, &opt_ba_color, bad_arg);
701
 
        else if (STRCMP(option, "ulcolor") == 0)
702
 
                par_color2(argc, argv, &opt_ulcolor, &opt_ul_color, bad_arg);
703
 
        else if (STRCMP(option, "hbcolor") == 0)
704
 
                par_color2(argc, argv, &opt_hbcolor, &opt_hb_color, bad_arg);
705
 
        else if (STRCMP(option, "inversescreen") == 0)
706
 
                parse_switch(argc, argv, &opt_inversescreen, &opt_invsc_on, bad_arg);
707
 
        else if (STRCMP(option, "bold") == 0)
708
 
                parse_switch(argc, argv, &opt_bold, &opt_bo_on, bad_arg);
709
 
        else if (STRCMP(option, "half-bright") == 0)
710
 
                parse_switch(argc, argv, &opt_halfbright, &opt_hb_on, bad_arg);
711
 
        else if (STRCMP(option, "blink") == 0)
712
 
                parse_switch(argc, argv, &opt_blink, &opt_bl_on, bad_arg);
713
 
        else if (STRCMP(option, "reverse") == 0)
714
 
                parse_switch(argc, argv, &opt_reverse, &opt_re_on, bad_arg);
715
 
        else if (STRCMP(option, "underline") == 0)
716
 
                parse_switch(argc, argv, &opt_underline, &opt_un_on, bad_arg);
717
 
        else if (STRCMP(option, "store") == 0)
718
 
                parse_none(argc, argv, &opt_store, bad_arg);
719
 
        else if (STRCMP(option, "clear") == 0)
720
 
                parse_clear(argc, argv, &opt_clear, &opt_cl_all, bad_arg);
721
 
        else if (STRCMP(option, "tabs") == 0)
722
 
                parse_tabs(argc, argv, &opt_tabs, opt_tb_array, bad_arg);
723
 
        else if (STRCMP(option, "clrtabs") == 0)
724
 
                parse_clrtabs(argc, argv, &opt_clrtabs, opt_tb_array, bad_arg);
725
 
        else if (STRCMP(option, "regtabs") == 0)
726
 
                parse_regtabs(argc, argv, &opt_regtabs, &opt_rt_len, bad_arg);
727
 
        else if (STRCMP(option, "blank") == 0)
728
 
                parse_blank(argc, argv, &opt_blank, &opt_bl_min, bad_arg);
729
 
        else if (STRCMP(option, "dump") == 0)
730
 
                parse_snap(argc, argv, &opt_snap, &opt_sn_num, bad_arg);
731
 
        else if (STRCMP(option, "append") == 0)
732
 
                parse_snap(argc, argv, &opt_append, &opt_sn_num, bad_arg);
733
 
        else if (STRCMP(option, "file") == 0)
734
 
                parse_snapfile(argc, argv, &opt_snapfile, (int *)opt_sn_name, bad_arg);
735
 
        else if (STRCMP(option, "msg") == 0)
736
 
                parse_switch(argc, argv, &opt_msg, &opt_msg_on, bad_arg);
737
 
        else if (STRCMP(option, "msglevel") == 0)
738
 
                parse_msglevel(argc, argv, &opt_msglevel, &opt_msglevel_num, bad_arg);
739
 
        else if (STRCMP(option, "powersave") == 0)
740
 
                parse_powersave(argc, argv, &opt_powersave, &opt_ps_mode, bad_arg);
741
 
        else if (STRCMP(option, "powerdown") == 0)
742
 
                parse_blank(argc, argv, &opt_powerdown, &opt_pd_min, bad_arg);
743
 
        else if (STRCMP(option, "blength") == 0)
744
 
                parse_blength(argc, argv, &opt_blength, &opt_blength_l, bad_arg);
745
 
        else if (STRCMP(option, "bfreq") == 0)
746
 
                parse_bfreq(argc, argv, &opt_bfreq, &opt_bfreq_f, bad_arg);
747
 
#if 0
748
 
        else if (STRCMP(option, "standout") == 0)
749
 
                parse_standout(argc, argv, &opt_standout, &opt_st_attr, bad_arg);
750
 
#endif
751
 
        else
752
 
                *bad_arg = TRUE;
753
 
}
754
 
 
755
 
/* End of command line parsing routines. */
756
 
 
757
 
static void
758
 
usage(char *prog_name) {
759
 
/* Print error message about arguments, and the command's syntax. */
760
 
 
761
 
        fprintf(stderr, _("%s: Argument error, usage\n"), prog_name);
762
 
        fprintf(stderr, "\n");
763
 
        fprintf(stderr, "%s\n", prog_name);
764
 
        fprintf(stderr, _("  [ -term terminal_name ]\n"));
765
 
        fprintf(stderr, _("  [ -reset ]\n"));
766
 
        fprintf(stderr, _("  [ -initialize ]\n"));
767
 
        fprintf(stderr, _("  [ -cursor [on|off] ]\n"));
768
 
#if 0
769
 
        fprintf(stderr, _("  [ -snow [on|off] ]\n"));
770
 
        fprintf(stderr, _("  [ -softscroll [on|off] ]\n"));
771
 
#endif
772
 
        fprintf(stderr, _("  [ -repeat [on|off] ]\n"));
773
 
        fprintf(stderr, _("  [ -appcursorkeys [on|off] ]\n"));
774
 
        fprintf(stderr, _("  [ -linewrap [on|off] ]\n"));
775
 
        fprintf(stderr, _("  [ -default ]\n"));
776
 
        fprintf(stderr, _("  [ -foreground black|blue|green|cyan"));
777
 
        fprintf(stderr, _("|red|magenta|yellow|white|default ]\n"));
778
 
        fprintf(stderr, _("  [ -background black|blue|green|cyan"));
779
 
        fprintf(stderr, _("|red|magenta|yellow|white|default ]\n"));
780
 
        fprintf(stderr, _("  [ -ulcolor black|grey|blue|green|cyan"));
781
 
        fprintf(stderr, _("|red|magenta|yellow|white ]\n"));
782
 
        fprintf(stderr, _("  [ -ulcolor bright blue|green|cyan"));
783
 
        fprintf(stderr, _("|red|magenta|yellow|white ]\n"));
784
 
        fprintf(stderr, _("  [ -hbcolor black|grey|blue|green|cyan"));
785
 
        fprintf(stderr, _("|red|magenta|yellow|white ]\n"));
786
 
        fprintf(stderr, _("  [ -hbcolor bright blue|green|cyan"));
787
 
        fprintf(stderr, _("|red|magenta|yellow|white ]\n"));
788
 
#if 0
789
 
        fprintf(stderr, _("  [ -standout [ attr ] ]\n"));
790
 
#endif
791
 
        fprintf(stderr, _("  [ -inversescreen [on|off] ]\n"));
792
 
        fprintf(stderr, _("  [ -bold [on|off] ]\n"));
793
 
        fprintf(stderr, _("  [ -half-bright [on|off] ]\n"));
794
 
        fprintf(stderr, _("  [ -blink [on|off] ]\n"));
795
 
        fprintf(stderr, _("  [ -reverse [on|off] ]\n"));
796
 
        fprintf(stderr, _("  [ -underline [on|off] ]\n"));
797
 
        fprintf(stderr, _("  [ -store ]\n"));
798
 
        fprintf(stderr, _("  [ -clear [all|rest] ]\n"));
799
 
        fprintf(stderr, _("  [ -tabs [ tab1 tab2 tab3 ... ] ]      (tabn = 1-160)\n"));
800
 
        fprintf(stderr, _("  [ -clrtabs [ tab1 tab2 tab3 ... ] ]   (tabn = 1-160)\n"));
801
 
        fprintf(stderr, _("  [ -regtabs [1-160] ]\n"));
802
 
        fprintf(stderr, _("  [ -blank [0-60|force|poke] ]\n"));
803
 
        fprintf(stderr, _("  [ -dump   [1-NR_CONSOLES] ]\n"));
804
 
        fprintf(stderr, _("  [ -append [1-NR_CONSOLES] ]\n"));
805
 
        fprintf(stderr, _("  [ -file dumpfilename ]\n"));
806
 
        fprintf(stderr, _("  [ -msg [on|off] ]\n"));
807
 
        fprintf(stderr, _("  [ -msglevel [0-8] ]\n"));
808
 
        fprintf(stderr, _("  [ -powersave [on|vsync|hsync|powerdown|off] ]\n"));
809
 
        fprintf(stderr, _("  [ -powerdown [0-60] ]\n"));
810
 
        fprintf(stderr, _("  [ -blength [0-2000] ]\n"));
811
 
        fprintf(stderr, _("  [ -bfreq freqnumber ]\n"));
812
 
}
813
 
 
814
 
static char *ti_entry(const char *name) {
815
 
        /* name: Terminfo capability string to lookup. */
816
 
 
817
 
/* Return the specified terminfo string, or an empty string if no such terminfo
818
 
 * capability exists.
819
 
 */
820
 
 
821
 
        char *buf_ptr;
822
 
 
823
 
        if ((buf_ptr = tigetstr((char *)name)) == (char *)-1)
824
 
                buf_ptr = NULL;
825
 
        return buf_ptr;
826
 
}
827
 
 
828
 
static void
829
 
perform_sequence(int vcterm) {
830
 
        /* vcterm: Set if terminal is a virtual console. */
831
 
 
832
 
        int result;
833
 
/* Perform the selected options. */
834
 
 
835
 
        /* -reset. */
836
 
        if (opt_reset) {
837
 
                putp(ti_entry("rs1"));
838
 
        }
839
 
 
840
 
        /* -initialize. */
841
 
        if (opt_initialize) {
842
 
                putp(ti_entry("is2"));
843
 
        }
844
 
 
845
 
        /* -cursor [on|off]. */
846
 
        if (opt_cursor) {
847
 
                if (opt_cu_on)
848
 
                        putp(ti_entry("cnorm"));
849
 
                else
850
 
                        putp(ti_entry("civis"));
851
 
        }
852
 
 
853
 
        /* -linewrap [on|off]. Vc only (vt102) */
854
 
        if (opt_linewrap && vcterm) {
855
 
                if (opt_li_on)
856
 
                        printf("\033[?7h");
857
 
                else
858
 
                        printf("\033[?7l");
859
 
        }
860
 
 
861
 
        /* -repeat [on|off]. Vc only (vt102) */
862
 
        if (opt_repeat && vcterm) {
863
 
                if (opt_rep_on)
864
 
                        printf("\033[?8h");
865
 
                else
866
 
                        printf("\033[?8l");
867
 
        }
868
 
 
869
 
        /* -appcursorkeys [on|off]. Vc only (vt102) */
870
 
        if (opt_appcursorkeys && vcterm) {
871
 
                if (opt_appck_on)
872
 
                        printf("\033[?1h");
873
 
                else
874
 
                        printf("\033[?1l");
875
 
        }
876
 
 
877
 
#if 0
878
 
        /* -snow [on|off].  Vc only. */
879
 
        if (opt_snow && vcterm) {
880
 
                if (opt_sn_on)
881
 
                        printf("%s%s%s", DCS, "snow.on", ST);
882
 
                else
883
 
                        printf("%s%s%s", DCS, "snow.off", ST);
884
 
        }
885
 
 
886
 
        /* -softscroll [on|off].  Vc only. */
887
 
        if (opt_softscroll && vcterm) {
888
 
                if (opt_so_on)
889
 
                        printf("%s%s%s", DCS, "softscroll.on", ST);
890
 
                else
891
 
                        printf("%s%s%s", DCS, "softscroll.off", ST);
892
 
        }
893
 
#endif
894
 
 
895
 
        /* -default.  Vc sets default rendition, otherwise clears all
896
 
         * attributes.
897
 
         */
898
 
        if (opt_default) {
899
 
                if (vcterm)
900
 
                        printf("\033[0m");
901
 
                else
902
 
                        putp(ti_entry("sgr0"));
903
 
        }
904
 
 
905
 
        /* -foreground black|red|green|yellow|blue|magenta|cyan|white|default.
906
 
         * Vc only (ANSI).
907
 
         */
908
 
        if (opt_foreground && vcterm) {
909
 
                printf("%s%s%c%s", ESC, "[3", '0' + opt_fo_color, "m");
910
 
        }
911
 
 
912
 
        /* -background black|red|green|yellow|blue|magenta|cyan|white|default.
913
 
         * Vc only (ANSI).
914
 
         */
915
 
        if (opt_background && vcterm) {
916
 
                printf("%s%s%c%s", ESC, "[4", '0' + opt_ba_color, "m");
917
 
        }
918
 
 
919
 
        /* -ulcolor black|red|green|yellow|blue|magenta|cyan|white|default.
920
 
         * Vc only.
921
 
         */
922
 
        if (opt_ulcolor && vcterm) {
923
 
                printf("\033[1;%d]", opt_ul_color);
924
 
        }
925
 
 
926
 
        /* -hbcolor black|red|green|yellow|blue|magenta|cyan|white|default.
927
 
         * Vc only.
928
 
         */
929
 
        if (opt_hbcolor && vcterm) {
930
 
                printf("\033[2;%d]", opt_hb_color);
931
 
        }
932
 
 
933
 
        /* -inversescreen [on|off].  Vc only (vt102).
934
 
         */
935
 
        if (opt_inversescreen) {
936
 
                if (vcterm) {
937
 
                        if (opt_invsc_on)
938
 
                                printf("\033[?5h");
939
 
                        else
940
 
                                printf("\033[?5l");
941
 
                }
942
 
        }
943
 
 
944
 
        /* -bold [on|off].  Vc behaves as expected, otherwise off turns off
945
 
         * all attributes.
946
 
         */
947
 
        if (opt_bold) {
948
 
                if (opt_bo_on)
949
 
                        putp(ti_entry("bold"));
950
 
                else {
951
 
                        if (vcterm)
952
 
                                printf("%s%s", ESC, "[22m");
953
 
                        else
954
 
                                putp(ti_entry("sgr0"));
955
 
                }
956
 
        }
957
 
 
958
 
        /* -half-bright [on|off].  Vc behaves as expected, otherwise off turns off
959
 
         * all attributes.
960
 
         */
961
 
        if (opt_halfbright) {
962
 
                if (opt_hb_on)
963
 
                        putp(ti_entry("dim"));
964
 
                else {
965
 
                        if (vcterm)
966
 
                                printf("%s%s", ESC, "[22m");
967
 
                        else
968
 
                                putp(ti_entry("sgr0"));
969
 
                }
970
 
        }
971
 
 
972
 
        /* -blink [on|off].  Vc behaves as expected, otherwise off turns off
973
 
         * all attributes.
974
 
         */
975
 
        if (opt_blink) {
976
 
                if (opt_bl_on)
977
 
                        putp(ti_entry("blink"));
978
 
                else {
979
 
                        if (vcterm)
980
 
                                printf("%s%s", ESC, "[25m");
981
 
                        else
982
 
                                putp(ti_entry("sgr0"));
983
 
                }
984
 
        }
985
 
 
986
 
        /* -reverse [on|off].  Vc behaves as expected, otherwise off turns
987
 
         * off all attributes.
988
 
         */
989
 
        if (opt_reverse) {
990
 
                if (opt_re_on)
991
 
                        putp(ti_entry("rev"));
992
 
                else {
993
 
                        if (vcterm)
994
 
                                printf("%s%s", ESC, "[27m");
995
 
                        else
996
 
                                putp(ti_entry("sgr0"));
997
 
                }
998
 
        }
999
 
 
1000
 
        /* -underline [on|off]. */
1001
 
        if (opt_underline) {
1002
 
                if (opt_un_on)
1003
 
                        putp(ti_entry("smul"));
1004
 
                else
1005
 
                        putp(ti_entry("rmul"));
1006
 
        }
1007
 
 
1008
 
        /* -store.  Vc only. */
1009
 
        if (opt_store && vcterm) {
1010
 
                printf("\033[8]");
1011
 
        }
1012
 
 
1013
 
        /* -clear [all|rest]. */
1014
 
        if (opt_clear) {
1015
 
                if (opt_cl_all)
1016
 
                        putp(ti_entry("clear"));
1017
 
                else
1018
 
                        putp(ti_entry("ed"));
1019
 
        }
1020
 
 
1021
 
        /* -tabs Vc only. */
1022
 
        if (opt_tabs && vcterm) {
1023
 
                int i;
1024
 
 
1025
 
                if (opt_tb_array[0] == -1)
1026
 
                        show_tabs();
1027
 
                else {
1028
 
                        for(i=0; opt_tb_array[i] > 0; i++)
1029
 
                                printf("\033[%dG\033H", opt_tb_array[i]);
1030
 
                        putchar('\r');
1031
 
                }
1032
 
        }
1033
 
 
1034
 
        /* -clrtabs Vc only. */
1035
 
        if (opt_clrtabs && vcterm) {
1036
 
                int i;
1037
 
 
1038
 
                if (opt_tb_array[0] == -1)
1039
 
                        printf("\033[3g");
1040
 
                else
1041
 
                        for(i=0; opt_tb_array[i] > 0; i++)
1042
 
                                printf("\033[%dG\033[g", opt_tb_array[i]);
1043
 
                putchar('\r');
1044
 
        }
1045
 
 
1046
 
        /* -regtabs Vc only. */
1047
 
        if (opt_regtabs && vcterm) {
1048
 
                int i;
1049
 
 
1050
 
                printf("\033[3g\r");
1051
 
                for(i=opt_rt_len+1; i<=160; i+=opt_rt_len)
1052
 
                        printf("\033[%dC\033H",opt_rt_len);
1053
 
                putchar('\r');
1054
 
        }
1055
 
 
1056
 
        /* -blank [0-60]. */
1057
 
        if (opt_blank && vcterm) {
1058
 
                if (opt_bl_min >= 0)
1059
 
                        printf("\033[9;%d]", opt_bl_min);
1060
 
                else if (opt_bl_min == BLANKSCREEN) {
1061
 
                        char ioctlarg = TIOCL_BLANKSCREEN;
1062
 
                        if (ioctl(0,TIOCLINUX,&ioctlarg))
1063
 
                                fprintf(stderr,_("cannot force blank\n"));
1064
 
                } else if (opt_bl_min == UNBLANKSCREEN) {
1065
 
                        char ioctlarg = TIOCL_UNBLANKSCREEN;
1066
 
                        if (ioctl(0,TIOCLINUX,&ioctlarg))
1067
 
                                fprintf(stderr,_("cannot force unblank\n"));
1068
 
                } else if (opt_bl_min == BLANKEDSCREEN) {
1069
 
                        char ioctlarg = TIOCL_BLANKEDSCREEN;
1070
 
                        int ret;
1071
 
                        ret = ioctl(0,TIOCLINUX,&ioctlarg);
1072
 
                        if (ret < 0)
1073
 
                                fprintf(stderr,_("cannot get blank status\n"));
1074
 
                        else
1075
 
                                printf("%d\n",ret);
1076
 
                }
1077
 
        }
1078
 
 
1079
 
        /* -powersave [on|vsync|hsync|powerdown|off] (console) */
1080
 
        if (opt_powersave) {
1081
 
                char ioctlarg[2];
1082
 
                ioctlarg[0] = TIOCL_SETVESABLANK;
1083
 
                ioctlarg[1] = opt_ps_mode;
1084
 
                if (ioctl(0,TIOCLINUX,ioctlarg))
1085
 
                        fprintf(stderr,_("cannot (un)set powersave mode\n"));
1086
 
        }
1087
 
 
1088
 
        /* -powerdown [0-60]. */
1089
 
        if (opt_powerdown) {
1090
 
                printf("\033[14;%d]", opt_pd_min);
1091
 
        }
1092
 
 
1093
 
#if 0
1094
 
        /* -standout [num]. */
1095
 
        if (opt_standout)
1096
 
                /* nothing */;
1097
 
#endif
1098
 
 
1099
 
        /* -snap [1-NR_CONS]. */
1100
 
        if (opt_snap || opt_append) {
1101
 
                FILE *F;
1102
 
 
1103
 
                F = fopen(opt_sn_name, opt_snap ? "w" : "a");
1104
 
                if (!F) {
1105
 
                        perror(opt_sn_name);
1106
 
                        fprintf(stderr,("setterm: can not open dump file %s for output\n"),
1107
 
                                opt_sn_name); 
1108
 
                        exit(-1);
1109
 
                }
1110
 
                screendump(opt_sn_num, F);
1111
 
                fclose(F);
1112
 
        }
1113
 
 
1114
 
        /* -msg [on|off]. */
1115
 
        if (opt_msg && vcterm) {
1116
 
                if (opt_msg_on)
1117
 
                        /* 7 -- Enable printk's to console */
1118
 
                        result = klogctl(7, NULL, 0);
1119
 
                else
1120
 
                        /*  6 -- Disable printk's to console */
1121
 
                        result = klogctl(6, NULL, 0);
1122
 
 
1123
 
                if (result != 0)
1124
 
                        printf(_("klogctl error: %s\n"), strerror(errno));
1125
 
        }
1126
 
 
1127
 
        /* -msglevel [0-8] */
1128
 
        if (opt_msglevel && vcterm) {
1129
 
                /* 8 -- Set level of messages printed to console */
1130
 
                result = klogctl(8, NULL, opt_msglevel_num);
1131
 
                if (result != 0)
1132
 
                        printf(_("klogctl error: %s\n"), strerror(errno));
1133
 
        }
1134
 
 
1135
 
        /* -blength [0-2000] */
1136
 
        if (opt_blength && vcterm) {
1137
 
                printf("\033[11;%d]", opt_blength_l);
1138
 
        }
1139
 
 
1140
 
        /* -bfreq freqnumber */
1141
 
        if (opt_bfreq && vcterm) {
1142
 
                printf("\033[10;%d]", opt_bfreq_f);
1143
 
        }
1144
 
 
1145
 
}
1146
 
 
1147
 
static void
1148
 
screendump(int vcnum, FILE *F) {
1149
 
    char infile[MAXPATHLEN];
1150
 
    unsigned char header[4];
1151
 
    unsigned int rows, cols;
1152
 
    int fd, i, j;
1153
 
    char *inbuf, *outbuf, *p, *q;
1154
 
 
1155
 
    sprintf(infile, "/dev/vcsa%d", vcnum);
1156
 
    fd = open(infile, O_RDONLY);
1157
 
    if (fd < 0 && vcnum == 0) {
1158
 
        /* vcsa0 is often called vcsa */
1159
 
        sprintf(infile, "/dev/vcsa");
1160
 
        fd = open(infile, O_RDONLY);
1161
 
    }
1162
 
    if (fd < 0) {
1163
 
        /* try devfs name - for zero vcnum just /dev/vcc/a */
1164
 
        /* some gcc's warn for %.u - add 0 */
1165
 
        sprintf(infile, "/dev/vcc/a%.0u", vcnum);
1166
 
        fd = open(infile, O_RDONLY);
1167
 
    }
1168
 
    if (fd < 0) {
1169
 
        sprintf(infile, "/dev/vcsa%d", vcnum);
1170
 
        goto read_error;
1171
 
    }
1172
 
    if (read(fd, header, 4) != 4)
1173
 
        goto read_error;
1174
 
    rows = header[0];
1175
 
    cols = header[1];
1176
 
    if (rows * cols == 0)
1177
 
        goto read_error;
1178
 
    inbuf = malloc(rows*cols*2);
1179
 
    outbuf = malloc(rows*(cols+1));
1180
 
    if(!inbuf || !outbuf) {
1181
 
        fputs(_("Out of memory"), stderr);
1182
 
        goto error;
1183
 
    }
1184
 
    if (read(fd, inbuf, rows*cols*2) != rows*cols*2)
1185
 
            goto read_error;
1186
 
    p = inbuf;
1187
 
    q = outbuf;
1188
 
    for(i=0; i<rows; i++) {
1189
 
        for(j=0; j<cols; j++) {
1190
 
            *q++ = *p;
1191
 
            p += 2;
1192
 
        }
1193
 
        while(j-- > 0 && q[-1] == ' ')
1194
 
          q--;
1195
 
        *q++ = '\n';
1196
 
    }
1197
 
    if (fwrite(outbuf, 1, q-outbuf, F) != q-outbuf) {
1198
 
        fprintf(stderr, _("Error writing screendump\n"));
1199
 
        goto error;
1200
 
    }
1201
 
    close(fd);
1202
 
    return;
1203
 
 
1204
 
read_error:
1205
 
    fprintf(stderr, _("Couldn't read %s\n"), infile);
1206
 
error:
1207
 
    if (fd >= 0)
1208
 
            close(fd);
1209
 
    exit(1);
1210
 
}
1211
 
 
1212
 
int
1213
 
main(int argc, char **argv) {
1214
 
        int bad_arg = FALSE;            /* Set if error in arguments. */
1215
 
        int arg, modifier;
1216
 
        char *term;                     /* Terminal type. */
1217
 
        int vcterm;                     /* Set if terminal is a virtual console. */
1218
 
 
1219
 
        setlocale(LC_ALL, "");
1220
 
        bindtextdomain(PACKAGE, LOCALEDIR);
1221
 
        textdomain(PACKAGE);
1222
 
 
1223
 
        if (argc < 2)
1224
 
                bad_arg = TRUE;
1225
 
 
1226
 
        /* Parse arguments. */
1227
 
 
1228
 
        for (arg = 1; arg < argc;) {
1229
 
                if (*argv[arg] == '-') {
1230
 
 
1231
 
                        /* Parse a single option. */
1232
 
 
1233
 
                        for (modifier = arg + 1; modifier < argc; modifier++) {
1234
 
                                if (*argv[modifier] == '-') break;
1235
 
                        }
1236
 
                        parse_option(argv[arg] + 1, modifier - arg - 1,
1237
 
                                     &argv[arg + 1], &bad_arg);
1238
 
                        arg = modifier;
1239
 
                } else {
1240
 
                        bad_arg = TRUE;
1241
 
                        arg++;
1242
 
                }
1243
 
        }
1244
 
 
1245
 
        /* Display syntax message if error in arguments. */
1246
 
 
1247
 
        if (bad_arg) {
1248
 
                usage(argv[0]);
1249
 
                exit(1);
1250
 
        }
1251
 
 
1252
 
        /* Find out terminal name. */
1253
 
 
1254
 
        if (opt_term) {
1255
 
                term = opt_te_terminal_name;
1256
 
        } else {
1257
 
                term = getenv("TERM");
1258
 
                if (term == NULL) {
1259
 
                        fprintf(stderr, _("%s: $TERM is not defined.\n"),
1260
 
                                argv[0]);
1261
 
                        exit(1);
1262
 
                }
1263
 
        }
1264
 
 
1265
 
        /* Find terminfo entry. */
1266
 
 
1267
 
        setupterm(term, 1, (int *)0);
1268
 
 
1269
 
        /* See if the terminal is a virtual console terminal. */
1270
 
 
1271
 
        vcterm = (!strncmp(term, "con", 3) || !strncmp(term, "linux", 5));
1272
 
 
1273
 
        /* Perform the selected options. */
1274
 
 
1275
 
        perform_sequence(vcterm);
1276
 
 
1277
 
        return 0;
1278
 
}