[go: up one dir, main page]

File: star.c

package info (click to toggle)
cook 2.29-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,656 kB
  • ctags: 4,083
  • sloc: ansic: 47,636; sh: 14,376; makefile: 4,656; yacc: 3,166; perl: 224; awk: 219
file content (244 lines) | stat: -rw-r--r-- 4,794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 *      cook - file construction tool
 *      Copyright (C) 1997, 2001, 2003, 2004, 2006, 2007 Peter Miller;
 *      All rights reserved.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate stars
 */

#include <common/ac/stdio.h>
#include <common/ac/string.h>
#include <common/ac/time.h>

#include <common/fflush_slow.h>
#include <common/page.h>
#include <common/progname.h>
#include <common/quit.h>
#include <common/star.h>
#include <common/trace.h>       /* for assert */

static int      previous;
static int      star_flag;
static int      star_col;
static time_t   star_time;


/*
 * NAME
 *      star
 *
 * SYNOPSIS
 *      void star(void);
 *
 * DESCRIPTION
 *      The star function is used to emit a progress star, if they are
 *      enabled.
 */

void
star(void)
{
    star_as_specified('*');
}


/*
 * NAME
 *      star_as_specified
 *
 * SYNOPSIS
 *      void star_as_specified(int);
 *
 * DESCRIPTION
 *      The star_as_specified function is used to emit the given
 *      character as a progress star, if they are enabled.
 */

void
star_as_specified(int c)
{
    int             immediate;
    time_t          now;
    static int      page_width;

    if (!star_flag)
        return;
    if (!page_width)
        page_width = page_width_get() - 1;
    if (c & STAR_IMMEDIATE)
    {
        c &= ~STAR_IMMEDIATE;
        immediate = 1;
    }
    else
        immediate = 0;
    immediate |= (c != previous);
    time(&now);
    if (now < star_time && !immediate)
        return;
    previous = c;
    if (!star_col)
    {
        char            *progname;

        progname = progname_get();
        fprintf(stderr, "%s: ", progname);
        star_col = strlen(progname) + 2;
    }
    fputc(c, stderr);
    star_col++;
    if (star_col >= page_width)
    {
        fputc('\n', stderr);
        star_col = 0;
    }
    fflush_slowly(stderr);
    star_time = now + 1;
}


/*
 * NAME
 *      star_eoln
 *
 * SYNOPSIS
 *      void star_eoln(void);
 *
 * DESCRIPTION
 *      The star_eoln function is used to end a line of progress stars,
 *      if any have been issued.  This should be done prior to any
 *      output.
 */

void
star_eoln(void)
{
    time_t          now;

    if (!star_flag)
        return;
    if (star_col)
    {
        fputc('\n', stderr);
        star_col = 0;
        fflush_slowly(stderr);
    }
    time(&now);
    star_time = now + 1;
}


/*
 * NAME
 *      star_sync
 *
 * SYNOPSIS
 *      void star_sync(void);
 *
 * DESCRIPTION
 *      The star_eoln function is used to end a line of progress stars,
 *      if any have been issued.  It also introduces a delay of 5
 *      seconds, rather than the defalt 1 second.  This should be done
 *      prior to any echoed command.
 */

void
star_sync(void)
{
    time_t          when;

    if (!star_flag)
        return;
    if (star_col)
    {
        fputc('\n', stderr);
        star_col = 0;
        fflush(stderr);
    }
    time(&when);
    when += 5;
    if (when > star_time)
        star_time = when;
}


/*
 * NAME
 *      star_quit_handler
 *
 * SYNOPSIS
 *      void star_quit_handler(void);
 *
 * DESCRIPTION
 *      The star_quit_handler function is used to clean up any trailing
 *      lines of stars at quit() time.
 *
 * RETURNS
 */

static void
star_quit_handler(void)
{
    star_flag = 0;
    if (star_col)
    {
        fputc('\n', stderr);
        star_col = 0;
    }
}


/*
 * NAME
 *      star_enable
 *
 * SYNOPSIS
 *      void star_enable(void);
 *
 * DESCRIPTION
 *      The star_enable function is used to turn on the emitting of
 *      progress stars.  The default it silence.
 */

void
star_enable(void)
{
    if (star_flag)
        return;
    star_flag = 1;
    quit_handler(star_quit_handler);
    trace(("enabled\n"));
}


/*
 * NAME
 *      star_bang
 *
 * SYNOPSIS
 *      void star_bang(void);
 *
 * DESCRIPTION
 *      The star function is used to emit a iminus as a progress star,
 *      if they are enabled.  (Silent command execution indicator.)
 */

void
star_bang(void)
{
    star_as_specified('-');
}