]> git.armaanb.net Git - mmenu.git/commitdiff
Initial commit
authorArmaan Bhojwani <me@armaanb.net>
Sun, 9 May 2021 00:10:40 +0000 (20:10 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Sun, 9 May 2021 00:10:40 +0000 (20:10 -0400)
.gitignore [new file with mode: 0644]
LICENSE [new file with mode: 0644]
Makefile [new file with mode: 0644]
mmenu.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..7e022e9
--- /dev/null
@@ -0,0 +1 @@
+mmenu
diff --git a/LICENSE b/LICENSE
new file mode 100644 (file)
index 0000000..f3fa714
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2021 Armaan Bhojwani
+
+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.
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..40af46e
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,3 @@
+all:
+       ${CC} mmenu.c -o mmenu -lXm -lXt -lcurl ${LDFLAGS} -std=c99 ${CFLAGS} -Wall \
+               -Wextra
diff --git a/mmenu.c b/mmenu.c
new file mode 100644 (file)
index 0000000..bee2aae
--- /dev/null
+++ b/mmenu.c
@@ -0,0 +1,114 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include <Xm/Text.h>
+#include <curl/curl.h>
+
+struct MemoryStruct {
+  char *memory;
+  size_t size;
+};
+
+static size_t
+memback(void *contents, size_t size, size_t nmemb, void *userp)
+{
+  size_t realsize = size * nmemb;
+  struct MemoryStruct *mem = (struct MemoryStruct *)userp;
+  char *ptr = realloc(mem->memory, mem->size + realsize + 1);
+  if(!ptr) {
+    /* out of memory! */
+    printf("not enough memory (realloc returned NULL)\n");
+    return 0;
+  }
+  mem->memory = ptr;
+  memcpy(&(mem->memory[mem->size]), contents, realsize);
+  mem->size += realsize;
+  mem->memory[mem->size] = 0;
+  return realsize;
+}
+
+int
+main(int argc, char *argv[])
+{
+       // Initialize motif
+       Widget             toplevel;
+       XtAppContext       app;
+       Widget             text_w;
+       Arg                args[2];
+
+       XtSetLanguageProc (NULL, NULL, NULL);
+       toplevel = XtVaOpenApplication (&app, "Castle Menu", NULL, 0, &argc, argv,
+                       NULL, sessionShellWidgetClass, NULL);
+
+       // Get webpage
+       CURL *curl = curl_easy_init();
+       CURLcode res;
+
+       if (!curl) {
+               fprintf(stderr, "ERROR: Failed to initialize cURL\n");
+               exit(1);
+       }
+
+  struct MemoryStruct chunk;
+       chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
+       chunk.size = 0;    /* no data at this point */
+
+       curl_easy_setopt(curl, CURLOPT_URL,
+                       "https://nobilis.nobles.edu/skyworld/castlemenu.php");
+  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memback);
+  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
+  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
+
+       printf("INFO: Fetching wepage\n");
+       res = curl_easy_perform(curl);
+
+       if (res != CURLE_OK) {
+               fprintf(stderr,"ERROR: Could not fetch webpage\n%s\n",
+                               curl_easy_strerror(res));
+               exit(2);
+       }
+
+       curl_easy_cleanup(curl);
+       
+       // Parse HTML
+       printf("INFO: Parsing HTML\n");
+       bool intag = false;
+       char *outp = (char *) malloc(strlen(chunk.memory) + 1); // Realloc?
+       strcpy(outp, "");
+       for (int i = 345; i < strlen(chunk.memory); i++) {
+               char c = chunk.memory[i];
+               if (c == '<') intag = true;
+               if (!intag) strncat(outp, &c, 1);
+               if (c == '>') intag = false;
+       }
+       strncat(outp, "\0", 1);
+
+       char *nl = outp;
+       strcpy(nl, "");
+       int old = 0;
+       /* for (int i = 0; i < 255; i++) { */
+       /*      if (old != 0 && i > 0) { */
+       /*              for (int j = old; j <= i; j++) { */
+       /*                      strncat(nl, outp, i - old); */
+       /*              } */
+       /*      } */
+       /*      if (outp[i] == '\n') */
+       /*              old = i; */
+       /* } */
+
+       // Display
+       int n = 0;
+       XtSetArg(args[0], XmNvalue, outp); n++;
+       XtSetArg(args[1], XmNeditable, False); n++;
+       text_w = XmCreateText(toplevel, "text", args, n);
+       free(outp);
+
+       XtManageChild(text_w);
+       XtRealizeWidget(toplevel);
+       XtAppMainLoop(app);
+}