| 1 | #include "stralloc.h" |
| 2 | #include "slurp.h" |
| 3 | #include "strerr.h" |
| 4 | #include "getconf.h" |
| 5 | |
| 6 | static stralloc data = {0}; |
| 7 | |
| 8 | void nomem(fatal) |
| 9 | char *fatal; |
| 10 | { |
| 11 | strerr_die2x(111,fatal,"out of memory"); |
| 12 | } |
| 13 | |
| 14 | int getconf(sa,fn,flagrequired,fatal,dir) |
| 15 | stralloc *sa; |
| 16 | char *fatal; |
| 17 | int flagrequired; |
| 18 | char *dir; |
| 19 | char *fn; |
| 20 | { |
| 21 | int i; |
| 22 | int j; |
| 23 | int k; |
| 24 | |
| 25 | if (!stralloc_copys(&data,"")) |
| 26 | nomem(fatal); |
| 27 | switch(slurp(fn,&data,128)) { |
| 28 | case -1: |
| 29 | strerr_die6sys(111,fatal,"unable to read ",dir,"/",fn,": "); |
| 30 | case 0: |
| 31 | if (!flagrequired) |
| 32 | return 0; |
| 33 | strerr_die5x(100,fatal,dir,"/",fn," does not exist"); |
| 34 | } |
| 35 | if (!stralloc_append(&data,"\n")) nomem(fatal); |
| 36 | if (!stralloc_copys(sa,"")) nomem(fatal); |
| 37 | i = 0; |
| 38 | for (j = 0;j < data.len;++j) |
| 39 | if (data.s[j] == '\n') { |
| 40 | k = j; |
| 41 | while ((k > i) && ((data.s[k-1] == ' ') || (data.s[k-1] == '\t'))) --k; |
| 42 | if ((k > i) && (data.s[i] != '#')) { |
| 43 | if (!stralloc_catb(sa,data.s + i,k - i)) nomem(fatal); |
| 44 | if (!stralloc_0(sa)) nomem(fatal); |
| 45 | } |
| 46 | i = j + 1; |
| 47 | } |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | int getconf_line(sa,fn,flagrequired,fatal,dir) |
| 52 | stralloc *sa; |
| 53 | char *fatal; |
| 54 | int flagrequired; |
| 55 | char *dir; |
| 56 | char *fn; |
| 57 | { |
| 58 | if (!getconf(sa,fn,flagrequired,fatal,dir)) return 0; |
| 59 | sa->len = byte_chr(sa->s,sa->len,0); |
| 60 | return 1; |
| 61 | } |