summaryrefslogtreecommitdiff
path: root/web/nuweb/msdos/output.c
blob: 150722ca71d5a7644c88cd3912476ae481b3d442 (plain)
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
#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 write_files(files)
     Name *files;
{
  while (files) {
    write_files(files->llink);
    {
      FILE *temp_file;
      char temp_name[12]="nuweb~~~.tmp";
    /*  sprintf(temp_name, "%s~", files->spelling);  */
      temp_file = fopen(temp_name, "w");
      if (temp_file) {
        char indent_chars[500];
        write_scraps(temp_file, files->defs, 0, indent_chars,
                     files->debug_flag, files->tab_flag, files->indent_flag);
        fclose(temp_file);
        if (compare_flag)
          {
            FILE *old_file = fopen(files->spelling, "r");
            if (old_file) {
              int x, y;
              temp_file = fopen(temp_name, "r");
              do {
                x = getc(old_file);
                y = getc(temp_file);
              } while (x == y && x != EOF);
              fclose(old_file);
              fclose(temp_file);
              if (x == y)
                unlink(temp_name);
              else
                rename(temp_name, files->spelling);
            }
            else
              rename(temp_name, files->spelling);
          }
        else
          rename(temp_name, files->spelling);
      }
      else {
        fprintf(stderr, "%s: can't open %s\n", command_name, temp_name);
        exit(-1);
      }
    }
    files = files->rlink;
  }
}