unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Alejandro Colomar \(man-pages\) via Libc-alpha" <libc-alpha@sourceware.org>
To: "Pali Rohár" <pali@kernel.org>
Cc: "Marek Behún" <kabel@kernel.org>,
	"G. Branden Robinson" <g.branden.robinson@gmail.com>,
	libc-alpha@sourceware.org,
	"Michael Kerrisk" <mtk.manpages@gmail.com>,
	linux-man@vger.kernel.org
Subject: Re: [PATCH] ioctl_tty.2: Add example how to get or set baudrate on the serial port
Date: Fri, 30 Jul 2021 14:21:38 +0200	[thread overview]
Message-ID: <9f1725cc-e808-9c12-2932-92144ce58a78@gmail.com> (raw)
In-Reply-To: <20210730120542.dv62jtm6lpfmfjyx@pali>

Hi Pali,


On 7/30/21 2:05 PM, Pali Rohár wrote:

> 
>>> +
>>> +int
>>> +main(int argc, char *argv[])
>>> +{
>>> +#if !defined(TCGETS2) || !defined(TCSETS2) || !defined(BOTHER)
>>> +    fprintf(stderr, "TCGETS2, TCSETS2 or BOTHER is unsupported\\n");
>>> +    return 1;
>>> +#else
>>
>> Do we want the program to compile if those are unsupported?
> 
> My intention was to provide example which compiles fine on any Linux
> architecture and in case of error it reports it at runtime.
> 
> On architectures where are TCGETS2/TCSETS2 ioctls unsupported, there are
> still supported TCGETS/TCSETS ioctls. So proper Linux portable program
> should fallback to TCGETS/TCSETS ioctls with Bnnn constants.
> 
> So for example setting baudrate to 115200 would be possible via
> predefined constant B115200 in c_cflag member even when struct termios2
> with c_ospeed is unsupported.
> 
> I just did not put this fallback into this example as it would be quite
> loooong (as it is needed to add big switch for every Bnnn constant and
> convert numeric value into Bnnn) and example show how to use
> TCGETS2/TCSETS2 (not TCGETS/TCSETS).

Okay, I leave it up to you what you consider best to do :)

> 
>> Maybe you can #error there and simplify the reader having to parse the
>> preprocessor directive mentally:
>>
>> #if !defined...
>> # error ...
>> #endif
>>
>> I know it's non-standard, but I think it's common enough so that we can use
>> it here.
> 
> #error is standard. It is already defined in C99 (section 6.10.5 Error
> directive).

Ahh, it is #warning that is non-standard!  Thanks.  I forgot that.

> 
>>> +    struct termios2 tio2;
>>> +    int fd, rc;
>>> +
>>> +    if (argc != 2 && argc != 3) {
>>> +        fprintf(stderr, "Usage: %s device [new_baudrate]\\n", argv[0]);
>>
>> We use \e for printing the escape character.  Not \\
> 
> Ok!
> 
>> CC: Branden
>>
>> See groff_man(7):
>>     Portability
>>         [...]
>>
>>         Similar  caveats  apply  to escapes.  Some escape sequences
>>         are however required for correct typesetting  even  in  man
>>         pages and usually do not cause portability problems:
>>
>>         [...]
>>
>>         \e     Widely used in man pages to  represent  a  backslash
>>                output  glyph.  It works reliably as long as the .ec
>>                request is not used, which should  never  happen  in
>>                man pages, and it is slightly more portable than the
>>                more exact ‘\(rs’  (“reverse  solidus”)  escape  se‐
>>                quence.
>>
>>
>>> +        return 1;
>>> +    }
>>> +
>>> +    fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
>>> +    if (fd < 0) {
>>> +        perror("open");
>>> +        return 1;
>>
>> exit(EXIT_FAILURE);
>>
>>> +    }
>>> +
>>> +    rc = ioctl(fd, TCGETS2, &tio2);
>>> +    if (rc) {
>>> +        perror("TCGETS2");
>>> +        close(fd);
>>> +        return 1;
>>
>> exit(3)
>>
>>> +    }
>>> +
>>> +    printf("%u\\n", tio2.c_ospeed);
>>
>> \e
>>
>>> +
>>> +    if (argc == 3) {
>>> +        tio2.c_cflag &= ~CBAUD;
>>> +        tio2.c_cflag |= BOTHER;
>>> +        tio2.c_ospeed = tio2.c_ispeed = atoi(argv[2]);
>>> +
>>> +        rc = ioctl(fd, TCSETS2, &tio2);
>>> +        if (rc) {
>>> +            perror("TCSETS2");
>>> +            close(fd);
>>> +            return 1;
>>
>> exit(3)
>>
>>> +        }
>>> +    }
>>> +
>>> +    close(fd);
>>> +    return 0;
>>
>> exit(3)
> 
> Interesting... Do you prefer to use exit(EXIT_SUCCESS) instead of return 0?

I don't care in my own code.
I typically use return 0 at the end of main.
But the historical convention in manual pages is using exit(EXIT_SUCCESS),
so let's follow that :)


Tanks,

Alex


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/

  reply	other threads:[~2021-07-30 12:22 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-30  9:53 [PATCH] ioctl_tty.2: Add example how to get or set baudrate on the serial port Pali Rohár via Libc-alpha
2021-07-30 11:47 ` Alejandro Colomar (man-pages) via Libc-alpha
2021-07-30 12:05   ` Pali Rohár via Libc-alpha
2021-07-30 12:21     ` Alejandro Colomar (man-pages) via Libc-alpha [this message]
2021-07-30 13:02 ` [PATCH v2] " Pali Rohár via Libc-alpha
2021-08-01 13:51 ` [PATCH v3] " Pali Rohár via Libc-alpha
2021-08-04 22:08   ` Pali Rohár via Libc-alpha
2021-08-05  5:52     ` Greg Kroah-Hartman via Libc-alpha
2021-08-05  8:22       ` Pali Rohár via Libc-alpha
2021-08-05  8:30         ` Greg Kroah-Hartman via Libc-alpha
2021-08-05  8:44           ` Pali Rohár via Libc-alpha
2021-08-05  8:50             ` Greg Kroah-Hartman via Libc-alpha
2021-08-05  9:51               ` Pali Rohár via Libc-alpha
2021-08-05 15:28                 ` Alejandro Colomar (man-pages) via Libc-alpha
2021-08-05 16:14                   ` Greg Kroah-Hartman via Libc-alpha
2021-08-05 16:45                     ` Licensing example programs in man-pages (was [PATCH v3] ioctl_tty.2: Add example how to get or set baudrate on the serial port) Alejandro Colomar (man-pages) via Libc-alpha
2021-08-05 17:54                       ` Greg Kroah-Hartman via Libc-alpha
2021-08-06  7:22                         ` Alejandro Colomar (man-pages) via Libc-alpha
2021-08-06  8:32                           ` Pali Rohár via Libc-alpha
2021-08-08  8:35   ` [PATCH v3] ioctl_tty.2: Add example how to get or set baudrate on the serial port Alejandro Colomar (man-pages) via Libc-alpha
2021-08-08 21:05     ` Pali Rohár via Libc-alpha
2021-08-08 21:19       ` Alejandro Colomar (man-pages) via Libc-alpha
2021-08-10 19:49 ` [PATCH v4] " Pali Rohár via Libc-alpha
2021-08-10 20:11   ` Pali Rohár via Libc-alpha
2021-08-31 20:34   ` Pali Rohár via Libc-alpha
2021-09-10 13:37     ` Alejandro Colomar (man-pages) via Libc-alpha
2021-09-10 13:39       ` Pali Rohár via Libc-alpha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9f1725cc-e808-9c12-2932-92144ce58a78@gmail.com \
    --to=libc-alpha@sourceware.org \
    --cc=alx.manpages@gmail.com \
    --cc=g.branden.robinson@gmail.com \
    --cc=kabel@kernel.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    --cc=pali@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).