aboutsummaryrefslogtreecommitdiff
path: root/winsup/utils/setmetamode.c
blob: fc1059f7c7eb3e0555d717f19a7c1bb83dbd668f (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
/* setmetamode.c

   Copyright 2006, 2011 Red Hat Inc.

   Written by Kazuhiro Fujieda <fujieda@jaist.ac.jp>

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <cygwin/kd.h>
#include <cygwin/version.h>

static void
usage (void)
{
  fprintf (stderr, "Usage: %s [metabit|escprefix]\n"
	   "\n"
	   "Get or set keyboard meta mode\n"
	   "\n"
	   "  Without argument, it shows the current meta key mode.\n"
	   "  metabit|meta|bit     The meta key sets the top bit of the character.\n"
	   "  escprefix|esc|prefix The meta key sends an escape prefix.\n"
	   "\n"
	   "Other options:\n"
	   "\n"
	   "  -h, --help           This text\n"
	   "  -V, --version        Print program version and exit\n\n",
	   program_invocation_short_name);
}

static void
error (void)
{
  fprintf (stderr,
	   "%s: The standard input isn't a console device.\n",
	   program_invocation_short_name);
}

void
print_version ()
{
  printf ("setmetamode (cygwin) %d.%d.%d\n"
	  "Get or set keyboard meta mode\n"
	  "Copyright (C) 2006 - %s Red Hat, Inc.\n"
	  "This is free software; see the source for copying conditions.  There is NO\n"
	  "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
	  CYGWIN_VERSION_DLL_MAJOR / 1000,
	  CYGWIN_VERSION_DLL_MAJOR % 1000,
	  CYGWIN_VERSION_DLL_MINOR,
	  strrchr (__DATE__, ' ') + 1);
}

struct option longopts[] = {
  {"help", no_argument, NULL, 'h'},
  {"version", no_argument, NULL, 'V'},
  {0, no_argument, NULL, 0}
};
const char *opts = "hV";

int
main (int ac, char *av[])
{
  int param;
  int opt;

  if (ac < 2)
    {
      if (ioctl (0, KDGKBMETA, &param) < 0)
	{
	  error ();
	  return 1;
	}
      if (param == 0x03)
	puts ("metabit");
      else
	puts ("escprefix");
      return 0;
    }

  while ((opt = getopt_long (ac, av, opts, longopts, NULL)) != -1)
    switch (opt)
      {
      case 'h':
	usage ();
	return 0;
      case 'V':
	print_version ();
	return 0;
      default:
	fprintf (stderr, "Try `%s --help' for more information.\n",
		 program_invocation_short_name);
	return 1;
      }

  if (!strcmp ("meta", av[1]) || !strcmp ("bit", av[1])
      || !strcmp ("metabit", av[1]))
    param = 0x03;
  else if (!strcmp ("esc", av[1]) || !strcmp ("prefix", av[1])
	   || !strcmp ("escprefix", av[1]))
    param = 0x04;
  else
    {
      usage ();
      return 1;
    }
  if (ioctl (0, KDSKBMETA, param) < 0)
    {
      error ();
      return 1;
    }
  return 0;
}