]> git.armaanb.net Git - mmenu.git/blob - mmenu.c
Initial commit
[mmenu.git] / mmenu.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdbool.h>
4 #include <string.h>
5
6 #include <Xm/Text.h>
7 #include <curl/curl.h>
8
9 struct MemoryStruct {
10   char *memory;
11   size_t size;
12 };
13
14 static size_t
15 memback(void *contents, size_t size, size_t nmemb, void *userp)
16 {
17   size_t realsize = size * nmemb;
18   struct MemoryStruct *mem = (struct MemoryStruct *)userp;
19  
20   char *ptr = realloc(mem->memory, mem->size + realsize + 1);
21   if(!ptr) {
22     /* out of memory! */
23     printf("not enough memory (realloc returned NULL)\n");
24     return 0;
25   }
26  
27   mem->memory = ptr;
28   memcpy(&(mem->memory[mem->size]), contents, realsize);
29   mem->size += realsize;
30   mem->memory[mem->size] = 0;
31  
32   return realsize;
33 }
34
35 int
36 main(int argc, char *argv[])
37 {
38         // Initialize motif
39         Widget             toplevel;
40         XtAppContext       app;
41         Widget             text_w;
42         Arg                args[2];
43
44         XtSetLanguageProc (NULL, NULL, NULL);
45         toplevel = XtVaOpenApplication (&app, "Castle Menu", NULL, 0, &argc, argv,
46                         NULL, sessionShellWidgetClass, NULL);
47
48         // Get webpage
49         CURL *curl = curl_easy_init();
50         CURLcode res;
51
52         if (!curl) {
53                 fprintf(stderr, "ERROR: Failed to initialize cURL\n");
54                 exit(1);
55         }
56
57   struct MemoryStruct chunk;
58         chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */
59         chunk.size = 0;    /* no data at this point */
60
61         curl_easy_setopt(curl, CURLOPT_URL,
62                         "https://nobilis.nobles.edu/skyworld/castlemenu.php");
63   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memback);
64   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
65   curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
66
67         printf("INFO: Fetching wepage\n");
68         res = curl_easy_perform(curl);
69
70         if (res != CURLE_OK) {
71                 fprintf(stderr,"ERROR: Could not fetch webpage\n%s\n",
72                                 curl_easy_strerror(res));
73                 exit(2);
74         }
75
76         curl_easy_cleanup(curl);
77         
78         // Parse HTML
79         printf("INFO: Parsing HTML\n");
80         bool intag = false;
81         char *outp = (char *) malloc(strlen(chunk.memory) + 1); // Realloc?
82         strcpy(outp, "");
83         for (int i = 345; i < strlen(chunk.memory); i++) {
84                 char c = chunk.memory[i];
85                 if (c == '<') intag = true;
86                 if (!intag) strncat(outp, &c, 1);
87                 if (c == '>') intag = false;
88         }
89         strncat(outp, "\0", 1);
90
91         char *nl = outp;
92         strcpy(nl, "");
93         int old = 0;
94         /* for (int i = 0; i < 255; i++) { */
95         /*      if (old != 0 && i > 0) { */
96         /*              for (int j = old; j <= i; j++) { */
97         /*                      strncat(nl, outp, i - old); */
98         /*              } */
99         /*      } */
100         /*      if (outp[i] == '\n') */
101         /*              old = i; */
102         /* } */
103
104         // Display
105         int n = 0;
106         XtSetArg(args[0], XmNvalue, outp); n++;
107         XtSetArg(args[1], XmNeditable, False); n++;
108         text_w = XmCreateText(toplevel, "text", args, n);
109         free(outp);
110
111         XtManageChild(text_w);
112         XtRealizeWidget(toplevel);
113         XtAppMainLoop(app);
114 }