static int is_v1_swap_partition(int fd, char **label, char *uuid) { int n = getpagesize(); char *buf = xmalloc(n); struct swap_header_v1_2 *p = (struct swap_header_v1_2 *) buf; if (lseek(fd, 0, SEEK_SET) == 0 && read(fd, buf, n) == n && !strncmp(buf+n-10, "SWAPSPACE2", 10) && p->version == 1) { store_uuid(uuid, p->uuid); store_label(label, p->volume_name, 16); return 1; } return 0; }
/* * Get both label and uuid. * For now, only ext2, ext3, xfs, ocfs, ocfs2, reiserfs, swap are supported * * Return 0 on success. */ int get_label_uuid(const char *device, char **label, char *uuid) { int fd; struct ext2_super_block e2sb; struct xfs_super_block xfsb; struct jfs_super_block jfssb; struct ocfs_volume_header ovh; /* Oracle */ struct ocfs_volume_label olbl; struct ocfs2_super_block osb; struct reiserfs_super_block reiserfssb; int blksize; int rv = 0; fd = open(device, O_RDONLY); if (fd < 0) return -1; /* If there is a RAID partition, or an error, ignore this partition */ if (is_raid_partition(fd)) { rv = 1; goto done; } if (is_v1_swap_partition(fd, label, uuid)) goto done; if (lseek(fd, 1024, SEEK_SET) == 1024 && read(fd, (char *) &e2sb, sizeof(e2sb)) == sizeof(e2sb) && (ext2magic(e2sb) == EXT2_SUPER_MAGIC)) { store_uuid(uuid, e2sb.s_uuid); store_label(label, e2sb.s_volume_name, sizeof(e2sb.s_volume_name)); goto done; } if (lseek(fd, 0, SEEK_SET) == 0 && read(fd, (char *) &xfsb, sizeof(xfsb)) == sizeof(xfsb) && (strncmp(xfsb.s_magic, XFS_SUPER_MAGIC, 4) == 0)) { store_uuid(uuid, xfsb.s_uuid); store_label(label, xfsb.s_fname, sizeof(xfsb.s_fname)); goto done; } if (lseek(fd, 0, SEEK_SET) == 0 && read(fd, (char *) &ovh, sizeof(ovh)) == sizeof(ovh) && (strncmp(ovh.signature, OCFS_MAGIC, sizeof(OCFS_MAGIC)) == 0) && (lseek(fd, 512, SEEK_SET) == 512) && read(fd, (char *) &olbl, sizeof(olbl)) == sizeof(olbl)) { store_uuid(uuid, NULL); store_label(label, olbl.label, ocfslabellen(olbl)); goto done; } if (lseek(fd, JFS_SUPER1_OFF, SEEK_SET) == JFS_SUPER1_OFF && read(fd, (char *) &jfssb, sizeof(jfssb)) == sizeof(jfssb) && (strncmp(jfssb.s_magic, JFS_MAGIC, 4) == 0)) { /* The situation for jfs is rather messy. The structure of the superblock changed a few times, but there seems to be no good way to check what kind of sb we have. Old (OS/2 compatible) jfs filesystems don't have UUIDs and have an 11-byte label in s_fpack[]. Kernel 2.5.6 supports jfs v1; 2.5.8 supports v2; 2.5.18 has label/uuid. Kernel 2.4.20 supports jfs v2 with label/uuid. s_version will be 2 for new filesystems using an external log. Other new filesystems will have version 1. Label and UUID can be set by jfs_tune. */ /* Let us believe label/uuid on v2, and on v1 only when label agrees with s_fpack in the first 11 bytes. */ if (assemble4le(jfssb.s_version) == 1 && strncmp(jfssb.s_label, jfssb.s_fpack, 11) != 0) { store_uuid(uuid, NULL); store_label(label, jfssb.s_fpack, sizeof(jfssb.s_fpack)); } else { store_uuid(uuid, jfssb.s_uuid); store_label(label, jfssb.s_label, sizeof(jfssb.s_label)); } goto done; } if (lseek(fd, REISERFS_DISK_OFFSET_IN_BYTES, SEEK_SET) == REISERFS_DISK_OFFSET_IN_BYTES && read(fd, (char *) &reiserfssb, sizeof(reiserfssb)) == sizeof(reiserfssb) /* Only 3.6.x format supers have labels or uuids. Label and UUID can be set by reiserfstune -l/-u. */ && reiserfs_magic_version(reiserfssb.s_magic) > 1) { store_uuid(uuid, reiserfssb.s_uuid); store_label(label, reiserfssb.s_label, sizeof(reiserfssb.s_label)); goto done; } for (blksize = OCFS2_MIN_BLOCKSIZE; blksize <= OCFS2_MAX_BLOCKSIZE; blksize <<= 1) { int blkoff = blksize * OCFS2_SUPER_BLOCK_BLKNO; if (lseek(fd, blkoff, SEEK_SET) == blkoff && read(fd, (char *) &osb, sizeof(osb)) == sizeof(osb) && strncmp(osb.signature, OCFS2_SUPER_BLOCK_SIGNATURE, sizeof(OCFS2_SUPER_BLOCK_SIGNATURE)) == 0) { store_uuid(uuid, osb.s_uuid); store_label(label, osb.s_label, sizeof(osb.s_label)); goto done; } } rv = 1; done: close(fd); return rv; }
gboolean mcus_compiler_parse (MCUSCompiler *self, const gchar *code, GError **error) { /* In EBNF: * comment ::= ";" , ? any characters ? , "\n" * terminating-whitespace ::= comment | whitespace | "\n" * assembly ::= { terminating-whitespace , { terminating-whitespace } , ( instruction | label | lookup_table ) } , { terminating-whitespace } , "\0" */ /* Set up parser variables */ reset_state (self); self->priv->code = code; self->priv->i = code; self->priv->dirty = TRUE; skip_whitespace (self, TRUE, FALSE); while (*(self->priv->i) != '\0') { MCUSInstruction instruction; MCUSLabel label; MCUSLookupTable lookup_table; GError *child_error = NULL; /* Are we finished? */ if (*(self->priv->i) == '\0') break; if (lex_lookup_table (self, &lookup_table, &child_error) == TRUE) { /* Lookup table */ if (store_lookup_table (self, &lookup_table, &child_error) == FALSE) { free_lookup_table (&lookup_table); goto throw_error; } skip_whitespace (self, TRUE, FALSE); continue; } else if (g_error_matches (child_error, MCUS_COMPILER_ERROR, MCUS_COMPILER_ERROR_INVALID_LOOKUP_TABLE) == FALSE) { goto throw_error; } else { g_clear_error (&child_error); } if (lex_label (self, &label, &child_error) == TRUE) { /* Label */ if (store_label (self, &label, &child_error) == FALSE) { free_label (&label); goto throw_error; } skip_whitespace (self, TRUE, FALSE); continue; } else if (g_error_matches (child_error, MCUS_COMPILER_ERROR, MCUS_COMPILER_ERROR_INVALID_LABEL_DELIMITATION) == FALSE) { goto throw_error; } else { g_clear_error (&child_error); } if (lex_instruction (self, &instruction, &child_error) == TRUE) { /* Instruction */ store_instruction (self, &instruction); skip_whitespace (self, TRUE, FALSE); continue; } throw_error: /* Throw the error */ g_propagate_error (error, child_error); return FALSE; } return TRUE; }
static int read_ftable(char *name,FREQ **f,int *pdim,int *pncvar,int *nc, char **varname,char **cname,char *type,int *pndec) { int i,j,k,j1,j2,apos,j0,ivar,rep,h,h2,n=0,len; unsigned int ncell,cell,ncell2,step; char x[LLENGTH], *px[EP4]; char *p; int ncol,nrow; int nlabel,nlab; int posr[MAXDIM]; FREQ cvalue; j=wfind("TABLE",name,1); if (j<0) { sprintf(sbuf,"\nTABLE %s not found in the edit field!",name); sur_print(sbuf); WAIT; return(-1); } edread(x,j); i=split(x+1,px,5); if (i>2 && strcmp(px[2],"/")==0) i=2; if (i==2) { *pdim=2; i=j; k=0; while (1) { ++i; edread(x,i); if (i==j+1) n=split(x+1,px,EP4); if (strncmp(x+1,space,c2)==0) break; ++k; } ncell=k*n; if (*f==NULL) *f=(FREQ *)muste_malloc(ncell*sizeof(FREQ)); else *f=(FREQ *)muste_realloc(*f,ncell*sizeof(FREQ)); if (*f==NULL) { not_enough_memory(); return(-1); } for (i=0; i<k; ++i) { edread(x,j+1+i); h=split(x+1,px,EP4); if (i==0) n=h; else { if (h!=n) { sprintf(sbuf,"\nError in table on edit line %d !",i); sur_print(sbuf); WAIT; return(-1); } } for (h=0; h<n; ++h) (*f)[i+k*h]=atoi(px[h]); } nc[1]=k; nc[0]=n; varname[0]="C"; varname[1]="R"; p=text; for (i=0; i<n; ++i) { cname[i]=p; sprintf(sbuf,"C%d",i+1); strcpy(p,sbuf); p+=strlen(sbuf); *p=EOS; ++p; } for (i=0; i<k; ++i) { cname[i+n]=p; sprintf(sbuf,"R%d",i+1); strcpy(p,sbuf); p+=strlen(sbuf); *p=EOS; ++p; } return(1); } if (i<5) { edread(x,j); i=strlen(x); while (x[i-1]==' ') x[--i]=EOS; sprintf(sbuf,"\nInvalid definition: %s",x+1); sur_print(sbuf); sprintf(sbuf,"\non line %d",j); sur_print(sbuf); sur_print("\nCorrect form: TABLE <name>,L1,L2,<type_of_table>"); WAIT; return(-1); } j1=edline2(px[2],1,1); if (j1==0) return(-1); j2=edline2(px[3],j1,1); if (j2==0) return(-1); strncpy(type,px[4],15); type[15]=EOS; ncell=0; for (j=j1; j<=j2; ++j) { edread(x,j); p=strchr(x+1,'*'); if (p!=NULL) break; } if (j>j2) { sprintf(sbuf,"\nLine of row classifiers ending with *'s missing in table %s!", name); sur_print(sbuf); WAIT; return(-1); } apos=p-x; j0=j; ncol=j0-j1; k=0; for (j=j0+1; j<=j2; ++j) { edread(x,j); i=split(x+apos,px,EP4); if (i==0) continue; if (k==0) k=i; if (i!=k) { sprintf(sbuf,"\nNumber of elements on line %d conflicts previous lines!", j); sur_print(sbuf); WAIT; return(-1); } ncell+=k; } if (*f==NULL) *f=(FREQ *)muste_malloc(ncell*sizeof(FREQ)); else *f=(FREQ *)muste_realloc(*f,ncell*sizeof(FREQ)); if (*f==NULL) { not_enough_memory(); return(-1); } nlabel=0; ivar=0; rep=1; for (j=j1; j<j0; ++j) { edread(x,j); n=split(x+1,px,EP4); if (n<3) { sprintf(sbuf,"\nError in column classifier on line %d",j); sur_print(sbuf); WAIT; return(-1); } h=(n-1)/rep; if (h*rep!=n-1) { sprintf(sbuf,"\nError in labels on line %d",j); sur_print(sbuf); WAIT; return(-1); } nc[ivar]=h; varname[ivar]=ptext; i=store_label(px[0]); if (i<0) return(-1); for (h=0; h<nc[ivar]; ++h) { if (nlabel>=MAXT) { sur_print("\nToo many labels!"); WAIT; return(-1); } cname[nlabel++]=ptext; i=store_label(px[h+1]); } rep*=nc[ivar]; ++ivar; } edread(x,j0); i=split(x+1,px,EP4); nrow=i-1; ivar=ncol; for (i=0; i<nrow; ++i) { posr[i]=px[i]-x; varname[ivar++]=ptext; h=store_label(px[i]); } posr[nrow]=apos; ivar=ncol; ncell2=k; for (i=0; i<nrow; ++i) { nc[ivar]=0; nlab=nlabel; for (j=j0+1; j<=j2; ++j) { edread(x,j); h=posr[i]; while (x[h]==' ' && h<posr[i+1]) ++h; if (h==posr[i+1]) continue; h2=h; while (x[h2]!=' ' && h2<posr[i+1]) ++h2; x[h2]=EOS; for (h2=0; h2<nc[ivar]; ++h2) { if (strcmp(x+h,cname[nlab+h2])==0) break; } if (h2<nc[ivar]) continue; ++nc[ivar]; if (nlabel>=MAXT) { sur_print("\nToo many labels!"); WAIT; return(-1); } cname[nlabel++]=ptext; h2=store_label(x+h); if (h2<0) return(-1); } ncell2*=nc[ivar]; ++ivar; } if (ncell2!=ncell) { sur_print("\nError in (row) labels!"); i=0; for (ivar=0; ivar<ncol+nrow; ++ivar) { sprintf(sbuf,"\n%s:",varname[ivar]); sur_print(sbuf); for (h=0; h<nc[ivar]; ++h) { sprintf(sbuf," %s",cname[i+h]); sur_print(sbuf); } i+=nc[ivar]; } WAIT; return(-1); } *pdim=ncol+nrow; h=0; step=ncell/k; *pndec=0; for (j=j0+1; j<=j2; ++j) { edread(x,j); i=split(x+apos,px,k); if (i==0) continue; cell=h; for (i=0; i<k; ++i) { len=strlen(px[i])-1; if (px[i][len]=='-') { cvalue=MISSING_VALUE; ++missing_values; } else if (strcmp(px[i],"*0")==0) cvalue=STRUCTURAL_ZERO; else cvalue=atof(px[i]); /* depends on FREQ */ (*f)[cell]=cvalue; cell+=step; p=strchr(px[i],'.'); if (p!=NULL) { h2=len-(p-px[i]); if (h2>*pndec) *pndec=h2; } } ++h; } *pncvar=ncol; return(1); }
int start_parser(struct p_parser *p, char *f) { FILE *file; int i,fd,pos; char c; char *buffer, *temp; struct stat file_stats; temp = NULL; buffer = NULL; file = fopen(f,"r"); if (file == NULL){ fprintf(stderr,"Error Reading File: %s\n",f); return errno; } fd = fileno(file); if (stat(f,&file_stats) != 0) return errno; p->open_time = file_stats.st_atime; p->fsize = file_stats.st_size; buffer = mmap(NULL,p->fsize,PROT_READ,MAP_SHARED,fd,0); if (buffer == MAP_FAILED){ #ifdef DEBUG fprintf(stderr,"mmap failed: %s\n",strerror(errno)); #endif fclose(file); return EIO; } #ifdef DEBUG fprintf(stderr,"fd: %d st_atime:%d st_size:%d mmap:%p\n",fd,(int)p->open_time,(int)p->fsize,buffer); #endif p->state = S_START; pos=0; for (i=0;i<p->fsize;i++){ c = buffer[i]; switch (p->state){ case S_COMMENT: switch(c) { case '\n': case '\r': if (!store_comment(p,buffer,pos,i)) return KATCP_RESULT_FAIL; p->state = S_START; pos = i+1; break; } break; case S_LABEL: switch(c){ case CLABEL: if (!store_label(p,buffer,pos,i)) return KATCP_RESULT_FAIL; p->state = S_START; pos = i; break; } break; case S_SETTING: if (!store_setting(p,buffer,pos,i-1)) return KATCP_RESULT_FAIL; pos = i; p->state = S_VALUE; break; case S_VALUE: switch(c){ case OLABEL: p->state = S_MLVALUE; pos = i; break; case VALUE: if (!store_value(p,buffer,pos,i)) return KATCP_RESULT_FAIL; p->state = S_VALUE; pos = i+1; break; case '\n': case '\r': if (!store_value(p,buffer,pos,i)) return KATCP_RESULT_FAIL; p->state = S_START; pos = i+1; break; } break; case S_MLVALUE: switch(c){ case CLABEL: if (!store_value(p,buffer,pos,i+1)) return KATCP_RESULT_FAIL; p->state = S_START; break; } break; default: switch (c){ case COMMENT: p->state = S_COMMENT; pos = i; break; case OLABEL: p->state = S_LABEL; pos = i+1; break; case SETTING: p->state = S_SETTING; break; case '\n': case '\r': p->state = S_START; pos = i+1; break; } } } munmap(buffer,p->fsize); fclose(file); return EX_OK; }