Example #1
0
local void convert(stream instr, stream outstr)
{
    char   line[MAX_LINELEN];          /* input linelength */
    int    i, nlines, noutv;
    string *outv;                   /* pointer to vector of strings to write */
    char   *cp, *seps=", \t";       /* column separators  */
        
    nlines=0;               /* count lines read so far */

    for (;;) {
        if (get_line(instr, line) < 0)      /* EOF */
            return; 					     

        dprintf(3,"LINE: (%s)\n",line);
        if (iscomment(line)) continue;
        
        nlines++;
        tab2space(line);                  /* work around a Gipsy (?) problem */

        outv = burststring(line,seps);
        noutv = xstrlen(outv,sizeof(string)) - 1;
        if (noutv < maxcol)
            error("Too few columns in input file (%d < %d)",noutv,maxcol);
        if (Qall) nkeep = noutv;
                        
        for (i=0; i<nkeep; i++) {
            if (keep[i] == 0)
                fprintf(outstr,"%d",nlines);
            else
                fprintf(outstr,"%s",outv[keep[i]-1]);
            if (i < nkeep-1) fprintf(outstr,"%c",colsep);
        }
        if (colsep != 'n') fprintf(outstr,"\n");    /* end of line */
    }
}
Example #2
0
/* seek a particular page */
void seekpage(int p)
{
   fseeko(infile, pageptr[p], SEEK_SET);
   if (fgets(buffer, BUFSIZ, infile) != NULL &&
       iscomment(buffer, "%%Page:")) {
      char *start, *end;
      for (start = buffer+7; isspace(*start); start++);
      if (*start == '(') {
	 int paren = 1;
	 for (end = start+1; paren > 0; end++)
	    switch (*end) {
	    case '\0':
	       message(FATAL, "Bad page label while seeking page %d\n", p);
	    case '(':
	       paren++;
	       break;
	    case ')':
	       paren--;
	       break;
            default:
               break;
	    }
      } else
	 for (end = start; !isspace(*end); end++);
      strncpy(pagelabel, start, end-start);
      pagelabel[end-start] = '\0';
      pageno = atoi(end);
   } else
      message(FATAL, "I/O error seeking page %d\n", p);
}
Example #3
0
int
cmdline_isendoftoken(char c)
{
	if (!c || iscomment(c) || isblank2(c) || isendofline(c))
		return 1;
	return 0;
}
Example #4
0
int
cmdline_isendofcommand(char c)
{
	if (!c || iscomment(c) || isendofline(c))
		return 1;
	return 0;
}
Example #5
0
static void *lex_comment(struct lexer *lexer)
{
    while (iscomment(next(lexer)))
        ;
    backup(lexer);
    return lex_config;
}
Example #6
0
void Lexer::findNext(void)
{
	this->getNext();

	while (isspace(this->current) || iscomment(this->current))
	{
		if (isspace(this->current))
		{
			while (isspace(this->current)) // skip white space
				this->getNext();
		}
		else if (iscomment(this->current))
		{
			while (this->current != '\n') // skip comments
				this->getNext();
		}
	}
}
static int parse_u8_array_string(unsigned char *d, const char *s)
{
	int i = 0;
	bool sop = 0;

	while (!iseop(*s)) {
		if (iscomment(*s)) {
			if (s[1] == '*') {
				s += 2;
				while (!(s[0] == '*' && s[1] == '/'))
					s++;
				s += 2;
				pr_info("%s: find end of comment\n", __func__);
				continue;
			} else if (s[1] == '/') {
				s += 2;
				while (!isnewline(*s++));
				pr_info("%s: find new line\n", __func__);
				continue;
			} else {
				/* syntax error */
				pr_info("%s: syntax error\n", __func__);
				return -EINVAL;
			}
		}

		if (sop) {
			if (isspace(*s)) {
				s++;
				continue;
			}

			if (!isxdigit(s[0]) || !isxdigit(s[1])) {
				/* syntax error */
				pr_err("%s: syntax error\n", __func__);
				return -EINVAL;
			}

			sscanf(s, "%2hhx", &d[i]);
			s += 2; i++;
			if (i >= MAX_BUF_SIZE) {
				pr_err("%s: exceeded dst buf size(%d)!!\n",
						__func__, MAX_BUF_SIZE);
				return -EINVAL;
			}
			continue;
		}

		if (issop(*s))
			sop = true;
		s++;
	}

	return i;
}
Example #8
0
static char *
resolve_name(
    const char *lc_name,
    char *file_name,
    MapDirection direction)
{
    FILE *fp;
    char buf[XLC_BUFSIZE], *name = NULL;

    fp = _XFopenFile (file_name, "r");
    if (fp == NULL)
	return NULL;

    while (fgets(buf, XLC_BUFSIZE, fp) != NULL) {
	char *p = buf;
	int n;
	char *args[2], *from, *to;
#ifdef __UNIXOS2__  /* Take out CR under OS/2 */
	int len;

	len = strlen(p);
	if (len > 1) {
	    if (*(p+len-2) == '\r' && *(p+len-1) == '\n') {
		*(p+len-2) = '\n';
		*(p+len-1) = '\0';
	    }
	}
#endif
	while (isspace(*p)) {
	    ++p;
	}
	if (iscomment(*p)) {
	    continue;
	}
	n = parse_line(p, args, 2);		/* get first 2 fields */
	if (n != 2) {
	    continue;
	}
	if (direction == LtoR) {
	    from = args[0], to = args[1];	/* left to right */
	} else {
	    from = args[1], to = args[0];	/* right to left */
	}
	if (! strcmp(from, lc_name)) {
	    name = Xmalloc(strlen(to) + 1);
	    if (name != NULL) {
		strcpy(name, to);
	    }
	    break;
	}
    }
    fclose(fp);
    return name;
}
Example #9
0
const Token Lexer::parseSymbol(void)
{
	std::string token;
	while (!isspace(this->current) &&
	       !iscomment(this->current) &&
	       !issyntax(this->current))
	{
		token += this->current;
		this->match();
	}

	this->finishToken();
	return Token(Token::Type::Symbol, token);
}
Example #10
0
void InitFORTRANFlags( linenum line_no )
{
    char    *text;
//    char    *start;
    line    *line;
    fcb     *fcb;
    vi_rc   rc;
    int     numQuotes = 0;

    flags.inString = false;

    rc = CGimmeLinePtr( line_no, &fcb, &line );
    if( rc != ERR_NO_ERR ) {
        // probably past eof
        return;
    }

    if( isInitialLine( line ) ) {
        return;
    }

    for( ;; ) {
        rc = GimmePrevLinePtr( &fcb, &line );
        if( rc != ERR_NO_ERR ) {
            break;
        }
        text = line->u.ld.nolinedata ? WorkLine->data : line->data;
//        start = text;
        if( iscomment( *text ) ) {
            continue;
        }
        while( *text != '\0' ) {
            if( *text == '!' ) {
                // rest of line is part of a comment
                break;
            } else if( *text == '\'' ) {
                numQuotes ^= 1;
            }
            text++;
        }
        if( isInitialLine( line ) ) {
            break;
        }
    }

    if( numQuotes == 1 ) {
        flags.inString = true;
    }
}
Example #11
0
/* get the next entry */
int
conffile_getent(conffile_t *sp, ent_t *ep)
{
	char	*from;

	for (;;) {
		if ((from = read_line(sp, ep)) == NULL) {
			return 0;
		}
		if (iscomment(sp, from)) {
			continue;
		}
		return conffile_split(sp, ep, from);
	}
}
Example #12
0
File: XlcDL.c Project: iquiw/xsrc
static int
parse_line(char *line, char **argv, int argsize)
{
    int argc = 0;
    char *p = line;

    while (argc < argsize) {
        while (isspace(*p)) {
            ++p;
        }
        if (iscomment(*p)) {
            break;
        }
        argv[argc++] = p;
        while (!isspace(*p)) {
            ++p;
        }
        if (iscomment(*p)) {
            break;
        }
        *p++ = '\0';
    }
    return argc;
}
Example #13
0
/* put the new entry (in place of ent[f] == val, if val is non-NULL) */
int
conffile_putent(conffile_t *sp, int f, char *val, char *newent)
{
	ent_t	 e;
	FILE	*fp;
	char	 name[MAXPATHLEN];
	char	*from;
	int	 fd;

	(void) strlcpy(name, "/tmp/split.XXXXXX", sizeof(name));
	if ((fd = mkstemp(name)) < 0) {
		(void) fprintf(stderr, "can't mkstemp `%s' (%s)\n", name, strerror(errno));
		return 0;
	}
	fp = fdopen(fd, "w");
	(void) memset(&e, 0x0, sizeof(e));
	for (;;) {
		if ((from = read_line(sp, &e)) == NULL) {
			break;
		}
		if (iscomment(sp, from)) {
			if (!safe_write(fp, e.buf, strlen(e.buf))) {
				return report_error(fp, name, "Short write 1 to `%s' (%s)\n", name, strerror(errno));
			}
		}
		(void) conffile_split(sp, &e, from);
		if (val != NULL && (uint32_t)f < e.sv.c && strcmp(val, e.sv.v[f]) == 0) {
			/* replace it */
			if (!safe_write(fp, newent, strlen(newent))) {
				return report_error(fp, name, "Short write 2 to `%s' (%s)\n", name, strerror(errno));
			}
		} else {
			if (!safe_write(fp, e.buf, strlen(e.buf))) {
				return report_error(fp, name, "Short write 3 to `%s' (%s)\n", name, strerror(errno));
			}
		}
	}
	if (val == NULL && !safe_write(fp, newent, strlen(newent))) {
		return report_error(fp, name, "Short write 4 to `%s' (%s)\n", name, strerror(errno));
	}
	(void) fclose(fp);
	if (rename(name, sp->name) < 0) {
		return report_error(NULL, name, "can't rename %s to %s (%s)\n", name, sp->name, strerror(errno));
	}
	return 1;
}
Example #14
0
int main(int argc, char *argv[])
{
   FILE         *fp     = NULL        ;
   char          fpt[1024-128]        ;
   int           e      = 0           ;
   int           i      = 0           ;
   unsigned long IH[0x08]             ;
   unsigned long  H[0x08]             ;
   char  mode[2], h[1024]             ; /* (128)hash message + file name */
   unsigned char l      = 0x00        ;

   if((argc > 1) && !((strcmp(argv[1], "-c") == 0)||(strcmp(argv[1], "-v") == 0))){
      for (i = 1; i < argc; i++) {
         if(md(argv[i], IH) == 0) {
            for(e=0; e<0x04; e++) printf("%08lx", IH[e]); printf("  %s\n", argv[i]);
         } else {
            fprintf(stderr, "md4sum unable to open: %s\n", argv[i]);
         }
      }
      return 0;
   }

   i = 0; e = 0;
   if((argc == 3) && ((strcmp(argv[1], "-c") == 0) || (strcmp(argv[1], "-v") == 0))){
      if ((fp = fopen(argv[2], "rb")) == NULL) { printf("md4sum unable to open: %s\n", argv[2]); return 0; }
      while(!feof(fp)) {
         if(fgets(h, 1024, fp)) {
            if(iscomment(h[0])) continue;
            gethashul(h, H, mode, fpt, 4);
            md(fpt, IH);
            l = (unsigned char )cmpul(IH, H, 4);
            printf("%s : %s\n", (!l) ? "[  OK  ]" : "[FAILED]" ,fpt);
            if(l) e++;
            i++;
         }
      }
      if(e>0) fprintf(stderr, "!WARNING!: %d of %d didn't have matching hash.\n", e, i);
      return 0;
   }

   return 0;
}
Example #15
0
const Token Lexer::Next(void)
{
	if (pushback_tokens.size() != 0) {
		Token t = pushback_tokens.back();
		pushback_tokens.pop_back();
		return t;
	}

	assert(this->HasNext());
	assert(!isspace(this->current));
	assert(!iscomment(this->current));

	if (isdigit(this->current))
		return this->parseNumber();
	else if (this->current == '\'' || this->current == '"')
		return this->parseString();
	else if (strchr(Lexer::SYNTAX, this->current))
		return this->parseSyntax();
	else
		return this->parseSymbol();
}
Example #16
0
// Return FALSE when compilation has finished
bool compiler::compile_token(const std::string& s, parser& p)
{
  if ( s.empty() ) {
    m.load_halt();
    resolve_forwards();
    return false;
  }
  else if ( ishalt(s) )    m.load_halt();
  else if ( iscomment(s) ) p.skip_line();
  else if ( isliteral(s) ) compile_literal(s);
  else if ( islabel(s) )   m.addlabel(s.c_str(), m.pos());
  else {
    Op op = tok2op(s);

    if ( op == NOP_END )
      error("Unknown operation: " + s);

    m.load(op);
  }

  return true;
}
Example #17
0
/**
 * try to match the buffer with an instruction (only the first
 * nb_match_token tokens if != 0). Return 0 if we match all the
 * tokens, else the number of matched tokens, else -1.
 */
static int
match_inst(cmdline_parse_inst_t *inst, const char *buf,
	   unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
{
	cmdline_parse_token_hdr_t *token_p = NULL;
	unsigned int i=0;
	int n = 0;
	struct cmdline_token_hdr token_hdr;

	/* check if we match all tokens of inst */
	while (!nb_match_token || i < nb_match_token) {
		token_p = get_token(inst, i);
		if (!token_p)
			break;
		memcpy(&token_hdr, token_p, sizeof(token_hdr));

		debug_printf("TK\n");
		/* skip spaces */
		while (isblank2(*buf)) {
			buf++;
		}

		/* end of buf */
		if ( isendofline(*buf) || iscomment(*buf) )
			break;

		if (resbuf == NULL) {
			n = token_hdr.ops->parse(token_p, buf, NULL, 0);
		} else {
			unsigned rb_sz;

			if (token_hdr.offset > resbuf_size) {
				printf("Parse error(%s:%d): Token offset(%u) "
					"exceeds maximum size(%u)\n",
					__FILE__, __LINE__,
					token_hdr.offset, resbuf_size);
				return -ENOBUFS;
			}
			rb_sz = resbuf_size - token_hdr.offset;

			n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
				token_hdr.offset, rb_sz);
		}

		if (n < 0)
			break;

		debug_printf("TK parsed (len=%d)\n", n);
		i++;
		buf += n;
	}

	/* does not match */
	if (i==0)
		return -1;

	/* in case we want to match a specific num of token */
	if (nb_match_token) {
		if (i == nb_match_token) {
			return 0;
		}
		return i;
	}

	/* we don't match all the tokens */
	if (token_p) {
		return i;
	}

	/* are there are some tokens more */
	while (isblank2(*buf)) {
		buf++;
	}

	/* end of buf */
	if ( isendofline(*buf) || iscomment(*buf) )
		return 0;

	/* garbage after inst */
	return i;
}
Example #18
0
int trd_read (TABREAD *trd)
{                               /* --- read the next table field */
  int  c, d;                    /* character read, delimiter type */
  char *p, *e;                  /* to traverse the field */

  /* --- initialize --- */
  assert(trd && trd->file);     /* check the function arguments */
  trd->pos = (trd->delim == TRD_FLD) ? trd->pos+1 : 1;
  trd->field[trd->len = 0] = 0; /* clear the current field */
  GETC(trd, c, TRD_EOF);        /* get the first character */

  /* --- skip comment records --- */
  if (trd->delim != TRD_FLD) {  /* if at the start of a record */
    while (iscomment(c)) {      /* while the record is a comment */
      while (!isrecsep(c))      /* while not at end of record, */
        GETC(trd, c, TRD_EOF);  /* get the next character */
      trd->rec++;               /* count the comment record */
      GETC(trd, c, TRD_EOF);    /* get the first character */
    }                           /* after the comment record */
  }                             /* (comment records are skipped) */

  /* --- skip leading blanks --- */
  while (isblank(c))            /* while the character is blank, */
    GETC(trd, c, TRD_REC);      /* get the next character */
  if (issep(c)) {               /* check for field/record separator */
    trd->last = c;              /* store the last character read */
    if (isfldsep(c)) return trd->delim = TRD_FLD;
    trd->rec++;      return trd->delim = TRD_REC;
  }                             /* if at end of record, count record */
  /* Note that after at least one valid character was read, even  */
  /* if it is a blank, the end of file/input is translated into a */
  /* record separator. EOF is returned only if no character could */
  /* be read before the end of file/input is encountered.         */

  /* --- read the field --- */
  p = trd->field; e = p +TRD_MAXLEN;
  while (1) {                   /* field read loop */
    if (p < e) *p++ = (char)c;  /* append the last character */
    c = trd_getc(trd);          /* and get the next character */
    if (c < 0)    { d = (c <= TRD_ERR) ? TRD_ERR : TRD_REC; break; }
    if (issep(c)) { d = (isfldsep(c))  ? TRD_FLD : TRD_REC; break; }
  }                             /* while character is no separator */
  trd->last = c;                /* store the last character read */

  /* --- remove trailing blanks --- */
  while (isblank(*--p));        /* skip blank characters at the end */
  *++p = '\0';                  /* and terminate the current field */
  trd->len = (size_t)(p -trd->field); /* store number of characters */

  /* --- check for a null value --- */
  while (--p >= trd->field)     /* check for only null value chars. */
    if (!isnull((unsigned char)*p)) break;
  if (p < trd->field)           /* clear field if null value */
    trd->field[trd->len = 0] = 0;

  /* --- check for end of line --- */
  if (d != TRD_FLD) {           /* if not at a field separator */
    if (d == TRD_REC) trd->rec++;
    return trd->delim = d;      /* if at end of record, count record, */
  }                             /* and then abort the function */

  /* --- skip trailing blanks --- */
  while (isblank(c)) {          /* while character is blank, */
    trd->last = c;              /* note the last character */
    GETC(trd, c, TRD_REC);      /* and get the next character */
  }
  if (isrecsep(c)) {            /* check for a record separator */
    trd->last = c; trd->rec++; return trd->delim = TRD_REC; }
  if (isfldsep(c))              /* note the field separator or */
    trd->last = c;              /* put back last character (may be */
  else trd_ungetc(trd, c);      /* necessary if blank = field sep.) */
  return trd->delim = TRD_FLD;  /* return the delimiter type */
}  /* trd_read() */
Example #19
0
int8_t
parse(parse_pgm_ctx_t ctx[], const char * buf)
{
	uint8_t inst_num=0;
	parse_pgm_inst_t * inst;
	const char * curbuf;
	char result_buf[256]; /* XXX align, size zé in broblém */
	void (*f)(void *, void *) = NULL;
	void * data = NULL;
	int comment = 0;
	int linelen = 0;
	int parse_it = 0;
	int8_t err = PARSE_NOMATCH;
	int8_t tok;
#ifdef CMDLINE_DEBUG
	char debug_buf[64];
#endif

	/* 
	 * - look if the buffer contains at least one line
	 * - look if line contains only spaces or comments 
	 * - count line length
	 */
	curbuf = buf;
	while (! isendofline(*curbuf)) {
		if ( *curbuf == '\0' ) {
			debug_printf("Incomplete buf (len=%d)\n", linelen);
			return 0;
		}
		if ( iscomment(*curbuf) ) {
			comment = 1;
		}
		if ( ! isblank(*curbuf) && ! comment) {
			parse_it = 1;
		}
		curbuf++;
		linelen++;
	}

	/* skip all endofline chars */
	while (isendofline(buf[linelen])) {
		linelen++;
	}

	/* empty line */
	if ( parse_it == 0 ) {
		debug_printf("Empty line (len=%d)\n", linelen);
		return linelen;
	}

#ifdef CMDLINE_DEBUG
	snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
	debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
#endif

	/* parse it !! */
	inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
	while (inst) {
		debug_printf("INST\n");

		/* fully parsed */
		tok = match_inst(inst, buf, 0, result_buf);

		if (tok > 0) /* we matched at least one token */
			err = PARSE_BAD_ARGS;

		else if (!tok) {
			debug_printf("INST fully parsed\n");
			/* skip spaces */
			while (isblank(*curbuf)) {
				curbuf++;
			}
			
			/* if end of buf -> there is no garbage after inst */
			if (isendofline(*curbuf) || iscomment(*curbuf)) {
				if (!f) {
					memcpy_P(&f, &inst->f, sizeof(f));
					memcpy_P(&data, &inst->data, sizeof(data));
				}
				else {
					/* more than 1 inst matches */
					err = PARSE_AMBIGUOUS;
					f=NULL;
					debug_printf("Ambiguous cmd\n");
					break;
				}
			}
		}
			
		inst_num ++;
		inst = (parse_pgm_inst_t *)pgm_read_word(ctx+inst_num);
	}
	
	/* call func */
	if (f) {
		f(result_buf, data);
	}

	/* no match */
	else {
		debug_printf("No match err=%d\n", err);
		return err;
	}
	
	return linelen;
}
Example #20
0
File: XlcDL.c Project: iquiw/xsrc
static void
resolve_object(char *path, const char *lc_name)
{
    char filename[BUFSIZ];
    FILE *fp;
    char buf[BUFSIZ];

    if (lc_len == 0) { /* True only for the 1st time */
        lc_len = OBJECT_INIT_LEN;
        xi18n_objects_list = Xmalloc(sizeof(XI18NObjectsListRec) * lc_len);
        if (!xi18n_objects_list) return;
    }
    snprintf(filename, sizeof(filename), "%s/%s", path, "XI18N_OBJS");
    fp = fopen(filename, "r");
    if (fp == (FILE *)NULL) {
        return;
    }

    while (fgets(buf, BUFSIZ, fp) != NULL) {
        char *p = buf;
        int n;
        char *args[6];
        while (isspace(*p)) {
            ++p;
        }
        if (iscomment(*p)) {
            continue;
        }

        if (lc_count == lc_len) {
            int new_len = lc_len + OBJECT_INC_LEN;
            XI18NObjectsListRec *tmp = Xrealloc(xi18n_objects_list,
                                                sizeof(XI18NObjectsListRec) * new_len);
            if (tmp == NULL)
                goto done;
            xi18n_objects_list = tmp;
            lc_len = new_len;
        }
        n = parse_line(p, args, 6);

        if (n == 3 || n == 5) {
            if (!strcmp(args[0], "XLC")) {
                xi18n_objects_list[lc_count].type = XLC_OBJECT;
            } else if (!strcmp(args[0], "XOM")) {
                xi18n_objects_list[lc_count].type = XOM_OBJECT;
            } else if (!strcmp(args[0], "XIM")) {
                xi18n_objects_list[lc_count].type = XIM_OBJECT;
            }
            xi18n_objects_list[lc_count].dl_name = strdup(args[1]);
            xi18n_objects_list[lc_count].open = strdup(args[2]);
            xi18n_objects_list[lc_count].dl_release = XI18N_DLREL;
            xi18n_objects_list[lc_count].locale_name = strdup(lc_name);
            xi18n_objects_list[lc_count].refcount = 0;
            xi18n_objects_list[lc_count].dl_module = (void*)NULL;
            if (n == 5) {
                xi18n_objects_list[lc_count].im_register = strdup(args[3]);
                xi18n_objects_list[lc_count].im_unregister = strdup(args[4]);
            } else {
                xi18n_objects_list[lc_count].im_register = NULL;
                xi18n_objects_list[lc_count].im_unregister = NULL;
            }
            lc_count++;
        }
    }
done:
    fclose(fp);
}
Example #21
0
/* build array of pointers to start/end of pages */
void scanpages(off_t *sizeheaders)
{
   register char *comment = buffer+2;
   register int nesting = 0;
   register off_t record;

   if (sizeheaders)
     *sizeheaders = 0;

   if ((pageptr = (off_t *)malloc(sizeof(off_t)*maxpages)) == NULL)
      message(FATAL, "out of memory\n");
   pages = 0;
   fseeko(infile, (off_t) 0, SEEK_SET);
   while (record = ftello(infile), fgets(buffer, BUFSIZ, infile) != NULL)
      if (*buffer == '%') {
	 if (buffer[1] == '%') {
	    if (nesting == 0 && iscomment(comment, "Page:")) {
	       if (pages >= maxpages-1) {
		  maxpages *= 2;
		  if ((pageptr = (off_t *)realloc((char *)pageptr,
					     sizeof(off_t)*maxpages)) == NULL)
		     message(FATAL, "out of memory\n");
	       }
	       pageptr[pages++] = record;
	    } else if (headerpos == 0 && iscomment(comment, "BoundingBox:")) {
	       if (sizeheaders) {
		  *(sizeheaders++) = record;
		  *sizeheaders = 0;
	       }
	    } else if (headerpos == 0 && iscomment(comment, "HiResBoundingBox:")) {
	       if (sizeheaders) {
		  *(sizeheaders++) = record;
		  *sizeheaders = 0;
	       }
	    } else if (headerpos == 0 && iscomment(comment,"DocumentPaperSizes:")) {
	       if (sizeheaders) {
		  *(sizeheaders++) = record;
		  *sizeheaders = 0;
	       }
	    } else if (headerpos == 0 && iscomment(comment,"DocumentMedia:")) {
	       if (sizeheaders) {
		  *(sizeheaders++) = record;
		  *sizeheaders = 0;
	       }
	    } else if (headerpos == 0 && iscomment(comment, "Pages:"))
	       pagescmt = record;
	    else if (headerpos == 0 && iscomment(comment, "EndComments"))
	       headerpos = ftello(infile);
	    else if (iscomment(comment, "BeginDocument") ||
		     iscomment(comment, "BeginBinary") ||
		     iscomment(comment, "BeginFile"))
	       nesting++;
	    else if (iscomment(comment, "EndDocument") ||
		     iscomment(comment, "EndBinary") ||
		     iscomment(comment, "EndFile"))
	       nesting--;
	    else if (nesting == 0 && iscomment(comment, "EndSetup"))
	       endsetup = record;
	    else if (nesting == 0 && iscomment(comment, "BeginProlog"))
	       headerpos = ftello(infile);
	    else if (nesting == 0 &&
		       iscomment(comment, "BeginProcSet: PStoPS"))
	       beginprocset = record;
	    else if (beginprocset && !endprocset &&
		     iscomment(comment, "EndProcSet"))
	       endprocset = ftello(infile);
	    else if (nesting == 0 && (iscomment(comment, "Trailer") ||
				      iscomment(comment, "EOF"))) {
	       fseeko(infile, record, SEEK_SET);
	       break;
	    }
	 } else if (headerpos == 0 && buffer[1] != '!')
	    headerpos = record;
      } else if (headerpos == 0)
	 headerpos = record;
   pageptr[pages] = ftello(infile);
   if (endsetup == 0 || endsetup > pageptr[0])
      endsetup = pageptr[0];
}
Example #22
0
File: cfg.c Project: Tilka/pmacct
void sanitize_cfg(int rows, char *filename)
{
  int rindex = 0, len, got_first;
  char localbuf[10240];

  while (rindex < rows) {
    memset(localbuf, 0, 10240);

    /* checking the whole line: if it's a comment starting with
       '!', it will be removed */
    if (iscomment(cfg[rindex])) memset(cfg[rindex], 0, strlen(cfg[rindex]));

    /* checking the whole line: if it's void, it will be removed */
    if (isblankline(cfg[rindex])) memset(cfg[rindex], 0, strlen(cfg[rindex]));

    /* 
       a pair of syntax checks on the whole line:
       - does the line contain at least a ':' verb ?
       - are the square brackets weighted both in key and value ?
    */
    len = strlen(cfg[rindex]);
    if (len) {
      int symbol = FALSE, cindex = 0, got_first = 0;

      if (!strchr(cfg[rindex], ':')) {
	Log(LOG_ERR, "ERROR ( %s ): Syntax error: missing ':' at line %d. Exiting.\n", filename, rindex+1); 
	exit(1);
      }
      while(cindex <= len) {
        if (cfg[rindex][cindex] == '[') symbol++;
        else if (cfg[rindex][cindex] == ']') {
	  symbol--;
	  got_first++;
	}
	
	if ((cfg[rindex][cindex] == ':') || (cfg[rindex][cindex] == '\0')) {
	  if (symbol && !got_first) {
            Log(LOG_ERR, "ERROR ( %s ): Syntax error: not weighted brackets at line %d. Exiting.\n", filename, rindex+1);
	    exit(1);
	  }
	}

	if (symbol < 0 && !got_first) {
	  Log(LOG_ERR, "ERROR ( %s ): Syntax error: not weighted brackets at line %d. Exiting.\n", filename, rindex+1);
	  exit(1);
	}

	if (symbol > 1 && !got_first) {
	  Log(LOG_ERR, "ERROR ( %s ): Syntax error: nested symbols not allowed at line %d. Exiting.\n", filename, rindex+1);
	  exit(1);
	}
	
	cindex++;
      }
    }

    /* checking the whole line: erasing unwanted spaces from key;
       trimming start/end spaces from value; symbols will be left
       untouched */
    len = strlen(cfg[rindex]);
    if (len) {
      int symbol = FALSE, value = FALSE, cindex = 0, lbindex = 0;
      char *valueptr;

      while(cindex <= len) {
	if (!value) {
          if (cfg[rindex][cindex] == '[') symbol++;
          else if (cfg[rindex][cindex] == ']') symbol--;
	  else if (cfg[rindex][cindex] == ':') {
	    value++;
	    valueptr = &localbuf[lbindex+1];
	  }
	}
        if ((!symbol) && (!value)) {
	  if (!isspace(cfg[rindex][cindex])) {
	    localbuf[lbindex] = cfg[rindex][cindex]; 
	    lbindex++;
	  }
        }
        else {
	  localbuf[lbindex] = cfg[rindex][cindex];
	  lbindex++;
        }
        cindex++;
      }
      localbuf[lbindex] = '\0';
      trim_spaces(valueptr);
      strcpy(cfg[rindex], localbuf);
    }

    /* checking key field: each symbol must refer to a key */
    len = strlen(cfg[rindex]);
    if (len) { 
      int symbol = FALSE, key = FALSE, cindex = 0;

      while (cindex < rows) {
        if (cfg[rindex][cindex] == '[') symbol++;
	else if (cfg[rindex][cindex] == ']') {
	  symbol--;
	  key--;
	}

	if (cfg[rindex][cindex] == ':') break;

	if (!symbol) {
	  if (isalpha(cfg[rindex][cindex])) key = TRUE;
	}
	else {
	  if (!key) {
            Log(LOG_ERR, "ERROR ( %s ): Syntax error: symbol not referring to any key at line %d. Exiting.\n", filename, rindex+1);
	    exit(1);
	  }
	}
        cindex++;
      }
    }


    /* checking key field: does a key still exist ? */
    len = strlen(cfg[rindex]);
    if (len) {
      if (cfg[rindex][0] == ':') {
	Log(LOG_ERR, "ERROR ( %s ): Syntax error: missing key at line %d. Exiting.\n", filename, rindex+1);
	exit(1);
      }
    }

    /* checking key field: converting key to lower chars */ 
    len = strlen(cfg[rindex]);
    if (len) {
      int symbol = FALSE, cindex = 0;

      while(cindex <= len) {
        if (cfg[rindex][cindex] == '[') symbol++;
	else if (cfg[rindex][cindex] == ']') symbol--;

	if (cfg[rindex][cindex] == ':') break;
	if (!symbol) {
	  if (isalpha(cfg[rindex][cindex]))
	    cfg[rindex][cindex] = tolower(cfg[rindex][cindex]);
	}
	cindex++;
      }
    }

    rindex++;
  }
}
Example #23
0
void GetFORTRANBlock( ss_block *ss_new, char *start, int text_col )
{
    int length = 0;

    if( start[0] == '\0' ) {
        if( text_col == 0 ) {
            // line is empty -
            // do not flag following line as having anything to do
            // with an unterminated string from previous line
            flags.inString = false;
        }
        getBeyondText( ss_new );
        return;
    }

    if( iscomment( firstChar ) ) {
        getComment( ss_new, start );
        return;
    }

    if( text_col <= 4 ) {
        getLabelOrWS( ss_new, start, text_col );
        return;
    }

    if( text_col == 5 ) {
        getContinuationOrWS( ss_new, start );
        return;
    }

    if( flags.inString ) {
        getLiteral( ss_new, start, 0 );
        return;
    }

    if( isspace( start[0] ) ) {
        getWhiteSpace( ss_new, start );
        return;
    }

    switch( start[0] ) {

    case '!':
        getComment( ss_new, start );
        return;
    case '\'':
        getLiteral( ss_new, start, 1 );
        return;
    case '.':
        if( isdigit( start[1] ) ) {
            getFloat( ss_new, start, 1, AFTER_DOT );
            return;
        }
        length = islogical( start );
        if( length > 0 ) {
            getSymbol( ss_new, length );
            return;
        }
    }

    if( issymbol( start[0] ) ) {
        getSymbol( ss_new, 1 );
        return;
    }

    if( isdigit( *start ) ) {
        getNumber( ss_new, start );
        return;
    }

    if( isalpha( *start ) || (*start == '_') || (*start == '$') ) {
        getText( ss_new, start );
        return;
    }

    getInvalidChar( ss_new );
}
Example #24
0
static void getLiteral( ss_block *ss_new, char *start, int skip )
{
    char    *text;
    char    lastchar = '\0';
    bool    empty;
    bool    multiLine = flags.inString;
    line    *line;
    fcb     *fcb;
    char    *data;
    vi_rc   rc;

    empty = true;
    for( text = start + skip; *text != '\0'; ++text ) {
        if( text[0] == '\'' ) {
            if( text[1] != '\'' )
                break;
            ++text;
        }
        empty = false;
    }
    flags.inString = false;
    if( *text == '\0' ) {
        // if next line a continuation line, then flag flags.inString, else
        // flag unterminated string
        rc = CGimmeLinePtr( thisLine + 1, &fcb, &line );
        for( ;; ) {
            if( rc != ERR_NO_ERR ) {
                break;
            }
            data = (line->u.ld.nolinedata) ? WorkLine->data : line->data;
            if( !iscomment( data[0] ) ) {
                break;
            }
            rc = CGimmeNextLinePtr( &fcb, &line );
        }
        ss_new->type = SE_INVALIDTEXT;
        if( rc == ERR_NO_ERR && !isInitialLine( line ) ) {
            ss_new->type = SE_STRING;
            flags.inString = true;
        }
    } else {
        text++;
        lastchar = tolower( *text );
        switch( lastchar ) {
        case 'c':
            text++;
            ss_new->type = SE_STRING;
            break;
        case 'x':
            text++;
            ss_new->type = SE_HEX;
            break;
        case 'o':
            text++;
            ss_new->type = SE_OCTAL;
            break;
        default:
            // hard to say if invalid or not - take a guess
            if( islower( *text ) ) {
                text++;
                ss_new->type = SE_INVALIDTEXT;
            } else {
                ss_new->type = SE_STRING;
            }
            break;
        }
    }
    ss_new->len = text - start;
    if( empty && !multiLine ) {
        ss_new->type = SE_INVALIDTEXT;
    }
}
/**
 * try to match the buffer with an instruction (only the first
 * nb_match_token tokens if != 0). Return 0 if we match all the
 * tokens, else the number of matched tokens, else -1.
 */
static int
match_inst(cmdline_parse_inst_t *inst, const char *buf,
	   unsigned int nb_match_token, void * result_buf)
{
	unsigned int token_num=0;
	cmdline_parse_token_hdr_t * token_p;
	unsigned int i=0;
	int n = 0;
	struct cmdline_token_hdr token_hdr;

	token_p = inst->tokens[token_num];
	if (token_p)
		memcpy(&token_hdr, token_p, sizeof(token_hdr));

	/* check if we match all tokens of inst */
	while (token_p && (!nb_match_token || i<nb_match_token)) {
		debug_printf("TK\n");
		/* skip spaces */
		while (isblank2(*buf)) {
			buf++;
		}

		/* end of buf */
		if ( isendofline(*buf) || iscomment(*buf) )
			break;

		if (result_buf)
			n = token_hdr.ops->parse(token_p, buf,
						 (char *)result_buf +
						 token_hdr.offset);
		else
			n = token_hdr.ops->parse(token_p, buf, NULL);

		if (n < 0)
			break;

		debug_printf("TK parsed (len=%d)\n", n);
		i++;
		buf += n;

		token_num ++;
		token_p = inst->tokens[token_num];
		if (token_p)
			memcpy(&token_hdr, token_p, sizeof(token_hdr));
	}

	/* does not match */
	if (i==0)
		return -1;

	/* in case we want to match a specific num of token */
	if (nb_match_token) {
		if (i == nb_match_token) {
			return 0;
		}
		return i;
	}

	/* we don't match all the tokens */
	if (token_p) {
		return i;
	}

	/* are there are some tokens more */
	while (isblank2(*buf)) {
		buf++;
	}

	/* end of buf */
	if ( isendofline(*buf) || iscomment(*buf) )
		return 0;

	/* garbage after inst */
	return i;
}
Example #26
0
void Lexer::finishToken(void)
{
	if (isspace(this->current) || iscomment(this->current))
		this->findNext();
}
Example #27
0
ConditionalReader::ConditionalReader(PClip _child, const char* filename, const char _varname[], bool _show, IScriptEnvironment* env) :
    GenericVideoFilter(_child), show(_show), variableName(_varname)
{
    FILE * f;
    char *line;
    int lines;

    if ((f = fopen(filename, "rb")) == NULL)
        env->ThrowError("ConditionalReader: Could not open file '%s'.", filename);

    lines = 0;
    mode = MODE_UNKNOWN;

    while ((line = readline(f)) != NULL) {
        char *ptr;
        int fields;

        lines++;

        /* We skip spaces */
        ptr = skipspaces(line);

        /* Skip coment lines or empty lines */
        if(iscomment(ptr) || *ptr == '\0') {
            free(line);
            continue;
        }

        if (mode == MODE_UNKNOWN) {
            // We have not recieved a mode - We expect type.
            char keyword [1024];
            char type [1024];
            fields = sscanf(ptr,"%1023s %1023s", keyword, type);
            if (fields) {
                if (!strcasecmp((const char*)keyword, "type")) {
                    if (!strcasecmp((const char*)type, "int")) {
                        mode = MODE_INT;
                        intVal = new int[vi.num_frames];
                    } else if (!strcasecmp((const char*)type, "float")) {
                        mode = MODE_FLOAT;
                        floatVal = new float[vi.num_frames];
                    } else if (!strcasecmp((const char*)type, "bool")) {
                        mode = MODE_BOOL;
                        boolVal = new bool[vi.num_frames];
                    } else {
                        ThrowLine("ConditionalReader: Unknown 'type' specified in line %d", lines, env);
                    }// end if compare type
                }// end if compare keyword
            }// end if fields

        } else { // We have a defined mode and allocated the values.

            char keyword [1024];
            char type [1024];
            fields = sscanf(ptr,"%1023s %1023s", keyword, type);

            if (!strcasecmp((const char*)keyword, "default")) {
                AVSValue def = ConvertType((const char*)type, lines, env);
                SetRange(0, vi.num_frames-1, def);
                free(line);
                continue;
            } // end if "default"

            if (ptr[0] == 'R' || ptr[0] == 'r') {  // Range
                ptr++;
                ptr = skipspaces(ptr);
                int start;
                int stop;
                char value [64];
                fields = sscanf(ptr, "%d %d %63s", &start, &stop, value);

                if (fields != 3)
                    ThrowLine("ConditionalReader: Could not read range in line %d", lines, env);
                if (start > stop)
                    ThrowLine("ConditionalReader: The start frame is after the end frame in line %d", lines, env);

                AVSValue set = ConvertType((const char*)value, lines, env);
                SetRange(start, stop, set);
            } else if (ptr[0] == 'I' || ptr[0] == 'i') {  // Interpolate
                if (mode == MODE_BOOL)
                    ThrowLine("ConditionalReader: Cannot interpolate booleans in line %d", lines, env);

                ptr++;
                ptr = skipspaces(ptr);
                int start;
                int stop;
                char start_value [64];
                char stop_value [64];
                fields = sscanf(ptr, "%d %d %63s %63s", &start, &stop, start_value, stop_value);

                if (fields != 4)
                    ThrowLine("ConditionalReader: Could not read interpolation range in line %d", lines, env);
                if (start > stop)
                    ThrowLine("ConditionalReader: The start frame is after the end frame in line %d", lines, env);

                AVSValue set_start = ConvertType((const char*)start_value, lines, env);
                AVSValue set_stop = ConvertType((const char*)stop_value, lines, env);

                int range = stop-start;
                double diff = set_stop.AsFloat() - set_start.AsFloat();
                for (int i = 0; i<=range; i++) {
                    double where = (double)(i)/(double)range;
                    double n = where * diff + set_start.AsFloat();
                    SetFrame(i+start, (mode == MODE_FLOAT)
                             ? AVSValue(n)
                             : AVSValue((int) n));
                }
            } else {
                char value [64];
                int cframe;
                fields = sscanf(ptr, "%d %63s", &cframe, value);
                if (fields == 2) {
                    AVSValue set = ConvertType((const char*)value, lines, env);
                    SetFrame(cframe, set);
                } else {
                    AVXLOG_INFO("ConditionalReader: Ignored line %d.\n", lines);
                }
            }

        } // End we have defined type
        free(line);
    }// end while still some file left to read.

    /* We are done with the file */
    fclose(f);

    if (mode == MODE_UNKNOWN)
        env->ThrowError("ConditionalReader: Mode was not defined!");

}
Example #28
0
int
cmdline_parse(struct cmdline *cl, const char * buf)
{
	unsigned int inst_num=0;
	cmdline_parse_inst_t *inst;
	const char *curbuf;
	union {
		char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
		long double align; /* strong alignment constraint for buf */
	} result, tmp_result;
	void (*f)(void *, struct cmdline *, void *) = NULL;
	void *data = NULL;
	int comment = 0;
	int linelen = 0;
	int parse_it = 0;
	int err = CMDLINE_PARSE_NOMATCH;
	int tok;
	cmdline_parse_ctx_t *ctx;
#ifdef RTE_LIBRTE_CMDLINE_DEBUG
	char debug_buf[BUFSIZ];
#endif

	if (!cl || !buf)
		return CMDLINE_PARSE_BAD_ARGS;

	ctx = cl->ctx;

	/*
	 * - look if the buffer contains at least one line
	 * - look if line contains only spaces or comments
	 * - count line length
	 */
	curbuf = buf;
	while (! isendofline(*curbuf)) {
		if ( *curbuf == '\0' ) {
			debug_printf("Incomplete buf (len=%d)\n", linelen);
			return 0;
		}
		if ( iscomment(*curbuf) ) {
			comment = 1;
		}
		if ( ! isblank2(*curbuf) && ! comment) {
			parse_it = 1;
		}
		curbuf++;
		linelen++;
	}

	/* skip all endofline chars */
	while (isendofline(buf[linelen])) {
		linelen++;
	}

	/* empty line */
	if ( parse_it == 0 ) {
		debug_printf("Empty line (len=%d)\n", linelen);
		return linelen;
	}

#ifdef RTE_LIBRTE_CMDLINE_DEBUG
	snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
	debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
#endif

	/* parse it !! */
	inst = ctx[inst_num];
	while (inst) {
		debug_printf("INST %d\n", inst_num);

		/* fully parsed */
		tok = match_inst(inst, buf, 0, tmp_result.buf,
				 sizeof(tmp_result.buf));

		if (tok > 0) /* we matched at least one token */
			err = CMDLINE_PARSE_BAD_ARGS;

		else if (!tok) {
			debug_printf("INST fully parsed\n");
			memcpy(&result, &tmp_result,
			       sizeof(result));
			/* skip spaces */
			while (isblank2(*curbuf)) {
				curbuf++;
			}

			/* if end of buf -> there is no garbage after inst */
			if (isendofline(*curbuf) || iscomment(*curbuf)) {
				if (!f) {
					memcpy(&f, &inst->f, sizeof(f));
					memcpy(&data, &inst->data, sizeof(data));
				}
				else {
					/* more than 1 inst matches */
					err = CMDLINE_PARSE_AMBIGUOUS;
					f=NULL;
					debug_printf("Ambiguous cmd\n");
					break;
				}
			}
		}

		inst_num ++;
		inst = ctx[inst_num];
	}

	/* call func */
	if (f) {
		f(result.buf, cl, data);
	}

	/* no match */
	else {
		debug_printf("No match err=%d\n", err);
		return err;
	}

	return linelen;
}
Example #29
0
ConditionalReader::ConditionalReader(PClip _child, const char* filename, const char _varname[], bool _show, IScriptEnvironment* env)
 : GenericVideoFilter(_child), show(_show), variableName(_varname), mode(MODE_UNKNOWN), offset(0), stringcache(0)
{
  FILE * f;
  char *line = 0;
  int lines;

  if ((f = fopen(filename, "rb")) == NULL)
    env->ThrowError("ConditionalReader: Could not open file '%s'.", filename);

  lines = 0;

  try {
    while ((line = readline(f)) != NULL) {
      char *ptr;
      int fields;

      lines++;

      /* We skip spaces */
      ptr = skipspaces(line);

      /* Skip coment lines or empty lines */
      if(iscomment(ptr) || *ptr == '\0') {
        free(line);
        line = 0;
        continue;
      }

      if (mode == MODE_UNKNOWN) {
        // We have not recieved a mode - We expect type.
        char* keyword = ptr;

        ptr = findspace(ptr);
        if (*ptr) {
          *ptr++ = '\0';
          if (!lstrcmpi(keyword, "type")) {
            /* We skip spaces */
            char* type = skipspaces(ptr);

            ptr = findspace(type);
            *ptr = '\0';

            if (!lstrcmpi(type, "int")) {
              mode = MODE_INT;
              intVal = new int[vi.num_frames];
            } else if (!lstrcmpi(type, "float")) {
              mode = MODE_FLOAT;
              floatVal = new float[vi.num_frames];
            } else if (!lstrcmpi(type, "bool")) {
              mode = MODE_BOOL;
              boolVal = new bool[vi.num_frames];
            } else if (!lstrcmpi(type, "string")) {
              mode = MODE_STRING;
              stringVal = new const char*[vi.num_frames];
            } else {
              ThrowLine("ConditionalReader: Unknown 'Type' specified in line %d", lines, env);
            }// end if compare type
            SetRange(0, vi.num_frames-1, AVSValue());
          }// end if compare keyword
        }// end if fields

      } else { // We have a defined mode and allocated the values.

        char* keyword = ptr;
        char* type = findspace(keyword);

        if (*type) *type++ = '\0';

        if (!lstrcmpi(keyword, "default")) {
          AVSValue def = ConvertType(type, lines, env);
          SetRange(0, vi.num_frames-1, def);

        } else if (!lstrcmpi(keyword, "offset")) {
          fields = sscanf(type, "%d", &offset);
          if (fields != 1) 
            ThrowLine("ConditionalReader: Could not read Offset in line %d", lines, env);

        } else if (keyword[0] == 'R' || keyword[0] == 'r') {  // Range
          int start;
          int stop;

          type = skipspaces(type);
          fields = sscanf(type, "%d", &start);

          type = findspace(type);
          type = skipspaces(type);
          fields += sscanf(type, "%d", &stop);

          type = findspace(type);
          if (!*type || fields != 2)
            ThrowLine("ConditionalReader: Could not read Range in line %d", lines, env);

          if (start > stop)
            ThrowLine("ConditionalReader: The Range start frame is after the end frame in line %d", lines, env);

          AVSValue set = ConvertType(type+1, lines, env);
          SetRange(start, stop, set);

        } else if (keyword[0] == 'I' || keyword[0] == 'i') {  // Interpolate
          if (mode == MODE_BOOL)
            ThrowLine("ConditionalReader: Cannot Interpolate booleans in line %d", lines, env);

          if (mode == MODE_STRING)
            ThrowLine("ConditionalReader: Cannot Interpolate strings in line %d", lines, env);

          type = skipspaces(type);
          int start;
          int stop;
          char start_value[64];
          char stop_value[64];
          fields = sscanf(type, "%d %d %63s %63s", &start, &stop, start_value, stop_value);

          if (fields != 4) 
            ThrowLine("ConditionalReader: Could not read Interpolation range in line %d", lines, env);
          if (start > stop)
            ThrowLine("ConditionalReader: The Interpolation start frame is after the end frame in line %d", lines, env);

          start_value[63] = '\0';
          AVSValue set_start = ConvertType(start_value, lines, env);

          stop_value[63] = '\0';
          AVSValue set_stop = ConvertType(stop_value, lines, env);

          const int range = stop-start;
          const double diff = (set_stop.AsFloat() - set_start.AsFloat()) / range;
          for (int i = 0; i<=range; i++) {
            const double n = i * diff + set_start.AsFloat();
            SetFrame(i+start, (mode == MODE_FLOAT)
                    ? AVSValue(n)
                    : AVSValue((int)(n+0.5)));
          }
        } else {
          int cframe;
          fields = sscanf(keyword, "%d", &cframe);
          if (*type && fields == 1) {
            AVSValue set = ConvertType(type, lines, env);
            SetFrame(cframe, set);
          } else {
            ThrowLine("ConditionalReader: Do not understand line %d", lines, env);
          }
        }
      
      } // End we have defined type
      free(line);
      line = 0;
    }// end while still some file left to read.
  }
  catch (...) {
    if (line) free(line);
    fclose(f);
    CleanUp();
    throw;
  }

  /* We are done with the file */
  fclose(f);

  if (mode == MODE_UNKNOWN)
    env->ThrowError("ConditionalReader: Type was not defined!");

}
Example #30
0
/** 
 * try to match the buffer with an instruction (only the first
 * nb_match_token tokens if != 0). Return 0 if we match all the
 * tokens, else the number of matched tokens, else -1.
 */
static int8_t
match_inst(parse_pgm_inst_t *inst, const char * buf, uint8_t nb_match_token, 
	   void * result_buf)
{
	uint8_t token_num=0;
	parse_pgm_token_hdr_t * token_p;
	uint8_t i=0;
	int8_t n = 0;
	struct token_hdr token_hdr;

	token_p = (parse_pgm_token_hdr_t *)pgm_read_word(&inst->tokens[token_num]);
	memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
	
	/* check if we match all tokens of inst */
	while (token_p && (!nb_match_token || i<nb_match_token)) {
		debug_printf("TK\n");
		/* skip spaces */
		while (isblank(*buf)) {
			buf++;
		}
		
		/* end of buf */
		if ( isendofline(*buf) || iscomment(*buf) )
			break;
		
		n = token_hdr.ops->parse(token_p, buf, (result_buf ? result_buf+token_hdr.offset : NULL));
		if ( n < 0 )
			break;
		debug_printf("TK parsed (len=%d)\n", n);
		i++;
		buf += n;
		
		token_num ++;
		token_p = (parse_pgm_token_hdr_t *)pgm_read_word(&inst->tokens[token_num]);
		memcpy_P(&token_hdr, token_p, sizeof(token_hdr));
	}
	
	/* does not match */
	if (i==0)
		return -1;
	
	/* in case we want to match a specific num of token */
	if (nb_match_token) {
		if (i == nb_match_token) {
			return 0;
		}
		return i;
	}

	/* we don't match all the tokens */
	if (token_p) {
		return i;
	}

	/* are there are some tokens more */
	while (isblank(*buf)) {
		buf++;
	}
	
	/* end of buf */
	if ( isendofline(*buf) || iscomment(*buf) )
		return 0;

	/* garbage after inst */
	return i;
}