示例#1
0
void
namelist_to_bitarray (bool_t quietmode, bool_t do_warning, const char *finp_name, const struct DATA *d, bitarray_t *pba)
{
	FILE *finp;
	char myline[MAXSIZE_CSVLINE];
	size_t linenumber = 0;
	bool_t line_success = TRUE;
	bool_t file_success = TRUE;

	assert (pba);
	assert (d);

	ba_clear (pba);

	if (NULL == finp_name) {
		return;
	}

	if (NULL != (finp = fopen (finp_name, "r"))) {

		csv_line_t csvln;
		line_success = TRUE;

		while ( line_success && NULL != fgets(myline, MAXSIZE_CSVLINE, finp)) {

			linenumber++;
			if (isblankline(myline)) continue;

			if (TRUE == (line_success = csv_line_init(&csvln, myline))) {
				if (!(csvln.n == 1 && do_tick (d, csvln.s[0], pba))) {
					warning(do_warning, finp_name, myline, linenumber);
				}
				csv_line_done(&csvln);		
			}
		}

		fclose(finp);
	} else {
		file_success = FALSE;
	}

	if (!file_success) {
		fprintf (stderr, "Errors in file \"%s\"\n",finp_name);
		exit(EXIT_FAILURE);
	} else 
	if (!line_success) {
		fprintf (stderr, "Errors in file \"%s\", line %ld (line parsing problem or lack of memory)\n",finp_name, (long)linenumber);
		exit(EXIT_FAILURE);
	} 
	if (!quietmode)	printf ("Names uploaded succesfully\n");

	return;
}
示例#2
0
static int nextline(char* line, int n, FILE* fp)
{
    char* s;

    do {
        s = fgets(line, n, fp);
    } while (s && (iscommentline(line) || isblankline(line)));

    if (s == NULL)
        return 0;

    return 1;
}
示例#3
0
文件: cfg.c 项目: 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++;
  }
}
示例#4
0
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
int process_script_file(char *filename, struct subcmd **cmdsp, int *numscp)
{
  FILE *stream;
  char *mem, *lineend, *p;
  int  curlen;

  if ( (stream = fopen(filename, "r")) == NULL)           /* @8c */
    {
       fprintf(stderr,"sed: can't open script file %s\n", filename);
       return(BAIL_OUT);
    }
  if (fgets(readarea, MAXLINE, stream) == NULL)   /* read first line */
    {
       fprintf(stderr,"sed: unable to read script file %s\n", filename);
       return(BAIL_OUT);
    }

  if (readarea[0] == COMMENT_MARK)  /* The first line can be a comment line */
    {
      if (readarea[1] == 'n')
        suppress_output = YES;

      if (fgets(readarea, MAXLINE, stream) == NULL)   /* replace first line */
        {
          if (feof(stream))
            fprintf(stderr,
                    "sed: script file %s must contain one non-comment line\n",
                   filename);
          else
            myerror(-1, "read error", filename);
          return(BAIL_OUT);
        }
     }

  do
    {
      if (isblankline(readarea))  /* if this line is blank */
        continue;               /* go get another line */
      curlen = strlen(readarea) + 1;
      if ( (mem = (char *)malloc(curlen)) == NULL)
        {
          myerror(ERROR_NOT_ENOUGH_MEMORY, "malloc line", "process_script_file");
          return(BAIL_OUT);
        }
      strcpy(mem, readarea);
      lineend = mem;
      do
      {
        for (p = lineend; *p && *p != '\n'; p++);
        if (*p == '\n' && *--p == BACKSLASH)
          {
            if (fgets(readarea, 512, stream) == NULL)   /* replace first line */
              {
                if (feof(stream))
                  fprintf(stderr,"sed: script file %s ends badly\n",
                         filename);
                else
                  myerror(-1, "read error", filename);
                return(BAIL_OUT);
              }
            curlen = curlen + strlen(readarea) + 1;
            if ( (mem = (char *)realloc(mem, curlen)) == NULL)
              {
                 myerror(ERROR_NOT_ENOUGH_MEMORY, "realloc", filename);
                 return(BAIL_OUT);
              }
            strcat(mem, readarea);
            lineend = p + 2;
          }
      } while(*p == BACKSLASH);
      if (add_subcmd(mem, cmdsp, numscp) == BAIL_OUT)
        return(BAIL_OUT);
      else
        *numscp += 1;
      free(mem);
    } while(fgets(readarea, 512, stream));
    if (ferror(stream))
      {
         myerror(-1, "read error", filename);
         return(BAIL_OUT);
      }
    fclose(stream);
}