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
|
/* _ ____ _ _
** (_) ___| ___| | ___ ___| |_
** / /\___ \ / _ \ |/ _ \/ __| __|
** / / ___) | __/ | __/ (__| |_
** (_( |____/ \___|_|\___|\___|\__|
**
** 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,1997,1998 Ralf S. Engelschall.
**
** This program is free software; it may be redistributed and/or modified
** only under the terms of either the Artistic License or the GNU General
** Public License, which may be found in the ePerl source distribution.
** Look at the files ARTISTIC and COPYING or run ``eperl -l'' to receive
** a built-in copy of both license files.
**
** 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 either the
** Artistic License or 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"
extern char iSelect_Hello[];
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, " -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, " -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");
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[] = {
{ "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' },
{ "multi-line", FALSE, NULL, 'm' },
{ "name", TRUE, NULL, 'n' },
{ "title", TRUE, NULL, 't' },
{ "strip-result", FALSE, NULL, 'S' },
{ "position-result", FALSE, NULL, 'P' },
{ "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;
char ca[1024];
char *title = "";
char *name = "iSelect";
int stripco = FALSE;
int stripws = FALSE;
int resultline = FALSE;
int browsealways = FALSE;
int allselectable = FALSE;
int multiselect = FALSE;
int exitnoselect = FALSE;
int i;
char *abortstr = NULL;
/*
* 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, "cfaep:mn:t:SPQ:Vh", options, NULL)) != (char)(-1)) {
if (optarg == NULL)
optarg = "(null)";
switch (c) {
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 '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 '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
*/
if (optind < argc) {
/* browsing text is given as arguments */
nBuf = 0;
for (; optind < argc; ++optind) {
cp = (argv[optind] == NULL ? "" : argv[optind]);
sprintf(caBuf+nBuf, "%s\n", cp);
nBuf += strlen(cp)+1;
}
caBuf[nBuf++] = NUL;
}
else if (optind == argc && !feof(stdin)) {
/* browsing text is given on stdin */
nBuf = 0;
while ((c = fgetc(stdin)) != (char)(EOF)) {
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(pos-1, title, name,
stripco, stripws,
browsealways, allselectable, multiselect, exitnoselect);
/*
* 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, spaLines[i]->cpResult, strlen(spaLines[i]->cpResult));
sprintf(ca, "\n");
write(fpStdout, ca, strlen(ca));
}
}
}
else {
if (abortstr != NULL)
write(fpStdout, abortstr, strlen(abortstr));
}
exit(0);
}
/*EOF*/
|