| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: scan.c,v 1.1 1999/07/01 08:56:23 mdw Exp $ |
| 4 | * |
| 5 | * Character scanners |
| 6 | * |
| 7 | * (c) 1999 Mark Wooding |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the `fw' port forwarder. |
| 13 | * |
| 14 | * `fw' is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * `fw' is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with `fw'; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: scan.c,v $ |
| 32 | * Revision 1.1 1999/07/01 08:56:23 mdw |
| 33 | * Initial revision |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | #include "config.h" |
| 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <string.h> |
| 44 | |
| 45 | #include <mLib/dstr.h> |
| 46 | |
| 47 | #include "scan.h" |
| 48 | |
| 49 | /*----- Main code ---------------------------------------------------------*/ |
| 50 | |
| 51 | /* --- Generic scanner setup --- */ |
| 52 | |
| 53 | static void scan_init(scanner *sc, scan_ops *ops, const char *src) |
| 54 | { |
| 55 | sc->ops = ops; |
| 56 | sc->line = 1; |
| 57 | sc->src = src; |
| 58 | dstr_create(&sc->d); |
| 59 | } |
| 60 | |
| 61 | /* --- @argv@ scanner --- */ |
| 62 | |
| 63 | static int argv_scan(void *p) |
| 64 | { |
| 65 | scan_argvctx *c = p; |
| 66 | int ch; |
| 67 | |
| 68 | if (c->ch != EOF) { |
| 69 | ch = c->ch; |
| 70 | c->ch = EOF; |
| 71 | } else if (*c->p) |
| 72 | ch = *c->p++; |
| 73 | else if (*c->pp) { |
| 74 | c->p = *c->pp++; |
| 75 | ch = '\n'; |
| 76 | } else |
| 77 | ch = EOF; |
| 78 | |
| 79 | return (ch); |
| 80 | } |
| 81 | |
| 82 | static void argv_unscan(int ch, void *p) |
| 83 | { |
| 84 | scan_argvctx *c = p; |
| 85 | c->ch = ch; |
| 86 | } |
| 87 | |
| 88 | void scan_argvinit(scan_argvctx *c, char **pp) |
| 89 | { |
| 90 | static struct scan_ops ops = { argv_scan, argv_unscan }; |
| 91 | c->p = *pp++; |
| 92 | c->pp = pp; |
| 93 | c->ch = EOF; |
| 94 | scan_init(&c->sc, &ops, "<args>"); |
| 95 | } |
| 96 | |
| 97 | /* --- File scanner --- */ |
| 98 | |
| 99 | static int file_scan(void *p) |
| 100 | { |
| 101 | scan_filectx *c = p; |
| 102 | return (getc(c->fp)); |
| 103 | } |
| 104 | |
| 105 | static void file_unscan(int ch, void *p) |
| 106 | { |
| 107 | scan_filectx *c = p; |
| 108 | ungetc(ch, c->fp); |
| 109 | } |
| 110 | |
| 111 | void scan_fileinit(scan_filectx *c, FILE *fp, const char *file) |
| 112 | { |
| 113 | static struct scan_ops ops = { file_scan, file_unscan }; |
| 114 | c->fp = fp; |
| 115 | scan_init(&c->sc, &ops, file); |
| 116 | } |
| 117 | |
| 118 | /* --- Miscellaneous functions --- */ |
| 119 | |
| 120 | void scan_destroy(void *p) |
| 121 | { |
| 122 | scanner *sc = p; |
| 123 | dstr_destroy(&sc->d); |
| 124 | } |
| 125 | |
| 126 | /*----- That's all, folks -------------------------------------------------*/ |