home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / nls / genlib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-08  |  19.2 KB  |  898 lines

  1. /* -*-c++-*- */
  2.  
  3.  
  4. /***********************************************************
  5. Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
  6.  
  7.                         All Rights Reserved
  8.  
  9. Permission to use, copy, modify, and distribute this software and its
  10. documentation for any purpose and without fee is hereby granted,
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in
  13. supporting documentation, and that Alfalfa's name not be used in
  14. advertising or publicity pertaining to distribution of the software
  15. without specific, written prior permission.
  16.  
  17. ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  18. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  19. ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  20. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. If you make any modifications, bugfixes or other changes to this software
  26. we'd appreciate it if you could send a copy to us so we can keep things
  27. up-to-date.  Many thanks.
  28.                 Kee Hinckley
  29.                 Alfalfa Software, Inc.
  30.                 267 Allston St., #3
  31.                 Cambridge, MA 02139  USA
  32.                 nazgul@alfalfa.com
  33.     
  34. ******************************************************************/
  35.  
  36. /* Edit History
  37.  
  38. 12/03/82     nazgul    Patch from <Jan.Djarv@sa.erisoft.se>.  This may fix
  39.             the problem with updating sets.
  40. 10/18/92   3 schulert    actually put in the changes described in last edit.
  41. 10/18/92   7 nazgul    Changes from gbooman@feds1.Prime.COM (Gordon Booman)
  42.             1) Support backslash quoted quotes in message file text.
  43.             2) Correctly indicate error location in messages that have tabs.
  44.             3) Fixed a misspelling.
  45. 04/15/91   6 nazgul    Check for byte order first
  46. 02/25/91   5 nazgul    Added flag for MS byteorder
  47. 01/14/91   4 nazgul    Off by one on number specified entries
  48. */
  49.  
  50. #include <stdio.h>
  51. #ifdef SYSV
  52. #include <sys/types.h>
  53. #include <unistd.h>
  54.  
  55. #ifndef __linux__
  56. #include <memory.h>
  57. static int bcopy(src, dst, length)
  58. char *src, *dst;
  59. int length;
  60. {
  61.     memcpy(dst, src, length);
  62. }
  63. static int bzero(b, length)
  64. char *b;
  65. int length;
  66. {
  67.     memset(b, '\0', length);
  68. }
  69. #endif
  70.  
  71. #endif
  72. #include <sys/file.h>
  73. #include <ctype.h>
  74. #include "msgcat.h"
  75. #include "gencat.h"
  76.  
  77. #ifndef L_SET
  78. #define L_SET SEEK_SET
  79. #endif
  80.  
  81. #ifndef L_INCR
  82. #define L_INCR SEEK_CUR
  83. #endif
  84.  
  85. static char *curline = NULL;
  86. static long lineno = 0;
  87.  
  88. static void warning(cptr, msg)
  89. char *cptr;
  90. char *msg;
  91. {
  92.     fprintf(stderr, "gencat: %s on line %d\n", msg, lineno);
  93.     fprintf(stderr, "%s\n", curline);
  94.     if (cptr) {
  95.     char    *tptr;
  96.     for (tptr = curline; tptr < cptr; ++tptr)
  97.         putc(*tptr == '\t' ? '\t' : ' ', stderr);
  98.     fprintf(stderr, "^\n");
  99.     }
  100. }
  101.  
  102. static void error(cptr, msg)
  103. char *cptr;
  104. char *msg;
  105. {
  106.     warning(cptr, msg);
  107.     exit(1);
  108. }
  109.  
  110. static void corrupt() {
  111.     error(NULL, "corrupt message catalog");
  112. }
  113. static void nomem() {
  114.     error(NULL, "out of memory");
  115. }
  116.         
  117. static char *getline(fd)
  118. int fd;
  119. {
  120.     static long    len = 0, curlen = BUFSIZ;
  121.     static char    buf[BUFSIZ], *bptr = buf, *bend = buf;
  122.     char    *cptr, *cend;
  123.     long    buflen;
  124.     
  125.     if (!curline) {
  126.     curline = (char *) malloc(curlen);
  127.     if (!curline) nomem();
  128.     }
  129.     ++lineno;
  130.     
  131.     cptr = curline;
  132.     cend = curline + curlen;
  133.     while (True) {
  134.     for (; bptr < bend && cptr < cend; ++cptr, ++bptr) {
  135.         if (*bptr == '\n') {
  136.         *cptr = '\0';
  137.         ++bptr;
  138.         return(curline);
  139.         } else *cptr = *bptr;
  140.     }
  141.     if (bptr == bend) {
  142.         buflen = read(fd, buf, BUFSIZ);
  143.         if (buflen <= 0) {
  144.         if (cptr > curline) {
  145.             *cptr = '\0';
  146.             return(curline);
  147.         }
  148.         return(NULL);
  149.         }
  150.         bend = buf + buflen;
  151.         bptr = buf;
  152.     }
  153.     if (cptr == cend) {
  154.         cptr = curline = (char *) realloc(curline, curlen *= 2);
  155.         cend = curline + curlen;
  156.     }
  157.     }
  158. }
  159.  
  160.  
  161. static char *token(cptr)
  162. char *cptr;
  163. {
  164.     static char    tok[MAXTOKEN+1];
  165.     char    *tptr = tok;
  166.     
  167.     while (*cptr && isspace(*cptr)) ++cptr;
  168.     while (*cptr && !isspace(*cptr)) *tptr++ = *cptr++;
  169.     *tptr = '\0';
  170.     return(tok);
  171. }
  172. static char *wskip(cptr)
  173. char *cptr;
  174. {
  175.     if (!*cptr || !isspace(*cptr)) {
  176.     warning(cptr, "expected a space");
  177.     return(cptr);
  178.     }
  179.     while (*cptr && isspace(*cptr)) ++cptr;
  180.     return(cptr);
  181. }
  182. static char *cskip(cptr)
  183. char *cptr;
  184. {
  185.     if (!*cptr || isspace(*cptr)) {
  186.     warning(cptr, "wasn't expecting a space");
  187.     return(cptr);
  188.     }
  189.     while (*cptr && !isspace(*cptr)) ++cptr;
  190.     return(cptr);
  191. }
  192.  
  193. static char *getmsg(fd, cptr, quote)
  194. int fd;
  195. char *cptr;
  196. char quote;
  197. {
  198.     static char    *msg = NULL;
  199.     static long    msglen = 0;
  200.     long    clen, i;
  201.     char    *tptr;
  202.     
  203.     int        needq;
  204.  
  205.     if (quote && *cptr == quote) {
  206.     needq = True;
  207.     ++cptr;
  208.     } else needq = False;
  209.  
  210.     clen = strlen(cptr) + 1;
  211.     if (clen > msglen) {
  212.     if (msglen) msg = (char *) realloc(msg, clen);
  213.     else msg = (char *) malloc(clen);
  214.     msglen = clen;
  215.     }
  216.     tptr = msg;
  217.     
  218.     while (*cptr) {
  219.     if (quote && *cptr == quote) {
  220.         char    *tmp;
  221.         tmp = cptr+1;
  222.         if (*tmp && (!isspace(*tmp) || *wskip(tmp))) {
  223.         warning(cptr, "quote character before end of line, ignoring");
  224.         *tptr++ = *cptr++;
  225.         } else {
  226.         *cptr = '\0';
  227.         }
  228.     } else if (*cptr == '\\') {
  229.         ++cptr;
  230.         switch (*cptr) {
  231.           case '\0':
  232.         cptr = getline(fd);
  233.         if (!cptr) error(NULL, "premature end of file");
  234.         msglen += strlen(cptr);
  235.         i = tptr - msg;
  236.         msg = (char *) realloc(msg, msglen);
  237.         tptr = msg + i;
  238.         break;
  239.           case 'n':
  240.         *tptr++ = '\n';
  241.         ++cptr;
  242.         break;
  243.           case 't':
  244.         *tptr++ = '\t';
  245.         ++cptr;
  246.         break;
  247.           case 'v':
  248.         *tptr++ = '\v';
  249.         ++cptr;
  250.         break;
  251.           case 'b':
  252.         *tptr++ = '\b';
  253.         ++cptr;
  254.         break;
  255.           case 'r':
  256.         *tptr++ = '\r';
  257.         ++cptr;
  258.         break;
  259.           case 'f':
  260.         *tptr++ = '\f';
  261.         ++cptr;
  262.         break;
  263.           case '\\':
  264.         *tptr++ = '\\';
  265.         ++cptr;
  266.         break;
  267.           case '"':
  268.         *tptr++ = '"';
  269.         ++cptr;
  270.         break;
  271.           default:
  272.         if (isdigit(*cptr)) {
  273.             *tptr = 0;
  274.             for (i = 0; i < 3; ++i) {
  275.             if (!isdigit(*cptr)) break;
  276.             if (*cptr > '7') warning(cptr, "octal number greater than 7?!");
  277.             *tptr *= 8;
  278.             *tptr += (*cptr - '0');
  279.             ++cptr;
  280.             }
  281.         } else if (*cptr == quote) {
  282.             *tptr++ = *cptr++;
  283.         } else {
  284.             warning(cptr, "unrecognized escape sequence");
  285.         }
  286.         }
  287.     } else {
  288.         *tptr++ = *cptr++;
  289.     }
  290.     }
  291.     *tptr = '\0';
  292.     return(msg);
  293. }
  294.  
  295.  
  296.  
  297. static char *dupstr(ostr)
  298. char *ostr;
  299. {
  300.     char    *nstr;
  301.  
  302.     nstr = (char *) malloc(strlen(ostr) + 1);
  303.     if (!nstr) error(NULL, "unable to allocate storage");
  304.     strcpy(nstr, ostr);
  305.     return(nstr);
  306. }
  307.  
  308.             
  309. /*
  310.  * The Global Stuff
  311.  */                
  312.  
  313.  
  314. typedef struct _msgT {
  315.     long    msgId;
  316.     char    *str;
  317.     char    *hconst;
  318.     long    offset;
  319.     struct _msgT    *prev, *next;
  320. } msgT;
  321. typedef struct _setT {
  322.     long    setId;
  323.     char    *hconst;
  324.     msgT    *first, *last;
  325.     struct _setT    *prev, *next;
  326. } setT;
  327. typedef struct {
  328.     setT    *first, *last;
  329. } catT;
  330.  
  331. static setT    *curSet;
  332. static catT    *cat;
  333.  
  334. /*
  335.  * Find the current byte order.  There are of course some others, but this will do
  336.  * for now.  Note that all we care about is "long".
  337.  */
  338. long MCGetByteOrder() {
  339.     long    l = 0x00010203;
  340.     char    *cptr = (char *) &l;
  341.     
  342.     if (cptr[0] == 0 && cptr[1] == 1 && cptr[2] == 2 && cptr[3] == 3)
  343.       return MC68KByteOrder;
  344.     else return MCn86ByteOrder;
  345. }
  346.  
  347.  
  348. void MCParse(
  349. #if PROTO
  350.         int fd)
  351. #else
  352.         fd)
  353. int fd;
  354. #endif
  355. {
  356.     char    *cptr, *str;
  357.     int    setid, msgid = 0;
  358.     char    hconst[MAXTOKEN+1];
  359.     char    quote = 0;
  360.     int        i;
  361.     
  362.     if (!cat) {
  363.     cat = (catT *) malloc(sizeof(catT));
  364.     if (!cat) nomem();
  365.     bzero(cat, sizeof(catT));
  366.     }
  367.  
  368.     hconst[0] = '\0';
  369.     
  370.     while ((cptr = getline(fd)) && *cptr != '$');
  371.  
  372.     if (!cptr) return;
  373.  
  374.     do {
  375.     if (*cptr == '$') {
  376.         ++cptr;
  377.         if (strncmp(cptr, "set", 3) == 0) {
  378.         cptr += 3;
  379.         cptr = wskip(cptr);
  380.         setid = atoi(cptr);
  381.         cptr = cskip(cptr);
  382.         if (*cptr) cptr = wskip(cptr);
  383.         if (*cptr == '#') {
  384.             ++cptr;
  385.             MCAddSet(setid, token(cptr));
  386.         } else MCAddSet(setid, NULL);
  387.         msgid = 0;
  388.         } else if (strncmp(cptr, "delset", 6) == 0) {
  389.         cptr += 6;
  390.         cptr = wskip(cptr);
  391.         setid = atoi(cptr);
  392.         MCDelSet(setid);
  393.         } else if (strncmp(cptr, "quote", 5) == 0) {
  394.         cptr += 5;
  395.         if (!*cptr) quote = 0;
  396.         else {
  397.             cptr = wskip(cptr);
  398.             if (!*cptr) quote = 0;
  399.             else quote = *cptr;
  400.         }
  401.         } else if (isspace(*cptr)) {
  402.         cptr = wskip(cptr);
  403.         if (*cptr == '#') {
  404.             ++cptr;
  405.             strcpy(hconst, token(cptr));
  406.         }
  407.         } else {
  408.         if (*cptr) {
  409.             cptr = wskip(cptr);
  410.             if (*cptr) warning(cptr, "unrecognized line");
  411.         }
  412.         }
  413.     } else {
  414.         if (isdigit(*cptr) || *cptr == '#') {
  415.         if (*cptr == '#') {
  416.             ++msgid;
  417.             ++cptr;
  418.             if (!*cptr) {
  419.             MCAddMsg(msgid, "", hconst);
  420.             hconst[0] = '\0';
  421.             continue;
  422.             }
  423.             if (!isspace(*cptr)) warning(cptr, "expected a space");
  424.             ++cptr;
  425.             if (!*cptr) {
  426.             MCAddMsg(msgid, "", hconst);
  427.             hconst[0] = '\0';
  428.             continue;
  429.             }
  430.         } else {
  431.             msgid = atoi(cptr);
  432.             cptr = cskip(cptr);
  433.             cptr = wskip(cptr);
  434.             /* if (*cptr) ++cptr; */
  435.         }
  436.         if (!*cptr) MCDelMsg(msgid);
  437.         else {
  438.             str = getmsg(fd, cptr, quote);
  439.             MCAddMsg(msgid, str, hconst);
  440.             hconst[0] = '\0';
  441.         }
  442.         }
  443.     }
  444.     } while (cptr = getline(fd));
  445. }
  446.  
  447. void MCReadCat(
  448. #if PROTO
  449.         int fd)
  450. #else
  451.         fd)
  452. int fd;
  453. #endif
  454. {
  455.     MCHeaderT    mcHead;
  456.     MCMsgT    mcMsg;
  457.     MCSetT    mcSet;
  458.     msgT    *msg;
  459.     setT    *set;
  460.     int        i;
  461.     char    *data;
  462.     
  463.     cat = (catT *) malloc(sizeof(catT));
  464.     if (!cat) nomem();
  465.     bzero(cat, sizeof(catT));
  466.  
  467.     if (read(fd, &mcHead, sizeof(mcHead)) != sizeof(mcHead)) corrupt();
  468.     if (strncmp(mcHead.magic, MCMagic, MCMagicLen) != 0) corrupt();
  469.     if ((mcHead.flags & MCGetByteOrder()) == 0) error(NULL, "wrong byte order");
  470.     if (mcHead.majorVer != MCMajorVer) error(NULL, "unrecognized catalog version");
  471.  
  472.     if (lseek(fd, mcHead.firstSet, L_SET) == -1) corrupt();
  473.  
  474.     while (True) {
  475.     if (read(fd, &mcSet, sizeof(mcSet)) != sizeof(mcSet)) corrupt();
  476.     if (mcSet.invalid) continue;
  477.     
  478.     set = (setT *) malloc(sizeof(setT));
  479.     if (!set) nomem();
  480.     bzero(set, sizeof(*set));
  481.     if (cat->first) {
  482.         cat->last->next = set;
  483.         set->prev = cat->last;
  484.         cat->last = set;
  485.     } else cat->first = cat->last = set;
  486.     
  487.     set->setId = mcSet.setId;
  488.  
  489.     /* Get the data */
  490.     if (mcSet.dataLen) {
  491.         data = (char *) malloc(mcSet.dataLen);
  492.         if (!data) nomem();
  493.         if (lseek(fd, mcSet.data.off, L_SET) == -1) corrupt();
  494.         if (read(fd, data, mcSet.dataLen) != mcSet.dataLen) corrupt();
  495.         if (lseek(fd, mcSet.u.firstMsg, L_SET) == -1) corrupt();
  496.     
  497.         for (i = 0; i < mcSet.numMsgs; ++i) {
  498.         if (read(fd, &mcMsg, sizeof(mcMsg)) != sizeof(mcMsg)) corrupt();
  499.         if (mcMsg.invalid) {
  500.             --i;
  501.             continue;
  502.         }
  503.         
  504.         msg = (msgT *) malloc(sizeof(msgT));
  505.         if (!msg) nomem();
  506.         bzero(msg, sizeof(*msg));
  507.         if (set->first) {
  508.             set->last->next = msg;
  509.             msg->prev = set->last;
  510.             set->last = msg;
  511.         } else set->first = set->last = msg;
  512.  
  513.         msg->msgId = mcMsg.msgId;
  514.         msg->str = dupstr((char *) (data + mcMsg.msg.off));
  515.         }
  516.         free(data);
  517.     }
  518.     if (!mcSet.nextSet) break;
  519.     if (lseek(fd, mcSet.nextSet, L_SET) == -1) corrupt();
  520.     }
  521. }
  522.  
  523.  
  524. static void printS(fd, str)
  525. int fd;
  526. char *str;
  527. {
  528.     write(fd, str, strlen(str));
  529. }
  530. static void printL(fd, l)
  531. int fd;
  532. long l;
  533. {
  534.     char    buf[32];
  535.     sprintf(buf, "%ld", l);
  536.     write(fd, buf, strlen(buf));
  537. }
  538. static void printLX(fd, l)
  539. int fd;
  540. long l;
  541. {
  542.     char    buf[32];
  543.     sprintf(buf, "%lx", l);
  544.     write(fd, buf, strlen(buf));
  545. }
  546.  
  547. static void genconst(fd, type, setConst, msgConst, val)
  548. int fd;
  549. int type;
  550. char *setConst;
  551. char *msgConst;
  552. long val;
  553. {
  554.     switch (type) {
  555.       case MCLangC:
  556.     if (!msgConst) {
  557.         printS(fd, "\n#define ");
  558.         printS(fd, setConst);
  559.         printS(fd, "Set");
  560.     } else {
  561.         printS(fd, "#define ");
  562.         printS(fd, setConst);
  563.         printS(fd, msgConst);
  564.     }
  565.     printS(fd, "\t0x");
  566.     printLX(fd, val);
  567.     printS(fd, "\n");
  568.     break;
  569.       case MCLangCPlusPlus:
  570.       case MCLangANSIC:
  571.     if (!msgConst) {
  572.         printS(fd, "\nconst long ");
  573.         printS(fd, setConst);
  574.         printS(fd, "Set");
  575.     } else {
  576.         printS(fd, "const long ");
  577.         printS(fd, setConst);
  578.         printS(fd, msgConst);
  579.     }
  580.     printS(fd, "\t= ");
  581.     printL(fd, val);
  582.     printS(fd, ";\n");
  583.     break;
  584.       default:
  585.     error(NULL, "not a recognized (programming) language type");
  586.     }
  587. }
  588.  
  589. void MCWriteConst(
  590. #if PROTO
  591.         int fd, int type, int orConsts)
  592. #else
  593.         fd, type, orConsts)
  594. int fd;
  595. int type;
  596. int orConsts;
  597. #endif
  598. {
  599.     msgT    *msg;
  600.     setT    *set;
  601.     long    id;
  602.     
  603.     if (orConsts && (type == MCLangC || type == MCLangCPlusPlus || type == MCLangANSIC)) {
  604.     printS(fd, "/* Use these Macros to compose and decompose setId's and msgId's */\n");
  605.     printS(fd, "#ifndef MCMakeId\n");
  606.     printS(fd, "# define MCuint unsigned int\n");
  607.     printS(fd, "# define MCushort unsigned short\n");
  608.     printS(fd, "# define MCulong unsigned long\n");
  609.         printS(fd, "# define MCMakeId(s,m)\t(MCulong)(((MCushort)s<<(sizeof(short)*8))\\\n");
  610.         printS(fd, "                      \t          |(MCushort)m)\n");
  611.         printS(fd, "# define MCSetId(id)\t(MCuint) ((MCuint)id >> (MCuint)(sizeof(short) * 8))\n");
  612.         printS(fd, "# define MCMsgId(id)\t(MCuint) (((MCuint)id << (MCuint)(sizeof(short) * 8))\\\n");
  613.         printS(fd, "                    \t          >> (MCuint)(sizeof(short) * 8))\n");
  614.     printS(fd, "#endif\n");
  615.     }
  616.     
  617.     for (set = cat->first; set; set = set->next) {
  618.     if (set->hconst) genconst(fd, type, set->hconst, NULL, set->setId);
  619.  
  620.     for (msg = set->first; msg; msg = msg->next) {
  621.         if (msg->hconst) {
  622.         if (orConsts) id = MCMakeId(set->setId, msg->msgId);
  623.         else id = msg->msgId;
  624.         genconst(fd, type, set->hconst, msg->hconst, id);
  625.         free(msg->hconst);
  626.         msg->hconst = NULL;
  627.         }
  628.     }
  629.     if (set->hconst) {
  630.         free(set->hconst);
  631.         set->hconst = NULL;
  632.     }
  633.     }
  634. }
  635.  
  636. void MCWriteCat(
  637. #if PROTO
  638.         int fd)
  639. #else
  640.         fd)
  641. int fd;
  642. #endif
  643. {
  644.     MCHeaderT    mcHead;
  645.     int        cnt;
  646.     setT    *set;
  647.     msgT    *msg;
  648.     MCSetT    mcSet;
  649.     MCMsgT    mcMsg;
  650.     off_t    pos;
  651.  
  652.     bcopy(MCMagic, mcHead.magic, MCMagicLen);
  653.     mcHead.majorVer = MCMajorVer;
  654.     mcHead.minorVer = MCMinorVer;
  655.     mcHead.flags = MCGetByteOrder();
  656.     mcHead.firstSet = 0;    /* We'll be back to set this in a minute */
  657.     
  658.     for (cnt = 0, set = cat->first; set; set = set->next) ++cnt;
  659.     mcHead.numSets = cnt;
  660.  
  661.     lseek(fd, 0L, L_SET);
  662.     write(fd, &mcHead, sizeof(mcHead));
  663.     mcHead.firstSet = lseek(fd, 0, L_INCR);
  664.     lseek(fd, 0L, L_SET);
  665.     write(fd, &mcHead, sizeof(mcHead));
  666.  
  667.     for (set = cat->first; set; set = set->next) {
  668.     bzero(&mcSet, sizeof(mcSet));
  669.  
  670.     mcSet.setId = set->setId;
  671.     mcSet.invalid = False;
  672.  
  673.     /* The rest we'll have to come back and change in a moment */
  674.     pos = lseek(fd, 0, L_INCR);
  675.     write(fd, &mcSet, sizeof(mcSet));
  676.     
  677.     /* Now write all the string data */
  678.     mcSet.data.off = lseek(fd, 0, L_INCR);
  679.     cnt = 0;
  680.     for (msg = set->first; msg; msg = msg->next) {
  681.         msg->offset = lseek(fd, 0, L_INCR) - mcSet.data.off;
  682.         mcSet.dataLen += write(fd, msg->str, strlen(msg->str) + 1);
  683.         ++cnt;
  684.     }
  685.     mcSet.u.firstMsg = lseek(fd, 0, L_INCR);
  686.     mcSet.numMsgs = cnt;
  687.  
  688.     /* Now write the message headers */
  689.     for (msg = set->first; msg; msg = msg->next) {
  690.         mcMsg.msgId = msg->msgId;
  691.         mcMsg.msg.off = msg->offset;
  692.         mcMsg.invalid = False;
  693.         write(fd, &mcMsg, sizeof(mcMsg));
  694.     }
  695.  
  696.     /* Go back and fix things up */
  697.  
  698.     if (set == cat->last) {
  699.         mcSet.nextSet = 0;
  700.         lseek(fd, pos, L_SET);
  701.         write(fd, &mcSet, sizeof(mcSet));
  702.     } else {
  703.         mcSet.nextSet = lseek(fd, 0, L_INCR);
  704.         lseek(fd, pos, L_SET);
  705.         write(fd, &mcSet, sizeof(mcSet));
  706.         lseek(fd, mcSet.nextSet, L_SET);
  707.     }
  708.     }
  709. }
  710.  
  711.  
  712. void MCAddSet(
  713. #if PROTO
  714.         int setId, char *hconst)
  715. #else
  716.         setId, hconst)
  717. int setId;
  718. char *hconst;
  719. #endif
  720. {
  721.     setT    *set;
  722.     
  723.     if (setId <= 0) {
  724.     error(NULL, "setId's must be greater than zero");
  725.     return;
  726.     }
  727.     
  728.     if (hconst && !*hconst) hconst = NULL;
  729.     for (set = cat->first; set; set = set->next) {
  730.     if (set->setId == setId) {
  731.         if (set->hconst && hconst) free(set->hconst);
  732.         set->hconst = NULL;
  733.         break;
  734.     } else if (set->setId > setId) {
  735.         setT    *newSet;
  736.         
  737.         newSet = (setT *) malloc(sizeof(setT));
  738.         if (!newSet) nomem();
  739.         bzero(newSet, sizeof(setT));
  740.         newSet->prev = set->prev;
  741.         newSet->next = set;
  742.         if (set->prev) set->prev->next = newSet;
  743.         else cat->first = newSet;
  744.         set->prev = newSet;
  745.         set = newSet;
  746.         break;
  747.     }
  748.     }
  749.     if (!set) {
  750.     set = (setT *) malloc(sizeof(setT));
  751.     if (!set) nomem();
  752.     bzero(set, sizeof(setT));
  753.     
  754.     if (cat->first) {
  755.         set->prev = cat->last;
  756.         set->next = NULL;
  757.         cat->last->next = set;
  758.         cat->last = set;
  759.     } else {
  760.         set->prev = set->next = NULL;
  761.         cat->first = cat->last = set;
  762.     }
  763.     }
  764.     set->setId = setId;
  765.     if (hconst) set->hconst = dupstr(hconst);
  766.     curSet = set;
  767. }
  768.  
  769. void MCAddMsg(
  770. #if PROTO
  771.         int msgId, char *str, char *hconst)
  772. #else
  773.         msgId, str, hconst)
  774. int msgId;
  775. char *str;
  776. char *hconst;
  777. #endif
  778. {
  779.     msgT    *msg;
  780.     
  781.     if (!curSet) error(NULL, "can't specify a message when no set exists");
  782.  
  783.     if (msgId <= 0) {
  784.     error(NULL, "msgId's must be greater than zero");
  785.     return;
  786.     }
  787.     
  788.     if (hconst && !*hconst) hconst = NULL;
  789.     for (msg = curSet->first; msg; msg = msg->next) {
  790.     if (msg->msgId == msgId) {
  791.         if (msg->hconst && hconst) free(msg->hconst);
  792.         if (msg->str) free(msg->str);
  793.         msg->hconst = msg->str = NULL;
  794.         break;
  795.     } else if (msg->msgId > msgId) {
  796.         msgT    *newMsg;
  797.         
  798.         newMsg = (msgT *) malloc(sizeof(msgT));
  799.         if (!newMsg) nomem();
  800.         bzero(newMsg, sizeof(msgT));
  801.         newMsg->prev = msg->prev;
  802.         newMsg->next = msg;
  803.         if (msg->prev) msg->prev->next = newMsg;
  804.         else curSet->first = newMsg;
  805.         msg->prev = newMsg;
  806.         msg = newMsg;
  807.         break;
  808.     }
  809.     }
  810.     if (!msg) {
  811.     msg = (msgT *) malloc(sizeof(msgT));
  812.     if (!msg) nomem();
  813.     bzero(msg, sizeof(msgT));
  814.     
  815.     if (curSet->first) {
  816.         msg->prev = curSet->last;
  817.         msg->next = NULL;
  818.         curSet->last->next = msg;
  819.         curSet->last = msg;
  820.     } else {
  821.         msg->prev = msg->next = NULL;
  822.         curSet->first = curSet->last = msg;
  823.     }
  824.     }
  825.     msg->msgId = msgId;
  826.     if (hconst) msg->hconst = dupstr(hconst);
  827.     msg->str = dupstr(str);
  828. }
  829.  
  830. void MCDelSet(
  831. #if PROTO
  832.         int setId)
  833. #else
  834.         setId)
  835. int setId;
  836. #endif
  837. {
  838.     setT    *set;
  839.     msgT    *msg;
  840.  
  841.     for (set = cat->first; set; set = set->next) {
  842.     if (set->setId == setId) {
  843.         for (msg = set->first; msg; msg = msg->next) {
  844.         if (msg->hconst) free(msg->hconst);
  845.         if (msg->str) free(msg->str);
  846.         free(msg);
  847.         }
  848.         if (set->hconst) free(set->hconst);
  849.  
  850.         if (set->prev) set->prev->next = set->next;
  851.         else cat->first = set->next;
  852.  
  853.         if (set->next) set->next->prev = set->prev;
  854.         else cat->last = set->prev;
  855.  
  856.         free(set);
  857.         return;
  858.     } else if (set->setId > setId) break;
  859.     }
  860.     warning(NULL, "specified set doesn't exist");
  861. }
  862.  
  863. void MCDelMsg(
  864. #if PROTO
  865.         int msgId)
  866. #else
  867.         msgId)
  868. int msgId;
  869. #endif
  870. {
  871.     msgT    *msg;
  872.  
  873.     if (!curSet) error(NULL, "you can't delete a message before defining the set");
  874.     
  875.     for (msg = curSet->first; msg; msg = msg->next) {
  876.     if (msg->msgId == msgId) {
  877.         if (msg->hconst) free(msg->hconst);
  878.         if (msg->str) free(msg->str);
  879.  
  880.         if (msg->prev) msg->prev->next = msg->next;
  881.         else curSet->first = msg->next;
  882.  
  883.         if (msg->next) msg->next->prev = msg->prev;
  884.         else curSet->last = msg->prev;
  885.  
  886.         free(msg);
  887.         return;
  888.     } else if (msg->msgId > msgId) break;
  889.     }    
  890.     warning(NULL, "specified msg doesn't exist");
  891. }
  892.  
  893.                 
  894.         
  895.         
  896.         
  897.         
  898.