summaryrefslogtreecommitdiff
path: root/demos/calc/calcread.c
blob: ca8395e1072f5c42291f1581943679a7c50decbd (plain)
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
/* Readline support for calc program.

Copyright 2000, 2001 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

This program 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.

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 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, see http://www.gnu.org/licenses/.  */

#include "calc-common.h"

#if WITH_READLINE
#include <stdio.h>   /* for FILE for old versions of readline/readline.h */
#include <stdlib.h>  /* for free */
#include <string.h>  /* for strdup */
#include <unistd.h>  /* for isatty */
#include <readline/readline.h>
#include <readline/history.h>

#include "gmp.h"


/* change this to "#define TRACE(x) x" for a few diagnostics */
#define TRACE(x) 


#define MIN(x,y) ((x) < (y) ? (x) : (y))

char *
calc_completion_entry (const char *text, int state)
{
  static int  index, len;
  char  *name;

  if (!state)
    {
      index = 0;
      len = strlen (text);
    }
  TRACE (printf ("calc_completion_entry %s %d, index=%d len=%d\n",
                 text, state, index, len));
  while ((name = calc_keywords[index].name) != NULL)
    {
      index++;
      if (memcmp (name, text, len) == 0)
        return (strdup (name));
    }
  return NULL;
}

void
calc_init_readline (void)
{
  /* By default use readline when the input is a tty.  It's a bit contrary
     to the GNU interface conventions to make the behaviour depend on where
     the input is coming from, but this is pretty convenient.  */
  if (calc_option_readline == -1)
    {
      calc_option_readline = isatty (fileno (stdin));
      TRACE (printf ("calc_option_readline %d\n", calc_option_readline));
    }

  if (calc_option_readline)
    {
      printf ("GNU MP demo calculator program, gmp version %s\n", gmp_version);
      printf ("Type \"help\" for help.\n");
      rl_readline_name = "gmp-calc";
      rl_completion_entry_function = calc_completion_entry;
    }
}


/* This function is supposed to return YY_NULL to indicate EOF, but that
   constant is only in calclex.c and we don't want to clutter calclex.l with
   this readline stuff, so instead just hard code 0 for YY_NULL.  That's
   it's defined value on unix anyway.  */

int
calc_input (char *buf, size_t max_size)
{
  if (calc_option_readline)
    {
      static char    *line = NULL;
      static size_t  line_size = 0;
      static size_t  upto = 0;
      size_t         copy_size;

      if (upto >= line_size)
        {
          if (line != NULL)
            free (line);

          line = readline (calc_more_input ? "more> " : "> ");
          calc_more_input = 1;
          if (line == NULL)
            return 0;
          TRACE (printf ("readline: %s\n", line));

          if (line[0] != '\0')
            add_history (line);

          line_size = strlen (line);
          line[line_size] = '\n';
          line_size++;
          upto = 0;
        }

      copy_size = MIN (line_size-upto, max_size);
      memcpy (buf, line+upto, copy_size);
      upto += copy_size;
      return copy_size;
    }
  else
    {
      /* not readline */
      return fread (buf, 1, max_size, stdin);
    }
}


/* This redefined input() might let a traditional lex use the readline
   support here.  Apparently POSIX doesn't specify whether an override like
   this will work, so maybe it'll work or maybe it won't.  This function is
   also not particularly efficient, but don't worry about that, since flex
   is the preferred parser.  */

int
input (void)
{
  char  c;
  if (calc_input (&c, 1) != 1)
    return EOF;
  else
    return (int) c;
}

#endif /* WITH_READLINE */