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
|
/*
* revoutput.c --- Provide an output wrapper that reverses lines.
*
* Arnold Robbins
* arnold@skeeve.com
* Written 8/2012
*/
/*
* Copyright (C) 2012, 2013 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK 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 3 of the License, or
* (at your option) any later version.
*
* GAWK 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "gawkapi.h"
#include "gettext.h"
#define _(msgid) gettext(msgid)
#define N_(msgid) msgid
static const gawk_api_t *api; /* for convenience macros to work */
static awk_ext_id_t *ext_id;
static const char *ext_version = "revoutput extension: version 1.0";
static awk_bool_t init_revoutput(void);
static awk_bool_t (*init_func)(void) = init_revoutput;
int plugin_is_GPL_compatible;
/* rev_fwrite --- write out characters in reverse order */
static size_t
rev_fwrite(const void *buf, size_t size, size_t count, FILE *fp, void *opaque)
{
const char *cp = buf;
int nbytes = size * count;
(void) opaque;
for (; nbytes >= 1; nbytes--)
putc(cp[nbytes-1], fp);
return (size * count);
}
/* revoutput_can_take_file --- return true if we want the file */
static awk_bool_t
revoutput_can_take_file(const awk_output_buf_t *outbuf)
{
awk_value_t value;
if (outbuf == NULL)
return awk_false;
if (! sym_lookup("REVOUT", AWK_NUMBER, & value))
return awk_false;
return (value.num_value != 0);
}
/*
* revoutput_take_control_of --- set up output wrapper.
* We can assume that revoutput_can_take_file just returned true,
* and no state has changed since then.
*/
static awk_bool_t
revoutput_take_control_of(awk_output_buf_t *outbuf)
{
if (outbuf == NULL)
return awk_false;
outbuf->gawk_fwrite = rev_fwrite;
outbuf->redirected = awk_true;
return awk_true;
}
static awk_output_wrapper_t output_wrapper = {
"revoutput",
revoutput_can_take_file,
revoutput_take_control_of,
NULL
};
/* init_revoutput --- set things ups */
static awk_bool_t
init_revoutput()
{
awk_value_t value;
register_output_wrapper(& output_wrapper);
make_number(0.0, & value); /* init to false */
if (! sym_update("REVOUT", & value)) {
warning(ext_id, _("revoutput: could not initialize REVOUT variable"));
return awk_false;
}
return awk_true;
}
static awk_ext_func_t func_table[] = {
{ NULL, NULL, 0 }
};
/* define the dl_load function using the boilerplate macro */
dl_load_func(func_table, revoutput, "")
|