~ubuntu-branches/ubuntu/maverick/tvtime/maverick

« back to all changes in this revision

Viewing changes to src/fifo.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon Law
  • Date: 2004-01-13 18:00:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040113180036-h996q67t476jymsu
Tags: upstream-0.9.12
ImportĀ upstreamĀ versionĀ 0.9.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright (C) 2002 Doug Bell <drbell@users.sourceforge.net>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software Foundation,
 
16
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
 
 
20
#include <stdio.h>
 
21
#include <stdlib.h>
 
22
#include <unistd.h>
 
23
#include <sys/types.h>
 
24
#include <sys/stat.h>
 
25
#include <fcntl.h>
 
26
#include <string.h>
 
27
#include <errno.h>
 
28
#include <ctype.h>
 
29
#include "utils.h"
 
30
#include "fifo.h"
 
31
 
 
32
struct fifo_s {
 
33
    int fd;
 
34
    char buf[ 256 ];
 
35
    int bufpos;
 
36
    char arg[ 256 ];
 
37
};
 
38
 
 
39
fifo_t *fifo_new( const char *filename )
 
40
{
 
41
    fifo_t *fifo = malloc( sizeof( struct fifo_s ) );
 
42
    if( !fifo ) {
 
43
        return 0;
 
44
    }
 
45
 
 
46
    memset( fifo->buf, 0, sizeof( fifo->buf ) );
 
47
    fifo->bufpos = 0;
 
48
 
 
49
    fifo->fd = open( filename, O_RDONLY | O_NONBLOCK );
 
50
    if( fifo->fd < 0 ) {
 
51
        /* Attempt to create it. */
 
52
        if( mkfifo( filename, S_IREAD | S_IWRITE ) < 0 ) {
 
53
            fprintf( stderr, "fifo: Cannot create %s: %s\n",
 
54
                     filename, strerror( errno ) );
 
55
            free( fifo );
 
56
            return 0;
 
57
        }
 
58
        fifo->fd = open( filename, O_RDONLY | O_NONBLOCK );
 
59
        if( fifo->fd < 0 ) {
 
60
            fprintf( stderr, "fifo: Cannot open %s: %s\n",
 
61
                     filename, strerror( errno ) );
 
62
            free( fifo );
 
63
            return 0;
 
64
        }
 
65
    }
 
66
 
 
67
    /* Our fifo is open for reading. */
 
68
    return fifo;
 
69
}
 
70
 
 
71
static char *fifo_next_line( fifo_t *fifo )
 
72
{
 
73
    char c;
 
74
 
 
75
    if( !fifo ) return 0;
 
76
 
 
77
    while( read( fifo->fd, &c, 1 ) > 0 ) {
 
78
        if( c == '\n' ) return fifo->buf;
 
79
        if( c == '\b' ) {
 
80
            if( fifo->bufpos == 0 ) continue;
 
81
            fifo->buf[ --fifo->bufpos ] = 0;
 
82
            continue;
 
83
        }
 
84
        if( fifo->bufpos < sizeof( fifo->buf ) - 1 ) 
 
85
            fifo->buf[ fifo->bufpos++ ] = c;
 
86
    }
 
87
    return 0;
 
88
}
 
89
 
 
90
int fifo_get_next_command( fifo_t *fifo )
 
91
{
 
92
    char *str = fifo_next_line( fifo );
 
93
    fifo->arg[0] = '\0';
 
94
    if( str ) {
 
95
        int cmd;
 
96
        int i;
 
97
 
 
98
        for( i = 0;; i++ ) {
 
99
            if( !str[ i ] ) break;
 
100
            if( isspace( str[ i ] ) ) {
 
101
                str[ i ] = '\0';
 
102
                memcpy( fifo->arg, str + i + 1, 255 - i );
 
103
                break;
 
104
            }
 
105
        }
 
106
 
 
107
        cmd = tvtime_string_to_command( str );
 
108
 
 
109
        memset( fifo->buf, 0, sizeof( fifo->buf ) );
 
110
        fifo->bufpos = 0;
 
111
    
 
112
        return cmd;
 
113
    }
 
114
 
 
115
    return TVTIME_NOCOMMAND;
 
116
}
 
117
 
 
118
const char *fifo_get_arguments( fifo_t *fifo )
 
119
{
 
120
    return fifo->arg;
 
121
}
 
122
 
 
123
void fifo_delete( fifo_t *fifo )
 
124
{
 
125
    close( fifo->fd );
 
126
    free( fifo );
 
127
}
 
128