2 * setleds.c - aeb, 940130, 940909, 991008
4 * Call: setleds [-L] [-D] [-F] [{+|-}{num|caps|scroll}]*
5 * will set or clear the indicated flags on the stdin tty,
6 * and report the settings before and after.
7 * In particular, setleds without arguments will only report.
13 #include <sys/ioctl.h>
17 static void __attribute__ ((noreturn))
22 " setleds [-v] [-L] [-D] [-F] [[+|-][ num | caps | scroll %s]]\n"
24 " setleds +caps -num\n"
25 "will set CapsLock, clear NumLock and leave ScrollLock unchanged.\n"
26 "The settings before and after the change (if any) are reported\n"
27 "when the -v option is given or when no change is requested.\n"
28 "Normally, setleds influences the vt flag settings\n"
29 "(and these are usually reflected in the leds).\n"
30 "With -L, setleds only sets the leds, and leaves the flags alone.\n"
31 "With -D, setleds sets both the flags and the default flags, so\n"
32 "that a subsequent reset will not change the flags.\n"
43 #define onoff(a) ((a) ? _("on ") : _("off"))
45 /* report the bits, in the order seen on the (my) keyboard */
53 printf("NumLock %s Compose %s ScrollLock %s CapsLock %s\n",
54 onoff(leds & LED_NLOCK),
55 onoff(leds & LED_CMPOSE),
56 onoff(leds & LED_SCRLCK),
57 onoff(leds & LED_CLOCK));
62 printf("NumLock %s CapsLock %s ScrollLock %s\n",
63 onoff(leds & LED_NUM),
64 onoff(leds & LED_CAP),
65 onoff(leds & LED_SCR));
73 { "scroll", LED_SCR, LED_SCRLCK },
74 { "num", LED_NUM, LED_NLOCK },
75 { "caps", LED_CAP, LED_CLOCK },
77 { "compose", 0, LED_CMPOSE }
82 getleds(char *cur_leds) {
83 if (ioctl(0, KDGETLED, cur_leds)) {
86 _("Error reading current led setting. Maybe stdin is not a VT?\n"));
92 setleds(char cur_leds) {
93 if (ioctl(0, KDSETLED, cur_leds)) {
101 getflags(char *flags) {
102 if (ioctl(0, KDGKBLED, flags)) {
105 _("Error reading current flags setting. "
106 "Maybe you are not on the console?\n"));
111 static int sunkbdfd = -1;
114 #define arg_state __attribute__ ((unused))
119 static void __attribute__ ((noreturn))
120 sungetleds(arg_state char *cur_leds) {
122 if (ioctl(sunkbdfd, KIOCGLED, cur_leds)) {
125 _("Error reading current led setting from /dev/kbd.\n"));
129 fprintf(stderr, _("KIOCGLED unavailable?\n"));
135 #define arg_state __attribute__ ((unused))
140 static void __attribute__ ((noreturn))
141 sunsetleds(arg_state char *cur_leds) {
143 if (ioctl(sunkbdfd, KIOCSLED, cur_leds)) {
146 _("Error reading current led setting from /dev/kbd.\n"));
150 fprintf(stderr, _("KIOCSLED unavailable?\n"));
156 main(int argc, char **argv) {
157 int optL = 0, optD = 0, optF = 0, verbose = 0;
158 char oleds, nleds, oflags, nflags, odefflags, ndefflags;
159 char nval, ndef, sign;
160 char osunleds = 0, nsunleds, nsunval, nsundef;
164 set_progname(argv[0]);
166 setlocale(LC_ALL, "");
167 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
168 textdomain(PACKAGE_NAME);
170 if (argc == 2 && (!strcmp("-V", argv[1]) || !strcmp("--version", argv[1])))
171 print_version_and_exit();
174 sunkbdfd = open("/dev/kbd", O_RDONLY);
177 fprintf(stderr, _("Error opening /dev/kbd.\n"));
185 sungetleds(&osunleds);
188 if (!strcmp("-L", argv[1]))
190 else if (!strcmp("-D", argv[1]))
192 else if (!strcmp("-F", argv[1]))
194 else if (!strcmp("-v", argv[1]))
202 odefflags = ndefflags = ((oflags >> 4) & 7);
203 oflags = nflags = (oflags & 7);
208 if (setleds(nleds)) {
209 fprintf(stderr, _("Error resetting ledmode\n"));
214 /* If nothing to do, report, even if not verbose */
215 if (!optD && !optL && !optF)
216 optD = optL = optF = 1;
218 printf(_("Current default flags: "));
222 printf(_("Current flags: "));
226 printf(_("Current leds: "));
244 sign = 1; /* by default: set */
247 else if(*ap == '-') {
251 for (lp = leds; (unsigned) (lp-leds) < sizeof(leds)/sizeof(leds[0]); lp++) {
252 if(!strcmp(ap, lp->name)) {
255 nsunval |= lp->sunbit;
258 nsundef |= lp->sunbit;
262 fprintf(stderr, _("unrecognized argument: _%s_\n\n"), ap);
269 ndefflags = (odefflags & ~ndef) | nval;
271 printf(_("Old default flags: "));
273 printf(_("New default flags: "));
278 nflags = ((oflags & ~ndef) | nval);
280 printf(_("Old flags: "));
282 printf(_("New flags: "));
287 if (ioctl(0, KDSKBLED, (ndefflags << 4) | nflags)) {
294 nsunleds = (osunleds & ~nsundef) | nsunval;
296 printf(_("Old leds: "));
298 printf(_("New leds: "));
301 sunsetleds(&nsunleds);
303 nleds = (oleds & ~ndef) | nval;
305 printf(_("Old leds: "));
307 printf(_("New leds: "));