nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

control.c (2732B)


      1 #include "control.h"
      2 
      3 #include "readwrite.h"
      4 #include "open.h"
      5 #include "getln.h"
      6 #include "substdio.h"
      7 #ifdef USING_SKALIBS
      8 #include <skalibs/stralloc.h>
      9 //#include <skalibs/buffer.h>
     10 #else
     11 #include "stralloc.h"
     12 #endif
     13 #include "error.h"
     14 #include "alloc.h"
     15 #include "scan.h"
     16 
     17 static char inbuf[64];
     18 #ifdef USING_SKALIBS
     19 static stralloc line = STRALLOC_ZERO;
     20 static stralloc me = STRALLOC_ZERO;
     21 #else
     22 static stralloc line = {0};
     23 static stralloc me = {0};
     24 #endif
     25 static int meok = 0;
     26 
     27 static void striptrailingwhitespace(stralloc *sa)
     28 {
     29 	while (sa->len > 0)
     30 			switch(sa->s[sa->len - 1])
     31 				{
     32 					case '\n': case ' ': case '\t':
     33 							--sa->len;
     34 							break;
     35 					default:
     36 							return;
     37 				}
     38 }
     39 
     40 int control_init()
     41 {
     42 	int r;
     43 	r = control_readline(&me,"control/me");
     44 	if (r == 1) meok = 1;
     45 	return r;
     46 }
     47 
     48 int control_rldef(stralloc *sa, char *fn, int flagme, char *def)
     49 {
     50 	int r;
     51 	r = control_readline(sa,fn);
     52 	if (r) return r;
     53 	if (flagme) if (meok) return stralloc_copy(sa,&me) ? 1 : -1;
     54 	if (def) return stralloc_copys(sa,def) ? 1 : -1;
     55 	return r;
     56 }
     57 
     58 int control_readline(stralloc *sa, char *fn)
     59 {
     60 //#ifdef USING_SKALIBS
     61 //	buffer ss;
     62 //#else
     63 	substdio ss;
     64 //#endif
     65 	int fd;
     66 	int match;
     67 
     68 	fd = open_read(fn);
     69 	if (fd == -1) { if (errno == error_noent) return 0; return -1; }
     70 
     71 //#ifdef USING_SKALIBS
     72 //	buffer_init(&ss,buffer_read,fd,inbuf,sizeof(inbuf));
     73 //#else
     74 	substdio_fdbuf(&ss,read,fd,inbuf,sizeof(inbuf));
     75 //#endif
     76 
     77 	if (getln(&ss,sa,&match,'\n') == -1) { close(fd); return -1; }
     78 
     79 	striptrailingwhitespace(sa);
     80 	close(fd);
     81 	return 1;
     82 }
     83 
     84 int control_readint(int *i, char *fn)
     85 {
     86 	unsigned long u;
     87 	switch(control_readline(&line,fn))
     88 		{
     89 			case 0: return 0;
     90 			case -1: return -1;
     91 		}
     92 	if (!stralloc_0(&line)) return -1;
     93 	if (!scan_ulong(line.s,&u)) return 0;
     94 	*i = u;
     95 	return 1;
     96 }
     97 
     98 int control_readfile(stralloc *sa, char *fn, int flagme)
     99 {
    100 //#ifdef USING_SKALIBS
    101 //	buffer ss;
    102 //#else
    103 	substdio ss;
    104 //#endif
    105 	int fd;
    106 	int match;
    107 
    108 	if (!stralloc_copys(sa,"")) return -1;
    109 
    110 	fd = open_read(fn);
    111 	if (fd == -1) 
    112 		{
    113 			if (errno == error_noent)
    114 				{
    115 					if (flagme && meok)
    116 						{
    117 							if (!stralloc_copy(sa,&me)) return -1;
    118 							if (!stralloc_0(sa)) return -1;
    119 							return 1;
    120 						}
    121 					return 0;
    122 				}
    123 			return -1;
    124 		}
    125 
    126 //#ifdef USING_SKALIBS
    127 //	buffer_init(&ss,buffer_read,fd,inbuf,sizeof(inbuf));
    128 //#else
    129 	substdio_fdbuf(&ss,read,fd,inbuf,sizeof(inbuf));
    130 //#endif
    131 	for (;;)
    132 		{
    133 			if (getln(&ss,&line,&match,'\n') == -1) break;
    134 			if (!match && !line.len) { close(fd); return 1; }
    135 			striptrailingwhitespace(&line);
    136 			if (!stralloc_0(&line)) break;
    137 			if (line.s[0])
    138 					if (line.s[0] != '#')
    139 							if (!stralloc_cat(sa,&line)) break;
    140 			if (!match) { close(fd); return 1; }
    141 		}
    142 	close(fd);
    143 	return -1;
    144 }