Mon, 16 Feb 2004 18:50:28 +0100
trunk: changeset 1318
Switched to using spaces only for indentation. Simple automatic "\t"
-> " " conversion; may need tuning later.
--- a/dlist.h Mon Feb 16 18:09:03 2004 +0100 +++ b/dlist.h Mon Feb 16 18:50:28 2004 +0100 @@ -11,66 +11,66 @@ #define LIBTU_DLIST_H #define LINK_ITEM(LIST, ITEM, NEXT, PREV) \ - (ITEM)->NEXT=NULL; \ - if((LIST)==NULL){ \ - (LIST)=(ITEM); \ - (ITEM)->PREV=(ITEM); \ - }else{ \ - (ITEM)->PREV=(LIST)->PREV; \ - (ITEM)->PREV->NEXT=(ITEM); \ - (LIST)->PREV=(ITEM); \ - } + (ITEM)->NEXT=NULL; \ + if((LIST)==NULL){ \ + (LIST)=(ITEM); \ + (ITEM)->PREV=(ITEM); \ + }else{ \ + (ITEM)->PREV=(LIST)->PREV; \ + (ITEM)->PREV->NEXT=(ITEM); \ + (LIST)->PREV=(ITEM); \ + } #define LINK_ITEM_FIRST(LIST, ITEM, NEXT, PREV) \ - (ITEM)->NEXT=(LIST); \ - if((LIST)==NULL){ \ - (ITEM)->PREV=(ITEM); \ - }else{ \ - (ITEM)->PREV=(LIST)->PREV; \ - (LIST)->PREV=(ITEM); \ - } \ - (LIST)=(ITEM); + (ITEM)->NEXT=(LIST); \ + if((LIST)==NULL){ \ + (ITEM)->PREV=(ITEM); \ + }else{ \ + (ITEM)->PREV=(LIST)->PREV; \ + (LIST)->PREV=(ITEM); \ + } \ + (LIST)=(ITEM); #define LINK_ITEM_LIST LINK_ITEM #define LINK_ITEM_BEFORE(LIST, BEFORE, ITEM, NEXT, PREV) \ - (ITEM)->NEXT=(BEFORE); \ - (ITEM)->PREV=(BEFORE)->PREV; \ - (BEFORE)->PREV=(ITEM); \ - if((BEFORE)==(LIST)) \ - (LIST)=(ITEM); \ - else \ - (ITEM)->PREV->NEXT=(ITEM) + (ITEM)->NEXT=(BEFORE); \ + (ITEM)->PREV=(BEFORE)->PREV; \ + (BEFORE)->PREV=(ITEM); \ + if((BEFORE)==(LIST)) \ + (LIST)=(ITEM); \ + else \ + (ITEM)->PREV->NEXT=(ITEM) #define LINK_ITEM_AFTER(LIST, AFTER, ITEM, NEXT, PREV) \ - (ITEM)->NEXT=(AFTER)->NEXT; \ - (ITEM)->PREV=(AFTER); \ - (AFTER)->NEXT=(ITEM); \ - if((ITEM)->NEXT==NULL) \ - (LIST)->PREV=(ITEM); \ - else \ - (ITEM)->NEXT->PREV=ITEM; + (ITEM)->NEXT=(AFTER)->NEXT; \ + (ITEM)->PREV=(AFTER); \ + (AFTER)->NEXT=(ITEM); \ + if((ITEM)->NEXT==NULL) \ + (LIST)->PREV=(ITEM); \ + else \ + (ITEM)->NEXT->PREV=ITEM; #define UNLINK_ITEM(LIST, ITEM, NEXT, PREV) \ - if((ITEM)->PREV!=NULL){ \ - if((ITEM)==(LIST)){ \ - (LIST)=(ITEM)->NEXT; \ - if((LIST)!=NULL) \ - (LIST)->PREV=(ITEM)->PREV; \ - }else if((ITEM)->NEXT==NULL){ \ - (ITEM)->PREV->NEXT=NULL; \ - (LIST)->PREV=(ITEM)->PREV; \ - }else{ \ - (ITEM)->PREV->NEXT=(ITEM)->NEXT; \ - (ITEM)->NEXT->PREV=(ITEM)->PREV; \ - } \ - } \ - (ITEM)->NEXT=NULL; \ - (ITEM)->PREV=NULL; + if((ITEM)->PREV!=NULL){ \ + if((ITEM)==(LIST)){ \ + (LIST)=(ITEM)->NEXT; \ + if((LIST)!=NULL) \ + (LIST)->PREV=(ITEM)->PREV; \ + }else if((ITEM)->NEXT==NULL){ \ + (ITEM)->PREV->NEXT=NULL; \ + (LIST)->PREV=(ITEM)->PREV; \ + }else{ \ + (ITEM)->PREV->NEXT=(ITEM)->NEXT; \ + (ITEM)->NEXT->PREV=(ITEM)->PREV; \ + } \ + } \ + (ITEM)->NEXT=NULL; \ + (ITEM)->PREV=NULL; #endif /* LIBTU_DLIST_H */
--- a/errorlog.c Mon Feb 16 18:09:03 2004 +0100 +++ b/errorlog.c Mon Feb 16 18:50:28 2004 +0100 @@ -21,107 +21,107 @@ static void add_to_log(ErrorLog *el, const char *message, int l) { - if(message==NULL) - return; - - /* Also write to stderr */ - fwrite(message, sizeof(char), l, stderr); + if(message==NULL) + return; + + /* Also write to stderr */ + fwrite(message, sizeof(char), l, stderr); - if(el==NULL) - return; - - if(el->file!=NULL){ - el->errors=TRUE; - fwrite(message, sizeof(char), l, el->file); - return; - } + if(el==NULL) + return; + + if(el->file!=NULL){ + el->errors=TRUE; + fwrite(message, sizeof(char), l, el->file); + return; + } - if(el->msgs==NULL){ - el->msgs=ALLOC_N(char, ERRORLOG_MAX_SIZE); - if(el->msgs==NULL){ - fprintf(stderr, "%s: %s\n", prog_execname(), strerror(errno)); - return; - } - el->msgs[0]=0; - el->msgs_len=0; - } - - el->errors=TRUE; - - if(l+el->msgs_len>ERRORLOG_MAX_SIZE-1){ - int n=0; - if(l<ERRORLOG_MAX_SIZE-1){ - n=ERRORLOG_MAX_SIZE-1-l; - memmove(el->msgs, el->msgs+el->msgs_len-n, n); - } - memcpy(el->msgs+n, message+l-(ERRORLOG_MAX_SIZE-1-n), - ERRORLOG_MAX_SIZE-1-n); - el->msgs[ERRORLOG_MAX_SIZE]='\0'; - el->msgs_len=ERRORLOG_MAX_SIZE-1; - }else{ - memcpy(el->msgs+el->msgs_len, message, l); - el->msgs[el->msgs_len+l]='\0'; - el->msgs_len+=l; - } + if(el->msgs==NULL){ + el->msgs=ALLOC_N(char, ERRORLOG_MAX_SIZE); + if(el->msgs==NULL){ + fprintf(stderr, "%s: %s\n", prog_execname(), strerror(errno)); + return; + } + el->msgs[0]=0; + el->msgs_len=0; + } + + el->errors=TRUE; + + if(l+el->msgs_len>ERRORLOG_MAX_SIZE-1){ + int n=0; + if(l<ERRORLOG_MAX_SIZE-1){ + n=ERRORLOG_MAX_SIZE-1-l; + memmove(el->msgs, el->msgs+el->msgs_len-n, n); + } + memcpy(el->msgs+n, message+l-(ERRORLOG_MAX_SIZE-1-n), + ERRORLOG_MAX_SIZE-1-n); + el->msgs[ERRORLOG_MAX_SIZE]='\0'; + el->msgs_len=ERRORLOG_MAX_SIZE-1; + }else{ + memcpy(el->msgs+el->msgs_len, message, l); + el->msgs[el->msgs_len+l]='\0'; + el->msgs_len+=l; + } } static void log_warn_handler(const char *message) { - const char *p=strchr(message, '\n'); - static int lineno=0; - int alternat=0; - - add_to_log(current_log, lineno==0 ? ">> " : " ", 3); - - if(p!=NULL){ - add_to_log(current_log, message, p-message+1); - lineno++; - log_warn_handler(p+1); - lineno--; - return; - } - - add_to_log(current_log, message, strlen(message)); - add_to_log(current_log, "\n", 1); + const char *p=strchr(message, '\n'); + static int lineno=0; + int alternat=0; + + add_to_log(current_log, lineno==0 ? ">> " : " ", 3); + + if(p!=NULL){ + add_to_log(current_log, message, p-message+1); + lineno++; + log_warn_handler(p+1); + lineno--; + return; + } + + add_to_log(current_log, message, strlen(message)); + add_to_log(current_log, "\n", 1); } - + void errorlog_begin_file(ErrorLog *el, FILE *file) { - el->msgs=NULL; - el->msgs_len=0; - el->file=file; - el->prev=current_log; - el->errors=FALSE; - el->old_handler=set_warn_handler(log_warn_handler); - current_log=el; + el->msgs=NULL; + el->msgs_len=0; + el->file=file; + el->prev=current_log; + el->errors=FALSE; + el->old_handler=set_warn_handler(log_warn_handler); + current_log=el; } void errorlog_begin(ErrorLog *el) { - errorlog_begin_file(el, NULL); + errorlog_begin_file(el, NULL); } bool errorlog_end(ErrorLog *el) { - current_log=el->prev; - set_warn_handler(el->old_handler); - el->prev=NULL; - el->old_handler=NULL; - return el->errors; + current_log=el->prev; + set_warn_handler(el->old_handler); + el->prev=NULL; + el->old_handler=NULL; + return el->errors; } void errorlog_deinit(ErrorLog *el) { - if(el->msgs!=NULL) - free(el->msgs); - el->msgs=NULL; - el->msgs_len=0; - el->file=NULL; - el->errors=FALSE; + if(el->msgs!=NULL) + free(el->msgs); + el->msgs=NULL; + el->msgs_len=0; + el->file=NULL; + el->errors=FALSE; }
--- a/errorlog.h Mon Feb 16 18:09:03 2004 +0100 +++ b/errorlog.h Mon Feb 16 18:50:28 2004 +0100 @@ -20,12 +20,12 @@ INTRSTRUCT(ErrorLog); DECLSTRUCT(ErrorLog){ - char *msgs; - int msgs_len; - FILE *file; - bool errors; - ErrorLog *prev; - WarnHandler *old_handler; + char *msgs; + int msgs_len; + FILE *file; + bool errors; + ErrorLog *prev; + WarnHandler *old_handler; }; /* el is assumed to be uninitialised */
--- a/map.c Mon Feb 16 18:09:03 2004 +0100 +++ b/map.c Mon Feb 16 18:50:28 2004 +0100 @@ -14,19 +14,19 @@ int stringintmap_ndx(const StringIntMap *map, const char *str) { int i; - - for(i=0; map[i].string!=NULL; i++){ - if(strcmp(str, map[i].string)==0) - return i; - } - - return -1; + + for(i=0; map[i].string!=NULL; i++){ + if(strcmp(str, map[i].string)==0) + return i; + } + + return -1; } int stringintmap_value(const StringIntMap *map, const char *str, int dflt) { int i=stringintmap_ndx(map, str); - return (i==-1 ? dflt : map[i].value); + return (i==-1 ? dflt : map[i].value); }
--- a/map.h Mon Feb 16 18:09:03 2004 +0100 +++ b/map.h Mon Feb 16 18:50:28 2004 +0100 @@ -11,8 +11,8 @@ #define LIBTU_MAP_H typedef struct _StringIntMap{ - const char *string; - int value; + const char *string; + int value; } StringIntMap; #define END_STRINGINTMAP {NULL, 0} @@ -20,6 +20,6 @@ /* Return the index of str in map or -1 if not found. */ extern int stringintmap_ndx(const StringIntMap *map, const char *str); extern int stringintmap_value(const StringIntMap *map, const char *str, - int dflt); + int dflt); #endif /* LIBTU_MAP_H */
--- a/minmax.h Mon Feb 16 18:09:03 2004 +0100 +++ b/minmax.h Mon Feb 16 18:50:28 2004 +0100 @@ -13,13 +13,13 @@ static int minof(int x, int y) { - return (x<y ? x : y); + return (x<y ? x : y); } static int maxof(int x, int y) { - return (x>y ? x : y); + return (x>y ? x : y); }
--- a/misc.c Mon Feb 16 18:09:03 2004 +0100 +++ b/misc.c Mon Feb 16 18:50:28 2004 +0100 @@ -16,114 +16,114 @@ void *malloczero(size_t size) { - void *p=malloc(size); - - if(p!=NULL) - memset(p, 0, size); - - return p; + void *p=malloc(size); + + if(p!=NULL) + memset(p, 0, size); + + return p; } void *remalloczero(void *ptr, size_t oldsize, size_t newsize) { - void *p=NULL; - - if(newsize!=0){ - p=realloc(ptr, newsize); - - if(p==NULL) - return NULL; - - if(newsize>oldsize) - memset((char*)p+oldsize, 0, newsize-oldsize); - } + void *p=NULL; + + if(newsize!=0){ + p=realloc(ptr, newsize); + + if(p==NULL) + return NULL; + + if(newsize>oldsize) + memset((char*)p+oldsize, 0, newsize-oldsize); + } - return p; + return p; } char *scopy(const char *p) { - char*pn; - size_t l=strlen(p); - - pn=(char*)malloc(l+1); - - if(pn==NULL) - return NULL; - - memcpy(pn, p, l+1); - - return pn; + char*pn; + size_t l=strlen(p); + + pn=(char*)malloc(l+1); + + if(pn==NULL) + return NULL; + + memcpy(pn, p, l+1); + + return pn; } - - + + char *scat(const char *p1, const char *p2) { - size_t l1, l2; - char*pn; - - l1=strlen(p1); - l2=strlen(p2); - - pn=(char*)malloc(l1+l2+1); - - if(pn==NULL) - return NULL; - - memcpy(pn, p1, l1); - memcpy(pn+l1, p2, l2+1); - - return pn; + size_t l1, l2; + char*pn; + + l1=strlen(p1); + l2=strlen(p2); + + pn=(char*)malloc(l1+l2+1); + + if(pn==NULL) + return NULL; + + memcpy(pn, p1, l1); + memcpy(pn+l1, p2, l2+1); + + return pn; } char *scat3(const char *p1, const char *p2, const char *p3) { - size_t l1, l2, l3; - char *pn; - - l1=strlen(p1); - l2=strlen(p2); - l3=strlen(p3); - - pn=(char*)malloc(l1+l2+l3+1); - - if(pn==NULL) - return NULL; - - memcpy(pn, p1, l1); - memcpy(pn+l1, p2, l2); - memcpy(pn+l1+l2, p3, l3+1); - - return pn; + size_t l1, l2, l3; + char *pn; + + l1=strlen(p1); + l2=strlen(p2); + l3=strlen(p3); + + pn=(char*)malloc(l1+l2+l3+1); + + if(pn==NULL) + return NULL; + + memcpy(pn, p1, l1); + memcpy(pn+l1, p2, l2); + memcpy(pn+l1+l2, p3, l3+1); + + return pn; } char *scatn(const char *s1, ssize_t l1, const char *s2, ssize_t l2) { - size_t tlen=1; - char *s; - - if(l1<0) - l1=strlen(s1); - - if(l2<0) - l2=strlen(s2); - - tlen+=l1+l2; - - s=(char*)malloc(tlen); - - if(s==NULL) - return NULL; - - memcpy(s, s1, l1); - memcpy(s+l1, s2, l2); - s[l1+l2]='\0'; - - return s; + size_t tlen=1; + char *s; + + if(l1<0) + l1=strlen(s1); + + if(l2<0) + l2=strlen(s2); + + tlen+=l1+l2; + + s=(char*)malloc(tlen); + + if(s==NULL) + return NULL; + + memcpy(s, s1, l1); + memcpy(s+l1, s2, l2); + s[l1+l2]='\0'; + + return s; } @@ -132,42 +132,42 @@ const char *simple_basename(const char *name) { - const char *p; - - p=name+strlen(name)-1; - - /* Skip any trailing slashes */ - while(*p=='/'){ - /* root? */ - if(p==name) - return name; - p--; - } - - while(p!=name){ - if(*p=='/') - return p+1; - p--; - } - - return name; + const char *p; + + p=name+strlen(name)-1; + + /* Skip any trailing slashes */ + while(*p=='/'){ + /* root? */ + if(p==name) + return name; + p--; + } + + while(p!=name){ + if(*p=='/') + return p+1; + p--; + } + + return name; } void stripws(char *p) { - int l; - - l=strspn(p, " "); - if(l!=0) - strcpy(p, p+l); - l=strlen(p); - - while(--l>=0){ - if(*(p+l)!=' ') - break; - } - *(p+l+1)='\0'; + int l; + + l=strspn(p, " "); + if(l!=0) + strcpy(p, p+l); + l=strlen(p); + + while(--l>=0){ + if(*(p+l)!=' ') + break; + } + *(p+l+1)='\0'; } @@ -176,11 +176,11 @@ bool readf(FILE *f, void *buf, size_t n) { - return fread(buf, 1, n, f)!=1; + return fread(buf, 1, n, f)!=1; } bool writef(FILE *f, const void *buf, size_t n) { - return fwrite(buf, 1, n, f)!=1; + return fwrite(buf, 1, n, f)!=1; }
--- a/np/np-conv.h Mon Feb 16 18:09:03 2004 +0100 +++ b/np/np-conv.h Mon Feb 16 18:50:28 2004 +0100 @@ -14,44 +14,44 @@ #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ { \ - if(num->type!=NPNUM_INT) \ - return E_TOKZ_NOTINT; \ - \ - if(!num->negative){ \ - *ret=num->ival; \ - if(allow_uns_big?num->ival>UMAX:num->ival>MAX) \ - return E_TOKZ_RANGE; \ - }else{ \ - *ret=-num->ival; \ - if(num->ival>-(ulong)MIN) \ - return E_TOKZ_RANGE; \ - } \ - return 0; \ + if(num->type!=NPNUM_INT) \ + return E_TOKZ_NOTINT; \ + \ + if(!num->negative){ \ + *ret=num->ival; \ + if(allow_uns_big?num->ival>UMAX:num->ival>MAX) \ + return E_TOKZ_RANGE; \ + }else{ \ + *ret=-num->ival; \ + if(num->ival>-(ulong)MIN) \ + return E_TOKZ_RANGE; \ + } \ + return 0; \ } #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ { \ - if(num->type!=NPNUM_INT) \ - return E_TOKZ_NOTINT; \ - \ - if(!num->negative){ \ - *ret=num->ival; \ - if(num->ival>UMAX) \ - return E_TOKZ_RANGE; \ - }else{ \ - *ret=-num->ival; \ - if(!allow_neg || num->ival>(ulong)-MIN) \ - return E_TOKZ_RANGE; \ - } \ - return 0; \ + if(num->type!=NPNUM_INT) \ + return E_TOKZ_NOTINT; \ + \ + if(!num->negative){ \ + *ret=num->ival; \ + if(num->ival>UMAX) \ + return E_TOKZ_RANGE; \ + }else{ \ + *ret=-num->ival; \ + if(!allow_neg || num->ival>(ulong)-MIN) \ + return E_TOKZ_RANGE; \ + } \ + return 0; \ } #define FN_NUM_TO_FLOAT(T, POW) \ static int num_to_##T(T *ret, const NPNum *num) \ { \ - *ret=(num->negative?-num->fval:num->fval); \ - return 0; \ + *ret=(num->negative?-num->fval:num->fval); \ + return 0; \ } #else /* NP_SIMPLE_IMPL */ @@ -59,57 +59,57 @@ #define FN_NUM_TO_SIGNED(T, UMAX, MAX, MIN) \ static int num_to_##T(T *ret, const NPNum *num, bool allow_uns_big) \ { \ - if(num->exponent) \ - return E_TOKZ_NOTINT; \ - if(num->nmantissa>0) \ - return E_TOKZ_RANGE; \ + if(num->exponent) \ + return E_TOKZ_NOTINT; \ + if(num->nmantissa>0) \ + return E_TOKZ_RANGE; \ \ - if(!num->negative){ \ - *ret=num->mantissa[0]; \ - if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \ - return E_TOKZ_RANGE; \ - }else{ \ - *ret=-num->mantissa[0]; \ - if(num->mantissa[0]>-(ulong)MIN) \ - return E_TOKZ_RANGE; \ - } \ - return 0; \ + if(!num->negative){ \ + *ret=num->mantissa[0]; \ + if(allow_uns_big?num->mantissa[0]>UMAX:num->mantissa[0]>MAX) \ + return E_TOKZ_RANGE; \ + }else{ \ + *ret=-num->mantissa[0]; \ + if(num->mantissa[0]>-(ulong)MIN) \ + return E_TOKZ_RANGE; \ + } \ + return 0; \ } #define FN_NUM_TO_UNSIGNED(T, UMAX, MIN) \ static int num_to_##T(T *ret, const NPNum *num, bool allow_neg) \ { \ - if(num->exponent) \ - return E_TOKZ_NOTINT; \ - if(num->nmantissa>0) \ - return E_TOKZ_RANGE; \ + if(num->exponent) \ + return E_TOKZ_NOTINT; \ + if(num->nmantissa>0) \ + return E_TOKZ_RANGE; \ \ - if(!num->negative){ \ - *ret=num->mantissa[0]; \ - if(num->mantissa[0]>UMAX) \ - return E_TOKZ_RANGE; \ - }else{ \ - *ret=-num->mantissa[0]; \ - if(!allow_neg || num->mantissa[0]>(ulong)-MIN) \ - return E_TOKZ_RANGE; \ - } \ - return 0; \ + if(!num->negative){ \ + *ret=num->mantissa[0]; \ + if(num->mantissa[0]>UMAX) \ + return E_TOKZ_RANGE; \ + }else{ \ + *ret=-num->mantissa[0]; \ + if(!allow_neg || num->mantissa[0]>(ulong)-MIN) \ + return E_TOKZ_RANGE; \ + } \ + return 0; \ } #define FN_NUM_TO_FLOAT(T, POW) \ static int num_to_##T(T *ret, const NPNum *num) \ { \ - T d=0; \ - int i; \ - \ - for(i=num->nmantissa;i>=0;i--) \ - d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ + T d=0; \ + int i; \ \ - d*=POW(num->base, num->exponent); \ - *ret=d; \ - \ - return 0; \ + for(i=num->nmantissa;i>=0;i--) \ + d=d*(T)(ULONG_MAX+1.0)+num->mantissa[i]; \ + \ + d*=POW(num->base, num->exponent); \ + *ret=d; \ + \ + return 0; \ } #endif /* NP_SIMPLE_IMPL */
--- a/np/numparser2.h Mon Feb 16 18:09:03 2004 +0100 +++ b/np/numparser2.h Mon Feb 16 18:50:28 2004 +0100 @@ -11,47 +11,47 @@ #define ULONG_SIZE (sizeof(ulong)*8) enum{ - NPNUM_INT, - NPNUM_FLOAT + NPNUM_INT, + NPNUM_FLOAT }; #ifdef NP_SIMPLE_IMPL typedef struct _NPNum{ - int type; - int base; - bool negative; - double fval; - ulong ival; + int type; + int base; + bool negative; + double fval; + ulong ival; } NPNum; #define NUM_INIT {0, 0, 0, 0.0, 0} static int npnum_mulbase_add(NPNum *num, long base, long v) { - double iold=num->ival; - - num->fval=num->fval*base+(double)v; - - num->ival*=base; - - if(num->ival<iold) - num->type=NPNUM_FLOAT; - - num->ival+=v; - - return 0; + double iold=num->ival; + + num->fval=num->fval*base+(double)v; + + num->ival*=base; + + if(num->ival<iold) + num->type=NPNUM_FLOAT; + + num->ival+=v; + + return 0; } #else /* NP_SIMPLE_IMPL */ typedef struct _NPNum{ - unsigned char nmantissa; - int type; - int base; - bool negative; - ulong mantissa[MAX_MANTISSA]; - long exponent; + unsigned char nmantissa; + int type; + int base; + bool negative; + ulong mantissa[MAX_MANTISSA]; + long exponent; } NPNum; #define NUM_INIT {0, 0, 0, 0, {0,}, 0} @@ -64,48 +64,48 @@ static int npnum_mulbase_add(NPNum *num, long base, long v) { - long i, j; - ulong overflow; + long i, j; + ulong overflow; #ifndef NP_I386_ASM - ulong val; + ulong val; #endif - - for(i=num->nmantissa;i>=0;i--){ + + for(i=num->nmantissa;i>=0;i--){ #ifdef NP_I386_ASM - __asm__("mul %4\n" - : "=a" (num->mantissa[i]), "=d" (overflow) - : "0" (num->mantissa[i]), "1" (0), "q" (base) - : "eax", "edx"); + __asm__("mul %4\n" + : "=a" (num->mantissa[i]), "=d" (overflow) + : "0" (num->mantissa[i]), "1" (0), "q" (base) + : "eax", "edx"); #else - overflow=0; - val=num->mantissa[i]; - - if(val<ULONG_MAX/base){ - val*=base; - }else if(val){ - ulong tmp=val; - ulong old=val; - for(j=0; j<base; j++){ - val+=tmp; - if(val<=old) - overflow++; - old=val; - } - } - num->mantissa[i]=val; + overflow=0; + val=num->mantissa[i]; + + if(val<ULONG_MAX/base){ + val*=base; + }else if(val){ + ulong tmp=val; + ulong old=val; + for(j=0; j<base; j++){ + val+=tmp; + if(val<=old) + overflow++; + old=val; + } + } + num->mantissa[i]=val; #endif - if(overflow){ - if(i==num->nmantissa){ - if(num->nmantissa==MAX_MANTISSA) - return E_TOKZ_TOOBIG; - num->nmantissa++; - } - num->mantissa[i+1]+=overflow; - } - } - num->mantissa[0]+=v; - - return 0; + if(overflow){ + if(i==num->nmantissa){ + if(num->nmantissa==MAX_MANTISSA) + return E_TOKZ_TOOBIG; + num->nmantissa++; + } + num->mantissa[i+1]+=overflow; + } + } + num->mantissa[0]+=v; + + return 0; } #undef NP_I386_ASM @@ -118,8 +118,8 @@ static bool is_end(int c) { - /* oops... EOF was missing */ - return (c==EOF || (c!='.' && ispunct(c)) || isspace(c) || iscntrl(c)); + /* oops... EOF was missing */ + return (c==EOF || (c!='.' && ispunct(c)) || isspace(c) || iscntrl(c)); } @@ -128,145 +128,145 @@ static int parse_exponent(NPNum *num, Tokenizer *tokz, int c) { - long exp=0; - bool neg=FALSE; - int err=0; - - c=GETCH(); - - if(c=='-' || c=='+'){ - if(c=='-') - neg=TRUE; - c=GETCH(); - } - - for(; 1; c=GETCH()){ - if(isdigit(c)){ - exp*=10; - exp+=c-'0'; - }else if(is_end(c)){ - UNGETCH(c); - break; - }else{ - err=E_TOKZ_NUMFMT; - } - } + long exp=0; + bool neg=FALSE; + int err=0; + + c=GETCH(); + + if(c=='-' || c=='+'){ + if(c=='-') + neg=TRUE; + c=GETCH(); + } + + for(; 1; c=GETCH()){ + if(isdigit(c)){ + exp*=10; + exp+=c-'0'; + }else if(is_end(c)){ + UNGETCH(c); + break; + }else{ + err=E_TOKZ_NUMFMT; + } + } - if(neg) - exp*=-1; + if(neg) + exp*=-1; #ifndef NP_SIMPLE_IMPL - ADD_EXP(num, exp); + ADD_EXP(num, exp); #else - num->fval*=pow(num->base, exp); -#endif - return err; + num->fval*=pow(num->base, exp); +#endif + return err; } static int parse_number(NPNum *num, Tokenizer *tokz, int c) { - int base=10; - int dm=1; - int err=0; - int tmp; + int base=10; + int dm=1; + int err=0; + int tmp; #ifdef NP_SIMPLE_IMPL - double divisor=base; -#endif - - if(c=='-' || c=='+'){ - if(c=='-') - num->negative=TRUE; - c=GETCH(); - if(!isdigit(c)) - err=E_TOKZ_NUMFMT; - } - - if(c=='0'){ - dm=0; - c=GETCH(); - if(c=='x' || c=='X'){ - base=16; - c=GETCH(); - }else if(c=='b' || c=='B'){ - base=2; - c=GETCH(); - }else if('0'<=c && c<='7'){ - base=8; - }else{ - dm=2; - } - } - - num->base=base; - - for(; 1; c=GETCH()){ - if((c=='e' || c=='E') && dm!=0){ - if(dm<2){ - err=E_TOKZ_NUMFMT; - continue; - } - tmp=parse_exponent(num, tokz, c); - if(err==0) - err=tmp; - break; - } - - if(isxdigit(c)){ - if('0'<=c && c<='9') - c-='0'; - else if(isupper(c)) - c-='A'-10; - else - c-='a'-10; - - if(c>=base) - err=E_TOKZ_NUMFMT; + double divisor=base; +#endif + + if(c=='-' || c=='+'){ + if(c=='-') + num->negative=TRUE; + c=GETCH(); + if(!isdigit(c)) + err=E_TOKZ_NUMFMT; + } + + if(c=='0'){ + dm=0; + c=GETCH(); + if(c=='x' || c=='X'){ + base=16; + c=GETCH(); + }else if(c=='b' || c=='B'){ + base=2; + c=GETCH(); + }else if('0'<=c && c<='7'){ + base=8; + }else{ + dm=2; + } + } + + num->base=base; + + for(; 1; c=GETCH()){ + if((c=='e' || c=='E') && dm!=0){ + if(dm<2){ + err=E_TOKZ_NUMFMT; + continue; + } + tmp=parse_exponent(num, tokz, c); + if(err==0) + err=tmp; + break; + } + + if(isxdigit(c)){ + if('0'<=c && c<='9') + c-='0'; + else if(isupper(c)) + c-='A'-10; + else + c-='a'-10; + + if(c>=base) + err=E_TOKZ_NUMFMT; #ifdef NP_SIMPLE_IMPL - if(dm==3){ - num->fval+=(double)c/divisor; - divisor*=base; - }else + if(dm==3){ + num->fval+=(double)c/divisor; + divisor*=base; + }else #endif - { - tmp=npnum_mulbase_add(num, base, c); - if(err==0) - err=tmp; - } - - if(dm==1) - dm=2; -#ifndef NP_SIMPLE_IMPL - else if(dm==3) - ADD_EXP(num, -1); -#endif - continue; - } - - if(c=='.'){ - if(dm!=2){ - err=E_TOKZ_NUMFMT; - } - dm=3; + { + tmp=npnum_mulbase_add(num, base, c); + if(err==0) + err=tmp; + } + + if(dm==1) + dm=2; +#ifndef NP_SIMPLE_IMPL + else if(dm==3) + ADD_EXP(num, -1); +#endif + continue; + } + + if(c=='.'){ + if(dm!=2){ + err=E_TOKZ_NUMFMT; + } + dm=3; #ifdef NP_SIMPLE_IMPL - num->type=NPNUM_FLOAT; - divisor=base; + num->type=NPNUM_FLOAT; + divisor=base; #endif - continue; - } - - if(is_end(c)){ - UNGETCH(c); - break; - } - - err=E_TOKZ_NUMFMT; - } - -#ifndef NP_SIMPLE_IMPL - num->type=(num->exponent==0 ? NPNUM_INT : NPNUM_FLOAT); + continue; + } + + if(is_end(c)){ + UNGETCH(c); + break; + } + + err=E_TOKZ_NUMFMT; + } + +#ifndef NP_SIMPLE_IMPL + num->type=(num->exponent==0 ? NPNUM_INT : NPNUM_FLOAT); #endif - return err; + return err; }
--- a/obj.c Mon Feb 16 18:09:03 2004 +0100 +++ b/obj.c Mon Feb 16 18:50:28 2004 +0100 @@ -27,28 +27,28 @@ void destroy_obj(Obj *obj) { - ClassDescr *d; - - if(OBJ_IS_BEING_DESTROYED(obj)) - return; - - obj->flags|=OBJ_DEST; - - do_watches(obj, TRUE); - - d=obj->obj_type; - - while(d!=NULL){ - if(d->destroy_fn!=NULL){ - d->destroy_fn(obj); - break; - } - d=d->ancestor; - } - - do_watches(obj, FALSE); + ClassDescr *d; + + if(OBJ_IS_BEING_DESTROYED(obj)) + return; + + obj->flags|=OBJ_DEST; + + do_watches(obj, TRUE); + + d=obj->obj_type; + + while(d!=NULL){ + if(d->destroy_fn!=NULL){ + d->destroy_fn(obj); + break; + } + d=d->ancestor; + } + + do_watches(obj, FALSE); - free(obj); + free(obj); } @@ -60,55 +60,55 @@ bool obj_is(const Obj *obj, const ClassDescr *descr) { - ClassDescr *d; - - if(obj==NULL) - return FALSE; - - d=obj->obj_type; - - while(d!=NULL){ - if(d==descr) - return TRUE; - d=d->ancestor; - } - return FALSE; + ClassDescr *d; + + if(obj==NULL) + return FALSE; + + d=obj->obj_type; + + while(d!=NULL){ + if(d==descr) + return TRUE; + d=d->ancestor; + } + return FALSE; } bool obj_is_str(const Obj *obj, const char *str) { - ClassDescr *d; - - if(obj==NULL || str==NULL) - return FALSE; - - d=obj->obj_type; - - while(d!=NULL){ - if(strcmp(d->name, str)==0) - return TRUE; - d=d->ancestor; - } - return FALSE; + ClassDescr *d; + + if(obj==NULL || str==NULL) + return FALSE; + + d=obj->obj_type; + + while(d!=NULL){ + if(strcmp(d->name, str)==0) + return TRUE; + d=d->ancestor; + } + return FALSE; } const void *obj_cast(const Obj *obj, const ClassDescr *descr) { - ClassDescr *d; - - if(obj==NULL) - return NULL; - - d=obj->obj_type; - - while(d!=NULL){ - if(d==descr) - return (void*)obj; - d=d->ancestor; - } - return NULL; + ClassDescr *d; + + if(obj==NULL) + return NULL; + + d=obj->obj_type; + + while(d!=NULL){ + if(d==descr) + return (void*)obj; + d=d->ancestor; + } + return NULL; } @@ -127,90 +127,90 @@ static int comp_fun(const void *a, const void *b) { - void *af=(void*)((DynFunTab*)a)->func; - void *bf=(void*)((DynFunTab*)b)->func; - - return (af<bf ? -1 : (af==bf ? 0 : 1)); + void *af=(void*)((DynFunTab*)a)->func; + void *bf=(void*)((DynFunTab*)b)->func; + + return (af<bf ? -1 : (af==bf ? 0 : 1)); } - + DynFun *lookup_dynfun(const Obj *obj, DynFun *func, - bool *funnotfound) + bool *funnotfound) { - ClassDescr *descr; - DynFunTab *df; - /*DynFunTab dummy={NULL, NULL};*/ - /*dummy.func=func;*/ - - if(obj==NULL) - return NULL; - - descr=obj->obj_type; - - for(; descr!=&Obj_classdescr; descr=descr->ancestor){ - if(descr->funtab==NULL) - continue; - - if(descr->funtab_n==-1){ - /* Need to sort the table. */ - descr->funtab_n=0; - df=descr->funtab; - while(df->func!=NULL){ - descr->funtab_n++; - df++; - } - - if(descr->funtab_n>0){ - qsort(descr->funtab, descr->funtab_n, sizeof(DynFunTab), - comp_fun); - } - } - - /* - if(descr->funtab_n==0) - continue; - - df=(DynFunTab*)bsearch(&dummy, descr->funtab, descr->funtab_n, - sizeof(DynFunTab), comp_fun); - if(df!=NULL){ - *funnotfound=FALSE; - return df->handler; - } - */ - - /* Using custom bsearch instead of the one in libc is probably - * faster as the overhead of calling a comparison function would - * be significant given that the comparisons are simple and - * funtab_n not that big. - */ - { - int min=0, max=descr->funtab_n-1; - int ndx; - df=descr->funtab; - while(max>=min){ - ndx=(max+min)/2; - if((void*)df[ndx].func==(void*)func){ - *funnotfound=FALSE; - return df[ndx].handler; - } - if((void*)df[ndx].func<(void*)func) - min=ndx+1; - else - max=ndx-1; - } - } - } - - *funnotfound=TRUE; - return dummy_dyn; + ClassDescr *descr; + DynFunTab *df; + /*DynFunTab dummy={NULL, NULL};*/ + /*dummy.func=func;*/ + + if(obj==NULL) + return NULL; + + descr=obj->obj_type; + + for(; descr!=&Obj_classdescr; descr=descr->ancestor){ + if(descr->funtab==NULL) + continue; + + if(descr->funtab_n==-1){ + /* Need to sort the table. */ + descr->funtab_n=0; + df=descr->funtab; + while(df->func!=NULL){ + descr->funtab_n++; + df++; + } + + if(descr->funtab_n>0){ + qsort(descr->funtab, descr->funtab_n, sizeof(DynFunTab), + comp_fun); + } + } + + /* + if(descr->funtab_n==0) + continue; + + df=(DynFunTab*)bsearch(&dummy, descr->funtab, descr->funtab_n, + sizeof(DynFunTab), comp_fun); + if(df!=NULL){ + *funnotfound=FALSE; + return df->handler; + } + */ + + /* Using custom bsearch instead of the one in libc is probably + * faster as the overhead of calling a comparison function would + * be significant given that the comparisons are simple and + * funtab_n not that big. + */ + { + int min=0, max=descr->funtab_n-1; + int ndx; + df=descr->funtab; + while(max>=min){ + ndx=(max+min)/2; + if((void*)df[ndx].func==(void*)func){ + *funnotfound=FALSE; + return df[ndx].handler; + } + if((void*)df[ndx].func<(void*)func) + min=ndx+1; + else + max=ndx-1; + } + } + } + + *funnotfound=TRUE; + return dummy_dyn; } bool has_dynfun(const Obj *obj, DynFun *func) { - bool funnotfound; - lookup_dynfun(obj, func, &funnotfound); - return !funnotfound; + bool funnotfound; + lookup_dynfun(obj, func, &funnotfound); + return !funnotfound; } @@ -222,74 +222,74 @@ bool watch_setup(Watch *watch, Obj *obj, WatchHandler *handler) { - if(OBJ_IS_BEING_DESTROYED(obj)) - return FALSE; - - watch_reset(watch); - - watch->handler=handler; - LINK_ITEM(obj->obj_watches, watch, next, prev); - watch->obj=obj; - - return TRUE; + if(OBJ_IS_BEING_DESTROYED(obj)) + return FALSE; + + watch_reset(watch); + + watch->handler=handler; + LINK_ITEM(obj->obj_watches, watch, next, prev); + watch->obj=obj; + + return TRUE; } void do_watch_reset(Watch *watch, bool call) { - WatchHandler *handler=watch->handler; - Obj *obj=watch->obj; - - watch->handler=NULL; - - if(obj==NULL) - return; - - UNLINK_ITEM(obj->obj_watches, watch, next, prev); - watch->obj=NULL; - - if(call && handler!=NULL) - handler(watch, obj); + WatchHandler *handler=watch->handler; + Obj *obj=watch->obj; + + watch->handler=NULL; + + if(obj==NULL) + return; + + UNLINK_ITEM(obj->obj_watches, watch, next, prev); + watch->obj=NULL; + + if(call && handler!=NULL) + handler(watch, obj); } void watch_reset(Watch *watch) { - do_watch_reset(watch, FALSE); + do_watch_reset(watch, FALSE); } bool watch_ok(Watch *watch) { - return watch->obj!=NULL; + return watch->obj!=NULL; } static void do_watches(Obj *obj, bool call) { - Watch *watch, *next; + Watch *watch, *next; - watch=obj->obj_watches; - - while(watch!=NULL){ - next=watch->next; - do_watch_reset(watch, call); - watch=next; - } + watch=obj->obj_watches; + + while(watch!=NULL){ + next=watch->next; + do_watch_reset(watch, call); + watch=next; + } } void watch_call(Obj *obj) { - do_watches(obj, FALSE); + do_watches(obj, FALSE); } void watch_init(Watch *watch) { - watch->obj=NULL; - watch->next=NULL; - watch->prev=NULL; - watch->handler=NULL; + watch->obj=NULL; + watch->next=NULL; + watch->prev=NULL; + watch->handler=NULL; }
--- a/obj.h Mon Feb 16 18:09:03 2004 +0100 +++ b/obj.h Mon Feb 16 18:50:28 2004 +0100 @@ -18,9 +18,9 @@ #define OBJ_CAST(OBJ, TYPE) (TYPE*)obj_cast((Obj*)OBJ, &CLASSDESCR(TYPE)) #define INTRSTRUCT(STRU) \ - struct STRU##_struct; typedef struct STRU##_struct STRU + struct STRU##_struct; typedef struct STRU##_struct STRU #define DECLSTRUCT(STRU) \ - struct STRU##_struct + struct STRU##_struct #define INTRCLASS(OBJ) INTRSTRUCT(OBJ); extern ClassDescr CLASSDESCR(OBJ) #define DECLCLASS(OBJ) DECLSTRUCT(OBJ) @@ -36,9 +36,9 @@ extern void destroy_obj(Obj *obj); DECLCLASS(Obj){ - ClassDescr *obj_type; - Watch *obj_watches; - int flags; + ClassDescr *obj_type; + Watch *obj_watches; + int flags; }; #define OBJ_DEST 0x0001 @@ -53,13 +53,13 @@ #define WWATCH_INIT {NULL, NULL, NULL, NULL} DECLSTRUCT(Watch){ - Obj *obj; - Watch *next, *prev; - WatchHandler *handler; + Obj *obj; + Watch *next, *prev; + WatchHandler *handler; }; extern bool watch_setup(Watch *watch, Obj *obj, - WatchHandler *handler); + WatchHandler *handler); extern void watch_reset(Watch *watch); extern bool watch_ok(Watch *watch); extern void watch_init(Watch *watch);
--- a/objlist.c Mon Feb 16 18:09:03 2004 +0100 +++ b/objlist.c Mon Feb 16 18:50:28 2004 +0100 @@ -19,67 +19,67 @@ static void free_node(ObjList **objlist, ObjList *node) { - UNLINK_ITEM(*objlist, node, next, prev); - free(node); + UNLINK_ITEM(*objlist, node, next, prev); + free(node); } - + void watch_handler(Watch *watch, Obj *obj) { - ObjList *node=(ObjList*)watch; - ObjList **list=node->list; - - if(iter_next==node) - iter_next=node->next; - - free_node(list, node); + ObjList *node=(ObjList*)watch; + ObjList **list=node->list; + + if(iter_next==node) + iter_next=node->next; + + free_node(list, node); } bool objlist_insert(ObjList **objlist, Obj *obj) { - ObjList *node; - - if(obj==NULL) - return FALSE; - - node=ALLOC(ObjList); - - if(node==NULL) - return FALSE; - - watch_init(&(node->watch)); - watch_setup(&(node->watch), obj, watch_handler); - node->list=objlist; - - LINK_ITEM_FIRST(*objlist, node, next, prev); - - return TRUE; + ObjList *node; + + if(obj==NULL) + return FALSE; + + node=ALLOC(ObjList); + + if(node==NULL) + return FALSE; + + watch_init(&(node->watch)); + watch_setup(&(node->watch), obj, watch_handler); + node->list=objlist; + + LINK_ITEM_FIRST(*objlist, node, next, prev); + + return TRUE; } void objlist_remove(ObjList **objlist, Obj *obj) { - ObjList *node=*objlist; - - while(node!=NULL){ - if(node->watch.obj==obj){ - watch_reset(&(node->watch)); - free_node(objlist, node); - return; - } - node=node->next; - } + ObjList *node=*objlist; + + while(node!=NULL){ + if(node->watch.obj==obj){ + watch_reset(&(node->watch)); + free_node(objlist, node); + return; + } + node=node->next; + } } void objlist_clear(ObjList **objlist) { - while(*objlist!=NULL){ - watch_reset(&((*objlist)->watch)); - free_node(objlist, *objlist); - } + while(*objlist!=NULL){ + watch_reset(&((*objlist)->watch)); + free_node(objlist, *objlist); + } } @@ -88,25 +88,25 @@ Obj *objlist_init_iter(ObjList *objlist) { - if(objlist==NULL){ - iter_next=NULL; - return NULL; - } - - iter_next=objlist->next; - return objlist->watch.obj; + if(objlist==NULL){ + iter_next=NULL; + return NULL; + } + + iter_next=objlist->next; + return objlist->watch.obj; } Obj *objlist_iter() { - ObjList *ret; - - if(iter_next==NULL) - return NULL; - - ret=iter_next; - iter_next=iter_next->next; - - return ret->watch.obj; + ObjList *ret; + + if(iter_next==NULL) + return NULL; + + ret=iter_next; + iter_next=iter_next->next; + + return ret->watch.obj; }
--- a/objlist.h Mon Feb 16 18:09:03 2004 +0100 +++ b/objlist.h Mon Feb 16 18:50:28 2004 +0100 @@ -18,16 +18,16 @@ DECLSTRUCT(ObjList){ - Watch watch; /* Must be kept at head of structure */ - ObjList *next, *prev; - ObjList **list; + Watch watch; /* Must be kept at head of structure */ + ObjList *next, *prev; + ObjList **list; }; #define FOR_ALL_ON_OBJLIST(TYPE, VAR, LIST) \ - for((VAR)=(TYPE)objlist_init_iter(LIST); \ - (VAR)!=NULL; \ - (VAR)=(TYPE)objlist_iter()) + for((VAR)=(TYPE)objlist_init_iter(LIST); \ + (VAR)!=NULL; \ + (VAR)=(TYPE)objlist_iter()) bool objlist_insert(ObjList **objlist, Obj *obj);
--- a/objp.h Mon Feb 16 18:09:03 2004 +0100 +++ b/objp.h Mon Feb 16 18:50:28 2004 +0100 @@ -18,54 +18,54 @@ INTRSTRUCT(DynFunTab); DECLSTRUCT(DynFunTab){ - DynFun *func, *handler; + DynFun *func, *handler; }; DECLSTRUCT(ClassDescr){ - const char *name; - ClassDescr *ancestor; - int funtab_n; - DynFunTab *funtab; - void (*destroy_fn)(); + const char *name; + ClassDescr *ancestor; + int funtab_n; + DynFunTab *funtab; + void (*destroy_fn)(); }; #define OBJ_TYPESTR(OBJ) (((Obj*)OBJ)->obj_type->name) #define IMPLCLASS(CLS, ANCESTOR, DFN, DYN) \ - ClassDescr CLASSDESCR(CLS)={ \ - #CLS, &CLASSDESCR(ANCESTOR), -1, DYN, (void (*)())DFN} + ClassDescr CLASSDESCR(CLS)={ \ + #CLS, &CLASSDESCR(ANCESTOR), -1, DYN, (void (*)())DFN} #define OBJ_INIT(O, TYPE) {((Obj*)(O))->obj_type=&CLASSDESCR(TYPE); \ - ((Obj*)(O))->obj_watches=NULL; ((Obj*)(O))->flags=0;} + ((Obj*)(O))->obj_watches=NULL; ((Obj*)(O))->flags=0;} #define CREATEOBJ_IMPL(OBJ, LOWOBJ, INIT_ARGS) \ - OBJ *p; p=ALLOC(OBJ); if(p==NULL){ warn_err(); return NULL; } \ - OBJ_INIT(p, OBJ); \ - if(!LOWOBJ ## _init INIT_ARGS) { free((void*)p); return NULL; } return p + OBJ *p; p=ALLOC(OBJ); if(p==NULL){ warn_err(); return NULL; } \ + OBJ_INIT(p, OBJ); \ + if(!LOWOBJ ## _init INIT_ARGS) { free((void*)p); return NULL; } return p #define SIMPLECREATEOBJ_IMPL(OBJ, LOWOBJ) \ - OBJ *p; p=ALLOC(OBJ); if(p==NULL){ warn_err(); return NULL; } \ - OBJ_INIT(p, OBJ); \ - return p; + OBJ *p; p=ALLOC(OBJ); if(p==NULL){ warn_err(); return NULL; } \ + OBJ_INIT(p, OBJ); \ + return p; #define END_DYNFUNTAB {NULL, NULL} extern DynFun *lookup_dynfun(const Obj *obj, DynFun *func, - bool *funnotfound); + bool *funnotfound); extern bool has_dynfun(const Obj *obj, DynFun *func); #define CALL_DYN(FUNC, OBJ, ARGS) \ - bool funnotfound; \ - lookup_dynfun((Obj*)OBJ, (DynFun*)FUNC, &funnotfound) ARGS; + bool funnotfound; \ + lookup_dynfun((Obj*)OBJ, (DynFun*)FUNC, &funnotfound) ARGS; #define CALL_DYN_RET(RETV, RET, FUNC, OBJ, ARGS) \ - typedef RET ThisDynFun(); \ - bool funnotfound; \ - ThisDynFun *funtmp; \ - funtmp=(ThisDynFun*)lookup_dynfun((Obj*)OBJ, (DynFun*)FUNC, \ - &funnotfound); \ - if(!funnotfound) \ - RETV=funtmp ARGS; + typedef RET ThisDynFun(); \ + bool funnotfound; \ + ThisDynFun *funtmp; \ + funtmp=(ThisDynFun*)lookup_dynfun((Obj*)OBJ, (DynFun*)FUNC, \ + &funnotfound); \ + if(!funnotfound) \ + RETV=funtmp ARGS; #define HAS_DYN(OBJ, FUNC) has_dynfun((Obj*)OBJ, (DynFun*)FUNC)
--- a/optparser.c Mon Feb 16 18:09:03 2004 +0100 +++ b/optparser.c Mon Feb 16 18:50:28 2004 +0100 @@ -16,10 +16,10 @@ #include <libtu/output.h> -#define O_ARGS(o) (o->flags&OPT_OPT_ARG) -#define O_ARG(o) (o->flasg&OPT_ARG) -#define O_OPT_ARG(o) (O_ARGS(o)==OPT_OPT_ARG) -#define O_ID(o) (o->optid) +#define O_ARGS(o) (o->flags&OPT_OPT_ARG) +#define O_ARG(o) (o->flasg&OPT_ARG) +#define O_OPT_ARG(o) (O_ARGS(o)==OPT_OPT_ARG) +#define O_ID(o) (o->optid) #define OPT_ID_HELP OPT_ID_RESERVED('h') #define OPT_ID_VERSION OPT_ID_RESERVED('V') @@ -27,19 +27,19 @@ static OptParserOpt common_opts[]={ - {OPT_ID_HELP, "help", 0, NULL, DUMMY_TR("Show this help")}, - {OPT_ID_VERSION, "version", 0, NULL, DUMMY_TR("Show program version")}, - {OPT_ID_ABOUT, "about", 0, NULL, DUMMY_TR("Show about text")}, - {0, NULL, 0, NULL, NULL} + {OPT_ID_HELP, "help", 0, NULL, DUMMY_TR("Show this help")}, + {OPT_ID_VERSION, "version", 0, NULL, DUMMY_TR("Show program version")}, + {OPT_ID_ABOUT, "about", 0, NULL, DUMMY_TR("Show about text")}, + {0, NULL, 0, NULL, NULL} }; static OptParserCommonInfo dummy_cinfo[]={ - NULL, /* version */ - "Usage: $p\n", /* usage_tmpl */ - NULL /* about */ + NULL, /* version */ + "Usage: $p\n", /* usage_tmpl */ + NULL /* about */ }; - + static const OptParserOpt *o_opts=NULL; static char *const *o_current=NULL; @@ -56,23 +56,23 @@ static void print_help(const OptParserOpt *opts, bool midlong, - const OptParserCommonInfo *cinfo); + const OptParserCommonInfo *cinfo); /* */ void optparser_init(int argc, char *const argv[], int mode, - const OptParserOpt *opts, const OptParserCommonInfo *cinfo) + const OptParserOpt *opts, const OptParserCommonInfo *cinfo) { - o_mode=mode; - o_opts=(opts==NULL ? common_opts : opts); - o_current=argv+1; - o_left=argc-1; - o_chain_ptr=NULL; - o_args_left=0; - o_tmp=NULL; - o_cinfo=(cinfo==NULL ? dummy_cinfo : cinfo); + o_mode=mode; + o_opts=(opts==NULL ? common_opts : opts); + o_current=argv+1; + o_left=argc-1; + o_chain_ptr=NULL; + o_args_left=0; + o_tmp=NULL; + o_cinfo=(cinfo==NULL ? dummy_cinfo : cinfo); } @@ -81,206 +81,206 @@ static const OptParserOpt *find_chain_opt(char p, const OptParserOpt *o) { - for(;O_ID(o);o++){ - if((O_ID(o)&~OPT_ID_RESERVED_FLAG)==p) - return o; - } - return NULL; + for(;O_ID(o);o++){ + if((O_ID(o)&~OPT_ID_RESERVED_FLAG)==p) + return o; + } + return NULL; } static bool is_option(const char *p) { - if(p==NULL) - return FALSE; - if(*p++!='-') - return FALSE; - if(*p++!='-') - return FALSE; - if(*p=='\0') - return FALSE; - return TRUE; + if(p==NULL) + return FALSE; + if(*p++!='-') + return FALSE; + if(*p++!='-') + return FALSE; + if(*p=='\0') + return FALSE; + return TRUE; } - + /* */ enum{ - SHORT, MIDLONG, LONG + SHORT, MIDLONG, LONG }; static int optparser_do_get_opt() { #define RET(X) return o_tmp=p, o_error=X - const char *p, *p2=NULL; - bool dash=TRUE; - int type=SHORT; - const OptParserOpt *o; - int l; + const char *p, *p2=NULL; + bool dash=TRUE; + int type=SHORT; + const OptParserOpt *o; + int l; - while(o_args_left) - optparser_get_arg(); + while(o_args_left) + optparser_get_arg(); - o_tmp=NULL; + o_tmp=NULL; - /* Are we doing a chain (i.e. opt. of style 'tar xzf')? */ - if(o_chain_ptr!=NULL){ - p=o_chain_ptr++; - if(!*o_chain_ptr) - o_chain_ptr=NULL; + /* Are we doing a chain (i.e. opt. of style 'tar xzf')? */ + if(o_chain_ptr!=NULL){ + p=o_chain_ptr++; + if(!*o_chain_ptr) + o_chain_ptr=NULL; - o=find_chain_opt(*p, o_opts); - - if(o==NULL) - RET(E_OPT_INVALID_CHAIN_OPTION); + o=find_chain_opt(*p, o_opts); + + if(o==NULL) + RET(E_OPT_INVALID_CHAIN_OPTION); - goto do_arg; - } - - if(o_left<1) - return OPT_ID_END; + goto do_arg; + } + + if(o_left<1) + return OPT_ID_END; - o_left--; - p=*o_current++; - - if(*p!='-'){ - dash=FALSE; - if(o_mode!=OPTP_NO_DASH) - RET(OPT_ID_ARGUMENT); - p2=p; - }else if(*(p+1)=='-'){ - /* --foo */ - if(*(p+2)=='\0'){ - /* -- arguments */ - o_args_left=o_left; - RET(OPT_ID_ARGUMENT); - } - type=LONG; - p2=p+2; - }else{ - /* -foo */ - if(*(p+1)=='\0'){ - /* - */ - o_args_left=1; - RET(OPT_ID_ARGUMENT); - } - if(*(p+2)!='\0' && o_mode==OPTP_MIDLONG) - type=MIDLONG; - - p2=p+1; - } + o_left--; + p=*o_current++; + + if(*p!='-'){ + dash=FALSE; + if(o_mode!=OPTP_NO_DASH) + RET(OPT_ID_ARGUMENT); + p2=p; + }else if(*(p+1)=='-'){ + /* --foo */ + if(*(p+2)=='\0'){ + /* -- arguments */ + o_args_left=o_left; + RET(OPT_ID_ARGUMENT); + } + type=LONG; + p2=p+2; + }else{ + /* -foo */ + if(*(p+1)=='\0'){ + /* - */ + o_args_left=1; + RET(OPT_ID_ARGUMENT); + } + if(*(p+2)!='\0' && o_mode==OPTP_MIDLONG) + type=MIDLONG; + + p2=p+1; + } - o=o_opts; + o=o_opts; again: - for(; O_ID(o); o++){ - if(type==LONG){ - /* Do long option (--foo=bar) */ - if(o->longopt==NULL) - continue; - l=strlen(o->longopt); - if(strncmp(p2, o->longopt, l)!=0) - continue; - - if(p2[l]=='\0'){ - if(O_ARGS(o)==OPT_ARG) - RET(E_OPT_MISSING_ARGUMENT); - return O_ID(o); - }else if(p2[l]=='='){ - if(!O_ARGS(o)) - RET(E_OPT_UNEXPECTED_ARGUMENT); - if(p2[l+1]=='\0') - RET(E_OPT_MISSING_ARGUMENT); - o_tmp=p2+l+1; - o_args_left=1; - return O_ID(o); - } - continue; - }else if(type==MIDLONG){ - if(o->longopt==NULL) - continue; - - if(strcmp(p2, o->longopt)!=0) - continue; - }else{ /* type==SHORT */ - if(*p2!=(O_ID(o)&~OPT_ID_RESERVED_FLAG)) - continue; - - if(*(p2+1)!='\0'){ - if(o_mode==OPTP_CHAIN || o_mode==OPTP_NO_DASH){ - /*valid_chain(p2+1, o_opts)*/ - o_chain_ptr=p2+1; - p++; - }else if(o_mode==OPTP_IMMEDIATE){ - if(!O_ARGS(o)){ - if(*(p2+1)!='\0') - RET(E_OPT_UNEXPECTED_ARGUMENT); - }else{ - if(*(p2+1)=='\0') - RET(E_OPT_MISSING_ARGUMENT); - o_tmp=p2+1; - o_args_left=1; - } - return O_ID(o); - }else{ - RET(E_OPT_SYNTAX_ERROR); - } - } - } - - do_arg: - - if(!O_ARGS(o)) - return O_ID(o); - - if(!o_left || is_option(*o_current)){ - if(O_ARGS(o)==OPT_OPT_ARG) - return O_ID(o); - RET(E_OPT_MISSING_ARGUMENT); - } + for(; O_ID(o); o++){ + if(type==LONG){ + /* Do long option (--foo=bar) */ + if(o->longopt==NULL) + continue; + l=strlen(o->longopt); + if(strncmp(p2, o->longopt, l)!=0) + continue; + + if(p2[l]=='\0'){ + if(O_ARGS(o)==OPT_ARG) + RET(E_OPT_MISSING_ARGUMENT); + return O_ID(o); + }else if(p2[l]=='='){ + if(!O_ARGS(o)) + RET(E_OPT_UNEXPECTED_ARGUMENT); + if(p2[l+1]=='\0') + RET(E_OPT_MISSING_ARGUMENT); + o_tmp=p2+l+1; + o_args_left=1; + return O_ID(o); + } + continue; + }else if(type==MIDLONG){ + if(o->longopt==NULL) + continue; + + if(strcmp(p2, o->longopt)!=0) + continue; + }else{ /* type==SHORT */ + if(*p2!=(O_ID(o)&~OPT_ID_RESERVED_FLAG)) + continue; + + if(*(p2+1)!='\0'){ + if(o_mode==OPTP_CHAIN || o_mode==OPTP_NO_DASH){ + /*valid_chain(p2+1, o_opts)*/ + o_chain_ptr=p2+1; + p++; + }else if(o_mode==OPTP_IMMEDIATE){ + if(!O_ARGS(o)){ + if(*(p2+1)!='\0') + RET(E_OPT_UNEXPECTED_ARGUMENT); + }else{ + if(*(p2+1)=='\0') + RET(E_OPT_MISSING_ARGUMENT); + o_tmp=p2+1; + o_args_left=1; + } + return O_ID(o); + }else{ + RET(E_OPT_SYNTAX_ERROR); + } + } + } + + do_arg: + + if(!O_ARGS(o)) + return O_ID(o); + + if(!o_left || is_option(*o_current)){ + if(O_ARGS(o)==OPT_OPT_ARG) + return O_ID(o); + RET(E_OPT_MISSING_ARGUMENT); + } - o_args_left=1; - return O_ID(o); - } - - if(o!=&(common_opts[3])){ - o=common_opts; - goto again; - } + o_args_left=1; + return O_ID(o); + } + + if(o!=&(common_opts[3])){ + o=common_opts; + goto again; + } - if(dash) - RET(E_OPT_INVALID_OPTION); - - RET(OPT_ID_ARGUMENT); + if(dash) + RET(E_OPT_INVALID_OPTION); + + RET(OPT_ID_ARGUMENT); #undef RET } int optparser_get_opt() { - int oid=optparser_do_get_opt(); - - if(oid<=0 || (oid&OPT_ID_RESERVED_FLAG)==0) - return oid; - - switch(oid){ - case OPT_ID_ABOUT: - if(o_cinfo->about!=NULL) - printf("%s", o_cinfo->about); - break; - - case OPT_ID_VERSION: - if(o_cinfo->version!=NULL) - printf("%s\n", o_cinfo->version); - break; - - case OPT_ID_HELP: - print_help(o_opts, o_mode==OPTP_MIDLONG, o_cinfo); - break; - } - - exit(EXIT_SUCCESS); + int oid=optparser_do_get_opt(); + + if(oid<=0 || (oid&OPT_ID_RESERVED_FLAG)==0) + return oid; + + switch(oid){ + case OPT_ID_ABOUT: + if(o_cinfo->about!=NULL) + printf("%s", o_cinfo->about); + break; + + case OPT_ID_VERSION: + if(o_cinfo->version!=NULL) + printf("%s\n", o_cinfo->version); + break; + + case OPT_ID_HELP: + print_help(o_opts, o_mode==OPTP_MIDLONG, o_cinfo); + break; + } + + exit(EXIT_SUCCESS); } @@ -289,26 +289,26 @@ const char* optparser_get_arg() { - const char *p; - - if(o_tmp!=NULL){ - /* If o_args_left==0, then were returning an invalid option - * otherwise an immediate argument (e.g. -funsigned-char - * where '-f' is the option and 'unsigned-char' the argument) - */ - if(o_args_left>0) - o_args_left--; - p=o_tmp; - o_tmp=NULL; - return p; - } - - if(o_args_left<1 || o_left<1) - return NULL; + const char *p; + + if(o_tmp!=NULL){ + /* If o_args_left==0, then were returning an invalid option + * otherwise an immediate argument (e.g. -funsigned-char + * where '-f' is the option and 'unsigned-char' the argument) + */ + if(o_args_left>0) + o_args_left--; + p=o_tmp; + o_tmp=NULL; + return p; + } + + if(o_args_left<1 || o_left<1) + return NULL; - o_left--; - o_args_left--; - return *o_current++; + o_left--; + o_args_left--; + return *o_current++; } @@ -316,54 +316,54 @@ static void warn_arg(const char *e) { - const char *p=optparser_get_arg(); - - if(p==NULL) - warn("%s (null)", e); - else - warn("%s \'%s\'", e, p); + const char *p=optparser_get_arg(); + + if(p==NULL) + warn("%s (null)", e); + else + warn("%s \'%s\'", e, p); } static void warn_opt(const char *e) { - if(o_tmp!=NULL && o_chain_ptr!=NULL) - warn("%s \'-%c\'", e, *o_tmp); - else - warn_arg(e); + if(o_tmp!=NULL && o_chain_ptr!=NULL) + warn("%s \'-%c\'", e, *o_tmp); + else + warn_arg(e); } void optparser_print_error() { - switch(o_error){ - case E_OPT_INVALID_OPTION: - case E_OPT_INVALID_CHAIN_OPTION: - warn_opt(TR("Invalid option")); - break; + switch(o_error){ + case E_OPT_INVALID_OPTION: + case E_OPT_INVALID_CHAIN_OPTION: + warn_opt(TR("Invalid option")); + break; - case E_OPT_SYNTAX_ERROR: - warn_arg(TR("Syntax error while parsing")); - break; - - case E_OPT_MISSING_ARGUMENT: - warn_opt(TR("Missing argument to")); - break; + case E_OPT_SYNTAX_ERROR: + warn_arg(TR("Syntax error while parsing")); + break; + + case E_OPT_MISSING_ARGUMENT: + warn_opt(TR("Missing argument to")); + break; - case E_OPT_UNEXPECTED_ARGUMENT: - warn_opt(TR("No argument expected:")); - break; - - case OPT_ID_ARGUMENT: - warn(TR("Unexpected argument")); - break; + case E_OPT_UNEXPECTED_ARGUMENT: + warn_opt(TR("No argument expected:")); + break; + + case OPT_ID_ARGUMENT: + warn(TR("Unexpected argument")); + break; - default: - warn(TR("(unknown error)")); - } - - o_tmp=NULL; - o_error=0; + default: + warn(TR("(unknown error)")); + } + + o_tmp=NULL; + o_error=0; } @@ -372,28 +372,28 @@ static uint opt_w(const OptParserOpt *opt, bool midlong) { - uint w=0; - - if((opt->optid&OPT_ID_NOSHORT_FLAG)==0){ - w+=2; /* "-o" */ - if(opt->longopt!=NULL) - w+=2; /* ", " */ - } - - if(opt->longopt!=NULL) - w+=strlen(opt->longopt)+(midlong ? 1 : 2); - - if(O_ARGS(opt)){ - if(opt->argname==NULL) - w+=4; - else - w+=1+strlen(opt->argname); /* "=ARG" or " ARG" */ - - if(O_OPT_ARG(opt)) - w+=2; /* [ARG] */ - } - - return w; + uint w=0; + + if((opt->optid&OPT_ID_NOSHORT_FLAG)==0){ + w+=2; /* "-o" */ + if(opt->longopt!=NULL) + w+=2; /* ", " */ + } + + if(opt->longopt!=NULL) + w+=strlen(opt->longopt)+(midlong ? 1 : 2); + + if(O_ARGS(opt)){ + if(opt->argname==NULL) + w+=4; + else + w+=1+strlen(opt->argname); /* "=ARG" or " ARG" */ + + if(O_OPT_ARG(opt)) + w+=2; /* [ARG] */ + } + + return w; } @@ -404,172 +404,172 @@ #define SPACER2 " " static void print_opt(const OptParserOpt *opt, bool midlong, - uint maxw, uint tw) + uint maxw, uint tw) { - FILE *f=stdout; - const char *p, *p2, *p3; - uint w=0; - - fprintf(f, SPACER1); - - /* short opt */ - - if((O_ID(opt)&OPT_ID_NOSHORT_FLAG)==0){ - fprintf(f, "-%c", O_ID(opt)&~OPT_ID_RESERVED_FLAG); - w+=2; - - if(opt->longopt!=NULL){ - fprintf(f, ", "); - w+=2; - } - } - - /* long opt */ - - if(opt->longopt!=NULL){ - if(midlong){ - w++; - fprintf(f, "-%s", opt->longopt); - }else{ - w+=2; - fprintf(f, "--%s", opt->longopt); - } - w+=strlen(opt->longopt); - } - - /* arg */ - - if(O_ARGS(opt)){ - w++; - if(opt->longopt!=NULL && !midlong) - putc('=', f); - else - putc(' ', f); - - if(O_OPT_ARG(opt)){ - w+=2; - putc('[', f); - } - - if(opt->argname!=NULL){ - fprintf(f, "%s", opt->argname); - w+=strlen(opt->argname); - }else{ - w+=3; - fprintf(f, "ARG"); - } + FILE *f=stdout; + const char *p, *p2, *p3; + uint w=0; + + fprintf(f, SPACER1); + + /* short opt */ + + if((O_ID(opt)&OPT_ID_NOSHORT_FLAG)==0){ + fprintf(f, "-%c", O_ID(opt)&~OPT_ID_RESERVED_FLAG); + w+=2; + + if(opt->longopt!=NULL){ + fprintf(f, ", "); + w+=2; + } + } + + /* long opt */ + + if(opt->longopt!=NULL){ + if(midlong){ + w++; + fprintf(f, "-%s", opt->longopt); + }else{ + w+=2; + fprintf(f, "--%s", opt->longopt); + } + w+=strlen(opt->longopt); + } + + /* arg */ + + if(O_ARGS(opt)){ + w++; + if(opt->longopt!=NULL && !midlong) + putc('=', f); + else + putc(' ', f); + + if(O_OPT_ARG(opt)){ + w+=2; + putc('[', f); + } + + if(opt->argname!=NULL){ + fprintf(f, "%s", opt->argname); + w+=strlen(opt->argname); + }else{ + w+=3; + fprintf(f, "ARG"); + } - if(O_OPT_ARG(opt)) - putc(']', f); - } - - while(w++<maxw) - putc(' ', f); - - /* descr */ - - p=p2=opt->descr; - - if(p==NULL){ - putc('\n', f); - return; - } - - fprintf(f, SPACER2); + if(O_OPT_ARG(opt)) + putc(']', f); + } + + while(w++<maxw) + putc(' ', f); + + /* descr */ + + p=p2=opt->descr; + + if(p==NULL){ + putc('\n', f); + return; + } + + fprintf(f, SPACER2); - maxw+=OFF1+OFF2; - tw-=maxw; - - while(strlen(p)>tw){ - p3=p2=p+tw-2; - - while(*p2!=' ' && p2!=p) - p2--; - - while(*p3!=' ' && *p3!='\0') - p3++; - - if((uint)(p3-p2)>tw){ - /* long word - just wrap */ - p2=p+tw-2; - } - - writef(f, p, p2-p); - if(*p2==' ') - putc('\n', f); - else - fprintf(f, "\\\n"); - - p=p2+1; - - w=maxw; - while(w--) - putc(' ', f); - } - - fprintf(f, "%s\n", p); + maxw+=OFF1+OFF2; + tw-=maxw; + + while(strlen(p)>tw){ + p3=p2=p+tw-2; + + while(*p2!=' ' && p2!=p) + p2--; + + while(*p3!=' ' && *p3!='\0') + p3++; + + if((uint)(p3-p2)>tw){ + /* long word - just wrap */ + p2=p+tw-2; + } + + writef(f, p, p2-p); + if(*p2==' ') + putc('\n', f); + else + fprintf(f, "\\\n"); + + p=p2+1; + + w=maxw; + while(w--) + putc(' ', f); + } + + fprintf(f, "%s\n", p); } - + static void print_opts(const OptParserOpt *opts, bool midlong, - const OptParserCommonInfo *cinfo) + const OptParserCommonInfo *cinfo) { - uint w, maxw=0; - const OptParserOpt *o; - - o=opts; + uint w, maxw=0; + const OptParserOpt *o; + + o=opts; again: - for(; O_ID(o); o++){ - w=opt_w(o, midlong); - if(w>maxw) - maxw=w; - } - - if(o!=&(common_opts[3])){ - o=common_opts; - goto again; - } + for(; O_ID(o); o++){ + w=opt_w(o, midlong); + if(w>maxw) + maxw=w; + } + + if(o!=&(common_opts[3])){ + o=common_opts; + goto again; + } - o=opts; + o=opts; again2: - for(; O_ID(o); o++) - print_opt(o, midlong, maxw, TERM_W); - - if(o!=&(common_opts[3])){ - printf("\n"); - o=common_opts; - goto again2; - } + for(; O_ID(o); o++) + print_opt(o, midlong, maxw, TERM_W); + + if(o!=&(common_opts[3])){ + printf("\n"); + o=common_opts; + goto again2; + } } static void print_help(const OptParserOpt *opts, bool midlong, - const OptParserCommonInfo *cinfo) + const OptParserCommonInfo *cinfo) { - const char *tmp, *p=cinfo->usage_tmpl; - size_t len; - size_t start; - - while(1){ - tmp=strchr(p, '$'); + const char *tmp, *p=cinfo->usage_tmpl; + size_t len; + size_t start; + + while(1){ + tmp=strchr(p, '$'); - if(tmp==NULL){ - writef(stdout, p, strlen(p)); - return; - } - - if(tmp!=p) - writef(stdout, p, tmp-p); - - p=tmp+1; - - if(*p=='p'){ - tmp=prog_execname(); - writef(stdout, tmp, strlen(tmp)); - }else if(*p=='o'){ - print_opts(opts, midlong, cinfo); - } - p++; - } + if(tmp==NULL){ + writef(stdout, p, strlen(p)); + return; + } + + if(tmp!=p) + writef(stdout, p, tmp-p); + + p=tmp+1; + + if(*p=='p'){ + tmp=prog_execname(); + writef(stdout, tmp, strlen(tmp)); + }else if(*p=='o'){ + print_opts(opts, midlong, cinfo); + } + p++; + } }
--- a/optparser.h Mon Feb 16 18:09:03 2004 +0100 +++ b/optparser.h Mon Feb 16 18:50:28 2004 +0100 @@ -16,67 +16,67 @@ #define OPT_ID_NOSHORT_FLAG 0x10000 #define OPT_ID_RESERVED_FLAG 0x20000 -#define OPT_ID(X) ((X)|OPT_ID_NOSHORT_FLAG) -#define OPT_ID_RESERVED(X) ((X)|OPT_ID_RESERVED_FLAG) +#define OPT_ID(X) ((X)|OPT_ID_NOSHORT_FLAG) +#define OPT_ID_RESERVED(X) ((X)|OPT_ID_RESERVED_FLAG) /* OPTP_CHAIN is the normal behavior, i.e. single-letter options can be - * "chained" together: 'lr -lR'. Use for normal command line programs. + * "chained" together: 'lr -lR'. Use for normal command line programs. * OPTP_MIDLONG allows '-display foo' -like args but disables chaining - * of single-letter options. X programs should probably use this. + * of single-letter options. X programs should probably use this. * OPTP_IMMEDIATE allows immediate arguments (-I/usr/include) (and disables - * chaining and midlong options). + * chaining and midlong options). * OPTP_NO_DASH is the same as OPTP_CHAIN but allows the dash to be omitted - * for 'tar xzf foo' -like behavior. + * for 'tar xzf foo' -like behavior. * Long '--foo=bar' options are supported in all of the modes. */ enum{ - OPTP_CHAIN=0, - OPTP_DEFAULT=0, - OPTP_MIDLONG=1, - OPTP_IMMEDIATE=2, - OPTP_NO_DASH=3 + OPTP_CHAIN=0, + OPTP_DEFAULT=0, + OPTP_MIDLONG=1, + OPTP_IMMEDIATE=2, + OPTP_NO_DASH=3 }; - + enum{ - OPT_ARG=1, /* option has an argument */ - OPT_OPT_ARG=3 /* option may have an argument */ + OPT_ARG=1, /* option has an argument */ + OPT_OPT_ARG=3 /* option may have an argument */ }; #define END_OPTPARSEROPTS {0, NULL, 0, NULL, NULL} typedef struct _OptParserOpt{ - int optid; - const char *longopt; - int flags; - const char *argname; - const char *descr; + int optid; + const char *longopt; + int flags; + const char *argname; + const char *descr; } OptParserOpt; typedef struct _OptParserCommonInfo{ - const char *version; - const char *usage_tmpl; - const char *about; + const char *version; + const char *usage_tmpl; + const char *about; } OptParserCommonInfo; enum{ - OPT_ID_END=0, - OPT_ID_ARGUMENT=1, + OPT_ID_END=0, + OPT_ID_ARGUMENT=1, - E_OPT_INVALID_OPTION=-1, - E_OPT_INVALID_CHAIN_OPTION=-2, - E_OPT_SYNTAX_ERROR=-3, - E_OPT_MISSING_ARGUMENT=-4, - E_OPT_UNEXPECTED_ARGUMENT=-5 + E_OPT_INVALID_OPTION=-1, + E_OPT_INVALID_CHAIN_OPTION=-2, + E_OPT_SYNTAX_ERROR=-3, + E_OPT_MISSING_ARGUMENT=-4, + E_OPT_UNEXPECTED_ARGUMENT=-5 }; extern void optparser_init(int argc, char *const argv[], int mode, - const OptParserOpt *opts, - const OptParserCommonInfo *cinfo); + const OptParserOpt *opts, + const OptParserCommonInfo *cinfo); extern int optparser_get_opt(); extern const char* optparser_get_arg();
--- a/output.c Mon Feb 16 18:09:03 2004 +0100 +++ b/output.c Mon Feb 16 18:50:28 2004 +0100 @@ -43,66 +43,66 @@ void verbose(const char *p, ...) { - va_list args; - - va_start(args, p); - - verbose_v(p, args); - - va_end(args); + va_list args; + + va_start(args, p); + + verbose_v(p, args); + + va_end(args); } - + void verbose_v(const char *p, va_list args) { - int i; - - if(verbose_mode){ - for(i=0; i<verbose_indent_lvl; i++) - writef(stdout, indentator, INDENTATOR_LENGTH); - - vprintf(p, args); - fflush(stdout); - } + int i; + + if(verbose_mode){ + for(i=0; i<verbose_indent_lvl; i++) + writef(stdout, indentator, INDENTATOR_LENGTH); + + vprintf(p, args); + fflush(stdout); + } } void verbose_enable(bool enable) { - verbose_mode=enable; + verbose_mode=enable; } int verbose_indent(int depth) { - int old=verbose_indent_lvl; - - if(depth>=0) - verbose_indent_lvl=depth; - - return old; + int old=verbose_indent_lvl; + + if(depth>=0) + verbose_indent_lvl=depth; + + return old; } - + void warn_progname_enable(bool enable) { - progname_enable=enable; + progname_enable=enable; } static void put_prog_name() { - const char*progname; - - if(!progname_enable) - return; - - progname=prog_execname(); - - if(progname==NULL) - return; - - fprintf(stderr, "%s: ", (char*)progname); + const char*progname; + + if(!progname_enable) + return; + + progname=prog_execname(); + + if(progname==NULL) + return; + + fprintf(stderr, "%s: ", (char*)progname); } /* warn @@ -111,89 +111,89 @@ static void fallback_warn() { - put_prog_name(); - fprintf(stderr, "Oops. Error string compilation failed: %s", - strerror(errno)); + put_prog_name(); + fprintf(stderr, "Oops. Error string compilation failed: %s", + strerror(errno)); } - - + + #define CALL_V(NAME, ARGS) \ - do { va_list args; va_start(args, p); NAME ARGS; va_end(args); } while(0) + do { va_list args; va_start(args, p); NAME ARGS; va_end(args); } while(0) #define DO_DISPATCH(NAME, ARGS) \ - do{ \ - char *msg; \ - if((msg=NAME ARGS)!=NULL){ \ - do_dispatch_message(msg); \ - free(msg);\ - }else{ \ - fallback_warn(); \ - } \ - }while(0) + do{ \ + char *msg; \ + if((msg=NAME ARGS)!=NULL){ \ + do_dispatch_message(msg); \ + free(msg);\ + }else{ \ + fallback_warn(); \ + } \ + }while(0) void libtu_asprintf(char **ret, const char *p, ...) { - CALL_V(vasprintf, (ret, p, args)); + CALL_V(vasprintf, (ret, p, args)); } void libtu_vasprintf(char **ret, const char *p, va_list args) { - vasprintf(ret, p, args); + vasprintf(ret, p, args); } void warn(const char *p, ...) { - CALL_V(warn_v, (p, args)); + CALL_V(warn_v, (p, args)); } void warn_obj(const char *obj, const char *p, ...) { - CALL_V(warn_obj_v, (obj, p, args)); + CALL_V(warn_obj_v, (obj, p, args)); } void warn_obj_line(const char *obj, int line, const char *p, ...) { - CALL_V(warn_obj_line_v, (obj, line, p, args)); + CALL_V(warn_obj_line_v, (obj, line, p, args)); } void warn_obj_v(const char *obj, const char *p, va_list args) { - warn_obj_line_v(obj, -1, p, args); + warn_obj_line_v(obj, -1, p, args); } void warn_v(const char *p, va_list args) { - DO_DISPATCH(errmsg_v, (p, args)); + DO_DISPATCH(errmsg_v, (p, args)); } void warn_obj_line_v(const char *obj, int line, const char *p, va_list args) { - DO_DISPATCH(errmsg_obj_line_v, (obj, line, p, args)); + DO_DISPATCH(errmsg_obj_line_v, (obj, line, p, args)); } void warn_err() { - DO_DISPATCH(errmsg_err, ()); + DO_DISPATCH(errmsg_err, ()); } void warn_err_obj(const char *obj) { - DO_DISPATCH(errmsg_err_obj, (obj)); + DO_DISPATCH(errmsg_err_obj, (obj)); } void warn_err_obj_line(const char *obj, int line) { - DO_DISPATCH(errmsg_err_obj_line, (obj, line)); + DO_DISPATCH(errmsg_err_obj_line, (obj, line)); } @@ -201,102 +201,102 @@ */ #define CALL_V_RET(NAME, ARGS) \ - char *ret; va_list args; va_start(args, p); ret=NAME ARGS; \ - va_end(args); return ret; + char *ret; va_list args; va_start(args, p); ret=NAME ARGS; \ + va_end(args); return ret; char* errmsg(const char *p, ...) { - CALL_V_RET(errmsg_v, (p, args)); + CALL_V_RET(errmsg_v, (p, args)); } char *errmsg_obj(const char *obj, const char *p, ...) { - CALL_V_RET(errmsg_obj_v, (obj, p, args)); + CALL_V_RET(errmsg_obj_v, (obj, p, args)); } char *errmsg_obj_line(const char *obj, int line, const char *p, ...) { - CALL_V_RET(errmsg_obj_line_v, (obj, line, p, args)); + CALL_V_RET(errmsg_obj_line_v, (obj, line, p, args)); } char* errmsg_obj_v(const char *obj, const char *p, va_list args) { - return errmsg_obj_line_v(obj, -1, p, args); + return errmsg_obj_line_v(obj, -1, p, args); } char *errmsg_v(const char *p, va_list args) { - char *res; - vasprintf(&res, p, args); - return res; + char *res; + vasprintf(&res, p, args); + return res; } char *errmsg_obj_line_v(const char *obj, int line, const char *p, va_list args) { - char *res1=NULL, *res2, *res3; - - if(obj!=NULL){ - if(line>0) - asprintf(&res1, TR("%s:%d: "), obj, line); - else - asprintf(&res1, "%s: ", obj); - }else{ - if(line>0) - asprintf(&res1, TR("%d: "), line); - } - vasprintf(&res2, p, args); - if(res1!=NULL){ - if(res2==NULL) - return NULL; - res3=scat(res1, res2); - free(res1); - free(res2); - return res3; - } - return res2; + char *res1=NULL, *res2, *res3; + + if(obj!=NULL){ + if(line>0) + asprintf(&res1, TR("%s:%d: "), obj, line); + else + asprintf(&res1, "%s: ", obj); + }else{ + if(line>0) + asprintf(&res1, TR("%d: "), line); + } + vasprintf(&res2, p, args); + if(res1!=NULL){ + if(res2==NULL) + return NULL; + res3=scat(res1, res2); + free(res1); + free(res2); + return res3; + } + return res2; } char *errmsg_err() { - char *res; - asprintf(&res, "%s\n", strerror(errno)); - return res; + char *res; + asprintf(&res, "%s\n", strerror(errno)); + return res; } char *errmsg_err_obj(const char *obj) { - char *res; - if(obj!=NULL) - asprintf(&res, "%s: %s\n", obj, strerror(errno)); - else - asprintf(&res, "%s\n", strerror(errno)); - return res; + char *res; + if(obj!=NULL) + asprintf(&res, "%s: %s\n", obj, strerror(errno)); + else + asprintf(&res, "%s\n", strerror(errno)); + return res; } char *errmsg_err_obj_line(const char *obj, int line) { - char *res; - if(obj!=NULL){ - if(line>0) - asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno)); - else - asprintf(&res, "%s: %s\n", obj, strerror(errno)); - }else{ - if(line>0) - asprintf(&res, TR("%d: %s\n"), line, strerror(errno)); - else - asprintf(&res, TR("%s\n"), strerror(errno)); - } - return res; + char *res; + if(obj!=NULL){ + if(line>0) + asprintf(&res, TR("%s:%d: %s\n"), obj, line, strerror(errno)); + else + asprintf(&res, "%s: %s\n", obj, strerror(errno)); + }else{ + if(line>0) + asprintf(&res, TR("%d: %s\n"), line, strerror(errno)); + else + asprintf(&res, TR("%s\n"), strerror(errno)); + } + return res; } @@ -306,93 +306,93 @@ void die(const char *p, ...) { - set_warn_handler(NULL); - CALL_V(die_v, (p, args)); + set_warn_handler(NULL); + CALL_V(die_v, (p, args)); } void die_v(const char *p, va_list args) { - set_warn_handler(NULL); - warn_v(p, args); - exit(EXIT_FAILURE); + set_warn_handler(NULL); + warn_v(p, args); + exit(EXIT_FAILURE); } void die_obj(const char *obj, const char *p, ...) { - set_warn_handler(NULL); - CALL_V(die_obj_v, (obj, p, args)); + set_warn_handler(NULL); + CALL_V(die_obj_v, (obj, p, args)); } void die_obj_line(const char *obj, int line, const char *p, ...) { - set_warn_handler(NULL); - CALL_V(die_obj_line_v, (obj, line, p, args)); + set_warn_handler(NULL); + CALL_V(die_obj_line_v, (obj, line, p, args)); } void die_obj_v(const char *obj, const char *p, va_list args) { - set_warn_handler(NULL); - warn_obj_v(obj, p, args); - exit(EXIT_FAILURE); + set_warn_handler(NULL); + warn_obj_v(obj, p, args); + exit(EXIT_FAILURE); } void die_obj_line_v(const char *obj, int line, const char *p, va_list args) { - set_warn_handler(NULL); - warn_obj_line_v(obj, line, p, args); - exit(EXIT_FAILURE); + set_warn_handler(NULL); + warn_obj_line_v(obj, line, p, args); + exit(EXIT_FAILURE); } void die_err() { - set_warn_handler(NULL); - warn_err(); - exit(EXIT_FAILURE); + set_warn_handler(NULL); + warn_err(); + exit(EXIT_FAILURE); } void die_err_obj(const char *obj) { - set_warn_handler(NULL); - warn_err_obj(obj); - exit(EXIT_FAILURE); + set_warn_handler(NULL); + warn_err_obj(obj); + exit(EXIT_FAILURE); } void die_err_obj_line(const char *obj, int line) { - set_warn_handler(NULL); - warn_err_obj_line(obj, line); - exit(EXIT_FAILURE); + set_warn_handler(NULL); + warn_err_obj_line(obj, line); + exit(EXIT_FAILURE); } static void default_warn_handler(const char *message) { - put_prog_name(); - fprintf(stderr, "%s", message); - putc('\n', stderr); + put_prog_name(); + fprintf(stderr, "%s", message); + putc('\n', stderr); } static void do_dispatch_message(const char *message) { - if(current_warn_handler!=NULL) - current_warn_handler(message); - else - default_warn_handler(message); + if(current_warn_handler!=NULL) + current_warn_handler(message); + else + default_warn_handler(message); } WarnHandler *set_warn_handler(WarnHandler *handler) { - WarnHandler *old=current_warn_handler; - current_warn_handler=(handler!=NULL ? handler : default_warn_handler); - return old; + WarnHandler *old=current_warn_handler; + current_warn_handler=(handler!=NULL ? handler : default_warn_handler); + return old; }
--- a/parser.c Mon Feb 16 18:09:03 2004 +0100 +++ b/parser.c Mon Feb 16 18:50:28 2004 +0100 @@ -14,18 +14,18 @@ #include <libtu/misc.h> #include <libtu/output.h> -#define MAX_TOKENS 256 -#define MAX_NEST 256 +#define MAX_TOKENS 256 +#define MAX_NEST 256 enum{ - P_NONE=1, - P_EOF, - P_STMT, - P_STMT_NS, - P_STMT_SECT, - P_BEG_SECT, - P_END_SECT + P_NONE=1, + P_EOF, + P_STMT, + P_STMT_NS, + P_STMT_SECT, + P_BEG_SECT, + P_END_SECT }; @@ -36,8 +36,8 @@ static ConfOpt common_opts[]={ - {"include", "s", opt_include, NULL}, - {NULL, NULL, NULL, NULL} + {"include", "s", opt_include, NULL}, + {NULL, NULL, NULL, NULL} }; @@ -46,130 +46,130 @@ static int read_statement(Tokenizer *tokz, Token *tokens, int *ntok_ret) { - int ntokens=0; - Token *tok=NULL; - int had_comma=0; /* 0 - no, 1 - yes, 2 - not had, not expected */ - int retval=0; - int e=0; + int ntokens=0; + Token *tok=NULL; + int had_comma=0; /* 0 - no, 1 - yes, 2 - not had, not expected */ + int retval=0; + int e=0; - while(1){ - tok=&tokens[ntokens]; - - if(!tokz_get_token(tokz, tok)){ - e=1; - continue; - } + while(1){ + tok=&tokens[ntokens]; + + if(!tokz_get_token(tokz, tok)){ + e=1; + continue; + } - if(ntokens==MAX_TOKENS-1){ - e=E_TOKZ_TOKEN_LIMIT; - tokz_warn_error(tokz, tok->line, e); - if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) - break; - }else{ - ntokens++; - } - - if(!TOK_IS_OP(tok)){ - if(ntokens==1 && !had_comma){ - /* first token */ - had_comma=2; - }else{ - if(had_comma==0) - goto syntax; - - had_comma=0; - } - continue; - } - - /* It is an operator */ - ntokens--; - - switch(TOK_OP_VAL(tok)){ - case OP_SCOLON: - retval=(ntokens==0 ? P_NONE : P_STMT_NS); - break; - - case OP_NEXTLINE: - retval=(ntokens==0 ? P_NONE : P_STMT); - break; - - case OP_L_BRC: - retval=(ntokens==0 ? P_BEG_SECT : P_STMT_SECT); - break; - - case OP_R_BRC: - if(ntokens==0){ - retval=P_END_SECT; - }else{ - tokz_unget_token(tokz, tok); - retval=P_STMT_NS; - } - break; + if(ntokens==MAX_TOKENS-1){ + e=E_TOKZ_TOKEN_LIMIT; + tokz_warn_error(tokz, tok->line, e); + if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) + break; + }else{ + ntokens++; + } + + if(!TOK_IS_OP(tok)){ + if(ntokens==1 && !had_comma){ + /* first token */ + had_comma=2; + }else{ + if(had_comma==0) + goto syntax; + + had_comma=0; + } + continue; + } + + /* It is an operator */ + ntokens--; + + switch(TOK_OP_VAL(tok)){ + case OP_SCOLON: + retval=(ntokens==0 ? P_NONE : P_STMT_NS); + break; + + case OP_NEXTLINE: + retval=(ntokens==0 ? P_NONE : P_STMT); + break; + + case OP_L_BRC: + retval=(ntokens==0 ? P_BEG_SECT : P_STMT_SECT); + break; + + case OP_R_BRC: + if(ntokens==0){ + retval=P_END_SECT; + }else{ + tokz_unget_token(tokz, tok); + retval=P_STMT_NS; + } + break; - case OP_EOF: - retval=(ntokens==0 ? P_EOF : P_STMT_NS); - - if(had_comma==1){ - e=E_TOKZ_UNEXPECTED_EOF; - goto handle_error; - } - - goto end; - - case OP_COMMA: - if(had_comma!=0) - goto syntax; + case OP_EOF: + retval=(ntokens==0 ? P_EOF : P_STMT_NS); + + if(had_comma==1){ + e=E_TOKZ_UNEXPECTED_EOF; + goto handle_error; + } + + goto end; + + case OP_COMMA: + if(had_comma!=0) + goto syntax; - had_comma=1; - continue; - - default: - goto syntax; - } - - if(had_comma!=1) - break; - - syntax: - e=E_TOKZ_SYNTAX; - handle_error: - tokz_warn_error(tokz, tok->line, e); - - if(!(tokz->flags&TOKZ_ERROR_TOLERANT) || retval!=0) - break; - } - + had_comma=1; + continue; + + default: + goto syntax; + } + + if(had_comma!=1) + break; + + syntax: + e=E_TOKZ_SYNTAX; + handle_error: + tokz_warn_error(tokz, tok->line, e); + + if(!(tokz->flags&TOKZ_ERROR_TOLERANT) || retval!=0) + break; + } + end: - if(e!=0) - retval=-retval; - - *ntok_ret=ntokens; - - return retval; + if(e!=0) + retval=-retval; + + *ntok_ret=ntokens; + + return retval; } static bool find_beg_sect(Tokenizer *tokz) { - Token tok=TOK_INIT; + Token tok=TOK_INIT; - while(tokz_get_token(tokz, &tok)){ - if(TOK_IS_OP(&tok)){ - if(TOK_OP_VAL(&tok)==OP_NEXTLINE) - continue; + while(tokz_get_token(tokz, &tok)){ + if(TOK_IS_OP(&tok)){ + if(TOK_OP_VAL(&tok)==OP_NEXTLINE) + continue; - if(TOK_OP_VAL(&tok)==OP_SCOLON) - return FALSE; - - if(TOK_OP_VAL(&tok)==OP_L_BRC) - return TRUE; - } - - tokz_unget_token(tokz, &tok); - break; - } - return FALSE; + if(TOK_OP_VAL(&tok)==OP_SCOLON) + return FALSE; + + if(TOK_OP_VAL(&tok)==OP_L_BRC) + return TRUE; + } + + tokz_unget_token(tokz, &tok); + break; + } + return FALSE; } @@ -178,223 +178,223 @@ static const ConfOpt* lookup_option(const ConfOpt *opts, const char *name) { - while(opts->optname!=NULL){ - if(strcmp(opts->optname, name)==0) - return opts; - opts++; - } - return NULL; + while(opts->optname!=NULL){ + if(strcmp(opts->optname, name)==0) + return opts; + opts++; + } + return NULL; } - + static bool call_end_sect(Tokenizer *tokz, const ConfOpt *opts) -{ - opts=lookup_option(opts, "#end"); - if(opts!=NULL) - return opts->fn(tokz, 0, NULL); - - return TRUE; +{ + opts=lookup_option(opts, "#end"); + if(opts!=NULL) + return opts->fn(tokz, 0, NULL); + + return TRUE; } static bool call_cancel_sect(Tokenizer *tokz, const ConfOpt *opts) { - opts=lookup_option(opts, "#cancel"); - if(opts!=NULL) - return opts->fn(tokz, 0, NULL); - - return TRUE; + opts=lookup_option(opts, "#cancel"); + if(opts!=NULL) + return opts->fn(tokz, 0, NULL); + + return TRUE; } - + /* */ bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options) { - Token tokens[MAX_TOKENS]; - bool alloced_optstack=FALSE; - int i, t, ntokens=0; - int init_nest_lvl; - bool had_error; - int errornest=0; - bool is_default=FALSE; + Token tokens[MAX_TOKENS]; + bool alloced_optstack=FALSE; + int i, t, ntokens=0; + int init_nest_lvl; + bool had_error; + int errornest=0; + bool is_default=FALSE; - /* Allocate tokz->optstack if it does not yet exist (if it does, - * we have been called from an option handler) - */ - if(!tokz->optstack){ - tokz->optstack=ALLOC_N(const ConfOpt*, MAX_NEST); - if(!tokz->optstack){ - warn_err(); - return FALSE; - } - - memset(tokz->optstack, 0, sizeof(ConfOpt*)*MAX_NEST); - init_nest_lvl=tokz->nest_lvl=0; - alloced_optstack=TRUE; - }else{ - init_nest_lvl=tokz->nest_lvl; - } - - tokz->optstack[init_nest_lvl]=options; - - for(i=0; i<MAX_TOKENS; i++) - tok_init(&tokens[i]); + /* Allocate tokz->optstack if it does not yet exist (if it does, + * we have been called from an option handler) + */ + if(!tokz->optstack){ + tokz->optstack=ALLOC_N(const ConfOpt*, MAX_NEST); + if(!tokz->optstack){ + warn_err(); + return FALSE; + } + + memset(tokz->optstack, 0, sizeof(ConfOpt*)*MAX_NEST); + init_nest_lvl=tokz->nest_lvl=0; + alloced_optstack=TRUE; + }else{ + init_nest_lvl=tokz->nest_lvl; + } + + tokz->optstack[init_nest_lvl]=options; + + for(i=0; i<MAX_TOKENS; i++) + tok_init(&tokens[i]); - - while(1){ - had_error=FALSE; + + while(1){ + had_error=FALSE; - /* free the tokens */ - while(ntokens--) - tok_free(&tokens[ntokens]); - - /* read the tokens */ - t=read_statement(tokz, tokens, &ntokens); - - if((had_error=t<0)) - t=-t; - - switch(t){ - case P_STMT: - case P_STMT_NS: - case P_STMT_SECT: + /* free the tokens */ + while(ntokens--) + tok_free(&tokens[ntokens]); + + /* read the tokens */ + t=read_statement(tokz, tokens, &ntokens); + + if((had_error=t<0)) + t=-t; + + switch(t){ + case P_STMT: + case P_STMT_NS: + case P_STMT_SECT: - if(errornest) - had_error=TRUE; - else if(tokz->flags&TOKZ_PARSER_INDENT_MODE) - verbose_indent(tokz->nest_lvl); - - if(!TOK_IS_IDENT(tokens+0)){ - had_error=TRUE; - tokz_warn_error(tokz, tokens->line, - E_TOKZ_IDENTIFIER_EXPECTED); - } - - if(t==P_STMT){ - if(find_beg_sect(tokz)) - t=P_STMT_SECT; - } - - if(had_error) - break; + if(errornest) + had_error=TRUE; + else if(tokz->flags&TOKZ_PARSER_INDENT_MODE) + verbose_indent(tokz->nest_lvl); + + if(!TOK_IS_IDENT(tokens+0)){ + had_error=TRUE; + tokz_warn_error(tokz, tokens->line, + E_TOKZ_IDENTIFIER_EXPECTED); + } + + if(t==P_STMT){ + if(find_beg_sect(tokz)) + t=P_STMT_SECT; + } + + if(had_error) + break; - /* Got the statement and its type */ - - options=lookup_option(tokz->optstack[tokz->nest_lvl], - TOK_IDENT_VAL(tokens+0)); - if(options==NULL) - options=lookup_option(common_opts, TOK_IDENT_VAL(tokens+0)); - if(options==NULL && (tokz->flags&TOKZ_DEFAULT_OPTION)){ - options=lookup_option(tokz->optstack[tokz->nest_lvl], "#default"); - is_default=(options!=NULL); - } + /* Got the statement and its type */ + + options=lookup_option(tokz->optstack[tokz->nest_lvl], + TOK_IDENT_VAL(tokens+0)); + if(options==NULL) + options=lookup_option(common_opts, TOK_IDENT_VAL(tokens+0)); + if(options==NULL && (tokz->flags&TOKZ_DEFAULT_OPTION)){ + options=lookup_option(tokz->optstack[tokz->nest_lvl], "#default"); + is_default=(options!=NULL); + } - if(options==NULL){ - had_error=TRUE; - tokz_warn_error(tokz, tokens->line, E_TOKZ_UNKNOWN_OPTION); - }else if(!is_default) { - had_error=!check_args(tokz, tokens, ntokens, options->argfmt); - } - - if(had_error) - break; - - /* Found the option and arguments are ok */ - - if(options->opts!=NULL){ - if(t!=P_STMT_SECT){ - had_error=TRUE; - tokz_warn_error(tokz, tokz->line, E_TOKZ_LBRACE_EXPECTED); - }else if(tokz->nest_lvl==MAX_NEST-1){ - tokz_warn_error(tokz, tokz->line, E_TOKZ_MAX_NEST); - had_error=TRUE; - }else{ - tokz->nest_lvl++; - tokz->optstack[tokz->nest_lvl]=options->opts; - } - }else if(t==P_STMT_SECT){ - had_error=TRUE; - tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); - } - - if(!had_error && options->fn!=NULL){ - had_error=!options->fn(tokz, ntokens, tokens); - if(t==P_STMT_SECT && had_error) - tokz->nest_lvl--; - } - break; - - case P_EOF: - if(tokz_popf(tokz)){ - break; - }else if(tokz->nest_lvl>0 || errornest>0){ - tokz_warn_error(tokz, 0, E_TOKZ_UNEXPECTED_EOF); - had_error=TRUE; - } - goto eof; - - case P_BEG_SECT: - had_error=TRUE; - errornest++; - tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); - break; - - case P_END_SECT: - if(tokz->nest_lvl+errornest==0){ - tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); - had_error=TRUE; - } - - if(had_error) - break; - - if(errornest!=0){ - errornest--; - }else{ - had_error=!call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); - tokz->nest_lvl--; - } - - if(tokz->nest_lvl<init_nest_lvl) - goto eof; - } - - if(!had_error) - continue; - - if(t==P_STMT_SECT) - errornest++; - - if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) - break; - } + if(options==NULL){ + had_error=TRUE; + tokz_warn_error(tokz, tokens->line, E_TOKZ_UNKNOWN_OPTION); + }else if(!is_default) { + had_error=!check_args(tokz, tokens, ntokens, options->argfmt); + } + + if(had_error) + break; + + /* Found the option and arguments are ok */ + + if(options->opts!=NULL){ + if(t!=P_STMT_SECT){ + had_error=TRUE; + tokz_warn_error(tokz, tokz->line, E_TOKZ_LBRACE_EXPECTED); + }else if(tokz->nest_lvl==MAX_NEST-1){ + tokz_warn_error(tokz, tokz->line, E_TOKZ_MAX_NEST); + had_error=TRUE; + }else{ + tokz->nest_lvl++; + tokz->optstack[tokz->nest_lvl]=options->opts; + } + }else if(t==P_STMT_SECT){ + had_error=TRUE; + tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); + } + + if(!had_error && options->fn!=NULL){ + had_error=!options->fn(tokz, ntokens, tokens); + if(t==P_STMT_SECT && had_error) + tokz->nest_lvl--; + } + break; + + case P_EOF: + if(tokz_popf(tokz)){ + break; + }else if(tokz->nest_lvl>0 || errornest>0){ + tokz_warn_error(tokz, 0, E_TOKZ_UNEXPECTED_EOF); + had_error=TRUE; + } + goto eof; + + case P_BEG_SECT: + had_error=TRUE; + errornest++; + tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); + break; + + case P_END_SECT: + if(tokz->nest_lvl+errornest==0){ + tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); + had_error=TRUE; + } + + if(had_error) + break; + + if(errornest!=0){ + errornest--; + }else{ + had_error=!call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); + tokz->nest_lvl--; + } + + if(tokz->nest_lvl<init_nest_lvl) + goto eof; + } + + if(!had_error) + continue; + + if(t==P_STMT_SECT) + errornest++; + + if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) + break; + } eof: - /* free the tokens */ - while(ntokens--) - tok_free(&tokens[ntokens]); + /* free the tokens */ + while(ntokens--) + tok_free(&tokens[ntokens]); - while(tokz->nest_lvl>=init_nest_lvl){ - if(tokz->flags&TOKZ_ERROR_TOLERANT || !had_error) - call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); - else - call_cancel_sect(tokz, tokz->optstack[tokz->nest_lvl]); - tokz->nest_lvl--; - } - - /* Free optstack if it was alloced by this call */ - if(alloced_optstack){ - free(tokz->optstack); - tokz->optstack=NULL; - tokz->nest_lvl=0; - } - - if(tokz->flags&TOKZ_PARSER_INDENT_MODE) - verbose_indent(init_nest_lvl); - - return !had_error; + while(tokz->nest_lvl>=init_nest_lvl){ + if(tokz->flags&TOKZ_ERROR_TOLERANT || !had_error) + call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); + else + call_cancel_sect(tokz, tokz->optstack[tokz->nest_lvl]); + tokz->nest_lvl--; + } + + /* Free optstack if it was alloced by this call */ + if(alloced_optstack){ + free(tokz->optstack); + tokz->optstack=NULL; + tokz->nest_lvl=0; + } + + if(tokz->flags&TOKZ_PARSER_INDENT_MODE) + verbose_indent(init_nest_lvl); + + return !had_error; } @@ -403,41 +403,41 @@ bool parse_config(const char *fname, const ConfOpt *options, int flags) { - Tokenizer *tokz; - bool ret; - - tokz=tokz_open(fname); - - if(tokz==NULL) - return FALSE; + Tokenizer *tokz; + bool ret; + + tokz=tokz_open(fname); + + if(tokz==NULL) + return FALSE; - tokz->flags|=flags&~TOKZ_READ_COMMENTS; - - ret=parse_config_tokz(tokz, options); - - tokz_close(tokz); - - return ret; + tokz->flags|=flags&~TOKZ_READ_COMMENTS; + + ret=parse_config_tokz(tokz, options); + + tokz_close(tokz); + + return ret; } bool parse_config_file(FILE *file, const ConfOpt *options, int flags) { - Tokenizer *tokz; - bool ret; - - tokz=tokz_open_file(file, NULL); - - if(tokz==NULL) - return FALSE; - - tokz->flags|=flags&~TOKZ_READ_COMMENTS; - - ret=parse_config_tokz(tokz, options); - - tokz_close(tokz); - - return ret; + Tokenizer *tokz; + bool ret; + + tokz=tokz_open_file(file, NULL); + + if(tokz==NULL) + return FALSE; + + tokz->flags|=flags&~TOKZ_READ_COMMENTS; + + ret=parse_config_tokz(tokz, options); + + tokz_close(tokz); + + return ret; } @@ -448,177 +448,177 @@ static int arg_match(Token *tok, char c, bool si) { - char c2=tok->type; - - if(c=='.' || c=='*') - return 0; - - if(c2==c) - return 0; - - if(si){ - if(c2=='i' && c=='s'){ - TOK_SET_IDENT(tok, TOK_STRING_VAL(tok)); - return 0; - } - - if(c2=='s' && c=='i'){ - TOK_SET_STRING(tok, TOK_IDENT_VAL(tok)); - return 0; - } - } - - if(c2=='c' && c=='l'){ - TOK_SET_LONG(tok, TOK_CHAR_VAL(tok)); - return 0; - } - - if(c2=='l' && c=='c'){ - TOK_SET_CHAR(tok, TOK_LONG_VAL(tok)); - return 0; - } - - if(c2=='l' && c=='d'){ - TOK_SET_DOUBLE(tok, TOK_LONG_VAL(tok)); - return 0; - } - - if(c=='b'){ - if(c2=='l'){ - TOK_SET_BOOL(tok, TOK_LONG_VAL(tok)); - return 0; - }else if(c2=='i'){ - if(strcmp(TOK_IDENT_VAL(tok), "TRUE")==0){ - tok_free(tok); - TOK_SET_BOOL(tok, TRUE); - return 0; - }else if(strcmp(TOK_IDENT_VAL(tok), "FALSE")==0){ - tok_free(tok); - TOK_SET_BOOL(tok, FALSE); - return 0; - } - } - } - - return E_TOKZ_INVALID_ARGUMENT; + char c2=tok->type; + + if(c=='.' || c=='*') + return 0; + + if(c2==c) + return 0; + + if(si){ + if(c2=='i' && c=='s'){ + TOK_SET_IDENT(tok, TOK_STRING_VAL(tok)); + return 0; + } + + if(c2=='s' && c=='i'){ + TOK_SET_STRING(tok, TOK_IDENT_VAL(tok)); + return 0; + } + } + + if(c2=='c' && c=='l'){ + TOK_SET_LONG(tok, TOK_CHAR_VAL(tok)); + return 0; + } + + if(c2=='l' && c=='c'){ + TOK_SET_CHAR(tok, TOK_LONG_VAL(tok)); + return 0; + } + + if(c2=='l' && c=='d'){ + TOK_SET_DOUBLE(tok, TOK_LONG_VAL(tok)); + return 0; + } + + if(c=='b'){ + if(c2=='l'){ + TOK_SET_BOOL(tok, TOK_LONG_VAL(tok)); + return 0; + }else if(c2=='i'){ + if(strcmp(TOK_IDENT_VAL(tok), "TRUE")==0){ + tok_free(tok); + TOK_SET_BOOL(tok, TRUE); + return 0; + }else if(strcmp(TOK_IDENT_VAL(tok), "FALSE")==0){ + tok_free(tok); + TOK_SET_BOOL(tok, FALSE); + return 0; + } + } + } + + return E_TOKZ_INVALID_ARGUMENT; } static int check_argument(const char **pret, Token *tok, const char *p, - bool si) + bool si) { - int mode; - int e=E_TOKZ_TOO_MANY_ARGS; + int mode; + int e=E_TOKZ_TOO_MANY_ARGS; again: - mode=0; - - if(*p=='*'){ - *pret=p; - return 0; - }else if(*p=='?'){ - mode=1; - p++; - }else if(*p==':'){ - mode=2; - p++; - }else if(*p=='+'){ - *pret=p; - return arg_match(tok, *(p-1), si); - } - - while(*p!='\0'){ - e=arg_match(tok, *p, si); - if(e==0){ - p++; - while(mode==2 && *p==':'){ - if(*++p=='\0') - break; /* Invalid argument format string, though... */ - p++; - } - *pret=p; - return 0; - } - - if(mode==0) - break; - - p++; - - if(mode==1) - goto again; - - /* mode==2 */ - - if(*p!=':') - break; - p++; - e=E_TOKZ_TOO_MANY_ARGS; - } - - *pret=p; - return e; + mode=0; + + if(*p=='*'){ + *pret=p; + return 0; + }else if(*p=='?'){ + mode=1; + p++; + }else if(*p==':'){ + mode=2; + p++; + }else if(*p=='+'){ + *pret=p; + return arg_match(tok, *(p-1), si); + } + + while(*p!='\0'){ + e=arg_match(tok, *p, si); + if(e==0){ + p++; + while(mode==2 && *p==':'){ + if(*++p=='\0') + break; /* Invalid argument format string, though... */ + p++; + } + *pret=p; + return 0; + } + + if(mode==0) + break; + + p++; + + if(mode==1) + goto again; + + /* mode==2 */ + + if(*p!=':') + break; + p++; + e=E_TOKZ_TOO_MANY_ARGS; + } + + *pret=p; + return e; } - + static bool args_at_end(const char *p) { - if(p==NULL) - return TRUE; - - while(*p!='\0'){ - if(*p=='*' || *p=='+') - p++; - else if(*p=='?') - p+=2; - else - return FALSE; - } - - return TRUE; + if(p==NULL) + return TRUE; + + while(*p!='\0'){ + if(*p=='*' || *p=='+') + p++; + else if(*p=='?') + p+=2; + else + return FALSE; + } + + return TRUE; } bool do_check_args(const Tokenizer *tokz, Token *tokens, int ntokens, - const char *fmt, bool si) + const char *fmt, bool si) { - int i; - int e; - - if(fmt==NULL){ - if(ntokens!=1) - tokz_warn_error(tokz, tokens[0].line, E_TOKZ_TOO_MANY_ARGS); - return ntokens==1; - } + int i; + int e; + + if(fmt==NULL){ + if(ntokens!=1) + tokz_warn_error(tokz, tokens[0].line, E_TOKZ_TOO_MANY_ARGS); + return ntokens==1; + } - for(i=1; i<ntokens; i++){ - e=check_argument(&fmt, &tokens[i], fmt, si); - if(e!=0){ - tokz_warn_error(tokz, tokens[i].line, e); - return FALSE; - } - } + for(i=1; i<ntokens; i++){ + e=check_argument(&fmt, &tokens[i], fmt, si); + if(e!=0){ + tokz_warn_error(tokz, tokens[i].line, e); + return FALSE; + } + } - if(!args_at_end(fmt)){ - tokz_warn_error(tokz, tokens[i].line, E_TOKZ_TOO_FEW_ARGS); - return FALSE; - } - - return TRUE; + if(!args_at_end(fmt)){ + tokz_warn_error(tokz, tokens[i].line, E_TOKZ_TOO_FEW_ARGS); + return FALSE; + } + + return TRUE; } bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens, - const char *fmt) + const char *fmt) { - return do_check_args(tokz, tokens, ntokens, fmt, FALSE); + return do_check_args(tokz, tokens, ntokens, fmt, FALSE); } bool check_args_loose(const Tokenizer *tokz, Token *tokens, int ntokens, - const char *fmt) + const char *fmt) { - return do_check_args(tokz, tokens, ntokens, fmt, TRUE); + return do_check_args(tokz, tokens, ntokens, fmt, TRUE); } @@ -627,91 +627,91 @@ static bool try_include(Tokenizer *tokz, const char *fname) { - FILE *f; - - f=fopen(fname, "r"); - - if(f==NULL) - return FALSE; - - if(!tokz_pushf_file(tokz, f, fname)){ - fclose(f); - return FALSE; - } - - return TRUE; + FILE *f; + + f=fopen(fname, "r"); + + if(f==NULL) + return FALSE; + + if(!tokz_pushf_file(tokz, f, fname)){ + fclose(f); + return FALSE; + } + + return TRUE; } static bool try_include_dir(Tokenizer *tokz, const char *dir, int dlen, - const char *file) + const char *file) { - char *tmpname; - bool retval; - - tmpname=scatn(dir, dlen, file, -1); - - if(tmpname==NULL){ - warn_err(); - return FALSE; - } - - retval=try_include(tokz, tmpname); + char *tmpname; + bool retval; + + tmpname=scatn(dir, dlen, file, -1); + + if(tmpname==NULL){ + warn_err(); + return FALSE; + } + + retval=try_include(tokz, tmpname); - free(tmpname); + free(tmpname); - return retval; + return retval; } static bool opt_include(Tokenizer *tokz, int n, Token *toks) { - const char *fname=TOK_STRING_VAL(toks+1); - const char *lastndx=NULL; - bool retval, e; - int i=0; - - if(fname[0]!='/' && tokz->name!=NULL) - lastndx=strrchr(tokz->name, '/'); - - if(lastndx==NULL) - retval=try_include(tokz, fname); - else - retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname); - - if(retval==TRUE) - return TRUE; - - e=errno; - - if(tokz->includepaths!=NULL){ - while(tokz->includepaths[i]!=NULL){ - if(try_include_dir(tokz, tokz->includepaths[i], -1, fname)) - return TRUE; - i++; - } - } - - warn_obj(fname, "%s", strerror(e)); - - return FALSE; + const char *fname=TOK_STRING_VAL(toks+1); + const char *lastndx=NULL; + bool retval, e; + int i=0; + + if(fname[0]!='/' && tokz->name!=NULL) + lastndx=strrchr(tokz->name, '/'); + + if(lastndx==NULL) + retval=try_include(tokz, fname); + else + retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname); + + if(retval==TRUE) + return TRUE; + + e=errno; + + if(tokz->includepaths!=NULL){ + while(tokz->includepaths[i]!=NULL){ + if(try_include_dir(tokz, tokz->includepaths[i], -1, fname)) + return TRUE; + i++; + } + } + + warn_obj(fname, "%s", strerror(e)); + + return FALSE; } extern void tokz_set_includepaths(Tokenizer *tokz, char **paths) { - tokz->includepaths=paths; + tokz->includepaths=paths; } ConfOpt libtu_dummy_confopts[]={ - END_CONFOPTS + END_CONFOPTS }; bool parse_config_tokz_skip_section(Tokenizer *tokz) { - return parse_config_tokz(tokz, libtu_dummy_confopts); + return parse_config_tokz(tokz, libtu_dummy_confopts); }
--- a/parser.h Mon Feb 16 18:09:03 2004 +0100 +++ b/parser.h Mon Feb 16 18:50:28 2004 +0100 @@ -14,29 +14,29 @@ /* * format: - * l = long - * d = double - * i = identifier - * s = string - * c = char + * l = long + * d = double + * i = identifier + * s = string + * c = char * . = 1 times any ("l.d") * * = 0 or more times any (must be the last, "sd*") - * ? = optional ("?c") - * : = conditional (":c:s") + * ? = optional ("?c") + * : = conditional (":c:s") * + = 1 or more times last (most be the last, "l+") * special entries: * - * "#end" call this handler at the end of section. - * "#cancel" call this handler when recovering from error + * "#end" call this handler at the end of section. + * "#cancel" call this handler when recovering from error */ #define END_CONFOPTS {NULL, NULL, NULL, NULL} typedef struct _ConfOpt{ - const char *optname; - const char *argfmt; - bool (*fn)(Tokenizer *tokz, int n, Token *toks); - struct _ConfOpt *opts; + const char *optname; + const char *argfmt; + bool (*fn)(Tokenizer *tokz, int n, Token *toks); + struct _ConfOpt *opts; } ConfOpt; #define CONFOPTS_NOT_SET libtu_dummy_confopts @@ -47,8 +47,8 @@ extern bool parse_config(const char *fname, const ConfOpt *options, int flags); extern bool parse_config_file(FILE *file, const ConfOpt *options, int flags); extern bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens, - const char *fmt); + const char *fmt); extern bool check_args_loose(const Tokenizer *tokz, Token *tokens, int ntokens, - const char *fmt); + const char *fmt); #endif /* LIBTU_PARSER_H */
--- a/symlist.c Mon Feb 16 18:09:03 2004 +0100 +++ b/symlist.c Mon Feb 16 18:50:28 2004 +0100 @@ -16,49 +16,49 @@ static void free_node(Symlist **symlist, Symlist *node) { - UNLINK_ITEM(*symlist, node, next, prev); - free(node); + UNLINK_ITEM(*symlist, node, next, prev); + free(node); } bool symlist_insert(Symlist **symlist, void *symbol) { - Symlist *node; - - if(symbol==NULL) - return FALSE; - - node=ALLOC(Symlist); - - if(node==NULL) - return FALSE; - - node->symbol=symbol; - - LINK_ITEM_FIRST(*symlist, node, next, prev); - - return TRUE; + Symlist *node; + + if(symbol==NULL) + return FALSE; + + node=ALLOC(Symlist); + + if(node==NULL) + return FALSE; + + node->symbol=symbol; + + LINK_ITEM_FIRST(*symlist, node, next, prev); + + return TRUE; } void symlist_remove(Symlist **symlist, void *symbol) { - Symlist *node=*symlist; - - while(node!=NULL){ - if(node->symbol==symbol){ - free_node(symlist, node); - return; - } - node=node->next; - } + Symlist *node=*symlist; + + while(node!=NULL){ + if(node->symbol==symbol){ + free_node(symlist, node); + return; + } + node=node->next; + } } void symlist_clear(Symlist **symlist) { - while(*symlist!=NULL) - free_node(symlist, *symlist); + while(*symlist!=NULL) + free_node(symlist, *symlist); } @@ -70,26 +70,26 @@ void *symlist_init_iter(Symlist *symlist) { - if(symlist==NULL){ - iter_next=NULL; - return NULL; - } - - iter_next=symlist->next; - return symlist->symbol; + if(symlist==NULL){ + iter_next=NULL; + return NULL; + } + + iter_next=symlist->next; + return symlist->symbol; } void *symlist_iter() { - Symlist *ret; - - if(iter_next==NULL) - return NULL; - - ret=iter_next; - iter_next=iter_next->next; - - return ret->symbol; + Symlist *ret; + + if(iter_next==NULL) + return NULL; + + ret=iter_next; + iter_next=iter_next->next; + + return ret->symbol; }
--- a/symlist.h Mon Feb 16 18:09:03 2004 +0100 +++ b/symlist.h Mon Feb 16 18:50:28 2004 +0100 @@ -18,15 +18,15 @@ DECLSTRUCT(Symlist){ - void *symbol; - Symlist *next, *prev; + void *symbol; + Symlist *next, *prev; }; #define ITERATE_SYMLIST(TYPE, VAR, LIST) \ - for((VAR)=(TYPE)symlist_init_iter(LIST); \ - (VAR)!=NULL; \ - (VAR)=(TYPE)symlist_iter()) + for((VAR)=(TYPE)symlist_init_iter(LIST); \ + (VAR)!=NULL; \ + (VAR)=(TYPE)symlist_iter()) bool symlist_insert(Symlist **symlist, void *symbol);
--- a/tester.c Mon Feb 16 18:09:03 2004 +0100 +++ b/tester.c Mon Feb 16 18:50:28 2004 +0100 @@ -15,40 +15,40 @@ int main(int argc, char *argv[]) { - Tokenizer*tokz; - Token tok=TOK_INIT; - - libtu_init(argv[0]); - - if(!(tokz=tokz_open_file(stdin, "stdin"))) - return EXIT_FAILURE; + Tokenizer*tokz; + Token tok=TOK_INIT; + + libtu_init(argv[0]); + + if(!(tokz=tokz_open_file(stdin, "stdin"))) + return EXIT_FAILURE; - while(tokz_get_token(tokz, &tok)){ - switch(tok.type){ - case TOK_LONG: - printf("long - %ld\n", TOK_LONG_VAL(&tok)); - break; - case TOK_DOUBLE: - printf("double - %g\n", TOK_DOUBLE_VAL(&tok)); - break; - case TOK_CHAR: - printf("char - '%c'\n", TOK_CHAR_VAL(&tok)); - break; - case TOK_STRING: - printf("string - \"%s\"\n", TOK_STRING_VAL(&tok)); - break; - case TOK_IDENT: - printf("ident - %s\n", TOK_IDENT_VAL(&tok)); - break; - case TOK_COMMENT: - printf("comment - %s\n", TOK_COMMENT_VAL(&tok)); - break; - case TOK_OP: - printf("operator - %03x\n", TOK_OP_VAL(&tok)); - break; - } - } - - return EXIT_SUCCESS; + while(tokz_get_token(tokz, &tok)){ + switch(tok.type){ + case TOK_LONG: + printf("long - %ld\n", TOK_LONG_VAL(&tok)); + break; + case TOK_DOUBLE: + printf("double - %g\n", TOK_DOUBLE_VAL(&tok)); + break; + case TOK_CHAR: + printf("char - '%c'\n", TOK_CHAR_VAL(&tok)); + break; + case TOK_STRING: + printf("string - \"%s\"\n", TOK_STRING_VAL(&tok)); + break; + case TOK_IDENT: + printf("ident - %s\n", TOK_IDENT_VAL(&tok)); + break; + case TOK_COMMENT: + printf("comment - %s\n", TOK_COMMENT_VAL(&tok)); + break; + case TOK_OP: + printf("operator - %03x\n", TOK_OP_VAL(&tok)); + break; + } + } + + return EXIT_SUCCESS; }
--- a/tester2.c Mon Feb 16 18:09:03 2004 +0100 +++ b/tester2.c Mon Feb 16 18:50:28 2004 +0100 @@ -17,53 +17,53 @@ static bool test_fn(Tokenizer *tokz, int n, Token *toks) { - printf("test_fn() %d %s\n", n, TOK_IDENT_VAL(toks)); - - return TRUE; + printf("test_fn() %d %s\n", n, TOK_IDENT_VAL(toks)); + + return TRUE; } static bool sect_fn(Tokenizer *tokz, int n, Token *toks) { - printf("sect_fn() %d %s\n", n, TOK_IDENT_VAL(toks+1)); - - return TRUE; + printf("sect_fn() %d %s\n", n, TOK_IDENT_VAL(toks+1)); + + return TRUE; } static bool test2_fn(Tokenizer *tokz, int n, Token *toks) { - printf("test2_fn() %d %s %f\n", n, TOK_BOOL_VAL(toks+1) ? "TRUE" : "FALSE", TOK_DOUBLE_VAL(toks+2)); + printf("test2_fn() %d %s %f\n", n, TOK_BOOL_VAL(toks+1) ? "TRUE" : "FALSE", TOK_DOUBLE_VAL(toks+2)); - return TRUE; + return TRUE; } static bool test3_fn(Tokenizer *tokz, int n, Token *toks) { - if(n<=2) - printf("test3_fn() %d \"%s\"\n", n, TOK_STRING_VAL(toks+1)); - else - printf("test3_fn() %d \"%s\" %ld\n", n, TOK_STRING_VAL(toks+1), TOK_LONG_VAL(toks+2)); + if(n<=2) + printf("test3_fn() %d \"%s\"\n", n, TOK_STRING_VAL(toks+1)); + else + printf("test3_fn() %d \"%s\" %ld\n", n, TOK_STRING_VAL(toks+1), TOK_LONG_VAL(toks+2)); - return TRUE; + return TRUE; } - - + + static ConfOpt opts[]={ - {"test", NULL, test_fn, NULL}, - {"t2", "bd", test2_fn, NULL}, - {"foo", "s?l", test3_fn, NULL}, - {"sect", "s", sect_fn, opts}, - {NULL, NULL, NULL, NULL} + {"test", NULL, test_fn, NULL}, + {"t2", "bd", test2_fn, NULL}, + {"foo", "s?l", test3_fn, NULL}, + {"sect", "s", sect_fn, opts}, + {NULL, NULL, NULL, NULL} }; - + int main(int argc, char *argv[]) { - libtu_init(argv[0]); - parse_config_file(stdin, opts, TOKZ_ERROR_TOLERANT); - - return EXIT_SUCCESS; + libtu_init(argv[0]); + parse_config_file(stdin, opts, TOKZ_ERROR_TOLERANT); + + return EXIT_SUCCESS; }
--- a/tester3.c Mon Feb 16 18:09:03 2004 +0100 +++ b/tester3.c Mon Feb 16 18:50:28 2004 +0100 @@ -15,58 +15,58 @@ static const char usage[]= - "Usage: $p [options]\n" - "\n" - "Where options are:\n" - "$o\n"; - + "Usage: $p [options]\n" + "\n" + "Where options are:\n" + "$o\n"; + static OptParserOpt opts[]={ - {'o', "opt", OPT_ARG, "OPTION", "foo bar baz quk asdf jklö äölk dfgh quik aaaa bbbb cccc dddd eeee ffff"}, - {'f', "file", OPT_ARG, "FILE", "asdfsadlfölökjasdflökjasdflkjöasdflkjöas dlöfjkasdfölkjasdfölkjasdfasdflöjasdfkasödjlfkasdlföjasdölfjkölkasjdfasdfölkjasd asdöljfasöldf asdölfköasdlf asfdlök asdföljkadsfölasdfölasdölkfjasdölfasödlflöskflasdföaölsdf"}, - {'v', "view", 0, NULL, "asfasdf"}, - {'z', "zip", 0, NULL, "asdfasdf"}, - {'x', "extract", 0, NULL, "asdfasdf"}, - {0, NULL, 0, NULL, NULL} + {'o', "opt", OPT_ARG, "OPTION", "foo bar baz quk asdf jklö äölk dfgh quik aaaa bbbb cccc dddd eeee ffff"}, + {'f', "file", OPT_ARG, "FILE", "asdfsadlfölökjasdflökjasdflkjöasdflkjöas dlöfjkasdfölkjasdfölkjasdfasdflöjasdfkasödjlfkasdlföjasdölfjkölkasjdfasdfölkjasd asdöljfasöldf asdölfköasdlf asfdlök asdföljkadsfölasdfölasdölkfjasdölfasödlflöskflasdföaölsdf"}, + {'v', "view", 0, NULL, "asfasdf"}, + {'z', "zip", 0, NULL, "asdfasdf"}, + {'x', "extract", 0, NULL, "asdfasdf"}, + {0, NULL, 0, NULL, NULL} }; - + static OptParserCommonInfo tester3_cinfo={ - NULL, - usage, - NULL + NULL, + usage, + NULL }; int main(int argc, char *argv[]) { - int opt; - - libtu_init(argv[0]); - - optparser_init(argc, argv, OPTP_NO_DASH, opts, &tester3_cinfo); - - while((opt=optparser_get_opt())){ - switch(opt){ - case 'o': - printf("opt: %s\n", optparser_get_arg()); - break; - case 'f': - printf("file: %s\n", optparser_get_arg()); - break; - case 'v': - printf("view\n"); - break; - case 'z': - printf("zip\n"); - break; - case 'x': - printf("extract\n"); - break; - default: - optparser_print_error(); - return 1; - } - } - return 0; + int opt; + + libtu_init(argv[0]); + + optparser_init(argc, argv, OPTP_NO_DASH, opts, &tester3_cinfo); + + while((opt=optparser_get_opt())){ + switch(opt){ + case 'o': + printf("opt: %s\n", optparser_get_arg()); + break; + case 'f': + printf("file: %s\n", optparser_get_arg()); + break; + case 'v': + printf("view\n"); + break; + case 'z': + printf("zip\n"); + break; + case 'x': + printf("extract\n"); + break; + default: + optparser_print_error(); + return 1; + } + } + return 0; }
--- a/tokenizer.c Mon Feb 16 18:09:03 2004 +0100 +++ b/tokenizer.c Mon Feb 16 18:50:28 2004 +0100 @@ -21,27 +21,27 @@ static const char *errors[]={ - DUMMY_TR("(no error)"), - DUMMY_TR("Unexpected end of file"), /* E_TOKZ_UNEXPECTED_EOF */ - DUMMY_TR("Unexpected end of line"), /* E_TOKZ_UNEXPECTED_EOL */ - DUMMY_TR("End of line expected"), /* E_TOKZ_EOL_EXPECTED */ - DUMMY_TR("Invalid character"), /* E_TOKZ_INVALID_CHAR*/ - DUMMY_TR("Numeric constant too big"), /* E_TOKZ_TOOBIG */ - DUMMY_TR("Invalid numberic format"), /* E_TOKZ_NUMFMT */ - DUMMY_TR("Junk after numeric constant"), /* E_TOKZ_NUM_JUNK */ - DUMMY_TR("Not an integer"), /* E_TOKZ_NOTINT */ - DUMMY_TR("Numeric constant out of range"), /* E_TOKZ_RANGE */ - DUMMY_TR("Multi-character character constant"), /* E_TOKZ_MULTICHAR */ - DUMMY_TR("Token/statement limit reached"), /* E_TOKZ_TOKEN_LIMIT */ - DUMMY_TR("Unknown option"), /* E_TOKZ_UNKONWN_OPTION */ - DUMMY_TR("Syntax error"), /* E_TOKZ_SYNTAX */ - DUMMY_TR("Invalid argument"), /* E_TOKZ_INVALID_ARGUMENT */ - DUMMY_TR("End of statement expected"), /* E_TOKZ_EOS_EXPECTED */ - DUMMY_TR("Too few arguments"), /* E_TOKZ_TOO_FEW_ARGS */ - DUMMY_TR("Too many arguments"), /* E_TOKZ_TOO_MANY_ARGS */ - DUMMY_TR("Maximum section nestin level exceeded"), /* E_TOK_Z_MAX_NEST */ - DUMMY_TR("Identifier expected"), /* E_TOKZ_IDENTIFIER_EXPECTED */ - DUMMY_TR("Starting brace ('{') expected"), /* E_TOKZ_LBRACE_EXPECTED */ + DUMMY_TR("(no error)"), + DUMMY_TR("Unexpected end of file"), /* E_TOKZ_UNEXPECTED_EOF */ + DUMMY_TR("Unexpected end of line"), /* E_TOKZ_UNEXPECTED_EOL */ + DUMMY_TR("End of line expected"), /* E_TOKZ_EOL_EXPECTED */ + DUMMY_TR("Invalid character"), /* E_TOKZ_INVALID_CHAR*/ + DUMMY_TR("Numeric constant too big"), /* E_TOKZ_TOOBIG */ + DUMMY_TR("Invalid numberic format"), /* E_TOKZ_NUMFMT */ + DUMMY_TR("Junk after numeric constant"), /* E_TOKZ_NUM_JUNK */ + DUMMY_TR("Not an integer"), /* E_TOKZ_NOTINT */ + DUMMY_TR("Numeric constant out of range"), /* E_TOKZ_RANGE */ + DUMMY_TR("Multi-character character constant"), /* E_TOKZ_MULTICHAR */ + DUMMY_TR("Token/statement limit reached"), /* E_TOKZ_TOKEN_LIMIT */ + DUMMY_TR("Unknown option"), /* E_TOKZ_UNKONWN_OPTION */ + DUMMY_TR("Syntax error"), /* E_TOKZ_SYNTAX */ + DUMMY_TR("Invalid argument"), /* E_TOKZ_INVALID_ARGUMENT */ + DUMMY_TR("End of statement expected"), /* E_TOKZ_EOS_EXPECTED */ + DUMMY_TR("Too few arguments"), /* E_TOKZ_TOO_FEW_ARGS */ + DUMMY_TR("Too many arguments"), /* E_TOKZ_TOO_MANY_ARGS */ + DUMMY_TR("Maximum section nestin level exceeded"), /* E_TOK_Z_MAX_NEST */ + DUMMY_TR("Identifier expected"), /* E_TOKZ_IDENTIFIER_EXPECTED */ + DUMMY_TR("Starting brace ('{') expected"), /* E_TOKZ_LBRACE_EXPECTED */ }; @@ -58,45 +58,45 @@ static bool _string_append(char **p, char *tmp, int *tmplen, char c) { - char *tmp2; - - if(*tmplen==STRBLEN-1){ - tmp[STRBLEN-1]='\0'; - if(*p!=NULL){ - tmp2=scat(*p, tmp); - free(*p); - *p=tmp2; - }else{ - *p=scopy(tmp); - } - *tmplen=1; - tmp[0]=c; - return *p!=NULL; - }else{ - tmp[(*tmplen)++]=c; - return TRUE; - } + char *tmp2; + + if(*tmplen==STRBLEN-1){ + tmp[STRBLEN-1]='\0'; + if(*p!=NULL){ + tmp2=scat(*p, tmp); + free(*p); + *p=tmp2; + }else{ + *p=scopy(tmp); + } + *tmplen=1; + tmp[0]=c; + return *p!=NULL; + }else{ + tmp[(*tmplen)++]=c; + return TRUE; + } } static bool _string_finish(char **p, char *tmp, int tmplen) { - char *tmp2; - - if(tmplen==0){ - if(*p==NULL) - *p=scopy(""); - }else{ - tmp[tmplen]='\0'; - if(*p!=NULL){ - tmp2=scat(*p, tmp); - free(*p); - *p=tmp2; - }else{ - *p=scopy(tmp); - } - } - return *p!=NULL; + char *tmp2; + + if(tmplen==0){ + if(*p==NULL) + *p=scopy(""); + }else{ + tmp[tmplen]='\0'; + if(*p!=NULL){ + tmp2=scat(*p, tmp); + free(*p); + *p=tmp2; + }else{ + *p=scopy(tmp); + } + } + return *p!=NULL; } @@ -109,28 +109,28 @@ static int _getch(Tokenizer *tokz) { - int c; - - if(tokz->ungetc!=-1){ - c=tokz->ungetc; - tokz->ungetc=-1; - }else if (tokz->flags&TOKZ_READ_FROM_BUFFER) { - assert(tokz->buffer.data!=NULL); - if (tokz->buffer.pos==tokz->buffer.len) - c=EOF; - else - c=tokz->buffer.data[tokz->buffer.pos++]; - }else{ - c=getc(tokz->file); - } + int c; + + if(tokz->ungetc!=-1){ + c=tokz->ungetc; + tokz->ungetc=-1; + }else if (tokz->flags&TOKZ_READ_FROM_BUFFER) { + assert(tokz->buffer.data!=NULL); + if (tokz->buffer.pos==tokz->buffer.len) + c=EOF; + else + c=tokz->buffer.data[tokz->buffer.pos++]; + }else{ + c=getc(tokz->file); + } - return c; + return c; } static void _ungetch(Tokenizer *tokz, int c) { - tokz->ungetc=c; + tokz->ungetc=c; } @@ -139,37 +139,37 @@ static int scan_line_comment(Token *tok, Tokenizer *tokz) { - STRING_DECL_P(s, "#"); - int c; + STRING_DECL_P(s, "#"); + int c; - c=GETCH(); - - while(c!='\n' && c!=EOF){ - STRING_APPEND(s, c); - c=GETCH(); - } + c=GETCH(); + + while(c!='\n' && c!=EOF){ + STRING_APPEND(s, c); + c=GETCH(); + } - UNGETCH(c); + UNGETCH(c); - STRING_FINISH(s); - - TOK_SET_COMMENT(tok, s); - - return 0; + STRING_FINISH(s); + + TOK_SET_COMMENT(tok, s); + + return 0; } static int skip_line_comment(Tokenizer *tokz) { - int c; - - do{ - c=GETCH(); - }while(c!='\n' && c!=EOF); + int c; + + do{ + c=GETCH(); + }while(c!='\n' && c!=EOF); - UNGETCH(c); - - return 0; + UNGETCH(c); + + return 0; } @@ -178,62 +178,62 @@ static int scan_c_comment(Token *tok, Tokenizer *tokz) { - STRING_DECL_P(s, "/*"); - int c; - int st=0; - - while(1){ - c=GETCH(); - - if(c==EOF){ - STRING_FREE(s); - return E_TOKZ_UNEXPECTED_EOF; - } - - STRING_APPEND(s, c); - - if(c=='\n'){ - INC_LINE(); - }else if(st==0 && c=='*'){ - st=1; - }else if(st==1){ - if(c=='/') - break; - st=0; - } - } + STRING_DECL_P(s, "/*"); + int c; + int st=0; + + while(1){ + c=GETCH(); + + if(c==EOF){ + STRING_FREE(s); + return E_TOKZ_UNEXPECTED_EOF; + } + + STRING_APPEND(s, c); + + if(c=='\n'){ + INC_LINE(); + }else if(st==0 && c=='*'){ + st=1; + }else if(st==1){ + if(c=='/') + break; + st=0; + } + } - STRING_FINISH(s); + STRING_FINISH(s); - TOK_SET_COMMENT(tok, s); + TOK_SET_COMMENT(tok, s); - return 0; + return 0; } static int skip_c_comment(Tokenizer *tokz) { - int c; - int st=0; - - while(1){ - c=GETCH(); - - if(c==EOF) - return E_TOKZ_UNEXPECTED_EOF; - - if(c=='\n') - INC_LINE(); - else if(st==0 && c=='*') - st=1; - else if(st==1){ - if(c=='/') - break; - st=0; - } - } - - return 0; + int c; + int st=0; + + while(1){ + c=GETCH(); + + if(c==EOF) + return E_TOKZ_UNEXPECTED_EOF; + + if(c=='\n') + INC_LINE(); + else if(st==0 && c=='*') + st=1; + else if(st==1){ + if(c=='/') + break; + st=0; + } + } + + return 0; } @@ -242,75 +242,75 @@ static int scan_char_escape(Tokenizer *tokz) { - static char* special_chars="nrtbae"; - static char* specials="\n\r\t\b\a\033"; - int base, max; - int i ,c; - - c=GETCH(); - - for(i=0;special_chars[i];i++){ - if(special_chars[i]==c) - return specials[c]; - } - - if(c=='x' || c=='X'){ - base=16;max=2;i=0; - }else if(c=='d' || c=='D'){ - base=10;max=3;i=0; - }else if(c=='8' || c=='9'){ - base=10;max=2;i=c-'0'; - }else if('0'<=c && c<='7'){ - base=8;max=2;i=c-'0'; - }else if(c=='\n'){ - UNGETCH(c); - return -2; - }else{ - return c; - } - - - while(--max>=0){ - c=GETCH(); - - if(c==EOF) - return EOF; - - if(c=='\n'){ - UNGETCH(c); - return -2; - } - - if(base==16){ - if(!isxdigit(c)) - break; - - i<<=4; - - if(isdigit(c)) - i+=c-'0'; - else if(i>='a') - i+=0xa+c-'a'; - else - i+=0xa+c-'a'; - - }else if(base==10){ - if(!isdigit(c)) - break; - i*=10; - i+=c-'0'; - }else{ - if(c<'0' || c>'7') - break; - i<<=3; - i+=c-'0'; - } - } - - if(max>=0) - UNGETCH(c); + static char* special_chars="nrtbae"; + static char* specials="\n\r\t\b\a\033"; + int base, max; + int i ,c; + + c=GETCH(); + + for(i=0;special_chars[i];i++){ + if(special_chars[i]==c) + return specials[c]; + } + + if(c=='x' || c=='X'){ + base=16;max=2;i=0; + }else if(c=='d' || c=='D'){ + base=10;max=3;i=0; + }else if(c=='8' || c=='9'){ + base=10;max=2;i=c-'0'; + }else if('0'<=c && c<='7'){ + base=8;max=2;i=c-'0'; + }else if(c=='\n'){ + UNGETCH(c); + return -2; + }else{ + return c; + } + + + while(--max>=0){ + c=GETCH(); + + if(c==EOF) + return EOF; + + if(c=='\n'){ + UNGETCH(c); + return -2; + } + + if(base==16){ + if(!isxdigit(c)) + break; + + i<<=4; + + if(isdigit(c)) + i+=c-'0'; + else if(i>='a') + i+=0xa+c-'a'; + else + i+=0xa+c-'a'; + + }else if(base==10){ + if(!isdigit(c)) + break; + i*=10; + i+=c-'0'; + }else{ + if(c<'0' || c>'7') + break; + i<<=3; + i+=c-'0'; + } + } + + if(max>=0) + UNGETCH(c); - return i; + return i; } @@ -319,42 +319,42 @@ static int scan_string(Token *tok, Tokenizer *tokz, bool escapes) { - STRING_DECL(s); - int c; + STRING_DECL(s); + int c; - while(1){ - c=GETCH(); - - if(c=='"') - break; - - if(c=='\n'){ - UNGETCH(c); - STRING_FREE(s); - return E_TOKZ_UNEXPECTED_EOL; - } - - if(c=='\\' && escapes){ - c=scan_char_escape(tokz); - if(c==-2){ - STRING_FREE(s); - return E_TOKZ_UNEXPECTED_EOL; - } - } - - if(c==EOF){ - STRING_FREE(s); - return E_TOKZ_UNEXPECTED_EOF; - } - - STRING_APPEND(s, c); - } - - STRING_FINISH(s); - - TOK_SET_STRING(tok, s); + while(1){ + c=GETCH(); + + if(c=='"') + break; + + if(c=='\n'){ + UNGETCH(c); + STRING_FREE(s); + return E_TOKZ_UNEXPECTED_EOL; + } + + if(c=='\\' && escapes){ + c=scan_char_escape(tokz); + if(c==-2){ + STRING_FREE(s); + return E_TOKZ_UNEXPECTED_EOL; + } + } + + if(c==EOF){ + STRING_FREE(s); + return E_TOKZ_UNEXPECTED_EOF; + } + + STRING_APPEND(s, c); + } + + STRING_FINISH(s); + + TOK_SET_STRING(tok, s); - return 0; + return 0; } @@ -363,34 +363,34 @@ static int scan_char(Token *tok, Tokenizer *tokz) { - int c, c2; - - c=GETCH(); - - if(c==EOF) - return E_TOKZ_UNEXPECTED_EOF; - - if(c=='\n') - return E_TOKZ_UNEXPECTED_EOL; + int c, c2; + + c=GETCH(); + + if(c==EOF) + return E_TOKZ_UNEXPECTED_EOF; + + if(c=='\n') + return E_TOKZ_UNEXPECTED_EOL; - if(c=='\\'){ - c=scan_char_escape(tokz); - - if(c==EOF) - return E_TOKZ_UNEXPECTED_EOF; - - if(c==-2) - return E_TOKZ_UNEXPECTED_EOL; - } - - c2=GETCH(); - - if(c2!='\'') - return E_TOKZ_MULTICHAR; - - TOK_SET_CHAR(tok, c); - - return 0; + if(c=='\\'){ + c=scan_char_escape(tokz); + + if(c==EOF) + return E_TOKZ_UNEXPECTED_EOF; + + if(c==-2) + return E_TOKZ_UNEXPECTED_EOL; + } + + c2=GETCH(); + + if(c2!='\'') + return E_TOKZ_MULTICHAR; + + TOK_SET_CHAR(tok, c); + + return 0; } @@ -402,20 +402,20 @@ static int scan_identifier(Token *tok, Tokenizer *tokz, int c) { - STRING_DECL(s); - - do{ - STRING_APPEND(s, c); - c=GETCH(); - }while(isalnum(c) || c=='_' || c=='$'); - - UNGETCH(c); - - STRING_FINISH(s); - - TOK_SET_IDENT(tok, s); + STRING_DECL(s); + + do{ + STRING_APPEND(s, c); + c=GETCH(); + }while(isalnum(c) || c=='_' || c=='$'); + + UNGETCH(c); + + STRING_FINISH(s); + + TOK_SET_IDENT(tok, s); - return 0; + return 0; } #define NP_SIMPLE_IMPL @@ -425,29 +425,29 @@ static int scan_number(Token *tok, Tokenizer *tokz, int c) { - NPNum num=NUM_INIT; - int e; - - if((e=parse_number(&num, tokz, c))) - return e; - - if(num.type==NPNUM_INT){ - long l; - if((e=num_to_long(&l, &num, TRUE))) - return e; - - TOK_SET_LONG(tok, l); - }else if(num.type==NPNUM_FLOAT){ - double d; - if((e=num_to_double(&d, &num))) - return e; - - TOK_SET_DOUBLE(tok, d); - }else{ - return E_TOKZ_NUMFMT; - } + NPNum num=NUM_INIT; + int e; + + if((e=parse_number(&num, tokz, c))) + return e; + + if(num.type==NPNUM_INT){ + long l; + if((e=num_to_long(&l, &num, TRUE))) + return e; + + TOK_SET_LONG(tok, l); + }else if(num.type==NPNUM_FLOAT){ + double d; + if((e=num_to_double(&d, &num))) + return e; + + TOK_SET_DOUBLE(tok, d); + }else{ + return E_TOKZ_NUMFMT; + } - return 0; + return 0; } @@ -455,100 +455,100 @@ static uchar op_map[]={ - 0x00, /* ________ 0-7 */ - 0x00, /* ________ 8-15 */ - 0x00, /* ________ 16-23 */ - 0x00, /* ________ 24-31 */ - 0x62, /* _!___%&_ 32-39 */ - 0xff, /* ()*+,-./ 40-47 */ - 0x00, /* ________ 48-55 */ - 0xfc, /* __:;<=>? 56-63 */ - 0x01, /* @_______ 64-71 */ - 0x00, /* ________ 72-79 */ - 0x00, /* ________ 80-87 */ - 0x78, /* ___[_]^_ 88-95 */ - 0x00, /* ________ 96-103 */ - 0x00, /* ________ 104-111 */ - 0x00, /* ________ 112-119 */ - 0x38 /* ___{|}__ 120-127 */ + 0x00, /* ________ 0-7 */ + 0x00, /* ________ 8-15 */ + 0x00, /* ________ 16-23 */ + 0x00, /* ________ 24-31 */ + 0x62, /* _!___%&_ 32-39 */ + 0xff, /* ()*+,-./ 40-47 */ + 0x00, /* ________ 48-55 */ + 0xfc, /* __:;<=>? 56-63 */ + 0x01, /* @_______ 64-71 */ + 0x00, /* ________ 72-79 */ + 0x00, /* ________ 80-87 */ + 0x78, /* ___[_]^_ 88-95 */ + 0x00, /* ________ 96-103 */ + 0x00, /* ________ 104-111 */ + 0x00, /* ________ 112-119 */ + 0x38 /* ___{|}__ 120-127 */ }; static bool map_isset(uchar *map, uint ch) { - if(ch>127) - return FALSE; + if(ch>127) + return FALSE; - return map[ch>>3]&(1<<(ch&7)); + return map[ch>>3]&(1<<(ch&7)); } static bool is_opch(uint ch) { - return map_isset(op_map, ch); + return map_isset(op_map, ch); } static int scan_op(Token *tok, Tokenizer *tokz, int c) { - int c2; - int op=-1; - - /* Quickly check it is an operator character */ - if(!is_opch(c)) - return E_TOKZ_INVALID_CHAR; + int c2; + int op=-1; + + /* Quickly check it is an operator character */ + if(!is_opch(c)) + return E_TOKZ_INVALID_CHAR; - switch(c){ - case '+': - case '-': - case '*': -/* case '/': Checked elsewhere */ - case '%': - case '^': - case '!': - case '=': - case '<': - case '>': - c2=GETCH(); - if(c2=='='){ - op=c|(c2<<8); - }else if(c2==c && (c2!='%' && c2!='!' && c2!='*')){ - if(c=='<' || c=='>'){ - int c3=GETCH(); - if(c3=='='){ - op=c|(c2<<8)|(c3<<16); - }else{ - UNGETCH(c3); - op=c|(c2<<8); - } - }else{ - op=c|(c2<<8); - } - }else{ - UNGETCH(c2); - op=c; - } - break; - - /* It is already known that it is a operator so these are not needed - case ':': - case '~': - case '?': - case '.': - case ';'; - case '{': - case '}': - case '@': - case '|': - case '&': - */ - default: - op=c; - } - - TOK_SET_OP(tok, op); + switch(c){ + case '+': + case '-': + case '*': +/* case '/': Checked elsewhere */ + case '%': + case '^': + case '!': + case '=': + case '<': + case '>': + c2=GETCH(); + if(c2=='='){ + op=c|(c2<<8); + }else if(c2==c && (c2!='%' && c2!='!' && c2!='*')){ + if(c=='<' || c=='>'){ + int c3=GETCH(); + if(c3=='='){ + op=c|(c2<<8)|(c3<<16); + }else{ + UNGETCH(c3); + op=c|(c2<<8); + } + }else{ + op=c|(c2<<8); + } + }else{ + UNGETCH(c2); + op=c; + } + break; + + /* It is already known that it is a operator so these are not needed + case ':': + case '~': + case '?': + case '.': + case ';'; + case '{': + case '}': + case '@': + case '|': + case '&': + */ + default: + op=c; + } + + TOK_SET_OP(tok, op); - return 0; + return 0; } @@ -557,156 +557,156 @@ void tokz_warn(const Tokenizer *tokz, int line, const char *fmt, ...) { - va_list args; - - va_start(args, fmt); - - if(tokz!=NULL) - warn_obj_line_v(tokz->name, line, fmt, args); - else - warn(fmt, args); - - va_end(args); + va_list args; + + va_start(args, fmt); + + if(tokz!=NULL) + warn_obj_line_v(tokz->name, line, fmt, args); + else + warn(fmt, args); + + va_end(args); } void tokz_warn_error(const Tokenizer *tokz, int line, int e) { - if(e==E_TOKZ_UNEXPECTED_EOF) - line=0; - - if(e<0) - tokz_warn(tokz, line, "%s", strerror(-e)); - else - tokz_warn(tokz, line, "%s", TR(errors[e])); + if(e==E_TOKZ_UNEXPECTED_EOF) + line=0; + + if(e<0) + tokz_warn(tokz, line, "%s", strerror(-e)); + else + tokz_warn(tokz, line, "%s", TR(errors[e])); } bool tokz_get_token(Tokenizer *tokz, Token *tok) { - int c, c2, e; - - if (!(tokz->flags&TOKZ_READ_FROM_BUFFER)) - assert(tokz->file!=NULL); - - tok_free(tok); - - if(!TOK_IS_INVALID(&(tokz->ungettok))){ - *tok=tokz->ungettok; - tokz->ungettok.type=TOK_INVALID; - return TRUE; - } + int c, c2, e; + + if (!(tokz->flags&TOKZ_READ_FROM_BUFFER)) + assert(tokz->file!=NULL); + + tok_free(tok); + + if(!TOK_IS_INVALID(&(tokz->ungettok))){ + *tok=tokz->ungettok; + tokz->ungettok.type=TOK_INVALID; + return TRUE; + } - while(1){ - - e=0; - - do{ - c=GETCH(); - }while(c!='\n' && c!=EOF && isspace(c)); - - tok->line=tokz->line; - - switch(c){ - case EOF: - TOK_SET_OP(tok, OP_EOF); - return TRUE; - - case '\n': - INC_LINE(); - - if(tokz->flags&TOKZ_IGNORE_NEXTLINE) - continue; - - TOK_SET_OP(tok, OP_NEXTLINE); - - return TRUE; - - case '\\': - do{ - c=GETCH(); - if(c==EOF){ - TOK_SET_OP(tok, OP_EOF); - return FALSE; - } - if(!isspace(c) && e==0){ - e=E_TOKZ_EOL_EXPECTED; - tokz_warn_error(tokz, tokz->line, e); - if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) - return FALSE; - } - }while(c!='\n'); - - INC_LINE(); - continue; + while(1){ + + e=0; + + do{ + c=GETCH(); + }while(c!='\n' && c!=EOF && isspace(c)); + + tok->line=tokz->line; + + switch(c){ + case EOF: + TOK_SET_OP(tok, OP_EOF); + return TRUE; + + case '\n': + INC_LINE(); + + if(tokz->flags&TOKZ_IGNORE_NEXTLINE) + continue; + + TOK_SET_OP(tok, OP_NEXTLINE); + + return TRUE; + + case '\\': + do{ + c=GETCH(); + if(c==EOF){ + TOK_SET_OP(tok, OP_EOF); + return FALSE; + } + if(!isspace(c) && e==0){ + e=E_TOKZ_EOL_EXPECTED; + tokz_warn_error(tokz, tokz->line, e); + if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) + return FALSE; + } + }while(c!='\n'); + + INC_LINE(); + continue; - case '#': - if(tokz->flags&TOKZ_READ_COMMENTS){ - e=scan_line_comment(tok, tokz); - break; - }else if((e=skip_line_comment(tokz))){ - break; - } - - continue; - - case '/': - c2=GETCH(); - - if(c2=='='){ - TOK_SET_OP(tok, OP_AS_DIV); - return TRUE; - } - - if(c2!='*'){ - UNGETCH(c2); - TOK_SET_OP(tok, OP_DIV); - return TRUE; - } - - if(tokz->flags&TOKZ_READ_COMMENTS){ - e=scan_c_comment(tok, tokz); - break; - }else if((e=skip_c_comment(tokz))){ - break; - } - - continue; - - case '\"': - e=scan_string(tok, tokz, TRUE); - break; + case '#': + if(tokz->flags&TOKZ_READ_COMMENTS){ + e=scan_line_comment(tok, tokz); + break; + }else if((e=skip_line_comment(tokz))){ + break; + } + + continue; + + case '/': + c2=GETCH(); + + if(c2=='='){ + TOK_SET_OP(tok, OP_AS_DIV); + return TRUE; + } + + if(c2!='*'){ + UNGETCH(c2); + TOK_SET_OP(tok, OP_DIV); + return TRUE; + } + + if(tokz->flags&TOKZ_READ_COMMENTS){ + e=scan_c_comment(tok, tokz); + break; + }else if((e=skip_c_comment(tokz))){ + break; + } + + continue; + + case '\"': + e=scan_string(tok, tokz, TRUE); + break; - case '\'': - e=scan_char(tok, tokz); - break; + case '\'': + e=scan_char(tok, tokz); + break; - default: - if(('0'<=c && c<='9') || c=='-' || c=='+'){ - e=scan_number(tok, tokz, c); - break; - } + default: + if(('0'<=c && c<='9') || c=='-' || c=='+'){ + e=scan_number(tok, tokz, c); + break; + } - if(START_IDENT(c)) - e=scan_identifier(tok, tokz, c); - else - e=scan_op(tok, tokz, c); - } - - if(!e) - return TRUE; - - tokz_warn_error(tokz, tokz->line, e); - return FALSE; - } + if(START_IDENT(c)) + e=scan_identifier(tok, tokz, c); + else + e=scan_op(tok, tokz, c); + } + + if(!e) + return TRUE; + + tokz_warn_error(tokz, tokz->line, e); + return FALSE; + } } void tokz_unget_token(Tokenizer *tokz, Token *tok) { - tok_free(&(tokz->ungettok)); - tokz->ungettok=*tok; - tok->type=TOK_INVALID; + tok_free(&(tokz->ungettok)); + tokz->ungettok=*tok; + tok->type=TOK_INVALID; } @@ -716,160 +716,160 @@ static bool do_tokz_pushf(Tokenizer *tokz) { - Tokenizer_FInfo *finfo; - - finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, - tokz->filestack_n, tokz->filestack_n+1); - - if(finfo==NULL) - return FALSE; + Tokenizer_FInfo *finfo; + + finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, + tokz->filestack_n, tokz->filestack_n+1); + + if(finfo==NULL) + return FALSE; - tokz->filestack=finfo; - finfo=&(finfo[tokz->filestack_n++]); - - finfo->file=tokz->file; - finfo->name=tokz->name; - finfo->line=tokz->line; - finfo->ungetc=tokz->ungetc; - finfo->ungettok=tokz->ungettok; - - return TRUE; + tokz->filestack=finfo; + finfo=&(finfo[tokz->filestack_n++]); + + finfo->file=tokz->file; + finfo->name=tokz->name; + finfo->line=tokz->line; + finfo->ungetc=tokz->ungetc; + finfo->ungettok=tokz->ungettok; + + return TRUE; } bool tokz_pushf_file(Tokenizer *tokz, FILE *file, const char *fname) { - char *fname_copy=NULL; - - if(file==NULL) - return FALSE; + char *fname_copy=NULL; + + if(file==NULL) + return FALSE; - if(fname!=NULL){ - fname_copy=scopy(fname); - if(fname_copy==NULL){ - warn_err(); - return FALSE; - } - } - - if(tokz->file!=NULL){ - if(!do_tokz_pushf(tokz)){ - warn_err(); - if(fname_copy!=NULL) - free(fname_copy); - return FALSE; - } - } - - tokz->file=file; - tokz->name=fname_copy; - tokz->line=1; - tokz->ungetc=-1; - tokz->ungettok.type=TOK_INVALID; - - return TRUE; + if(fname!=NULL){ + fname_copy=scopy(fname); + if(fname_copy==NULL){ + warn_err(); + return FALSE; + } + } + + if(tokz->file!=NULL){ + if(!do_tokz_pushf(tokz)){ + warn_err(); + if(fname_copy!=NULL) + free(fname_copy); + return FALSE; + } + } + + tokz->file=file; + tokz->name=fname_copy; + tokz->line=1; + tokz->ungetc=-1; + tokz->ungettok.type=TOK_INVALID; + + return TRUE; } bool tokz_pushf(Tokenizer *tokz, const char *fname) { - FILE *file; - - file=fopen(fname, "r"); - - if(file==NULL){ - warn_err_obj(fname); - return FALSE; - } - - if(!tokz_pushf_file(tokz, file, fname)){ - fclose(file); - return FALSE; - } + FILE *file; + + file=fopen(fname, "r"); + + if(file==NULL){ + warn_err_obj(fname); + return FALSE; + } + + if(!tokz_pushf_file(tokz, file, fname)){ + fclose(file); + return FALSE; + } - return TRUE; + return TRUE; } - + static Tokenizer *tokz_create() { - Tokenizer*tokz; - - tokz=ALLOC(Tokenizer); - - if(tokz==NULL){ - warn_err(); - return NULL; - } - - tokz->file=NULL; - tokz->name=NULL; - tokz->line=1; - tokz->ungetc=-1; - tokz->ungettok.type=TOK_INVALID; - tokz->flags=0; - tokz->optstack=NULL; - tokz->nest_lvl=0; - tokz->filestack_n=0; - tokz->filestack=NULL; - tokz->buffer.data=0; - tokz->buffer.len=0; - tokz->buffer.pos=0; - - return tokz; + Tokenizer*tokz; + + tokz=ALLOC(Tokenizer); + + if(tokz==NULL){ + warn_err(); + return NULL; + } + + tokz->file=NULL; + tokz->name=NULL; + tokz->line=1; + tokz->ungetc=-1; + tokz->ungettok.type=TOK_INVALID; + tokz->flags=0; + tokz->optstack=NULL; + tokz->nest_lvl=0; + tokz->filestack_n=0; + tokz->filestack=NULL; + tokz->buffer.data=0; + tokz->buffer.len=0; + tokz->buffer.pos=0; + + return tokz; } - + Tokenizer *tokz_open(const char *fname) { - Tokenizer *tokz; - - tokz=tokz_create(); - - if(!tokz_pushf(tokz, fname)){ - free(tokz); - return NULL; - } - - return tokz; + Tokenizer *tokz; + + tokz=tokz_create(); + + if(!tokz_pushf(tokz, fname)){ + free(tokz); + return NULL; + } + + return tokz; } Tokenizer *tokz_open_file(FILE *file, const char *fname) { - Tokenizer *tokz; - - tokz=tokz_create(); - - if(!tokz_pushf_file(tokz, file, fname)){ - free(tokz); - return NULL; - } - - return tokz; + Tokenizer *tokz; + + tokz=tokz_create(); + + if(!tokz_pushf_file(tokz, file, fname)){ + free(tokz); + return NULL; + } + + return tokz; } Tokenizer *tokz_prepare_buffer(char *buffer, int len) { - Tokenizer *tokz; - char old=0; + Tokenizer *tokz; + char old=0; - tokz=tokz_create(); - if(len>0){ - old=buffer[len-1]; - buffer[len-1]='\0'; - } + tokz=tokz_create(); + if(len>0){ + old=buffer[len-1]; + buffer[len-1]='\0'; + } - tokz->flags|=TOKZ_READ_FROM_BUFFER; - tokz->buffer.data=scopy(buffer); - tokz->buffer.len=(len>0 ? (uint)len : strlen(tokz->buffer.data)); - tokz->buffer.pos=0; + tokz->flags|=TOKZ_READ_FROM_BUFFER; + tokz->buffer.data=scopy(buffer); + tokz->buffer.len=(len>0 ? (uint)len : strlen(tokz->buffer.data)); + tokz->buffer.pos=0; - if(old>0) - buffer[len-1]=old; + if(old>0) + buffer[len-1]=old; - return tokz; + return tokz; } /* @@ -878,58 +878,58 @@ static bool do_tokz_popf(Tokenizer *tokz, bool shrink) { - Tokenizer_FInfo *finfo; - - if(tokz->filestack_n<=0) - return FALSE; + Tokenizer_FInfo *finfo; + + if(tokz->filestack_n<=0) + return FALSE; - if(tokz->file!=NULL) - fclose(tokz->file); - if(tokz->name!=NULL) - free(tokz->name); - - finfo=&(tokz->filestack[--tokz->filestack_n]); - - tokz->file=finfo->file; - tokz->name=finfo->name; - tokz->line=finfo->line; - tokz->ungetc=finfo->ungetc; - tokz->ungettok=finfo->ungettok; - - if(tokz->filestack_n==0){ - free(tokz->filestack); - tokz->filestack=NULL; - }else if(shrink){ - finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, - tokz->filestack_n+1, tokz->filestack_n); - if(finfo==NULL) - warn_err(); - else - tokz->filestack=finfo; - } - - return TRUE; + if(tokz->file!=NULL) + fclose(tokz->file); + if(tokz->name!=NULL) + free(tokz->name); + + finfo=&(tokz->filestack[--tokz->filestack_n]); + + tokz->file=finfo->file; + tokz->name=finfo->name; + tokz->line=finfo->line; + tokz->ungetc=finfo->ungetc; + tokz->ungettok=finfo->ungettok; + + if(tokz->filestack_n==0){ + free(tokz->filestack); + tokz->filestack=NULL; + }else if(shrink){ + finfo=REALLOC_N(tokz->filestack, Tokenizer_FInfo, + tokz->filestack_n+1, tokz->filestack_n); + if(finfo==NULL) + warn_err(); + else + tokz->filestack=finfo; + } + + return TRUE; } bool tokz_popf(Tokenizer *tokz) { - return do_tokz_popf(tokz, TRUE); + return do_tokz_popf(tokz, TRUE); } - + void tokz_close(Tokenizer *tokz) { - while(tokz->filestack_n>0) - do_tokz_popf(tokz, FALSE); - - if(tokz->file!=NULL) - fclose(tokz->file); - if(tokz->name!=NULL) - free(tokz->name); - tok_free(&(tokz->ungettok)); - - free(tokz); + while(tokz->filestack_n>0) + do_tokz_popf(tokz, FALSE); + + if(tokz->file!=NULL) + fclose(tokz->file); + if(tokz->name!=NULL) + free(tokz->name); + tok_free(&(tokz->ungettok)); + + free(tokz); } @@ -939,19 +939,19 @@ void tok_free(Token *tok) { - if(TOK_IS_STRING(tok) || TOK_IS_IDENT(tok) || TOK_IS_COMMENT(tok)){ - if(TOK_STRING_VAL(tok)!=NULL) - free(TOK_STRING_VAL(tok)); - } - - tok->type=TOK_INVALID; + if(TOK_IS_STRING(tok) || TOK_IS_IDENT(tok) || TOK_IS_COMMENT(tok)){ + if(TOK_STRING_VAL(tok)!=NULL) + free(TOK_STRING_VAL(tok)); + } + + tok->type=TOK_INVALID; } void tok_init(Token *tok) { - static Token dummy=TOK_INIT; - - memcpy(tok, &dummy, sizeof(*tok)); + static Token dummy=TOK_INIT; + + memcpy(tok, &dummy, sizeof(*tok)); }
--- a/tokenizer.h Mon Feb 16 18:09:03 2004 +0100 +++ b/tokenizer.h Mon Feb 16 18:50:28 2004 +0100 @@ -14,52 +14,52 @@ #include "types.h" -#define TOK_SET_BOOL(TOK, VAL) {(TOK)->type=TOK_BOOL; (TOK)->u.bval=VAL;} -#define TOK_SET_LONG(TOK, VAL) {(TOK)->type=TOK_LONG; (TOK)->u.lval=VAL;} -#define TOK_SET_DOUBLE(TOK, VAL) {(TOK)->type=TOK_DOUBLE; (TOK)->u.dval=VAL;} -#define TOK_SET_CHAR(TOK, VAL) {(TOK)->type=TOK_CHAR; (TOK)->u.cval=VAL;} -#define TOK_SET_STRING(TOK, VAL) {(TOK)->type=TOK_STRING; (TOK)->u.sval=VAL;} -#define TOK_SET_IDENT(TOK, VAL) {(TOK)->type=TOK_IDENT; (TOK)->u.sval=VAL;} -#define TOK_SET_COMMENT(TOK, VAL) {(TOK)->type=TOK_COMMENT; (TOK)->u.sval=VAL;} -#define TOK_SET_OP(TOK, VAL) {(TOK)->type=TOK_OP; (TOK)->u.opval=VAL;} +#define TOK_SET_BOOL(TOK, VAL) {(TOK)->type=TOK_BOOL; (TOK)->u.bval=VAL;} +#define TOK_SET_LONG(TOK, VAL) {(TOK)->type=TOK_LONG; (TOK)->u.lval=VAL;} +#define TOK_SET_DOUBLE(TOK, VAL) {(TOK)->type=TOK_DOUBLE; (TOK)->u.dval=VAL;} +#define TOK_SET_CHAR(TOK, VAL) {(TOK)->type=TOK_CHAR; (TOK)->u.cval=VAL;} +#define TOK_SET_STRING(TOK, VAL) {(TOK)->type=TOK_STRING; (TOK)->u.sval=VAL;} +#define TOK_SET_IDENT(TOK, VAL) {(TOK)->type=TOK_IDENT; (TOK)->u.sval=VAL;} +#define TOK_SET_COMMENT(TOK, VAL) {(TOK)->type=TOK_COMMENT; (TOK)->u.sval=VAL;} +#define TOK_SET_OP(TOK, VAL) {(TOK)->type=TOK_OP; (TOK)->u.opval=VAL;} -#define TOK_TYPE(TOK) ((TOK)->type) -#define TOK_BOOL_VAL(TOK) ((TOK)->u.bval) -#define TOK_LONG_VAL(TOK) ((TOK)->u.lval) -#define TOK_DOUBLE_VAL(TOK) ((TOK)->u.dval) -#define TOK_CHAR_VAL(TOK) ((TOK)->u.cval) -#define TOK_STRING_VAL(TOK) ((TOK)->u.sval) -#define TOK_IDENT_VAL(TOK) ((TOK)->u.sval) -#define TOK_COMMENT_VAL(TOK) ((TOK)->u.sval) -#define TOK_OP_VAL(TOK) ((TOK)->u.opval) +#define TOK_TYPE(TOK) ((TOK)->type) +#define TOK_BOOL_VAL(TOK) ((TOK)->u.bval) +#define TOK_LONG_VAL(TOK) ((TOK)->u.lval) +#define TOK_DOUBLE_VAL(TOK) ((TOK)->u.dval) +#define TOK_CHAR_VAL(TOK) ((TOK)->u.cval) +#define TOK_STRING_VAL(TOK) ((TOK)->u.sval) +#define TOK_IDENT_VAL(TOK) ((TOK)->u.sval) +#define TOK_COMMENT_VAL(TOK) ((TOK)->u.sval) +#define TOK_OP_VAL(TOK) ((TOK)->u.opval) -#define TOK_IS_INVALID(TOK) ((TOK)->type==TOK_INVALID) -#define TOK_IS_BOOL(TOK) ((TOK)->type==TOK_BOOL) -#define TOK_IS_LONG(TOK) ((TOK)->type==TOK_LONG) -#define TOK_IS_DOUBLE(TOK) ((TOK)->type==TOK_DOUBLE) -#define TOK_IS_CHAR(TOK) ((TOK)->type==TOK_CHAR) -#define TOK_IS_STRING(TOK) ((TOK)->type==TOK_STRING) -#define TOK_IS_IDENT(TOK) ((TOK)->type==TOK_IDENT) -#define TOK_IS_COMMENT(TOK) ((TOK)->type==TOK_COMMENT) -#define TOK_IS_OP(TOK) ((TOK)->type==TOK_OP) +#define TOK_IS_INVALID(TOK) ((TOK)->type==TOK_INVALID) +#define TOK_IS_BOOL(TOK) ((TOK)->type==TOK_BOOL) +#define TOK_IS_LONG(TOK) ((TOK)->type==TOK_LONG) +#define TOK_IS_DOUBLE(TOK) ((TOK)->type==TOK_DOUBLE) +#define TOK_IS_CHAR(TOK) ((TOK)->type==TOK_CHAR) +#define TOK_IS_STRING(TOK) ((TOK)->type==TOK_STRING) +#define TOK_IS_IDENT(TOK) ((TOK)->type==TOK_IDENT) +#define TOK_IS_COMMENT(TOK) ((TOK)->type==TOK_COMMENT) +#define TOK_IS_OP(TOK) ((TOK)->type==TOK_OP) -#define TOK_OP_IS(TOK, OP) ((TOK)->type==TOK_OP && (TOK)->u.opval==(OP)) +#define TOK_OP_IS(TOK, OP) ((TOK)->type==TOK_OP && (TOK)->u.opval==(OP)) -#define TOK_TAKE_STRING_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) -#define TOK_TAKE_IDENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) -#define TOK_TAKE_COMMENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) +#define TOK_TAKE_STRING_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) +#define TOK_TAKE_IDENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) +#define TOK_TAKE_COMMENT_VAL(TOK) ((TOK)->type=TOK_INVALID, (TOK)->u.sval) enum{ - TOK_INVALID=0, - TOK_LONG='l', - TOK_DOUBLE='d', - TOK_CHAR='c', - TOK_STRING='s', - TOK_IDENT='i', - TOK_BOOL='b', - TOK_COMMENT='#', - TOK_OP='+' + TOK_INVALID=0, + TOK_LONG='l', + TOK_DOUBLE='d', + TOK_CHAR='c', + TOK_STRING='s', + TOK_IDENT='i', + TOK_BOOL='b', + TOK_COMMENT='#', + TOK_OP='+' }; @@ -67,48 +67,48 @@ #define OP2(X,Y) ((X)|((Y)<<8)) #define OP3(X,Y,Z) ((X)|((Y)<<8)|((Z)<<16)) - OP_L_PAR= '(', OP_R_PAR= ')', OP_L_BRK= '[', OP_R_BRK= ']', - OP_L_BRC= '{', OP_R_BRC= '}', OP_COMMA= ',', OP_SCOLON= ';', + OP_L_PAR= '(', OP_R_PAR= ')', OP_L_BRK= '[', OP_R_BRK= ']', + OP_L_BRC= '{', OP_R_BRC= '}', OP_COMMA= ',', OP_SCOLON= ';', - OP_PLUS= '+', OP_MINUS= '-', OP_MUL= '*', OP_DIV= '/', - OP_MOD= '%', OP_POW= '^', OP_OR= '|', OP_AND= '&', - /*OP_NOT= '~',*/ OP_NOT= '!', OP_ASGN= '=', OP_LT= '<', - OP_GT= '>', OP_DOT= '.', OP_COLON= ':', OP_QMARK= '?', - OP_AT= '@', - OP_NEXTLINE='\n',OP_EOF= -1, - - OP_INC= OP2('+','+'), OP_DEC= OP2('-','-'), - OP_LSHIFT= OP2('<','<'), OP_RSHIFT= OP2('>','>'), - OP_AS_INC= OP2('+','='), OP_AS_DEC= OP2('-','='), - OP_AS_MUL= OP2('*','='), OP_AS_DIV= OP2('/','='), - OP_AS_MOD= OP2('%','='), OP_AS_POW= OP2('^','='), + OP_PLUS= '+', OP_MINUS= '-', OP_MUL= '*', OP_DIV= '/', + OP_MOD= '%', OP_POW= '^', OP_OR= '|', OP_AND= '&', + /*OP_NOT= '~',*/ OP_NOT= '!', OP_ASGN= '=', OP_LT= '<', + OP_GT= '>', OP_DOT= '.', OP_COLON= ':', OP_QMARK= '?', + OP_AT= '@', + OP_NEXTLINE='\n',OP_EOF= -1, + + OP_INC= OP2('+','+'), OP_DEC= OP2('-','-'), + OP_LSHIFT= OP2('<','<'), OP_RSHIFT= OP2('>','>'), + OP_AS_INC= OP2('+','='), OP_AS_DEC= OP2('-','='), + OP_AS_MUL= OP2('*','='), OP_AS_DIV= OP2('/','='), + OP_AS_MOD= OP2('%','='), OP_AS_POW= OP2('^','='), -/* AS_OR= OP2('|','='), AS_AND= OP2('&','='), */ - OP_EQ= OP2('=','='), OP_NE= OP2('!','='), - OP_LE= OP2('<','='), OP_GE= OP2('>','=') - -/* L_AND= OP2('&','&'), L_OR= OP2('|','|'), - L_XOR= OP2('^','^'), */ +/* AS_OR= OP2('|','='), AS_AND= OP2('&','='), */ + OP_EQ= OP2('=','='), OP_NE= OP2('!','='), + OP_LE= OP2('<','='), OP_GE= OP2('>','=') + +/* L_AND= OP2('&','&'), L_OR= OP2('|','|'), + L_XOR= OP2('^','^'), */ -/* AsLShift= OP3('<','<','='), - AsRShift= OP3('>','>','='), */ - +/* AsLShift= OP3('<','<','='), + AsRShift= OP3('>','>','='), */ + #undef OP2 #undef OP3 }; typedef struct{ - int type; - int line; - union{ - bool bval; - long lval; - double dval; - char cval; - char *sval; - int opval; - } u; + int type; + int line; + union{ + bool bval; + long lval; + double dval; + char cval; + char *sval; + int opval; + } u; } Token; #define TOK_INIT {0, 0, {0}} @@ -122,75 +122,75 @@ enum{ - TOKZ_IGNORE_NEXTLINE=0x1, - TOKZ_READ_COMMENTS=0x2, - TOKZ_PARSER_INDENT_MODE=0x04, - TOKZ_ERROR_TOLERANT=0x8, - TOKZ_READ_FROM_BUFFER=0x10, - TOKZ_DEFAULT_OPTION=0x20 + TOKZ_IGNORE_NEXTLINE=0x1, + TOKZ_READ_COMMENTS=0x2, + TOKZ_PARSER_INDENT_MODE=0x04, + TOKZ_ERROR_TOLERANT=0x8, + TOKZ_READ_FROM_BUFFER=0x10, + TOKZ_DEFAULT_OPTION=0x20 }; enum{ - E_TOKZ_UNEXPECTED_EOF=1, - E_TOKZ_UNEXPECTED_EOL, - E_TOKZ_EOL_EXPECTED, - E_TOKZ_INVALID_CHAR, - E_TOKZ_TOOBIG, - E_TOKZ_NUMFMT, - E_TOKZ_NUM_JUNK, - E_TOKZ_NOTINT, - E_TOKZ_RANGE, - E_TOKZ_MULTICHAR, - - E_TOKZ_TOKEN_LIMIT, - E_TOKZ_UNKNOWN_OPTION, - E_TOKZ_SYNTAX, - E_TOKZ_INVALID_ARGUMENT, - E_TOKZ_EOS_EXPECTED, - E_TOKZ_TOO_FEW_ARGS, - E_TOKZ_TOO_MANY_ARGS, - E_TOKZ_MAX_NEST, - E_TOKZ_IDENTIFIER_EXPECTED, - - E_TOKZ_LBRACE_EXPECTED + E_TOKZ_UNEXPECTED_EOF=1, + E_TOKZ_UNEXPECTED_EOL, + E_TOKZ_EOL_EXPECTED, + E_TOKZ_INVALID_CHAR, + E_TOKZ_TOOBIG, + E_TOKZ_NUMFMT, + E_TOKZ_NUM_JUNK, + E_TOKZ_NOTINT, + E_TOKZ_RANGE, + E_TOKZ_MULTICHAR, + + E_TOKZ_TOKEN_LIMIT, + E_TOKZ_UNKNOWN_OPTION, + E_TOKZ_SYNTAX, + E_TOKZ_INVALID_ARGUMENT, + E_TOKZ_EOS_EXPECTED, + E_TOKZ_TOO_FEW_ARGS, + E_TOKZ_TOO_MANY_ARGS, + E_TOKZ_MAX_NEST, + E_TOKZ_IDENTIFIER_EXPECTED, + + E_TOKZ_LBRACE_EXPECTED }; struct _ConfOpt; typedef struct _Tokenizer_FInfo{ - FILE *file; - char *name; - int line; - int ungetc; - Token ungettok; + FILE *file; + char *name; + int line; + int ungetc; + Token ungettok; } Tokenizer_FInfo; typedef struct _Tokenizer_Buffer{ - char *data; - int len; - int pos; + char *data; + int len; + int pos; } Tokenizer_Buffer; - + typedef struct _Tokenizer{ - FILE *file; - char *name; - int line; - int ungetc; - Token ungettok; - - Tokenizer_Buffer buffer; + FILE *file; + char *name; + int line; + int ungetc; + Token ungettok; + + Tokenizer_Buffer buffer; - int flags; - const struct _ConfOpt **optstack; - int nest_lvl; - void *user_data; + int flags; + const struct _ConfOpt **optstack; + int nest_lvl; + void *user_data; - int filestack_n; - Tokenizer_FInfo *filestack; - - char **includepaths; + int filestack_n; + Tokenizer_FInfo *filestack; + + char **includepaths; } Tokenizer;
--- a/util.c Mon Feb 16 18:09:03 2004 +0100 +++ b/util.c Mon Feb 16 18:50:28 2004 +0100 @@ -25,35 +25,35 @@ void libtu_init(const char *argv0) { - progname=argv0; - + progname=argv0; + #ifdef CONFIG_LOCALE - textdomain(simple_basename(argv0)); + textdomain(simple_basename(argv0)); #endif } void libtu_init_copt(int argc, char *const argv[], - const OptParserCommonInfo *cinfo) + const OptParserCommonInfo *cinfo) { - int opt; - - libtu_init(argv[0]); - - optparser_init(argc, argv, OPTP_DEFAULT, NULL, cinfo); - - while((opt=optparser_get_opt())){ - switch(opt){ - default: - optparser_print_error(); - exit(EXIT_FAILURE); - } - } + int opt; + + libtu_init(argv[0]); + + optparser_init(argc, argv, OPTP_DEFAULT, NULL, cinfo); + + while((opt=optparser_get_opt())){ + switch(opt){ + default: + optparser_print_error(); + exit(EXIT_FAILURE); + } + } } const char *prog_execname() { - return progname; + return progname; }
--- a/util.h Mon Feb 16 18:09:03 2004 +0100 +++ b/util.h Mon Feb 16 18:50:28 2004 +0100 @@ -19,8 +19,8 @@ extern void libtu_init(const char *argv0); extern void libtu_init_copt(int argc, char *const argv[], - const OptParserCommonInfo *cinfo); - + const OptParserCommonInfo *cinfo); + extern const char *prog_execname();