Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider libreadline/libedit only in interactive mode #1207

Merged
merged 3 commits into from
Dec 14, 2022

Conversation

dl8dtl
Copy link
Contributor

@dl8dtl dl8dtl commented Dec 1, 2022

There's not much point in running the entire terminal mode session on top of libreadline or libedit, unless it is clear the session is really running interactively on a terminal.

Thus, only use the libreadline/libedit version if stdin points to a terminal. Otherwise, resort to the simple version without readline support. That ensures everything works well when running it within a pipeline.

There's not much point in running the entire terminal mode session
on top of libreadline or libedit, unless it is clear the session
is really running interactively on a terminal.

Thus, only use the libreadline/libedit version if stdin points to
a terminal. Otherwise, resort to the simple version without readline
support. That ensures everything works well when running it within
a pipeline.
@mcuee mcuee added bug Something isn't working enhancement New feature or request labels Dec 1, 2022
@mcuee
Copy link
Collaborator

mcuee commented Dec 2, 2022

This is great.

Tested okay under macOS Ventura 13.0.1 (Apple Mac Mini M1 2020) with an Arduino Uno clone using -c urclock, both the line editing feature and keepalive feature are working.

@mcuee
Copy link
Collaborator

mcuee commented Dec 2, 2022

I also tested under Windows 11 with an Arduino Uno clone using MSYS2 mingw64. There is no regression here. Both line editing and keepalive feature are working with -c urclock.

@MCUdude
Copy link
Collaborator

MCUdude commented Dec 2, 2022

That's a clever fix! Works great on my mac.
@mcuee I guess the build instructions for macOS have to be updated again once this PR has been merged?

$ ./avrdude -cusbasp -patmega16 -t

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9403 (probably m16)
avrdude> read flash
>>> read flash 

Reading | ################################################## | 100% 0.05 s 

0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0050  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0060  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0070  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00b0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00c0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00d0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00e0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00f0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> quit
>>> quit 
avrdude> 
avrdude done.  Thank you.

Hanss-MacBook-Pro-2:src hans$ echo "read flash" | ./avrdude -cusbasp -patmega16 -t

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9403 (probably m16)
avrdude> >>> read flash 

Reading | ################################################## | 100% 0.05 s 

0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0050  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0060  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0070  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00b0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00c0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00d0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00e0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00f0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> 
avrdude done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Dec 2, 2022

@MCUdude

I have done the Wiki updates as I think this PR can be merged. Please take a look if my update is good or not. Thanks.
https://github.com/avrdudes/avrdude/wiki/Building-AVRDUDE-for-macOS

Copy link
Collaborator

@stefanrueger stefanrueger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat workaround to libedit callback functions failing to consider input from pipes/files (in contrast to libreadline which does). Reviewing the changes I am convinced this should work!

@stefanrueger
Copy link
Collaborator

stefanrueger commented Dec 2, 2022

Edge cases could be for pipeline input that is slow (think a process that prepares the input for avrdude does a lot of sleeping) when terminal mode talks to a bootloader that might time out, but I actually don't know whether libreadline would handle that correctly with callback functions. After all, for what I know libreadline might reason exactly as this PR does: if it's not a proper terminal there is no point in a fully-fledged editing service. Looks like Windows has the same philosophy. They have kitted out the standard C library function fgets() with editing functionality, history etc iff it's a terminal.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 2, 2022

I agree this is an edge case, but I think at some point we have to draw a line. If someone is talking to a bootloader that employs timeouts, it's their responsibility to be fast enough to make the bootloader not time out.
Regarding the Wiki: looks fine to me, but why not mention that the default case is as simple as ./build.sh?
(If there's interest, I could also add clean and install targets to the shell script.)

@mcuee
Copy link
Collaborator

mcuee commented Dec 2, 2022

Regarding the Wiki: looks fine to me, but why not mention that the default case is as simple as ./build.sh?
(If there's interest, I could also add clean and install targets to the shell script.)

Updated.
https://github.com/avrdudes/avrdude/wiki/Building-AVRDUDE-for-macOS

Yes it will be good to add the clean and install targets.

Linux and FreeBSD building instruction Wiki page have also been updated to use build.sh.
https://github.com/avrdudes/avrdude/wiki/Building-AVRDUDE-for-Linux
https://github.com/avrdudes/avrdude/wiki/Building-AVRDUDE-for-FreeBSD

@stefanrueger
Copy link
Collaborator

add clean and install targets to the shell script

Always cool. I personally copy the following Makefile into the avrdude directory after cloning and then work with that

$ cat Makefile
all:
	cmake --build build_linux

install:
	cmake --build build_linux; sudo cmake --build build_linux --target install

new:
	sudo rm -rf build_linux
	cmake -D BUILD_DOC=1 -D CMAKE_BUILD_TYPE=RelWithDebInfo -D HAVE_LINUXGPIO=1 -D HAVE_LINUXSPI=1 -D HAVE_PARPORT=1 -B build_linux
	cmake --build build_linux

I guess that's Uni*x specific but keeps me for having to type long cmake lines all the time during development.

@stefanrueger
Copy link
Collaborator

stefanrueger commented Dec 2, 2022

| edge case

libreadline works with pipes and callbacks even when the input is slow and the part is a bootloader that times out:

$ (echo r fl 0x80 0x30; sleep 10; echo r fl) | avrdude -qqt -c urclock
avrdude> r fl 0x80 0x30
0080  3c 3e 78 6a 4c 31 4c 4c  5a 05 18 1a 0c 02 12 0e  |<>xjL1LLZ... ...|
0090  10 04 17 0d 14 07 06 0f  16 1d 0a 08 03 09 11 0b  |... ...... .. . |
00a0  19 1b 1c 4c 4c 4c 4c 4d  11 24 1f be cf ef d8 e0  |...LLLLM.$......|

avrdude> r fl
0000  3f cf 00 00 6c c0 00 00  6a c0 00 00 68 c0 00 00  |?...l...j...h...|
0010  66 c0 00 00 64 c0 00 00  62 c0 00 00 60 c0 00 00  |f...d...b...`...|
0020  5e c0 00 00 5c c0 00 00  5a c0 00 00 58 c0 00 00  |^...\...Z...X...|
0030  56 c0 00 00 f3 c0 00 00  52 c0 00 00 50 c0 00 00  |V.......R...P...|
0040  4e c0 00 00 4c c0 00 00  2c c1 00 00 00 c1 00 00  |N...L...,.......|
0050  46 c0 00 00 44 c0 00 00  42 c0 00 00 40 c0 00 00  |F...D...B...@...|
0060  3e c0 00 00 0c 94 54 00  00 6b 52 4c 89 4c 28 5e  |>... .T..kRL.L(^|
0070  36 6d 19 2a 73 61 55 32  3f 2f 27 23 21 20 30 38  |6m.*saU2?/'#! 08|
0080  3c 3e 78 6a 4c 31 4c 4c  5a 05 18 1a 0c 02 12 0e  |<>xjL1LLZ... ...|
0090  10 04 17 0d 14 07 06 0f  16 1d 0a 08 03 09 11 0b  |... ...... .. . |
00a0  19 1b 1c 4c 4c 4c 4c 4d  11 24 1f be cf ef d8 e0  |...LLLLM.$......|
00b0  de bf cd bf 21 e0 a2 e1  b1 e0 01 c0 1d 92 a6 37  |....!..........7|
00c0  b2 07 e1 f7 11 e0 a0 e0  b1 e0 ec ef f3 e0 02 c0  |................|
00d0  05 90 0d 92 a2 31 b1 07  d9 f7 0e d1 7b c1 90 cf  |.. ..1......{...|
00e0  cf 93 c8 2f 8a 30 19 f4  8d e0 fa df 03 c0 8f 37  |.../.0.........7|
00f0  08 f0 cf e3 80 91 6e 01  8f 5f 8f 73 90 91 6d 01  |......n.._.s..m.|

avrdude> 

I predict above test will

  • Fail on a Mac when compiled with libedit
  • Work on a Mac when compiled with libreadline
  • Fail on Windows even though when compiled with libreadline b/c of these lines

| If someone is talking to a bootloader that employs timeouts, it's their responsibility to be fast enough

Correct, but it is actually AVRDUDE that talks to the bootloader!

| at some point we have to draw a line

Correct. And I am vvv happy with the current solution

  • It works under Linux, FreeBSD, ...
  • It (almost) works under other OS, so they don't need to upgrade to Linux, FreeBSD, ... ;) [edit: mildly unhappy about the regression, see below for a suggested change]

@mcuee
Copy link
Collaborator

mcuee commented Dec 2, 2022

Fail on Windows even though when compiled with libreadline b/c of these lines

Indeed. It works until sleep 1.4 but failed on sleep 1.5 under my Windows 11 system. It is the same with this PR or without this PR, when using GNU Readline.

It has even the same behavior for MSVC build without Readline.

This PR:

 MINGW64 /c/work/avr/avrdude_test/avrdude_pr1207/build_mingw64_nt-10.0-22621/src
$ (echo r fl 0x80 0x30; sleep 10; echo r fl) | ./avrdude -qqt -c urclock -P COM3 -p m328p
avrdude> >>> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> >>> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude> avrdude error: programmer is not responding; try -xstrict and/or vary -xdelay=100

$ (echo r fl 0x80 0x30; sleep 1; echo r fl) | ./avrdude -qqt -c urclock -P COM3 -p m328p
avrdude> >>> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> >>> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude>

git main:

$ (echo r fl 0x80 0x30; sleep 1; echo r fl) | ./avrdude_git -qqt -c urclock -P COM3 -p m328p
avrdude> r fl 0x80 0x30
>>> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> r fl
>>> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude>
avrdude>
xiaof@LAPTOP-913SIMUD MINGW64 /c/work/avr/avrdude_test/avrdude_bin
$ (echo r fl 0x80 0x30; sleep 2; echo r fl) | ./avrdude_git -qqt -c urclock -P COM3 -p m328p
avrdude> r fl 0x80 0x30
>>> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> r fl
>>> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude>
avrdude> avrdude_git error: programmer is not responding; try -xstrict and/or vary -xdelay=100

MSVC build.

$ (echo r fl 0x80 0x30; sleep 1; echo r fl) | ./avrdude_pr1207_msvc -qqt -c urclock -P COM3 -p m328p
avrdude_pr1207_msvc warning: compiled without readline library, cannot use avrdude -t -c urclock interactively
                    but it is still possible to pipe: echo "d fl 0 32; quit" | tr \; \\n | avrdude -t -curclock
avrdude> >>> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> >>> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude>

$ (echo r fl 0x80 0x30; sleep 1.5; echo r fl) | ./avrdude_pr1207_msvc -qqt -c urclock -P COM3 -p m328p
avrdude_pr1207_msvc warning: compiled without readline library, cannot use avrdude -t -c urclock interactively
                    but it is still possible to pipe: echo "d fl 0 32; quit" | tr \; \\n | avrdude -t -curclock
avrdude> >>> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> >>> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude> avrdude_pr1207_msvc error: programmer is not responding; try -xstrict and/or vary -xdelay=100

@mcuee
Copy link
Collaborator

mcuee commented Dec 2, 2022

@stefanrueger and @dl8dtl

This PR fails the test under Linux (GNU Readline), and behavior is the same as Windows MSYS2 (with GNU Readline).

git main works fine.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ (echo r fl 0x80 0x30; sleep 1.5; echo r fl) | ./avrdude_pr1207
 -qqt -c urclock -P /dev/ttyACM0 -p m328p
avrdude> 0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> 0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude> avrdude_pr1207 error: programmer is not responding; try -xstrict and/or vary -xdelay=100

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ (echo r fl 0x80 0x30; sleep 1.5; echo r fl) | ./avrdude_git -qqt
 -c urclock -P /dev/ttyACM0 -p m328p
avrdude> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude> 

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ ./avrdude_git -qq -c urclock -P /dev/ttyACM0 -p m328p -xshowall
0 2022-06-10 21.21 Blink.ino.standard.hex 924 store 31298 meta 34 boot 512 
o8.3 --s-h-r-- vector 0 (RESET) ATmega328P

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

I predict above test will

Fail on a Mac when compiled with libedit
Work on a Mac when compiled with libreadline
Fail on Windows even though when compiled with libreadline b/c of these lines

@stefanrueger

What you say is correct about git main (piping is not working under macOS with libedit).

With this PR, they all behave the same in this aspect -- all will fail the tests. So this PR brings equality between libedit and GNU Readline under macOS, and brings eaulity to Windows GNU Readline build as well.

For the paticular tests, even Windows MSVC build without GNU Readline behaves the same.

Correct. And I am vvv happy with the current solution

It works under Linux, FreeBSD, ...
It (almost) works under other OS, so they don't need to upgrade to Linux, FreeBSD, ... ;)

So I guess you are still fine with this PR, right? Or do you want to treat Linux/FreeBSD a bit differently and unconditionally use GNU Readline? I am okay either way.

BTW, I am OS neutral. :)

I like cross-platform projects and my engagements with the open source world are almost all cross-platform projects.

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

I predict above test will

Fail on a Mac when compiled with libedit
Work on a Mac when compiled with libreadline

@stefanrueger

This PR brings equality of libedit and GNU Readline under macOS, both behave the same and both will fail the tests.

mcuee@mcuees-Mac-mini avrdude_bin % (echo r fl 0x80 0x30; sleep 2; echo r fl) | ./avrdude_pr1207_libedit
 -qqt -c urclock -P /dev/cu.usbmodem2213701 -p m328p
avrdude> >>> r fl 0x80 0x30 
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> >>> r fl 
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude> avrdude_pr1207_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100
mcuee@mcuees-Mac-mini avrdude_bin % (echo r fl 0x80 0x30; sleep 2; echo r fl) | ./avrdude_pr1207_readline
 -qqt -c urclock -P /dev/cu.usbmodem2213701 -p m328p
avrdude> >>> r fl 0x80 0x30 
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> >>> r fl 
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude> avrdude_pr1207_readline error: programmer is not responding; try -xstrict and/or vary -xdelay=100

Without this PR, you are right.

mcuee@mcuees-Mac-mini avrdude_bin % (echo r fl 0x80 0x30; sleep 10; echo r fl) | ./avrdude_git_readline
 -qqt -c urclock -P /dev/cu.usbmodem2213701 -p m328p
avrdude> r fl 0x80 0x30
>>> r fl 0x80 0x30 
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> r fl
>>> r fl 
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude> 
avrdude> %  

@stefanrueger
Copy link
Collaborator

Ah, of course! This test works without the PR on Linux but is bound to fail under Linux with this PR. My bad. So, there is a regression here. The PR is actually only needed for Mac when compiled with libedit.

So, I suggest to replace terminal_mode with something to the effect of (just typed, not compiled or tested)

int terminal_mode(PROGRAMMER *pgm, AVRPART *p) {
  return
#if defined(HAVE_LIBREADLINE) && defined(__APPLE__) && !defined(COMPILED_WITH_GNU_READLINE)
    (isatty(fileno(stdin))? terminal_mode_interactive: terminal_mode_noninteractive)
#elif defined(HAVE_LIBREADLINE)
    terminal_mode_interactive
#else
    terminal_mode_noninteractive
#endif
      (pgm, p);
}

Then

  • Linux, FreeBSD etc continue to work in this edge case (native GNU libreadline is just very good)
  • When you compile on a Mac with GNU readline and set -DCOMPILED_WITH_GNU_READLINE it works there, too
  • If someone figures out how to improve these lines then the edge case also works with Windows

@stefanrueger
Copy link
Collaborator

| equality between libedit and GNU Readline under macOS

What we want is justice though :) https://www.pnwu.edu/files/2021/05/EEJ.jpeg

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

| equality between libedit and GNU Readline under macOS

What we want is justice though :) https://www.pnwu.edu/files/2021/05/EEJ.jpeg

Haha, nice picture.

I am okay with your proposal and indeed there is a regression here under Linux.

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

I tried to use the same Linux code path for MinGW but then terminal mode does not work at all with GNU Readline.

PS C:\work\avr\avrdude_test\avrdude_main> git diff
diff --git a/src/term.c b/src/term.c
index c399c3e..13053fc 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1288,7 +1288,7 @@ static int term_running;

 // Any character in standard input available (without sleeping)?
 static int readytoread() {
-#ifdef WIN32
+#ifdef _MSC_VER
   HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);

   while(1) {

On the other hand, cygwin uses the same code path as Linux (without any changes) and it works fine out of box.
Note /dev/ttyS2 is Windows COM3.

/cygdrive/c/work/avr/avrdude_test/avrdude_main/build_cygwin/src
$  (echo r fl 0x80 0x30; sleep 10; echo r fl) | ./avrdude -qqt -c urclock -P /dev/ttyS2 -p m328p
avrdude> r fl 0x80 0x30
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|

avrdude> r fl
0000  0c 94 5c 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .\. .n. .n. .n.|
0010  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0020  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0030  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0040  0c 94 13 01 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | ... .n. .n. .n.|
0050  0c 94 6e 00 0c 94 6e 00  0c 94 6e 00 0c 94 6e 00  | .n. .n. .n. .n.|
0060  0c 94 6e 00 0c 94 6e 00  00 00 00 00 24 00 27 00  | .n. .n.....$.'.|
0070  2a 00 00 00 00 00 25 00  28 00 2b 00 04 04 04 04  |*.....%.(.+.....|
0080  04 04 04 04 02 02 02 02  02 02 03 03 03 03 03 03  |................|
0090  01 02 04 08 10 20 40 80  01 02 04 08 10 20 01 02  |..... @...... ..|
00a0  04 08 10 20 00 00 00 08  00 02 01 00 00 03 04 07  |... ............|
00b0  00 00 00 00 00 00 00 00  11 24 1f be cf ef d8 e0  |.........$......|
00c0  de bf cd bf 21 e0 a0 e0  b1 e0 01 c0 1d 92 a9 30  |....!..........0|
00d0  b2 07 e1 f7 0e 94 5d 01  0c 94 cc 01 0c 94 00 00  |......]. ... ...|
00e0  e1 eb f0 e0 24 91 ed e9  f0 e0 94 91 e9 e8 f0 e0  |....$...........|
00f0  e4 91 ee 23 c9 f0 22 23  39 f0 23 30 01 f1 a8 f4  |...#.."#9.#0....|

avrdude>
avrdude>

$ ldd avrdude.exe
        ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffda8590000)
        KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffda6cd0000)
        KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffda5b30000)
        cygwin1.dll => /usr/bin/cygwin1.dll (0x180040000)
        cygelf-0.dll => /usr/bin/cygelf-0.dll (0x3ff820000)
        cygftdi1-2.dll => /usr/bin/cygftdi1-2.dll (0x3ff730000)
        cyghidapi-0.dll => /usr/bin/cyghidapi-0.dll (0x3ff4a0000)
        SETUPAPI.dll => /cygdrive/c/WINDOWS/System32/SETUPAPI.dll (0x7ffda7740000)
        cygreadline7.dll => /usr/bin/cygreadline7.dll (0x3feaa0000)
        msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffda6b20000)
        cygusb-1.0.dll => /usr/bin/cygusb-1.0.dll (0x3fe4f0000)
        cygusb0.dll => /usr/bin/cygusb0.dll (0x3fe4d0000)
        ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ffda74d0000)
        sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ffda65e0000)
        cygintl-8.dll => /usr/bin/cygintl-8.dll (0x3ff2a0000)
        cygncursesw-10.dll => /usr/bin/cygncursesw-10.dll (0x3feda0000)
        RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ffda6390000)
        GDI32.dll => /cygdrive/c/WINDOWS/System32/GDI32.dll (0x7ffda6f30000)
        CFGMGR32.dll => /cygdrive/c/WINDOWS/SYSTEM32/CFGMGR32.dll (0x7ffda56b0000)
        cygiconv-2.dll => /usr/bin/cygiconv-2.dll (0x3ff360000)
        win32u.dll => /cygdrive/c/WINDOWS/System32/win32u.dll (0x7ffda62f0000)
        ucrtbase.dll => /cygdrive/c/WINDOWS/System32/ucrtbase.dll (0x7ffda6130000)
        gdi32full.dll => /cygdrive/c/WINDOWS/System32/gdi32full.dll (0x7ffda5ed0000)
        msvcp_win.dll => /cygdrive/c/WINDOWS/System32/msvcp_win.dll (0x7ffda6250000)
        USER32.dll => /cygdrive/c/WINDOWS/System32/USER32.dll (0x7ffda66f0000)

I have updated Wiki for Cygwin to add GNU Readline dependancy.
https://github.com/avrdudes/avrdude/wiki/Building-AVRDUDE-for-Windows-using-Cygwin

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 3, 2022

The PR is actually only needed for Mac when compiled with libedit.

Keep in mind that libedit originates from NetBSD. We just don't test this system very much.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 3, 2022

So since this is not an OS issue per se (someone could install GNU libreadline on MacOS as well as on NetBSD), I'd prefer to not drag in any OS dependency, but depend it on a proper detection whether the readline facility is GNU readline or not.

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

The PR is actually only needed for Mac when compiled with libedit.

Keep in mind that libedit originates from NetBSD. We just don't test this system very much.

I have not figured how to build avrdude under NetBSD yet. I can test avrdude under NetBSD once I figure it out. As of now the binary is not pointing to the right library. I am not familiar with pkgsrc myself.

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

So since this is not an OS issue per se (someone could install GNU libreadline on MacOS as well as on NetBSD), I'd prefer to not drag in any OS dependency, but depend it on a proper detection whether the readline facility is GNU readline or not.

That will be the best. Unfortunately we need some CMake magic in order to do that. Hopefully @mariusgreuel can figure that out when he got the time.

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

Keep in mind that libedit originates from NetBSD. We just don't test this system very much.

I have not figured how to build avrdude under NetBSD yet. I can test avrdude under NetBSD once I figure it out. As of now the binary is not pointing to the right library. I am not familiar with pkgsrc myself.

@dl8dtl

I have figured out how to build avrdude under NetBSD properly. We just need to replace -L with -R for the linker option.

But I have not figured how to get my Arduino Uno USB to Serial port to appear under NetBSD VM yet.

Ref:
ocaml/opam#3482 (comment)

Screenshot 2022-12-03 183034

@mcuee
Copy link
Collaborator

mcuee commented Dec 3, 2022

@dl8dtl

In any case, official avrdude 6.4 under NetBSD uses GNU Readline. So indeed @stefanrueger is correct that only macOS is using libedit by default for the purpose of avrdude.

https://github.com/NetBSD/pkgsrc/blob/trunk/cross/avrdude/Makefile
https://github.com/NetBSD/pkgsrc/blob/trunk/devel/readline/DESCR

Screenshot 2022-12-03 191724

@stefanrueger
Copy link
Collaborator

| since this is not an OS issue per se

The issue is that our make-cmake-build-configure-amake-boostrap-idontknowwhat detects MacOS's libedit incorrectly as HAVE_LIBREADLINE. We have not found a proper solution for that (well, I know I don't know, so I haven't even looked). I suspect that needs someone who can debug our c/make file. Hence, the workaround to consult the OS here.

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 3, 2022

Well, since libedit offers a libreadline compat layer, it's not quite surprising that the readline test also triggers.

I'm not sure what the respective tests could perform at configure time, but one possible distinction is the variable rl_library_version. In libedit, it points to the string "EditLine wrapper", whereas in GNU libreadline, it points to a string like "8.1".

@mcuee
Copy link
Collaborator

mcuee commented Dec 6, 2022

From the following test results, it seems this PR is actually good for the Readline-win32 which can be used with MSVC.

PS C:\work\avr\avrdude_test\avrdude_bin> echo "dump flash 0 0x20" | .\avrdude_git_msvc_readline
 -c urclock -P COM1 -p m328p -xbootsize=512 -qqt
avrdude> dump flash 0 0x20
>>> dump flash 0 0x20
0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude>
avrdude>(need to hit CTRL-C to get out)

PS C:\work\avr\avrdude_test\avrdude_bin> echo "dump flash 0 0x20" | .\avrdude_pr1207_msvc_readline
 -c urclock -P COM1 -p m328p -xbootsize=512 -qqt
avrdude> >>> dump flash 0 0x20
0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> (good, no need to hit CTRL-C).

$ (echo r fl 0x80 0x30; sleep 2; echo r fl) | ./avrdude_pr1207_msvc_readline -c urclock
 -P COM1 -p m328p -xbootsize=512 -qqt
avrdude> >>> r fl 0x80 0x30
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> >>> r fl
0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0050  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0060  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0070  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00b0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00c0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00d0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00e0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00f0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> avrdude_pr1207_msvc_readline error: programmer is not responding; try -xstrict and/or vary -xdelay=100

xiaof@LAPTOP-913SIMUD MINGW32 /c/work/avr/avrdude_test/avrdude_bin
$ (echo r fl 0x80 0x30; sleep 2; echo r fl) | ./avrdude_git_msvc_readline -c urclock
 -P COM1 -p m328p -xbootsize=512 -qqt
avrdude> r fl 0x80 0x30
>>> r fl 0x80 0x30
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> r fl
>>> r fl
0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0050  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0060  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0070  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00b0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00c0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00d0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00e0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00f0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> (hanging here, need to hit CTRL-C to be able to quit).

$ (echo r fl 0x80 0x30; sleep 2; echo r fl; echo quit) | ./avrdude_git_msvc_readline
 -c urclock -P COM1 -p m328p -xbootsize=512 -qqt
avrdude> r fl 0x80 0x30
>>> r fl 0x80 0x30
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> r fl
>>> r fl
0000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0010  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0020  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0030  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0040  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0050  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0060  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0070  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0080  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
0090  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00a0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00b0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00c0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00d0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00e0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
00f0  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

avrdude> quit
>>> quit
avrdude> avrdude_git_msvc_readline error: programmer is not responding; try -xstrict and/or vary -xdelay=100

The echo quit command is a workaround for git main with MSVC Readline-win32.

@mcuee
Copy link
Collaborator

mcuee commented Dec 7, 2022

BTW, I found a port libedit for Windows called WinEditline. Unfortunately it does not support the rl_callback group of functions needed by term.c.
https://mingweditline.sourceforge.net/?Description

@mcuee
Copy link
Collaborator

mcuee commented Dec 7, 2022

Latest version of libedit (20221030) seems to perform better under Linux than the older version in Ubuntu 20.04.
Download: https://www.thrysoee.dk/editline/libedit-20221030-3.1.tar.gz

git main -- history works, pipe does not work, keep-alive works.
pr1207 -- history works, pipe works, keep-alive works. Of course long speed will fail.

Ubuntu 20.04 old version (20191231).

git main -- history does not work, pipe does not work, keep-alive works.
pr1207 -- history does not work, pipe works, keep-alive works.

@mcuee
Copy link
Collaborator

mcuee commented Dec 8, 2022

@stefanrueger

Can I suggest the PR be changed so that teminal_mode(pgm, p) returns terminal_mode_interactive(pgm, p) whenever AVRDUDE was compiled with GNU readline. This is a mature library (and native to many Un*x systems), where pipe input, file input, callbacks, keep-alive all work - the latter even with input from a slow pipe.

I tried your patch under Linux with GNU Readline and it works fine. (As mentioned before, it does not work with libedit under Linux but we would not really cate for libedit under Linux).

BTW, I feel there is a missing return for -qqt.

mcuee@UbuntuSwift3:~/build/avr/avrdude_jw$ git diff
diff --git a/src/term.c b/src/term.c
index 573141a..3bafd85 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1398,12 +1398,16 @@ int terminal_mode_noninteractive(PROGRAMMER *pgm, struct avrpart *p) {
   return pgm->flush_cache(pgm, p);
 }
 
-int terminal_mode(PROGRAMMER *pgm, struct avrpart *p) {
-#if defined(HAVE_LIBREADLINE)
-  if (isatty(fileno(stdin)))
-    return terminal_mode_interactive(pgm, p);
+int terminal_mode(PROGRAMMER *pgm, AVRPART *p) {
+  return
+#if defined(HAVE_LIBREADLINE) && defined(__APPLE__) && !defined(COMPILED_WITH_GNU_READLINE)
+    (isatty(fileno(stdin))? terminal_mode_interactive: terminal_mode_noninteractive)
+#elif defined(HAVE_LIBREADLINE)
+    terminal_mode_interactive
+#else
+    terminal_mode_noninteractive
 #endif
-  return terminal_mode_noninteractive(pgm, p);
+      (pgm, p);
 }
 
 static void update_progress_tty(int percent, double etime, const char *hdr, int finish) {

mcuee@UbuntuSwift3:~/build/avr/avrdude_jw/build_linux/src$ echo "dump flash 0x7e00 0x10" | 
./avrdude -c urclock -P /dev/ttyACM0 -p m328p -qqt
avrdude> dump flash 0x7e00 0x10
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|

avrdude> 
avrdude> mcuee@UbuntuSwift3:~/build/avr/avrdude_jw/build_linux/src$ (I hear a return to feel better)
mcuee@UbuntuSwift3:~/build/avr/avrdude_jw/build_linux/src$ 
mcuee@UbuntuSwift3:~/build/avr/avrdude_jw/build_linux/src$ (echo r fl 0x7e00 0x30; sleep 10; 
echo r fl 0x7f00 0x30) | ./avrdude -qqt -c urclock -P /dev/ttyACM0 -p m328p
avrdude> r fl 0x7e00 0x30
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> r fl 0x7f00 0x30
7f00  a6 01 a0 e0 b1 e0 2c 91  30 e0 11 96 8c 91 11 97  |......,.0.......|
7f10  90 e0 98 2f 88 27 82 2b  93 2b 12 96 fa 01 0c 01  |.../.'.+.+.... .|
7f20  87 be e8 95 11 24 4e 5f  5f 4f f1 e0 a0 38 bf 07  |.....$N__O...8..|

avrdude> 
avrdude> mcuee@UbuntuSwift3:~/build/avr/avrdude_jw/build_linux/src$  (I hit a return to feel better)
mcuee@UbuntuSwift3:~/build/avr/avrdude_jw/build_linux/src$ 

@mcuee
Copy link
Collaborator

mcuee commented Dec 8, 2022

I actully figured out a rather hacky way to use CMake to differentiate libedit vs GNU Readline, as macOS detects the symbolic link libreadline.tbd as GNU readline. But then I need to add another one to detect libedit and we need to change term.c as well.

So I think @dl8dtl's idea to detect at runtime is much better.

mcuee@mcuees-Mac-mini avrdude_jw % git diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1269650..57521b6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -132,7 +132,7 @@ else()
     set(PREFERRED_LIBHIDAPI hidapi hidapi-libusb hidapi-hidraw)
     set(PREFERRED_LIBFTDI ftdi)
     set(PREFERRED_LIBFTDI1 ftdi1)
-    set(PREFERRED_LIBREADLINE readline)
+    set(PREFERRED_LIBREADLINE libreadline.so libreadline.dylib libreadline.dll.a readline.lib)
 endif()

@mcuee
Copy link
Collaborator

mcuee commented Dec 8, 2022

@stefanrueger
I also tried your patch under macOS, it seems to be okay with the default libedit build of this PR #1207.

mcuee@mcuees-Mac-mini avrdude_jw % git diff
diff --git a/src/term.c b/src/term.c
index 573141a..3bafd85 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1398,12 +1398,16 @@ int terminal_mode_noninteractive(PROGRAMMER *pgm, struct avrpart *p) {
   return pgm->flush_cache(pgm, p);
 }
 
-int terminal_mode(PROGRAMMER *pgm, struct avrpart *p) {
-#if defined(HAVE_LIBREADLINE)
-  if (isatty(fileno(stdin)))
-    return terminal_mode_interactive(pgm, p);
+int terminal_mode(PROGRAMMER *pgm, AVRPART *p) {
+  return
+#if defined(HAVE_LIBREADLINE) && defined(__APPLE__) && !defined(COMPILED_WITH_GNU_READLINE)
+    (isatty(fileno(stdin))? terminal_mode_interactive: terminal_mode_noninteractive)
+#elif defined(HAVE_LIBREADLINE)
+    terminal_mode_interactive
+#else
+    terminal_mode_noninteractive
 #endif
-  return terminal_mode_noninteractive(pgm, p);
+      (pgm, p);
 }

mcuee@mcuees-Mac-mini src % otool -L avrdude
avrdude:
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1319.0.0)
	/opt/homebrew/opt/libusb-compat/lib/libusb-0.1.4.dylib (compatibility version 9.0.0, current version 9.4.0)
	/opt/homebrew/opt/libusb/lib/libusb-1.0.0.dylib (compatibility version 4.0.0, current version 4.0.0)
	/opt/homebrew/opt/libhid/lib/libhid.0.dylib (compatibility version 1.0.0, current version 1.0.0)
	/opt/homebrew/opt/hidapi/lib/libhidapi.0.dylib (compatibility version 0.0.0, current version 0.12.0)
	/opt/homebrew/opt/libftdi0/lib/libftdi.1.dylib (compatibility version 22.0.0, current version 22.0.0)
	/opt/homebrew/opt/libftdi/lib/libftdi1.2.dylib (compatibility version 2.0.0, current version 2.5.0)
	/usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)

mcuee@mcuees-Mac-mini src % (echo r fl 0x7e00 0x30; sleep 2; echo r fl 0x7f00 0x30) | ./avrdude -qqt
 -c urclock -P /dev/cu.usbmodem2213701 -p m328p
avrdude> >>> r fl 0x7e00 0x30 
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> >>> r fl 0x7f00 0x30 
avrdude error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude error: unable to read flash page at addr 0x7f00
avrdude error: (dump) error reading flash address 0x07f00 of part ATmega328P
               read operation not supported on memory type flash
avrdude> avrdude error: programmer is not responding; try -xstrict and/or vary -xdelay=100

mcuee@mcuees-Mac-mini src % ./avrdude -qqt -c urclock -P /dev/cu.usbmodem2213701 -p m328p 
avrdude> dump flash 0x7e00 0x30
>>> dump flash 0x7e00 0x30 
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> quit
>>> quit 
avrdude> %   (noticed this extra % prompt, but at least I do not need to hit return).
mcuee@mcuees-Mac-mini src % 

@mcuee
Copy link
Collaborator

mcuee commented Dec 8, 2022

@stefanrueger

For you patch to work under macOS with GNU Readline, apparently we need to change CMake scripts.


mcuee@mcuees-Mac-mini avrdude_jw % cmake -D CMAKE_C_FLAGS=-I/opt/homebrew/include
 -D CMAKE_EXE_LINKER_FLAGS=-L/opt/homebrew/Cellar 
-D HAVE_LIBREADLINE:FILEPATH=/opt/homebrew/opt/readline/lib/libreadline.dylib
 -D COMPILED_WITH_GNU_READLINE  -D CMAKE_BUILD_TYPE=RelWithDebInfo 
-B build_darwin_readline

CMake Error: Parse error in command line argument: COMPILED_WITH_GNU_READLINE
 Should be: VAR:type=value

CMake Error: Run 'cmake --help' for all supported options.

@mcuee
Copy link
Collaborator

mcuee commented Dec 9, 2022

@dl8dtl
FYI -- libedit under NetBSD performs the same as macOS libedit.

git main (default with GNU Readline) -- everything works fine
git main (libedit) -- pipes do not work, history works, keep-alive works.
pr1207 (default with GNU Readline) -- everything works fine except long sleep
pr1207 (libedit) -- everything works fine except long sleep.

Screenshot 2022-12-09 122800

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 9, 2022

FYI -- libedit under NetBSD performs the same as macOS libedit.

Doesn't surprise me – I've been testing their library under my FreeBSD.

@mcuee
Copy link
Collaborator

mcuee commented Dec 9, 2022

FYI -- libedit under NetBSD performs the same as macOS libedit.
git main (libedit) -- pipes do not work, history works, keep-alive works.
pr1207 (libedit) -- everything works fine except long sleep.

Doesn't surprise me – I've been testing their library under my FreeBSD.

Great. So we can say the BSDs are similar in this aspect as macOS if using libedit.

Even libedit latest version performs similar under Ununtu 20.04 Linux.
git main (libedit)-- history works, pipe does not work, keep-alive works.
pr1207 (libedit) -- everything works fine except long sleep.

Only the old libedit versio from Ubutnu 20.04 is a bit strange.
git main(libedit) -- history does not work, pipe does not work, keep-alive works
pr1207 (libedit) -- history does not work, pipe works, keep-alive works
pr1207 with pr1185 reverted (libedit) -- everything works fine except long sleep.

@mcuee
Copy link
Collaborator

mcuee commented Dec 9, 2022

Same for OpenBSD.
git main (default with GNU readline) -- everything works fine
git main (libedit)-- history works, pipe does not work, keep-alive works.
pr1207 (GNU Readline) -- everything works fine except long sleep.
pr1207 (libedit) -- everything works fine except long sleep.

Screenshot 2022-12-09 180616

GNU libreadline does not seem to have any issues when running on a
pipe as input, thus use it all the time.

EditLine (NetBSD, MacOS) has issues with that, so only use it when
running interactively on a terminal.
@stefanrueger
Copy link
Collaborator

I'm just going to test for "Editline", that's enough.

@dl8dtl Have you pushed a runtime check in this PR? Doesn't look like it.

Let me suggest replacing if(isatty(0)) with if(!strstr(rl_library_version, "Editline") || isatty(0)):

diff --git a/src/term.c b/src/term.c
index 573141a..67d2d00 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1400,7 +1400,7 @@ int terminal_mode_noninteractive(PROGRAMMER *pgm, struct avrpart *p) {
 
 int terminal_mode(PROGRAMMER *pgm, struct avrpart *p) {
 #if defined(HAVE_LIBREADLINE)
-  if (isatty(fileno(stdin)))
+  if (!strstr(rl_library_version, "Editline") || isatty(fileno(stdin)))
     return terminal_mode_interactive(pgm, p);
 #endif
   return terminal_mode_noninteractive(pgm, p);

This runs the callback readline input (what you call interactive) when it's a real terminal or when AVRDUDE was seemingly compiled using GNU libreadline as opposed to the (buggy) libedit. Just considering isatty(0) fails for slow pipe input:

$ (echo d fl 0 16; sleep 3; echo d fl 1024 16) | avrdude -qqt
avrdude> 0000  3f cf 00 00 c3 c1 00 00  90 c0 00 00 8e c0 00 00  |?...............|

avrdude> avrdude error: protocol expects sync byte 0xa0 but got 0x18 in urclock_paged_load()
avrdude error: protocol expects sync byte 0xa0 but got 0xe6 in ur_readEF()
avrdude error: unable to read flash page at addr 0x0400
avrdude error: (dump) error reading flash address 0x00400 of part ATmega328P
               read operation not supported on memory type flash
avrdude> avrdude error: protocol expects sync byte 0xa0 but got 0x9e

Using !strstr(rl_library_version, "Editline") || isatty(0) lets libreadline compiled AVRDUDEs keep the terminal alive even with slow pipe input:

$ (echo d fl 0 16; sleep 3; echo d fl 1024 16) | avrdude -qqt
avrdude> d fl 0 16
0000  3f cf 00 00 c3 c1 00 00  90 c0 00 00 8e c0 00 00  |?...............|

avrdude> d fl 1024 16
0400  02 c0 88 23 d9 f0 8c 2f  90 e0 20 91 23 01 2e bd  |...#.../.. .#...|

avrdude> 

So I prefer that additional runtime test. In a slight complication that additional test might not improve things under Windows compiled with libreadline (as these lines still warrant fixing, but that might happen at some point). So all things considered, I would change this PR as outlined above.

Do you want to check and change, @dl8dtl, or should I push the change onto your PR?

@stefanrueger
Copy link
Collaborator

@mcuee thanks for brilliant testing here and sharing your insights as to the matrix of OS/library variants

@stefanrueger
Copy link
Collaborator

@dl8dtl Should that not be the other way round? strstr(...) == NULL instead of strstr(...) != 0

When it's GNU libreadline, strstr() will not find the string, and return NULL, in which case to run term_interactive (even when not a terminal).

@stefanrueger
Copy link
Collaborator

stefanrueger commented Dec 11, 2022

... also i just checked the spellings mentioned in this conversation; three are mentioned: EditLine wrapper, rl Editline wrapper and Editline wrapper. So perhaps best to check for non-existence of "wrapper" as oppsed to "EditLine wrapper" ... or simply "rapper" just in case someone gifts wrapper a capital start

@mcuee
Copy link
Collaborator

mcuee commented Dec 11, 2022

@stefanrueger

Indeed you are right, the latest version does not work under Linux with GNU Readline.

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ (echo r fl 0x7e00 0x30; sleep 3; echo r fl 0x7f00 0x30) |
 ./avrdude_pr1207v1 -qqt -c urclock -P /dev/ttyACM0 -p m328p
avrdude> 7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> avrdude_pr1207v1 error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1 error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1 error: unable to read flash page at addr 0x7f00
avrdude_pr1207v1 error: (dump) error reading flash address 0x07f00 of part ATmega328P
                        read operation not supported on memory type flash
avrdude> avrdude_pr1207v1 error: programmer is not responding; try -xstrict and/or vary -xdelay=100
mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ 

And your modification works fine. It can cope with long sleep when building with GNU Readline. And timeout will happen when building with libedit.

I will test macOS later.

mcuee@UbuntuSwift3:~/build/avr/avrdude_jw$ git diff
diff --git a/src/term.c b/src/term.c
index ab457ed..028394d 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1403,7 +1403,7 @@ int terminal_mode(PROGRAMMER *pgm, struct avrpart *p) {
   // GNU libreadline can also work if input is a pipe.
   // EditLine (NetBSD, MacOS) has issues with that, so only use it when
   // running interactively.
-  if (isatty(fileno(stdin)) || (strstr(rl_library_version, "EditLine wrapper") != 0))
+  if (isatty(fileno(stdin)) || !(strstr(rl_library_version, "wrapper")))
     return terminal_mode_interactive(pgm, p);
 #endif
   return terminal_mode_noninteractive(pgm, p);

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ (echo r fl 0x7e00 0x30; sleep 3; echo r fl 0x7f00 0x30) | 
./avrdude_pr1207v1_sr -qqt -c urclock -P /dev/ttyACM0 -p m328p
avrdude> r fl 0x7e00 0x30
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> r fl 0x7f00 0x30
7f00  a6 01 a0 e0 b1 e0 2c 91  30 e0 11 96 8c 91 11 97  |......,.0.......|
7f10  90 e0 98 2f 88 27 82 2b  93 2b 12 96 fa 01 0c 01  |.../.'.+.+.... .|
7f20  87 be e8 95 11 24 4e 5f  5f 4f f1 e0 a0 38 bf 07  |.....$N__O...8..|

avrdude> 
avrdude> mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ 

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ (echo r fl 0x7e00 0x30; sleep 3; echo r fl 0x7f00 0x30) | 
./avrdude_pr1207v1_sr_libedit -qqt -c urclock -P /dev/ttyACM0 -p m328p
avrdude> 7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> avrdude_pr1207v1_sr_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1_sr_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1_sr_libedit error: unable to read flash page at addr 0x7f00
avrdude_pr1207v1_sr_libedit error: (dump) error reading flash address 0x07f00 of part ATmega328P
                                   read operation not supported on memory type flash
avrdude> avrdude_pr1207v1_sr_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100

@mcuee
Copy link
Collaborator

mcuee commented Dec 11, 2022

The modification is also okay for macOS.

BTW, I have also tested with Windows, it will still time out as mentioned by @stefanrueger, but at least no regression.

mcuee@mcuees-Mac-mini avrdude_jw % git diff
diff --git a/src/term.c b/src/term.c
index ab457ed..028394d 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1403,7 +1403,7 @@ int terminal_mode(PROGRAMMER *pgm, struct avrpart *p) {
   // GNU libreadline can also work if input is a pipe.
   // EditLine (NetBSD, MacOS) has issues with that, so only use it when
   // running interactively.
-  if (isatty(fileno(stdin)) || (strstr(rl_library_version, "EditLine wrapper") != 0))
+  if (isatty(fileno(stdin)) || !(strstr(rl_library_version, "wrapper")))
     return terminal_mode_interactive(pgm, p);
 #endif
   return terminal_mode_noninteractive(pgm, p);

mcuee@mcuees-Mac-mini avrdude_bin % (echo r fl 0x7e00 0x30; sleep 3; echo r fl 0x7f00 0x30) | 
./avrdude_pr1207v1_libedit -qqt -c urclock -P /dev/cu.usbmodem2213701 -p m328p 
avrdude> >>> r fl 0x7e00 0x30 
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> >>> r fl 0x7f00 0x30 
avrdude_pr1207v1_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1_libedit error: unable to read flash page at addr 0x7f00
avrdude_pr1207v1_libedit error: (dump) error reading flash address 0x07f00 of part ATmega328P
                                read operation not supported on memory type flash
avrdude> avrdude_pr1207v1_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100

mcuee@mcuees-Mac-mini avrdude_bin % (echo r fl 0x7e00 0x30; sleep 3; echo r fl 0x7f00 0x30) | 
./avrdude_pr1207v1_readline -qqt -c urclock -P /dev/cu.usbmodem2213701 -p m328p
avrdude> r fl 0x7e00 0x30
>>> r fl 0x7e00 0x30 
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> r fl 0x7f00 0x30
>>> r fl 0x7f00 0x30 
7f00  a6 01 a0 e0 b1 e0 2c 91  30 e0 11 96 8c 91 11 97  |......,.0.......|
7f10  90 e0 98 2f 88 27 82 2b  93 2b 12 96 fa 01 0c 01  |.../.'.+.+.... .|
7f20  87 be e8 95 11 24 4e 5f  5f 4f f1 e0 a0 38 bf 07  |.....$N__O...8..|

avrdude> 
avrdude> %        

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 11, 2022

Should that not be the other way round?

Probably. It was too late last night for fixing it, I only noticed it didn't work correctly, but then went to bed. -)

@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 11, 2022

also i just checked the spellings mentioned in this conversation; three are mentioned

I think this was partially my fault, partially typing sloppiness.
All source code I could find (including the MacOS version) reports it as "EditLine wrapper".

Just another idea: all editline wrappers I find use numeric version 0x0402. Maybe GNU readline was about the same level featurewise with that old version, and we could just compare rl_readline_version >= 0x0500? Current GNU readline is at version 8.x.

@mcuee
Copy link
Collaborator

mcuee commented Dec 11, 2022

also i just checked the spellings mentioned in this conversation; three are mentioned

I think this was partially my fault, partially typing sloppiness. All source code I could find (including the MacOS version) reports it as "EditLine wrapper".

Yes that should be the correct one based on what I have seen.

Just another idea: all editline wrappers I find use numeric version 0x0402. Maybe GNU readline was about the same level featurewise with that old version, and we could just compare rl_readline_version >= 0x0500? Current GNU readline is at version 8.x.

I think this will work.
The oldest GNU Readline version we want to support is indeed 5.0 (0x0500) for the MSVC port.
https://github.com/lltcggie/readline/blob/vs/src/readline/5.0/readline-5.0-src/readline.h

I looked at the history here, only the initial version (2.11-20080614-6 does not have that rl_readline_version. All other versions after that is okay.
https://salsa.debian.org/debian/libedit/-/commits/master/src/editline/readline.h

@mcuee
Copy link
Collaborator

mcuee commented Dec 11, 2022

Indeed the version check works fine under Linux.
Build with GNU Readline: long speed works
Build with latest libedit version -- long sleep does not work.

mcuee@UbuntuSwift3:~/build/avr/avrdude_jw$ git diff
diff --git a/src/term.c b/src/term.c
index ab457ed..a47c9ff 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1403,7 +1403,7 @@ int terminal_mode(PROGRAMMER *pgm, struct avrpart *p) {
   // GNU libreadline can also work if input is a pipe.
   // EditLine (NetBSD, MacOS) has issues with that, so only use it when
   // running interactively.
-  if (isatty(fileno(stdin)) || (strstr(rl_library_version, "EditLine wrapper") != 0))
+  if (isatty(fileno(stdin)) || (rl_readline_version >= 0x0500))
     return terminal_mode_interactive(pgm, p);
 #endif
   return terminal_mode_noninteractive(pgm, p);

mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ (echo r fl 0x7e00 0x30; sleep 3; echo r fl 0x7f00 0x30) | ./avrdude_pr1207v1mod -qqt -c urclock -P /dev/ttyACM0 -p m328p
avrdude> r fl 0x7e00 0x30
7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> r fl 0x7f00 0x30
7f00  a6 01 a0 e0 b1 e0 2c 91  30 e0 11 96 8c 91 11 97  |......,.0.......|
7f10  90 e0 98 2f 88 27 82 2b  93 2b 12 96 fa 01 0c 01  |.../.'.+.+.... .|
7f20  87 be e8 95 11 24 4e 5f  5f 4f f1 e0 a0 38 bf 07  |.....$N__O...8..|

avrdude> 
avrdude> mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ 
mcuee@UbuntuSwift3:~/build/avr/avrdude_bin$ (echo r fl 0x7e00 0x30; sleep 3; echo r fl 0x7f00 0x30) | ./avrdude_pr1207v1mod_libedit -qqt -c urclock -P /dev/ttyACM0 -p m328p
avrdude> 7e00  11 24 84 b7 14 be 81 ff  f0 d0 85 e0 80 93 81 00  |.$..............|
7e10  82 e0 80 93 c0 00 88 e1  80 93 c1 00 86 e0 80 93  |................|
7e20  c2 00 80 e1 80 93 c4 00  8e e0 c9 d0 25 9a 86 e0  |............%...|

avrdude> avrdude_pr1207v1mod_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1mod_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100
avrdude_pr1207v1mod_libedit error: unable to read flash page at addr 0x7f00
avrdude_pr1207v1mod_libedit error: (dump) error reading flash address 0x07f00 of part ATmega328P
                                   read operation not supported on memory type flash
avrdude> avrdude_pr1207v1mod_libedit error: programmer is not responding; try -xstrict and/or vary -xdelay=100

Besides of fixing a logico, this allows for a really straightforward
decision. All EditLine versions so far use 0x0402 as their version
number.
@dl8dtl
Copy link
Contributor Author

dl8dtl commented Dec 11, 2022

OK, I updated the PR.
I think it's ready to be merged now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avrdude on macOS is built without libreadline
4 participants