#include #include #include #include #define DATAFILE "/ggs/ggs.txt" /* ====================================================== Sri Vaishnava Sovereign View.c — FINAL - Flat-file CRUD (no MySQL) - Proper redirects (no 302 text issue) - Import from other Leela IDs - Dual render (large + mini) ====================================================== */ /* ---------- Redirect Helper ---------- */ static void redirect_to(const char *url){ printf("Status: 303 See Other\r\n"); printf("Location: %s\r\n", url); printf("Content-Type: text/html; charset=utf-8\r\n\r\n"); printf("", url); } /* ---------- HTML HELPERS ---------- */ static void html_header(const char *title){ printf("Content-Type: text/html\r\n\r\n"); printf(""); printf(""); printf("%s", title ? title : "Leela View"); printf(""); } static void html_footer(void){ printf("

← Back to Loom

"); printf(""); } /* ---------- FORM HELPERS ---------- */ static char* read_body(size_t *out_len){ const char *cl = getenv("CONTENT_LENGTH"); if(!cl){ *out_len=0; return NULL; } long n = strtol(cl,NULL,10); if(n<=0 || n>200000){ *out_len=0; return NULL; } char *b = malloc((size_t)n+1); size_t r = fread(b,1,(size_t)n,stdin); b[r]='\0'; *out_len=r; return b; } static char* form_get(const char* body, const char* key){ if(!body) return NULL; size_t klen=strlen(key); const char* p=body; while(p && *p){ const char* amp=strchr(p,'&'); size_t seglen= amp? (size_t)(amp-p):strlen(p); const char* eq=memchr(p,'=',seglen); if(eq && (size_t)(eq-p)==klen && strncmp(p,key,klen)==0){ size_t vlen=seglen-klen-1; char* v=malloc(vlen+1); memcpy(v,eq+1,vlen); v[vlen]='\0'; for(char *s=v; *s; s++) if(*s=='+') *s=' '; return v; } p= amp? amp+1:NULL; } return NULL; } /* ---------- FILE OPS ---------- */ static int load_grid(long id, int cells[256], char colors[10][32]){ FILE *f=fopen(DATAFILE,"r"); if(!f) return 0; char line[32768]; while(fgets(line,sizeof(line),f)){ long lid=strtol(line,NULL,10); if(lid==id){ char *tok=strtok(line,","); for(int i=0;i<256;i++){ tok=strtok(NULL,","); cells[i]=tok?atoi(tok):0; } for(int j=0;j<10;j++){ tok=strtok(NULL,","); if(tok && *tok && strcasecmp(tok,"NULL")) snprintf(colors[j],32,"%s",tok); else snprintf(colors[j],32,""); } fclose(f); return 1; } } fclose(f); return 0; } static void save_grid(long id, int cells[256], char colors[10][32]){ FILE *f=fopen(DATAFILE,"r"); if(!f) return; char *rows[5000]; int total=0; char line[32768]; while(fgets(line,sizeof(line),f)){ rows[total++]=strdup(line); } fclose(f); for(int i=0;iNo data for %ld

",id); return; } printf("",cls); for(int r=0;r<16;r++){ printf(""); for(int c=0;c<16;c++){ int v=cells[r*16+c]; const char *col = (v>=0 && v<10 && colors[v][0]) ? colors[v] : "#FFFFFF"; printf("", col); } printf(""); } printf("
"); } /* ---------- MAIN ---------- */ int main(void){ const char *qs=getenv("QUERY_STRING"); long id=1; if(qs && *qs){ char *p=strstr(qs,"id="); if(p) id=strtol(p+3,NULL,10); } size_t blen=0; const char *method=getenv("REQUEST_METHOD"); if(method && !strcmp(method,"POST")){ char *body=read_body(&blen); char *action=form_get(body,"action"); if(action && !strcmp(action,"Update")){ int cells[256]; char colors[10][32]; for(int i=0;i<256;i++){ char key[16]; sprintf(key,"cell_%d",i+1); char *v=form_get(body,key); cells[i]=v?atoi(v):0; free(v); } for(int j=0;j<10;j++){ char key[16]; sprintf(key,"color%d",j); char *v=form_get(body,key); snprintf(colors[j],32,"%s",v?v:""); free(v); } save_grid(id,cells,colors); char to[128]; snprintf(to,sizeof(to),"/cgi-bin/view.cgi?id=%ld", id); redirect_to(to); free(action); free(body); return 0; } else if(action && !strcmp(action,"Import")){ char *imp=form_get(body,"import_id"); long src=imp?strtol(imp,NULL,10):0; free(imp); if(src>0) import_grid(id,src); char to[128]; snprintf(to,sizeof(to),"/cgi-bin/view.cgi?id=%ld", id); redirect_to(to); free(action); free(body); return 0; } free(action); free(body); } /* VIEW PAGE */ html_header("Leela View"); printf("

Leela %ld

",id); printf("
"); printf("
"); render_grid(id,"big"); printf("
"); printf("
"); render_grid(id,"mini"); printf("
"); printf("

"); int cells[256]; char colors[10][32]; if(!load_grid(id,cells,colors)){ printf("

No data for %ld

",id); html_footer(); return 0; } printf("
",id); for(int r=0;r<16;r++){ for(int c=0;c<16;c++){ int idx=r*16+c; printf("",idx+1,cells[idx]); } printf("
"); } printf("

"); for(int j=0;j<10;j++) printf("Color %d: ",j,j,colors[j]); printf("

"); printf(""); printf("

"); printf("

Import data from ID: "); printf("

"); printf("
"); html_footer(); return 0; }