/* * vtxget.c: Parse command-line options & args, build queue with requested pages * * $Id: vtxget.c,v 1.3 1997/07/09 21:59:15 mb Exp mb $ * * Copyright (c) 1994-96 Martin Buck * Read COPYING for more information * */ #include #include #include #include #include #include #include #include #include #include "vtx_assert.h" #include "safe_malloc.h" #include "misc.h" #include "batch.h" #include "cct.h" #include "fileio.h" #include "postscript.h" #include "toptext.h" #include "vtxtools.h" #include "vtxdecode.h" #include "vtxget.h" int page_timeout = 60, show_hidden, show_header, output_virtual, stdout_sep; int ps_xpages = 1, ps_ypages = 1, ps_landscape; ps_papertype_t ps_papertype; ofmt_t ofmt; vtx_info_t vtx_info; vtxpage_t page; vtxbmfont_t *vtxbmfont; char *outdir, *fname_prefix = ""; int main(int argc, char *argv[]) { int optchr, index, status, page, subpage; int do_clear_cache = TRUE, getall = FALSE, dump_top = FALSE, view_files = FALSE, pagecount = 0; unsigned int font_xsize = 10, font_ysize = 12; char *tmpptr, tmpstr[100]; smalloc_init(argv[0]); if ((tmpptr = getenv("VTX_DEVICE"))) { cct_device = tmpptr; } while ((optchr = getopt(argc, argv, "t:i:hrf:F:TVa:n:so:p:cD:P:v?")) != -1) { switch (optchr) { case 't': { char *tail; page_timeout = strtol(optarg, &tail, 10); if (*optarg == '\0' || *tail || page_timeout < 0) { fprintf(stderr, "%s: invalid timeout-value: %s (valid range is >= 0)\n", smalloc_progname, optarg); exit(1); } else if (!page_timeout) { page_timeout = 60 * 60 * 24 * 365; /* One year should be infinite enough :-) */ } } break; case 'i': interleave = strtol(optarg, NULL, 10); interleave_hex = vtx_dec2hex(interleave); interleave_set = TRUE; if (interleave < -100 || interleave > 100) { fprintf(stderr, "%s: invalid page-interleave: %s (valid range is -100-100)\n", smalloc_progname, optarg); exit(1); } break; case 'h': show_header = TRUE; break; case 'r': show_hidden = TRUE; break; case 'f': if (!strcmp(optarg, "iso")) { ofmt = FMT_ISO; } else if (!strcmp(optarg, "ansi")) { ofmt = FMT_ANSI; } else if (!strcmp(optarg, "text")) { ofmt = FMT_TEXT; } else if (!strcmp(optarg, "vtx")) { ofmt = FMT_VTX; #ifdef GIF_SUPPORT } else if (!strcmp(optarg, "gif")) { ofmt = FMT_GIF; } else if (!strcmp(optarg, "gifint")) { ofmt = FMT_GIFINT; #endif } else if (!strcmp(optarg, "ppm")) { ofmt = FMT_PPM; #ifdef PNG_SUPPORT } else if (!strcmp(optarg, "png")) { ofmt = FMT_PNG; } else if (!strcmp(optarg, "pngint")) { ofmt = FMT_PNGINT; #endif } else if (!strcmp(optarg, "ps")) { ofmt = FMT_PS; } else if (!strcmp(optarg, "ips")) { ofmt = FMT_IPS; } else if (!strcmp(optarg, "cps")) { ofmt = FMT_CPS; } else { fprintf(stderr, "%s: invalid output format: %s\n", smalloc_progname, optarg); exit(1); } break; case 'F': if (sscanf(optarg, " %u x %u ", &font_xsize, &font_ysize) != 2) { fprintf(stderr, "%s: invalid font size syntax: %s\n", smalloc_progname, optarg); exit(1); } break; case 'T': ascii_insert_pagelist(NUM_BASICTOP, -1); pagecount += 2; dump_top = TRUE; break; case 'V': output_virtual = TRUE; break; case 'a': { int type; for (type = 0; ps_paperdata[type].name; type++) { if (!strcasecmp(optarg, ps_paperdata[type].name)) { ps_papertype = type; break; } } if (!ps_paperdata[type].name) { fprintf(stderr, "%s: invalid pagetype: %s\n", smalloc_progname, optarg); exit(1); } } break; case 'n': tmpstr[0] = '\0'; if (sscanf(optarg, " %d x %d %99c ", &ps_xpages, &ps_ypages, tmpstr) >= 2 && ps_xpages > 0 && ps_xpages < 5 && ps_ypages > 0 && ps_ypages < 5 && (*tmpstr == '\0' || !strcasecmp(tmpstr, "l") || !strcasecmp(tmpstr, "landscape"))) { if (!strcasecmp(tmpstr, "l") || !strcasecmp(tmpstr, "landscape")) { ps_landscape = TRUE; } } else { fprintf(stderr, "%s: invalid page-layout: %s\n", smalloc_progname, optarg); exit(1); } break; case 's': stdout_sep = TRUE; break; case 'o': { struct stat st; outdir = optarg; if (stat(outdir, &st) || access(outdir, W_OK)) { if (errno == ENOENT) { if (mkdir(outdir, S_IRWXU | S_IRWXG | S_IRWXO)) { fprintf(stderr, "%s: %s: %s\n", smalloc_progname, outdir, strerror(errno)); exit(1); } } else { fprintf(stderr, "%s: %s: %s\n", smalloc_progname, outdir, strerror(errno)); exit(1); } } else if (!S_ISDIR(st.st_mode)) { fprintf(stderr, "%s: %s: not a directory\n", smalloc_progname, outdir); exit(1); } } break; case 'p': fname_prefix = optarg; break; case 'c': do_clear_cache = FALSE; break; case 'D': cct_device = optarg; break; case 'P': vtx_fontpath = optarg; break; case 'v': view_files = TRUE; break; case '?': printf("vtxget version " VTXVERSION ", Copyright 1994-96 Martin Buck\n\n" "Usage: %s [options] \n" "Page-syntax:\n" " x Get next subpage of page x (x:100-8FF)\n" " x.y Get subpage y of page x (y:1-7F)\n" " x.0 Get all subpages of page x\n" " x.l Keep seraching for page x (until timeout)\n" " x[.0]-y Search for all pages in range x-y (with all subpages, if .0 was\n" " given). Include hex-pages if either x or y is hex-page.\n" " allnorm Get all normal pages (TOP-Text required)\n" " allsub Get all pages with subpages\n" " all Get all normal and subpages\n" " - Read page-numbers line by line from stdin\n" "Options:\n" " -t Set page-timeout (default: 60s)\n" " -i Set/enable page-interleave (default: 20)\n" " -h Print page-header & status-bits\n" " -r Reveal hidden parts of page\n" " -f {iso|ansi|text|vtx|ppm| Set output-format (default: iso)\n" #ifdef PNG_SUPPORT " gif|gifint|png|pngint|\n" " ps|ips|cps}\n" #else " gif|gifint|ps|ips|cps}\n" #endif " -F x Select font-size for bitmapped output-formats\n" " -o Set output directory (default: stdout)\n" " -p Set prefix for output-files\n" " -T Dump TOP-text information\n" " -V Write virtual rows (vtx-format only)\n" " -a {a4|a3|letter|legal} Set papertype for PS output (default: a4)\n" " -n x[l] Set page-layout for PS output (default: 1x1)\n" " -s Separate pages when writing PS to stdout\n" " -c Don't clear cache on startup\n" " -D Set device to use (default: /dev/vtx)\n" " -P Set font-path (default: " VTX_LIBDIR ")\n" " -v Display files, don't search for pages\n" " -\\? Show this help\n", smalloc_progname); exit(1); break; default: assert(0); break; } } if ((ofmt == FMT_PPM #ifdef GIF_SUPPORT || ofmt == FMT_GIF || ofmt == FMT_GIFINT #endif #ifdef PNG_SUPPORT || ofmt == FMT_PNG || ofmt == FMT_PNGINT #endif ) && !(vtxbmfont = read_font(font_xsize, font_ysize))) { fprintf(stderr, "%s: can't load %dx%d-font\n", smalloc_progname, font_xsize, font_ysize); exit(1); } if (!view_files) { if (optind < argc) { if (!strcasecmp(argv[optind], "allnorm")) { ascii_insert_pagelist(NUM_BASICTOP, -1); pagecount += 2; getall = GET_ALLNORM; } else if (!strcasecmp(argv[optind], "allsub")) { ascii_insert_pagelist(NUM_BASICTOP, -1); pagecount += 2; getall = GET_ALLSUB; } else if (!strcasecmp(argv[optind], "all")) { ascii_insert_pagelist(NUM_BASICTOP, -1); pagecount += 2; getall = GET_ALL; } else if (!strcmp(argv[optind], "-")) { if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) < 0) { fputs(smalloc_progname, stderr); perror(": fcntl(stdin)"); exit(1); } pagecount += 2; } else { for (index = optind; index < argc; index++) { if (strchr(argv[index], '-')) { char *pagestr; int page1, page2, subpage1, subpage2, is_hex; pagestr = sstrdup(argv[index]); if (!(tmpptr = strtok(pagestr, "-")) || !str2pagenum(tmpptr, &page1, &subpage1) | !(tmpptr = strtok(NULL, "-")) || strtok(NULL, "-") || !str2pagenum(tmpptr, &page2, &subpage2) || subpage1 < -1 || subpage1 > 0 || subpage2 != -1 || page2 < page1) { fprintf(stderr, "%s: invalid page-range: %s\n", smalloc_progname, argv[index]); exit(1); } /* Skip hexadecimal pages if neither start- nor end-page are hexadecimal */ is_hex = (page1 & 0xf) >= 0xa || (page1 & 0xf0) >= 0xa0 || (page2 & 0xf) >= 0xa || (page2 & 0xf0) >= 0xa0; for (page = page1; page <= page2; page++) { if (((page & 0xf) >= 0xa || (page & 0xf0) >= 0xa0) && !is_hex) continue; ascii_insert_pagelist(page, subpage1); pagecount += (subpage1 == -1 ? 1 : 2); } free(pagestr); } else { if (!str2pagenum(argv[index], &page, &subpage)) { fprintf(stderr, "%s: invalid page-number: %s\n", smalloc_progname, argv[index]); exit(1); } ascii_insert_pagelist(page, subpage); pagecount += (subpage == -1 ? 1 : 2); /* We don't need the exact number of */ /* pages; we only need to know if it's == 1 */ } } } } if (!pagecount) { fprintf(stderr, "%s: no pages to search for (try `%s -\\?' to get help)\n", smalloc_progname, smalloc_progname); exit(1); } if ((ofmt == FMT_VTX || ofmt == FMT_PPM #ifdef GIF_SUPPORT || ofmt == FMT_GIF || ofmt == FMT_GIFINT #endif #ifdef PNG_SUPPORT || ofmt == FMT_PNG || ofmt == FMT_PNGINT #endif ) && !outdir && pagecount != 1) { const char *fmtstr; switch (ofmt) { case FMT_VTX: fmtstr = "VTX"; break; #ifdef GIF_SUPPORT case FMT_GIF: case FMT_GIFINT: fmtstr = "GIF"; break; #endif case FMT_PPM: fmtstr = "PPM"; break; #ifdef PNG_SUPPORT case FMT_PNG: case FMT_PNGINT: fmtstr = "PNG"; break; #endif default: assert(0); break; } fprintf(stderr, "%s: can't write multiple %s-files to stdout\n", smalloc_progname, fmtstr); exit(1); } if ((status = cct_open(REQ_MAJOR, REQ_MINOR, &vtx_info)) < 0) { if (status == CCTEVERSION) { fprintf(stderr, "%s: can't open videotext-device: incompatible driver version (need %d.%d)\n", smalloc_progname, REQ_MAJOR, REQ_MINOR); } else { fprintf(stderr, "%s: can't open videotext-device %s: %s\n", smalloc_progname, cct_device, strerror(errno)); } exit(1); } if (do_clear_cache) cct_clear_cache(); if (output_virtual && cct_set_virtual(TRUE) < 0) { fprintf(stderr, "%s: Warning: can't enable virtual rows: %s\n", smalloc_progname, strerror(errno)); output_virtual = FALSE; } exit(ascii_get_pages(getall, dump_top) ? 2 : 0); } else { if (optind >= argc) { fprintf(stderr, "%s: no files to display (try `%s -\\?' to get help)\n", smalloc_progname, smalloc_progname); exit(1); } if ((ofmt == FMT_VTX || ofmt == FMT_PPM #ifdef GIF_SUPPORT || ofmt == FMT_GIF || ofmt == FMT_GIFINT #endif #ifdef PNG_SUPPORT || ofmt == FMT_PNG || ofmt == FMT_PNGINT #endif ) && !outdir && optind != argc - 1) { const char *fmtstr; switch (ofmt) { case FMT_VTX: fmtstr = "VTX"; break; #ifdef GIF_SUPPORT case FMT_GIF: case FMT_GIFINT: fmtstr = "GIF"; break; #endif case FMT_PPM: fmtstr = "PPM"; break; #ifdef PNG_SUPPORT case FMT_PNG: case FMT_PNGINT: fmtstr = "PNG"; break; #endif default: assert(0); break; } fprintf(stderr, "%s: can't write multiple %s-files to stdout\n", smalloc_progname, fmtstr); exit(1); } if (display_files(argv + optind) < 0) { exit(1); } } exit(0); }