[go: up one dir, main page]

File: print_prompt.c

package info (click to toggle)
trueprint 5.4-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,384 kB
  • sloc: sh: 12,296; ansic: 8,482; makefile: 115
file content (242 lines) | stat: -rw-r--r-- 6,653 bytes parent folder | download | duplicates (11)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Source file:
 *	print_prompt.c
 */

#include "config.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "trueprint.h"
#include "main.h"
#include "index.h"
#include "utils.h"
#include "options.h"
#include "output.h"

#include "print_prompt.h"

/*
 * Public part
 */

char    *print_selection;



/*
 * Private part
 */
static boolean check_selection(long page_no, char *filename);
static boolean no_prompt_to_print;

/*
 * Function:
 *	setup_print_prompter()
 */

void
setup_print_prompter(void)
{
  string_option("A", "print-pages", NULL, &print_selection, NULL, NULL,
		OPT_PRINT,
		"specify list of pages to be printed");

  boolean_option("a", "no-prompt", "prompt", TRUE, &no_prompt_to_print, NULL, NULL,
		 OPT_PRINT,
		 "don't prompt for each page, whether it should be printed or not",
		 "prompt for each page, whether it should be printed or not");

}

/*
 * Function:
 *	print_prompt
 *
 * Returns whether or not this page should be printed.  Uses
 * -a and -A information.
 */
boolean
print_prompt(page_types type, long filepage_no, char *filename)
{
  char *fn_name;
  char prompt[MAXLINELENGTH];
  char response[INPUT_LINE_LEN];
  char *response_ptr;
  boolean retval=UNSET;
  static int yes_count=0;
  static int no_count=0;
  static boolean all_yes=FALSE, all_no=FALSE;

  if (pass == 0) return FALSE;

  if (all_yes) return TRUE;
  if (all_no) return FALSE;

  if (print_selection != NULL) return check_selection(page_number, filename);

  if (yes_count != 0)
    {
      yes_count--;
      return TRUE;
    }
  if (no_count != 0)
    {
      no_count--;
      return FALSE;
    }

  if (no_prompt_to_print==FALSE)
    {
      switch(type)
	{
	case PAGE_BODY:
	  fn_name = get_function_name(page_number);
	  if (*fn_name)
	    sprintf(prompt, "Print page %5ld, page %3ld of %s %s(%s%s)?",
			  page_number, filepage_no, filename, page_changed(page_number)?"[changed]":"", fn_name,
			  function_changed(page_number)?"[changed]":"");
	  else
	    sprintf(prompt, "Print page %5ld, page %3ld of %s %s?",
			  page_number, filepage_no, filename, page_changed(page_number)?"[changed]":"");

	  break;
	case PAGE_BLANK:
	  sprintf(prompt, "Print blank page %5ld?", page_number+1);
	  break;
	case PAGE_SPECIAL:
	  sprintf(prompt, "Print %s?", filename);
	  break;
	default:
	  abort();
	}

      while (retval == UNSET)
	{
	  fprintf(stderr, gettext("%s [n] ? for help: "), prompt);
	  fflush(stderr);
	  fgets(response,INPUT_LINE_LEN-1,stdin);

	  response_ptr = response;
	  skipspaces(&response_ptr);
	  switch (*response_ptr++)
	    {
	    case 'y':
	    case 'Y':
	      yes_count = strtol(response_ptr, &response_ptr, 10);
	      if ((yes_count == 0) && (*response_ptr == '*')) all_yes = TRUE;
	      if (yes_count == 1) yes_count = 0;
	      retval = TRUE;
	      break;
	    case 'n':
	    case 'N':
	      no_count = strtol(response_ptr, &response_ptr, 10);
	      if ((no_count == 0) && (*response_ptr == '*')) all_no = TRUE;
	      if (no_count == 1) no_count = 0;
	      /*FALLTHROUGH*/
	    case '\0':
	      retval = FALSE;
	      break;
	    case 'p':
	    case 'P':
	      print_selection = strdup(response_ptr);
	      retval = check_selection(page_number, filename);
	      break;
	    case '?':
	    default:
	      fprintf(stderr, "---------------------------------------------\n");
	      fprintf(stderr, gettext("y       print this page\n"));
	      fprintf(stderr, gettext("y<n>    print <n> pages\n"));
	      fprintf(stderr, gettext("y*      print all remaining pages\n"));
	      fprintf(stderr, gettext("n       skip this page\n"));
	      fprintf(stderr, gettext("n<n>    skip <n> pages\n"));
	      fprintf(stderr, gettext("n*      skip all remaining pages\n"));
	      fprintf(stderr, gettext("p<list> print all remaining pages that match <list>\n"));
	      fprintf(stderr, gettext("?       show this message\n\n"));
	      fprintf(stderr, gettext("format for <list>:\n"));
	      fprintf(stderr, gettext("    comma-separated list of specifiers:\n"));
	      fprintf(stderr, gettext("    <n>             print page <n>\n"));
	      fprintf(stderr, gettext("    <n>-<o>         print all pages from page <n> to <o>\n"));
	      fprintf(stderr, gettext("    <function-name> print all pages for function\n"));
	      fprintf(stderr, gettext("    f               print function index\n"));
	      fprintf(stderr, gettext("    F               print file index\n"));
	      fprintf(stderr, gettext("    c               print cross reference info\n"));
	      fprintf(stderr, gettext("e.g. p 1-3,main,5,6 will print pages 1,2,3,5,6 and all\n"));
	      fprintf(stderr, gettext("                    pages for function main.\n"));
	      fprintf(stderr, "---------------------------------------------\n");
	    }
	}
      return retval;
    }
  return TRUE;
}

/*
 * Function:
 *	check_selection
 *
 * Returns whether or not this page should be printed, using the -A
 * string.
 */
static boolean
check_selection(long page_no, char *filename)
{
  char *s_index = print_selection;
  long start_page, end_page;
  size_t name_length;
  char *fn_name = NULL;

  if (page_no != 0) fn_name = get_function_name(page_no);

  while (*s_index)
    {
      start_page = strtol(s_index, &s_index, 10);
      skipspaces(&s_index);
      if ((start_page != 0) && (*s_index == '-'))
	{
	  s_index++;
	  end_page = strtol(s_index, &s_index, 10);
	  if ((start_page <= page_no) && (page_no <= end_page)) return TRUE;
	  skipspaces(&s_index);
	}
      if ((start_page != 0) && ((*s_index == ',')||(*s_index == '\0')))
	{
	  if (start_page == page_no) return TRUE;
	}
      if ((start_page == 0) && (*s_index != '\0'))
	{
	  name_length=0;
	  while ((s_index[name_length] != ',')
		 && (s_index[name_length] != '\0')
		 && !isspace(s_index[name_length]))
	    name_length++;
	  if (name_length == 1)
	    {
	      switch (*s_index)
		{
		case 'f':  if (filename && (strcmp(filename, "function index")==0))    return TRUE; break;
		case 'F':  if (filename && (strcmp(filename, "file index")==0))        return TRUE; break;
		case 'd':  if (page_changed(page_no))                    return TRUE; break;
		case 'D':  if (function_changed(page_no))                return TRUE; break;
		default:   fprintf(stderr, gettext(CMD_NAME ": ignoring unrecognized letter %c\n"), *s_index);
		}
	    }
	  if (fn_name
	      && (strlen(fn_name) == name_length)
	      && (strncmp(get_function_name(page_no), s_index, name_length) == 0))
	    return TRUE;

	  s_index += name_length;
	}

      skipspaces(&s_index);
      if (*s_index == ',') s_index++;
      skipspaces(&s_index);
    }

  return FALSE;
}