source: rtems-libbsd/freebsd/bin/stty/stty.c @ 567cce1

55-freebsd-126-freebsd-12
Last change on this file since 567cce1 was 567cce1, checked in by Kevin Kirspel <kevin-kirspel@…>, on 05/04/17 at 12:27:57

Updating STTY command for use in RTEMS shell

  • Property mode set to 100644
File size: 5.4 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3#ifdef __rtems__
4#include "rtems-bsd-stty-namespace.h"
5#endif /* __rtems__ */
6/*-
7 * Copyright (c) 1989, 1991, 1993, 1994
8 *      The Regents of the University of California.  All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#if 0
36#ifndef lint
37static char const copyright[] =
38"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
39        The Regents of the University of California.  All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
43static char sccsid[] = "@(#)stty.c      8.3 (Berkeley) 4/2/94";
44#endif /* not lint */
45#endif
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD$");
48
49#ifdef __rtems__
50#define __need_getopt_newlib
51#include <getopt.h>
52#include <machine/rtems-bsd-program.h>
53#include <machine/rtems-bsd-commands.h>
54#endif /* __rtems__ */
55#include <sys/types.h>
56
57#include <ctype.h>
58#include <err.h>
59#include <errno.h>
60#include <fcntl.h>
61#include <limits.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66
67#include "stty.h"
68#include "extern.h"
69#ifdef __rtems__
70#include "rtems-bsd-stty-stty-data.h"
71#endif /* __rtems__ */
72
73#ifdef __rtems__
74static int main(int argc, char *argv[]);
75
76RTEMS_LINKER_RWSET(bsd_prog_stty, char);
77
78int
79rtems_bsd_command_stty(int argc, char *argv[])
80{
81  int exit_code;
82  void *data_begin;
83  size_t data_size;
84
85  data_begin = RTEMS_LINKER_SET_BEGIN(bsd_prog_stty);
86  data_size = RTEMS_LINKER_SET_SIZE(bsd_prog_stty);
87
88  rtems_bsd_program_lock();
89  exit_code = rtems_bsd_program_call_main_with_data_restore("stty",
90      main, argc, argv, data_begin, data_size);
91  rtems_bsd_program_unlock();
92
93  return exit_code;
94}
95#endif /* __rtems__ */
96int
97main(int argc, char *argv[])
98{
99        struct info i;
100        enum FMT fmt;
101        int ch;
102        const char *file, *errstr = NULL;
103#ifdef __rtems__
104        struct getopt_data getopt_data;
105        memset(&getopt_data, 0, sizeof(getopt_data));
106#define optind getopt_data.optind
107#define optarg getopt_data.optarg
108#define opterr getopt_data.opterr
109#define optopt getopt_data.optopt
110#define getopt(argc, argv, opt) getopt_r(argc, argv, "+" opt, &getopt_data)
111#endif /* __rtems__ */
112
113        fmt = NOTSET;
114        i.fd = STDIN_FILENO;
115        file = "stdin";
116
117        opterr = 0;
118        while (optind < argc &&
119#ifndef __rtems__
120            strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
121#else /* __rtems__ */
122            strspn(argv[optind == 0 ? 1 : optind], "-aefg") == strlen(argv[optind == 0 ? 1 : optind]) &&
123#endif /* __rtems__ */
124            (ch = getopt(argc, argv, "aef:g")) != -1)
125                switch(ch) {
126                case 'a':               /* undocumented: POSIX compatibility */
127                        fmt = POSIX;
128                        break;
129                case 'e':
130                        fmt = BSD;
131                        break;
132                case 'f':
133                        if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
134                                err(1, "%s", optarg);
135                        file = optarg;
136                        break;
137                case 'g':
138                        fmt = GFLAG;
139                        break;
140                case '?':
141                default:
142                        goto args;
143                }
144
145args:   argc -= optind;
146        argv += optind;
147
148        if (tcgetattr(i.fd, &i.t) < 0)
149                errx(1, "%s isn't a terminal", file);
150        if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
151                err(1, "TIOCGETD");
152        if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
153                warn("TIOCGWINSZ");
154
155        checkredirect();                        /* conversion aid */
156
157        switch(fmt) {
158        case NOTSET:
159                if (*argv)
160                        break;
161                /* FALLTHROUGH */
162        case BSD:
163        case POSIX:
164                print(&i.t, &i.win, i.ldisc, fmt);
165                break;
166        case GFLAG:
167                gprint(&i.t, &i.win, i.ldisc);
168                break;
169        }
170
171        for (i.set = i.wset = 0; *argv; ++argv) {
172                if (ksearch(&argv, &i))
173                        continue;
174
175                if (csearch(&argv, &i))
176                        continue;
177
178                if (msearch(&argv, &i))
179                        continue;
180
181                if (isdigit(**argv)) {
182                        speed_t speed;
183
184                        speed = strtonum(*argv, 0, UINT_MAX, &errstr);
185                        if (errstr)
186                                err(1, "speed");
187                        cfsetospeed(&i.t, speed);
188                        cfsetispeed(&i.t, speed);
189                        i.set = 1;
190                        continue;
191                }
192
193                if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
194                        gread(&i.t, *argv + sizeof("gfmt1") - 1);
195                        i.set = 1;
196                        continue;
197                }
198
199                warnx("illegal option -- %s", *argv);
200                usage();
201        }
202
203        if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
204                err(1, "tcsetattr");
205        if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
206                warn("TIOCSWINSZ");
207        exit(0);
208}
209
210void
211usage(void)
212{
213
214        (void)fprintf(stderr,
215            "usage: stty [-a | -e | -g] [-f file] [arguments]\n");
216        exit (1);
217}
Note: See TracBrowser for help on using the repository browser.