예제 #1
0
/*
 * snippy-snip
 */
static void cut_file(FILE *file)
{
	char *line = NULL;
	unsigned int linenum = 0; /* keep these zero-based to be consistent */

	/* go through every line in the file */
	while ((line = get_line_from_file(file)) != NULL) {
		chomp(line);

		/* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
		if (part == 'c' || part == 'b')
			cut_line_by_chars(line);

		/* cut based on fields */
		else if (part == 'f') {
			if (delim == '\n')
				cut_file_by_lines(line, linenum);
			else
				cut_line_by_fields(line);
		}

		linenum++;
		free(line);
	}
}
/*
 * Read a control file (or a stanza of a status file) and parse it,
 * filling parsed fields into the package structure
 */
static int control_read(FILE *file, package_t *p)
{
	char *line;

	while ((line = get_line_from_file(file)) != NULL) {
		line[strlen(line)] = 0;

		if (strlen(line) == 0) {
			break;
		} else
			if (strstr(line, "Package: ") == line) {
				p->package = strdup(line + 9);
		} else
			if (strstr(line, "Status: ") == line) {
				p->status = status_parse(line + 8);
		} else
			if (strstr(line, "Depends: ") == line) {
				p->depends = strdup(line + 9);
		} else
			if (strstr(line, "Provides: ") == line) {
				p->provides = strdup(line + 10);
		} else
			if (strstr(line, "Description: ") == line) {
				p->description = strdup(line + 13);
		/* This is specific to the Debian Installer. Ifdef? */
		} else
			if (strstr(line, "installer-menu-item: ") == line) {
				p->installer_menu_item = atoi(line + 21);
		}
		/* TODO: localized descriptions */
	}
	free(line);
	return EXIT_SUCCESS;
}
예제 #3
0
파일: get_line.c 프로젝트: VannTen/42sh
int			getline(t_input *input, const int interactive, const int mode,
					size_t *state)
{
	t_term	*term;
	int		ret;

	term = &get_shell_data()->term;
	ret = 0;
	if (interactive == 1)
	{
		apply_termcaps("ks");
		set_shell_sigmode(e_shell_sigmode_line_editing);
		restore_custom_attr(term);
		if ((ret = wait_for_input(input, mode)) == MALLOC_FAIL)
			return (ret);
		apply_termcaps("ke");
		set_shell_sigmode(e_shell_sigmode_shell);
		restore_initial_attr(term);
	}
	else
	{
		if ((ret = get_line_from_file(input, state)) == MALLOC_FAIL)
			return (MALLOC_FAIL);
	}
	(ret == RETURN) ? ret = 0 : 0;
	return (ret);
}
예제 #4
0
파일: gplib.c 프로젝트: jkeuffer/pari
/* return 0 if no line could be read (EOF). If PROMPT = NULL, expand and
 * color default prompt; otherwise, use PROMPT as-is. */
int
gp_read_line(filtre_t *F, const char *PROMPT)
{
  static const char *DFT_PROMPT = "? ";
  Buffer *b = (Buffer*)F->buf;
  const char *p;
  int res, interactive;
  if (b->len > 100000) fix_buffer(b, 100000);
  interactive = is_interactive();
  if (interactive || pari_logfile || GP_DATA->echo)
  {
    p = PROMPT;
    if (!p) {
      p = F->in_comment? GP_DATA->prompt_comment: GP_DATA->prompt;
      p = gp_format_prompt(p);
    }
  }
  else
    p = DFT_PROMPT;

  if (interactive)
  {
    BLOCK_EH_START
    if (cb_pari_get_line_interactive)
      res = cb_pari_get_line_interactive(p, GP_DATA->prompt_cont, F);
    else {
      pari_puts(p); pari_flush();
      res = get_line_from_file(p, F, pari_infile);
    }
    BLOCK_EH_END
  }
  else
  { /* in case UI fakes non-interactivity, e.g. TeXmacs */
    if (cb_pari_start_output && cb_pari_get_line_interactive)
예제 #5
0
static void	load_regexes_from_file(const char *filename)
{
	char *line;
	FILE *f = xfopen(filename, "r");
	while ((line = get_line_from_file(f)) != NULL) {
		chomp(line);
		add_regex(line);
		free(line);
	}
}
예제 #6
0
static void load_cmd_file(char *filename)
{
	FILE *cmdfile;
	char *line;
	char *nextline;

	cmdfile = xfopen(filename, "r");

	while ((line = get_line_from_file(cmdfile)) != NULL) {
		/* if a line ends with '\' it needs the next line appended to it */
		while (line[strlen(line)-2] == '\\' &&
				(nextline = get_line_from_file(cmdfile)) != NULL) {
			line = xrealloc(line, strlen(line) + strlen(nextline) + 1);
			strcat(line, nextline);
			free(nextline);
		}
		/* eat trailing newline (if any) --if I don't do this, edit commands
		 * (aic) will print an extra newline */
		chomp(line);
		add_cmd_str(line);
		free(line);
	}
}
예제 #7
0
Process create_process_data(const std::string& path)
{
    Process process;

    auto pid = path.substr(path.rfind('/') + 1);
    process.processid = std::atoi(pid.c_str());

    auto cmdline_file = path + "/cmdline";
    auto cmdline = get_line_from_file(cmdline_file);
    cmdline = replace_null(cmdline);

    process.cmdline = cmdline;
    process.wchan = get_line_from_file(path + "/wchan");

    auto uid_line = get_line_starting_with(path + "/status", "Uid:");
    std::stringstream ss {uid_line};
    
    std::string uid;
    ss >> uid >> uid;

    process.userid = std::atoi(uid.c_str());
    return process;
}
예제 #8
0
파일: layout-text.c 프로젝트: 0ida/coreboot
/****************************************************************************
 * get_layout_file_line
 *
 * Get a line of input from file 'f'.  Store result in 'line' which is an
 * array of 'line_buf_size' bytes.  Return OK on success or an error code on
 * failure.
 ****************************************************************************/
static int get_layout_file_line(FILE * f, char line[], int line_buf_size)
{
	switch (get_line_from_file(f, line, line_buf_size)) {
	case OK:
		return OK;

	case LINE_EOF:
		return LINE_EOF;

	case LINE_TOO_LONG:
		fprintf(stderr,
			"%s: Error on line %d of CMOS layout file: Maximum "
			"line length exceeded.  Max is %d characters.\n",
			prog_name, line_num, line_buf_size - 2);
		break;
	}

	exit(1);
	return 1;		/* keep compiler happy */
}
int sort_main(int argc, char **argv)
{
	FILE *fp;
	char *line, **lines = NULL;
	int i, opt, nlines = 0;
	int (*compare)(const void *, const void *) = compare_ascii;
#ifdef BB_FEATURE_SORT_REVERSE
	int reverse = FALSE;
#endif

	while ((opt = getopt(argc, argv, "nrl")) != -1) {
		switch (opt) {
			case 'n':
				compare = compare_numeric;
				break;
#ifdef BB_FEATURE_SORT_REVERSE
			case 'r':
				reverse = TRUE;
				break;
#endif
			/*
			 * FIXME: This hack is used so that comparison will be limitid
			 * to LOG_COMPARE_LEN chars so as to only sort using time stamp
			 * of format:
			 * -->Mmm DD HH:MM:SS<--
			 * -->012345678901234<--
			 */
			#define LOG_COMPARE_LEN 15
			case 'l':
				max_compare_len = LOG_COMPARE_LEN;
				break;
			default:
				show_usage();
		}
	}

	/* read the input */
	for (i = optind; i == optind || i < argc; i++) {
		if (argv[i] == NULL)
			fp = stdin;
		else
			fp = xfopen(argv[i], "r");

		while ((line = get_line_from_file(fp)) != NULL) {
			lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
			lines[nlines++] = line;
		}
	}

	/* sort it */
	qsort(lines, nlines, sizeof(char *), compare);

	/* print it */
#ifdef BB_FEATURE_SORT_REVERSE
	if (reverse)
		for (i = nlines - 1; 0 <= i; i--)
			fputs(lines[i], stdout);
	else
#endif
	for (i = 0; i < nlines; i++)
	{
		fputs(lines[i], stdout);
		free(lines[i]);
	}
	free(lines);
	fclose(fp);
	return EXIT_SUCCESS;
}
예제 #10
0
static void process_file(FILE *file)
{
	char *line = NULL;
	static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
	unsigned int still_in_range = 0;
	int altered;
	int i;

	/* go through every line in the file */
	while ((line = get_line_from_file(file)) != NULL) {

		chomp(line);
		linenum++;
		altered = 0;

		/* for every line, go through all the commands */
		for (i = 0; i < ncmds; i++) {


			/*
			 * entry point into sedding...
			 */
			if (
					/* no range necessary */
					(sed_cmds[i].beg_line == 0 && sed_cmds[i].end_line == 0 &&
					 sed_cmds[i].beg_match == NULL &&
					 sed_cmds[i].end_match == NULL) ||
					/* this line number is the first address we're looking for */
					(sed_cmds[i].beg_line && (sed_cmds[i].beg_line == linenum)) ||
					/* this line matches our first address regex */
					(sed_cmds[i].beg_match && (regexec(sed_cmds[i].beg_match, line, 0, NULL, 0) == 0)) ||
					/* we are currently within the beginning & ending address range */
					still_in_range
			   ) {
				int deleted = 0;

				/*
				 * actual sedding
				 */
				switch (sed_cmds[i].cmd) {

					case 'p':
						puts(line);
						break;

					case 'd':
						altered++;
						deleted = 1;
						break;

					case 's':

						/*
						 * Some special cases for 's' printing to make it compliant with
						 * GNU sed printing behavior (aka "The -n | s///p Matrix"):
						 *
						 *    -n ONLY = never print anything regardless of any successful
						 *    substitution
						 *
						 *    s///p ONLY = always print successful substitutions, even if
						 *    the line is going to be printed anyway (line will be printed
						 *    twice).
						 *
						 *    -n AND s///p = print ONLY a successful substitution ONE TIME;
						 *    no other lines are printed - this is the reason why the 'p'
						 *    flag exists in the first place.
						 */

						/* if the user specified that they didn't want anything printed (i.e., a -n
						 * flag and no 'p' flag after the s///), then there's really no point doing
						 * anything here. */
						if (be_quiet && !sed_cmds[i].sub_p)
							break;

						/* we print the line once, unless we were told to be quiet */
						if (!be_quiet)
							altered |= do_subst_command(&sed_cmds[i], &line);

						/* we also print the line if we were given the 'p' flag
						 * (this is quite possibly the second printing) */
						if (sed_cmds[i].sub_p)
							altered |= do_subst_command(&sed_cmds[i], &line);
						if (altered && (i+1 >= ncmds || sed_cmds[i+1].cmd != 's'))
							puts(line);

						break;

					case 'a':
						puts(line);
						fputs(sed_cmds[i].editline, stdout);
						altered++;
						break;

					case 'i':
						fputs(sed_cmds[i].editline, stdout);
						break;

					case 'c':
						/* single-address case */
						if (sed_cmds[i].end_match == NULL && sed_cmds[i].end_line == 0) {
							fputs(sed_cmds[i].editline, stdout);
						}
						/* multi-address case */
						else {
							/* matching text */
							if (sed_cmds[i].end_match && (regexec(sed_cmds[i].end_match, line, 0, NULL, 0) == 0))
								fputs(sed_cmds[i].editline, stdout);
							/* matching line numbers */
							if (sed_cmds[i].end_line > 0 && sed_cmds[i].end_line == linenum)
								fputs(sed_cmds[i].editline, stdout);
						}
						altered++;

						break;

					case 'r': {
								  FILE *outfile;
								  puts(line);
								  outfile = fopen(sed_cmds[i].filename, "r");
								  if (outfile)
									  print_file(outfile);
								  /* else if we couldn't open the output file,
								   * no biggie, just don't print anything */
								  altered++;
							  }
							  break;
				}

				/*
				 * exit point from sedding...
				 */
				if (
					/* this is a single-address command or... */
					(sed_cmds[i].end_line == 0 && sed_cmds[i].end_match == NULL) || (
						/* we were in the middle of our address range (this
						 * isn't the first time through) and.. */
						(still_in_range == 1) && (
							/* this line number is the last address we're looking for or... */
							(sed_cmds[i].end_line && (sed_cmds[i].end_line == linenum)) ||
							/* this line matches our last address regex */
							(sed_cmds[i].end_match && (regexec(sed_cmds[i].end_match, line, 0, NULL, 0) == 0))
						)
					)
				) {
					/* we're out of our address range */
					still_in_range = 0;
				}

				/* didn't hit the exit? then we're still in the middle of an address range */
				else {
					still_in_range = 1;
				}
				if (deleted)
					break;

			}
		}

		/* we will print the line unless we were told to be quiet or if the
		 * line was altered (via a 'd'elete or 's'ubstitution), in which case
		 * the altered line was already printed */
		if (!be_quiet && !altered)
			puts(line);

		free(line);
	}
}
예제 #11
0
static void grep_file(FILE *file)
{
	char *line = NULL;
	int ret;
	int linenum = 0;
	int nmatches = 0;
	int i;
#ifdef BB_FEATURE_GREP_CONTEXT
	int print_n_lines_after = 0;
	int curpos = 0; /* track where we are in the circular 'before' buffer */
	int idx = 0; /* used for iteration through the circular buffer */
#endif /* BB_FEATURE_GREP_CONTEXT */ 

	while ((line = get_line_from_file(file)) != NULL) {
		chomp(line);
		linenum++;

		for (i = 0; i < nregexes; i++) {
			/*
			 * test for a postitive-assertion match (regexec returns success (0)
			 * and the user did not specify invert search), or a
			 * negative-assertion match (regexec returns failure (REG_NOMATCH)
			 * and the user specified
			 * invert search)
			 */
#if defined(__UC_LIBC__)
			ret = regexec(regexes[i], line);
			if ((ret != 0 && !invert_search) || (ret == 0 && invert_search)) {
#else
			ret = regexec(&regexes[i], line, 0, NULL, 0);
			if ((ret == 0 && !invert_search) || (ret == REG_NOMATCH && invert_search)) {
#endif

				/* if we found a match but were told to be quiet, stop here and
				 * return success */
				if (be_quiet)
					exit(0);

				/* keep track of matches */
				nmatches++;

				/* if we're just printing filenames, we stop after the first match */
				if (print_files_with_matches)
					break;

				/* print the matched line */
				if (print_match_counts == 0) {
#ifdef BB_FEATURE_GREP_CONTEXT
					int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;

					/* if we were told to print 'before' lines and there is at least
					 * one line in the circular buffer, print them */
					if (lines_before && before_buf[prevpos] != NULL) {
						int first_buf_entry_line_num = linenum - lines_before;

						/* advance to the first entry in the circular buffer, and
						 * figure out the line number is of the first line in the
						 * buffer */
						idx = curpos;
						while (before_buf[idx] == NULL) {
							idx = (idx + 1) % lines_before;
							first_buf_entry_line_num++;
						}

						/* now print each line in the buffer, clearing them as we go */
						while (before_buf[idx] != NULL) {
							print_line(before_buf[idx], first_buf_entry_line_num, '-');
							free(before_buf[idx]);
							before_buf[idx] = NULL;
							idx = (idx + 1) % lines_before;
							first_buf_entry_line_num++;
						}
					}

					/* make a note that we need to print 'after' lines */
					print_n_lines_after = lines_after;
#endif /* BB_FEATURE_GREP_CONTEXT */ 
					print_line(line, linenum, ':');
				}
			}
#ifdef BB_FEATURE_GREP_CONTEXT
			else { /* no match */
				/* Add the line to the circular 'before' buffer */
				if(lines_before) {
					if(before_buf[curpos])
						free(before_buf[curpos]);
					before_buf[curpos] = xstrdup(line);
					curpos = (curpos + 1) % lines_before;
				}
			}

			/* if we need to print some context lines after the last match, do so */
			if (print_n_lines_after && (last_line_printed != linenum)) {
				print_line(line, linenum, '-');
				print_n_lines_after--;
			}
#endif /* BB_FEATURE_GREP_CONTEXT */ 
		} /* for */
		free(line);
	}


	/* special-case file post-processing for options where we don't print line
	 * matches, just filenames and possibly match counts */

	/* grep -c: print [filename:]count, even if count is zero */
	if (print_match_counts) {
		if (print_filename)
			printf("%s:", cur_file);
		if (print_files_with_matches && nmatches > 0)
			printf("1\n");
		else
		    printf("%d\n", nmatches);
	}

	/* grep -l: print just the filename, but only if we grepped the line in the file  */
	if (print_files_with_matches && nmatches > 0) {
		puts(cur_file);
	}


	/* remember if we matched */
	if (nmatches != 0)
		matched = 1;
}


static void add_regex(const char *restr)
{
	int reflags;

#if defined(__UC_LIBC__)
	reflags = 0;
	if (ignore_case)
		reflags = 1;
#else
	reflags = REG_NOSUB; 
	if (ignore_case)
		reflags |= REG_ICASE;
#endif

#if defined(__UC_LIBC__)
	regexes = xrealloc(regexes, sizeof(regexp *) * (++nregexes));
#else
	regexes = xrealloc(regexes, sizeof(regex_t) * (++nregexes));
#endif
	xregcomp(&regexes[nregexes-1], restr, reflags);
}
static int status_merge(void *status, package_t *pkgs)
{
	FILE *fin, *fout;
	char *line;
	package_t *pkg = 0, *statpkg = 0;
	package_t locpkg;
	int r = 0;

	if ((fin = fopen(statusfile, "r")) == NULL) {
		perror(statusfile);
		return 0;
	}
	if ((fout = fopen(new_statusfile, "w")) == NULL) {
		perror(new_statusfile);
		return 0;
	}
	if (getenv(udpkg_quiet) == NULL) {
		printf("(Updating database...)\n");
	}

	while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { 
		line[strlen(line)] = 0; /* trim newline */
		/* If we see a package header, find out if it's a package
		 * that we have processed. if so, we skip that block for
		 * now (write it at the end).
		 *
		 * we also look at packages in the status cache and update
		 * their status fields
		 */
		if (strstr(line, "Package: ") == line) {
			for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
					pkg->package, strlen(line) - 9) != 0;
			     pkg = pkg->next) ;

			locpkg.package = line + 9;
			statpkg = tfind(&locpkg, &status, package_compare);
			
			/* note: statpkg should be non-zero, unless the status
			 * file was changed while we are processing (no locking
			 * is currently done...
			 */
			if (statpkg != 0) {
				statpkg = *(package_t **)statpkg;
			}
		}
		if (pkg != 0) {
			continue;
		}
		if (strstr(line, "Status: ") == line && statpkg != 0) {
			snprintf(line, sizeof(line), "Status: %s",
				status_print(statpkg->status));
		}
		fputs(line, fout);
		fputc('\n', fout);
	}
	free(line);

	// Print out packages we processed.
	for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
		fprintf(fout, "Package: %s\nStatus: %s\n", 
			pkg->package, status_print(pkg->status));
		if (pkg->depends)
			fprintf(fout, "Depends: %s\n", pkg->depends);
		if (pkg->provides)
			fprintf(fout, "Provides: %s\n", pkg->provides);
		if (pkg->installer_menu_item)
			fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
		if (pkg->description)
			fprintf(fout, "Description: %s\n", pkg->description);
		fputc('\n', fout);
	}
	
	fclose(fin);
	fclose(fout);

	r = rename(statusfile, bak_statusfile);
	if (r == 0) {
		r = rename(new_statusfile, statusfile);
	}

	return 0;
}
예제 #13
0
파일: gplib.c 프로젝트: jkeuffer/pari
void
gp_initrc(pari_stack *p_A)
{
  FILE *file = gprc_get();
  Buffer *b;
  filtre_t F;
  VOLATILE long c = 0;
  jmp_buf *env;
  pari_stack s_env;

  if (!file) return;
  b = filtered_buffer(&F);
  pari_stack_init(&s_env, sizeof(*env), (void**)&env);
  (void)pari_stack_new(&s_env);
  for(;;)
  {
    char *nexts, *s, *t;
    if (setjmp(env[s_env.n-1])) err_printf("...skipping line %ld.\n", c);
    c++;
    if (!get_line_from_file(NULL,&F,file)) break;
    s = b->buf;
    if (*s == '#')
    { /* preprocessor directive */
      int z, NOT = 0;
      s++;
      if (strncmp(s,"if",2)) err_gprc("unknown directive",s,b->buf);
      s += 2;
      if (!strncmp(s,"not",3)) { NOT = !NOT; s += 3; }
      if (*s == '!')           { NOT = !NOT; s++; }
      t = s;
      z = get_preproc_value(&s);
      if (z < 0) err_gprc("unknown preprocessor variable",t,b->buf);
      if (NOT) z = !z;
      if (!*s)
      { /* make sure at least an expr follows the directive */
        if (!get_line_from_file(NULL,&F,file)) break;
        s = b->buf;
      }
      if (!z) continue; /* dump current line */
    }
    /* parse line */
    for ( ; *s; s = nexts)
    {
      nexts = next_expr(s);
      if (!strncmp(s,"read",4) && (s[4] == ' ' || s[4] == '\t' || s[4] == '"'))
      { /* read file */
        s += 4;
        t = (char*)pari_malloc(strlen(s) + 1);
        if (*s == '"') (void)pari_translate_string(s, t, s-4); else strcpy(t,s);
        pari_stack_pushp(p_A,t);
      }
      else
      { /* set default */
        parse_key_val(s, &s,&t);
        (void)setdefault(s,t,d_INITRC);
      }
    }
  }
  pari_stack_delete(&s_env);
  pop_buffer();
  if (!(GP_DATA->flags & gpd_QUIET)) err_printf("Done.\n\n");
  fclose(file);
}