Skip to content

Commit 4bacedf

Browse files
committed
update from dbg, dyn_alloc and jparse repos.
Updated from repos via: ```sh make all.recreate_clone && make all.update_from_clone ``` Performed `make release` to test the above under macOS.
1 parent c417644 commit 4bacedf

File tree

6 files changed

+128
-23
lines changed

6 files changed

+128
-23
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ progress their dementia! :-) they can look at that repo's log or the
1414
jparse/CHANGES.md file.
1515

1616
New option `-N` to `jstrdecode(1)` and `jstrencode(1)` to ignore (in decoding
17-
and encoding internal to the tool itself, not JSON) final newline in input.
17+
and encoding internal to the tool itself, not JSON) all newlines found in data.
1818

1919

2020
## Release 1.5.24 2024-10-09

jparse/jstrdecode.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ main(int argc, char **argv)
350350
size_t outputlen; /* length of write of decode buffer */
351351
bool success = true; /* true ==> encoding OK, false ==> error while encoding */
352352
bool nloutput = true; /* true ==> output newline after JSON decode */
353-
bool ignore_nl = false; /* true ==> ignore final newline when encoding */
353+
bool ignore_nl = false; /* true ==> ignore all newlines when encoding */
354354
bool write_quote = false; /* true ==> output enclosing quotes */
355355
bool esc_quotes = false; /* true ==> escape quotes */
356356
int ret; /* libc return code */
@@ -489,6 +489,8 @@ main(int argc, char **argv)
489489
* replace input with the duplicate w/o newline input if needed
490490
*/
491491
input = dup_input;
492+
dbg(DBG_VHIGH, "-N and arg is now: %d: <%s>", i-optind, input);
493+
dbg(DBG_VHIGH, "-N and arg length is now: %ju", (uintmax_t)inputlen);
492494
}
493495

494496
/*
@@ -516,7 +518,7 @@ main(int argc, char **argv)
516518
/*
517519
* free duplicated arg if -N
518520
*/
519-
if (ignore_nl) {
521+
if (ignore_nl && input != NULL) {
520522
free(input);
521523
input = NULL;
522524
}

jparse/jstrencode.c

Lines changed: 120 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static const char * const usage_msg =
5454
"\t-V\t\tprint version string and exit\n"
5555
"\t-t\t\tperform tests of JSON decode/encode functionality\n"
5656
"\t-n\t\tdo not output newline after encode output (def: print final newline)\n"
57-
"\t-N\t\tignore final newline in input\n"
57+
"\t-N\t\tignore all newline characters\n"
5858
"\t-Q\t\tdo not encode double quotes that enclose the concatenation of args (def: do encode)\n"
5959
"\t-e\t\tdo not output double quotes that enclose each arg (def: do not remove)\n"
6060
"\n"
@@ -78,7 +78,7 @@ static const char * const usage_msg =
7878
*/
7979
static void usage(int exitcode, char const *prog, char const *str) __attribute__((noreturn));
8080
static struct jstring *jstrencode_stream(FILE *in_stream, bool skip_enclosing, bool ignore_first, bool remove_last,
81-
bool skip_eol_nl);
81+
bool ignore_nl);
8282
static struct jstring *add_encoded_string(char *string, size_t bufsiz);
8383
static void free_json_encoded_strings(void);
8484

@@ -178,6 +178,68 @@ free_json_encoded_strings(void)
178178
}
179179

180180

181+
/*
182+
* dup_without_nl - duplicate a buffer and remove all newlines
183+
*
184+
* given:
185+
* input original input buffer
186+
* inputlen pointer to the length of the input buffer
187+
*
188+
* returns:
189+
* malloced buffer without any newlines
190+
* NULL ==> malloc error, or NULL argument
191+
*
192+
* NOTE: If newlines were removed in the copy, then *inputlen will be updated
193+
* to account for the new length.
194+
*/
195+
static char *
196+
dup_without_nl(char *input, size_t *inputlen)
197+
{
198+
char *dup_input = NULL; /* duplicate of input */
199+
size_t i;
200+
size_t j;
201+
202+
/*
203+
* firewall
204+
*/
205+
if (input == NULL) {
206+
warn(__func__, "input is NULL");
207+
return NULL;
208+
}
209+
if (inputlen == NULL) {
210+
warn(__func__, "inputlen is NULL");
211+
return NULL;
212+
}
213+
214+
/*
215+
* copy input removing all newlines
216+
*/
217+
dup_input = malloc(*inputlen + 1); /* + 1 for guard NUL byte */
218+
if (dup_input == NULL) {
219+
warn(__func__, "malloc of input failed");
220+
return NULL;
221+
}
222+
for (i=0, j=0; i < *inputlen; ++i) {
223+
if (input[i] != '\n') {
224+
dup_input[j++] = input[i];
225+
}
226+
}
227+
dup_input[j] = '\0'; /* paranoia */
228+
229+
/*
230+
* update inputlen if we removed newlines
231+
*/
232+
if (j != i) {
233+
*inputlen = j;
234+
}
235+
236+
/*
237+
* return success
238+
*/
239+
return dup_input;
240+
}
241+
242+
181243
/*
182244
* jstrencode_stream - encode an open file stream into a char *
183245
*
@@ -189,7 +251,7 @@ free_json_encoded_strings(void)
189251
* false ==> do not remove
190252
* remove_last remove any final double quote
191253
* false ==> do not remove
192-
* skip_eol_nl true ==> skip final newline
254+
* ignore_nl true ==> ignore all newline characters
193255
*
194256
* returns:
195257
* allocated struct jstring * ==> encoding was successful,
@@ -199,14 +261,15 @@ free_json_encoded_strings(void)
199261
* encoded JSON strings.
200262
*/
201263
static struct jstring *
202-
jstrencode_stream(FILE *in_stream, bool skip_enclosing, bool ignore_first, bool remove_last, bool skip_eol_nl)
264+
jstrencode_stream(FILE *in_stream, bool skip_enclosing, bool ignore_first, bool remove_last, bool ignore_nl)
203265
{
204266
char *orig_input = NULL; /* argument to process */
205267
char *input = NULL; /* possibly updated orig_input */
206268
size_t inputlen = 0; /* length of input buffer */
207269
char *buf = NULL; /* encode buffer */
208270
size_t bufsiz; /* length of the buffer */
209271
struct jstring *jstr = NULL; /* for jstring list */
272+
char *dup_input = NULL; /* duplicate of input w/o newlines */
210273

211274
/*
212275
* firewall
@@ -226,13 +289,30 @@ jstrencode_stream(FILE *in_stream, bool skip_enclosing, bool ignore_first, bool
226289
return NULL;
227290
}
228291
dbg(DBG_MED, "stream read length: %ju", (uintmax_t)inputlen);
292+
293+
/*
294+
* if -N, remove all newlines from data
295+
*/
296+
if (ignore_nl) {
297+
298+
/*
299+
* copy input removing all newlines, update inputlen if needed
300+
*/
301+
dup_input = dup_without_nl(orig_input, &inputlen);
302+
if (dup_input == NULL) {
303+
err(11, __func__, "dup_without_nl failed");
304+
not_reached();
305+
}
306+
307+
/*
308+
* replace input with the duplicate w/o newline input if needed
309+
*/
310+
free(orig_input);
311+
orig_input = dup_input;
312+
}
229313
input = orig_input;
230314
dbg(DBG_HIGH, "stream data is: <%s>", input);
231315

232-
if (skip_eol_nl && inputlen > 0 && input[inputlen - 1] == '\n') {
233-
input[inputlen - 1] = '\0';
234-
--inputlen;
235-
}
236316
/*
237317
* case: we need to remove BOTH a leading and a trailing double quote
238318
*/
@@ -335,18 +415,19 @@ main(int argc, char **argv)
335415
size_t outputlen; /* length of write of encode buffer */
336416
bool success = true; /* true ==> encoding OK, false ==> error while encoding */
337417
bool nloutput = true; /* true ==> output newline after JSON encode */
338-
bool skip_eol_nl = false; /* true ==> ignore final newline when encoding */
418+
bool ignore_nl = false; /* true ==> ignore all newlines when encoding */
339419
bool skip_concat_quotes = false; /* true ==> skip enclosing quotes around the arg concatenation */
340420
bool skip_each = false; /* true ==> skip enclosing quotes around each arg */
341421
int ret; /* libc return code */
342422
int i;
343423
struct jstring *jstr = NULL; /* to iterate through list */
424+
char *dup_input = NULL; /* duplicate of input w/o newlines */
344425

345426
/*
346427
* set locale
347428
*/
348429
if (setlocale(LC_ALL, "") == NULL) {
349-
err(11, __func__, "failed to set locale");
430+
err(12, __func__, "failed to set locale");
350431
not_reached();
351432
}
352433

@@ -394,7 +475,7 @@ main(int argc, char **argv)
394475
nloutput = false;
395476
break;
396477
case 'N':
397-
skip_eol_nl = true;
478+
ignore_nl = true;
398479
break;
399480
case 'Q':
400481
skip_concat_quotes = true;
@@ -444,7 +525,7 @@ main(int argc, char **argv)
444525
*/
445526
jstr = jstrencode_stream(stdin, skip_each,
446527
(skip_concat_quotes && i == optind),
447-
(skip_concat_quotes && i == argc-1), skip_eol_nl);
528+
(skip_concat_quotes && i == argc-1), ignore_nl);
448529
if (!jstr) {
449530
warn(__func__, "failed to encode string from stdin");
450531
success = false;
@@ -463,11 +544,25 @@ main(int argc, char **argv)
463544
}
464545

465546
/*
466-
* case: we need to remove trailing newline
547+
* If -N, and newlines in arg, remove them
467548
*/
468-
if (skip_eol_nl && inputlen > 0 && input[inputlen-1] == '\n') {
469-
input[inputlen - 1] = '\0';
470-
--inputlen;
549+
if (ignore_nl) {
550+
551+
/*
552+
* copy input removing all newlines, update inputlen if needed
553+
*/
554+
dup_input = dup_without_nl(input, &inputlen);
555+
if (dup_input == NULL) {
556+
err(13, __func__, "dup_without_nl failed");
557+
not_reached();
558+
}
559+
560+
/*
561+
* replace input with the duplicate w/o newline input if needed
562+
*/
563+
input = dup_input;
564+
dbg(DBG_VHIGH, "-N and arg is now: %d: <%s>", i-optind, input);
565+
dbg(DBG_VHIGH, "-N and arg length is now: %ju", (uintmax_t)inputlen);
471566
}
472567

473568
/*
@@ -545,6 +640,14 @@ main(int argc, char **argv)
545640
dbg(DBG_MED, "added string of size %ju to encoded strings list", bufsiz);
546641
}
547642
}
643+
644+
/*
645+
* free duplicated arg if -N
646+
*/
647+
if (ignore_nl && input != NULL) {
648+
free(input);
649+
input = NULL;
650+
}
548651
}
549652

550653
/*
@@ -560,7 +663,7 @@ main(int argc, char **argv)
560663
* NOTE: jstrencode_stream() adds the allocated struct jstring * to the
561664
* encoded JSON strings list
562665
*/
563-
jstr = jstrencode_stream(stdin, skip_concat_quotes, skip_each, skip_each, skip_eol_nl);
666+
jstr = jstrencode_stream(stdin, skip_concat_quotes, skip_each, skip_each, ignore_nl);
564667
if (jstr != NULL) {
565668
dbg(DBG_MED, "encode length: %ju", jstr->bufsiz);
566669
} else {

jparse/jstrencode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
/*
7070
* official jstrencode version
7171
*/
72-
#define JSTRENCODE_VERSION "1.2.2 2024-10-10" /* format: major.minor YYYY-MM-DD */
72+
#define JSTRENCODE_VERSION "1.2.3 2024-10-10" /* format: major.minor YYYY-MM-DD */
7373

7474

7575
/*

jparse/man/man1/jstrdecode.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Run tests on the JSON decode/encode functions
7171
Do not output a newline after the decode function
7272
.TP
7373
.B \-N
74-
Ignore and skip over all newlines input, for easier typing commands.
74+
Ignore and skip over all newlines, for easier typing commands.
7575
.TP
7676
.B \-Q
7777
Enclose output in double quotes

jparse/man/man1/jstrencode.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Run tests on the JSON encode/encode functions
5858
Do not output a newline after the encode function
5959
.TP
6060
.B \-N
61-
Ignore trailing newline in input
61+
Ignore and skip over all newlines, for easier typing commands.
6262
.TP
6363
.B \-Q
6464
Do not encode double quotes that enclose the concatenation of args (def: do encode)

0 commit comments

Comments
 (0)