1 /*
2  * Source file:
3  *	print_prompt.c
4  */
5 
6 #include "config.h"
7 
8 #include <ctype.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "trueprint.h"
14 #include "main.h"
15 #include "index.h"
16 #include "utils.h"
17 #include "options.h"
18 #include "output.h"
19 
20 #include "print_prompt.h"
21 
22 /*
23  * Public part
24  */
25 
26 char    *print_selection;
27 
28 
29 
30 /*
31  * Private part
32  */
33 static boolean check_selection(long page_no, char *filename);
34 static boolean no_prompt_to_print;
35 
36 /*
37  * Function:
38  *	setup_print_prompter()
39  */
40 
41 void
setup_print_prompter(void)42 setup_print_prompter(void)
43 {
44   string_option("A", "print-pages", NULL, &print_selection, NULL, NULL,
45 		OPT_PRINT,
46 		"specify list of pages to be printed");
47 
48   boolean_option("a", "no-prompt", "prompt", TRUE, &no_prompt_to_print, NULL, NULL,
49 		 OPT_PRINT,
50 		 "don't prompt for each page, whether it should be printed or not",
51 		 "prompt for each page, whether it should be printed or not");
52 
53 }
54 
55 /*
56  * Function:
57  *	print_prompt
58  *
59  * Returns whether or not this page should be printed.  Uses
60  * -a and -A information.
61  */
62 boolean
print_prompt(page_types type,long filepage_no,char * filename)63 print_prompt(page_types type, long filepage_no, char *filename)
64 {
65   char *fn_name;
66   char prompt[MAXLINELENGTH];
67   char response[INPUT_LINE_LEN];
68   char *response_ptr;
69   boolean retval=UNSET;
70   static int yes_count=0;
71   static int no_count=0;
72   static boolean all_yes=FALSE, all_no=FALSE;
73 
74   if (pass == 0) return FALSE;
75 
76   if (all_yes) return TRUE;
77   if (all_no) return FALSE;
78 
79   if (print_selection != NULL) return check_selection(page_number, filename);
80 
81   if (yes_count != 0)
82     {
83       yes_count--;
84       return TRUE;
85     }
86   if (no_count != 0)
87     {
88       no_count--;
89       return FALSE;
90     }
91 
92   if (no_prompt_to_print==FALSE)
93     {
94       switch(type)
95 	{
96 	case PAGE_BODY:
97 	  fn_name = get_function_name(page_number);
98 	  if (*fn_name)
99 	    sprintf(prompt, "Print page %5ld, page %3ld of %s %s(%s%s)?",
100 			  page_number, filepage_no, filename, page_changed(page_number)?"[changed]":"", fn_name,
101 			  function_changed(page_number)?"[changed]":"");
102 	  else
103 	    sprintf(prompt, "Print page %5ld, page %3ld of %s %s?",
104 			  page_number, filepage_no, filename, page_changed(page_number)?"[changed]":"");
105 
106 	  break;
107 	case PAGE_BLANK:
108 	  sprintf(prompt, "Print blank page %5ld?", page_number+1);
109 	  break;
110 	case PAGE_SPECIAL:
111 	  sprintf(prompt, "Print %s?", filename);
112 	  break;
113 	default:
114 	  abort();
115 	}
116 
117       while (retval == UNSET)
118 	{
119 	  fprintf(stderr, gettext("%s [n] ? for help: "), prompt);
120 	  fflush(stderr);
121 	  fgets(response,INPUT_LINE_LEN-1,stdin);
122 
123 	  response_ptr = response;
124 	  skipspaces(&response_ptr);
125 	  switch (*response_ptr++)
126 	    {
127 	    case 'y':
128 	    case 'Y':
129 	      yes_count = strtol(response_ptr, &response_ptr, 10);
130 	      if ((yes_count == 0) && (*response_ptr == '*')) all_yes = TRUE;
131 	      if (yes_count == 1) yes_count = 0;
132 	      retval = TRUE;
133 	      break;
134 	    case 'n':
135 	    case 'N':
136 	      no_count = strtol(response_ptr, &response_ptr, 10);
137 	      if ((no_count == 0) && (*response_ptr == '*')) all_no = TRUE;
138 	      if (no_count == 1) no_count = 0;
139 	      /*FALLTHROUGH*/
140 	    case '\0':
141 	      retval = FALSE;
142 	      break;
143 	    case 'p':
144 	    case 'P':
145 	      print_selection = strdup(response_ptr);
146 	      retval = check_selection(page_number, filename);
147 	      break;
148 	    case '?':
149 	    default:
150 	      fprintf(stderr, "---------------------------------------------\n");
151 	      fprintf(stderr, gettext("y       print this page\n"));
152 	      fprintf(stderr, gettext("y<n>    print <n> pages\n"));
153 	      fprintf(stderr, gettext("y*      print all remaining pages\n"));
154 	      fprintf(stderr, gettext("n       skip this page\n"));
155 	      fprintf(stderr, gettext("n<n>    skip <n> pages\n"));
156 	      fprintf(stderr, gettext("n*      skip all remaining pages\n"));
157 	      fprintf(stderr, gettext("p<list> print all remaining pages that match <list>\n"));
158 	      fprintf(stderr, gettext("?       show this message\n\n"));
159 	      fprintf(stderr, gettext("format for <list>:\n"));
160 	      fprintf(stderr, gettext("    comma-separated list of specifiers:\n"));
161 	      fprintf(stderr, gettext("    <n>             print page <n>\n"));
162 	      fprintf(stderr, gettext("    <n>-<o>         print all pages from page <n> to <o>\n"));
163 	      fprintf(stderr, gettext("    <function-name> print all pages for function\n"));
164 	      fprintf(stderr, gettext("    f               print function index\n"));
165 	      fprintf(stderr, gettext("    F               print file index\n"));
166 	      fprintf(stderr, gettext("    c               print cross reference info\n"));
167 	      fprintf(stderr, gettext("e.g. p 1-3,main,5,6 will print pages 1,2,3,5,6 and all\n"));
168 	      fprintf(stderr, gettext("                    pages for function main.\n"));
169 	      fprintf(stderr, "---------------------------------------------\n");
170 	    }
171 	}
172       return retval;
173     }
174   return TRUE;
175 }
176 
177 /*
178  * Function:
179  *	check_selection
180  *
181  * Returns whether or not this page should be printed, using the -A
182  * string.
183  */
184 static boolean
check_selection(long page_no,char * filename)185 check_selection(long page_no, char *filename)
186 {
187   char *s_index = print_selection;
188   long start_page, end_page;
189   size_t name_length;
190   char *fn_name = NULL;
191 
192   if (page_no != 0) fn_name = get_function_name(page_no);
193 
194   while (*s_index)
195     {
196       start_page = strtol(s_index, &s_index, 10);
197       skipspaces(&s_index);
198       if ((start_page != 0) && (*s_index == '-'))
199 	{
200 	  s_index++;
201 	  end_page = strtol(s_index, &s_index, 10);
202 	  if ((start_page <= page_no) && (page_no <= end_page)) return TRUE;
203 	  skipspaces(&s_index);
204 	}
205       if ((start_page != 0) && ((*s_index == ',')||(*s_index == '\0')))
206 	{
207 	  if (start_page == page_no) return TRUE;
208 	}
209       if ((start_page == 0) && (*s_index != '\0'))
210 	{
211 	  name_length=0;
212 	  while ((s_index[name_length] != ',')
213 		 && (s_index[name_length] != '\0')
214 		 && !isspace(s_index[name_length]))
215 	    name_length++;
216 	  if (name_length == 1)
217 	    {
218 	      switch (*s_index)
219 		{
220 		case 'f':  if (filename && (strcmp(filename, "function index")==0))    return TRUE; break;
221 		case 'F':  if (filename && (strcmp(filename, "file index")==0))        return TRUE; break;
222 		case 'd':  if (page_changed(page_no))                    return TRUE; break;
223 		case 'D':  if (function_changed(page_no))                return TRUE; break;
224 		default:   fprintf(stderr, gettext(CMD_NAME ": ignoring unrecognized letter %c\n"), *s_index);
225 		}
226 	    }
227 	  if (fn_name
228 	      && (strlen(fn_name) == name_length)
229 	      && (strncmp(get_function_name(page_no), s_index, name_length) == 0))
230 	    return TRUE;
231 
232 	  s_index += name_length;
233 	}
234 
235       skipspaces(&s_index);
236       if (*s_index == ',') s_index++;
237       skipspaces(&s_index);
238     }
239 
240   return FALSE;
241 }
242 
243