From d0ddfb8112d41c1ccbf86b4a2c2ca5f574e584e7 Mon Sep 17 00:00:00 2001 From: Masanori Ogino Date: Wed, 29 Nov 2023 03:35:37 +0900 Subject: [PATCH] demo: KNF and ISO C migration Signed-off-by: Masanori Ogino --- demo/sample.c | 251 +++++++++++++++++++------------------------------- 1 file changed, 94 insertions(+), 157 deletions(-) diff --git a/demo/sample.c b/demo/sample.c index 22e6bc2..7feb945 100644 --- a/demo/sample.c +++ b/demo/sample.c @@ -1,6 +1,6 @@ /* * Copyright (c) 1994 Sony Corporation - * + * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including @@ -8,10 +8,10 @@ * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. @@ -19,7 +19,7 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * + * * Except as contained in this notice, the name of Sony Corporation * shall not be used in advertising or otherwise to promote the sale, use * or other dealings in this Software without prior written authorization @@ -27,19 +27,10 @@ * */ -/* - * $SonyRCSfile: sample.c,v $ - * $SonyRevision: 1.3 $ - * $SonyDate: 1997/01/23 11:26:29 $ - */ - - - - - -#include #include #include +#include + #include "sj3lib.h" #include "wchar16.h" @@ -49,73 +40,66 @@ #define HBUFSIZ 512 #define QBUFSIZ 256 -char *pname; -char *hname; -int current_locale = LC_CTYPE_EUC; +char *pname; +char *hname; +int current_locale = LC_CTYPE_EUC; +void usage() { fprintf(stderr, "Usage: %s [-v] [-H hostname]\n", pname); exit(1); } -main (argc, argv) -int argc; -char **argv; +int +main(int argc, char **argv) { - register int c; - int ret; - int vflag; - struct bunsetu bun[HBUFSIZ]; - unsigned char alphabet[QBUFSIZ]; - unsigned char yomi[HBUFSIZ]; - unsigned char kanji[BUFSIZ]; - char *uname, *loc; - extern char *optarg; - char *rindex(), *getlogin(); - struct passwd *getpwuid(), *pwd; - int count=1; + struct bunsetu bun[HBUFSIZ]; + unsigned char alphabet[QBUFSIZ]; + unsigned char yomi[HBUFSIZ]; + unsigned char kanji[BUFSIZ]; + extern char *optarg; + char *rindex(), *getlogin(); + char *loc, *uname; + struct passwd *getpwuid(); + struct passwd *pwd; + int count = 1; + int c, ret, vflag; loc = setlocale(LC_CTYPE, ""); if (strcmp(loc, "ja_JP.SJIS") == 0) { current_locale = LC_CTYPE_SHIFTJIS; - } else if (strncmp(loc, "ja", 2) != 0 && - strncmp(loc, "Ja", 2) != 0) { + } else if (strncmp(loc, "ja", 2) != 0 && strncmp(loc, "Ja", 2) != 0) { fprintf(stderr, "Error: %s is not japanese locale\n", loc); exit(1); } vflag = 0; hname = NULL; - print_head(); - pname = argv[0]; - - while ((c = getopt(argc, argv, "vk:H:")) != EOF) { - switch(c) { + while ((c = getopt(argc, argv, "vk:H:")) != EOF) { + switch (c) { case 'v': - vflag++; break; case 'H': - hname = optarg; break; case 'k': - fprintf(stderr, "optarg = %s\n", optarg); - count = atoi(optarg); - break; + fprintf(stderr, "optarg = %s\n", optarg); + count = atoi(optarg); + break; case '?': - default : + default: usage(); break; } } - + uname = getlogin(); if (uname == NULL || *uname == '\0') { setpwent(); @@ -127,18 +111,16 @@ char **argv; exit(1); } } -#ifndef ORG -while (count--) { -#endif - if (ret = sj3_rkinit(RKFILE)) { - if (ret == 1) - fprintf(stderr, "Can't open Romaji Kana Rule file %s\n", - RKFILE); - else + if (ret == 1) { + fprintf(stderr, + "Can't open Romaji Kana Rule file %s\n", + RKFILE); + } else { fprintf(stderr, "Error sj3_rkinit\n"); - exit (1); + } + exit(1); } ret = sj3_open(hname, uname); if (hname == NULL) @@ -147,84 +129,61 @@ while (count--) { if (ret & SJ3_SERVER_DEAD) { fprintf(stderr, "%s:sj3serv dead\n", hname); exit(1); - } - else if (ret & SJ3_ALREADY_CONNECTED) { - fprintf(stderr, "Already connected %s:sj3serv\n",hname); + } else if (ret & SJ3_ALREADY_CONNECTED) { + fprintf(stderr, + "Already connected %s:sj3serv\n", hname); exit(1); - } - else if (ret & SJ3_CONNECT_ERROR) { - fprintf(stderr, "Can't connect %s:sj3serv\n", hname); + } else if (ret & SJ3_CONNECT_ERROR) { + fprintf(stderr, "Can't connect %s:sj3serv\n", + hname); exit(1); - } - else if (ret & SJ3_CANNOT_OPEN_MDICT) { - fprintf(stderr, "Can't open sj3 main dictionary\n"); + } else if (ret & SJ3_CANNOT_OPEN_MDICT) { + fprintf(stderr, + "Can't open sj3 main dictionary\n"); (void)sj3_close(); exit(1); } - fprintf(stderr, "Warning sj3_open error (return 0x%x)\n", ret); + fprintf(stderr, + "Warning sj3_open error (return 0x%x)\n", ret); } - - -#ifdef ORG while (1) { - - fprintf(stdout, "[Input Alphabet] "); - if (fgets((char *)alphabet, QBUFSIZ, stdin) == NULL) + if (fgets((char *)alphabet, QBUFSIZ, stdin) == NULL) break; if (RETURN(alphabet[0])) break; -#else - sprintf(alphabet, "%s", "tonarinokyakuhayokukakikuukyakuda"); -#endif - rkconvs(yomi, alphabet); - - ret = sj3_getkan_mb(yomi, bun, kanji, BUFSIZ); if (ret == -1) { fprintf(stderr, "sj3_getkan: disconnected %s:sj3serv\n", - hname); + hname); exit(1); } - - if (vflag) pr_kanji(bun, ret); - - - if (bun[ret - 1].destlen == 0) + if (bun[ret - 1].destlen == 0) strcat(kanji, bun[ret - 1].srcstr); fprintf(stdout, "[Output Yomi ]:[%s]\n", yomi); fprintf(stdout, "[Output Kanji]:[%s]\n", kanji); -#ifdef ORG } -#endif - (void)sj3_close(); -#ifndef ORG -} -#endif } -rkconvs(yomi, roma) -unsigned char *yomi; -unsigned char *roma; +void +rkconvs(unsigned char *yomi, unsigned char *roma) { - register unsigned char *p, *q, *yp; - register int i; - int rlen; - int len; - unsigned char rbuf[16]; - unsigned char ybuf[32]; - unsigned char tmp[HBUFSIZ]; - + int i, len, rlen; + unsigned char rbuf[16]; + unsigned char *p, *q, *yp; + unsigned char ybuf[32]; + unsigned char tmp[HBUFSIZ]; + yp = tmp; p = rbuf; rlen = 0; @@ -232,57 +191,49 @@ unsigned char *roma; *p++ = *roma++; *p = '\0'; rlen++; - while(1) { - + while (1) { if ((len = sj3_rkconv_mb(rbuf, ybuf)) > 0) { q = ybuf; - while (*q != '\0') { + while (*q != '\0') *yp++ = *q++; - } i = rlen; rlen = strlen(rbuf); p = rbuf; p += rlen; if (len >= i) break; - } - - else if (len < 0) { + } else if (len < 0) { *yp++ = rbuf[0]; p = rbuf; q = (p + 1); - while(*q != '\0') + while (*q != '\0') *p++ = *q++; *p = '\0'; rlen--; - } - - else + } else { break; + } } } - if (rlen > 0) { *p++ = '\n'; *p = '\0'; rlen++; - while(rbuf[0] != '\0') { + while (rbuf[0] != '\0') { if ((len = sj3_rkconv_mb(rbuf, ybuf)) > 0) { q = ybuf; - while (*q != '\0') { + while (*q != '\0') *yp++ = *q++; - } - + rlen = strlen(rbuf); p = rbuf; p += rlen; - } - else { + } else { *yp++ = rbuf[0]; p = rbuf; q = (p + 1); - while(*q != '\0') + while (*q != '\0') *p++ = *q++; *p = '\0'; } @@ -290,14 +241,13 @@ unsigned char *roma; yp--; } *yp = '\0'; - sj3_hantozen_mb(yomi, tmp); } -print_head() { - - +void +print_head() +{ fprintf(stdout, "\t*--------------------------------------------*\n"); fprintf(stdout, "\t* Sample Program for *\n"); fprintf(stdout, "\t* New Japanese Convert Library *\n"); @@ -310,31 +260,24 @@ print_head() { fprintf(stdout, "\t*--------------------------------------------*\n"); } -str_make(out_str, in_str, len) -unsigned char *out_str; -unsigned char *in_str; -int len; +void +str_make(unsiged char *out_str, unsigned char *in_str, int len) { - - strncpy(out_str, in_str, len); out_str += len; *out_str = '\0'; } -pr_douon(yomi) -unsigned char *yomi; +void +pr_douon(unsigned char *yomi) { - register int i; - struct douon douon[BUFSIZ]; - int douon_cnt; - - + struct douon douon[BUFSIZ]; + int douon_cnt, i; douon_cnt = sj3_getdouon_mb(yomi, douon); if (douon_cnt < 0) { fprintf(stderr, "sj3_getdouon: disconnected %s:sj3serv\n", - hname); + hname); exit(1); } fprintf(stdout, "\t\tsj3_getdouon(yomi, douon)"); @@ -342,22 +285,19 @@ unsigned char *yomi; for (i = 0; i < douon_cnt; i++) { fprintf(stdout, "\t\t--- * Douon : %d * ---\n", i); fprintf(stdout, "\t\t\tdlen : %d\n", douon[i].dlen); - fprintf(stdout, "\t\t\tddata:[%s]\n",douon[i].ddata); + fprintf(stdout, "\t\t\tddata:[%s]\n", douon[i].ddata); } } -pr_bunsetu(now_bun, bunsetu_cnt) -struct bunsetu *now_bun; -int bunsetu_cnt; +void +pr_bunsetu(struct bunsetu *now_bun, int bunsetu_cnt) { - unsigned char tmp_str[BUFSIZ]; - unsigned char *yomi; - - + unsigned char tmp_str[BUFSIZ]; + unsigned char *yomi; fprintf(stdout, "\t--- < Bunsetu : %d > ---\n", bunsetu_cnt); - fprintf(stdout, "\t\tsrclen : %d\n", now_bun -> srclen); - str_make(tmp_str, now_bun -> srcstr, now_bun -> srclen); + fprintf(stdout, "\t\tsrclen : %d\n", now_bun->srclen); + str_make(tmp_str, now_bun->srcstr, now_bun->srclen); fprintf(stdout, "\t\tsrcstr:[%s]\n", tmp_str); yomi = tmp_str; @@ -366,21 +306,18 @@ int bunsetu_cnt; else fprintf(stderr, "\t\tNot Conversion Bunsetsu\n"); - fprintf(stdout, "\t\tdestlen : %d\n", now_bun -> destlen); - str_make(tmp_str, now_bun -> deststr, now_bun -> destlen); + fprintf(stdout, "\t\tdestlen : %d\n", now_bun->destlen); + str_make(tmp_str, now_bun->deststr, now_bun->destlen); fprintf(stdout, "\t\tdeststr:[%s]\n", tmp_str); } -pr_kanji(bun, bnum) -struct bunsetu *bun; -int bnum; +void +pr_kanji(struct bunsetu *bun, int bnum) { - register int i; - - + int i; fprintf(stdout, "\tsj3_getkan(yomi, kanji)"); fprintf(stdout, "...Bunsetu count : %d\n", bnum); - for (i = 0; i < bnum; i++) + for (i = 0; i < bnum; i++) pr_bunsetu(&(bun[i]), i); }