~ubuntu-branches/debian/squeeze/alpine/squeeze

« back to all changes in this revision

Viewing changes to pith/help_c_gen.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ========================================================================
 
3
 * Copyright 2006-2007 University of Washington
 
4
 *
 
5
 * Licensed under the Apache License, Version 2.0 (the "License");
 
6
 * you may not use this file except in compliance with the License.
 
7
 * You may obtain a copy of the License at
 
8
 *
 
9
 *     http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * ========================================================================
 
12
 */
 
13
 
 
14
#include <stdio.h>
 
15
#include <string.h>
 
16
 
 
17
 
 
18
typedef struct helplist {
 
19
    char            *name;
 
20
    struct helplist *next;
 
21
} HELPLIST_S;
 
22
 
 
23
 
 
24
HELPLIST_S *help_list;
 
25
 
 
26
 
 
27
void  preamble(FILE *ofp);
 
28
void  body(FILE *ifp, FILE *ofp);
 
29
char *quote_clean(char *rawline);
 
30
int   only_tags(char *line);
 
31
int   append_to_help_list(HELPLIST_S **, char *new);
 
32
void  print_help_list(HELPLIST_S *, FILE *fp);
 
33
 
 
34
 
 
35
int
 
36
main(int argc, char **argv)
 
37
{
 
38
    preamble(stdout);
 
39
    body(stdin, stdout);
 
40
}
 
41
 
 
42
 
 
43
void
 
44
preamble(FILE *ofp)
 
45
{
 
46
    fprintf(ofp, "\n\t\t/*\n");
 
47
    fprintf(ofp, "\t\t * AUTMATICALLY GENERATED FILE!\n");
 
48
    fprintf(ofp, "\t\t * DO NOT EDIT!!\n");
 
49
    fprintf(ofp, "\t\t * See help_c_gen.c.\n\t\t */\n\n\n");
 
50
    fprintf(ofp, "#include <stdio.h>\n#include \"headers.h\"\n#include \"helptext.h\"\n\n");
 
51
}
 
52
 
 
53
 
 
54
void
 
55
body(FILE *ifp, FILE *ofp)
 
56
{
 
57
    char  rawline[10000];
 
58
    char *line;
 
59
#define SPACE ' '
 
60
    char *space = " ";
 
61
    char *p, *helpname;
 
62
    int   in_text = 0, new_topic = 0, first_one = 1, justtags;
 
63
 
 
64
    while(fgets(rawline, sizeof(rawline), ifp) != NULL){
 
65
        if(rawline[0] == '#')
 
66
          continue;
 
67
 
 
68
        line = quote_clean(rawline);
 
69
 
 
70
        if(!line){
 
71
            /*
 
72
             * Put errors in result so that it will cause a compile
 
73
             * error and be noticed.
 
74
             */
 
75
            fprintf(ofp, "Error: quote_clean returns NULL for help line\n  %s\n", rawline);
 
76
            exit(-1);
 
77
        }
 
78
 
 
79
        justtags = 0;
 
80
        if(!strncmp(line, "====", 4)){
 
81
            p = line;
 
82
            /* skip to first space */
 
83
            while(*p && *p != SPACE)
 
84
              p++;
 
85
 
 
86
            if(!*p){
 
87
                fprintf(ofp, "Error: help input line\n  %s\n No space after ====\n", rawline);
 
88
                exit(-1);
 
89
            }
 
90
 
 
91
            /* skip spaces */
 
92
            while(*p && *p == SPACE)
 
93
              p++;
 
94
 
 
95
            if(!*p){
 
96
                fprintf(ofp, "Error: help input line\n  %s\n Missing helpname after ====\n", rawline);
 
97
                exit(-1);
 
98
            }
 
99
 
 
100
            helpname = p;
 
101
 
 
102
            /* skip to next space */
 
103
            while(*p && *p != SPACE)
 
104
              p++;
 
105
 
 
106
            *p = '\0';          /* tie off helpname */
 
107
 
 
108
            /* finish previous one */
 
109
            if(in_text)
 
110
              fprintf(ofp, "NULL\n};\n\n\n");
 
111
 
 
112
            in_text = new_topic = 1;
 
113
 
 
114
            fprintf(ofp, "char *%s[] = {\n", helpname);
 
115
 
 
116
            if(append_to_help_list(&help_list, helpname) < 0){
 
117
                fprintf(ofp, "Error: Can't allocate memory for help_list after line\n  %s\n", rawline);
 
118
                exit(-1);
 
119
            }
 
120
        }
 
121
        else if(line[0] == '\0'){
 
122
            if(in_text)
 
123
              fprintf(ofp, "\" \",\n");         /* why the space? */
 
124
        }
 
125
        else if(only_tags(line)){
 
126
            if(in_text){
 
127
                fprintf(ofp, "\"%s\",\n", line);
 
128
                justtags = 1;
 
129
            }
 
130
        }
 
131
 
 
132
        if(line[0] && line[0] != '='){
 
133
            if(in_text && !justtags){
 
134
                if(first_one){
 
135
                    first_one = 0;
 
136
                    fprintf(ofp, "/*\n");
 
137
                    fprintf(ofp, "TRANSLATORS: The translation strings for pith/helptext.c\n");
 
138
                    fprintf(ofp, "are automatically generated by a script from the help\n");
 
139
                    fprintf(ofp, "text in pith/pine.hlp. This means that the translation job for\n");
 
140
                    fprintf(ofp, "the help text is particularly difficult.\n");
 
141
                    fprintf(ofp, "This is HTML source so please leave the text inside HTML tags untranslated.\n");
 
142
                    fprintf(ofp, "HTML tags like <LI> or <TITLE> should, of course, be left untranslated.\n");
 
143
                    fprintf(ofp, "Special HTML characters like &lt; (less than character) should be left alone.\n");
 
144
                    fprintf(ofp, "Alpine option names are short phrases with the words separated by\n");
 
145
                    fprintf(ofp, "dashes. An example of an option name is Quell-Extra-Post-Prompt.\n");
 
146
                    fprintf(ofp, "Option names should not be translated.\n");
 
147
                    fprintf(ofp, "The file pith/helptext.c contains many separate help topics.\n");
 
148
                    fprintf(ofp, "Some of them are very short and some are long. If left unsorted the\n");
 
149
                    fprintf(ofp, "text for a single topic is together in the translation file. The start\n");
 
150
                    fprintf(ofp, "of each new topic is marked by the comment\n");
 
151
                    fprintf(ofp, "TRANSLATORS: Start of new help topic.\n");
 
152
                    fprintf(ofp, "*/\n");
 
153
                }
 
154
                else if(new_topic){
 
155
                    new_topic = 0;
 
156
                    fprintf(ofp, "/* TRANSLATORS: Start of new help topic. */\n");
 
157
                }
 
158
 
 
159
                fprintf(ofp, "N_(\"%s\"),\n", line);
 
160
            }
 
161
            else{
 
162
                ; /* skip leading cruft */
 
163
            }
 
164
        }
 
165
    }
 
166
 
 
167
    if(in_text)
 
168
      fprintf(ofp, "NULL\n};\n\n\n");
 
169
 
 
170
    print_help_list(help_list, ofp);
 
171
}
 
172
 
 
173
 
 
174
char *
 
175
quote_clean(char *rawline)
 
176
{
 
177
    char *p, *q, *cleaned = NULL;
 
178
    size_t len;
 
179
 
 
180
    if(rawline){
 
181
        len = strlen(rawline);
 
182
        cleaned = (char *) malloc((2*len+1) * sizeof(char));
 
183
 
 
184
        if(cleaned){
 
185
            p = rawline;
 
186
            q = cleaned;
 
187
 
 
188
            while(*p && *p != '\n'){
 
189
                if(*p == '"' && !(p > rawline && *(p-1) == '\\'))
 
190
                  *q++ = '\\';
 
191
 
 
192
                *q++ = *p++;
 
193
            }
 
194
 
 
195
            *q = '\0';
 
196
        }
 
197
    }
 
198
 
 
199
    return cleaned;
 
200
}
 
201
 
 
202
 
 
203
int
 
204
only_tags(char *line)
 
205
{
 
206
    char *p;
 
207
    int   is_tags = 1;  /* only tags seen so far */
 
208
 
 
209
    if(!line)
 
210
      return 0;
 
211
 
 
212
    p = line;
 
213
 
 
214
    while(is_tags && *p){
 
215
        /* leading space before a tag */
 
216
        while(*p && isspace(*p))
 
217
          p++;
 
218
 
 
219
        if(*p == '<'){
 
220
            p++;
 
221
            /* skip through interior of tag */
 
222
            while(*p && *p != '<' && *p != '>')
 
223
              p++;
 
224
 
 
225
            if(*p == '>'){
 
226
                p++;
 
227
                /* trailing space after tag */
 
228
                while(*p && isspace(*p))
 
229
                  p++;
 
230
            }
 
231
            else
 
232
              is_tags = 0;
 
233
        }
 
234
        else if(*p)
 
235
          is_tags = 0;
 
236
    }
 
237
 
 
238
    return is_tags;
 
239
}
 
240
 
 
241
 
 
242
int
 
243
append_to_help_list(HELPLIST_S **head, char *name)
 
244
{
 
245
    HELPLIST_S *new, *h;
 
246
    size_t len;
 
247
 
 
248
    if(!(name && *name && head))
 
249
      return 0;
 
250
 
 
251
    new = (HELPLIST_S *) malloc(sizeof(*new));
 
252
    if(!new)
 
253
      return -1;
 
254
 
 
255
    memset(new, 0, sizeof(*new));
 
256
    len = strlen(name);
 
257
    new->name = (char *) malloc((len+1) * sizeof(char));
 
258
    strncpy(new->name, name, len);
 
259
    new->name[len] = '\0';
 
260
 
 
261
    if(*head){
 
262
        for(h = *head; h->next; h = h->next)
 
263
          ;
 
264
 
 
265
        h->next = new;
 
266
    }
 
267
    else
 
268
      *head = new;
 
269
 
 
270
    return 0;
 
271
}
 
272
 
 
273
 
 
274
void
 
275
print_help_list(HELPLIST_S *head, FILE *fp)
 
276
{
 
277
    HELPLIST_S *h;
 
278
 
 
279
    if(head){
 
280
        fprintf(fp, "struct help_texts h_texts[] = {\n");
 
281
 
 
282
        for(h = head; h; h = h->next)
 
283
          if(h->name && h->name[0])
 
284
            fprintf(fp, "{%s,\"%s\"},\n", h->name, h->name);
 
285
 
 
286
        fprintf(fp, "{NO_HELP, NULL}\n};\n");
 
287
    }
 
288
}