Truncate overlong tag info and replace newlines by spaces.
[paraslash.git] / stdin.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file stdin.c Functions that deal with reading from stdin. */
8
9 #include <dirent.h> /* readdir() */
10 #include <assert.h>
11
12
13 #include "para.h"
14 #include "string.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "fd.h"
18 #include "error.h"
19 #include "stdin.h"
20
21 /**
22  * The pre_select function of the stdin task.
23  *
24  * \param s The scheduler this task was registered to.
25  * \param t The task structure of the stdin task.
26  *
27  * This function is always successful. If there is space left in the
28  * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
29  * of \a s.
30  */
31 static void stdin_pre_select(struct sched *s, struct task *t)
32 {
33         struct stdin_task *sit = container_of(t, struct stdin_task, task);
34
35         if (sit->output_error && *sit->output_error < 0) {
36                 t->error = *sit->output_error;
37                 return;
38         }
39         t->error = 0;
40         sit->check_fd = 0;
41         if (sit->loaded >= sit->bufsize)
42                 return;
43         sit->check_fd = 1;
44         para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
45 }
46
47 /**
48  * The post select function of the stdin task.
49  *
50  * \param s The scheduler this task was registered to.
51  * \param t The task structure of the stdin task.
52  *
53  * This function checks if \p STDIN_FILENO was included by in the read fd set
54  * of \a s during the previous pre_select call.  If yes, and \p STDIN_FILENO
55  * appears to be readable, data is read from stdin into the buffer of the
56  * stdin task.
57  */
58 static void stdin_post_select(struct sched *s, struct task *t)
59 {
60         struct stdin_task *sit = container_of(t, struct stdin_task, task);
61         ssize_t ret;
62
63         if (sit->output_error && *sit->output_error < 0) {
64                 t->error = *sit->output_error;
65                 return;
66         }
67         t->error = 0;
68         if (!sit->check_fd)
69                 return;
70         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
71                 return;
72         ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
73         if (ret < 0)
74                 t->error = ERRNO_TO_PARA_ERROR(errno);
75         else if (ret > 0)
76                 sit->loaded += ret;
77         else
78                 t->error = -E_STDIN_EOF;
79 }
80
81 /**
82  * Initialize a stdin task structure with default values.
83  *
84  * \param sit The stdin task structure.
85  *
86  * This fills in the pre/post select function pointers of the task structure
87  * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
88  * mode and \a bufsize is initialized to 16 KB (but no buffer is allocated).
89  */
90 void stdin_set_defaults(struct stdin_task *sit)
91 {
92         int ret;
93
94         sit->bufsize = 16 * 1024,
95         sit->task.pre_select = stdin_pre_select;
96         sit->task.post_select = stdin_post_select;
97         sprintf(sit->task.status, "stdin reader");
98         ret = mark_fd_nonblocking(STDIN_FILENO);
99         if (ret >= 0)
100                 return;
101         sit->output_error = NULL;
102         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
103         exit(EXIT_FAILURE);
104 }