/ [swis] / trunk / src / readname.c
To checkout: svn checkout http://svn.gnu.org.ua/sources/swis/trunk/src/readname.c
Puszcza

Contents of /trunk/src/readname.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations)
Thu Oct 4 12:10:46 2007 UTC (13 years, 8 months ago) by gray
File MIME type: text/plain
File size: 3659 byte(s)
src/version.c, src/readname.c: New files
src/word-split.c, src/html-strip.l: Add support for --from-file
(-T) option. 
Makefile.am (libswis.a): New goal
swis.h: Add new prototypes
gnulib.modules: Add obstack

1 /* This file is part of SWIS
2 Copyright (C) 2007 Sergey Poznyakoff
3
4 SWIS is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 SWIS is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with SWIS. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include "swis.h"
18
19 int filename_terminator = '\n';
20
21 enum read_file_list_state /* Result of reading file name from the list file */
22 {
23 file_list_success, /* OK, name read successfully */
24 file_list_end, /* End of list file */
25 file_list_zero, /* Zero separator encountered where it should not */
26 file_list_skip /* Empty (zero-length) entry encountered, skip it */
27 };
28
29 /* Read from FP a sequence of characters up to FILENAME_TERMINATOR and put them
30 into STK.
31 */
32 static enum read_file_list_state
33 read_name_from_file (FILE *fp, struct obstack *stk)
34 {
35 int c;
36 size_t counter = 0;
37
38 for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
39 {
40 if (c == 0)
41 {
42 /* We have read a zero separator. The file possibly is
43 zero-separated */
44 return file_list_zero;
45 }
46 obstack_1grow (stk, c);
47 counter++;
48 }
49
50 if (counter == 0 && c != EOF)
51 return file_list_skip;
52
53 obstack_1grow (stk, 0);
54
55 return (counter == 0 && c == EOF) ? file_list_end : file_list_success;
56 }
57
58 struct obstack argv_stk;
59 int stk_init;
60 size_t argcount;
61
62 void
63 read_names_from_file (const char *filename)
64 {
65 FILE *fp;
66 size_t count = 0, i;
67 enum read_file_list_state read_state;
68
69 if (!strcmp (filename, "-"))
70 {
71 // request_stdin ("-T");
72 fp = stdin;
73 }
74 else
75 {
76 if ((fp = fopen (filename, "r")) == NULL)
77 error (1, errno, "cannot open file %s", filename);
78 }
79
80 if (!stk_init)
81 {
82 stk_init = 1;
83 obstack_init (&argv_stk);
84 }
85
86 while ((read_state = read_name_from_file (fp, &argv_stk)) != file_list_end)
87 {
88 switch (read_state)
89 {
90 case file_list_success:
91 argcount++;
92 break;
93
94 case file_list_end: /* won't happen, just to pacify gcc */
95 break;
96
97 case file_list_zero:
98 {
99 size_t size;
100 char *p;
101
102 error (0, 0, "%s: file name read contains nul character",
103 filename);
104
105 /* Prepare new stack contents */
106 /* FIXME */
107 size = obstack_object_size (&argv_stk);
108 p = obstack_finish (&argv_stk);
109 for (i = 0; size > 0; size--, p++)
110 {
111 if (*p)
112 obstack_1grow (&argv_stk, *p);
113 else if (i < argcount)
114 i++;
115 else
116 obstack_1grow (&argv_stk, '\n');
117 }
118 obstack_1grow (&argv_stk, 0);
119 count = 1;
120 /* Read rest of files using new filename terminator */
121 filename_terminator = 0;
122 break;
123 }
124
125 case file_list_skip:
126 break;
127 }
128 }
129
130 fclose (fp);
131 argcount += count;
132 }
133
134 void
135 update_argcv (int *pargc, char ***pargv)
136 {
137 int i, new_argc;
138 char **new_argv;
139 char *p, *start;
140
141 new_argc = *pargc + argcount + 1;
142 new_argv = xmalloc (sizeof (new_argv[0]) * (new_argc + 1));
143 memcpy (new_argv, *pargv, sizeof (new_argv[0]) * *pargc);
144
145 start = obstack_finish (&argv_stk);
146 for (i = *pargc, p = start; *p; p += strlen (p) + 1, i++)
147 new_argv[i] = xstrdup (p);
148 new_argv[i] = NULL;
149
150 obstack_free (&argv_stk, NULL);
151
152 *pargv = new_argv;
153 *pargc = new_argc;
154 }

Send suggestions and bug reports to Sergey Poznyakoff
ViewVC Help
Powered by ViewVC 1.1.20