/*  
  prerex.h  -- header file for an interactive editor of prerequisite-chart 
               descriptions
  Copyright (c) 2005-08  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.
*/

# include <stdlib.h>
# include <stdio.h>
# include <math.h>
# include <ctype.h>
# include <limits.h>
# include <string.h>
# include <readline/readline.h>
# include <readline/history.h>
# include <unistd.h>
# include <signal.h>
# include <stdbool.h>
# include <unistd.h>
# include <sys/types.h>

# define LINE_LEN 1023
# define FILE_LEN 127
# define COMMAND_LEN 63
# define CODE_LEN 31
# define TITLE_LEN 127
# define TIMETABLE_LEN 31
# define KILL_WAIT 4 /* number of seconds to wait before killing PDFVIEWER */

# define PRIVATE static

typedef int coord;

typedef struct point
{
  coord x, y;
} point;

typedef struct box
{
  bool required;
  bool half;
  char title[TITLE_LEN + 1];
  char timetable[TIMETABLE_LEN + 1];
} box;

typedef struct mini
{
} mini;

typedef struct text
{
  char txt[LINE_LEN+1];
} text;

typedef char CourseCode[CODE_LEN + 1];
typedef enum node_type
{ BOX, MINI, TEXT } node_type;
typedef struct node
{
  point at;
  CourseCode code;
  node_type tag;
  union
  {
    box b;
    mini m;
    text t;
  } u;
} node;

typedef struct element *element_ptr;
typedef enum arrow_type
{ PREREQ, COREQ, RECOMM } arrow_type;
typedef struct arrow
{
  struct arrow *next;
  element_ptr source, target;
  coord curvature;		/* a negative value denotes the default curvature */
  arrow_type tag;
} arrow;

typedef enum element_type
{ NODE, ARROW } element_type;
typedef struct element
{
  element_ptr next;		/* to next element of linked list */
  element_type tag;
  union
  {
    node n;
    arrow a;
  } u;
} element;

extern char *optarg;
extern int optind;

extern void error (char msg[]);	/* abort with stderr message msg */
extern size_t strlcpy (char *s, const char *t, size_t n);
extern size_t strlcat (char *s, const char *t, size_t n);
extern int set_deftext (void);	/* used by readline */
extern pid_t pid;               /* process id for PDFVIEWER */

extern void regenerate_and_process (void);
extern void process_tex_file (void);
extern void create_backup (void);

extern void remove_write_access (void);
extern void restore_write_access (void);

extern element *first_node;
extern element *first_arrow;
extern element *first_cut;	/* stack of cut nodes */
extern element *first_deletion;	/* stack of deleted nodes/arrows */

extern bool grid;
extern bool useLatex;
extern bool reprocess;

extern char line[LINE_LEN];
extern char *command_line;
extern char *lp;		/* pointer into line or command_line */
extern char *cp;		/* pointer into text fields */

extern char filename[FILE_LEN];
extern FILE *tex_file, *pretext, *posttext, *comments;
extern char backup_filename[FILE_LEN + 1];
extern FILE *backup_tex_file;

extern char deftext[LINE_LEN];
/* buffer for default input; set using set_deftext */

/* defined in inout.c:  */

extern bool gt (point p, point q);
extern bool eq (point p, point q);

extern element *node_at (point p);
extern int insert_node (element * pc);
extern int insert_arrow (element * pa);

extern void free_lists (void);
extern void analyze_tex_file (void);

extern void copy (FILE * f, FILE * g);
extern void regenerate_tex_file (void);

/* defined in edit.c:  */

extern void quit (void);
extern void usage (void);
extern void help (void);

extern bool prefix (const char *cs, const char *ct);
extern bool suffix (const char *cs, const char *ct);

extern void analyze_user_command (void);
