-
Notifications
You must be signed in to change notification settings - Fork 127
feat: add native JSON output (-J/-j flags) #353
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
Changes from 7 commits
3328494
f221123
cf0b102
5eba279
f622651
c2e600f
34d6b1b
bb8bab1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -151,7 +151,7 @@ int main(int argc, char *argv[]) { | |
| * Create option mask. | ||
| */ | ||
| (void)snpf(options, sizeof(options), | ||
| "?a%sbc:%sD:d:%s%sf:F:g:hHi:%s%slL:%s%snNo:Op:QPr:%ss:S:tT:u:" | ||
| "?a%sbc:%sD:d:%s%sf:F:g:hHi:%s%sJjlL:%s%snNo:Op:QPr:%ss:S:tT:u:" | ||
| "UvVwx:%s%s%s", | ||
|
|
||
| #if defined(HAS_AFS) && defined(HASAOPT) | ||
|
|
@@ -556,6 +556,24 @@ int main(int argc, char *argv[]) { | |
| case '?': | ||
| Fhelp = 1; | ||
| break; | ||
| case 'J': | ||
| if (GOp == '+') { | ||
| (void)fprintf(stderr, "%s: +J is not supported\n", Pn); | ||
| err = 1; | ||
| break; | ||
| } | ||
| Fjson = 1; | ||
| Ffield = 1; | ||
| break; | ||
| case 'j': | ||
| if (GOp == '+') { | ||
| (void)fprintf(stderr, "%s: +j is not supported\n", Pn); | ||
| err = 1; | ||
| break; | ||
| } | ||
| Fjsonl = 1; | ||
| Ffield = 1; | ||
| break; | ||
| case 'i': | ||
| if (!GOv || *GOv == '-' || *GOv == '+') { | ||
| Fnet = 1; | ||
|
|
@@ -1097,6 +1115,14 @@ int main(int argc, char *argv[]) { | |
| (void)fprintf(stderr, "%s: -x must accompany +d or +D\n", Pn); | ||
| err++; | ||
| } | ||
| if (Fjson && Fjsonl) { | ||
|
jiegec marked this conversation as resolved.
|
||
| (void)fprintf(stderr, "%s: -J and -j are mutually exclusive\n", Pn); | ||
| err++; | ||
| } | ||
| if ((Fjson || Fjsonl) && Fterse) { | ||
| (void)fprintf(stderr, "%s: -J/-j and -t are mutually exclusive\n", Pn); | ||
| err++; | ||
| } | ||
|
|
||
| #if defined(HASEOPT) | ||
| if (Efsysl) { | ||
|
|
@@ -1126,6 +1152,58 @@ int main(int argc, char *argv[]) { | |
| } | ||
| #endif /* defined(HASEOPT) */ | ||
|
|
||
| /* | ||
| * If -J/-j was given, ensure field selections are set. | ||
| * If -F was also given with field chars, those selections are already | ||
| * in FieldSel[]. Otherwise, enable the default field set. | ||
| */ | ||
| if (Fjson || Fjsonl) { | ||
| int has_fields = 0; | ||
| for (i = 0; FieldSel[i].nm; i++) { | ||
| if (FieldSel[i].st && FieldSel[i].id != LSOF_FID_PID && | ||
| FieldSel[i].id != LSOF_FID_MARK) { | ||
| has_fields = 1; | ||
| break; | ||
| } | ||
| } | ||
| if (!has_fields) { | ||
| for (i = 0; FieldSel[i].nm; i++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lots of code duplication with existing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in bb8bab1. Extracted |
||
| #if !defined(HASPPID) | ||
| if (FieldSel[i].id == LSOF_FID_PPID) | ||
| continue; | ||
| #endif | ||
| #if !defined(HASTASKS) | ||
| if (FieldSel[i].id == LSOF_FID_TCMD || | ||
| FieldSel[i].id == LSOF_FID_TID) | ||
| continue; | ||
| #endif | ||
| #if !defined(HASFSTRUCT) | ||
| if (FieldSel[i].id == LSOF_FID_CT || | ||
| FieldSel[i].id == LSOF_FID_FA || | ||
| FieldSel[i].id == LSOF_FID_FG || | ||
| FieldSel[i].id == LSOF_FID_NI) | ||
| continue; | ||
| #endif | ||
| #if defined(HASSELINUX) | ||
| if ((FieldSel[i].id == LSOF_FID_CNTX) && !CntxStatus) | ||
| continue; | ||
| #else | ||
| if (FieldSel[i].id == LSOF_FID_CNTX) | ||
| continue; | ||
| #endif | ||
| if (FieldSel[i].id == LSOF_FID_RDEV) | ||
| continue; | ||
| #if !defined(HASZONES) | ||
| if (FieldSel[i].id == LSOF_FID_ZONE) | ||
| continue; | ||
| #endif | ||
| FieldSel[i].st = 1; | ||
| if (FieldSel[i].opt && FieldSel[i].ov) | ||
| *(FieldSel[i].opt) |= FieldSel[i].ov; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (DChelp || err || Fhelp || fh || version) | ||
| usage(ctx, err ? 1 : 0, fh, version); | ||
| /* | ||
|
|
@@ -1312,6 +1390,9 @@ int main(int argc, char *argv[]) { | |
| (void)qsort((QSORT_P *)slp, (size_t)Nlproc, | ||
| (size_t)sizeof(struct lproc *), comppid); | ||
| } | ||
| if (Fjson) { | ||
| json_open_envelope(); | ||
| } | ||
| if ((n = Nlproc)) { | ||
|
|
||
| #if defined(HASNCACHE) | ||
|
|
@@ -1468,6 +1549,11 @@ int main(int argc, char *argv[]) { | |
| } | ||
| Lf = lf; | ||
| } | ||
| if (Fjson) { | ||
| json_close_envelope(); | ||
| } else if (Fjsonl && RptTm) { | ||
| putchar('\n'); | ||
| } | ||
| /* | ||
| * If a repeat time is set, sleep for the specified time. | ||
| * | ||
|
|
@@ -1515,7 +1601,9 @@ int main(int argc, char *argv[]) { | |
| } | ||
| #endif /* defined(HAS_STRFTIME) */ | ||
|
|
||
| if (Ffield) { | ||
| if (Fjson || Fjsonl) { | ||
| /* JSON modes handle their own cycle separation */ | ||
| } else if (Ffield) { | ||
| putchar(LSOF_FID_MARK); | ||
|
|
||
| #if defined(HAS_STRFTIME) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.