File: prerex.c

package info (click to toggle)
prerex 6.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 548 kB
  • sloc: ansic: 2,750; sh: 865; makefile: 14
file content (299 lines) | stat: -rw-r--r-- 8,772 bytes parent folder | download | duplicates (2)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*  
  prerex.c  -- interactive editor of prerequisite-chart descriptions
  Copyright (C) 2005 - 2019  R. D. Tennent   
  School of Computing, Queen's University, rdt@cs.queensu.ca 

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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/* 

The key functions in this program are as follows:

  analyze_tex_file():  reads a TeX file and builds an internal 
    representation of the boxes and arrows for the chart in linked lists 
    accessed through global pointer variables first_box and first_arrow, 
    respectively.  Non-chart elements of the TeX file are preserved in 
    temporary files pretext, posttext, and comments. 

  regenerate_tex_file():  re-constructs a TeX file from the temporary files
    pretext, posttext and comments, and the linked lists of nodes and arrows;
    merge_lists() implements the output-ordering strategy.

  process_tex_file():  uses system calls to process the TeX file using
    pdflatex.

  analyze_user_command(): processes editing and other commands entered at 
    the interactive prompt. The libedit (editline) library is used to 
    generate prompts with default responses and allow editing of the 
    responses.

The current stack of "cuts" is accessed through pointer variable
first_cut. A stack of "done" records to support subsequent undo commands
is accessed through pointer variable first_done.

The bool variables grid and reprocess record whether a background
coordinate grid should be displayed, and whether the tex file should be
reprocessed after an editing operation.


*/

# include "prerex.h"


extern int optind;              /* defined in unistd.h  */

bool grid;			/* true if coordinate grid should be shown in background */
bool reprocess = true;          /* false if regenerate_and_process should do nothing */ 

# ifdef HAVE_LIBEDIT
EditLine *el;
History *hist;
HistEvent ev;
# endif

PRIVATE char *command_line;

PRIVATE char basefilename[FILE_LEN];
PRIVATE char *basefilename_n = basefilename;
char chartfilename[FILE_LEN];
char *chartfilename_n = chartfilename;
char backup_filename[FILE_LEN + 1];
char *backup_filename_n = backup_filename;

FILE *tex_file, *backup_tex_file;

PRIVATE void
usage (void)
{
  puts ("Usage: prerex [-v | --version | -h | --help] [basefile[.tex] [chartfile[.tex]]]");
}



PRIVATE void
sigproc (int i)  /* signal handler */
{
# ifdef HAVE_LIBEDIT
  history_end (hist);
  el_end (el);
# endif
  i++;  /* to avoid spurious compiler warning */
  grid = false;
  puts("Turning off coordinate grid.");
  regenerate_tex_file(); 
  fclose(tex_file);
  exit (0);
}


PRIVATE void
create_blank_tex_file (void)
{
  tex_file = fopen (chartfilename, "w+");
  if (tex_file == NULL)
  {
    error ("Opening new file fails.");
  }
  fprintf (tex_file, "%s\n", "\\documentclass{article}");
  fprintf (tex_file, "%s\n", "\\usepackage{geometry}");
  fprintf (tex_file, "%s\n", "\\geometry{noheadfoot, margin=0.5in}");
  fprintf (tex_file, "%s\n", "\\usepackage{prerex}");
  fprintf (tex_file, "%s\n", "\\begin{document}");
  fprintf (tex_file, "%s\n", "\\thispagestyle{empty}");
  fprintf (tex_file, "%s\n", "\\begin{chart}");
  fprintf (tex_file, "%s\n", "\\end{chart}");
  fprintf (tex_file, "%s\n", "\\end{document}");
  fflush (tex_file);
  rewind (tex_file);
}


PRIVATE void
open_tex_file (void)  /* initial opening  */
{
  tex_file = fopen (chartfilename, "r+");
  if (tex_file == NULL)
    {
      printf ("Creating new prerex chart file %s.\n", chartfilename);
      create_blank_tex_file ();
    }
  printf ("Chart file %s opened.\n", chartfilename);
}

PRIVATE void
reopen_tex_file (void)
{
  tex_file = fopen (chartfilename, "r+");
  if (tex_file == NULL)
     error ("Can't re-open the chart file.");
} 

PRIVATE void
create_backup (void)
{
  backup_filename[0] = '\0';
  backup_filename_n = backup_filename;
  append (backup_filename, &backup_filename_n, ".", sizeof (backup_filename));
  append (backup_filename, &backup_filename_n, chartfilename, sizeof (backup_filename));
  backup_tex_file = fopen (backup_filename, "w+");
  if (backup_tex_file == NULL)
    {
      puts ("Can't open backup file.");
      return;
    }
  copy (tex_file, backup_tex_file);
  fclose (backup_tex_file);
}

PRIVATE bool
process_tex_file (void)
{ /*  generates system calls to make file.pdf */
  /*  returns true if processing fails */
  char command[LINE_LEN] = {'\0'};
  char *command_n = command;
  char filename_root[FILE_LEN + 3] = {'\0'};
  char *filename_root_n = filename_root;
  append (filename_root, &filename_root_n, basefilename, sizeof (filename_root));
  filename_root[strlen(filename_root) - 4] = '\0'; /*   truncate ".tex"     */
  printf ("Processing %s.\n", basefilename);
  append (command, &command_n, "pdflatex ", sizeof (command));
  append (command, &command_n,
           "-halt-on-error -interaction batchmode ", sizeof (command));
  append (command, &command_n, filename_root, sizeof (command));
  if (fclose(tex_file) == EOF) error ("Chart file not closed.");
  if (system (command))
    { 
     printf ("Processing %s fails.\n", basefilename);
     return true;
    }
  reopen_tex_file ();
  return false;  /* success */
}


void
regenerate_and_process (void)
{
  if (!reprocess) return;
  regenerate_tex_file ();
  process_tex_file ();  /* ignore failure; allows user to recover by editing the tex file */
}

int
main (int argc, char *argv[])
{
  int ch;
  const char version[] = "6.8.0, 2019-11-15";
# define NOPTS 3
  struct option longopts[NOPTS] =
  {  { "help", 0, NULL, 'h'},
     { "version", 0, NULL, 'v'},
     { NULL, 0, NULL, 0}
  };


  printf ("This is prerex, version %s.\n", version);
  ch = getopt_long (argc, argv, "hv", longopts, NULL);
  while (ch != -1)
    {
      switch (ch)
	{
	case 'h':
	  usage ();
          puts ( "Please report bugs to rdt@cs.queensu.ca." );
	  exit (0);
	case 'v':
	  exit (0);
	case '?':
	  exit (EXIT_FAILURE);
	default:
	  printf ("Function getopt returned character code 0%o.\n",
		  (unsigned int) ch);
	  exit (EXIT_FAILURE);
	}
      ch = getopt_long (argc, argv, "hv", longopts, NULL);
    }
  puts ( "Copyright (C) 2005 - 2014  R. D. Tennent" );
  puts ( "School of Computing, Queen's University, rdt@cs.queensu.ca" );
  puts ( "License GNU GPL version 2 or later <http://gnu.org/licences/gpl.html>." );
  puts ( "There is NO WARRANTY, to the extent permitted by law." );
  puts ( "" );
# ifdef HAVE_LIBEDIT
  el = el_init (argv[0], stdin, stdout, stderr);
  hist = history_init();
  history(hist, &ev, H_SETSIZE, 800);
  el_set (el, EL_HIST, history, hist);
  el_set (el, EL_PROMPT, &prompt_f);
  el_set (el, EL_EDITOR, "emacs");
# endif

  basefilename[0] = '\0';
  basefilename_n = basefilename;
  if (optind >= argc)
    {
      puts ("");
      do
	{
      deftext[0] = '\0';
	  command_line = Readline ("Please enter a file name: ");
	  sscanf (command_line, "%127s", basefilename);
          while (*basefilename_n)  
            basefilename_n++;
	}
      while (basefilename[0] == '\0');
    }
  else
    append (basefilename, &basefilename_n, argv[optind], sizeof (basefilename));
  if (!suffix (".tex", basefilename))
    append (basefilename, &basefilename_n, ".tex", sizeof (basefilename));
  optind++;
  chartfilename[0] = '\0';
  chartfilename_n = chartfilename;
  if (optind < argc)  /* user-provided chartfilename */
    {
      append (chartfilename, &chartfilename_n, argv[optind], sizeof (chartfilename));
      if (!suffix (".tex", chartfilename))
        append (chartfilename, &chartfilename_n, ".tex", sizeof (chartfilename));
    }
  else /* use basefile as chartfile */
    append (chartfilename, &chartfilename_n, basefilename, sizeof (chartfilename));

  open_tex_file ();

  if (process_tex_file ()) /* bail if initial processing fails */
  {
    exit(EXIT_FAILURE);
  }

  analyze_tex_file ();

  /* catch signals in order to regenerate tex file, restore write access, etc. */
  signal (SIGINT, &sigproc); 
  signal (SIGILL, &sigproc);
  signal (SIGSEGV, &sigproc);
  signal (SIGTERM, &sigproc);  

  grid = true;	
  puts("Turning on coordinate grid.");
  regenerate_and_process (); 

  create_backup ();  

  process_commands();

  return 0;
}