Subversion Repositories SvarDOS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2019 mateusz.vi 1
/* Functions that emulate UNIX catgets */
2
 
3
/* Copyright (C) 1999,2000 Jim Hall <jhall1@isd.net> */
4
 
5
/*
6
  This library is free software; you can redistribute it and/or
7
  modify it under the terms of the GNU Lesser General Public
8
  License as published by the Free Software Foundation; either
9
  version 2.1 of the License, or (at your option) any later version.
10
 
11
  This library is distributed in the hope that it will be useful,
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
  Lesser General Public License for more details.
15
 
16
  You should have received a copy of the GNU Lesser General Public
17
  License along with this library; if not, write to the Free Software
18
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
*/
20
 
21
 
22
#include <stdio.h>			/* sprintf */
23
#include <stdlib.h>			/* getenv  */
24
#include <string.h>			/* strtok, strchr */
25
 
26
/* #include <dir.h>			/* fnmerge */
27
 
28
#include "db.h"
29
#include "catgets.h"
30
 
31
 
32
/* External functions */
33
 
34
char *get_line (FILE *pfile, int continue_ch);
35
 
36
 
37
/* Local prototypes */
38
 
39
int _catread (char *catfile);		/* Reads a catfile into the hash */
40
 
41
 
42
/* Globals */
43
 
44
nl_catd catalog = 0;			/* Catalog descriptor, either 0 or 1 */
45
 
46
 
47
/* Functions */
48
 
49
char *
50
catgets(nl_catd  cat,  int set_number, int message_number,
51
	 char *message)
52
{
53
  /* get message from a message catalog */
54
 
55
  /* 'message' should really be const, but not when it is returned */
56
 
57
  /* On success, catgets() returns a pointer to an internal buffer
58
     area containing the null-terminated message string.  On failure,
59
     catgets() returns the value 'message'.  */
60
 
61
  char key[10];
62
  db_t *ptr;
63
 
64
  /* Is this the same catalog we have in memory? */
65
 
66
  if (cat != catalog)
67
    {
68
      return (message);
69
    }
70
 
71
  /* fetch the message that goes with the set/message number */
72
 
73
  sprintf (key, "%d.%d", set_number, message_number);
74
  ptr = db_fetch (key);
75
 
76
  if (ptr)
77
    {
78
      return (ptr->value);
79
    }
80
 
81
  /* else */
82
 
83
  return (message);
84
}
85
 
86
nl_catd
87
catopen(char *name, int flag)
88
{
89
  /* catopen() returns a message catalog descriptor of type nl_catd on
90
     success.  On failure, it returns -1. */
91
 
92
  /* 'flag' is completely ignored. */
93
 
94
  /* I'm not sure 128 is a good value to use here.  Would MAXPATH be
95
     better, from dir.h? */
96
 
97
  char catfile[128];			/* full path to the msg catalog */
98
  char nlspath[128];                    /* value of %NLSPATH% */
99
  char nlspath_lang[128];               /* value of %NLSPATH%\%LANG% */
100
  char *nlsptr;				/* ptr to NLSPATH */
101
  char *lang;                           /* ptr to LANG */
102
  char lang_2[3];                       /* 2-char version of %LANG% */
103
  char *tok;                            /* pointer when using strtok */
104
 
105
  /* Open the catalog file */
106
 
107
  /* The value of `catalog' will be set based on _catread */
108
 
109
  if (catalog)
110
    {
111
      /* Already one open */
112
 
113
      return (-1);
114
    }
115
 
116
  /* If the message catalog file name contains a directory separator,
117
     assume that this is a real path to the catalog file.  Note that
118
     _catread will return a true or false value based on its ability
119
     to read the catfile. */
120
 
121
  if (strchr (name, '\\'))
122
    {
123
      /* first approximation: 'name' is a filename */
124
 
125
      strcpy (catfile, name);
126
      catalog = _catread (catfile);
127
      return (catalog);
128
    }
129
 
130
  /* If the message catalog file name does not contain a directory
131
     separator, then we need to try to locate the message catalog on
132
     our own.  We will use several methods to find it. */
133
 
134
  /* We will need the value of LANG, and may need a 2-letter abbrev of
135
     LANG later on, so get it now. */
136
 
137
  lang = getenv ("LANG");
138
 
139
  if (lang == NULL)
140
    {
141
      /* Return failure - we won't be able to locate the cat file */
142
      return (-1);
143
    }
144
 
145
  strncpy (lang_2, lang, 2);
146
  lang_2[2] = '\0';
147
 
148
  /* step through NLSPATH */
149
 
150
  nlsptr = getenv ("NLSPATH");
151
 
152
  if (nlsptr == NULL)
153
    {
154
      /* Return failure - we won't be able to locate the cat file */
155
      return (-1);
156
    }
157
 
158
  strcpy (nlspath, nlsptr);
159
 
160
  tok = strtok (nlspath, ";");
161
  while (tok != NULL)
162
    {
163
      /* Try to find the catalog file in each path from NLSPATH */
164
 
165
      /* Rule #1: %NLSPATH%\%LANG%\cat */
166
 
167
      strcpy (nlspath_lang, nlspath);
168
      strcat (nlspath_lang, "\\");
169
      strcat (nlspath_lang, lang);
170
 
171
      _makepath (catfile, NULL, nlspath_lang, name, NULL);
172
      catalog = _catread (catfile);
173
      if (catalog)
174
	{
175
	  return (catalog);
176
	}
177
 
178
      /* Rule #2: %NLSPATH%\cat.%LANG% */
179
 
180
      _makepath (catfile, NULL, tok, name, lang);
181
      catalog = _catread (catfile);
182
      if (catalog)
183
	{
184
	  return (catalog);
185
	}
186
 
187
      /* Rule #3: if LANG looks to be in format "en-uk" then
188
         %NLSPATH%\cat.EN */
189
 
190
      if (lang[2] == '-')
191
        {
192
          _makepath (catfile, NULL, tok, name, lang_2);
193
	  catalog = _catread (catfile);
194
	  if (catalog)
195
	    {
196
	      return (catalog);
197
	    }
198
        }
199
 
200
      /* Grab next tok for the next while iteration */
201
 
202
      tok = strtok (NULL, ";");
203
    } /* while tok */
204
 
205
  /* We could not find it.  Return failure. */
206
 
207
  return (0);
208
}
209
 
210
int
211
_catread (char *catfile)
212
{
213
  FILE *pfile;				/* pointer to the catfile */
214
  char *key;				/* part of key-value for hash */
215
  char *value;				/* part of key-value for hash */
216
  char *str;                            /* the string read from the file */
217
 
218
  /* Open the catfile for reading */
219
 
220
  pfile = fopen (catfile, "r");
221
  if (!pfile)
222
    {
223
      /* Cannot open the file.  Return failure */
224
      return (0);
225
    }
226
 
227
  /* Read the file into memory */
228
 
229
  while ((str = get_line (pfile, 0)) != NULL)
230
    {
231
      /* Break into parts.  Entries should be of the form:
232
	 "1.2:This is a message" */
233
 
234
      /* A line that starts with '#' is considered a comment, and will
235
         be thrown away without reading it. */
236
 
237
      /* Assumes no blank lines */
238
 
239
      if (str[0] != '#')
240
	{
241
	  key = strtok (str, ":");
242
	  value = strtok (NULL, "\n");
243
 
244
	  db_insert (key, value);
245
	} /* if comment */
246
 
247
      free (str);
248
    } /* while */
249
 
250
  fclose (pfile);
251
 
252
  /* Return success */
253
 
254
  return (1);
255
}
256
 
257
void
258
catclose (nl_catd cat)
259
{
260
  /* close a message catalog */
261
 
262
  catalog = 0;
263
}