1 /*
2 * ordchr.c - Builtin functions that provide ord() and chr() functions.
3 *
4 * Arnold Robbins
5 * arnold@skeeve.com
6 * 8/2001
7 * Revised 6/2004
8 * Revised 5/2012
9 */
10
11 /*
12 * Copyright (C) 2001, 2004, 2011, 2012, 2013, 2018, 2020, 2021,
13 * the Free Software Foundation, Inc.
14 *
15 * This file is part of GAWK, the GNU implementation of the
16 * AWK Programming Language.
17 *
18 * GAWK is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * GAWK is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <stdio.h>
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <sys/types.h>
44 #include <sys/stat.h>
45
46 #include "gawkapi.h"
47
48 #include "gettext.h"
49 #define _(msgid) gettext(msgid)
50 #define N_(msgid) msgid
51
52 static const gawk_api_t *api; /* for convenience macros to work */
53 static awk_ext_id_t ext_id;
54 static const char *ext_version = "ordchr extension: version 1.0";
55 static awk_bool_t (*init_func)(void) = NULL;
56
57 int plugin_is_GPL_compatible;
58
59 /* do_ord --- return numeric value of first char of string */
60
61 static awk_value_t *
62 do_ord(int nargs, awk_value_t *result, struct awk_ext_func *unused)
63 {
64 awk_value_t str;
65 double ret = -1;
66
67 assert(result != NULL);
68
69 if (get_argument(0, AWK_STRING, & str)) {
70 ret = (unsigned char) str.str_value.str[0];
71 } else if (do_lint)
72 lintwarn(ext_id, _("ord: first argument is not a string"));
73
74 /* Set the return value */
75 return make_number(ret, result);
76 }
77
78 /* do_chr --- turn numeric value into a string */
79
80 static awk_value_t *
81 do_chr(int nargs, awk_value_t *result, struct awk_ext_func *unused)
82 {
83 awk_value_t num;
84 unsigned int ret = 0;
85 double val = 0.0;
86 char str[2];
87
88 str[0] = str[1] = '\0';
89
90 assert(result != NULL);
91
92 if (get_argument(0, AWK_NUMBER, & num)) {
93 val = num.num_value;
94 ret = val; /* convert to int */
95 ret &= 0xff;
96 str[0] = ret;
97 str[1] = '\0';
98 } else if (do_lint)
99 lintwarn(ext_id, _("chr: first argument is not a number"));
100
101 /* Set the return value */
102 return make_const_string(str, 1, result);
103 }
104
105 static awk_ext_func_t func_table[] = {
106 { "ord", do_ord, 1, 1, awk_false, NULL },
107 { "chr", do_chr, 1, 1, awk_false, NULL },
108 };
109
110 /* define the dl_load function using the boilerplate macro */
111
112 dl_load_func(func_table, ord_chr, "")