├── .gitignore ├── src ├── mcpi.h ├── test.c └── mcpi.c ├── Makefile ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | # Builds 2 | build/ 3 | 4 | # Debian 5 | libmcpi_*-*.deb 6 | deb/ 7 | -------------------------------------------------------------------------------- /src/mcpi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * mcpi.h 3 | * 4 | * Copyright 2020-2021 Alvarito050506 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; version 3 of the License. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 | * MA 02110-1301, USA. 19 | * 20 | * 21 | */ 22 | 23 | #ifndef LIB_MCPI_H 24 | #define LIB_MCPI_H 25 | 26 | typedef struct mcpi_command_t 27 | { 28 | char package[16]; 29 | char name[32]; 30 | char args[16][32]; 31 | int argc; 32 | } mcpi_command_t; 33 | 34 | typedef struct mcpi_err_t 35 | { 36 | char code; 37 | int pos; 38 | char chr; 39 | } mcpi_err_t; 40 | 41 | mcpi_command_t mcpi_parse_command(const char* string, mcpi_err_t* err); 42 | 43 | #endif /* LIB_MCPI_H */ 44 | -------------------------------------------------------------------------------- /src/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * test.c 3 | * 4 | * Copyright 2020-2021 Alvarito050506 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; version 3 of the License. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 | * MA 02110-1301, USA. 19 | * 20 | * 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | int main(int argc, char* argv[]) 27 | { 28 | mcpi_err_t err; 29 | mcpi_command_t command; 30 | 31 | command = mcpi_parse_command("package.command(0,arg,arg2)", &err); 32 | if (err.code) 33 | { 34 | printf("\x1b[1;31mSyntax Error:\x1b[0m at position %i, unexcepted character '\x1b[1m%c\x1b[0m'.\n", err.pos, err.chr); 35 | return -1; 36 | } 37 | printf("%s.%s(%s, %s, %s) /* Argument count: %i */\n", command.package, command.name, command.args[0], command.args[1], command.args[2], command.argc); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile 3 | # 4 | # Copyright 2020-2021 Alvarito050506 5 | # 6 | # This program is free software; you can redistribute it and/or modify 7 | # it under the terms of the GNU Lesser General Public License as published by 8 | # the Free Software Foundation; version 3 of the License. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program; if not, write to the Free Software 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 | # MA 02110-1301, USA. 19 | # 20 | # 21 | 22 | all: 23 | mkdir -p build 24 | gcc -c -fPIC ./src/mcpi.c -I./src -o ./build/mcpi.o 25 | gcc -shared -fPIC ./build/mcpi.o -o ./build/libmcpi.so 26 | 27 | install: 28 | cp ./build/libmcpi.so /usr/lib 29 | cp ./src/mcpi.h /usr/include 30 | chmod 0755 /usr/lib/libmcpi.so 31 | ldconfig 32 | 33 | test-global: 34 | gcc ./test.c -lmcpi -o ./build/test.elf 35 | 36 | test-local: 37 | gcc ./src/test.c -I./src/ -L./build/ -lmcpi -Wl,-rpath=./build/ -lmcpi -o ./build/test.elf 38 | 39 | pack: 40 | mkdir -p ./deb/ 41 | mkdir -p ./deb/usr/lib/ 42 | mkdir -p ./deb/usr/include/ 43 | mkdir -p ./deb/DEBIAN/ 44 | cp ./build/libmcpi.so ./deb/usr/lib/ 45 | cp ./src/mcpi.h ./deb/usr/include/ 46 | chmod 0755 ./deb/usr/lib/libmcpi.so 47 | @echo "Package: libmcpi" > ./deb/DEBIAN/control 48 | @echo "Version: 0.2.0" >> ./deb/DEBIAN/control 49 | @echo "Priority: optional" >> ./deb/DEBIAN/control 50 | @echo "Architecture: armhf" >> ./deb/DEBIAN/control 51 | @echo "Maintainer: Alvarito050506 " >> ./deb/DEBIAN/control 52 | @echo "Homepage: https://mcpirevival.tk" >> ./deb/DEBIAN/control 53 | @echo "Vcs-Browser: https://github.com/MCPI-Devs/libmcpi" >> ./deb/DEBIAN/control 54 | @echo "Vcs-Git: https://github.com/MCPI-Devs/libmcpi.git" >> ./deb/DEBIAN/control 55 | @echo "Description: C library for interacting with and extending the Minecraft Pi Protocol.\n" >> ./deb/DEBIAN/control 56 | dpkg-deb -b ./deb/ ./libmcpi_0.2.0-1.deb 57 | 58 | clean: 59 | rm -rf ./build/ 60 | rm -rf ./deb/ 61 | rm -f ./libmcpi_*-*.deb 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libmcpi 2 | 3 | A C library for interacting with and extending the Minecraft Pi Protocol. 4 | 5 | ## Getting Started 6 | ### Prerequisites 7 | To build and use the library, you need: 8 | + A C99+ compiler (gcc, clang, bcc, etc.). 9 | + A standard C library (libc, uClibc, etc.) 10 | + A GNU-compatible Makefile proccessor. 11 | 12 | ### Download 13 | If you are using a Raspberry Pi, you can download and install the Debian package from the _[releases](https://github.com/MCPI-Devs/libmcpi/releases)_ section. 14 | 15 | You can also install it from the Packagecloud Debian repository: 16 | ```shell 17 | # If you didn't add the repository yet 18 | curl -s https://packagecloud.io/install/repositories/Alvarito050506/mcpi-devs/script.deb.sh | sudo bash 19 | 20 | # Now the actual installation 21 | sudo apt-get install libmcpi 22 | ``` 23 | 24 | ## Build 25 | You can build, test and install the sources using `make`: 26 | ```bash 27 | make 28 | make test-local # For testing before install 29 | make install 30 | make test-global # For testing after install 31 | ``` 32 | 33 | You can also build the Debian package using `make`: 34 | ```bash 35 | make pack 36 | ``` 37 | 38 | ## Usage 39 | To use the API, you first need to include the `mcpi.h` header: 40 | ```c 41 | #include 42 | ``` 43 | 44 | You also need to link the library using the `-l` option: 45 | ``` 46 | gcc -lmcpi your_program.c 47 | ``` 48 | 49 | ## API 50 | ### `typedef struct mcpi_command_t` 51 | Represents a Minecraft Pi API call: 52 | ```c 53 | typedef struct mcpi_command_t 54 | { 55 | char package[16]; 56 | char name[32]; 57 | char args[16][32]; 58 | int argc; 59 | } mcpi_command_t; 60 | ``` 61 | 62 | ### `typedef struct mcpi_err_t` 63 | Represents a Minecraft Pi API syntax error: 64 | ```c 65 | typedef struct mcpi_err_t 66 | { 67 | char code; 68 | int pos; 69 | char chr; 70 | } mcpi_err_t; 71 | ``` 72 | 73 | ### `mcpi_command_t mcpi_parse_command(const char* string, mcpi_err_t* err)` 74 | Returns the parsed `string` as a `mcpi_command_t`. You should check the `err` output argument for errors. 75 | 76 | ## Licensing 77 | All the code of this project is licensed under the [GNU Lesser General Public License version 3.0](https://github.com/MCPI-Devs/libmcpi/blob/master/LICENSE) (LGPL-3.0). 78 | 79 | All the documentation of this project is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/) (CC BY-SA 4.0) license. 80 | 81 | ![CC BY-SA 4.0](https://i.creativecommons.org/l/by-sa/4.0/88x31.png) 82 | -------------------------------------------------------------------------------- /src/mcpi.c: -------------------------------------------------------------------------------- 1 | /* 2 | * mcpi.c 3 | * 4 | * Copyright 2020-2021 Alvarito050506 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation; version 3 of the License. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 18 | * MA 02110-1301, USA. 19 | * 20 | * Part of this code is based in code generated by re2c 1.1.1. 21 | * 22 | */ 23 | 24 | #include 25 | #include 26 | 27 | mcpi_command_t mcpi_parse_command(const char* string, mcpi_err_t* err) 28 | { 29 | char y; 30 | int i = 0, j = 0, k = 0; 31 | int arg_start = 0; 32 | int arg_end = 0; 33 | mcpi_command_t command; 34 | y = *string; 35 | switch (y) 36 | { 37 | case '(': 38 | case ')': 39 | case '.': 40 | case '{': 41 | case '}': 42 | case '[': 43 | case ']': 44 | case '=': 45 | case ',': 46 | goto error; 47 | default: 48 | goto package; 49 | } 50 | 51 | package: 52 | y = *++string; 53 | i++; 54 | switch (y) 55 | { 56 | case '(': 57 | case ')': 58 | case '{': 59 | case '}': 60 | case '[': 61 | case ']': 62 | case '=': 63 | case ',': 64 | goto error; 65 | case '.': 66 | command.package[i] = '\x00'; 67 | i = 0; 68 | goto func; 69 | default: 70 | command.package[i - 1] = *(string - 1); 71 | command.package[i] = y; 72 | goto package; 73 | } 74 | 75 | func: 76 | y = *++string; 77 | i++; 78 | switch (y) 79 | { 80 | case '(': 81 | arg_start = i + 1; 82 | arg_end = i + 1; 83 | command.name[i - 1] = '\x00'; 84 | goto arg; 85 | default: 86 | command.name[i - 1] = y; 87 | goto func; 88 | } 89 | 90 | arg: 91 | y = *++string; 92 | switch (y) 93 | { 94 | case '(': 95 | case '{': 96 | case '}': 97 | case '[': 98 | case ']': 99 | case '=': 100 | goto error; 101 | case ')': 102 | command.args[j][k] = '\x00'; 103 | goto final; 104 | case ',': 105 | goto comma; 106 | default: 107 | arg_end++; 108 | command.args[j][k] = y; 109 | k++; 110 | goto arg; 111 | } 112 | 113 | comma: 114 | y = *++string; 115 | switch (y) 116 | { 117 | case '(': 118 | case ')': 119 | case '.': 120 | case '{': 121 | case '}': 122 | case '[': 123 | case ']': 124 | case '=': 125 | case ',': 126 | goto error; 127 | default: 128 | y = *--string; 129 | command.args[j][k] = '\x00'; 130 | j++; 131 | k = 0; 132 | goto arg; 133 | } 134 | 135 | final: 136 | command.argc = j + 1; 137 | return command; 138 | 139 | error: 140 | err->code = 1; 141 | err->pos = i; 142 | err->chr = y; 143 | return command; 144 | } 145 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | --------------------------------------------------------------------------------