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

#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE (!0)
#endif
typedef struct scrap_node {
  struct scrap_node *next;
  int scrap;
} Scrap_Node;
typedef struct name {
  char *spelling;
  struct name *llink;
  struct name *rlink;
  Scrap_Node *defs;
  Scrap_Node *uses;
  int mark;
  char tab_flag;
  char indent_flag;
  char debug_flag;
} Name;

int tex_flag;      /* if FALSE, don't emit the .tex file */
int output_flag;   /* if FALSE, don't emit the output files */
int compare_flag;  /* if FALSE, overwrite without comparison */
char *command_name;
char *source_name;  /* name of the current file */
int source_line;    /* current line in the source file */
Name *file_names;
Name *macro_names;
Name *user_names;

void pass1();
void write_tex();
void write_files();
void source_open(); /* pass in the name of the source file */
int source_get();   /* no args; returns the next char or EOF */
void init_scraps();
int collect_scrap();
int write_scraps();
Name *collect_file_name();
Name *collect_macro_name();
Name *collect_scrap_name();
Name *name_add();
Name *prefix_add();
char *save_string();
void reverse_lists();
void *arena_getmem();
void arena_free();


void main(argc, argv)
     int argc;
     char **argv;
{
  int arg = 1;
  tex_flag = TRUE;
  output_flag = TRUE;
  compare_flag = TRUE;
  command_name = argv[0];
  while (arg < argc) {
    char *s = argv[arg];
    if (*s++ == '-') {
      {
        char c = *s++;
        while (c) {
          switch (c) {
            case 'c': compare_flag = FALSE;
                      break;
            case 'o': output_flag = FALSE;
                      break;
            case 't': tex_flag = FALSE;
                      break;
            case 'v': printf("nuweb version 0.6\n");
                      break;
            default:  fprintf(stderr, "%s: unexpected argument ignored.  ",
                              command_name);
                      fprintf(stderr, "Usage is: %s [-cot] file...\n",
                              command_name);
                      break;
          }
          c = *s++;
        }
      }
      arg++;
    }
    else break;
  }
  if (arg < argc)
    do {
      {
        char source_name[100];
        char tex_name[100];
        {
          char *p = argv[arg];
          char *q = source_name;
          char *trim = q;
          char *dot = NULL;
          char c = *p++;
          while (c) {
            *q++ = c;
            if (c == '/') {
              trim = q;
              dot = NULL;
            }
            else if (c == '.')
              dot = q - 1;
            c = *p++;
          }
          *q = '\0';
          if (dot) {
            *dot = '\0';
            sprintf(tex_name, "%s.tex", trim);
            *dot = '.';
          }
          else {
            sprintf(tex_name, "%s.tex", trim);
            *q++ = '.';
            *q++ = 'w';
            *q = '\0';
          }
        }
        {
          pass1(source_name);
          if (tex_flag)
            write_tex(source_name, tex_name);
          if (output_flag)
            write_files(file_names);
          arena_free();
        }
      }
      arg++;
    } while (arg < argc);
  else {
    fprintf(stderr, "%s: expected a file name.  ", command_name);
    fprintf(stderr, "Usage is: %s [-cot] file-name...\n", command_name);
    exit(-1);
  }
  exit(0);
}