File: iselect_main.c

package info (click to toggle)
iselect 1.2.0-12
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 444 kB
  • ctags: 213
  • sloc: ansic: 2,074; sh: 1,192; perl: 485; makefile: 154
file content (309 lines) | stat: -rw-r--r-- 10,294 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*      _ ____       _           _   
**     (_) ___|  ___| | ___  ___| |_ 
**    / /\___ \ / _ \ |/ _ \/ __| __|
**   / /  ___) |  __/ |  __/ (__| |_ 
**  (_(  |____/ \___|_|\___|\___|\__|
**
**  iSelect -- Interactive Selection Tool
**
**  iSelect is a Curses-based tool for interactive line selection 
**  in an ASCII file via a full-screen terminal session.
**  
**  ======================================================================
**
**  Copyright (c) 1996-1999 Ralf S. Engelschall.
**
**  This program is free software; it may be redistributed and/or
**  modified only under the terms of the GNU General Public License, 
**  which may be found in the iSelect source distribution.  
**  Look at the file COPYING for details. 
**
**  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 the GNU General Public License for more details.
**
**  ======================================================================
**
**  iselect_main.c -- main program
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include "iselect_global.h"
#include "iselect_getopt.h"
#include "iselect_browse.h"
#include "iselect_keys.h"
#define AS_HEADER
#include "iselect_version.c"

void give_version(char *name);
void give_usage(char *name);

void give_version(char *name)
{
    fprintf(stderr, "%s\n", iSelect_Hello);
    fprintf(stdout, "\n");
    fprintf(stdout, "Copyright (c) 1996,1997,1998 Ralf S. Engelschall.\n");
    fprintf(stdout, "\n");
    fprintf(stdout, "This program is distributed in the hope that it will be useful,\n");
    fprintf(stdout, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
    fprintf(stdout, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
    fprintf(stdout, "GNU General Public License for more details.\n");
    fprintf(stdout, "\n");
}

void give_usage(char *name)
{
    fprintf(stderr, "Usage: %s [options] [line1 line2 ...]\n", name);
    fprintf(stderr, "\n");
    fprintf(stderr, " Input Options:\n");
    fprintf(stderr, "  -d, --delimiter=STR,STR selection tag delimiters\n");
    fprintf(stderr, "  -c, --strip-comments    strip sharp-comments in input buffer\n");
    fprintf(stderr, "  -f, --force-browse      browse even if input has 0 or only 1 line\n");
    fprintf(stderr, "  -a, --all-select        force all lines to be selectable\n");
    fprintf(stderr, "  -e, --exit-no-select    exit immediately if no lines are selectable\n");
    fprintf(stderr, "\n");
    fprintf(stderr, " Display Options:\n");
    fprintf(stderr, "  -p, --position=NUM      initial line position of cursor\n");
    fprintf(stderr, "  -k, --key=KEY[:OKEY]    enable an additional input key\n");
    fprintf(stderr, "  -m, --multi-line        allow multiple lines to be selected\n");
    fprintf(stderr, "  -n, --name=STR          program name shown flush-left on status bar\n");
    fprintf(stderr, "  -t, --title=STR         title string shown centered on status bar\n");
    fprintf(stderr, "\n");
    fprintf(stderr, " Output Options:\n");
    fprintf(stderr, "  -S, --strip-result      strip whitespaces in result string\n");
    fprintf(stderr, "  -P, --position-result   prefix result string with `N:' (N=line number)\n");
    fprintf(stderr, "  -K, --key-result        prefix result string with `K:' (K=select key)\n");
    fprintf(stderr, "  -Q, --quit-result=STR   result string on quit (default='')\n");
    fprintf(stderr, "\n");
    fprintf(stderr, " Giving Feedback:\n");
    fprintf(stderr, "  -V, --version           display version string\n");
    fprintf(stderr, "  -h, --help              display this page\n");
    fprintf(stderr, "\n");
}

struct option options[] = {
    { "delimiter",       TRUE,  NULL, 'd'  },
    { "strip-comments",  FALSE, NULL, 'c'  },
    { "force-browse",    FALSE, NULL, 'f'  },
    { "all-select",      FALSE, NULL, 'a'  },
    { "exit-no-select",  FALSE, NULL, 'e'  },
    { "position",        TRUE,  NULL, 'p'  },
    { "key",             TRUE,  NULL, 'k'  },
    { "multi-line",      FALSE, NULL, 'm'  },
    { "name",            TRUE,  NULL, 'n'  },
    { "title",           TRUE,  NULL, 't'  },
    { "strip-result",    FALSE, NULL, 'S'  },
    { "position-result", FALSE, NULL, 'P'  },
    { "key-result",      FALSE, NULL, 'K'  },
    { "quit-result",     TRUE,  NULL, 'Q'  },
    { "version",         FALSE, NULL, 'V'  },
    { "help",            FALSE, NULL, 'h'  },
    { NULL,              FALSE, NULL, '\0' },
};

int main(int argc, char **argv)
{
    int fpStdout;
    int fpStdin;
    char *cp;
    char c;
    int pos = -1;
    char *progname;
    int nBuf, p;
    /*  This buffer is used to store the string representation of an int */
    char ca[22];
    char *title = "";
    char *name = "iSelect";
    int stripco = FALSE;
    int stripws = FALSE;
    int resultline = FALSE;
    int keyresultline = FALSE;
    int browsealways = FALSE;
    int allselectable = FALSE;
    int multiselect = FALSE;
    int exitnoselect = FALSE;
    int i;
    char *keystr;
    char *abortstr = NULL;
	char *tagbegin = "<";
	char *tagend   = ">";
    char *caBuf;
    int caBufLen;

    /*
     *  argument handling
     */

    /*  canonicalize program name */
    if ((cp = strrchr(argv[0], '/')) != NULL)
        progname = cp+1;
    else
        progname = argv[0];
    argv[0] = progname;

    /*  parse the option arguments */
    opterr = 0;
    while ((c = getopt_long(argc, argv, "d:cfaep:k:mn:t:SPKQ:Vh", options, NULL)) != (char)(-1)) {
        if (optarg == NULL) 
            optarg = "(null)";
        switch (c) {
            case 'd':
				tagbegin = strdup(optarg);
				if ((cp = strchr(tagbegin, ',')) == NULL) {
                    fprintf(stderr, "iSelect: bad argument to option '%c'\n", optopt);
                    fprintf(stderr, "Try `%s --help' for more information.\n", progname);
                    exit(EX_USAGE);
				}
				*cp++ = NUL;
				tagend = cp;
                break;
            case 'c':
                stripco = TRUE;
                break;
            case 'f':
                browsealways = TRUE;
                break;
            case 'a':
                allselectable = TRUE;
                break;
            case 'e':
                exitnoselect = TRUE;
                break;
            case 'p':
                pos = atoi(optarg); 
                break;
            case 'k':
                configure_custom_key(optarg); 
                break;
            case 'm':
                multiselect = TRUE;
                break;
            case 'n':
                name = strdup(optarg); 
                break;
            case 't':
                title = strdup(optarg); 
                break;
            case 'S':
                stripws = TRUE;
                break;
            case 'P':
                resultline = TRUE;
                break;
            case 'K':
                keyresultline = TRUE;
                break;
            case 'Q':
                abortstr = strdup(optarg); 
                break;
            case 'V':
                give_version(progname);
                exit(EX_OK);
            case 'h':
                give_usage(progname);
                exit(EX_OK);
            case '?':
                fprintf(stderr, "iSelect: invalid option: '%c'\n", optopt);
                fprintf(stderr, "Try `%s --help' for more information.\n", progname);
                exit(EX_USAGE);
            case ':':
                fprintf(stderr, "iSelect: missing argument to option '%c'\n", optopt);
                fprintf(stderr, "Try `%s --help' for more information.\n", progname);
                exit(EX_USAGE);
        }
    }

    /*
     *  read input
     */

    nBuf = 0;
    caBufLen = 1024;
    caBuf = (char *) malloc(caBufLen+1);
    caBuf[0] = NUL;
    if (optind < argc) {
        /* browsing text is given as arguments */
        for (; optind < argc; ++optind) {
            cp = (argv[optind] == NULL ? "" : argv[optind]);
            if (nBuf+strlen(cp)+1 >= caBufLen) {
                caBufLen += strlen(cp)+1;
                caBuf = (char *) realloc((void *)caBuf, caBufLen+1);
            }
            strcpy(caBuf+nBuf, cp);
            nBuf += strlen(cp);
            caBuf[nBuf++] = NL;
        }
        caBuf[nBuf++] = NUL;
    }
    else if (optind == argc && !feof(stdin)) {
        /* browsing text is given on stdin */
        while ((c = fgetc(stdin)) != (char)(EOF)) {
            if (nBuf >= caBufLen) {
                caBufLen += 1024;
                caBuf = (char *) realloc((void *)caBuf, caBufLen+1);
            }
            caBuf[nBuf++] = c;
        }
        caBuf[nBuf++] = NUL;

        /* save stdin filehandle and reconnect it to tty */
        fpStdin = dup(0);
        close(0);
        open("/dev/tty", O_RDONLY);
    }
    else {
        give_usage(progname);
        exit(EX_USAGE);
    }

    /*
     *  preserve stdout filehandle for result string, i.e.
     *  use the terminal directly for output
     */
    fpStdout = dup(1);
    close(1);
    open("/dev/tty", O_RDWR);

    pos = (pos < 1 ? 1 : pos);

    p = iSelect(caBuf, pos-1, title, name,
                tagbegin, tagend, stripco, stripws, 
                browsealways, allselectable, multiselect, exitnoselect, &keystr);

    /*
     *  give back the result string to the user via 
     *  the stdout file handle
     */
    if (p != -1) {
        for (i = 0; i < nLines; i++) {
            if (spaLines[i]->fSelected) {
                if (resultline) {
                    sprintf(ca, "%d", i+1);
                    write(fpStdout, ca, strlen(ca));
                    write(fpStdout, ":", 1);
                }
                if (keyresultline) {
                    write(fpStdout, keystr, strlen(keystr));
                    write(fpStdout, ":", 1);
                }
                write(fpStdout, spaLines[i]->cpResult, strlen(spaLines[i]->cpResult));
                write(fpStdout, "\n", 1);
            }
        }
    }
    else {
        if (abortstr != NULL)
            write(fpStdout, abortstr, strlen(abortstr));
    }
    exit(0);
}


/*EOF*/