XINU
Functions
addargs.c File Reference

XINUシェルが作成したコマンドプロセスのスタックに引数argv(任意個)のローカルコピーを追加する。 More...

#include <xinu.h>
#include "shprototypes.h"
Include dependency graph for addargs.c:

Go to the source code of this file.

Functions

status addargs (pid32 pid, int32 ntok, int32 tok[], int32 tlen, char *tokbuf, void *dummy)
 XINUシェルが作成したコマンドプロセスのスタックに引数argv(任意個)のローカルコピーを追加する。 More...
 

Detailed Description

XINUシェルが作成したコマンドプロセスのスタックに引数argv(任意個)のローカルコピーを追加する。

Definition in file addargs.c.

Function Documentation

◆ addargs()

status addargs ( pid32  pid,
int32  ntok,
int32  tok[],
int32  tlen,
char *  tokbuf,
void *  dummy 
)

XINUシェルが作成したコマンドプロセスのスタックに引数argv(任意個)のローカルコピーを追加する。

Step1. 割り込みを禁止する。
Step2. 「引数の数が0以下」もしくは「トークンバッファの中にあるデータの長さが0以下」であれば、
割り込み状態を復元し、処理を修正する。
Step3. args配列と引数文字列(トークンバッファ)を続けて格納するために、プロセススタックの最下位の場所を計算する。
4Byte単位での丸め計算も行うため、args配列はユーザスタック最下位かその次の4byteの倍数アドレスから始まる。
最下位アドレス:[トークン1へのポインタ][トークン2へのポインタ][NULL][トークン1][NULL][トークン2]...:最上位アドレス
Step4. 文字列は可変長のため、args配列を超えた最初の位置を計算する。
Step5. args配列に格納されたポインタ(各引数へのポインタ)に対して、文字列領域開始アドレスをオフセットとして加え、
スタック最下位から順に格納する。 Step6. args配列に格納されたポインタの後に、区切りとしてNULLを付与する。
Step7. 引数文字列をargs配列(+ NULL)を超えた位置にコピーする。
Step8. プロセススタック中の第2引数(XINUシェルがプロセスをcreate()した際にスタックに追加した引数であり、
引数dummyと同じアドレスを持つ)を探し、第2引数の内容をプロセススタック中のargs配列アドレスに置換する。
Step9. 割り込みを許可状態に復元する。

Parameters
[in]pid使用するプロセスID
[in]ntok引数の数
[in]tokトークンバッファの中にあるトークンのインデックス
[in]tlenトークンバッファの中にあるデータの長さ
[in]tokbufNULLで終わるトークン配列
[in,out]dummy生成時に使用されるダミー引数。引数の配列へのポインタで置き換える必要がある。
Returns

Definition at line 33 of file addargs.c.

References disable(), memcpy(), NULL, OK, proctab, procent::prstkbase, procent::prstklen, procent::prstkptr, restore(), and SYSERR.

Referenced by shell().

34 {
35  intmask mask; /* Saved interrupt mask */
36  struct procent *prptr; /* Ptr to process' table entry */
37  uint32 aloc; /* Argument location in process stack as an integer */
38  uint32 *argloc; /* Location in process's stack to place args vector */
39  char *argstr; /* Location in process's stack to place arg strings */
40  uint32 *search; /* pointer that searches for dummy argument on stack */
41  uint32 *aptr; /* Walks through args array */
42  int32 i; /* Index into tok array */
43 
44  mask = disable();
45 
46  /* Check argument count and data length */
47 
48  if ((ntok <= 0) || (tlen < 0))
49  {
50  restore(mask);
51  return SYSERR;
52  }
53 
54  prptr = &proctab[pid];
55 
56  /* Compute lowest location in the process stack where the */
57  /* args array will be stored followed by the argument */
58  /* strings */
59 
60  aloc = (uint32)(prptr->prstkbase - prptr->prstklen + sizeof(uint32));
61  argloc = (uint32 *)((aloc + 3) & ~0x3); /* round multiple of 4 */
62 
63  /* Compute the first location beyond args array for the strings */
64 
65  argstr = (char *)(argloc + (ntok + 1)); /* +1 for a null ptr */
66 
67  /* Set each location in the args vector to be the address of */
68  /* string area plus the offset of this argument */
69 
70  for (aptr = argloc, i = 0; i < ntok; i++)
71  {
72  *aptr++ = (uint32)(argstr + tok[i]);
73  }
74 
75  /* Add a null pointer to the args array */
76 
77  *aptr++ = (uint32)NULL;
78 
79  /* Copy the argument strings from tokbuf into process's stack */
80  /* just beyond the args vector */
81 
82  memcpy(aptr, tokbuf, tlen);
83 
84  /* Find the second argument in process's stack */
85 
86  for (search = (uint32 *)prptr->prstkptr;
87  search < (uint32 *)prptr->prstkbase; search++)
88  {
89 
90  /* If found, replace with the address of the args vector*/
91 
92  if (*search == (uint32)dummy)
93  {
94  *search = (uint32)argloc;
95  restore(mask);
96  return OK;
97  }
98  }
99 
100  /* Argument value not found on the stack - report an error */
101 
102  restore(mask);
103  return SYSERR;
104 }
#define NULL
連結リスト用のNULLポインタ
Definition: kernel.h:68
void restore(intmask)
#define SYSERR
処理が失敗した場合
Definition: kernel.h:79
#define OK
処理が成功した場合
Definition: kernel.h:77
uint32 prstklen
Bytesで表されたスタックの長さ(最大値。Byte)。
Definition: process.h:96
char * prstkbase
ランタイムスタックの基点(メモリ領域で最上位のアドレス)。
Definition: process.h:94
uint32 intmask
保存された割り込みマスク
Definition: kernel.h:38
int int32
符号あり32ビット整数(int)
Definition: kernel.h:11
struct procent proctab[]
プロセステーブル。
Definition: initialize.c:23
プロセステーブル(32bitsの倍数)。
Definition: process.h:85
unsigned int uint32
符号なし32ビット整数(unsigned int)
Definition: kernel.h:15
void * memcpy(void *, const void *, int32)
メモリAの領域(source)からメモリBの領域(Destination)にN Byteコピーする。
Definition: memcpy.c:13
intmask disable(void)
割り込み禁止(intr.Sに定義がある)
char * prstkptr
保存されたスタックポインタ。
Definition: process.h:92
Here is the call graph for this function:
Here is the caller graph for this function: