make get_playlist_data() generic.
[paraslash.git] / stdin.c
1 /*
2 * Copyright (C) 2006 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 = t->private_data;
34 t->ret = 1;
35 sit->check_fd = 0;
36 if (sit->loaded >= sit->bufsize)
37 return;
38 sit->check_fd = 1;
39 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
40 }
41
42 static void stdin_default_event_handler(struct task *t)
43 {
44 PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
45 unregister_task(t);
46 }
47
48 /**
49 * the post select function of the stdin task
50 *
51 * \param s the scheduler this task was registered to
52 * \param t the task structure of the stdin task
53 *
54 * This function checks if \p STDIN_FILENO was included by in the read fd set
55 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
56 * appeears to be readable, data is read from stdin into the buffer of the
57 * stdin task.
58 */
59 static void stdin_post_select(struct sched *s, struct task *t)
60 {
61 struct stdin_task *sit = t->private_data;
62 ssize_t ret;
63
64 t->ret = 1;
65 if (!sit->check_fd)
66 return;
67 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
68 return;
69 ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
70 if (ret < 0)
71 t->ret = -E_STDIN_READ;
72 else if (ret > 0) {
73 sit->loaded += ret;
74 t->ret = ret;
75 } else
76 t->ret = -E_STDIN_EOF;
77 if (t->ret < 0)
78 sit->eof = 1;
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 poinzters of the task structure
87 * given by \a sit. It also sets up a default error handler which unregisters
88 * the task. Moreover, \a loaded and \a eof are set to zero and \a bufsize is
89 * initialized to 16 KB (but no buffer is allocated).
90 */
91 void stdin_set_defaults(struct stdin_task *sit)
92 {
93 sit->bufsize = 16 * 1024,
94 sit->loaded = 0,
95 sit->eof = 0,
96 sit->task.pre_select = stdin_pre_select;
97 sit->task.post_select = stdin_post_select;
98 sit->task.event_handler = stdin_default_event_handler;
99 sit->task.private_data = sit;
100 mark_fd_nonblock(STDIN_FILENO);
101 sprintf(sit->task.status, "stdin reader");
102 }