11#include "utils.h"
2+ #include "json.h"
23#include <stdio.h>
4+ #include <unistd.h>
35#include <stdlib.h>
46#include <string.h>
57
@@ -33,20 +35,59 @@ int checkArgs(int argc, char *argv[], int *fopt, FILE **fptr) {
3335 return 0 ;
3436}
3537
36- void getLines (int * fopt , FILE * fptr , Document * doc ) {
38+ void getBody (int * fopt , FILE * fptr , Document * doc ) {
3739 int c ; // Parsed input character
40+
41+ while ((c = (* fopt ? fgetc (fptr ) : getchar ())) != EOF ) {
42+ appendString (doc -> body , c );
43+ }
44+ }
45+
46+ void getLines (Document * doc ) {
3847 Line line ;
3948 initLine (& line );
4049
41- while ((c = (* fopt ? fgetc (fptr ) : getchar ())) != EOF ) {
42- if (c == CR ) {
43- appendDocumentLine (doc , cloneLine (& line ));
44- freeLine (& line );
45- initLine (& line );
50+ int go = 1 ;
51+
52+ for (int i = 0 ; i < doc -> body -> len ; i ++ ) {
53+ char c = doc -> body -> text [i ];
54+ char next = doc -> body -> text [i + 1 ];
55+ char nextnext ;
56+ if (i + 2 < doc -> body -> len ) {
57+ nextnext = doc -> body -> text [i + 2 ];
4658 } else {
47- appendLine (& line , c );
59+ nextnext = '\0' ;
60+ }
61+
62+ if (c == '\\' && next == CR && nextnext == SP ) {
63+ go = 0 ;
64+ } else if (c == CR && next == SP ) {
65+ go = 0 ;
66+ }
67+
68+
69+ if (go ) {
70+ if (c == CR ) {
71+ appendDocumentLine (doc , cloneLine (& line ));
72+ freeLine (& line );
73+ initLine (& line );
74+ } else {
75+ appendLine (& line , c );
76+ }
77+ } else {
78+ if (c != '\\' && c != CR && c != SP ) {
79+ appendLine (& line , c );
80+ go = 1 ;
81+ }
4882 }
4983 }
84+
85+ if (line .len > 0 ) {
86+ appendDocumentLine (doc , cloneLine (& line ));
87+ freeLine (& line );
88+ } else {
89+ freeLine (& line );
90+ }
5091}
5192
5293void filterLines (Document * doc , char filtered ) {
@@ -65,18 +106,6 @@ void filterLines(Document *doc, char filtered) {
65106 }
66107}
67108
68- void formatLines (Document , * doc ) {
69- Key key ;
70- Value value ;
71- for (int i = 0 ; i < doc -> llen ; i ++ ) {
72- appendKey (& key , '"' );
73- for (int j = 0 ; j < doc -> lines [i ]-> len ; j ++ ) {
74-
75- }
76- appendKey (& key , '",' );
77- }
78- }
79-
80109void getEntries (Document * doc ) {
81110 Entry entry ;
82111 initEntry (& entry );
@@ -86,9 +115,15 @@ void getEntries(Document *doc) {
86115 freeEntry (& entry );
87116 initEntry (& entry );
88117 } else {
118+ formatLineAsJson (doc -> lines [i ]);
89119 appendEntry (& entry , doc -> lines [i ]);
90120 }
91121 }
122+
123+ if (entry .len > 0 ) {
124+ appendDocumentEntry (doc , cloneEntry (& entry ));
125+ freeEntry (& entry );
126+ }
92127}
93128
94129void squashEntries (Document * doc ) {
@@ -112,7 +147,13 @@ void printJSON(Document *doc) {
112147 for (int i = 0 ; i < doc -> elen ; i ++ ) {
113148 printf (IDNT2 "{\n" );
114149 for (int j = 0 ; j < doc -> entries [i ]-> len ; j ++ ) {
115- printf (IDNT4 "%s\n" , doc -> entries [i ]-> lines [j ]-> text );
150+ char * closing ;
151+ if (j == doc -> entries [i ]-> len - 1 ) {
152+ closing = "\n" ;
153+ } else {
154+ closing = ",\n" ;
155+ }
156+ printf (IDNT4 "\"%s\": \"%s\"%s" , doc -> entries [i ]-> lines [j ]-> key -> text , doc -> entries [i ]-> lines [j ]-> vals [0 ]-> text , closing );
116157 }
117158 char * closing ;
118159 if (i == doc -> elen - 1 ) {
@@ -136,9 +177,14 @@ int main(int argc, char *argv[]) {
136177 return 0 ;
137178 }
138179
139- getLines (& fopt , fptr , & doc ); // Gather input data by lines
180+ if (!fopt && isatty (STDIN_FILENO )) {
181+ printf ("No input provided.\n" );
182+ return 0 ;
183+ }
184+
185+ getBody (& fopt , fptr , & doc ); // Gather input data by lines
186+ getLines (& doc ); // Gather input data by lines
140187 filterLines (& doc , '#' ); // Remove all comment lines that start with # character
141- formatLines (& doc ); // Format each line into valid JSON
142188 getEntries (& doc ); // Gather lines separated by NLs into entries
143189 squashEntries (& doc ); // Remove all empty entries
144190 printJSON (& doc ); // Output formatted JSON
0 commit comments