Re: [PATCH] ed: add support for -p command-line option as mandated by POSIX

2021-11-20 Thread Kang-Che Sung
On Sunday, November 21, 2021, David Laight  wrote:
> From: soe...@soeren-tempel.net
>> Sent: 20 November 2021 17:17
>>
>> The POSIX.1-2008 specification of ed(1) mandates two command-line
>> options: -p (for specifying a prompt string) and -s (to suppress writing
>> of byte counts). This commit adds support for the former. Furthermore,
>> it also changes the default prompt string to an empty string (instead
>> of ": ") since this is also mandated by POSIX:
>>
>>   -p string Use string as the prompt string when in command mode.
>> By default, there shall be no prompt string.
>>
> ...
>> - if (argv[1]) {
>> - fileName = xstrdup(argv[1]);
>> + opt = getopt32(argv, "p:", );
>> + if (!(opt & 0x1))
>> + prompt = xstrdup(""); /* no prompt by default */
>
> You shouldn't need the strdup().
> I think you can even do:
> if (!(opt & 1))
> prompt = "";
> because (IIRC and for historic reasons) quoted strings are char[] not
const char[].

I don't know why you are messing up with the "constness" of the strings. C
standard says string literal is of type const char[], and the const keyword
didn't exist before C89.
Note the compiler is free to merge string literals with identical content
so they share the same buffer in the .rodata section (that's why they are
const).
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


RE: [PATCH] ed: add support for -p command-line option as mandated by POSIX

2021-11-20 Thread David Laight
From: soe...@soeren-tempel.net
> Sent: 20 November 2021 17:17
> 
> The POSIX.1-2008 specification of ed(1) mandates two command-line
> options: -p (for specifying a prompt string) and -s (to suppress writing
> of byte counts). This commit adds support for the former. Furthermore,
> it also changes the default prompt string to an empty string (instead
> of ": ") since this is also mandated by POSIX:
> 
>   -p string Use string as the prompt string when in command mode.
> By default, there shall be no prompt string.
> 
...
> - if (argv[1]) {
> - fileName = xstrdup(argv[1]);
> + opt = getopt32(argv, "p:", );
> + if (!(opt & 0x1))
> + prompt = xstrdup(""); /* no prompt by default */

You shouldn't need the strdup().
I think you can even do:
if (!(opt & 1))
prompt = "";
because (IIRC and for historic reasons) quoted strings are char[] not const 
char[].
OTOH an explicit local static or a zero byte in the data area
might be more usual for busybox.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, 
UK
Registration No: 1397386 (Wales)
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[PATCH] ed: add support for -p command-line option as mandated by POSIX

2021-11-20 Thread soeren
From: Sören Tempel 

The POSIX.1-2008 specification of ed(1) mandates two command-line
options: -p (for specifying a prompt string) and -s (to suppress writing
of byte counts). This commit adds support for the former. Furthermore,
it also changes the default prompt string to an empty string (instead
of ": ") since this is also mandated by POSIX:

-p string Use string as the prompt string when in command mode.
  By default, there shall be no prompt string.

Support for the remaining -s option will be added in a separate commit
since it requires a general restructuring of error handling in Busybox
ed.

Signed-off-by: Sören Tempel 
---
 editors/ed.c | 17 ++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/editors/ed.c b/editors/ed.c
index 0d96d263c..6473281a1 100644
--- a/editors/ed.c
+++ b/editors/ed.c
@@ -48,6 +48,7 @@ struct globals {
char *bufBase;
char *bufPtr;
char *fileName;
+   char *prompt;
LINE lines;
smallint dirty;
int marks[26];
@@ -57,6 +58,7 @@ struct globals {
 #define bufBase(G.bufBase   )
 #define bufPtr (G.bufPtr)
 #define fileName   (G.fileName  )
+#define prompt (G.prompt)
 #define curNum (G.curNum)
 #define lastNum(G.lastNum   )
 #define bufUsed(G.bufUsed   )
@@ -790,7 +792,7 @@ static void doCommands(void)
 * 0  on ctrl-C,
 * >0 length of input string, including terminating '\n'
 */
-   len = read_line_input(NULL, ": ", buf, sizeof(buf));
+   len = read_line_input(NULL, prompt, buf, sizeof(buf));
if (len <= 0)
return;
while (len && isspace(buf[--len]))
@@ -994,6 +996,8 @@ static void doCommands(void)
 int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int ed_main(int argc UNUSED_PARAM, char **argv)
 {
+   int opt;
+
INIT_G();
 
bufSize = INITBUF_SIZE;
@@ -1002,8 +1006,15 @@ int ed_main(int argc UNUSED_PARAM, char **argv)
lines.next = 
lines.prev = 
 
-   if (argv[1]) {
-   fileName = xstrdup(argv[1]);
+   opt = getopt32(argv, "p:", );
+   if (!(opt & 0x1))
+   prompt = xstrdup(""); /* no prompt by default */
+
+   argc -= optind;
+   argv += optind;
+
+   if (argc >= 1) {
+   fileName = xstrdup(argv[0]);
if (!readLines(fileName, 1)) {
return EXIT_SUCCESS;
}
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox