Skip to content

Conversation

@fanquake
Copy link
Contributor

@fanquake fanquake commented Apr 12, 2022

include <bits/termios-c_lflag.h>  // for ECHO
include <bits/termios-struct.h>   // for termios
include <bits/termios-tcflow.h>   // for TCSANOW

void SetStdinEcho(bool enable)
{
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    if (!enable) {
        tty.c_lflag &= ~ECHO;
    } else {
        tty.c_lflag |= ECHO;
    }
    (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}

@kimgr
Copy link
Contributor

kimgr commented Apr 15, 2022

Unfortunately mappings need to go in two places -- where you made the change, and in include_picker.cc, these look like they belong in the libc_include_map array.

Could you also show a full example (including reduced source code) in the commit message, so git log --grep makes it easier to cross-reference? Thanks!

@fanquake fanquake force-pushed the add_more_termios_mappings branch from d4308df to 14537b6 Compare April 20, 2022 09:01
@fanquake
Copy link
Contributor Author

Thanks. Apologies for the slow follow up. I've added the mappings to libc_include_map, and added a minified example of the source code to the commit message.

@kimgr
Copy link
Contributor

kimgr commented Apr 21, 2022

Thank you! The CI failed due to an upstream Clang change, so I updated that in #1027. Please rebase once that's merged, so we can see that CI runs green before merge here. I'll merge that within half an hour.

```cpp
include <bits/termios-c_lflag.h>  // for ECHO
include <bits/termios-struct.h>   // for termios
include <bits/termios-tcflow.h>   // for TCSANOW

void SetStdinEcho(bool enable)
{
    struct termios tty;
    tcgetattr(STDIN_FILENO, &tty);
    if (!enable) {
        tty.c_lflag &= ~ECHO;
    } else {
        tty.c_lflag |= ECHO;
    }
    (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}
```
@fanquake fanquake force-pushed the add_more_termios_mappings branch from 14537b6 to 2e34f85 Compare April 21, 2022 19:10
@fanquake
Copy link
Contributor Author

Please rebase once that's merged,

No worries. Have rebased on master.

@kimgr kimgr merged commit d3e7eb4 into include-what-you-use:master Apr 21, 2022
@kimgr
Copy link
Contributor

kimgr commented Apr 21, 2022

Very nice, thank you!

@fanquake fanquake deleted the add_more_termios_mappings branch April 21, 2022 19:15
@kimgr kimgr added this to the iwyu 0.19 milestone Apr 21, 2022
@alejandro-colomar
Copy link
Contributor

alejandro-colomar commented Apr 21, 2022

Hi,

I'm not sure about the correctness of this change. termios is a complex thing.

Please check some relevant threads in the linux-man@ mailing list:
https://lore.kernel.org/linux-man/20210730000915.d6ieqiuqah4tjjxf@pali/T/#u
https://lore.kernel.org/linux-man/[email protected]/T/

Maybe @pali can help.

@kimgr
Copy link
Contributor

kimgr commented Apr 21, 2022

@alejandro-colomar Is that discussion for kernel-side <termios.h> or does it apply to userland <termios.h> as well, or are they one and the same?

@alejandro-colomar
Copy link
Contributor

alejandro-colomar commented Apr 21, 2022

@alejandro-colomar Is that discussion for kernel-side <termios.h> or does it apply to userland <termios.h> as well, or are they one and the same?

It affects user space, AFAIR.

@pali
Copy link

pali commented Apr 23, 2022

Hello!

There are two incompatible and different definitions of termios macros. One is in <termios.h> and used by glibc and second is <asm/termbits.h> (included also by <linux/termios.h>) and used by ioctls/syscalls.

So, if you are using libc functions like tcgetattr() or tcsetattr() then you must include <termios.h>. And if you are using ioctl(TCGETS) or ioctl(TIOCMGET) like in ioctl_tty(2) manpage (as mentioned in second linked patch) then you must include <linux/termios.h> or <asm/termbits.h>.

This mess means that you cannot use both tcgetattr() and ioctl(TIOCMGET) in one source file as you cannot include both <termios.h> and <asm/termbits.h> at the same time.

@kimgr
Copy link
Contributor

kimgr commented Apr 23, 2022

@pali Thanks for the additional info!

So it seems to me this mapping from <bits/termios.h> to <termios.h> is safe and sound. It might be prudent to map all the ioctl IDs to <asm/termios.h>, but I suppose that can go in on top of this change if necessary.

@alejandro-colomar
Copy link
Contributor

alejandro-colomar commented Apr 25, 2022

@pali Thanks for the additional info!

So it seems to me this mapping from <bits/termios.h> to <termios.h> is safe and sound.

Yes, this PR seems safe.

It might be prudent to map all the ioctl IDs to <asm/termios.h>, but I suppose that can go in on top of this change if necessary.

Not sure. The IOCTL IDs come from <sys/ioctl.h>, if you mean for example TCGETS or TIOCMGET.

maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Apr 28, 2022
9b0a13a tidy: Add include-what-you-use (fanquake)
74cd038 refactor: fix includes in src/init (fanquake)
c79ad93 refactor: fix includes in src/compat (fanquake)

Pull request description:

  We recently added a [`clang-tidy` job](https://github.com/bitcoin/bitcoin/blob/master/ci/test/00_setup_env_native_tidy.sh) to the CI, which generates a compilation database. We can leverage that now existing database to begin running [include-what-you-use](https://include-what-you-use.org/) over the codebase.

  This PR demonstrates using a mapping_file to indicate fixups / includes that may differ from IWYU suggestions. In this case, I've added some fixups for glibc includes that I've [upstreamed changes for](include-what-you-use/include-what-you-use#1026):
  ```bash
  # Fixups / upstreamed changes
  [
    { include: [ "<bits/termios-c_lflag.h>", private, "<termios.h>", public ] },
    { include: [ "<bits/termios-struct.h>", private, "<termios.h>", public ] },
    { include: [ "<bits/termios-tcflow.h>", private, "<termios.h>", public ] },
  ]
  ```

  The include "fixing" commits of this PR:
  * Adds missing includes.
  * Swaps C headers for their C++ counterparts.
  * Removes the pointless / unmaintainable `//for abc, xyz` comments. When using IWYU, if anyone wants to see / generate those comments, to see why something is included, it is trivial to do so (IWYU outputs them by default). i.e:
  ```cpp
  // The full include-list for compat/stdin.cpp:
  #include <compat/stdin.h>
  #include <poll.h>                  // for poll, pollfd, POLLIN
  #include <termios.h>               // for tcgetattr, tcsetattr
  #include <unistd.h>                // for isatty, STDIN_FILENO
  ```

  TODO:
  - [ ] Qt mapping_file. There is one in the IWYU repo, but it's for Qt 5.11. Needs testing.
  - [ ] Boost mapping_file. There is one in the IWYU repo, but it's for Boost 1.75. Needs testing.

  I'm not suggesting we turn this on the for entire codebase, or immediately go-nuts refactoring all includes. However I think our dependency includes are now slim enough, and our CI infrastructure in place such that we can start doing this in some capacity, and just automate away include fixups / refactorings etc.

ACKs for top commit:
  MarcoFalke:
    review ACK 9b0a13a
  jonatack:
    ACK 9b0a13a reviewed changes and run CI output in https://cirrus-ci.com/task/4750910332076032

Tree-SHA512: 00beab5a5f2a6fc179abf08321a15391ecccaa91ab56f3c50c511e7b29a0d7c95d8bb43eac2c31489711086f6f77319d43d803cf8ea458e7cd234a780d9ae69e
hebasto added a commit to hebasto/bitcoin that referenced this pull request Dec 28, 2022
hebasto added a commit to hebasto/bitcoin that referenced this pull request Dec 28, 2022
hebasto added a commit to hebasto/bitcoin that referenced this pull request May 21, 2023
hebasto added a commit to hebasto/bitcoin that referenced this pull request May 22, 2023
hebasto added a commit to hebasto/bitcoin that referenced this pull request May 29, 2023
hebasto added a commit to hebasto/bitcoin that referenced this pull request May 30, 2023
hebasto added a commit to hebasto/bitcoin that referenced this pull request Jun 13, 2023
hebasto added a commit to hebasto/bitcoin that referenced this pull request Jun 14, 2023
hebasto added a commit to hebasto/bitcoin that referenced this pull request Sep 21, 2023
hebasto added a commit to hebasto/bitcoin that referenced this pull request Jan 5, 2024
fanquake added a commit to bitcoin/bitcoin that referenced this pull request Jan 11, 2024
a395218 ci, iwyu: Drop backported mappings (Hennadii Stepanov)

Pull request description:

  See include-what-you-use/include-what-you-use#1026.

  Split from #27710 as a non-controversial change.

ACKs for top commit:
  fanquake:
    ACK a395218

Tree-SHA512: ae7955a99396ab4f62ab7c989dba59c26448837f0ba4436bf3fddebe4099b5d3c03492e22a8497104c6afcceede1bb1b81f9d71c7c7e43692e6d70dcdfc11e7c
janus pushed a commit to BitgesellOfficial/bitgesell that referenced this pull request Apr 6, 2024
DashCoreAutoGuix pushed a commit to DashCoreAutoGuix/dash that referenced this pull request Jul 30, 2025
a395218 ci, iwyu: Drop backported mappings (Hennadii Stepanov)

Pull request description:

  See include-what-you-use/include-what-you-use#1026.

  Split from bitcoin#27710 as a non-controversial change.

ACKs for top commit:
  fanquake:
    ACK a395218

Tree-SHA512: ae7955a99396ab4f62ab7c989dba59c26448837f0ba4436bf3fddebe4099b5d3c03492e22a8497104c6afcceede1bb1b81f9d71c7c7e43692e6d70dcdfc11e7c
DashCoreAutoGuix pushed a commit to DashCoreAutoGuix/dash that referenced this pull request Aug 7, 2025
a395218 ci, iwyu: Drop backported mappings (Hennadii Stepanov)

Pull request description:

  See include-what-you-use/include-what-you-use#1026.

  Split from bitcoin#27710 as a non-controversial change.

ACKs for top commit:
  fanquake:
    ACK a395218

Tree-SHA512: ae7955a99396ab4f62ab7c989dba59c26448837f0ba4436bf3fddebe4099b5d3c03492e22a8497104c6afcceede1bb1b81f9d71c7c7e43692e6d70dcdfc11e7c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants