Home | History | Annotate | Line # | Download | only in msgc
      1  1.24  christos /*	$NetBSD: msgdb.c,v 1.24 2019/06/20 00:45:18 christos Exp $	*/
      2   1.1      phil 
      3   1.1      phil /*
      4   1.1      phil  * Copyright 1997 Piermont Information Systems Inc.
      5   1.1      phil  * All rights reserved.
      6   1.1      phil  *
      7   1.1      phil  * Written by Philip A. Nelson for Piermont Information Systems Inc.
      8   1.1      phil  *
      9   1.1      phil  * Redistribution and use in source and binary forms, with or without
     10   1.1      phil  * modification, are permitted provided that the following conditions
     11   1.1      phil  * are met:
     12   1.1      phil  * 1. Redistributions of source code must retain the above copyright
     13   1.1      phil  *    notice, this list of conditions and the following disclaimer.
     14   1.1      phil  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      phil  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      phil  *    documentation and/or other materials provided with the distribution.
     17  1.23   mbalmer  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
     18   1.1      phil  *    or promote products derived from this software without specific prior
     19   1.1      phil  *    written permission.
     20   1.1      phil  *
     21   1.1      phil  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     22   1.1      phil  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1      phil  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.21   mbalmer  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     25  1.21   mbalmer  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.21   mbalmer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1      phil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1      phil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1      phil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.21   mbalmer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31   1.1      phil  * THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1      phil  *
     33   1.1      phil  */
     34   1.1      phil 
     35   1.1      phil /* mdb.c - message database manipulation */
     36  1.16       agc 
     37  1.20       jmc #if HAVE_NBTOOL_CONFIG_H
     38  1.20       jmc #include "nbtool_config.h"
     39  1.20       jmc #endif
     40  1.20       jmc 
     41  1.16       agc #include <sys/cdefs.h>
     42  1.16       agc 
     43  1.18     lukem #if defined(__RCSID) && !defined(lint)
     44  1.24  christos __RCSID("$NetBSD: msgdb.c,v 1.24 2019/06/20 00:45:18 christos Exp $");
     45  1.16       agc #endif
     46  1.16       agc 
     47   1.1      phil 
     48   1.1      phil #include <stdio.h>
     49   1.1      phil #include <stdlib.h>
     50   1.2     enami #include <string.h>
     51   1.2     enami 
     52   1.1      phil #include "defs.h"
     53  1.12     bjh21 #include "pathnames.h"
     54   1.1      phil 
     55   1.1      phil static struct id_rec *head = NULL, *tail = NULL;
     56  1.15       dsl static int msg_no = 1;
     57   1.1      phil 
     58   1.1      phil void define_msg (char *name, char *value)
     59   1.1      phil {
     60   1.1      phil 	struct id_rec *tmp = (struct id_rec *)malloc(sizeof(struct id_rec));
     61   1.1      phil 
     62   1.1      phil 	if (find_id (root, name))
     63   1.1      phil 		yyerror ("%s is defined twice", name);
     64   1.1      phil 
     65   1.1      phil 	tmp->id     = name;
     66   1.1      phil 	tmp->msg    = value;
     67   1.1      phil 	tmp->msg_no = msg_no++;
     68   1.1      phil 	tmp->next   = NULL;
     69   1.1      phil 	if (tail == NULL)
     70   1.1      phil 		head = tail = tmp;
     71   1.1      phil 	else {
     72   1.1      phil 		tail->next = tmp;
     73   1.1      phil 		tail = tmp;
     74   1.1      phil 	}
     75   1.1      phil 
     76   1.1      phil 	insert_id (&root, tmp);
     77   1.1      phil }
     78   1.1      phil 
     79   1.1      phil static void write_str (FILE *f, char *str)
     80   1.1      phil {
     81   1.1      phil 	(void)fprintf (f, "\"");
     82   1.1      phil 	while (*str) {
     83   1.1      phil 		if (*str == '\n')
     84   1.1      phil 			(void) fprintf (f, "\\n\"\n\""), str++;
     85   1.1      phil 		else if (*str == '"')
     86   1.1      phil 			(void) fprintf (f, "\\\""), str++;
     87   1.1      phil 		else
     88   1.1      phil 			(void) fprintf (f, "%c", *str++);
     89   1.1      phil 	}
     90   1.1      phil 	(void)fprintf (f, "\",");
     91   1.1      phil }
     92   1.1      phil 
     93   1.1      phil /* Write out the msg files. */
     94   1.1      phil void
     95   1.1      phil write_msg_file ()
     96   1.1      phil {
     97   1.1      phil 	FILE *out_file;
     98   1.1      phil 	FILE *sys_file;
     99   1.1      phil 	char hname[1024];
    100   1.1      phil 	char cname[1024];
    101   1.1      phil 	char sname[1024];
    102   1.1      phil 	char *sys_prefix;
    103   1.1      phil 
    104   1.1      phil 	int nlen;
    105   1.1      phil 	int ch;
    106   1.1      phil 
    107   1.1      phil 	struct id_rec *t;
    108   1.1      phil 
    109   1.1      phil 	/* Generate file names */
    110   1.1      phil 	snprintf (hname, 1024, "%s.h", out_name);
    111   1.1      phil 	nlen = strlen(hname);
    112   1.1      phil 	if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
    113   1.1      phil 		(void) fprintf (stderr, "%s: name `%s` too long.\n",
    114   1.1      phil 				prog_name, out_name);
    115   1.1      phil 		exit(1);
    116   1.1      phil 	}
    117   1.1      phil 	snprintf (cname, 1024, "%s.c", out_name);
    118   1.1      phil 
    119   1.1      phil 	/* Open the msg_sys file first. */
    120   1.1      phil 	sys_prefix = getenv ("MSGDEF");
    121   1.1      phil 	if (sys_prefix == NULL)
    122  1.12     bjh21 		sys_prefix = _PATH_DEFSYSPREFIX;
    123   1.1      phil 	snprintf (sname, 1024, "%s/%s", sys_prefix, sys_name);
    124   1.1      phil 	sys_file = fopen (sname, "r");
    125   1.1      phil 	if (sys_file == NULL) {
    126   1.1      phil 		(void) fprintf (stderr, "%s: could not open %s.\n",
    127   1.1      phil 				prog_name, sname);
    128   1.1      phil 		exit (1);
    129   1.1      phil 	}
    130   1.1      phil 
    131   1.1      phil 	/* Output the .h file first. */
    132   1.1      phil 	out_file = fopen (hname, "w");
    133   1.1      phil 	if (out_file == NULL) {
    134   1.1      phil 		(void) fprintf (stderr, "%s: could not open %s.\n",
    135   1.1      phil 				prog_name, hname);
    136   1.1      phil 		exit (1);
    137   1.1      phil 	}
    138   1.1      phil 
    139   1.1      phil 	/* Write it */
    140   1.1      phil 	(void) fprintf (out_file, "%s",
    141   1.1      phil 		"/* msg system definitions. */\n"
    142   1.1      phil 		"\n"
    143   1.1      phil 		"#ifndef MSG_DEFS_H\n"
    144   1.1      phil 		"#define MSG_DEFS_H\n"
    145   1.1      phil 		"#include <stdio.h>\n"
    146   1.4       cgd 		"#include <stdlib.h>\n"
    147  1.17       dsl 		"#include <unistd.h>\n"
    148  1.17       dsl 		"#include <fcntl.h>\n"
    149   1.1      phil 		"#include <string.h>\n"
    150   1.1      phil 		"#include <ctype.h>\n"
    151   1.1      phil 		"#include <stdarg.h>\n"
    152  1.13  christos 		"#include <stdint.h>\n"
    153   1.1      phil 		"#include <curses.h>\n"
    154  1.17       dsl 		"#include <sys/mman.h>\n"
    155   1.1      phil 		"\n"
    156  1.10       cgd 		"typedef const char *msg;\n"
    157  1.10       cgd 		"\n"
    158   1.1      phil 		"/* Prototypes */\n"
    159  1.15       dsl 		"WINDOW *msg_window(WINDOW *window);\n"
    160  1.17       dsl 		"const char *msg_string(msg msg_no);\n"
    161  1.17       dsl 		"int msg_file(const char *);\n"
    162   1.1      phil 		"void msg_clear(void);\n"
    163   1.1      phil 		"void msg_standout(void);\n"
    164   1.1      phil 		"void msg_standend(void);\n"
    165  1.22   mbalmer 		"void msg_printf(const char *fmt, ...) __printflike(1, 2);\n"
    166  1.24  christos 		"void msg_display(msg msg_no);\n"
    167  1.24  christos 		"void msg_fmt_display(msg msg_no, const char *fmt, ...)"
    168  1.24  christos 			" __printflike(2, 3);\n"
    169  1.24  christos 
    170  1.24  christos 		"void msg_display_add(msg msg_no);\n"
    171  1.24  christos 		"void msg_fmt_display_add(msg msg_no, const char *fmt, ...);\n"
    172  1.24  christos 
    173  1.24  christos 		"void msg_prompt(msg msg_no, const char *def,"
    174  1.24  christos 			" char *val, size_t max_chars);\n"
    175  1.24  christos 		"void msg_fmt_prompt(msg msg_no, const char *def,"
    176  1.24  christos 			" char *val, size_t max_chars, const char *fmt, ...)"
    177  1.24  christos 			" __printflike(5, 6);\n"
    178  1.24  christos 
    179  1.24  christos 		"void msg_prompt_add(msg msg_no, const char *def,"
    180  1.24  christos 			" char *val, size_t max_chars);\n"
    181  1.24  christos 		"void msg_fmt_prompt_add(msg msg_no, const char *def,"
    182  1.24  christos 			" char *val, size_t max_chars, const char *fmt, ...)"
    183  1.24  christos 			" __printflike(5, 6);\n"
    184  1.24  christos 
    185  1.24  christos 		"void msg_prompt_noecho(msg msg_no, const char *def,"
    186  1.24  christos 			" char *val, size_t max_chars);\n"
    187  1.24  christos 		"void msg_fmt_prompt_noecho(msg msg_no, const char *def,"
    188  1.24  christos 			" char *val, size_t max_chars, const char *fmt, ...)"
    189  1.24  christos 			" __printflike(5, 6);\n"
    190  1.24  christos 
    191  1.24  christos 		"void msg_prompt_win(msg msg_no, int x, int y, int w,"
    192  1.24  christos 			" int h, const char *def, char *val,"
    193  1.24  christos 			" size_t max_chars);\n"
    194  1.24  christos 		"void msg_fmt_prompt_win(msg msg_no, int x, int y, int w,"
    195  1.24  christos 			" int h, const char *def, char *val, size_t max_chars,"
    196  1.24  christos 			" const char *fmt, ...)"
    197  1.24  christos 			" __printflike(9, 10);\n"
    198  1.24  christos 
    199  1.24  christos 		"void msg_table_add(msg msg_no);"
    200  1.24  christos 		"void msg_fmt_table_add(msg msg_no, const char *fmt, ...)"
    201  1.24  christos 			" __printflike(2, 3);\n"
    202  1.24  christos 
    203  1.19       dsl 		"int msg_row(void);\n"
    204   1.1      phil 		"\n"
    205   1.1      phil 		"/* Message names */\n"
    206   1.1      phil 	      );
    207  1.10       cgd 	(void) fprintf (out_file, "#define MSG_NONE\tNULL\n");
    208   1.1      phil 	for (t=head; t != NULL; t = t->next) {
    209  1.10       cgd 		(void) fprintf (out_file, "#define MSG_%s\t((msg)(long)%d)\n",
    210   1.1      phil 				t->id, t->msg_no);
    211   1.1      phil 	}
    212   1.5       cgd 	(void) fprintf (out_file, "\n#endif\n");
    213   1.1      phil 
    214   1.1      phil 	fclose (out_file);
    215   1.1      phil 
    216   1.1      phil 	/* Now the C file */
    217   1.1      phil 	out_file = fopen (cname, "w");
    218   1.1      phil 	if (out_file == NULL) {
    219   1.1      phil 		(void) fprintf (stderr, "%s: could not open %s.\n",
    220   1.1      phil 				prog_name, cname);
    221   1.1      phil 		exit (1);
    222   1.1      phil 	}
    223   1.1      phil 
    224   1.1      phil 	/* hfile include ... */
    225   1.1      phil 	(void)fprintf (out_file, "#include \"%s\"\n", hname);
    226   1.1      phil 
    227   1.1      phil 	/* msg_list */
    228  1.15       dsl 	(void)fprintf (out_file, "const char *msg_list[] = {\nNULL,\n");
    229  1.21   mbalmer 	for (t=head ; t != NULL; t = t->next)
    230   1.1      phil 		write_str (out_file, t->msg);
    231   1.1      phil 	(void)fprintf (out_file, "NULL};\n");
    232   1.1      phil 
    233   1.1      phil 	/* sys file out! */
    234   1.1      phil 	while ((ch = fgetc(sys_file)) != EOF)
    235  1.21   mbalmer 		fputc(ch, out_file);
    236  1.21   mbalmer 
    237   1.1      phil 	fclose (out_file);
    238   1.1      phil 	fclose (sys_file);
    239   1.1      phil }
    240