Skip to content

Commit 3328494

Browse files
committed
feat: add -J/-j option parsing for JSON output
Add Fjson/Fjsonl global flags, option parsing for -J (nested JSON) and -j (JSON Lines), mutual exclusivity validation, and default field selection when -F is not explicitly given.
1 parent 761ca77 commit 3328494

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

lib/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,9 @@ extern int ErrStat;
684684
extern uid_t Euid;
685685
extern int Fcntx;
686686
extern int Ffield;
687+
extern int Fjson;
688+
extern int Fjsonl;
689+
extern int Fjson_first_proc;
687690
extern int Ffilesys;
688691
extern int Fhelp;
689692
extern int Fhost;

src/main.c

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ int main(int argc, char *argv[]) {
151151
* Create option mask.
152152
*/
153153
(void)snpf(options, sizeof(options),
154-
"?a%sbc:%sD:d:%s%sf:F:g:hHi:%s%slL:%s%snNo:Op:QPr:%ss:S:tT:u:"
154+
"?a%sbc:%sD:d:%s%sf:F:g:hHi:%s%sJjlL:%s%snNo:Op:QPr:%ss:S:tT:u:"
155155
"UvVwx:%s%s%s",
156156

157157
#if defined(HAS_AFS) && defined(HASAOPT)
@@ -556,6 +556,24 @@ int main(int argc, char *argv[]) {
556556
case '?':
557557
Fhelp = 1;
558558
break;
559+
case 'J':
560+
if (GOp == '+') {
561+
(void)fprintf(stderr, "%s: +J is not supported\n", Pn);
562+
err = 1;
563+
break;
564+
}
565+
Fjson = 1;
566+
Ffield = 1;
567+
break;
568+
case 'j':
569+
if (GOp == '+') {
570+
(void)fprintf(stderr, "%s: +j is not supported\n", Pn);
571+
err = 1;
572+
break;
573+
}
574+
Fjsonl = 1;
575+
Ffield = 1;
576+
break;
559577
case 'i':
560578
if (!GOv || *GOv == '-' || *GOv == '+') {
561579
Fnet = 1;
@@ -1097,6 +1115,14 @@ int main(int argc, char *argv[]) {
10971115
(void)fprintf(stderr, "%s: -x must accompany +d or +D\n", Pn);
10981116
err++;
10991117
}
1118+
if (Fjson && Fjsonl) {
1119+
(void)fprintf(stderr, "%s: -J and -j are mutually exclusive\n", Pn);
1120+
err++;
1121+
}
1122+
if ((Fjson || Fjsonl) && Fterse) {
1123+
(void)fprintf(stderr, "%s: -J/-j and -t are mutually exclusive\n", Pn);
1124+
err++;
1125+
}
11001126

11011127
#if defined(HASEOPT)
11021128
if (Efsysl) {
@@ -1126,6 +1152,58 @@ int main(int argc, char *argv[]) {
11261152
}
11271153
#endif /* defined(HASEOPT) */
11281154

1155+
/*
1156+
* If -J/-j was given, ensure field selections are set.
1157+
* If -F was also given with field chars, those selections are already
1158+
* in FieldSel[]. Otherwise, enable the default field set.
1159+
*/
1160+
if (Fjson || Fjsonl) {
1161+
int has_fields = 0;
1162+
for (i = 0; FieldSel[i].nm; i++) {
1163+
if (FieldSel[i].st && FieldSel[i].id != LSOF_FID_PID &&
1164+
FieldSel[i].id != LSOF_FID_MARK) {
1165+
has_fields = 1;
1166+
break;
1167+
}
1168+
}
1169+
if (!has_fields) {
1170+
for (i = 0; FieldSel[i].nm; i++) {
1171+
#if !defined(HASPPID)
1172+
if (FieldSel[i].id == LSOF_FID_PPID)
1173+
continue;
1174+
#endif
1175+
#if !defined(HASTASKS)
1176+
if (FieldSel[i].id == LSOF_FID_TCMD ||
1177+
FieldSel[i].id == LSOF_FID_TID)
1178+
continue;
1179+
#endif
1180+
#if !defined(HASFSTRUCT)
1181+
if (FieldSel[i].id == LSOF_FID_CT ||
1182+
FieldSel[i].id == LSOF_FID_FA ||
1183+
FieldSel[i].id == LSOF_FID_FG ||
1184+
FieldSel[i].id == LSOF_FID_NI)
1185+
continue;
1186+
#endif
1187+
#if defined(HASSELINUX)
1188+
if ((FieldSel[i].id == LSOF_FID_CNTX) && !CntxStatus)
1189+
continue;
1190+
#else
1191+
if (FieldSel[i].id == LSOF_FID_CNTX)
1192+
continue;
1193+
#endif
1194+
if (FieldSel[i].id == LSOF_FID_RDEV)
1195+
continue;
1196+
#if !defined(HASZONES)
1197+
if (FieldSel[i].id == LSOF_FID_ZONE)
1198+
continue;
1199+
#endif
1200+
FieldSel[i].st = 1;
1201+
if (FieldSel[i].opt && FieldSel[i].ov)
1202+
*(FieldSel[i].opt) |= FieldSel[i].ov;
1203+
}
1204+
}
1205+
}
1206+
11291207
if (DChelp || err || Fhelp || fh || version)
11301208
usage(ctx, err ? 1 : 0, fh, version);
11311209
/*

src/store.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ int NcacheReload = 1; /* 1 == call ncache_load() */
7272
#endif /* defined(HASNCACHE) */
7373

7474
int Ffield = 0; /* -f and -F status */
75+
int Fjson = 0; /* -J JSON output status */
76+
int Fjsonl = 0; /* -j JSON Lines output status */
77+
int Fjson_first_proc = 1; /* first process flag for JSON comma handling */
7578
int FgColW; /* FILE-FLAG column width */
7679
int Fhelp = 0; /* -h option status */
7780
int Fhost = 1; /* -H option status */

0 commit comments

Comments
 (0)