ref: 57a3cb892d1df2f3787531e0ead84c8e943744ea
dir: /conf.c/
#include <u.h> #include <libc.h> #include <ctype.h> #include <bio.h> #include "pro.h" static char* strip(char *s) { char *e; while(isspace(*s)) s++; e = s + strlen(s); while(e > s && isspace(*--e)) *e = 0; return s; } static char* wd2sfile(void) { char pathname[512]; char *pr; if(getwd(pathname, sizeof(pathname)) == 0) return "/srv/work"; if((pr = utfrrune(pathname, '/'))) return strcat("/srv", pr); return "/srv/work"; } static char** fromCSV(char *s, int *nfields) { char **fields; int i, cap, nf; char *token; cap = 1; nf = 0; fields = malloc(sizeof(char*)); if(!fields) return nil; token = strtok(s, ","); while(token){ if(nf >= cap){ cap *= 2; char **tmp = realloc(fields, cap * sizeof(char*)); if(!tmp) goto Fail; fields = tmp; } fields[nf] = strdup(token); if(!fields[nf]) goto Fail; nf++; token = strtok(nil, ","); } *nfields = nf; return fields; Fail: for(i = 0; i < nf; i++) free(fields[i]); free(fields); werrstr("failed to parse csv"); return nil; } void freeconf(Proconf *conf) { int i; if(!conf) return; if(conf->stubs){ for(i = 0; i < conf->nstub; i++) free(conf->stubs[i]); free(conf->stubs); } free(conf); } Proconf* pconf(void) { Proconf *conf; Biobuf *f; char *ln, *p; if((f = Bopen(".pro/config", OREAD)) == nil) return nil; conf = malloc(sizeof(Proconf)); conf->nstub = 0; conf->stubs = nil; while((ln = Brdstr(f, '\n', 1)) != nil){ p = strip(ln); if(strncmp(p, "name", 4) == 0){ print("Into Sfile\n"); p = strip(p + 4); if(*p == '='){ p = strip(p + 1); conf->sfile = strcat("/srv/", p); } print("Out of Sfile\n"); } if(strncmp(p, "stubs", 5) == 0){ print("Stubby\n"); p = strip(p + 5); if(*p == '='){ p = strip(p + 1); conf->stubs = fromCSV(p, &conf->nstub); if(!conf->stubs){ Bterm(f); freeconf(conf); free(ln); return nil; } } print("Out of stubby\n"); } free(ln); } Bterm(f); if(!conf->sfile) conf->sfile = wd2sfile(); return conf; }