summaryrefslogtreecommitdiffstats
path: root/otherservice/rpc_library/tool/apidef.cc
blob: 5a5df1c2c85c64c66e44971403d1dd2deeafa2bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
/*
 * @copyright Copyright (c) 2016-2019 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file apidef.cc
 * @brief RPC tools--Main processing(Implementations of APIDef classes)
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>

#include <other_service/rpc.h>

#include "apidef.h"
#include "apidef.tab.h"

static APIDef *API_Def;

extern "C" {  // C interface functions

void
ApidefDefineId(const char *id) {
  if (id != NULL) {
    if (API_Def == NULL) {
      API_Def = new APIDef();
    }
    API_Def->DefineId(id);
  }
}

void
ApidefAddHeader(const char *filename) {
  if (filename != NULL) {
    if (API_Def == NULL) {
      API_Def = new APIDef();
    }
    API_Def->AddHeader(filename);
  }
}

int
ApidefPushFunctionArg(int arg_code, int buffer_bytes, int is_pointer,
       int is_vararray, int is_array_size,
       const char *var_type_name, int in_out,
       const char *var_name) {
  if (API_Def == NULL) {
    API_Def = new APIDef();
  }
  return API_Def->AddFunctionArg(arg_code, buffer_bytes, is_pointer,
             is_vararray, is_array_size,
           var_type_name, in_out, var_name);
}

void
ApidefProcessFunction(const char *name) {
  if (API_Def == NULL) {
    API_Def = new APIDef();
  }
  API_Def->NameFunction(name);
}

void
ApidefListFunctions(int with_args) {
  if (API_Def != NULL) {
    API_Def->Print(with_args);
  }
}

void
ApidefFreeAllocation(void) {
  if (API_Def != NULL) {
    delete API_Def;
    API_Def = NULL;
  }
}

int
ApidefMakeStubs(void) {
  if (API_Def != NULL) {
    API_Def->MakeStubs();
    return 0;
  } else {
    return 1;
  }
}

} // extern "C"

/*
 * Retrieving Argument Names Without Decorators
 */
void
Arg::GetUndecolatedName(string& name /* OUT */, int num) {
  name = "v";
  if (num > 0) {
    /* Making Variable Names Sequential Numbers */
    char num_str[5];
    sprintf(num_str, "%d", num);
    name += num_str;
  } else {
    /* Use a user-supplied pseudo-variable name */
    name = m_name;
  }
}

int
Arg::PrintUndecoratedName(fstream& out, int num) {
  /* Pointer argument */
  if (m_is_pointer) {
    out << "*";
  }

  if (num > 0) {
    /* Making Variable Names Sequential Numbers */
    out << "v" << num;
  } else {
    /* Use a user-supplied pseudo-variable name */
    out << m_name;
  }

  return 0;
}

/*
 * Outputs one argument in a function prototype declaration
 * out: Destination file
 * num: Handling of Formal Argument Names
 *      0 :Formal argument name specified in the API definition file
 *      Non-zero: Use "v" + specified number(Examples: v1, v2, ...)
 */
int
Arg::PrintPrototype(fstream& out, int num) {
  const char *type = TypeCodeString(m_code);
  if (type == NULL) {
    type = m_type_name.c_str();
  }
  if (type == NULL) {
    cout << "Internal Error Occurrence!!\n";
    return 1;
  }

  /* Const non-output pointers */
  if ((m_in_out & RPC_OUT_ARG) == 0 && (m_bytes != 0 || m_is_pointer != 0)) {
    out << "const ";
  }
  out << type << " ";
  if (m_is_pointer && m_bytes == 0) {/* Non-string pointer arguments */
    out << "*";
  }

  if (num > 0) {/* Making variable names sequential numbers */
    out << "v" << num;
  } else {/* Use a user-supplied pseudo-variable name */
    out << m_name;
  }

  /* Variable-length arrays */
  if (m_is_vararray != 0) {
    out << "/* VAR_ARRAY */";
  }

  /* Variable-length arrays length */
  if (m_is_array_size != 0) {
    out << "/* VAR_SIZE */";
  }

  /*
   * Commenting on OUT/INOUT Arguments
   * Maximum number of bytes of a string argument added to a comment
   */
  if ((m_in_out & RPC_OUT_ARG) != 0 || m_bytes != 0) {
    out << "/* ";
    if ((m_in_out & RPC_OUT_ARG) != 0) {
      if ((m_in_out & RPC_IN_ARG) != 0) {
        out << "IN";
      }
      out << "OUT ";
    }
    if (m_bytes) {
      out << m_bytes << "Byte ";
    }
    out << "*/";
  }

  return 0;
}

int
Arg::CreateMarshallArgs(fstream& out, int num) {
  return CreateMarshallArgs(out, num, (RPC_IN_ARG|RPC_OUT_ARG));
}

int
Arg::CreateMarshallArgs(fstream& out, int num, string &array_size_name) {
  return CreateMarshallArgs(out, num, (RPC_IN_ARG|RPC_OUT_ARG), array_size_name);
}

int
Arg::CreateMarshallArgs(fstream& out, int num, int in_out) {
  string dummy = "";
  return CreateMarshallArgs(out, num, in_out, dummy);
}

/*
 * The arguments to rpc_marshall_args() are generated for each API argument.
 * for each API argument.
 * out: Destination file
 * num: Number given to the variable name of the argument (Examples: 1 for v1)
 * in_out: Specifying the IN or OUT Attributes
 *         Output only if the argument has the specified attribute
 */
int
Arg::CreateMarshallArgs(fstream& out, int num, int in_out, string &array_size_name) {
  if ((m_in_out & in_out) != 0) {
    /*
     * Collectively passes the argument type, pointer/non-pointer, IN/OUT attribute, 
     * and the maximum number of bytes of the argument type to one integer.
     */
    RPC_marshall_flag flag;
    flag.bits.code = m_code & ((1<<RPC_MARSHALL_FLAG_BITS_CODE)-1);
    flag.bits.is_vararray = ((array_size_name.size() != 0) ? 1: 0) &
                            ((1<<RPC_MARSHALL_FLAG_BITS_IS_VARARRAY)-1);
    flag.bits.is_pointer = m_is_pointer & ((1<<RPC_MARSHALL_FLAG_BITS_IS_POINTER)-1);
    flag.bits.in_out = m_in_out & ((1<<RPC_MARSHALL_FLAG_BITS_IN_OUT)-1);
    flag.bits.bytes = (UINT16)(m_bytes);
    char str[11];
    sprintf(str, "0x%x", htonl(flag.all));
    out << "\t\t\t" << str;

    /*
     * Add size of user-defined type to argument
     */
    if (m_code == rpc_USER_DEFINED) {
      if (array_size_name.size() != 0) {
        out << " + ntohs(sizeof(" << m_type_name << ") * " << array_size_name << ")";
      } else {
        out << " + ntohs(sizeof(" << m_type_name << "))";
      }
    } else if (array_size_name.size() != 0) {
      out << " + ntohs(sizeof(" << TypeCodeString(m_code) << ") * " << array_size_name << ")";
    }

    out << ", v" << num;
    return 1;
  } else {
    return 0;
  }
}

int
Arg::CreateDemarshallArgs(fstream& out, int deliver_pointer, int num) {
  return CreateDemarshallArgs(out, deliver_pointer, num, (RPC_IN_ARG|RPC_OUT_ARG));
}

int
Arg::CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, string &array_size_name) {
  return CreateDemarshallArgs(out, deliver_pointer, num, (RPC_IN_ARG|RPC_OUT_ARG), array_size_name);
}

int
Arg::CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, int in_out) {
  string dummy = "";
  return CreateDemarshallArgs(out, deliver_pointer, num, in_out, dummy);
}

/*
 * The arguments to rpc_demarshall_args() are generated for each API argument.
 * for each API argument.
 * out: Destination file
 * deliver_pointer: Passing a Variable Pointer(non-0)
 * num: Number given to the variable name of the argument (Examples: 1 for v1)
 * in_out: Specifying the IN or OUT Attributes
 *         Output only if the argument has the specified attribute
 */
int
Arg::CreateDemarshallArgs(fstream& out, int deliver_pointer, int num, int in_out, string &array_size_name) {
  if ((m_in_out & in_out) != 0) {
    /*
     * Collectively passes the argument type, pointer/non-pointer, IN/OUT attribute,
     * and the maximum number of bytes of the argument type to one integer.
     */
    RPC_marshall_flag flag;
    flag.bits.code = m_code & ((1<<RPC_MARSHALL_FLAG_BITS_CODE)-1);
    flag.bits.is_vararray = ((array_size_name.size() != 0) ? 1: 0) &
                            ((1<<RPC_MARSHALL_FLAG_BITS_IS_VARARRAY)-1);
    flag.bits.is_pointer = m_is_pointer & ((1<<RPC_MARSHALL_FLAG_BITS_IS_POINTER)-1);
    flag.bits.in_out = m_in_out & ((1<<RPC_MARSHALL_FLAG_BITS_IN_OUT)-1);
    flag.bits.bytes = (UINT16)(m_bytes);
    char str[11];
    sprintf(str, "0x%x", htonl(flag.all));
    out << "\t\t\t" << str;

    /*
     * Add size of user-defined type to argument
     */
    if (m_code == rpc_USER_DEFINED) {
        out << " + ntohs(sizeof(" << m_type_name << "))";
    }

    out << ", ";
    if (deliver_pointer) {
      /* Pass a pointer */
      out << "&";
    }
    out << "v" << num;
    return 1;
  } else {
    return 0;
  }
}

int
Function::AppendArg(int code, int bytes, int is_pointer,
         int is_vararray, int is_array_size,
         const char *var_type_name, int in_out, const char *var_name) {
  if (NumOfArgs() >= RPC_MAX_API_ARG_NUM) {
    cout << "Too many API function arguments.\n";
    return -1;
  }
  Arg *a = new Arg(code, bytes, is_pointer, is_vararray, is_array_size,
       var_type_name, in_out, var_name);
  m_args.push_back(*a);
  delete a;
  return 0;
}

/*
 * Generate prototype declarations for a single API function
 * out: Destination file
 */
int
Function::PrintPrototype(fstream& out, int server) {
  int ret = 0;

  const char *return_type
    = server ? RPC_API_SERVER_RETURN : RPC_API_CLIENT_RETURN;
#ifdef DBG_ENABLE
  if ((server) || (!rpc_log_enable)) {
    out << return_type << " " << m_name << "(";
  } else {
    out << return_type << " __" << m_name << "(";
  }
#else
  out << return_type << " " << m_name << "(";
#endif

  list<Arg>::size_type num_args = m_args.size();
  if (num_args > 0) {
    list<Arg>::iterator a;
    a = m_args.begin();
    for (list<Arg>::size_type i = 1; i <= num_args; ++i, ++a) {
      a->PrintPrototype(out);
#ifdef DBG_ENABLE
      if ((server) || (!rpc_log_enable)) {
        if (i != num_args) {
          out << ", ";
        }
      } else {
  out << ", ";
      }
#else
      if (i != num_args) {
  out << ", ";
      }
#endif
    }
  } else {
#ifdef DBG_ENABLE
    if ((server) || (!rpc_log_enable)) {
      out << "void";
    }
#else
    out << "void";
#endif
  }
#ifdef DBG_ENABLE
  /* Debug information */
  if ((!server) && (rpc_log_enable)) {
    out << "const char *filename, const char *funcname, int line ";
  }
#endif
  out << ");\n";
  return ret;
}

#ifdef DBG_ENABLE
int
Function::PrintMacro(fstream& out) {
  int ret = 0;
  out << "#define " << m_name << "(";

  int num_args = m_args.size();
  if (num_args > 0) {
    int i;
    char c;
    for (i = 1, c = 'a'; i <= num_args; i++, c++) {
      out << c;
      if (i != num_args) {
        out << ", ";
      }
    }
    out << ") \\\n\t";

    /* Entity */
    out << "__" << m_name << "(";
    for (i = 1, c = 'a'; i <= num_args; i++, c++) {
      out << c << ", ";
    }
    out << "__FILE__, __func__, __LINE__)\n";
  } else { /* Without arguments */
    out << ") " << "__" << m_name << "(__FILE__, __func__, __LINE__)\n";
  }

  return ret;
}
#endif

int
APIDef::MakeHeaderFiles(int server) {
  char filename[MAX_FILENAME_LEN+1];
  sprintf(filename, "%s%s", m_lowerid.c_str(),
    (server ? SERVER_HEADER_FILE : CLIENT_HEADER_FILE));
  int ret = 0;

  fstream out(filename, ios::out);
  if (!out.is_open()) {
    cout << "ERROR: File " << filename << " Could not open.\n";
    return 1;
  }
  cout << "info: Header file " << filename << " Creating.\n";

  const char *define_str =
    server ? SERVER_HEADER_DEFINE : CLIENT_HEADER_DEFINE;
  const char *title = server ? SERVER_HEADER_TITLE : CLIENT_HEADER_TITLE;

  out << "#ifndef _" << m_lowerid << define_str << "\n";
  out << "#define _" << m_lowerid << define_str << "\n";
  out << "/*\n";
  out << " * " << m_id << " " << title << " " << filename << "\n";
  out << " *\n";
  out << RPCTOOL_WARNING_STRING << "\n";
  out << " */\n\n";

  list<string>::iterator i;
  for (i = m_headers.begin(); i != m_headers.end(); ++i) {
    out << "#include <" << *i << ">\n";
  }
  out << "\n";

  if (server) {

    out << EXTERN_C_START << "\n";

      out << RPC_API_DISPATCH_RETURN << " "
    << m_id << RPC_API_DISPATCH_FUNC_FULL << ";\n";

      out << "#ifdef RPC_DISPATCH_FUNC\n"
    << "#undef RPC_DISPATCH_FUNC\n"
    << "#define RPC_DISPATCH_FUNC " << m_id  << RPC_API_DISPATCH_FUNC_NAME
    << "\n#else /* !RPC_DISPATCH_FUNC */\n"
    << "#error \"Please include <rpc.h> first!!\"\n"
    << "#endif /* !RPC_DISPATCH_FUNC */\n\n";

    out << EXTERN_C_END << "\n";

  }
#ifdef DBG_ENABLE
  else if (rpc_log_enable) {
    /* DEBUG INFORMATION EMBEDDED MACRO */
    out << "/* Debug information embedded macro definition */\n";
    list<Function>::iterator f;
    for (f = m_funcs.begin(); f != m_funcs.end(); ++f) {
      if (f->PrintMacro(out) != 0) {
        ret = 1;
        break;
      }
    }
    out << "\n";
  }
#endif

  out << EXTERN_C_START << "\n";

  out << "/* API definitions */\n";
  list<Function>::iterator f;
  for (f = m_funcs.begin(); f != m_funcs.end(); ++f) {
    if (f->PrintPrototype(out, server) != 0) {
      ret = 1;
      break;
    }
  }
  out << "\n";

  out << EXTERN_C_END << "\n";

  out << "#endif /* _" << m_lowerid << define_str << " */\n";
  out.close();
  return ret;
}

int
Function::PrintServerStub(fstream& out) {
  int num_args = NumOfArgs();

  out << "\tcase " << RPC_API_NUM_PREFIX << m_name << ": {\n";

  list<Arg>::iterator a = m_args.begin();
  for (int i = 1; i <= num_args; ++i, ++a) {
    const char *type = TypeCodeString(a->Code());
    if (type == NULL) {
      type = (a->TypeName()).c_str();
    }
    if (type == NULL) {
      return 1;
    }
    out << "\t\t" << type << " ";
    if (a->Bytes() > 0 || a->IsPointer()) {/* Pointer-passing argument */
      if (a->Bytes() > 0) {
  out << "v" << i << " = NULL;\n";
      } else if (a->IsPointer()) {
  out << "*v" << i << " = NULL;\n";
      }
    } else {/* Pass-by-value argument */
      out << "v" << i << ";\n";
    }
  }
  if (num_args > 0) {
    out << "\t\tif (" << RPC_DEMARSHALL_FUNCTION
  << " (args_string, args_size, 1, " << num_args << ",\n";
    /* In the STUB of servers for all pre-call demarshall */
    /* Passing Pointers to Local Variables in Server STUB */
    /* Pointer/ Buffer will alloc the required space in demarshall_arguments() */

    a = m_args.begin();
    for (int i = 1; i <= num_args; ++i, ++a) {
      if (a->IsVararray() != 0) { /* Variable-length arrays */
        a->CreateDemarshallArgs(out, 1, i, m_array_size_name);
      } else {
        a->CreateDemarshallArgs(out, 1, i);
      }
      if (i < num_args) {
  out << ",";
      }
      out << "\n";
    }
    out << "\t\t   ) < 0) {\n";
    out << "\t\t\tgoto _" << m_name << "_Error;\n";
    out << "\t\t}\n";
  }

  out << "\t\tresult = " << m_name << "(";
  for (int i = 1; i <= num_args; i++) {
    out << "v" << i;
    if (i < num_args) {
      out << ", ";
    }
  }
  out << ");\n";

  int to_process = NumOfInOutArgs(RPC_OUT_ARG);
  if (to_process > 0) {
    /* Only OUT-arguments marshall after the server stub is called. */
    out << "\t\t/*if (result == RPC_OK) {*/\n";
    out << "\t\t    *ret_string = " << RPC_MARSHALL_FUNCTION
  << "(ret_size, 0, " << to_process << ",\n";

    a = m_args.begin();
    int processed = 0;
    int ret;
    for (int i = 1; i <= num_args; ++i, ++a) {
      if (a->IsVararray() != 0) { /* Variable-length arrays */
  ret = a->CreateMarshallArgs(out, i, RPC_OUT_ARG, m_array_size_name);
      } else {
  ret = a->CreateMarshallArgs(out, i, RPC_OUT_ARG);
      }
      if (ret) {
  processed++;
  if (processed < to_process) {
    out << ",";
  }
  out << "\n";
      }
    }
    out << "\t\t   );\n"
  << "\t\t    if (*ret_string == NULL) {\n"
  << "\t\t\tresult = " << RPC_API_SERVER_ERROR << ";\n"
  << "\t\t    }\n"
  << "\t\t/*}*/\n";
  }

  if (num_args > 0) {
    out << "_" << m_name << "_Error:\n";
  }
  int num_pointer_args = 0;
  a = m_args.begin();
  for (int i = 1; i <= num_args; ++i, ++a) {
    if (a->Bytes() || a->IsPointer()) {
      num_pointer_args++;
    }
  }
  if (num_pointer_args > 0) {
    int processed = 0;
    out << "\t\t" << RPC_MARSHALL_FREE_FUNCTION
  << "(" << num_pointer_args << ", ";
    a = m_args.begin();
    for (int i = 1; i <= num_args; ++i, ++a) {
      if (a->Bytes() || a->IsPointer()) {
  out << "v" << i;
  processed++;
  if (processed < num_pointer_args) {
    out << ", ";
  }
      }
    }
    out << ");\n";
  }
  out << "\t\tbreak;\n\t}\n";
  return 0;
}

int
APIDef::MakeServerStub(void) {
  char filename[MAX_FILENAME_LEN+1];
  sprintf(filename, "%s%s", m_lowerid.c_str(), SERVER_STUB_FILE);
  int ret = 0;

  fstream out(filename, ios::out);
  if (!out.is_open()) {
    cout << "File " << filename << " Could not open.\n";
    return 1;
  }
  cout << "info: Stub file " << filename << " Creating.\n";

  const char *title = SERVER_STUB_TITLE;

  out << "/*\n";
  out << " * " << m_id << " " << title << " " << filename << "\n";
  out << " *\n";
  out << RPCTOOL_WARNING_STRING << "\n";
  out << " */\n";
  out << "#include <" RPC_GLOBAL_HEADER_FILE << ">\n";
  out << "#include \"" << m_lowerid << SERVER_HEADER_FILE << "\"\n";
  out << "#include <netinet/in.h> /* for ntohs() */\n\n";

  int api_num = 1;
  list<Function>::iterator f;

  for (f = m_funcs.begin(); f != m_funcs.end(); ++f, ++api_num) {
    if (api_num >= (1<<16)) {
      cout << "Too many API functions.(Up to 65535)\n";
      return 1;
    }
    out << "#define " << RPC_API_NUM_PREFIX << f->Name()
  << " " << api_num << "\n";
  }
  out << "\n";

  out << "RPC_Result\n";
  out << m_id << RPC_API_DISPATCH_FUNC_FULL << "\n";
  out << "{\n";
  out << "\tRPC_Result result = " << RPC_API_SERVER_ERROR << ";\n";
  out << "\t*ret_string = NULL;\n";
  out << "\tswitch(api_num) {\n";

  for (f = m_funcs.begin(); f != m_funcs.end(); ++f) {
    if (f->PrintServerStub(out) != 0) {
      ret = 1;
      break;
    }
  }

  if (ret != 0) {
    return ret;
  }

  out << "\tdefault:\n\t\tbreak;\n\t}\n";
  out << "\treturn result;\n";
  out << "}\n";

  return ret;
}

int
Function::PrintClientStub(const char *moduleid, fstream& out) {
  int ret = 0;

  list<Arg>::iterator a;
  /* Function Names and Arguments */
  out << RPC_API_CLIENT_RETURN << "\n";
#ifdef DBG_ENABLE
  if (rpc_log_enable) {
    out << "__" << m_name << "(";
  } else {
    out << m_name << "(";
  }
#else
  out << m_name << "(";
#endif
  int num_args = (int)(m_args.size());
  if (num_args > 0) {
    a = m_args.begin();
    for (int i = 1; i <= num_args; ++i, ++a) {
      if (a->PrintPrototype(out, i)) {
  return 1;
      }
#ifdef DBG_ENABLE
      if (rpc_log_enable) {
        out << ", ";
      } else {
        if (i < num_args) {
          out << ", ";
        }
      }
#else
      if (i < num_args) {
        out << ", ";
      }
#endif
    }
  } else {
#ifndef DBG_ENABLE
    out << "void";
#else
    if (!rpc_log_enable) {
      out << "void";
    }
#endif
  }
#ifdef DBG_ENABLE
  if (rpc_log_enable) {
    out << "const char *filename, const char *funcname, int line ";
  }
#endif
  out << ")\n";

  out << "{\n";

  /* Contents of stub functions */

  /* If the argument is present and the variable-length array does not exist */
  if ((num_args > 0) && (m_array_size_pos == 0)) {
    int is_exist_mytype = 0;
    /* Restricted specifications */
    out << "#ifdef RPC_STATIC_ASSERT\n";
    out << "\tchar RPC_TOTAL_ARGSIZE_ERROR_in_" << m_name << "[\n";
    out << "\t(((";
    a = m_args.begin();
    for (int i = 1; i <= num_args; ++i, ++a) {
      if (i > 1) {
        out << " + ";
      }
      out << "sizeof(";
      a->PrintUndecoratedName(out, i);
      out << ")";
      /* For user types */
      if ((!is_exist_mytype) && (NULL == TypeCodeString(a->Code()))) {
        is_exist_mytype = 1;
      }
    }
    out << ") > RPC_MAX_API_ARG_TOTAL_SIZE) ? -1: 1)\n";
    out << "\t]__attribute__((unused));\n";

    /* Have a user type */
    if (is_exist_mytype) {
      char c[3];
      a = m_args.begin();
      for (int i = 1; i <= num_args; ++i, ++a) {
        if (NULL == TypeCodeString(a->Code())) {
          sprintf(c, "%d", i);
          out << "\tchar RPC_ARGSIZE_ERROR_in_" << m_name << "_arg" << c;
          out << "[\n\t(sizeof(";
          a->PrintUndecoratedName(out, i);
          out << ") > RPC_MAX_API_ARG_SIZE) ? -1: 1\n";
          out << "\t]__attribute__((unused));\n";
        }
      }
    }

    out << "#endif /* RPC_STATIC_ASSERT */\n";

  }

  out << "\tRPC_Result result = " << RPC_API_CLIENT_ERROR << ";\n";
  out << "\tunsigned int args_size = 0, ret_size;\n";
  out << "\tchar retcode[9];\n";

  if (num_args > 0) {
    /* Advance preparation==Marshalling of arguments */
    out << "\tchar *args_string = " << RPC_MARSHALL_FUNCTION
  << "(&args_size, 1, " << num_args << ",\n";
    /* In the clients STUB for all pre-call marshall */
    a = m_args.begin();
    for (int i = 1; i <= num_args; ++i, ++a) {
      if (a->IsVararray() != 0) { /* Variable-length arrays */
  a->CreateMarshallArgs(out, i, m_array_size_name);
      } else {
        a->CreateMarshallArgs(out, i);
      }
      if (i < num_args) {
  out << ",";
      }
      out << "\n";
    }
    out << "\t);\n";
    out << "\tif (args_string == NULL) {\n"
  << "\t\tgoto _" << m_name << "_Error;\n\t}\n";
  } else {
    out << "\tchar *args_string = NULL;\n";
  }

#ifdef DBG_ENABLE
  if (rpc_log_enable) {
    out << "\tif (" << moduleid << "_record_dbg_log(filename, funcname, line, \"";
    out << m_name << "\") != 0) {\n";
    out << "\t\tgoto _" << m_name << "_Error;\n\t}\n";
  }
#endif

  /* RPC API call */
  out << "\tchar *ret_string = NULL; /* pgr0431 */\n";
#ifdef RPC_STRING_ID
  out << "\tresult=RPC_API_call(\"" << moduleid << "\", ";
#else
  out << "\tresult=RPC_API_call(" << moduleid << "_RPC_ID, ";
#endif
  out << RPC_API_NUM_PREFIX << m_name;
  out << ", args_string, args_size, &ret_string, &ret_size);\n";

  // 2007.08.25 To avoid over run within the sscanf
  // Locally copies strings needed for determining return values and uses strtol instead of sscanf
  // (Since there is no guarantee that the byte sequence to be grabbed by communication is 0-terminated)
  out << "\n"
      << "\tif (result == RPC_OK && ret_string != NULL) {\n"
      << "\t    strncpy(retcode, ret_string, 8);\n"
      << "\t    retcode[8] = '\\0';\n"
      << "\t    result = (RPC_Result)strtoul(retcode, NULL, 16);\n";

  /* Post-processing==Arguments de-marshalling, free() */
  int num_of_out_args = NumOfInOutArgs(RPC_OUT_ARG);
  if (num_of_out_args > 0) {
    out << "\t    if (" << RPC_DEMARSHALL_FUNCTION
  << "(ret_string + RPC_RETCODE_LEN, ret_size - RPC_RETCODE_LEN, 0, "
  << num_of_out_args << ",\n";

    a = m_args.begin();
    int processed = 0;
    int ret;
    for (int i = 1; i <= num_args; ++i, ++a) {
      if (a->IsVararray() != 0) { /* Variable-length arrays */
        ret = a->CreateDemarshallArgs(out, 0, i, RPC_OUT_ARG, m_array_size_name);
      } else {
        ret = a->CreateDemarshallArgs(out, 0, i, RPC_OUT_ARG);
      }
      if (ret) {
  processed++;
  if (processed < num_of_out_args) {
    out << ",";
  }
  out << "\n";
      }
    }
    out << "\t   ) < 0) {\n"
  << "\t\tresult = " << RPC_API_CLIENT_ERROR << ";\n"
  << "\t    }\n";
  }

  out << "\t} else {\n\t    //result = " << RPC_API_CLIENT_ERROR << ";\n"
      << "\t}\n";

  out << "\t" << RPC_RETURN_FREE_FUNCTION << "(ret_string);\n";
  out << "\t" << RPC_MARSHALL_FREE_FUNCTION << "(1, args_string);\n";

  /* END, RETURN */
#ifndef DBG_ENABLE
  if (num_args)
#else
  if ((num_args)||(rpc_log_enable))
#endif
  {
    out << "_" << m_name << "_Error:\n";
  }

  out << "\treturn result;\n}\n";

  return ret;
}

int
APIDef::MakeClientStub(void) {
  char filename[MAX_FILENAME_LEN+1];
  sprintf(filename, "%s%s", m_lowerid.c_str(), CLIENT_STUB_FILE);
  int ret = 0;

  fstream out(filename, ios::out);
  if (!out.is_open()) {
    cout << "File " << filename << " Could not open.\n";
    return 1;
  }
  cout << "info: Stub file " << filename << " Creating.\n";

  const char *title = CLIENT_STUB_TITLE;

  out << "/*\n";
  out << " * " << m_id << " " << title << " " << filename << "\n";
  out << " *\n";
  out << RPCTOOL_WARNING_STRING << "\n";
  out << " */\n";
  out << "#include <" RPC_GLOBAL_HEADER_FILE << ">\n";
  out << "#include <" << m_lowerid << CLIENT_HEADER_FILE << ">\n\n";
  out << "#include <stdio.h> /* for sscanf() */\n";
  out << "#include <stdlib.h> /* for getenv() */\n";
  out << "#include <string.h> /* for strncpy() */\n";
  out << "#include <netinet/in.h> /* for ntohs() */\n\n";

  int api_num = 1;
  list<Function>::iterator f;
  for (f = m_funcs.begin(); f != m_funcs.end(); ++f, ++api_num) {
    if (api_num >= (1<<16)) {
      cout << "Too many API functions.(Up to 65535)\n";
      return 1;
    }
    out << "#define " << RPC_API_NUM_PREFIX << f->Name()
  << " " << api_num << "\n";
  }
  out << "\n";

  /* Specification Restriction Debug Constants */
  out << "/*#define RPC_STATIC_ASSERT*/\n";
  out << "\n";

#ifdef DBG_ENABLE
  if (rpc_log_enable) {
    int m_id_work_cnt;
    string m_id_work = m_id;
    for (m_id_work_cnt = 0; m_id_work_cnt < m_id_work.length(); m_id_work_cnt++) {
      m_id_work[m_id_work_cnt] = toupper(m_id_work[m_id_work_cnt]);
    }
    out << "int\n" << m_id << "_record_dbg_log(const char *filename, " <<
    "const char *funcname, int line, const char *apiname)\n";
    out << "{\n\t";
    out << "if (getenv(\"" << m_id_work << "_RPC_LOG\") != NULL) {\n\t\t";
    out << "return RPC_record_dbg_log(filename, funcname, line, apiname);\n\t";
    out << "}\n\t";
    out << "return 0;\n";
    out << "}\n\n";
  }
#endif

  /* API definitions */
  for (f = m_funcs.begin(); f != m_funcs.end(); ++f) {
    if (f->PrintClientStub(m_id.c_str(), out) != 0) {
      ret = 1;
      break;
    }
  }
  return ret;
}

int
Arg::IsArraySize(void) {
  if (m_is_array_size != 0) {
    if (IsTypeCodeNumeric(m_code) == 1) {
      return 1;
    } else {
      cout << "Variable-length array length specification variables must be integers.\n";
      return -1;
    }
  }
  return 0;
}

int
Function::CheckFuncArraySize(void) {
  int num_args = (int)(m_args.size());
  list<Arg>::iterator a;
  a = m_args.begin();
  for (int i = 1; i <= num_args; ++i, ++a) {
    int ret = a->IsArraySize();
    if (ret > 0) {
      if (m_array_size_pos != 0)
      {
        cout << "Two or more variable array length specification arguments exist.\n";
        return 1;
      }
      m_array_size_pos = i;
      a->GetUndecolatedName(m_array_size_name, i);
    } else if (ret < 0) {
      return 1;
    }
  }
  return 0;
}

int
APIDef::CheckAllArraySize(void) {
  list<Function>::iterator f;
  for (f = m_funcs.begin(); f != m_funcs.end(); ++f) {
    if (f->CheckFuncArraySize() != 0) {
      return 1;
    }
  }
  return 0;
}

int
APIDef::MakeStubs(void) {
  if (m_id.size() == 0) {
    cout << "The module name is not specified.\n";
    return 1;
  }

  if (m_id.size() > MAX_FILENAME_LEN - strlen(SERVER_STUB_FILE)) {
    cout << "The module name is too long.\n";
    return 1;
  }

  /* Pre-examine the ARRAYSIZE specification */
  //cout << "<check_all_array_size>\n";
  if (CheckAllArraySize() != 0) {
    return 1;
  }

  //cout << "<MakeHeaderFiles(0)>\n";
  if (MakeHeaderFiles(1) != 0) {
    return 1;
  }

  //cout << "<MakeHeaderFiles(1)>\n";
  if (MakeHeaderFiles(0) != 0) {
    return 1;
  }

  if (MakeServerStub() != 0) {
    return 1;
  }

  if (MakeClientStub() != 0) {
    return 1;
  }

  return 0;
}

void
Arg::Print(void) {
  cout << "\t";
  if (m_name.size() > 0) {
    cout << "Variable name=" << m_name << " ";
  }
  cout << "Type=" << TypeCodeString(m_code) << "In bytes=" << m_bytes << " ";
  if (m_is_pointer) {
    cout << "Pointer ";
  }
  if ((m_in_out & RPC_OUT_ARG) != 0) {
    cout << "Output ";
  }
  cout << "\n";
}

void
Function::Print(int with_args) {
  printf("Function name=%s\n", m_name.c_str());
  if (with_args) {
    list<Arg>::iterator a;
    for (a = m_args.begin(); a != m_args.end(); ++a) {
      a->Print();
    }
  }
}

void
APIDef::Print(int with_args) {
  list<Function>::iterator f;
  for (f = m_funcs.begin(); f != m_funcs.end(); ++f) {
    f->Print(with_args);
  }
}

void
APIDef::IdTolower(void) {
  char *lower = new char[m_id.size() + 1];
  strcpy(lower, m_id.c_str());
  char *p;
  for (p = lower; *p; p++) {
    *p = (char)(tolower(*p));
  }
  m_lowerid = lower;
  delete[] lower;
}