Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerysong committed Nov 16, 2023
0 parents commit 03a0d91
Show file tree
Hide file tree
Showing 12 changed files with 2,410 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AlignConsecutiveDeclarations: true
AlignTrailingComments: true
AlignAfterOpenBracket: DontAlign
AllowShortIfStatementsOnASingleLine: false
AlwaysBreakAfterReturnType: AllDefinitions
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBraces: Linux
BreakBeforeBinaryOperators: false
BreakBeforeTernaryOperators: true
BreakStringLiterals: true
ColumnLimit: 80
ConstructorInitializerAllOnOneLineOrOnePerLine: true
DerivePointerAlignment: false
PointerAlignment: Right
IndentCaseLabels: false
MaxEmptyLinesToKeep: 2
PointerBindsToType: false
ReflowComments: false
IndentWidth: 4
TabWidth: 4
UseTab: Never
BreakBeforeBraces: Attach
IndentFunctionDeclarationAfterType: false
SpacesInSquareBrackets: true
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true
ContinuationIndentWidth: 8
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.o
.deps/
Makefile
Makefile.in
aclocal.m4
autom4te.cache/
compile
config.h
config.h.in
config.h.in~
config.log
config.status
configure
depcomp
ejq
install-sh
missing
19 changes: 19 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023 Paul Arthur MacIain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions COPYING.easy-json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2023, James W M Barford-Evans <jamesbarfordevans at gmail dot com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5 changes: 5 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
AM_CFLAGS = -Wall

bin_PROGRAMS = ejq

ejq_SOURCES = json.c json.h json-selector.c json-selector.h ejq.c
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is a simple CLI wrapper around James Barford's
[easy-json](https://github.com/Jamesbarford/easy-json) library.
9 changes: 9 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AC_INIT([easy-json-q], [0.1], [[email protected]])
AM_INIT_AUTOMAKE([-Wall -Werror foreign no-dist-gzip dist-xz])

AC_PROG_CC
AC_PROG_CC_C99
AC_PROG_INSTALL

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
81 changes: 81 additions & 0 deletions ejq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include <json-selector.h>
#include <json.h>

char *
read_file(const char *path) {
char *buffer;
int fd = open(path, O_RDONLY, 0666);
if (fd == -1) {
fprintf(stderr, "Cannot access file %s", path);
exit(1);
}

long size = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);

buffer = calloc(sizeof(char), size);

if (read(fd, buffer, size) != size) {
fprintf(stderr, "Failed to read file %s: %m\n", path);
exit(1);
}

close(fd);
return buffer;
}

int
main(int ac, char *av[]) {
int c;
int err = 0;
bool raw = false;
char *expr = NULL;
char *fname = NULL;

while ((c = getopt(ac, av, "e:f:r")) != -1) {
switch (c) {
case 'e':
expr = optarg;
break;

case 'f':
fname = optarg;
break;

case 'r':
raw = true;
break;

default:
err++;
}
}

if (err || optind != ac) {
fprintf(stderr, "Usage:\t%s", av[ 0 ]);
fprintf(stderr, " -e <expression> -f <file> [-r]\n");
exit(1);
}

json *parsed = jsonParse(read_file(fname));
json *res = jsonSelect(parsed, expr);

if (jsonIsArray(res)) {
res = res->array;
} else if (jsonIsObject(res)) {
res = res->object;
}

if (raw) {

} else {
printf("%s", jsonToString(res, NULL));
}
}
Loading

0 comments on commit 03a0d91

Please sign in to comment.