Exemplo n.º 1
0
int	move_cursor(int fildes, int num_up, int num_left)
{
	int	ret;

	if (num_up < 0)
	{
		ret = write_loop (fildes, (unsigned char *)CURSOR_DOWN, -num_up);
		if (0 > ret)
			return -1;
	} else if (num_up > 0)
	{
		ret = write_loop (fildes, (unsigned char *)CURSOR_UP, num_up);
		if (0 > ret)
			return -1;
	}

	if (num_left < 0)
	{
		ret = write_loop(fildes, (unsigned char *)CURSOR_RIGHT, -num_left);
		if (0 > ret)
			return -1;
	} else if (num_left > 0)
	{
		ret = write_loop(fildes, (unsigned char *)CURSOR_LEFT, num_left);
		if (0 > ret)
			return -1;
	}
	return 0;
}
Exemplo n.º 2
0
Arquivo: html.c Projeto: aabc/blists
int html_error_real(const char *file, unsigned int lineno, const char *msg)
{
	char loc[128];
	char *msgt;
	const char *prefix = "The request has failed: ";

	if (!msg)
		msg = "Internal server error";

	const char *p = getenv("SERVER_PROTOCOL");
	if (!p || strcmp(p, "INCLUDED"))
		msgt = concat("Status: 404 Not Found\nContent-Type: text/plain\n\n", prefix, msg, "\n", NULL);
	else if (html_flags & HTML_HEADER)
		msgt = concat("\n<title>", prefix, msg, "</title>\n"
		    "<meta name=\"robots\" content=\"noindex\">\n", NULL);
	else
		msgt = concat("\n<p>", prefix, msg, "\n", footer, NULL);

	write_loop(STDOUT_FILENO, msgt, strlen(msgt));
	free(msgt);

	snprintf(loc, sizeof(loc), " (%s:%u)", file, lineno);
	msgt = concat("The request has failed: ", msg, loc, "\n", NULL);
	write_loop(STDERR_FILENO, msgt, strlen(msgt));
	free(msgt);

	return 1;
}
Exemplo n.º 3
0
int 	move_cursor_right(int col, int num_cols)
{
	/* -------------------------------------------------------
	 *	moves cursor right by num_cols columns, if col is rightmost,
	 *	then it goes to the start of the next line
	 *	returns 0 if success, != 0 if error
	 * -------------------------------------------------------
	 */
	int	fildes = ((d_tt_struct *)((io_curr_device.in)->dev_sp))->fildes;
	int	ret;
	io_desc *io_ptr = io_curr_device.in;

	if (0 == num_cols)
		ret = 0;
	else if (0 > num_cols)
		ret = move_cursor_left(col, -num_cols);
	else if ((io_curr_device.in->width - num_cols) > col)
		ret = write_loop(fildes, (unsigned char *)CURSOR_RIGHT, num_cols);
	else
	{
		DOWRITERC(fildes, NATIVE_TTEOL, strlen(NATIVE_TTEOL), ret);
		if (0 > ret)
			return -1;
		num_cols -= (io_curr_device.in->width - col);
		if (num_cols)
			ret = write_loop(fildes, (unsigned char *)CURSOR_RIGHT, num_cols);
	}
	return ret;
}
Exemplo n.º 4
0
int 	move_cursor_left(int col, int num_cols)
{
	/* -------------------------------------------------------
	 *  moves cursor left by num_cols columns.  if col is leftmost,
	 *  then it goes back to the end of the previous line
	 *  returns 0 if success, != 0 if error
	 * -------------------------------------------------------
	 */
	int	fildes = ((d_tt_struct *)((io_curr_device.in)->dev_sp))->fildes;
	int	ret;

	if (0 == num_cols)
		ret = 0;
	else if (0 > num_cols)
		ret = move_cursor_right(col, -num_cols);
	else if (0 < col)
	{
		ret = write_loop(fildes, (unsigned char *)CURSOR_LEFT, MIN(col, num_cols));
		num_cols -= MIN(col, num_cols);
		if (num_cols)
		{
			DOWRITERC(fildes, CURSOR_UP, strlen(CURSOR_UP), ret);
			if (0 > ret)
				return -1;
			ret = write_loop(fildes, (unsigned char *)CURSOR_RIGHT, io_curr_device.in->width - num_cols);
		}
	} else
	{
		DOWRITERC(fildes, CURSOR_UP, strlen(CURSOR_UP), ret);
		if (0 > ret)
			return -1;
		ret = write_loop(fildes, (unsigned char *)CURSOR_RIGHT, io_curr_device.in->width - num_cols);
	}
	return ret;
}
Exemplo n.º 5
0
static bool test_stuff(screen_t *scr)
{
    data_buffer_t output;
    scoped_buffer_t scoped_buffer(&output);

    s_move(scr, &output, 0, 0);
    int screen_width = common_get_width();

    const wchar_t *left = L"left";
    const wchar_t *right = L"right";

    for (size_t idx = 0; idx < 80; idx++)
    {
        output.push_back('A');
    }

    if (! output.empty())
    {
        write_loop(STDOUT_FILENO, &output.at(0), output.size());
        output.clear();
    }

    sleep(5);

    for (size_t i=0; i < 1; i++)
    {
        writembs(cursor_left);
    }

    if (! output.empty())
    {
        write_loop(1, &output.at(0), output.size());
        output.clear();
    }



    while (1)
    {
        int c = getchar();
        if (c != EOF) break;
    }


    while (1)
    {
        int c = getchar();
        if (c != EOF) break;
    }
    puts("Bye");
    exit(0);
    while (1) sleep(10000);
    return true;
}
Exemplo n.º 6
0
static void
write_statements(tree *statements)
{
  tree *list;
  tree *statement;

  list = statements;
  assert(list->tag == node_list);
  while(list) {
    statement = HEAD(list);
    switch(statement->tag) {
    case node_call:
      write_call(statement);
      break;
    case node_cond:
      write_cond(statement);  
      break;
    case node_loop:
      write_loop(statement);
      break; 
    default:
      write_expression(statement);
    }
    list = TAIL(list);
  }

}
Exemplo n.º 7
0
static void s_check_status( screen_t *s)
{
	fflush( stdout );
	fflush( stderr );

	fstat( 1, &s->post_buff_1 );
	fstat( 2, &s->post_buff_2 );

	int changed = ( s->prev_buff_1.st_mtime != s->post_buff_1.st_mtime ) ||
		( s->prev_buff_2.st_mtime != s->post_buff_2.st_mtime );

	if (room_for_usec( &s->post_buff_1))
	{
		changed = changed || ( (&s->prev_buff_1.st_mtime)[1] != (&s->post_buff_1.st_mtime)[1] ) ||
			( (&s->prev_buff_2.st_mtime)[1] != (&s->post_buff_2.st_mtime)[1] );
	}

	if( changed )
	{
		/*
		  Ok, someone has been messing with our screen. We will want
		  to repaint. However, we do not know where the cursor is. It
		  is our best bet that we are still on the same line, so we
		  move to the beginning of the line, reset the modelled screen
		  contents, and then set the modeled cursor y-pos to its
		  earlier value.
		*/

		int prev_line = s->actual_cursor[1];
		write_loop( 1, "\r", 1 );
		s_reset( s, 0 );
		s->actual_cursor[1] = prev_line;
	}
}
Exemplo n.º 8
0
/**
   Load or save all variables
*/
static bool load_or_save_variables_at_path(bool save, const std::string &path)
{
    bool result = false;

    debug(4, L"Open file for %s: '%s'",
          save?"saving":"loading",
          path.c_str());

    /* OK to not use CLO_EXEC here because fishd is single threaded */
    int fd = open(path.c_str(), save?(O_CREAT | O_TRUNC | O_WRONLY):O_RDONLY, 0600);
    if (fd >= 0)
    {
        /* Success */
        result = true;
        connection_t c(fd);

        if (save)
        {
            /* Save to the file */
            write_loop(c.fd, SAVE_MSG, strlen(SAVE_MSG));
            enqueue_all(&c);
        }
        else
        {
            /* Read from the file */
            read_message(&c);
        }

        connection_destroy(&c);
    }
    return result;
}
Exemplo n.º 9
0
/// Stat stdout and stderr and compare result to previous result in reader_save_status. Repaint if
/// modification time has changed.
///
/// Unfortunately, for some reason this call seems to give a lot of false positives, at least under
/// Linux.
static void s_check_status(screen_t *s) {
    fflush(stdout);
    fflush(stderr);

    fstat(1, &s->post_buff_1);
    fstat(2, &s->post_buff_2);

    int changed = (s->prev_buff_1.st_mtime != s->post_buff_1.st_mtime) ||
                  (s->prev_buff_2.st_mtime != s->post_buff_2.st_mtime);

#if defined HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
    changed = changed ||
              s->prev_buff_1.st_mtimespec.tv_nsec != s->post_buff_1.st_mtimespec.tv_nsec ||
              s->prev_buff_2.st_mtimespec.tv_nsec != s->post_buff_2.st_mtimespec.tv_nsec;
#elif defined HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
    changed = changed || s->prev_buff_1.st_mtim.tv_nsec != s->post_buff_1.st_mtim.tv_nsec ||
              s->prev_buff_2.st_mtim.tv_nsec != s->post_buff_2.st_mtim.tv_nsec;
#endif

    if (changed) {
        // Ok, someone has been messing with our screen. We will want to repaint. However, we do not
        // know where the cursor is. It is our best bet that we are still on the same line, so we
        // move to the beginning of the line, reset the modelled screen contents, and then set the
        // modeled cursor y-pos to its earlier value.
        int prev_line = s->actual.cursor.y;
        write_loop(STDOUT_FILENO, "\r", 1);
        s_reset(s, screen_reset_current_line_and_prompt);
        s->actual.cursor.y = prev_line;
    }
}
Exemplo n.º 10
0
void iothread_perform_on_main(void_function_t &&func) {
    if (is_main_thread()) {
        func();
        return;
    }

    // Make a new request. Note we are synchronous, so this can be stack allocated!
    main_thread_request_t req(std::move(func));

    // Append it. Do not delete the nested scope as it is crucial to the proper functioning of this
    // code by virtue of the lock management.
    {
        scoped_lock queue_lock(s_main_thread_request_q_lock);
        s_main_thread_request_queue.push(&req);
    }

    // Tell the pipe.
    const char wakeup_byte = IO_SERVICE_MAIN_THREAD_REQUEST_QUEUE;
    assert_with_errno(write_loop(s_write_pipe, &wakeup_byte, sizeof wakeup_byte) != -1);

    // Wait on the condition, until we're done.
    std::unique_lock<std::mutex> perform_lock(s_main_thread_performer_lock);
    while (!req.done) {
        // It would be nice to support checking for cancellation here, but the clients need a
        // deterministic way to clean up to avoid leaks
        s_main_thread_performer_cond.wait(perform_lock);
    }

    // Ok, the request must now be done.
    assert(req.done);
}
Exemplo n.º 11
0
/**
   Flush \c pager_buffer to stdout
*/
static void pager_flush()
{
    if (! pager_buffer.empty()) {
        write_loop( 1, & pager_buffer.at(0), pager_buffer.size() * sizeof(char) );
        pager_buffer.clear();
    }
}
Exemplo n.º 12
0
/// The function that does thread work.
static void *iothread_worker(void *unused) {
    UNUSED(unused);
    struct spawn_request_t req;
    while (dequeue_spawn_request(&req)) {
        debug(5, "pthread %p dequeued", this_thread());

        // Perform the work
        req.handler();

        // If there's a completion handler, we have to enqueue it on the result queue.
        // Note we're using std::function's weirdo operator== here
        if (req.completion != nullptr) {
            // Enqueue the result, and tell the main thread about it.
            enqueue_thread_result(std::move(req));
            const char wakeup_byte = IO_SERVICE_RESULT_QUEUE;
            assert_with_errno(write_loop(s_write_pipe, &wakeup_byte, sizeof wakeup_byte) != -1);
        }
    }

    // We believe we have exhausted the thread request queue. We want to decrement
    // thread_count and exit. But it's possible that a request just came in. Furthermore,
    // it's possible that the main thread saw that thread_count is full, and decided to not
    // spawn a new thread, trusting in one of the existing threads to handle it. But we've already
    // committed to not handling anything else. Therefore, we have to decrement
    // the thread count under the lock, which we still hold. Likewise, the main thread must
    // check the value under the lock.
    int new_thread_count = --s_spawn_requests.acquire().value.thread_count;
    assert(new_thread_count >= 0);

    debug(5, "pthread %p exiting", this_thread());
    // We're done.
    return NULL;
}
Exemplo n.º 13
0
void my_list(int data_conn_sock ,char *filepath)
{
	char mymode[11]= "----------";
	struct stat buf;
	struct dirent *p = NULL;
	struct passwd *passwd;	
	char uname[50];
	char gname[50];
	char mtime[50];
	DIR *mydir =  NULL;
	char buffer[100];

	chdir(filepath);
	mydir = opendir(filepath);

	while( (p = readdir(mydir)) != NULL )
	{		
		memset( &buf, 0, sizeof(buf));
		stat(p->d_name,&buf);
		
		getFileMode(buf.st_mode, mymode);
		getUserName(buf.st_uid, uname);
		getGroupName(buf.st_gid, gname);
		getTime(buf.st_mtime, mtime);

		sprintf(buffer,"%s %d %s %s %10d %s %s\r\n", mymode, buf.st_nlink, uname, gname, buf.st_size, mtime, p->d_name);
		if (data_conn_sock != 0)
		{			
			write_loop(data_conn_sock, buffer, strlen(buffer));
		}
	}

	closedir(mydir);
}
Exemplo n.º 14
0
/// Called in a forked child.
static void exec_write_and_exit(int fd, const char *buff, size_t count, int status) {
    if (write_loop(fd, buff, count) == -1) {
        debug(0, WRITE_ERROR);
        wperror(L"write");
        exit_without_destructors(status);
    }
    exit_without_destructors(status);
}
Exemplo n.º 15
0
void print_help(const char *c, int fd) {
    char cmd[CMD_LEN];
    int printed = snprintf(cmd, CMD_LEN, "fish -c '__fish_print_help %s >&%d'", c, fd);

    if (printed < CMD_LEN && system(cmd) == -1) {
        write_loop(2, HELP_ERR, strlen(HELP_ERR));
    }
}
Exemplo n.º 16
0
/* Writes our state to the fd. path is provided only for error reporting */
bool env_universal_t::write_to_fd(int fd, const wcstring &path)
{
    ASSERT_IS_LOCKED(lock);
    assert(fd >= 0);
    bool success = true;

    // Stuff we output to fd
    std::string contents;
    
    // Temporary storage
    std::string storage;
    
    // Write the save message. If this fails, we don't bother complaining.
    write_loop(fd, SAVE_MSG, strlen(SAVE_MSG));
    
    var_table_t::const_iterator iter = vars.begin();
    while (iter != vars.end())
    {
        // Append the entry. Note that append_file_entry may fail, but that only affects one variable; soldier on.
        const wcstring &key = iter->first;
        const var_entry_t &entry = iter->second;
        append_file_entry(entry.exportv ? SET_EXPORT : SET, key, entry.val, &contents, &storage);
        
        // Go to next
        ++iter;
        
        // Flush if this is the last iteration or we exceed a page
        if (iter == vars.end() || contents.size() >= 4096)
        {
            if (write_loop(fd, contents.data(), contents.size()) < 0)
            {
                int err = errno;
                report_error(err, L"Unable to write to universal variables file '%ls'", path.c_str());
                success = false;
                break;
            }
            contents.clear();
        }
    }
    
    /* Since we just wrote out this file, it matches our internal state; pretend we read from it */
    this->last_read_file = file_id_for_fd(fd);
    
    /* We don't close the file */
    return success;
}
Exemplo n.º 17
0
int main(int argc, char *argv[])
{
	snd_pcm_t *handle;
	snd_pcm_t *handle_play;
	int err, morehelp;
	snd_pcm_hw_params_t *hwparams;
	snd_pcm_sw_params_t *swparams;
	int method = 0;
	signed short *samples;
	unsigned int chn;
	snd_pcm_hw_params_alloca(&hwparams);
	snd_pcm_sw_params_alloca(&swparams);
	printf("Capture device is %s\n", device);
	printf("Stream parameters are %iHz, %s, %i channels\n", rate, snd_pcm_format_name(format), channels);

	/*open the playback*/
	if ((err = snd_pcm_open(&handle_play, device,SND_PCM_STREAM_PLAYBACK, 0)) < 0)
	{
		printf("Capture open error: %s\n", snd_strerror(err));
		return 0;
	}
	/* set the hard ware parameter*/
	if ((err = set_hwparams(handle_play,hwparams,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
	{
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
		exit(EXIT_FAILURE);
	}

	/*open the capture*/
	if ((err = snd_pcm_open(&handle, device,SND_PCM_STREAM_CAPTURE, 0)) < 0)
	{
		printf("Capture open error: %s\n", snd_strerror(err));
		return 0;
	}
	/* set the hard ware parameter*/
	if ((err = set_hwparams(handle, hwparams,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
	{
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
		exit(EXIT_FAILURE);
	}

	static struct timeval oldtv;
	static struct timeval tv;
	/*avasounil = snd_pcm_avail_update(handle);*/
		 gettimeofday(&tv,NULL);
  		// printf("play back time %lu\n",(tv.tv_sec-oldtv.tv_sec)*1000000+tv.tv_usec-oldtv.tv_usec);
		 printf("main time %u: %u \n",tv.tv_sec,tv.tv_usec);
		 oldtv = tv;
	/*async for capture */
	async_loop(handle);
	/*	*/
	write_loop(handle_play);

	/*while(1)
	{

	}*/
}
Exemplo n.º 18
0
int do_pop_auth(int channel)
{
	pop_init();

	if (pop_reply_ok()) return 1;

	pop_user = NULL;
	if (pop_handle_state(pop_auth_commands) == POP_STATE) {
		pop_clean();
		write_loop(channel, (char *)&pop_buffer, sizeof(pop_buffer));
		write_loop(channel, pop_user, strlen(pop_user) + 1);
		write_loop(channel, pop_pass, strlen(pop_pass) + 1);
		memset(pop_pass, 0, strlen(pop_pass));
		if (close(channel)) return 1;
	}

	return 0;
}
Exemplo n.º 19
0
/// Perform output from builtins. May be called from a forked child, so don't do anything that may
/// allocate memory, etc.
bool do_builtin_io(const char *out, size_t outlen, const char *err, size_t errlen) {
    bool success = true;
    if (out && outlen) {
        if (write_loop(STDOUT_FILENO, out, outlen) < 0) {
            int e = errno;
            debug_safe(0, "Error while writing to stdout");
            safe_perror("write_loop");
            success = false;
            errno = e;
        }
    }

    if (err && errlen) {
        if (write_loop(STDERR_FILENO, err, errlen) < 0) {
            success = false;
        }
    }
    return success;
}
Exemplo n.º 20
0
void check_abort(int be_async_signal_safe)
{
	if (!event_abort) return;

	tty_done();

	if (be_async_signal_safe) {
		if (john_main_process) {
			if (aborted_by_timer)
				write_loop(2, "Session stopped (max run-time"
				           " reached)\n", 39);
			else
				write_loop(2, "Session aborted\n", 16);
		}
		_exit(1);
	}

	if (john_main_process)
		fprintf(stderr, "Session %s\n", (aborted_by_timer) ?
		        "stopped (max run-time reached)" : "aborted");
	error();
}
Exemplo n.º 21
0
void check_abort(int be_async_signal_safe)
{
	if (!event_abort) return;

	if (be_async_signal_safe) {
		write_loop(2, "Session aborted\n", 16);
		MPI_Finalize();
		_exit(1);
	}

	fprintf(stderr, "Session aborted\n");
	error();
}
static void send_one_byte ( const uint8_t data )
{
  assert( connection_socket != -1 );

  try
  {
    write_loop( connection_socket, &data, sizeof(data) );
  }
  catch ( const std::exception & e )
  {
    throw std::runtime_error( format_msg( "Error writing to the JTAG socket: %s", e.what() ) );
  }
}
Exemplo n.º 23
0
int main(int argc, char *argv[])
{
	snd_pcm_t *handle;
	snd_pcm_t *handle_play;
	int err, morehelp;
	snd_pcm_hw_params_t *hwparams;
	snd_pcm_sw_params_t *swparams;
	int method = 0;
	signed short *samples;
	unsigned int chn;
	snd_pcm_hw_params_alloca(&hwparams);
	snd_pcm_sw_params_alloca(&swparams);
	
	printf("Capture device is %s\n", device);
	printf("Stream parameters are %iHz, %s, %i channels\n", rate, snd_pcm_format_name(format), channels);

	/*open the playback*/
	if ((err = snd_pcm_open(&handle_play, device,SND_PCM_STREAM_PLAYBACK, 0)) < 0) 
	{
		printf("Capture open error: %s\n", snd_strerror(err));
		return 0;
	}
	/* set the hard ware parameter*/
	if ((err = set_hwparams(handle_play,hwparams,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) 
	{
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
		exit(EXIT_FAILURE);
	}

	/*open the capture*/
	if ((err = snd_pcm_open(&handle, device,SND_PCM_STREAM_CAPTURE, 0)) < 0) 
	{
		printf("Capture open error: %s\n", snd_strerror(err));
		return 0;
	}
	/* set the hard ware parameter*/
	if ((err = set_hwparams(handle, hwparams,SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) 
	{
		printf("Setting of hwparams failed: %s\n", snd_strerror(err));
		exit(EXIT_FAILURE);
	}	
	/*async for capture */
	async_loop(handle);	
	/*	*/
	write_loop(handle_play);
	
	/*while(1)
	{
		
	}*/
}
Exemplo n.º 24
0
static void sig_handle_abort(int signum)
{
	int saved_errno = errno;

	check_abort(1);

	event_abort = event_pending = 1;

	write_loop(2, "Wait...\r", 8);

	sig_install_abort();

	errno = saved_errno;
}
Exemplo n.º 25
0
Arquivo: html.c Projeto: aabc/blists
static int html_send(struct buffer *dst)
{
	if (html_flags & HTML_BODY)
		buffer_appends(dst, footer);

	if (dst->error) {
		buffer_free(dst);
		return html_error(NULL);
	}

	write_loop(STDOUT_FILENO, dst->start, dst->ptr - dst->start);
	buffer_free(dst);

	return 0;
}
Exemplo n.º 26
0
/**
   Load or save all variables
*/
static void load_or_save( int save)
{
	const wcstring wdir = fishd_get_config();
	char hostname[HOSTNAME_LEN];
	connection_t c;
	int fd;
	
	if (wdir.empty())
		return;
	
	std::string dir = wcs2string( wdir );
	
	gethostname( hostname, HOSTNAME_LEN );
	
    std::string name;
    name.append(dir);
    name.append("/");
    name.append(FILE);
    name.append(hostname);
	
	debug( 4, L"Open file for %s: '%s'", 
		   save?"saving":"loading", 
		   name.c_str() );
	
    /* OK to not use CLO_EXEC here because fishd is single threaded */
	fd = open(name.c_str(), save?(O_CREAT | O_TRUNC | O_WRONLY):O_RDONLY, 0600);
	
	if( fd == -1 )
	{
		debug( 1, L"Could not open load/save file. No previous saves?" );
		wperror( L"open" );
		return;		
	}
	debug( 4, L"File open on fd %d", c.fd );

	connection_init( &c, fd );

	if( save )
	{
		
		write_loop( c.fd, SAVE_MSG, strlen(SAVE_MSG) );
		enqueue_all( &c );
	}
	else
		read_message( &c );

	connection_destroy( &c );	
}
Exemplo n.º 27
0
void check_abort(int be_async_signal_safe)
{
	if (!event_abort) return;

	tty_done();

	if (be_async_signal_safe) {
		if (john_main_process)
			write_loop(2, "Session aborted\n", 16);
		_exit(1);
	}

	if (john_main_process)
		fprintf(stderr, "Session aborted\n");
	error();
}
Exemplo n.º 28
0
static void sig_handle_abort(int signum)
{
	int saved_errno = errno;

#if OS_FORK
	if (john_main_process) {
/*
 * We assume that our children are running on the same tty with us, so if we
 * receive a SIGINT they probably do as well without us needing to forward the
 * signal to them.  If we forwarded the signal anyway, this could result in
 * them receiving the signal twice for a single Ctrl-C keypress and proceeding
 * with immediate abort without updating the files, which is behavior that we
 * reserve for (presumably intentional) repeated Ctrl-C keypress.
 *
 * We forward the signal as SIGINT even though ours was different (typically a
 * SIGTERM) in order not to trigger a repeated same signal for children if the
 * user does e.g. "killall john", which would send SIGTERM directly to children
 * and also have us forward a signal.
 */
		if (signum != SIGINT)
			signal_children(SIGINT);
	} else {
		static int prev_signum;
/*
 * If it's not the same signal twice in a row, don't proceed with immediate
 * abort since these two signals could have been triggered by the same killall
 * (perhaps a SIGTERM from killall directly and a SIGINT as forwarded by our
 * parent).  event_abort would be set back to 1 just below the check_abort()
 * call.  We only reset it to 0 temporarily to skip the immediate abort here.
 */
		if (prev_signum && signum != prev_signum)
			event_abort = 0;
		prev_signum = signum;
	}
#endif

	check_abort(1);

	event_abort = event_pending = 1;

	write_loop(2, "Wait...\r", 8);

	sig_install_abort();

	errno = saved_errno;
}
Exemplo n.º 29
0
static void log_file_flush(struct log_file *f)
{
	int count;

	if (f->fd < 0) return;

	count = f->ptr - f->buffer;
	if (count <= 0) return;

#if defined(LOCK_EX) && OS_FLOCK
	if (flock(f->fd, LOCK_EX)) pexit("flock");
#endif
	if (write_loop(f->fd, f->buffer, count) < 0) pexit("write");
	f->ptr = f->buffer;
#if defined(LOCK_EX) && OS_FLOCK
	if (flock(f->fd, LOCK_UN)) pexit("flock");
#endif
}
Exemplo n.º 30
0
static bool write_color(char *todo, unsigned char idx, bool is_fg) {
    bool result = false;
    if (idx < 16 || term256_support_is_native()) {
        /* Use tparm */
        putp( tparm( todo, idx ) );
        result = true;
    } else {
        /* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */
        char stridx[128];
        format_long_safe(stridx, (long)idx);
        char buff[128] = "\x1b[";
        strcat(buff, is_fg ? "38;5;" : "48;5;");
        strcat(buff, stridx);
        strcat(buff, "m");
        write_loop(STDOUT_FILENO, buff, strlen(buff));
        result = true;
    }
    return result;
}